From eef532850633b8d8266240ddb611a14397b2fff7 Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Fri, 22 Dec 2023 14:46:52 +0800 Subject: [PATCH] Enhancements Refine libcurl logs. Do not retry web request on API mode. Optimize codes. --- src/handler/webget.cpp | 13 +++++++------ src/script/script_quickjs.cpp | 9 +++++---- src/utils/logger.cpp | 3 ++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/handler/webget.cpp b/src/handler/webget.cpp index 736aaa54d..4433536bf 100644 --- a/src/handler/webget.cpp +++ b/src/handler/webget.cpp @@ -87,11 +87,13 @@ static int logger(CURL *handle, curl_infotype type, char *data, size_t size, voi switch(type) { case CURLINFO_TEXT: - prefix = "CURL_INFO"; + prefix = "CURL_INFO: "; break; case CURLINFO_HEADER_IN: + prefix = "CURL_HEADER: < "; + break; case CURLINFO_HEADER_OUT: - prefix = "CURL_HEADER"; + prefix = "CURL_HEADER: > "; break; case CURLINFO_DATA_IN: case CURLINFO_DATA_OUT: @@ -105,7 +107,6 @@ static int logger(CURL *handle, curl_infotype type, char *data, size_t size, voi for(auto &x : lines) { std::string log_content = prefix; - log_content += ": "; log_content += x; writeLog(0, log_content, LOG_LEVEL_VERBOSE); } @@ -113,7 +114,6 @@ static int logger(CURL *handle, curl_infotype type, char *data, size_t size, voi else { std::string log_content = prefix; - log_content += ": "; log_content += trimWhitespace(content); writeLog(0, log_content, LOG_LEVEL_VERBOSE); } @@ -172,7 +172,8 @@ static int curlGet(const FetchArgument &argument, FetchResult &result) { for(auto &x : *argument.request_headers) { - header_list = curl_slist_append(header_list, (x.first + ": " + x.second).data()); + auto header = x.first + ": " + x.second; + header_list = curl_slist_append(header_list, header.data()); } if(!argument.request_headers->contains("User-Agent")) curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str); @@ -233,7 +234,7 @@ static int curlGet(const FetchArgument &argument, FetchResult &result) while(true) { retVal = curl_easy_perform(curl_handle); - if(retVal == CURLE_OK || max_fails <= fail_count) + if(retVal == CURLE_OK || max_fails <= fail_count || global.APIMode) break; else fail_count++; diff --git a/src/script/script_quickjs.cpp b/src/script/script_quickjs.cpp index b745b8f8c..ebf230be3 100644 --- a/src/script/script_quickjs.cpp +++ b/src/script/script_quickjs.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #ifdef _WIN32 @@ -226,7 +227,7 @@ class qjs_fetch_Request qjs_fetch_Headers headers; std::string cookies; std::string postdata; - explicit qjs_fetch_Request(const std::string &url) : url(url) {} + explicit qjs_fetch_Request(std::string url) : url(std::move(url)) {} }; class qjs_fetch_Response @@ -389,7 +390,7 @@ void script_runtime_init(qjs::Runtime &runtime) js_std_init_handlers(runtime.rt); } -int ShowMsgbox(const std::string &title, std::string content, uint16_t type = 0) +int ShowMsgbox(const std::string &title, const std::string &content, uint16_t type = 0) { #ifdef _WIN32 if(!type) @@ -424,7 +425,7 @@ struct Lambda { uint32_t currentTime() { - return time(NULL); + return time(nullptr); } int script_context_init(qjs::Context &context) @@ -525,7 +526,7 @@ int script_context_init(qjs::Context &context) )", "", JS_EVAL_TYPE_MODULE); return 0; } - catch(qjs::exception) + catch(qjs::exception&) { script_print_stack(context); return 1; diff --git a/src/utils/logger.cpp b/src/utils/logger.cpp index a7694d505..ec6353b25 100644 --- a/src/utils/logger.cpp +++ b/src/utils/logger.cpp @@ -26,7 +26,8 @@ std::string getTime(int type) format = "%Y%m%d-%H%M%S"; break; case 2: - format = "%Y/%m/%d %a %H:%M:%S." + std::string(cMillis); + format = "%Y/%m/%d %a %H:%M:%S."; + format += cMillis; break; case 3: default: