From bf067f718a29a4c9b2226d404bdcb4dcf926b5f8 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Fri, 29 Jan 2016 13:17:10 +0100 Subject: [PATCH 1/3] make random more random --- cores/esp8266/WMath.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/WMath.cpp b/cores/esp8266/WMath.cpp index da46a3ddb..d56db8e5c 100644 --- a/cores/esp8266/WMath.cpp +++ b/cores/esp8266/WMath.cpp @@ -26,10 +26,11 @@ extern "C" { #include } +#include "esp8266_peri.h" void randomSeed(unsigned long seed) { if(seed != 0) { - srand(seed); + srand((seed ^ RANDOM_REG32)); } } @@ -37,7 +38,7 @@ long random(long howbig) { if(howbig == 0) { return 0; } - return rand() % howbig; + return (rand() ^ RANDOM_REG32) % howbig; } long random(long howsmall, long howbig) { From 27f1a63170d0cbdccea117a1480f6834dfd52de7 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Fri, 29 Jan 2016 13:19:25 +0100 Subject: [PATCH 2/3] allow String for payload in HTTPclient sendRequest --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 10 ++++++++++ libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h | 1 + 2 files changed, 11 insertions(+) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index edb09e668..6877f14ba 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -305,6 +305,16 @@ int HTTPClient::POST(String payload) { return POST((uint8_t *) payload.c_str(), payload.length()); } +/** + * sendRequest + * @param type const char * "GET", "POST", .... + * @param payload String data for the message body + * @return + */ +int HTTPClient::sendRequest(const char * type, String payload) { + return sendRequest(type, (uint8_t *) payload.c_str(), payload.length()); +} + /** * sendRequest * @param type const char * "GET", "POST", .... diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 704841a8c..0b865c2a6 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -147,6 +147,7 @@ class HTTPClient { int GET(); int POST(uint8_t * payload, size_t size); int POST(String payload); + int sendRequest(const char * type, String payload); int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0); int sendRequest(const char * type, Stream * stream, size_t size = 0); From 1060db94c200ae18425f069455257d6447a5c060 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Fri, 29 Jan 2016 14:02:09 +0100 Subject: [PATCH 3/3] handle possible dead lock in HTTP client see: #1520 --- .../src/ESP8266HTTPClient.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 6877f14ba..fd11c0360 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -264,7 +264,7 @@ void HTTPClient::setAuthorization(const char * auth) { /** * set the timeout for the TCP connection - * @param timeout unsigned int + * @param timeout unsigned int */ void HTTPClient::setTimeout(uint16_t timeout) { _tcpTimeout = timeout; @@ -273,14 +273,12 @@ void HTTPClient::setTimeout(uint16_t timeout) { } } - - /** * use HTTP1.0 * @param timeout */ void HTTPClient::useHTTP10(bool useHTTP10) { - _useHTTP10 = useHTTP10; + _useHTTP10 = useHTTP10; } /** @@ -392,7 +390,6 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { // create buffer for read uint8_t * buff = (uint8_t *) malloc(buff_size); - if(buff) { // read all data from stream and send it to server while(connected() && (stream->available() > -1) && (len > 0 || len == -1)) { @@ -471,8 +468,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { free(buff); if(size && (int) size != bytesWritten) { - 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!"); + 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!"); return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED); } else { DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten); @@ -829,10 +825,12 @@ int HTTPClient::handleHeaderResponse() { if(!connected()) { return HTTPC_ERROR_NOT_CONNECTED; } + String transferEncoding; _returnCode = -1; _size = -1; _transferEncoding = HTTPC_TE_IDENTITY; + unsigned long lastDataTime = millis(); while(connected()) { size_t len = _tcp->available(); @@ -840,6 +838,8 @@ int HTTPClient::handleHeaderResponse() { String headerLine = _tcp->readStringUntil('\n'); headerLine.trim(); // remove \r + lastDataTime = millis(); + DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str()); if(headerLine.startsWith("HTTP/1.")) { @@ -895,6 +895,9 @@ int HTTPClient::handleHeaderResponse() { } } else { + if((millis() - lastDataTime) > _tcpTimeout) { + return HTTPC_ERROR_READ_TIMEOUT; + } delay(0); } } @@ -902,8 +905,6 @@ int HTTPClient::handleHeaderResponse() { return HTTPC_ERROR_CONNECTION_LOST; } - - /** * write one Data Block to Stream * @param stream Stream *