arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
282 stars 170 forks source link

Make HttpClient::responseBody more robust #7

Closed sandeepmistry closed 7 years ago

sandeepmistry commented 8 years ago

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 https://github.com/arduino/ArduinoCore-samd/pull/153 and https://github.com/arduino/Arduino/pull/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");
  }
}
zonque commented 8 years ago

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

sandeepmistry commented 7 years ago

@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);
}