HttpRequest: Add support for custom timeouts
This commit is contained in:
parent
ba3f16edbf
commit
0d58a0bfe2
|
@ -22,7 +22,7 @@ public:
|
||||||
POST,
|
POST,
|
||||||
};
|
};
|
||||||
|
|
||||||
Impl();
|
Impl(int timeout_ms);
|
||||||
|
|
||||||
bool IsValid() const;
|
bool IsValid() const;
|
||||||
Response Fetch(const std::string& url, Method method, const Headers& headers, const u8* payload,
|
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};
|
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());
|
reinterpret_cast<const u8*>(payload.data()), payload.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpRequest::Impl::Impl()
|
HttpRequest::Impl::Impl(int timeout_ms)
|
||||||
{
|
{
|
||||||
if (!m_curl)
|
if (!m_curl)
|
||||||
return;
|
return;
|
||||||
|
@ -69,7 +69,7 @@ HttpRequest::Impl::Impl()
|
||||||
// libcurl may not have been built with async DNS support, so we disable
|
// 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.
|
// 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_NOSIGNAL, true);
|
||||||
curl_easy_setopt(m_curl.get(), CURLOPT_TIMEOUT, 3);
|
curl_easy_setopt(m_curl.get(), CURLOPT_TIMEOUT_MS, timeout_ms);
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// ALPN support is enabled by default but requires Windows >= 8.1.
|
// ALPN support is enabled by default but requires Windows >= 8.1.
|
||||||
curl_easy_setopt(m_curl.get(), CURLOPT_SSL_ENABLE_ALPN, false);
|
curl_easy_setopt(m_curl.get(), CURLOPT_SSL_ENABLE_ALPN, false);
|
||||||
|
|
|
@ -17,7 +17,7 @@ namespace Common
|
||||||
class HttpRequest final
|
class HttpRequest final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HttpRequest();
|
HttpRequest(int timeout_ms = 3000);
|
||||||
~HttpRequest();
|
~HttpRequest();
|
||||||
bool IsValid() const;
|
bool IsValid() const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue