Skip to content

Commit ad8b8d5

Browse files
committed
Support both HTTP and HTTPS-GET
1 parent 8af1b19 commit ad8b8d5

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
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, bool const is_https = false);
292292

293293
friend class WiFiClient;
294294
friend class WiFiServer;

libraries/WiFi/src/WiFiHelpers.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,42 @@
2121

2222
static FILE* target;
2323

24-
void body_callback(const char* data, uint32_t data_len) {
24+
void body_callback(const char* data, uint32_t data_len)
25+
{
2526
fwrite(data, 1, data_len, target);
2627
}
2728

28-
int WiFiClass::download(char* url, const char* target_file) {
29+
int WiFiClass::download(char* url, const char* target_file, bool const is_https)
30+
{
2931
target = fopen(target_file, "wb");
30-
HttpsRequest* req = new HttpsRequest(getNetwork(), nullptr, HTTP_GET, url, &body_callback);
31-
if (req->send(NULL, 0) == NULL)
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
3247
{
33-
fclose(target);
34-
return req->get_error();
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+
}
3554
}
55+
56+
while (!rsp->is_message_complete()) {
57+
delay(10);
58+
}
59+
3660
int const size = ftell(target);
3761
fclose(target);
3862
return size;

0 commit comments

Comments
 (0)