Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c8ca2d

Browse files
committed
Merge pull request esp8266#1530 from Links2004/master
improve http client
2 parents 8fdd29e + 1060db9 commit 6c8ca2d

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

cores/esp8266/WMath.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@
2626
extern "C" {
2727
#include <stdlib.h>
2828
}
29+
#include "esp8266_peri.h"
2930

3031
void randomSeed(unsigned long seed) {
3132
if(seed != 0) {
32-
srand(seed);
33+
srand((seed ^ RANDOM_REG32));
3334
}
3435
}
3536

3637
long random(long howbig) {
3738
if(howbig == 0) {
3839
return 0;
3940
}
40-
return rand() % howbig;
41+
return (rand() ^ RANDOM_REG32) % howbig;
4142
}
4243

4344
long random(long howsmall, long howbig) {

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+20-9
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void HTTPClient::setAuthorization(const char * auth) {
264264

265265
/**
266266
* set the timeout for the TCP connection
267-
* @param timeout unsigned int
267+
* @param timeout unsigned int
268268
*/
269269
void HTTPClient::setTimeout(uint16_t timeout) {
270270
_tcpTimeout = timeout;
@@ -273,14 +273,12 @@ void HTTPClient::setTimeout(uint16_t timeout) {
273273
}
274274
}
275275

276-
277-
278276
/**
279277
* use HTTP1.0
280278
* @param timeout
281279
*/
282280
void HTTPClient::useHTTP10(bool useHTTP10) {
283-
_useHTTP10 = useHTTP10;
281+
_useHTTP10 = useHTTP10;
284282
}
285283

286284
/**
@@ -305,6 +303,16 @@ int HTTPClient::POST(String payload) {
305303
return POST((uint8_t *) payload.c_str(), payload.length());
306304
}
307305

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+
308316
/**
309317
* sendRequest
310318
* @param type const char * "GET", "POST", ....
@@ -382,7 +390,6 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
382390
// create buffer for read
383391
uint8_t * buff = (uint8_t *) malloc(buff_size);
384392

385-
386393
if(buff) {
387394
// read all data from stream and send it to server
388395
while(connected() && (stream->available() > -1) && (len > 0 || len == -1)) {
@@ -461,8 +468,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
461468
free(buff);
462469

463470
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!");
466472
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
467473
} else {
468474
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
@@ -819,17 +825,21 @@ int HTTPClient::handleHeaderResponse() {
819825
if(!connected()) {
820826
return HTTPC_ERROR_NOT_CONNECTED;
821827
}
828+
822829
String transferEncoding;
823830
_returnCode = -1;
824831
_size = -1;
825832
_transferEncoding = HTTPC_TE_IDENTITY;
833+
unsigned long lastDataTime = millis();
826834

827835
while(connected()) {
828836
size_t len = _tcp->available();
829837
if(len > 0) {
830838
String headerLine = _tcp->readStringUntil('\n');
831839
headerLine.trim(); // remove \r
832840

841+
lastDataTime = millis();
842+
833843
DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str());
834844

835845
if(headerLine.startsWith("HTTP/1.")) {
@@ -885,15 +895,16 @@ int HTTPClient::handleHeaderResponse() {
885895
}
886896

887897
} else {
898+
if((millis() - lastDataTime) > _tcpTimeout) {
899+
return HTTPC_ERROR_READ_TIMEOUT;
900+
}
888901
delay(0);
889902
}
890903
}
891904

892905
return HTTPC_ERROR_CONNECTION_LOST;
893906
}
894907

895-
896-
897908
/**
898909
* write one Data Block to Stream
899910
* @param stream Stream *

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class HTTPClient {
147147
int GET();
148148
int POST(uint8_t * payload, size_t size);
149149
int POST(String payload);
150+
int sendRequest(const char * type, String payload);
150151
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
151152
int sendRequest(const char * type, Stream * stream, size_t size = 0);
152153

0 commit comments

Comments
 (0)