Skip to content

Add server connect timeout to HTTPClient #2606

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 1 commit into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 11 additions & 2 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,15 @@ void HTTPClient::setAuthorization(const char * auth)
}
}

/**
* set the timeout (ms) for establishing a connection to the server
* @param connectTimeout int32_t
*/
void HTTPClient::setConnectTimeout(int32_t connectTimeout)
{
_connectTimeout = connectTimeout;
}

/**
* set the timeout for the TCP connection
* @param timeout unsigned int
Expand All @@ -442,7 +451,7 @@ void HTTPClient::setTimeout(uint16_t timeout)

/**
* use HTTP1.0
* @param timeout
* @param use
*/
void HTTPClient::useHTTP10(bool useHTTP10)
{
Expand Down Expand Up @@ -966,7 +975,7 @@ bool HTTPClient::connect(void)
return false;
}

if(!_client->connect(_host.c_str(), _port)) {
if(!_client->connect(_host.c_str(), _port, _connectTimeout)) {
log_d("failed connect to %s:%u", _host.c_str(), _port);
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions libraries/HTTPClient/src/HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class HTTPClient
void setUserAgent(const String& userAgent);
void setAuthorization(const char * user, const char * password);
void setAuthorization(const char * auth);
void setConnectTimeout(int32_t connectTimeout);
void setTimeout(uint16_t timeout);

void useHTTP10(bool usehttp10 = true);
Expand Down Expand Up @@ -213,6 +214,7 @@ class HTTPClient
/// request handling
String _host;
uint16_t _port = 0;
int32_t _connectTimeout = -1;
bool _reuse = false;
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
bool _useHTTP10 = false;
Expand Down