Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make HttpClient::responseBody more robust
Return invalidated String if memory allocation fails or content length
does not match body data length. Also, use timed reads to support
responses without a content length.
  • Loading branch information
sandeepmistry committed Jul 12, 2016
commit fe46191445476bfc650878a019a7d34a650844de
31 changes: 27 additions & 4 deletions src/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void HttpClient::resetState()
{
iState = eIdle;
iStatusCode = 0;
iContentLength = 0;
iContentLength = kNoContentLengthHeader;
iBodyLengthConsumed = 0;
iContentLengthPtr = kContentLengthPrefix;
iHttpResponseTimeout = kHttpResponseTimeout;
Expand Down Expand Up @@ -521,12 +521,35 @@ String HttpClient::responseBody()

if (bodyLength > 0)
{
response.reserve(bodyLength);
// try to reserve bodyLength bytes
if (response.reserve(bodyLength) == 0) {
// String reserve failed
return String((const char*)NULL);
}
}

while (available())
// keep on timedRead'ing, until:
// - we have a content length: body length equals consumed or no bytes
// available
// - no content length: no bytes are available
while (iBodyLengthConsumed != bodyLength)
{
response += (char)read();
int c = timedRead();

if (c == -1) {
// read timed out, done
break;
}

if (!response.concat((char)c)) {
// adding char failed
return String((const char*)NULL);
}
}

if (bodyLength > 0 && (unsigned int)bodyLength != response.length()) {
// failure, we did not read in reponse content length bytes
return String((const char*)NULL);
}

return response;
Expand Down