Skip to content

Make HttpClient::responseBody more robust #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2016

Conversation

sandeepmistry
Copy link

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.

In order to perform the following check, a patched version of String is needed (see arduino/ArduinoCore-samd#153 and arduino/Arduino#5127):


String response;
int statusCode = 0;

// ...

void loop() {
  client.get("/");

  statusCode = client.responseStatusCode();
  response = client.responseBody();

  if (response) {
    Serial.print("Response: ");
    Serial.println(response);
  } else {
    Serial.println("Reading response failed");
  }
}

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.
@zonque
Copy link

zonque commented Aug 22, 2016

Hmm, I don't think the library can rely on behavior of a patched core class.

@@ -686,6 +686,7 @@ int HttpClient::readHeader()
// Just in case we get multiple Content-Length headers, this
// will ensure we just get the value of the last one
iContentLength = 0;
iBodyLengthConsumed = 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale of this, does it fix a bug?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was to reset state when a HttpClient is reused.

@sandeepmistry
Copy link
Author

@zonque

Hmm, I don't think the library can rely on behavior of a patched core class.

For cores without the fix something like this can be used:

String response = client.responseBody();
if (httpClient.contentLength() > response.length()) {
  // reading response body failed
} else {
  Serial.print("Response: ");
  Serial.println(response);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants