diff --git a/libraries/WiFi/src/WiFi.h b/libraries/WiFi/src/WiFi.h index 5352e98b5..6f9a6566a 100644 --- a/libraries/WiFi/src/WiFi.h +++ b/libraries/WiFi/src/WiFi.h @@ -288,7 +288,7 @@ 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); + int download(char* url, const char* target, bool const is_https = false); friend class WiFiClient; friend class WiFiServer; diff --git a/libraries/WiFi/src/WiFiHelpers.cpp b/libraries/WiFi/src/WiFiHelpers.cpp index 353515b30..3d1e35cc8 100644 --- a/libraries/WiFi/src/WiFiHelpers.cpp +++ b/libraries/WiFi/src/WiFiHelpers.cpp @@ -17,16 +17,47 @@ #include "WiFi.h" #include "mbed.h" #include "utility/http_request.h" +#include "utility/https_request.h" static FILE* target; -void body_callback(const char* data, uint32_t data_len) { +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) { +int WiFiClass::download(char* url, const char* target_file, bool const is_https) +{ target = fopen(target_file, "wb"); - HttpRequest* req = new HttpRequest(getNetwork(), HTTP_GET, url, &body_callback); - req->send(NULL, 0); - fclose(target); + + HttpRequest * req_http = nullptr; + HttpsRequest * req_https = nullptr; + HttpResponse * rsp = nullptr; + + if (is_https) + { + req_https = new HttpsRequest(getNetwork(), nullptr, HTTP_GET, url, &body_callback); + rsp = req_https->send(NULL, 0); + if (rsp == NULL) { + fclose(target); + return req_https->get_error(); + } + } + else + { + req_http = new HttpRequest(getNetwork(), HTTP_GET, url, &body_callback); + rsp = req_http->send(NULL, 0); + if (rsp == NULL) { + fclose(target); + return req_http->get_error(); + } + } + + while (!rsp->is_message_complete()) { + delay(10); + } + + int const size = ftell(target); + fclose(target); + return size; } diff --git a/libraries/WiFi/src/utility/https_request.h b/libraries/WiFi/src/utility/https_request.h index d5335ddb7..8458722ef 100644 --- a/libraries/WiFi/src/utility/https_request.h +++ b/libraries/WiFi/src/utility/https_request.h @@ -49,16 +49,22 @@ class HttpsRequest : public HttpRequestBase { const char* ssl_ca_pem, http_method method, const char* url, - Callback body_callback = 0) + mbed::Callback body_callback = 0) : HttpRequestBase(NULL, body_callback) { + _error = 0; + _network = network; + _parsed_url = new ParsedUrl(url); _request_builder = new HttpRequestBuilder(method, _parsed_url); _response = NULL; _socket = new TLSSocket(); ((TLSSocket*)_socket)->open(network); - ((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem); + if (ssl_ca_pem) + ((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem); + else + ((TLSSocket*)_socket)->set_root_ca_cert("/wlan/", 0); _we_created_socket = true; } @@ -76,7 +82,7 @@ class HttpsRequest : public HttpRequestBase { HttpsRequest(TLSSocket* socket, http_method method, const char* url, - Callback body_callback = 0) + mbed::Callback body_callback = 0) : HttpRequestBase(socket, body_callback) { _parsed_url = new ParsedUrl(url); @@ -91,7 +97,10 @@ class HttpsRequest : public HttpRequestBase { protected: virtual nsapi_error_t connect_socket(char *host, uint16_t port) { - return ((TLSSocket*)_socket)->connect(host, port); + SocketAddress socketAddress = SocketAddress(); + socketAddress.set_port(port); + _network->gethostbyname(host, &socketAddress); + return ((TLSSocket*)_socket)->connect(socketAddress); } };