diff --git a/Http/HttpClient.cpp b/Http/HttpClient.cpp index bbe2466..4da8b86 100644 --- a/Http/HttpClient.cpp +++ b/Http/HttpClient.cpp @@ -4,8 +4,8 @@ namespace Http { -HttpClient::HttpClient() : - m_pHttpClientImpl(new HttpClientImpl()) +HttpClient::HttpClient(int timeout) : + m_pHttpClientImpl(new HttpClientImpl(timeout)) { } diff --git a/Http/HttpClientImpl.cpp b/Http/HttpClientImpl.cpp index 5472a0e..18c5d94 100644 --- a/Http/HttpClientImpl.cpp +++ b/Http/HttpClientImpl.cpp @@ -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) { diff --git a/Http/HttpClientImpl.h b/Http/HttpClientImpl.h index ce81f41..29b7f62 100644 --- a/Http/HttpClientImpl.h +++ b/Http/HttpClientImpl.h @@ -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 m_userAgents; }; diff --git a/include/HttpClient.h b/include/HttpClient.h index 2fa9b4e..d61f62a 100644 --- a/include/HttpClient.h +++ b/include/HttpClient.h @@ -13,7 +13,7 @@ class HttpClientImpl; class HttpClient { public: - HttpClient(); + HttpClient(int timeout = 3); ~HttpClient(); HttpClient(const HttpClient&) = delete;