diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index 4982503a7fd..a74b91dd74f 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -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 @@ -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 @@ -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); @@ -1143,7 +1156,7 @@ 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); @@ -1151,7 +1164,7 @@ bool HTTPClient::connect(void) } // 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); @@ -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'); } @@ -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); @@ -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--;