From f2a50d09636915dcc3aa99ab7a7374cc4e3fd4fb Mon Sep 17 00:00:00 2001 From: BingChunMoLi <44092516+BingChunMoLi@users.noreply.github.com> Date: Sat, 23 Aug 2025 12:58:23 +0800 Subject: [PATCH] feat: custom_user_agent --- lib/xy.h | 9 ++++++--- src/framework/core.c | 22 ++++++++-------------- src/recipe/lang/Java.c | 6 +++++- src/recipe/lang/Ruby/Ruby.c | 5 +++-- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 50915bf..9a00e97 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -696,7 +696,7 @@ _xy_log_brkt (int level, const char *prompt1, const char *prompt2, const char *c * 由于目标行会被返回出来,所以 iter_func() 并不执行目标行,只会执行遍历过的行 */ static char * -xy_run_iter (const char *cmd, unsigned long n, void (*iter_func) (const char *)) +xy_run_iter (const char *cmd, unsigned long n, bool (*iter_func) (const char *)) { const int size = 512; char *buf = (char *) malloc (size); @@ -722,7 +722,10 @@ xy_run_iter (const char *cmd, unsigned long n, void (*iter_func) (const char * break; if (iter_func) { - iter_func (buf); + if (iter_func (buf)) { + pclose (stream); + return ret; + } } } @@ -927,4 +930,4 @@ xy_parent_dir (const char *path) return dir; } -#endif +#endif \ No newline at end of file diff --git a/src/framework/core.c b/src/framework/core.c index 971e1a7..56393d3 100644 --- a/src/framework/core.c +++ b/src/framework/core.c @@ -80,7 +80,6 @@ bool chsrc_in_no_color_mode(){return ProgMode.NoColorMode;} static bool in_measure_mode(){return ProgMode.MeasureMode;} static bool in_ipv6_mode(){return ProgMode.Ipv6Mode;} static bool in_dry_run_mode(){return ProgMode.DryRunMode;} -static bool in_custom_user_agent_mode(){return false;} /** * Target Group mode (相反则称为 standalone mode) @@ -132,7 +131,8 @@ ProgStatus = { .leader_selected_index = -1, .chgtype = ChgType_Auto, - .chsrc_run_faas = false + .chsrc_run_faas = false, + .user_agent = user_agent }; @@ -603,15 +603,6 @@ measure_speed_for_url (void *url) ipv6 = "--ipv6"; } - char *user_agent = NULL; - if (in_custom_user_agent_mode()) - { - user_agent = strdup("maven/3.9.11"); - } - else - { - user_agent = xy_2strjoin("chsrc/", Chsrc_Version); - } char *os_devnull = xy_os_devnull; /** @@ -620,11 +611,10 @@ measure_speed_for_url (void *url) char *curl_cmd = xy_strjoin (10, "curl -qsL ", ipv6, " -o ", os_devnull, " -w \"%{http_code} %{speed_download}\" -m", time_sec, - " -A ", user_agent, " ", url); + " -A ", ProgStatus.user_agent, " ", url); // chsrc_info (xy_2strjoin ("测速命令 ", curl_cmd)); char *curl_buf = xy_run (curl_cmd, 0); - free(user_agent); return curl_buf; } @@ -1077,7 +1067,11 @@ chsrc_determine_chgtype (ChgType_t type) ProgStatus.chgtype = chsrc_in_reset_mode() ? ChgType_Reset : type; } - +void +chsrc_custom_user_agent (*char user_agent) +{ + ProgStatus.user_agent = user_agent; +} #define MSG_EN_PUBLIC_URL "If the URL you specify is a public service, you are invited to contribute: chsrc issue" #define MSG_CN_PUBLIC_URL "若您指定的URL为公有服务,邀您参与贡献: chsrc issue" diff --git a/src/recipe/lang/Java.c b/src/recipe/lang/Java.c index 376a2fe..91482b7 100644 --- a/src/recipe/lang/Java.c +++ b/src/recipe/lang/Java.c @@ -104,6 +104,10 @@ pl_java_getsrc (char *option) } +void use_custom_user_agent() { + chsrc_custom_user_agent("Maven/3.9.11") +} + /** * @consult https://developer.aliyun.com/mirror/maven */ @@ -114,7 +118,7 @@ pl_java_setsrc (char *option) pl_java_check_cmd (&maven_exist, &gradle_exist, &maven_daemon_exist); use_this_source(pl_java); - + use_custom_user_agent(); if (maven_exist) { char *file = xy_str_gsub (RAWSTR_pl_java_maven_config, "@1@", source.mirror->code); diff --git a/src/recipe/lang/Ruby/Ruby.c b/src/recipe/lang/Ruby/Ruby.c index b24cef5..d3b2643 100644 --- a/src/recipe/lang/Ruby/Ruby.c +++ b/src/recipe/lang/Ruby/Ruby.c @@ -52,7 +52,7 @@ pl_ruby_getsrc (char *option) chsrc_run ("bundle config get mirror.https://rubygems.org", RunOpt_Default); } -void +bool pl_ruby_remove_gem_source (const char *source) { char *cmd = NULL; @@ -61,6 +61,7 @@ pl_ruby_remove_gem_source (const char *source) cmd = xy_2strjoin ("gem sources -r ", source); chsrc_run (cmd, RunOpt_Default); } + return false } /** @@ -105,4 +106,4 @@ void pl_ruby_resetsrc (char *option) { pl_ruby_setsrc (option); -} +} \ No newline at end of file