Add Timeout to HttpClient

This commit is contained in:
2020-03-27 15:44:23 +01:00
parent 3785c09dc1
commit 0d93451591
4 changed files with 14 additions and 5 deletions
+2 -2
View File
@@ -4,8 +4,8 @@
namespace Http {
HttpClient::HttpClient() :
m_pHttpClientImpl(new HttpClientImpl())
HttpClient::HttpClient(int timeout) :
m_pHttpClientImpl(new HttpClientImpl(timeout))
{
}
+9 -1
View File
@@ -10,7 +10,8 @@
namespace Http {
HttpClientImpl::HttpClientImpl()
HttpClientImpl::HttpClientImpl(int timeout) :
m_timeout(timeout)
{
curl_global_init(CURL_GLOBAL_ALL);
@@ -207,6 +208,7 @@ std::string HttpClientImpl::GetUrlPostAttachmentContents(const std::string& url,
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
@@ -285,6 +287,7 @@ void HttpClientImpl::GetUrlPostAttachmentSilent(const std::string& url, const st
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
int code = 0;
@@ -358,6 +361,7 @@ std::string HttpClientImpl::GetUrlPostAttachmentRedirect(const std::string& url,
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
int code = 0;
@@ -447,6 +451,7 @@ std::string HttpClientImpl::GetUrlPostAttachmentContents(const std::string& url,
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
@@ -527,6 +532,7 @@ void HttpClientImpl::GetUrlPostAttachmentSilent(const std::string& url, const st
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
@@ -602,6 +608,7 @@ std::string HttpClientImpl::GetUrlPostAttachmentRedirect(const std::string& url,
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
@@ -647,6 +654,7 @@ std::string HttpClientImpl::PerformOperation(HttpClientImpl::Operation::type typ
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout);
if (type == HttpClientImpl::Operation::Silent)
{
+2 -1
View File
@@ -12,7 +12,7 @@ namespace Http {
class HttpClientImpl
{
public:
HttpClientImpl();
HttpClientImpl(int timeout);
~HttpClientImpl();
HttpClientImpl(const HttpClientImpl&) = delete;
@@ -86,6 +86,7 @@ private:
static size_t WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData);
private:
int m_timeout;
std::vector<std::string> m_userAgents;
};
+1 -1
View File
@@ -13,7 +13,7 @@ class HttpClientImpl;
class HttpClient
{
public:
HttpClient();
HttpClient(int timeout = 3);
~HttpClient();
HttpClient(const HttpClient&) = delete;