HttpRequest: Add support for custom timeouts

This commit is contained in:
Léo Lam 2017-06-13 19:17:11 +02:00
parent ba3f16edbf
commit 0d58a0bfe2
2 changed files with 5 additions and 5 deletions

View File

@ -22,7 +22,7 @@ public:
POST,
};
Impl();
Impl(int timeout_ms);
bool IsValid() const;
Response Fetch(const std::string& url, Method method, const Headers& headers, const u8* payload,
@ -32,7 +32,7 @@ private:
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> m_curl{curl_easy_init(), curl_easy_cleanup};
};
HttpRequest::HttpRequest() : m_impl(std::make_unique<Impl>())
HttpRequest::HttpRequest(int timeout_ms) : m_impl(std::make_unique<Impl>(timeout_ms))
{
}
@ -61,7 +61,7 @@ HttpRequest::Response HttpRequest::Post(const std::string& url, const std::strin
reinterpret_cast<const u8*>(payload.data()), payload.size());
}
HttpRequest::Impl::Impl()
HttpRequest::Impl::Impl(int timeout_ms)
{
if (!m_curl)
return;
@ -69,7 +69,7 @@ HttpRequest::Impl::Impl()
// libcurl may not have been built with async DNS support, so we disable
// signal handlers to avoid a possible and likely crash if a resolve times out.
curl_easy_setopt(m_curl.get(), CURLOPT_NOSIGNAL, true);
curl_easy_setopt(m_curl.get(), CURLOPT_TIMEOUT, 3);
curl_easy_setopt(m_curl.get(), CURLOPT_TIMEOUT_MS, timeout_ms);
#ifdef _WIN32
// ALPN support is enabled by default but requires Windows >= 8.1.
curl_easy_setopt(m_curl.get(), CURLOPT_SSL_ENABLE_ALPN, false);

View File

@ -17,7 +17,7 @@ namespace Common
class HttpRequest final
{
public:
HttpRequest();
HttpRequest(int timeout_ms = 3000);
~HttpRequest();
bool IsValid() const;