|
|
@@ -58,7 +58,7 @@ static inline void curl_set_common_options(CURL *curl_handle, const char *url)
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
|
|
|
}
|
|
|
|
|
|
-static std::string curlGet(std::string url, std::string proxy, std::string &response_headers, CURLcode &return_code)
|
|
|
+static std::string curlGet(const std::string &url, const std::string &proxy, std::string &response_headers, CURLcode &return_code)
|
|
|
{
|
|
|
CURL *curl_handle;
|
|
|
std::string data;
|
|
|
@@ -72,7 +72,7 @@ static std::string curlGet(std::string url, std::string proxy, std::string &resp
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &data);
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, writer);
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, &response_headers);
|
|
|
- if(proxy != "")
|
|
|
+ if(proxy.size())
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
|
|
|
|
|
|
return_code = curl_easy_perform(curl_handle);
|
|
|
@@ -87,7 +87,7 @@ static std::string curlGet(std::string url, std::string proxy, std::string &resp
|
|
|
}
|
|
|
|
|
|
// data:[<mediatype>][;base64],<data>
|
|
|
-static std::string dataGet(std::string url)
|
|
|
+static std::string dataGet(const std::string &url)
|
|
|
{
|
|
|
if (!startsWith(url, "data:"))
|
|
|
return "";
|
|
|
@@ -103,14 +103,14 @@ static std::string dataGet(std::string url)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-std::string buildSocks5ProxyString(std::string addr, int port, std::string username, std::string password)
|
|
|
+std::string buildSocks5ProxyString(const std::string &addr, int port, const std::string &username, const std::string &password)
|
|
|
{
|
|
|
- std::string authstr = username != "" && password != "" ? username + ":" + password + "@" : "";
|
|
|
+ std::string authstr = username.size() && password.size() ? username + ":" + password + "@" : "";
|
|
|
std::string proxystr = "socks5://" + authstr + addr + ":" + std::to_string(port);
|
|
|
return proxystr;
|
|
|
}
|
|
|
|
|
|
-std::string webGet(std::string url, std::string proxy, std::string &response_headers, unsigned int cache_ttl)
|
|
|
+std::string webGet(const std::string &url, const std::string &proxy, std::string &response_headers, unsigned int cache_ttl)
|
|
|
{
|
|
|
std::string content;
|
|
|
CURLcode return_code;
|
|
|
@@ -161,19 +161,19 @@ std::string webGet(std::string url, std::string proxy, std::string &response_hea
|
|
|
return curlGet(url, proxy, response_headers, return_code);
|
|
|
}
|
|
|
|
|
|
-std::string webGet(std::string url, std::string proxy)
|
|
|
+std::string webGet(const std::string &url, const std::string &proxy)
|
|
|
{
|
|
|
std::string dummy;
|
|
|
return webGet(url, proxy, dummy);
|
|
|
}
|
|
|
|
|
|
-std::string webGet(std::string url, std::string proxy, unsigned int cache_ttl)
|
|
|
+std::string webGet(const std::string &url, const std::string &proxy, unsigned int cache_ttl)
|
|
|
{
|
|
|
std::string dummy;
|
|
|
return webGet(url, proxy, dummy, cache_ttl);
|
|
|
}
|
|
|
|
|
|
-int curlPost(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData)
|
|
|
+int curlPost(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
|
|
|
{
|
|
|
CURL *curl_handle;
|
|
|
CURLcode res;
|
|
|
@@ -183,8 +183,8 @@ int curlPost(std::string url, std::string data, std::string proxy, std::string a
|
|
|
curl_init();
|
|
|
curl_handle = curl_easy_init();
|
|
|
list = curl_slist_append(list, "Content-Type: application/json;charset='utf-8'");
|
|
|
- if(auth_token.size())
|
|
|
- list = curl_slist_append(list, std::string("Authorization: token " + auth_token).data());
|
|
|
+ for(const std::string &x : request_headers)
|
|
|
+ list = curl_slist_append(list, x.data());
|
|
|
|
|
|
curl_set_common_options(curl_handle, url.data());
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
|
|
|
@@ -194,7 +194,7 @@ int curlPost(std::string url, std::string data, std::string proxy, std::string a
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, retData);
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
|
|
|
- if(proxy != "")
|
|
|
+ if(proxy.size())
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
|
|
|
|
|
|
res = curl_easy_perform(curl_handle);
|
|
|
@@ -210,12 +210,12 @@ int curlPost(std::string url, std::string data, std::string proxy, std::string a
|
|
|
return retVal;
|
|
|
}
|
|
|
|
|
|
-int webPost(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData)
|
|
|
+int webPost(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
|
|
|
{
|
|
|
- return curlPost(url, data, proxy, auth_token, retData);
|
|
|
+ return curlPost(url, data, proxy, request_headers, retData);
|
|
|
}
|
|
|
|
|
|
-int curlPatch(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData)
|
|
|
+int curlPatch(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
|
|
|
{
|
|
|
CURL *curl_handle;
|
|
|
CURLcode res;
|
|
|
@@ -227,8 +227,8 @@ int curlPatch(std::string url, std::string data, std::string proxy, std::string
|
|
|
curl_handle = curl_easy_init();
|
|
|
|
|
|
list = curl_slist_append(list, "Content-Type: application/json;charset='utf-8'");
|
|
|
- if(auth_token.size())
|
|
|
- list = curl_slist_append(list, std::string("Authorization: token " + auth_token).data());
|
|
|
+ for(const std::string &x : request_headers)
|
|
|
+ list = curl_slist_append(list, x.data());
|
|
|
|
|
|
curl_set_common_options(curl_handle, url.data());
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "PATCH");
|
|
|
@@ -238,7 +238,7 @@ int curlPatch(std::string url, std::string data, std::string proxy, std::string
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, retData);
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
|
|
|
- if(proxy != "")
|
|
|
+ if(proxy.size())
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
|
|
|
|
|
|
res = curl_easy_perform(curl_handle);
|
|
|
@@ -253,7 +253,46 @@ int curlPatch(std::string url, std::string data, std::string proxy, std::string
|
|
|
return retVal;
|
|
|
}
|
|
|
|
|
|
-int webPatch(std::string url, std::string data, std::string proxy, std::string auth_token, std::string *retData)
|
|
|
+int webPatch(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
|
|
|
{
|
|
|
- return curlPatch(url, data, proxy, auth_token, retData);
|
|
|
+ return curlPatch(url, data, proxy, request_headers, retData);
|
|
|
+}
|
|
|
+
|
|
|
+int curlHead(const std::string &url, const std::string &proxy, const string_array &request_headers, std::string &response_headers)
|
|
|
+{
|
|
|
+ CURL *curl_handle;
|
|
|
+ CURLcode res;
|
|
|
+ long retVal = 0;
|
|
|
+ struct curl_slist *list = NULL;
|
|
|
+
|
|
|
+ curl_init();
|
|
|
+
|
|
|
+ curl_handle = curl_easy_init();
|
|
|
+
|
|
|
+ list = curl_slist_append(list, "Content-Type: application/json;charset='utf-8'");
|
|
|
+ for(const std::string &x : request_headers)
|
|
|
+ list = curl_slist_append(list, x.data());
|
|
|
+
|
|
|
+ curl_set_common_options(curl_handle, url.data());
|
|
|
+ curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, writer);
|
|
|
+ curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, &response_headers);
|
|
|
+ curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1L);
|
|
|
+ curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
|
|
|
+ curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
|
|
|
+ if(proxy.size())
|
|
|
+ curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
|
|
|
+
|
|
|
+ res = curl_easy_perform(curl_handle);
|
|
|
+ curl_slist_free_all(list);
|
|
|
+ if(res == CURLE_OK)
|
|
|
+ res = curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &retVal);
|
|
|
+
|
|
|
+ curl_easy_cleanup(curl_handle);
|
|
|
+
|
|
|
+ return retVal;
|
|
|
+}
|
|
|
+
|
|
|
+int webHead(const std::string &url, const std::string &proxy, const string_array &request_headers, std::string &response_headers)
|
|
|
+{
|
|
|
+ return curlHead(url, proxy, request_headers, response_headers);
|
|
|
}
|