Skip to content

Commit 53287f4

Browse files
committed
rework sendRequest stream too
1 parent bd7d915 commit 53287f4

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,12 @@ int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
331331
int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
332332

333333
if(!stream) {
334-
return HTTPC_ERROR_NO_STREAM;
334+
return returnError(HTTPC_ERROR_NO_STREAM);
335335
}
336336

337337
// connect to server
338338
if(!connect()) {
339-
return HTTPC_ERROR_CONNECTION_REFUSED;
339+
return returnError(HTTPC_ERROR_CONNECTION_REFUSED);
340340
}
341341

342342
if(size > 0) {
@@ -345,7 +345,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
345345

346346
// send Header
347347
if(!sendHeader(type)) {
348-
return HTTPC_ERROR_SEND_HEADER_FAILED;
348+
return returnError(HTTPC_ERROR_SEND_HEADER_FAILED);
349349
}
350350

351351
int buff_size = HTTP_TCP_BUFFER_SIZE;
@@ -371,25 +371,68 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
371371
while(connected() && (stream->available() > -1) && (len > 0 || len == -1)) {
372372

373373
// get available data size
374-
int s = stream->available();
374+
int sizeAvailable = stream->available();
375375

376-
if(len) {
377-
s = ((s > len) ? len : s);
378-
}
376+
if(sizeAvailable) {
377+
378+
int readBytes = sizeAvailable;
379+
380+
// read only the asked bytes
381+
if(len > 0 && readBytes > len) {
382+
readBytes = len;
383+
}
384+
385+
// not read more the buffer can handle
386+
if(readBytes > buff_size) {
387+
readBytes = buff_size;
388+
}
379389

380-
if(s) {
381-
int c = stream->readBytes(buff, ((s > buff_size) ? buff_size : s));
390+
// read data
391+
int bytesRead = stream->readBytes(buff, readBytes);
382392

383393
// write it to Stream
384-
int w = _tcp->write((const uint8_t *) buff, c);
385-
bytesWritten += w;
386-
if(w != c) {
387-
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] short write asked for %d but got %d\n", c, w);
388-
break;
394+
int bytesWrite = _tcp->write((const uint8_t *) buff, bytesRead);
395+
bytesWritten += bytesWrite;
396+
397+
// are all Bytes a writen to stream ?
398+
if(bytesWrite != bytesRead) {
399+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] short write, asked for %d but got %d retry...\n", bytesRead, bytesWrite);
400+
401+
// check for write error
402+
if(_tcp->getWriteError()) {
403+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] stream write error %d\n", _tcp->getWriteError());
404+
405+
//reset write error for retry
406+
_tcp->clearWriteError();
407+
}
408+
409+
// some time for the stream
410+
delay(1);
411+
412+
int leftBytes = (readBytes - bytesWrite);
413+
414+
// retry to send the missed bytes
415+
bytesWrite = _tcp->write((const uint8_t *) (buff + bytesWrite), leftBytes);
416+
bytesWritten += bytesWrite;
417+
418+
if(bytesWrite != leftBytes) {
419+
// failed again
420+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] short write, asked for %d but got %d failed.\n", leftBytes, bytesWrite);
421+
free(buff);
422+
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
423+
}
424+
}
425+
426+
// check for write error
427+
if(_tcp->getWriteError()) {
428+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] stream write error %d\n", _tcp->getWriteError());
429+
free(buff);
430+
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
389431
}
390432

433+
// count bytes to read left
391434
if(len > 0) {
392-
len -= c;
435+
len -= readBytes;
393436
}
394437

395438
delay(0);
@@ -403,18 +446,18 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
403446
if(size && (int) size != bytesWritten) {
404447
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, size);
405448
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
406-
return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
449+
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
407450
} else {
408451
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
409452
}
410453

411454
} else {
412455
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] too less ram! need %d\n", HTTP_TCP_BUFFER_SIZE);
413-
return HTTPC_ERROR_TOO_LESS_RAM;
456+
return returnError(HTTPC_ERROR_TOO_LESS_RAM);
414457
}
415458

416459
// handle Server Response (Header)
417-
return handleHeaderResponse();
460+
return returnError(handleHeaderResponse());
418461
}
419462

420463
/**
@@ -851,7 +894,7 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size) {
851894
int readBytes = sizeAvailable;
852895

853896
// read only the asked bytes
854-
if(readBytes > len) {
897+
if(len > 0 && readBytes > len) {
855898
readBytes = len;
856899
}
857900

@@ -899,6 +942,7 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size) {
899942
// check for write error
900943
if(stream->getWriteError()) {
901944
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStreamDataBlock] stream write error %d\n", stream->getWriteError());
945+
free(buff);
902946
return HTTPC_ERROR_STREAM_WRITE;
903947
}
904948

0 commit comments

Comments
 (0)