Skip to content

Commit

Permalink
Fix timeout in WebServer::_uploadReadByte and set timeout handleClien…
Browse files Browse the repository at this point in the history
…t() (#457)

Fixes: espressif#9990

Co-authored-by: TD-er <[email protected]>
  • Loading branch information
Jason2866 and TD-er authored Jul 5, 2024
1 parent 3652346 commit 458ad90
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
38 changes: 33 additions & 5 deletions libraries/WebServer/src/Parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,41 @@ void WebServer::_uploadWriteByte(uint8_t b) {

int WebServer::_uploadReadByte(NetworkClient &client) {
int res = client.read();

if (res < 0) {
while (!client.available() && client.connected()) {
delay(2);
}
// keep trying until you either read a valid byte or timeout
const unsigned long startMillis = millis();
const long timeoutIntervalMillis = client.getTimeout();
bool timedOut = false;
for(;;) {
if (!client.connected()) return -1;
// loosely modeled after blinkWithoutDelay pattern
while(!timedOut && !client.available() && client.connected()){
delay(2);
timedOut = (millis() - startMillis) >= timeoutIntervalMillis;
}

res = client.read();
res = client.read();
if(res >= 0) {
return res; // exit on a valid read
}
// NOTE: it is possible to get here and have all of the following
// assertions hold true
//
// -- client.available() > 0
// -- client.connected == true
// -- res == -1
//
// a simple retry strategy overcomes this which is to say the
// assertion is not permanent, but the reason that this works
// is elusive, and possibly indicative of a more subtle underlying
// issue

timedOut = (millis() - startMillis) >= timeoutIntervalMillis;
if (timedOut) {
return res; // exit on a timeout
}
}
}

return res;
Expand Down
4 changes: 1 addition & 3 deletions libraries/WebServer/src/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,8 @@ void WebServer::handleClient() {
case HC_WAIT_READ:
// Wait for data from client to become available
if (_currentClient.available()) {
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT); /* / 1000 removed, WifiClient setTimeout changed to ms */
if (_parseRequest(_currentClient)) {
// because HTTP_MAX_SEND_WAIT is expressed in milliseconds,
// it must be divided by 1000
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT); /* / 1000 removed, WifiClient setTimeout changed to ms */
_contentLength = CONTENT_LENGTH_NOT_SET;
_handleRequest();

Expand Down

0 comments on commit 458ad90

Please sign in to comment.