@@ -32,7 +32,7 @@ void HttpClient::resetState()
32
32
{
33
33
iState = eIdle;
34
34
iStatusCode = 0 ;
35
- iContentLength = 0 ;
35
+ iContentLength = kNoContentLengthHeader ;
36
36
iBodyLengthConsumed = 0 ;
37
37
iContentLengthPtr = kContentLengthPrefix ;
38
38
iHttpResponseTimeout = kHttpResponseTimeout ;
@@ -521,12 +521,35 @@ String HttpClient::responseBody()
521
521
522
522
if (bodyLength > 0 )
523
523
{
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
+ }
525
529
}
526
530
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)
528
536
{
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 );
530
553
}
531
554
532
555
return response;
@@ -663,6 +686,7 @@ int HttpClient::readHeader()
663
686
// Just in case we get multiple Content-Length headers, this
664
687
// will ensure we just get the value of the last one
665
688
iContentLength = 0 ;
689
+ iBodyLengthConsumed = 0 ;
666
690
}
667
691
}
668
692
else if ((iContentLengthPtr == kContentLengthPrefix ) && (c == ' \r ' ))
0 commit comments