Skip to content

Commit 3bc3a87

Browse files
committed
Web server: wait for data to arrive
Web server: wait for request body until either contentLength is received, or no data is received within 1000ms interval.
2 parents f3b6ec1 + b72cf2c commit 3bc3a87

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

libraries/ESP8266WebServer/src/Parsing.cpp

+22-8
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,28 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
138138

139139
if (!isForm){
140140
if (searchStr != "") searchStr += '&';
141-
//some clients send headers first and data after (like we do)
142-
//give them a chance
143-
int tries = 100;//100ms max wait
144-
while(!client.available() && tries--)delay(1);
145-
size_t plainLen = client.available();
146-
char *plainBuf = (char*)malloc(plainLen+1);
147-
client.readBytes(plainBuf, plainLen);
148-
plainBuf[plainLen] = '\0';
141+
char *plainBuf = nullptr;
142+
size_t plainLen = 0;
143+
do
144+
{
145+
//some clients send headers first and data after (like we do)
146+
//give them a chance
147+
int tries = 1000;//1000ms max wait
148+
size_t newLen;
149+
while( !(newLen = client.available()) && tries--) delay(1);
150+
if (!newLen) break;
151+
plainBuf = (plainBuf == nullptr) ? (char *) malloc(newLen + 1) : (char *) realloc(plainBuf, plainLen + newLen + 1);
152+
client.readBytes(&plainBuf[plainLen], newLen);
153+
plainLen += newLen;
154+
plainBuf[plainLen] = '\0';
155+
} while (plainLen < contentLength);
156+
/* if data loss, exit */
157+
if (plainBuf == nullptr) return false;
158+
if (plainLen < contentLength)
159+
{
160+
free(plainBuf);
161+
return false;
162+
}
149163
#ifdef DEBUG_ESP_HTTP_SERVER
150164
DEBUG_OUTPUT.print("Plain: ");
151165
DEBUG_OUTPUT.println(plainBuf);

0 commit comments

Comments
 (0)