mirror of
https://github.com/MetaCubeX/subconverter.git
synced 2025-11-04 18:19:42 +08:00
Fix Gist upload and other web operation may fail to post data
Remove unused files in Docker images. Cleanup codes.
This commit is contained in:
parent
61b671c13a
commit
94ee747649
@ -38,7 +38,7 @@ RUN apk add --no-cache --virtual .build-tools git g++ build-base linux-headers c
|
||||
mv subconverter /usr/bin && \
|
||||
mv base ../ && \
|
||||
cd .. && \
|
||||
rm -rf subconverter quickjspp libcron toml11 && \
|
||||
rm -rf subconverter quickjspp libcron toml11 /usr/lib/lib*.a /usr/include/* /usr/local/include/lib*.a /usr/local/include/* && \
|
||||
apk add --no-cache --virtual subconverter-deps pcre2 libcurl yaml-cpp libevent && \
|
||||
apk del .build-tools .build-deps
|
||||
|
||||
|
||||
@ -231,13 +231,19 @@ static int curlGet(const FetchArgument &argument, FetchResult &result)
|
||||
{
|
||||
case HTTP_POST:
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, argument.post_data.data());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, argument.post_data.size());
|
||||
if(argument.post_data)
|
||||
{
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, argument.post_data->data());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, argument.post_data->size());
|
||||
}
|
||||
break;
|
||||
case HTTP_PATCH:
|
||||
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "PATCH");
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, argument.post_data.data());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, argument.post_data.size());
|
||||
if(argument.post_data)
|
||||
{
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, argument.post_data->data());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, argument.post_data->size());
|
||||
}
|
||||
break;
|
||||
case HTTP_HEAD:
|
||||
curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1L);
|
||||
@ -249,14 +255,16 @@ static int curlGet(const FetchArgument &argument, FetchResult &result)
|
||||
unsigned int fail_count = 0, max_fails = 1;
|
||||
while(true)
|
||||
{
|
||||
*result.status_code = curl_easy_perform(curl_handle);
|
||||
if(*result.status_code == CURLE_OK || max_fails >= fail_count)
|
||||
retVal = curl_easy_perform(curl_handle);
|
||||
if(retVal == CURLE_OK || max_fails >= fail_count)
|
||||
break;
|
||||
else
|
||||
fail_count++;
|
||||
}
|
||||
|
||||
curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &retVal);
|
||||
long code = 0;
|
||||
curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &code);
|
||||
*result.status_code = code;
|
||||
|
||||
if(result.cookies)
|
||||
{
|
||||
@ -277,9 +285,9 @@ static int curlGet(const FetchArgument &argument, FetchResult &result)
|
||||
|
||||
curl_easy_cleanup(curl_handle);
|
||||
|
||||
if(data)
|
||||
if(data && !argument.keep_resp_on_fail)
|
||||
{
|
||||
if(*result.status_code != CURLE_OK || retVal != 200)
|
||||
if(retVal != CURLE_OK || *result.status_code != 200)
|
||||
data->clear();
|
||||
data->shrink_to_fit();
|
||||
}
|
||||
@ -316,7 +324,7 @@ std::string webGet(const std::string &url, const std::string &proxy, unsigned in
|
||||
int return_code = 0;
|
||||
std::string content;
|
||||
|
||||
FetchArgument argument {HTTP_GET, url, proxy, "", request_headers, nullptr, cache_ttl};
|
||||
FetchArgument argument {HTTP_GET, url, proxy, nullptr, request_headers, nullptr, cache_ttl};
|
||||
FetchResult fetch_res {&return_code, &content, response_headers, nullptr};
|
||||
|
||||
if (startsWith(url, "data:"))
|
||||
@ -386,138 +394,29 @@ void flushCache()
|
||||
operateFiles("cache", [](const std::string &file){ remove(("cache/" + file).data()); return 0; });
|
||||
}
|
||||
|
||||
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;
|
||||
struct curl_slist *list = NULL;
|
||||
long retVal = 0;
|
||||
|
||||
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_progress_data limit;
|
||||
curl_set_common_options(curl_handle, url.data(), &limit);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data.data());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, data.size());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writer);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, retData);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
|
||||
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)
|
||||
{
|
||||
curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &retVal);
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl_handle);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
int webPost(const std::string &url, const std::string &data, const std::string &proxy, const string_icase_map &request_headers, std::string *retData)
|
||||
{
|
||||
//return curlPost(url, data, proxy, request_headers, retData);
|
||||
int return_code = 0;
|
||||
FetchArgument argument {HTTP_POST, url, proxy, "", &request_headers, nullptr, 0};
|
||||
FetchArgument argument {HTTP_POST, url, proxy, &data, &request_headers, nullptr, 0, true};
|
||||
FetchResult fetch_res {&return_code, retData, nullptr, nullptr};
|
||||
return webGet(argument, fetch_res);
|
||||
}
|
||||
|
||||
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;
|
||||
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_progress_data limit;
|
||||
curl_set_common_options(curl_handle, url.data(), &limit);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "PATCH");
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data.data());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, data.size());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writer);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, retData);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
|
||||
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 webPatch(const std::string &url, const std::string &data, const std::string &proxy, const string_icase_map &request_headers, std::string *retData)
|
||||
{
|
||||
//return curlPatch(url, data, proxy, request_headers, retData);
|
||||
int return_code = 0;
|
||||
FetchArgument argument {HTTP_PATCH, url, proxy, "", &request_headers, nullptr, 0};
|
||||
FetchArgument argument {HTTP_PATCH, url, proxy, &data, &request_headers, nullptr, 0, true};
|
||||
FetchResult fetch_res {&return_code, retData, nullptr, nullptr};
|
||||
return webGet(argument, fetch_res);
|
||||
}
|
||||
|
||||
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_progress_data limit;
|
||||
curl_set_common_options(curl_handle, url.data(), &limit);
|
||||
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);
|
||||
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_icase_map &request_headers, std::string &response_headers)
|
||||
{
|
||||
//return curlHead(url, proxy, request_headers, response_headers);
|
||||
int return_code = 0;
|
||||
FetchArgument argument {HTTP_HEAD, url, proxy, "", &request_headers, nullptr, 0};
|
||||
FetchArgument argument {HTTP_HEAD, url, proxy, nullptr, &request_headers, nullptr, 0};
|
||||
FetchResult fetch_res {&return_code, nullptr, &response_headers, nullptr};
|
||||
return webGet(argument, fetch_res);
|
||||
}
|
||||
@ -533,39 +432,4 @@ string_array headers_map_to_array(const string_map &headers)
|
||||
int webGet(const FetchArgument& argument, FetchResult &result)
|
||||
{
|
||||
return curlGet(argument, result);
|
||||
/*
|
||||
switch(argument.method)
|
||||
{
|
||||
case HTTP_GET:
|
||||
return curlGet(argument, result);
|
||||
case HTTP_POST:
|
||||
{
|
||||
string_array request_header;
|
||||
if(argument.request_headers != nullptr)
|
||||
request_header = headers_map_to_array(*argument.request_headers);
|
||||
int res = curlPost(argument.url, argument.post_data, argument.proxy, request_header, result.content);
|
||||
*result.status_code = res;
|
||||
return res;
|
||||
}
|
||||
case HTTP_PATCH:
|
||||
{
|
||||
string_array request_header;
|
||||
if(argument.request_headers != nullptr)
|
||||
request_header = headers_map_to_array(*argument.request_headers);
|
||||
int res = curlPatch(argument.url, argument.post_data, argument.proxy, request_header, result.content);
|
||||
*result.status_code = res;
|
||||
return res;
|
||||
}
|
||||
case HTTP_HEAD:
|
||||
{
|
||||
string_array request_header;
|
||||
if(argument.request_headers != nullptr)
|
||||
request_header = headers_map_to_array(*argument.request_headers);
|
||||
int res = curlHead(argument.url, argument.proxy, request_header, *result.response_headers);
|
||||
*result.status_code = res;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
*/
|
||||
}
|
||||
|
||||
@ -20,10 +20,11 @@ struct FetchArgument
|
||||
const http_method method;
|
||||
const std::string url;
|
||||
const std::string proxy;
|
||||
const std::string post_data;
|
||||
const std::string *post_data = nullptr;
|
||||
const string_icase_map *request_headers = nullptr;
|
||||
std::string *cookies = nullptr;
|
||||
const unsigned int cache_ttl = 0;
|
||||
const bool keep_resp_on_fail = false;
|
||||
};
|
||||
|
||||
struct FetchResult
|
||||
|
||||
@ -86,7 +86,6 @@ bool getSubInfoFromHeader(const std::string &header, std::string &result)
|
||||
bool getSubInfoFromNodes(const std::vector<Proxy> &nodes, const RegexMatchConfigs &stream_rules, const RegexMatchConfigs &time_rules, std::string &result)
|
||||
{
|
||||
std::string remarks, stream_info, time_info, retStr;
|
||||
string_size spos;
|
||||
|
||||
for(const Proxy &x : nodes)
|
||||
{
|
||||
|
||||
@ -370,7 +370,7 @@ static qjs_fetch_Response qjs_fetch(qjs_fetch_Request request)
|
||||
}
|
||||
|
||||
std::string response_headers;
|
||||
FetchArgument argument {method, request.url, request.proxy, request.postdata, &request.headers.headers, &request.cookies, 0};
|
||||
FetchArgument argument {method, request.url, request.proxy, &request.postdata, &request.headers.headers, &request.cookies, 0};
|
||||
FetchResult result {&response.status_code, &response.content, &response_headers, &response.cookies};
|
||||
|
||||
webGet(argument, result);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user