Skip to content

allow HTTPClient to handle large payloads, in case the client (such as WifiClientSecure) does not #6969

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 7 additions & 2 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,13 @@ int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size)
}

// send Payload if needed
if(payload && size > 0) {
if(_client->write(&payload[0], size) != size) {
if(payload && (size > 0)) {
size_t totalSent = 0;
while(_client->connected() && (totalSent < size))
{
totalSent += _client->write(&payload[totalSent], (size - totalSent));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if write returns 0 and client is still connected, that would mean that the socket is not ready to be written to. I suggest you catch this situation and delay a bit before retrying. Currently the code will try in a busy loop and on single core chips (or when code runs on the same core and WiFi) this is not a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

Suggested change
totalSent += _client->write(&payload[totalSent], (size - totalSent));
const size_t thisSend = _client->write(&payload[totalSent], (size - totalSent));
totalSent += thisSend;
if(thisSend == 0) {
delay(10);
}

Should we add a delay? Or just bail out if write returns 0? I can't remember, but maybe I thought client->write was blocking?

}
if(totalSent != size) {
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
}
}
Expand Down