Skip to content

Commit

Permalink
Revert size_t parameter for HttpRequest::getBody() and `HttpRespo…
Browse files Browse the repository at this point in the history
…nse::getBody()` methods. Use move semantics.
  • Loading branch information
mikee47 committed Oct 23, 2020
1 parent 46d6382 commit 3f455c1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
3 changes: 1 addition & 2 deletions Sming/Core/Network/Http/HttpConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ class HttpConnection : public TcpClient
*/
String getResponseString() SMING_DEPRECATED
{
// Place a reasonable limit on returned content
return response.getBody(4096);
return response.getBody();
}

protected:
Expand Down
18 changes: 9 additions & 9 deletions Sming/Core/Network/Http/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,19 @@ class HttpRequest
}

/**
* @brief Returns content from the body stream as string.
* @param maxLen Limit size of returned String
* @brief Moves content from the body stream into a String.
* @retval String
* @note This method consumes the stream
* @note Allocation of String doubles amount of memory required, so use with care.
* @note Move semantics are used to ensure that no/minimal additional memory is required.
* If your application has set a non-memory stream type then the method will
* fail and return an invalid String. The stream content will be left unchanged.
*/
String getBody(size_t maxLen)
String getBody()
{
if(bodyStream == nullptr) {
return nullptr;
String s;
if(bodyStream != nullptr) {
bodyStream->moveString(s);
}

return bodyStream->readString(maxLen);
return s;
}

/**
Expand Down
17 changes: 9 additions & 8 deletions Sming/Core/Network/Http/HttpResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,19 @@ class HttpResponse
bool sendDataStream(IDataSourceStream* newDataStream, const String& reqContentType = nullptr);

/**
* @brief Get response body as a string
* @param maxLen Limit size of returned String
* @brief Moves content from the body stream into a String.
* @retval String
* @note Use with caution if response is large
* @note Move semantics are used to ensure that no/minimal additional memory is required.
* If your application has set a non-memory stream type then the method will
* fail and return an invalid String. The stream content will be left unchanged.
*/
String getBody(size_t maxLen)
String getBody()
{
if(stream == nullptr) {
return nullptr;
String s;
if(stream != nullptr) {
stream->moveString(s);
}

return stream->readString(maxLen);
return s;
}

/**
Expand Down
10 changes: 4 additions & 6 deletions docs/source/upgrading/4.1-4.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ This is supported by :cpp:class:`LimitedMemoryStream` and :cpp:class:`MemoryData
getBody methods
---------------

A ``size_t`` parameter has been added to :cpp:func:`HttpRequest::getBody` and :cpp:func:`HttpResponse::getBody`.
This is to restrict the amount of memory which might be allocated to the returned :cpp:class:`String` object.
The :cpp:func:`HttpRequest::getBody` and :cpp:func:`HttpResponse::getBody` methods have been revised to use
move semantics. Previously, the data was copied into a new String which effectively doubled memory usage.

This can be used to help mitigate memory-overflow attacks.
You should generally choose a value no greater than absolutely necessary for the expected body size.

For larger amounts of data use :cpp:func:`HttpResponse::getBodyStream` instead.
If you have set a non-memory stream type (e.g. :cpp:class:`FileStream`) which does not implement `:cpp:func:`IDataSourceStream::moveString()`
then an invalid String will be returned. In this situation you should use :cpp:func:`HttpResponse::getBodyStream` instead.
4 changes: 3 additions & 1 deletion samples/HttpClient/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ int onDownload(HttpConnection& connection, bool success)
debugf("Got response code: %d", connection.getResponse()->code);
debugf("Success: %d", success);
if(connection.getRequest()->method != HTTP_HEAD) {
debugf("Got content starting with: %s", connection.getResponse()->getBody(1000).c_str());
Serial.print(_F("Got content: "));
Serial.copyFrom(connection.getResponse()->getBodyStream());
Serial.println();
}

auto ssl = connection.getSsl();
Expand Down
20 changes: 10 additions & 10 deletions samples/HttpClient_ThingSpeak/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ HttpClient thingSpeak;

int onDataSent(HttpConnection& client, bool successful)
{
if(successful)
Serial.println("Success sent");
else
Serial.println("Failed");
Serial.println(successful ? _F("Success sent") : _F("Failed"));

String response = client.getResponse()->getBody(256);
Serial.println("Server response: '" + response + "'");
String response = client.getResponse()->getBody();
Serial.print(_F("Server response: '"));
Serial.print(response);
Serial.println('\'');
if(response.length() > 0) {
int intVal = response.toInt();

if(intVal == 0)
if(intVal == 0) {
Serial.println("Sensor value wasn't accepted. May be we need to wait a little?");
}
}

return 0;
Expand All @@ -44,18 +44,18 @@ void sendData()
url.Scheme = URI_SCHEME_HTTP;
url.Host = "api.thingspeak.com";
url.Path = "/update";
url.Query["key"] = "7XXUJWCWYTMXKN3L";
url.Query["key"] = F("7XXUJWCWYTMXKN3L");
url.Query["field1"] = String(sensorValue);
thingSpeak.downloadString(url, onDataSent);
}

// Will be called when WiFi station timeout was reached
void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
{
Serial.println("I'm NOT CONNECTED. Need help :(");
Serial.println(_F("I'm NOT CONNECTED. Need help :("));

// Start soft access point
WifiAccessPoint.config("CONFIG ME PLEEEEASE...", "", AUTH_OPEN);
WifiAccessPoint.config(F("CONFIG ME PLEEEEASE..."), "", AUTH_OPEN);
WifiAccessPoint.enable(true);

// .. some you code for configuration ..
Expand Down

0 comments on commit 3f455c1

Please sign in to comment.