@@ -264,7 +264,7 @@ void HTTPClient::setAuthorization(const char * auth) {
264
264
265
265
/* *
266
266
* set the timeout for the TCP connection
267
- * @param timeout unsigned int
267
+ * @param timeout unsigned int
268
268
*/
269
269
void HTTPClient::setTimeout (uint16_t timeout) {
270
270
_tcpTimeout = timeout;
@@ -273,14 +273,12 @@ void HTTPClient::setTimeout(uint16_t timeout) {
273
273
}
274
274
}
275
275
276
-
277
-
278
276
/* *
279
277
* use HTTP1.0
280
278
* @param timeout
281
279
*/
282
280
void HTTPClient::useHTTP10 (bool useHTTP10) {
283
- _useHTTP10 = useHTTP10;
281
+ _useHTTP10 = useHTTP10;
284
282
}
285
283
286
284
/* *
@@ -305,6 +303,16 @@ int HTTPClient::POST(String payload) {
305
303
return POST ((uint8_t *) payload.c_str (), payload.length ());
306
304
}
307
305
306
+ /* *
307
+ * sendRequest
308
+ * @param type const char * "GET", "POST", ....
309
+ * @param payload String data for the message body
310
+ * @return
311
+ */
312
+ int HTTPClient::sendRequest (const char * type, String payload) {
313
+ return sendRequest (type, (uint8_t *) payload.c_str (), payload.length ());
314
+ }
315
+
308
316
/* *
309
317
* sendRequest
310
318
* @param type const char * "GET", "POST", ....
@@ -382,7 +390,6 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
382
390
// create buffer for read
383
391
uint8_t * buff = (uint8_t *) malloc (buff_size);
384
392
385
-
386
393
if (buff) {
387
394
// read all data from stream and send it to server
388
395
while (connected () && (stream->available () > -1 ) && (len > 0 || len == -1 )) {
@@ -461,8 +468,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
461
468
free (buff);
462
469
463
470
if (size && (int ) size != bytesWritten) {
464
- DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n " , bytesWritten, size);
465
- DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!" );
471
+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n " , bytesWritten, size); DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!" );
466
472
return returnError (HTTPC_ERROR_SEND_PAYLOAD_FAILED);
467
473
} else {
468
474
DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload written: %d\n " , bytesWritten);
@@ -819,17 +825,21 @@ int HTTPClient::handleHeaderResponse() {
819
825
if (!connected ()) {
820
826
return HTTPC_ERROR_NOT_CONNECTED;
821
827
}
828
+
822
829
String transferEncoding;
823
830
_returnCode = -1 ;
824
831
_size = -1 ;
825
832
_transferEncoding = HTTPC_TE_IDENTITY;
833
+ unsigned long lastDataTime = millis ();
826
834
827
835
while (connected ()) {
828
836
size_t len = _tcp->available ();
829
837
if (len > 0 ) {
830
838
String headerLine = _tcp->readStringUntil (' \n ' );
831
839
headerLine.trim (); // remove \r
832
840
841
+ lastDataTime = millis ();
842
+
833
843
DEBUG_HTTPCLIENT (" [HTTP-Client][handleHeaderResponse] RX: '%s'\n " , headerLine.c_str ());
834
844
835
845
if (headerLine.startsWith (" HTTP/1." )) {
@@ -885,15 +895,16 @@ int HTTPClient::handleHeaderResponse() {
885
895
}
886
896
887
897
} else {
898
+ if ((millis () - lastDataTime) > _tcpTimeout) {
899
+ return HTTPC_ERROR_READ_TIMEOUT;
900
+ }
888
901
delay (0 );
889
902
}
890
903
}
891
904
892
905
return HTTPC_ERROR_CONNECTION_LOST;
893
906
}
894
907
895
-
896
-
897
908
/* *
898
909
* write one Data Block to Stream
899
910
* @param stream Stream *
0 commit comments