diff --git a/libraries/ESP8266WebServer/src/Parsing.cpp b/libraries/ESP8266WebServer/src/Parsing.cpp index 7eac20def..519918308 100644 --- a/libraries/ESP8266WebServer/src/Parsing.cpp +++ b/libraries/ESP8266WebServer/src/Parsing.cpp @@ -138,14 +138,28 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { if (!isForm){ if (searchStr != "") searchStr += '&'; - //some clients send headers first and data after (like we do) - //give them a chance - int tries = 100;//100ms max wait - while(!client.available() && tries--)delay(1); - size_t plainLen = client.available(); - char *plainBuf = (char*)malloc(plainLen+1); - client.readBytes(plainBuf, plainLen); - plainBuf[plainLen] = '\0'; + char *plainBuf = nullptr; + size_t plainLen = 0; + do + { + //some clients send headers first and data after (like we do) + //give them a chance + int tries = 1000;//1000ms max wait + size_t newLen; + while( !(newLen = client.available()) && tries--) delay(1); + if (!newLen) break; + plainBuf = (plainBuf == nullptr) ? (char *) malloc(newLen + 1) : (char *) realloc(plainBuf, plainLen + newLen + 1); + client.readBytes(&plainBuf[plainLen], newLen); + plainLen += newLen; + plainBuf[plainLen] = '\0'; + } while (plainLen < contentLength); + /* if data loss, exit */ + if (plainBuf == nullptr) return false; + if (plainLen < contentLength) + { + free(plainBuf); + return false; + } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Plain: "); DEBUG_OUTPUT.println(plainBuf);