Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Socket download function improvements#785

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
facchinm merged 2 commits intoarduino:mainfromandreagilardoni:socket-additions
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionslibraries/SocketWrapper/src/SocketHelpers.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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(constchar* 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 deletionslibraries/SocketWrapper/src/SocketHelpers.h
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp