Skip to content
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

Socket download function improvements #785

Merged
merged 2 commits into from
Dec 4, 2023
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
51 changes: 40 additions & 11 deletions libraries/SocketWrapper/src/SocketHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,37 +125,66 @@ void MbedSocketClass::feedWatchdog() {

void MbedSocketClass::body_callback(const char* data, uint32_t data_len) {
feedWatchdog();
fwrite(data, 1, data_len, download_target);
fwrite(data, sizeof(data[0]), data_len, download_target);
}

int MbedSocketClass::download(char* url, const char* target_file, bool const is_https) {
int MbedSocketClass::download(const char* url, const char* target_file, bool const is_https) {
download_target = fopen(target_file, "wb");

int res = this->download(url, is_https, mbed::callback(this, &MbedSocketClass::body_callback));

fclose(download_target);
download_target = nullptr;

return res;
}

int MbedSocketClass::download(const char* url, bool const is_https, mbed::Callback<void(const char*, uint32_t)> cbk) {
if(cbk == nullptr) {
return 0; // a call back must be set
}

HttpRequest* req_http = nullptr;
HttpsRequest* req_https = nullptr;
HttpResponse* rsp = nullptr;
int res=0;
std::vector<string*> header_fields;

if (is_https) {
req_https = new HttpsRequest(getNetwork(), nullptr, HTTP_GET, url, mbed::callback(this, &MbedSocketClass::body_callback));
req_https = new HttpsRequest(getNetwork(), nullptr, HTTP_GET, url, cbk);
rsp = req_https->send(NULL, 0);
if (rsp == NULL) {
fclose(download_target);
return req_https->get_error();
res = req_https->get_error();
goto exit;
}
} else {
req_http = new HttpRequest(getNetwork(), HTTP_GET, url, mbed::callback(this, &MbedSocketClass::body_callback));
req_http = new HttpRequest(getNetwork(), HTTP_GET, url, cbk);
rsp = req_http->send(NULL, 0);
if (rsp == NULL) {
fclose(download_target);
return req_http->get_error();
res = req_http->get_error();
goto exit;
}
}

while (!rsp->is_message_complete()) {
delay(10);
}

int const size = ftell(download_target);
fclose(download_target);
return size;
// find the header containing the "Content-Length" value and return that
header_fields = rsp->get_headers_fields();
for(int i=0; i<header_fields.size(); i++) {

if(strcmp(header_fields[i]->c_str(), "Content-Length") == 0) {
res = std::stoi(*rsp->get_headers_values()[i]);
break;
}
}

exit:
if(req_http) delete req_http;
if(req_https) delete req_https;
// no need to delete rsp, it is already deleted by deleting the request
// this may be harmful since it can allow dangling pointers existence

return res;
}
20 changes: 18 additions & 2 deletions libraries/SocketWrapper/src/SocketHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,24 @@ class MbedSocketClass {
IPAddress dnsIP(int n = 0);

virtual NetworkInterface* getNetwork() = 0;

int download(char* url, const char* target, bool const is_https = false);

/*
* Download a file from an HTTP endpoint and save it in the provided `target` location on the fs
* The parameter cbk can be used to perform actions on the buffer before saving on the fs
*
* return: on success the size of the downloaded file, on error -status code
*/
int download(
const char* url, const char* target, bool const is_https = false);
/*
* Download a file from an HTTP endpoint and handle the body of the request on a callback
* passed as an argument
*
* return: on success the size of the downloaded file, on error -status code
*/
int download(
const char* url, bool const is_https = false,
mbed::Callback<void(const char*, uint32_t)> cbk = nullptr);

int hostByName(const char* aHostname, IPAddress& aResult);

Expand Down