ソースを参照

Add Timeout to HttpClient

JDierkse 5 年 前
コミット
0d93451591
4 ファイル変更14 行追加5 行削除
  1. 2 2
      Http/HttpClient.cpp
  2. 9 1
      Http/HttpClientImpl.cpp
  3. 2 1
      Http/HttpClientImpl.h
  4. 1 1
      include/HttpClient.h

+ 2 - 2
Http/HttpClient.cpp

@@ -4,8 +4,8 @@
 
 namespace Http {
 
-HttpClient::HttpClient() :
-	m_pHttpClientImpl(new HttpClientImpl())
+HttpClient::HttpClient(int timeout) :
+	m_pHttpClientImpl(new HttpClientImpl(timeout))
 {
 }
 

+ 9 - 1
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)
 	{

+ 2 - 1
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<std::string> m_userAgents;
 };
 

+ 1 - 1
include/HttpClient.h

@@ -13,7 +13,7 @@ class HttpClientImpl;
 class HttpClient
 {
 public:
-	HttpClient();
+	HttpClient(int timeout = 3);
 	~HttpClient();
 
 	HttpClient(const HttpClient&) = delete;