Add Timeout to HttpClient
This commit is contained in:
+2
-2
@@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
namespace Http {
|
namespace Http {
|
||||||
|
|
||||||
HttpClient::HttpClient() :
|
HttpClient::HttpClient(int timeout) :
|
||||||
m_pHttpClientImpl(new HttpClientImpl())
|
m_pHttpClientImpl(new HttpClientImpl(timeout))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,8 @@
|
|||||||
|
|
||||||
namespace Http {
|
namespace Http {
|
||||||
|
|
||||||
HttpClientImpl::HttpClientImpl()
|
HttpClientImpl::HttpClientImpl(int timeout) :
|
||||||
|
m_timeout(timeout)
|
||||||
{
|
{
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
|
||||||
@@ -207,6 +208,7 @@ std::string HttpClientImpl::GetUrlPostAttachmentContents(const std::string& url,
|
|||||||
headerlist = curl_slist_append(headerlist, headerBuffer);
|
headerlist = curl_slist_append(headerlist, headerBuffer);
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
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_HTTPPOST, formpost);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
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);
|
headerlist = curl_slist_append(headerlist, headerBuffer);
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
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_HTTPPOST, formpost);
|
||||||
|
|
||||||
int code = 0;
|
int code = 0;
|
||||||
@@ -358,6 +361,7 @@ std::string HttpClientImpl::GetUrlPostAttachmentRedirect(const std::string& url,
|
|||||||
headerlist = curl_slist_append(headerlist, headerBuffer);
|
headerlist = curl_slist_append(headerlist, headerBuffer);
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
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_HTTPPOST, formpost);
|
||||||
|
|
||||||
int code = 0;
|
int code = 0;
|
||||||
@@ -447,6 +451,7 @@ std::string HttpClientImpl::GetUrlPostAttachmentContents(const std::string& url,
|
|||||||
headerlist = curl_slist_append(headerlist, headerBuffer);
|
headerlist = curl_slist_append(headerlist, headerBuffer);
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
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_HTTPPOST, formpost);
|
||||||
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
|
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, 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);
|
headerlist = curl_slist_append(headerlist, headerBuffer);
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
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_HTTPPOST, formpost);
|
||||||
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
|
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, 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);
|
headerlist = curl_slist_append(headerlist, headerBuffer);
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
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_HTTPPOST, formpost);
|
||||||
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
|
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, 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_SSL_VERIFYHOST, 0);
|
||||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
|
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_URL, url.c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_timeout);
|
||||||
|
|
||||||
if (type == HttpClientImpl::Operation::Silent)
|
if (type == HttpClientImpl::Operation::Silent)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Http {
|
|||||||
class HttpClientImpl
|
class HttpClientImpl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HttpClientImpl();
|
HttpClientImpl(int timeout);
|
||||||
~HttpClientImpl();
|
~HttpClientImpl();
|
||||||
|
|
||||||
HttpClientImpl(const HttpClientImpl&) = delete;
|
HttpClientImpl(const HttpClientImpl&) = delete;
|
||||||
@@ -86,6 +86,7 @@ private:
|
|||||||
static size_t WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData);
|
static size_t WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
int m_timeout;
|
||||||
std::vector<std::string> m_userAgents;
|
std::vector<std::string> m_userAgents;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class HttpClientImpl;
|
|||||||
class HttpClient
|
class HttpClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HttpClient();
|
HttpClient(int timeout = 3);
|
||||||
~HttpClient();
|
~HttpClient();
|
||||||
|
|
||||||
HttpClient(const HttpClient&) = delete;
|
HttpClient(const HttpClient&) = delete;
|
||||||
|
|||||||
Reference in New Issue
Block a user