1
0
mirror of https://github.com/arduino-libraries/ArduinoHttpClient.git synced 2025-04-19 21:22:15 +03:00

Reworked HttpClient::read() so it doesn't hit the bug in WiFiClient::read(...) (the mulit-byte version)

This commit is contained in:
amcewen 2012-09-03 18:28:59 +01:00
parent 8bca4a37d2
commit ea1618be0e

View File

@ -309,46 +309,49 @@ int HttpClient::responseStatusCode()
if (available()) if (available())
{ {
c = read(); c = read();
switch(iState) if (c != -1)
{ {
case eRequestSent: switch(iState)
// We haven't reached the status code yet
if ( (*statusPtr == '*') || (*statusPtr == c) )
{ {
// This character matches, just move along case eRequestSent:
statusPtr++; // We haven't reached the status code yet
if (*statusPtr == '\0') if ( (*statusPtr == '*') || (*statusPtr == c) )
{ {
// We've reached the end of the prefix // This character matches, just move along
iState = eReadingStatusCode; statusPtr++;
if (*statusPtr == '\0')
{
// We've reached the end of the prefix
iState = eReadingStatusCode;
}
} }
} else
else {
{ return HTTP_ERROR_INVALID_RESPONSE;
return HTTP_ERROR_INVALID_RESPONSE; }
} break;
break; case eReadingStatusCode:
case eReadingStatusCode: if (isdigit(c))
if (isdigit(c)) {
{ // This assumes we won't get more than the 3 digits we
// This assumes we won't get more than the 3 digits we // want
// want iStatusCode = iStatusCode*10 + (c - '0');
iStatusCode = iStatusCode*10 + (c - '0'); }
} else
else {
{ // We've reached the end of the status code
// We've reached the end of the status code // We could sanity check it here or double-check for ' '
// We could sanity check it here or double-check for ' ' // rather than anything else, but let's be lenient
// rather than anything else, but let's be lenient iState = eStatusCodeRead;
iState = eStatusCodeRead; }
} break;
break; case eStatusCodeRead:
case eStatusCodeRead: // We're just waiting for the end of the line now
// We're just waiting for the end of the line now break;
break; };
}; // We read something, reset the timeout counter
// We read something, reset the timeout counter timeoutStart = millis();
timeoutStart = millis(); }
} }
else else
{ {
@ -430,6 +433,7 @@ bool HttpClient::endOfBodyReached()
int HttpClient::read() int HttpClient::read()
{ {
#if 0 // Fails on WiFi because multi-byte read seems to be broken
uint8_t b[1]; uint8_t b[1];
int ret = read(b, 1); int ret = read(b, 1);
if (ret == 1) if (ret == 1)
@ -440,6 +444,19 @@ int HttpClient::read()
{ {
return -1; return -1;
} }
#else
int ret = iClient->read();
if (ret >= 0)
{
if (endOfHeadersReached() && iContentLength > 0)
{
// We're outputting the body now and we've seen a Content-Length header
// So keep track of how many bytes are left
iBodyLengthConsumed++;
}
}
return ret;
#endif
} }
int HttpClient::read(uint8_t *buf, size_t size) int HttpClient::read(uint8_t *buf, size_t size)