Skip to content

Commit

Permalink
Reworked HttpClient::read() so it doesn't hit the bug in WiFiClient::…
Browse files Browse the repository at this point in the history
…read(...) (the mulit-byte version)
  • Loading branch information
amcewen committed Sep 3, 2012
1 parent 8bca4a3 commit ea1618b
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,46 +309,49 @@ int HttpClient::responseStatusCode()
if (available())
{
c = read();
switch(iState)
if (c != -1)
{
case eRequestSent:
// We haven't reached the status code yet
if ( (*statusPtr == '*') || (*statusPtr == c) )
switch(iState)
{
// This character matches, just move along
statusPtr++;
if (*statusPtr == '\0')
case eRequestSent:
// We haven't reached the status code yet
if ( (*statusPtr == '*') || (*statusPtr == c) )
{
// We've reached the end of the prefix
iState = eReadingStatusCode;
// This character matches, just move along
statusPtr++;
if (*statusPtr == '\0')
{
// We've reached the end of the prefix
iState = eReadingStatusCode;
}
}
}
else
{
return HTTP_ERROR_INVALID_RESPONSE;
}
break;
case eReadingStatusCode:
if (isdigit(c))
{
// This assumes we won't get more than the 3 digits we
// want
iStatusCode = iStatusCode*10 + (c - '0');
}
else
{
// We've reached the end of the status code
// We could sanity check it here or double-check for ' '
// rather than anything else, but let's be lenient
iState = eStatusCodeRead;
}
break;
case eStatusCodeRead:
// We're just waiting for the end of the line now
break;
};
// We read something, reset the timeout counter
timeoutStart = millis();
else
{
return HTTP_ERROR_INVALID_RESPONSE;
}
break;
case eReadingStatusCode:
if (isdigit(c))
{
// This assumes we won't get more than the 3 digits we
// want
iStatusCode = iStatusCode*10 + (c - '0');
}
else
{
// We've reached the end of the status code
// We could sanity check it here or double-check for ' '
// rather than anything else, but let's be lenient
iState = eStatusCodeRead;
}
break;
case eStatusCodeRead:
// We're just waiting for the end of the line now
break;
};
// We read something, reset the timeout counter
timeoutStart = millis();
}
}
else
{
Expand Down Expand Up @@ -430,6 +433,7 @@ bool HttpClient::endOfBodyReached()

int HttpClient::read()
{
#if 0 // Fails on WiFi because multi-byte read seems to be broken
uint8_t b[1];
int ret = read(b, 1);
if (ret == 1)
Expand All @@ -440,6 +444,19 @@ int HttpClient::read()
{
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)
Expand Down

0 comments on commit ea1618b

Please sign in to comment.