Skip to content

Commit ea1618b

Browse files
committed
Reworked HttpClient::read() so it doesn't hit the bug in WiFiClient::read(...) (the mulit-byte version)
1 parent 8bca4a3 commit ea1618b

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

HttpClient.cpp

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -309,46 +309,49 @@ int HttpClient::responseStatusCode()
309309
if (available())
310310
{
311311
c = read();
312-
switch(iState)
312+
if (c != -1)
313313
{
314-
case eRequestSent:
315-
// We haven't reached the status code yet
316-
if ( (*statusPtr == '*') || (*statusPtr == c) )
314+
switch(iState)
317315
{
318-
// This character matches, just move along
319-
statusPtr++;
320-
if (*statusPtr == '\0')
316+
case eRequestSent:
317+
// We haven't reached the status code yet
318+
if ( (*statusPtr == '*') || (*statusPtr == c) )
321319
{
322-
// We've reached the end of the prefix
323-
iState = eReadingStatusCode;
320+
// This character matches, just move along
321+
statusPtr++;
322+
if (*statusPtr == '\0')
323+
{
324+
// We've reached the end of the prefix
325+
iState = eReadingStatusCode;
326+
}
324327
}
325-
}
326-
else
327-
{
328-
return HTTP_ERROR_INVALID_RESPONSE;
329-
}
330-
break;
331-
case eReadingStatusCode:
332-
if (isdigit(c))
333-
{
334-
// This assumes we won't get more than the 3 digits we
335-
// want
336-
iStatusCode = iStatusCode*10 + (c - '0');
337-
}
338-
else
339-
{
340-
// We've reached the end of the status code
341-
// We could sanity check it here or double-check for ' '
342-
// rather than anything else, but let's be lenient
343-
iState = eStatusCodeRead;
344-
}
345-
break;
346-
case eStatusCodeRead:
347-
// We're just waiting for the end of the line now
348-
break;
349-
};
350-
// We read something, reset the timeout counter
351-
timeoutStart = millis();
328+
else
329+
{
330+
return HTTP_ERROR_INVALID_RESPONSE;
331+
}
332+
break;
333+
case eReadingStatusCode:
334+
if (isdigit(c))
335+
{
336+
// This assumes we won't get more than the 3 digits we
337+
// want
338+
iStatusCode = iStatusCode*10 + (c - '0');
339+
}
340+
else
341+
{
342+
// We've reached the end of the status code
343+
// We could sanity check it here or double-check for ' '
344+
// rather than anything else, but let's be lenient
345+
iState = eStatusCodeRead;
346+
}
347+
break;
348+
case eStatusCodeRead:
349+
// We're just waiting for the end of the line now
350+
break;
351+
};
352+
// We read something, reset the timeout counter
353+
timeoutStart = millis();
354+
}
352355
}
353356
else
354357
{
@@ -430,6 +433,7 @@ bool HttpClient::endOfBodyReached()
430433

431434
int HttpClient::read()
432435
{
436+
#if 0 // Fails on WiFi because multi-byte read seems to be broken
433437
uint8_t b[1];
434438
int ret = read(b, 1);
435439
if (ret == 1)
@@ -440,6 +444,19 @@ int HttpClient::read()
440444
{
441445
return -1;
442446
}
447+
#else
448+
int ret = iClient->read();
449+
if (ret >= 0)
450+
{
451+
if (endOfHeadersReached() && iContentLength > 0)
452+
{
453+
// We're outputting the body now and we've seen a Content-Length header
454+
// So keep track of how many bytes are left
455+
iBodyLengthConsumed++;
456+
}
457+
}
458+
return ret;
459+
#endif
443460
}
444461

445462
int HttpClient::read(uint8_t *buf, size_t size)

0 commit comments

Comments
 (0)