Skip to content

Commit

Permalink
WiFi: add download helper
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Dec 21, 2020
1 parent aa86f00 commit 9ecd46f
Show file tree
Hide file tree
Showing 12 changed files with 4,165 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libraries/WiFi/src/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ class WiFiClass
int ping(const String &hostname, uint8_t ttl = 128);
int ping(IPAddress host, uint8_t ttl = 128);

int download(char* url, const char* target);

friend class WiFiClient;
friend class WiFiServer;
friend class WiFiUDP;
Expand Down
32 changes: 32 additions & 0 deletions libraries/WiFi/src/WiFiHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
WiFi.h - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "WiFi.h"
#include "mbed.h"
#include "utility/http_request.h"

static FILE* target;

void body_callback(const char* data, uint32_t data_len) {
fwrite(data, 1, data_len, target);
}

int WiFiClass::download(char* url, const char* target_file) {
target = fopen(target_file, "wb");
HttpRequest* req = new HttpRequest(getNetwork(), HTTP_GET, url, &body_callback);
req->send(NULL, 0);
fclose(target);
}
94 changes: 94 additions & 0 deletions libraries/WiFi/src/utility/http_parsed_url.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* PackageLicenseDeclared: Apache-2.0
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _MBED_HTTP_PARSED_URL_H_
#define _MBED_HTTP_PARSED_URL_H_

#include "http_parser/http_parser.h"

class ParsedUrl {
public:
ParsedUrl(const char* url) {
struct http_parser_url parsed_url;
http_parser_parse_url(url, strlen(url), false, &parsed_url);

for (size_t ix = 0; ix < UF_MAX; ix++) {
char* value;
if (parsed_url.field_set & (1 << ix)) {
value = (char*)calloc(parsed_url.field_data[ix].len + 1, 1);
memcpy(value, url + parsed_url.field_data[ix].off,
parsed_url.field_data[ix].len);
}
else {
value = (char*)calloc(1, 1);
}

switch ((http_parser_url_fields)ix) {
case UF_SCHEMA: _schema = value; break;
case UF_HOST: _host = value; break;
case UF_PATH: _path = value; break;
case UF_QUERY: _query = value; break;
case UF_USERINFO: _userinfo = value; break;
default:
// PORT is already parsed, FRAGMENT is not relevant for HTTP requests
free(value);
break;
}
}

_port = parsed_url.port;
if (!_port) {
if (strcmp(_schema, "https") == 0 || strcmp(_schema, "wss") == 0) {
_port = 443;
}
else {
_port = 80;
}
}

if (strcmp(_path, "") == 0) {
free(_path);
_path = (char*)calloc(2, 1);
_path[0] = '/';
}
}

~ParsedUrl() {
if (_schema) free(_schema);
if (_host) free(_host);
if (_path) free(_path);
if (_query) free(_query);
if (_userinfo) free(_userinfo);
}

uint16_t port() const { return _port; }
char* schema() const { return _schema; }
char* host() const { return _host; }
char* path() const { return _path; }
char* query() const { return _query; }
char* userinfo() const { return _userinfo; }

private:
uint16_t _port;
char* _schema;
char* _host;
char* _path;
char* _query;
char* _userinfo;
};

#endif // _MBED_HTTP_PARSED_URL_H_
23 changes: 23 additions & 0 deletions libraries/WiFi/src/utility/http_parser/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright
Igor Sysoev.

Additional changes are licensed under the same terms as NGINX and
copyright Joyent, Inc. and other Node contributors. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Loading

0 comments on commit 9ecd46f

Please sign in to comment.