|
21 | 21 |
|
22 | 22 | static FILE* target;
|
23 | 23 |
|
24 |
| -void body_callback(const char* data, uint32_t data_len) { |
| 24 | +void body_callback(const char* data, uint32_t data_len) |
| 25 | +{ |
25 | 26 | fwrite(data, 1, data_len, target);
|
26 | 27 | }
|
27 | 28 |
|
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 | +{ |
29 | 31 | 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 |
32 | 47 | {
|
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 | + } |
35 | 54 | }
|
| 55 | + |
| 56 | + while (!rsp->is_message_complete()) { |
| 57 | + delay(10); |
| 58 | + } |
| 59 | + |
36 | 60 | int const size = ftell(target);
|
37 | 61 | fclose(target);
|
38 | 62 | return size;
|
|
0 commit comments