@@ -32,7 +32,7 @@ void HttpClient::resetState()
3232{
3333 iState = eIdle;
3434 iStatusCode = 0 ;
35- iContentLength = 0 ;
35+ iContentLength = kNoContentLengthHeader ;
3636 iBodyLengthConsumed = 0 ;
3737 iContentLengthPtr = kContentLengthPrefix ;
3838 iHttpResponseTimeout = kHttpResponseTimeout ;
@@ -521,12 +521,35 @@ String HttpClient::responseBody()
521521
522522 if (bodyLength > 0 )
523523 {
524- response.reserve (bodyLength);
524+ // try to reserve bodyLength bytes
525+ if (response.reserve (bodyLength) == 0 ) {
526+ // String reserve failed
527+ return String ((const char *)NULL );
528+ }
525529 }
526530
527- while (available ())
531+ // keep on timedRead'ing, until:
532+ // - we have a content length: body length equals consumed or no bytes
533+ // available
534+ // - no content length: no bytes are available
535+ while (iBodyLengthConsumed != bodyLength)
528536 {
529- response += (char )read ();
537+ int c = timedRead ();
538+
539+ if (c == -1 ) {
540+ // read timed out, done
541+ break ;
542+ }
543+
544+ if (!response.concat ((char )c)) {
545+ // adding char failed
546+ return String ((const char *)NULL );
547+ }
548+ }
549+
550+ if (bodyLength > 0 && (unsigned int )bodyLength != response.length ()) {
551+ // failure, we did not read in reponse content length bytes
552+ return String ((const char *)NULL );
530553 }
531554
532555 return response;
@@ -663,6 +686,7 @@ int HttpClient::readHeader()
663686 // Just in case we get multiple Content-Length headers, this
664687 // will ensure we just get the value of the last one
665688 iContentLength = 0 ;
689+ iBodyLengthConsumed = 0 ;
666690 }
667691 }
668692 else if ((iContentLengthPtr == kContentLengthPrefix ) && (c == ' \r ' ))
0 commit comments