espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.69k stars 7.42k forks source link

Modify to "String HTTPClient::getString(size_t size)" #10375

Open Rob58329 opened 1 month ago

Rob58329 commented 1 month ago

Related area

Significantly reduce delay

Hardware specification

All ESP32 boards

Is your feature request related to a problem?

In cases where Server sends no Content-Length header, the above getString() function gets "_size=-1", and will wait as long as 2 minutes before returning.

Describe the solution you'd like

Further to: https://github.com/espressif/arduino-esp32/issues/2667

re. https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/src/HTTPClient.h and https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/src/HTTPClient.cpp

In cases where Server sends no Content-Length header, the above getString() function gets "_size=-1", and will wait as long as 2 minutes before returning.

eg. URL=http://api.openweathermap.org/data/2.5/weather?lon=-1.264&lat=52.371&units=metric&lang=en&appid=???

(The http.setConnectTimeout(3000) and http.setTimeout(3000) commands do not change this.)

A fix for this is to set a limit on the minimum data size you need (ie. the maximum to receive) as follow:

Modify "HTTPClient.h" to say: String getString(size_t size=0); Modify / add 1 line to "HTTPClient.cpp" to:

  String HTTPClient::getString(size_t size) {
    if ((_size==-1) && (size!=0)) _size=size;
    ...

This allows existing "String s=http.getString()" commands to still work fine. But will also allow the new "String s=http.getString(500)" command which for the above URL example returns the data I need and reduces the wait time to under 1 second.

Although this modification is not as universal as implementing a selectable-timeout on this function, it is far simpler.

I can write a PR for this if someone is able to consider approving/commiting it?

Describe alternatives you've considered

No response

Additional context

No response

I have checked existing list of Feature requests and the Contribution Guide

TD-er commented 1 month ago

HTTPClient::writeToStream does call functions from _client, which has its own timeout. So it is a bit strange it will not obey those timeouts.