Skip to content

Commit 35d9a5c

Browse files
facchinmsebromero
authored andcommitted
WiFi: add download helper
1 parent fb6b51f commit 35d9a5c

12 files changed

+4165
-0
lines changed

libraries/WiFi/src/WiFi.h

+2
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ class WiFiClass
288288
int ping(const String &hostname, uint8_t ttl = 128);
289289
int ping(IPAddress host, uint8_t ttl = 128);
290290

291+
int download(char* url, const char* target);
292+
291293
friend class WiFiClient;
292294
friend class WiFiServer;
293295
friend class WiFiUDP;

libraries/WiFi/src/WiFiHelpers.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
WiFi.h - Library for Arduino Wifi shield.
3+
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
This library is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
Lesser General Public License for more details.
12+
You should have received a copy of the GNU Lesser General Public
13+
License along with this library; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15+
*/
16+
17+
#include "WiFi.h"
18+
#include "mbed.h"
19+
#include "utility/http_request.h"
20+
21+
static FILE* target;
22+
23+
void body_callback(const char* data, uint32_t data_len) {
24+
fwrite(data, 1, data_len, target);
25+
}
26+
27+
int WiFiClass::download(char* url, const char* target_file) {
28+
target = fopen(target_file, "wb");
29+
HttpRequest* req = new HttpRequest(getNetwork(), HTTP_GET, url, &body_callback);
30+
req->send(NULL, 0);
31+
fclose(target);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* PackageLicenseDeclared: Apache-2.0
3+
* Copyright (c) 2017 ARM Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef _MBED_HTTP_PARSED_URL_H_
19+
#define _MBED_HTTP_PARSED_URL_H_
20+
21+
#include "http_parser/http_parser.h"
22+
23+
class ParsedUrl {
24+
public:
25+
ParsedUrl(const char* url) {
26+
struct http_parser_url parsed_url;
27+
http_parser_parse_url(url, strlen(url), false, &parsed_url);
28+
29+
for (size_t ix = 0; ix < UF_MAX; ix++) {
30+
char* value;
31+
if (parsed_url.field_set & (1 << ix)) {
32+
value = (char*)calloc(parsed_url.field_data[ix].len + 1, 1);
33+
memcpy(value, url + parsed_url.field_data[ix].off,
34+
parsed_url.field_data[ix].len);
35+
}
36+
else {
37+
value = (char*)calloc(1, 1);
38+
}
39+
40+
switch ((http_parser_url_fields)ix) {
41+
case UF_SCHEMA: _schema = value; break;
42+
case UF_HOST: _host = value; break;
43+
case UF_PATH: _path = value; break;
44+
case UF_QUERY: _query = value; break;
45+
case UF_USERINFO: _userinfo = value; break;
46+
default:
47+
// PORT is already parsed, FRAGMENT is not relevant for HTTP requests
48+
free(value);
49+
break;
50+
}
51+
}
52+
53+
_port = parsed_url.port;
54+
if (!_port) {
55+
if (strcmp(_schema, "https") == 0 || strcmp(_schema, "wss") == 0) {
56+
_port = 443;
57+
}
58+
else {
59+
_port = 80;
60+
}
61+
}
62+
63+
if (strcmp(_path, "") == 0) {
64+
free(_path);
65+
_path = (char*)calloc(2, 1);
66+
_path[0] = '/';
67+
}
68+
}
69+
70+
~ParsedUrl() {
71+
if (_schema) free(_schema);
72+
if (_host) free(_host);
73+
if (_path) free(_path);
74+
if (_query) free(_query);
75+
if (_userinfo) free(_userinfo);
76+
}
77+
78+
uint16_t port() const { return _port; }
79+
char* schema() const { return _schema; }
80+
char* host() const { return _host; }
81+
char* path() const { return _path; }
82+
char* query() const { return _query; }
83+
char* userinfo() const { return _userinfo; }
84+
85+
private:
86+
uint16_t _port;
87+
char* _schema;
88+
char* _host;
89+
char* _path;
90+
char* _query;
91+
char* _userinfo;
92+
};
93+
94+
#endif // _MBED_HTTP_PARSED_URL_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright
2+
Igor Sysoev.
3+
4+
Additional changes are licensed under the same terms as NGINX and
5+
copyright Joyent, Inc. and other Node contributors. All rights reserved.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to
9+
deal in the Software without restriction, including without limitation the
10+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11+
sell copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23+
IN THE SOFTWARE.

0 commit comments

Comments
 (0)