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

Merge pull request #7 from sandeepmistry/response-body

Make HttpClient::responseBody more robust

Tested LGTM
This commit is contained in:
Arturo Guadalupi 2016-12-16 16:51:25 +01:00 committed by GitHub
commit b8820741df

View File

@ -32,7 +32,7 @@ void HttpClient::resetState()
{ {
iState = eIdle; iState = eIdle;
iStatusCode = 0; iStatusCode = 0;
iContentLength = 0; iContentLength = kNoContentLengthHeader;
iBodyLengthConsumed = 0; iBodyLengthConsumed = 0;
iContentLengthPtr = kContentLengthPrefix; iContentLengthPtr = kContentLengthPrefix;
iHttpResponseTimeout = kHttpResponseTimeout; iHttpResponseTimeout = kHttpResponseTimeout;
@ -521,12 +521,35 @@ String HttpClient::responseBody()
if (bodyLength > 0) 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; return response;
@ -663,6 +686,7 @@ int HttpClient::readHeader()
// Just in case we get multiple Content-Length headers, this // Just in case we get multiple Content-Length headers, this
// will ensure we just get the value of the last one // will ensure we just get the value of the last one
iContentLength = 0; iContentLength = 0;
iBodyLengthConsumed = 0;
} }
} }
else if ((iContentLengthPtr == kContentLengthPrefix) && (c == '\r')) else if ((iContentLengthPtr == kContentLengthPrefix) && (c == '\r'))