Skip to content

Update HTTPClient.cpp to send big payload #7638

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
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
37 changes: 25 additions & 12 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the HTTPClient for Arduino.
* Port to ESP32 by Evandro Luis Copercini (2017),
* changed fingerprints to CA verification.
* changed fingerprints to CA verification.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -604,6 +604,9 @@ int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size)

if(payload && size > 0) {
addHeader(F("Content-Length"), String(size));
} else {
// force 0 content length for redirect request
addHeader(F("Content-Length"), String("0"));
}

// add cookies to header, if present
Expand All @@ -619,10 +622,20 @@ 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) {
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
// send the payload per 1000 bytes
log_d("Sending %u byte ...\n", size);
size_t sent_bytes = 0;
size_t _chunkSize = 1000;

while(sent_bytes < size){
size_t sent = _client->write(&payload[sent_bytes], std::min(size - sent_bytes, _chunkSize));
if (sent < 0){
log_e("Failed to send chunk!");
break;
}
sent_bytes += sent;
}
}
}

code = handleHeaderResponse();
log_d("sendRequest code=%d\n", code);
Expand Down Expand Up @@ -1143,15 +1156,15 @@ bool HTTPClient::connect(void)
log_d("transport level verify failed");
_client->stop();
return false;
}
}
#endif
if(!_client->connect(_host.c_str(), _port, _connectTimeout)) {
log_d("failed connect to %s:%u", _host.c_str(), _port);
return false;
}

// set Timeout for WiFiClient and for Stream::readBytesUntil() and Stream::readStringUntil()
_client->setTimeout((_tcpTimeout + 500) / 1000);
_client->setTimeout((_tcpTimeout + 500) / 1000);

log_d(" connected to %s:%u", _host.c_str(), _port);

Expand Down Expand Up @@ -1250,7 +1263,7 @@ int HTTPClient::handleHeaderResponse()
log_v("RX: '%s'", headerLine.c_str());

if(firstLine) {
firstLine = false;
firstLine = false;
if(_canReuse && headerLine.startsWith("HTTP/1.")) {
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
}
Expand Down Expand Up @@ -1382,10 +1395,10 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
if(readBytes > buff_size) {
readBytes = buff_size;
}
// stop if no more reading
if (readBytes == 0)
break;
// stop if no more reading
if (readBytes == 0)
break;

// read data
int bytesRead = _client->readBytes(buff, readBytes);
Expand Down Expand Up @@ -1677,7 +1690,7 @@ bool HTTPClient::generateCookieString(String *cookieString)
{
return false;
}
for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
if ((c->max_age.valid && ((c->date + c->max_age.duration) < now_gmt)) || (!c->max_age.valid && c->expires.valid && c->expires.date < now_gmt)) {
_cookieJar->erase(c);
c--;
Expand Down