Skip to content

Respect timeout with SSL #8899

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

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix timeout propagation
  • Loading branch information
d-a-v committed Mar 28, 2023
commit c6b2311956540af2a962adca3e786ac2933f32c2
16 changes: 12 additions & 4 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ bool HTTPClient::begin(WiFiClient &client, const String& url) {

_port = (protocol == "https" ? 443 : 80);
_client = client.clone();
if (_deferredTimeout) {
_client->setTimeout(_deferredTimeout);
}


return beginInternal(url, protocol.c_str());
}
Expand All @@ -109,6 +113,10 @@ bool HTTPClient::begin(WiFiClient &client, const String& host, uint16_t port, co
}

_client = client.clone();
if (_deferredTimeout) {
_client->setTimeout(_deferredTimeout);
}


clear();

Expand Down Expand Up @@ -308,10 +316,12 @@ void HTTPClient::setAuthorization(String auth)
*/
void HTTPClient::setTimeout(uint16_t timeout)
{
_tcpTimeout = timeout;
if(connected()) {
_client->setTimeout(timeout);
}
else {
_deferredTimeout = timeout;
}
}

/**
Expand Down Expand Up @@ -794,8 +804,6 @@ bool HTTPClient::connect(void)
return false;
}

_client->setTimeout(_tcpTimeout);

if(!_client->connect(_host.c_str(), _port)) {
DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u\n", _host.c_str(), _port);
return false;
Expand Down Expand Up @@ -978,7 +986,7 @@ int HTTPClient::handleHeaderResponse()
}

} else {
if((millis() - lastDataTime) > _tcpTimeout) {
if((millis() - lastDataTime) > _client.get()->getTimeout()) {
return HTTPC_ERROR_READ_TIMEOUT;
}
esp_yield();
Expand Down
4 changes: 1 addition & 3 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
#define DEBUG_HTTPCLIENT(...) do { (void)0; } while (0)
#endif

#define HTTPCLIENT_DEFAULT_TCP_TIMEOUT (5000)

/// HTTP client errors
#define HTTPC_ERROR_CONNECTION_FAILED (-1)
#define HTTPC_ERROR_SEND_HEADER_FAILED (-2)
Expand Down Expand Up @@ -250,7 +248,7 @@ class HTTPClient
String _host;
uint16_t _port = 0;
bool _reuse = true;
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
uint16_t _deferredTimeout = 0;
bool _useHTTP10 = false;

String _uri;
Expand Down
5 changes: 3 additions & 2 deletions libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,9 @@ size_t WiFiClientSecureCtx::peekBytes(uint8_t *buffer, size_t length) {
return 0;
}

_timeout = std::max(_userTimeout, _minimalTimeout);
_startMillis = millis();
while ((_pollRecvBuffer() < (int) length) && ((millis() - _startMillis) < 5000)) {
while ((_pollRecvBuffer() < (int)length) && ((millis() - _startMillis) < _timeout)) {
yield();
}

Expand Down Expand Up @@ -1214,7 +1215,7 @@ bool WiFiClientSecureCtx::_connectSSL(const char* hostName) {
_x509_knownkey = nullptr;

// reduce timeout after successful handshake to fail fast if server stop accepting our data for whathever reason
if (ret) _minimalTimeout = 5000;
if (ret) _minimalTimeout = 0;
_timeout = std::max(_userTimeout, _minimalTimeout);

return ret;
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class WiFiClientSecure : public WiFiClient {
public:

WiFiClientSecure():_ctx(new WiFiClientSecureCtx()) { _owned = _ctx.get(); }
WiFiClientSecure(const WiFiClientSecure &rhs): WiFiClient(), _ctx(rhs._ctx) { if (_ctx) _owned = _ctx.get(); }
WiFiClientSecure(const WiFiClientSecure &rhs): WiFiClient(), _ctx(rhs._ctx) { if (_ctx) _owned = _ctx.get(); _timeout = rhs._timeout; }
~WiFiClientSecure() override { _ctx = nullptr; }

WiFiClientSecure& operator=(const WiFiClientSecure&) = default;
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy