Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fbf34a2

Browse files
Merge pull request #126 from arduino/fix-https-download
Use HTTPS-GET instead of HTTP-GET for download API
2 parents 81939d9 + ad8b8d5 commit fbf34a2

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

‎libraries/WiFi/src/WiFi.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ 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);
291+
int download(char* url, const char* target, boolconst is_https = false);
292292

293293
friend class WiFiClient;
294294
friend class WiFiServer;

‎libraries/WiFi/src/WiFiHelpers.cpp‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,47 @@
1717
#include "WiFi.h"
1818
#include "mbed.h"
1919
#include "utility/http_request.h"
20+
#include "utility/https_request.h"
2021

2122
static FILE* target;
2223

23-
void body_callback(const char* data, uint32_t data_len) {
24+
void body_callback(const char* data, uint32_t data_len)
25+
{
2426
fwrite(data, 1, data_len, target);
2527
}
2628

27-
int WiFiClass::download(char* url, const char* target_file) {
29+
int WiFiClass::download(char* url, const char* target_file, bool const is_https)
30+
{
2831
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+
33+
HttpRequest * req_http = nullptr;
34+
HttpsRequest * req_https = nullptr;
35+
HttpResponse * rsp = nullptr;
36+
37+
if (is_https)
38+
{
39+
req_https = new HttpsRequest(getNetwork(), nullptr, HTTP_GET, url, &body_callback);
40+
rsp = req_https->send(NULL, 0);
41+
if (rsp == NULL) {
42+
fclose(target);
43+
return req_https->get_error();
44+
}
45+
}
46+
else
47+
{
48+
req_http = new HttpRequest(getNetwork(), HTTP_GET, url, &body_callback);
49+
rsp = req_http->send(NULL, 0);
50+
if (rsp == NULL) {
51+
fclose(target);
52+
return req_http->get_error();
53+
}
54+
}
55+
56+
while (!rsp->is_message_complete()) {
57+
delay(10);
58+
}
59+
60+
int const size = ftell(target);
61+
fclose(target);
62+
return size;
3263
}

‎libraries/WiFi/src/utility/https_request.h‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,22 @@ class HttpsRequest : public HttpRequestBase {
4949
const char* ssl_ca_pem,
5050
http_method method,
5151
const char* url,
52-
Callback<void(const char *at, uint32_t length)> body_callback = 0)
52+
mbed::Callback<void(const char *at, uint32_t length)> body_callback = 0)
5353
: HttpRequestBase(NULL, body_callback)
5454
{
55+
_error = 0;
56+
_network = network;
57+
5558
_parsed_url = new ParsedUrl(url);
5659
_request_builder = new HttpRequestBuilder(method, _parsed_url);
5760
_response = NULL;
5861

5962
_socket = new TLSSocket();
6063
((TLSSocket*)_socket)->open(network);
61-
((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem);
64+
if (ssl_ca_pem)
65+
((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem);
66+
else
67+
((TLSSocket*)_socket)->set_root_ca_cert("/wlan/", 0);
6268
_we_created_socket = true;
6369
}
6470

@@ -76,7 +82,7 @@ class HttpsRequest : public HttpRequestBase {
7682
HttpsRequest(TLSSocket* socket,
7783
http_method method,
7884
const char* url,
79-
Callback<void(const char *at, uint32_t length)> body_callback = 0)
85+
mbed::Callback<void(const char *at, uint32_t length)> body_callback = 0)
8086
: HttpRequestBase(socket, body_callback)
8187
{
8288
_parsed_url = new ParsedUrl(url);
@@ -91,7 +97,10 @@ class HttpsRequest : public HttpRequestBase {
9197

9298
protected:
9399
virtual nsapi_error_t connect_socket(char *host, uint16_t port) {
94-
return ((TLSSocket*)_socket)->connect(host, port);
100+
SocketAddress socketAddress = SocketAddress();
101+
socketAddress.set_port(port);
102+
_network->gethostbyname(host, &socketAddress);
103+
return ((TLSSocket*)_socket)->connect(socketAddress);
95104
}
96105
};
97106

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /