Http: Set a sane default connection timeout

This commit is contained in:
Vojtech Kral 2018-12-19 15:40:07 +01:00
parent 0ac4d13015
commit 17c2f3d813
2 changed files with 11 additions and 0 deletions

View File

@ -32,6 +32,7 @@ class CurlGlobalInit
struct Http::priv struct Http::priv
{ {
enum { enum {
DEFAULT_TIMEOUT = 10,
DEFAULT_SIZE_LIMIT = 5 * 1024 * 1024, DEFAULT_SIZE_LIMIT = 5 * 1024 * 1024,
}; };
@ -84,6 +85,7 @@ Http::priv::priv(const std::string &url)
throw std::runtime_error(std::string("Could not construct Curl object")); throw std::runtime_error(std::string("Could not construct Curl object"));
} }
::curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, DEFAULT_TIMEOUT);
::curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // curl makes a copy internally ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // curl makes a copy internally
::curl_easy_setopt(curl, CURLOPT_USERAGENT, SLIC3R_FORK_NAME "/" SLIC3R_VERSION); ::curl_easy_setopt(curl, CURLOPT_USERAGENT, SLIC3R_FORK_NAME "/" SLIC3R_VERSION);
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer.front()); ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer.front());
@ -293,6 +295,13 @@ Http::~Http()
} }
Http& Http::timeout(long timeout)
{
if (timeout < 1) { timeout = priv::DEFAULT_TIMEOUT; }
if (p) { ::curl_easy_setopt(p->curl, CURLOPT_CONNECTTIMEOUT, timeout); }
return *this;
}
Http& Http::size_limit(size_t sizeLimit) Http& Http::size_limit(size_t sizeLimit)
{ {
if (p) { p->limit = sizeLimit; } if (p) { p->limit = sizeLimit; }

View File

@ -55,6 +55,8 @@ public:
Http& operator=(const Http &) = delete; Http& operator=(const Http &) = delete;
Http& operator=(Http &&) = delete; Http& operator=(Http &&) = delete;
// Sets a maximum connection timeout in seconds
Http& timeout(long timeout);
// Sets a maximum size of the data that can be received. // Sets a maximum size of the data that can be received.
// A value of zero sets the default limit, which is is 5MB. // A value of zero sets the default limit, which is is 5MB.
Http& size_limit(size_t sizeLimit); Http& size_limit(size_t sizeLimit);