Skip to content

Use HTTPS-GET instead of HTTP-GET for download API #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/WiFi/src/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 36 additions & 5 deletions libraries/WiFi/src/WiFiHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 13 additions & 4 deletions libraries/WiFi/src/utility/https_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,22 @@ class HttpsRequest : public HttpRequestBase {
const char* ssl_ca_pem,
http_method method,
const char* url,
Callback<void(const char *at, uint32_t length)> body_callback = 0)
mbed::Callback<void(const char *at, uint32_t length)> 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;
}

Expand All @@ -76,7 +82,7 @@ class HttpsRequest : public HttpRequestBase {
HttpsRequest(TLSSocket* socket,
http_method method,
const char* url,
Callback<void(const char *at, uint32_t length)> body_callback = 0)
mbed::Callback<void(const char *at, uint32_t length)> body_callback = 0)
: HttpRequestBase(socket, body_callback)
{
_parsed_url = new ParsedUrl(url);
Expand All @@ -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);
}
};

Expand Down