fix(proxy): address code review feedback on response handling

Fixes from PR #1714 code review:

- Extract `read_decoded_body()` and `strip_entity_headers_for_rebuilt_body()`
  in response_processor to properly clean content-encoding/content-length
  headers after decompression
- Reuse `read_decoded_body()` in handlers.rs for Claude transform path,
  ensuring compressed responses are decoded before format conversion
- Make `build_proxy_url_from_config()` public so forwarder can pass proxy
  URL to the hyper raw write path
- Add `has_system_proxy_env()` utility with test coverage
- Add 50ms backoff after accept() failures in server.rs to prevent
  tight-loop CPU spin on transient socket errors
This commit is contained in:
YoVinchen
2026-03-28 12:16:25 +08:00
parent 324a1da8e6
commit f4e960253e
4 changed files with 135 additions and 37 deletions
+38 -1
View File
@@ -280,6 +280,28 @@ fn system_proxy_points_to_loopback() -> bool {
.any(|value| proxy_points_to_loopback(&value))
}
/// 检查当前环境是否存在可用的系统代理配置。
#[allow(dead_code)]
pub fn has_system_proxy_env() -> bool {
const KEYS: [&str; 6] = [
"HTTP_PROXY",
"http_proxy",
"HTTPS_PROXY",
"https_proxy",
"ALL_PROXY",
"all_proxy",
];
if system_proxy_points_to_loopback() {
return false;
}
KEYS.iter()
.filter_map(|key| env::var(key).ok())
.map(|value| value.trim().to_string())
.any(|value| !value.is_empty())
}
fn proxy_points_to_loopback(value: &str) -> bool {
fn host_is_loopback(host: &str) -> bool {
if host.eq_ignore_ascii_case("localhost") {
@@ -337,7 +359,7 @@ pub fn mask_url(url: &str) -> String {
/// 根据供应商单独代理配置构建代理 URL
///
/// 将 ProviderProxyConfig 转换为代理 URL 字符串
fn build_proxy_url_from_config(config: &ProviderProxyConfig) -> Option<String> {
pub fn build_proxy_url_from_config(config: &ProviderProxyConfig) -> Option<String> {
let proxy_type = config.proxy_type.as_deref().unwrap_or("http");
let host = config.proxy_host.as_deref()?;
let port = config.proxy_port?;
@@ -544,4 +566,19 @@ mod tests {
std::env::remove_var(key);
}
}
#[test]
fn test_has_system_proxy_env_ignores_own_loopback_proxy() {
let _guard = env_lock().lock().unwrap();
set_proxy_port(15721);
std::env::remove_var("HTTP_PROXY");
std::env::set_var("HTTP_PROXY", "http://127.0.0.1:15721");
assert!(!has_system_proxy_env());
std::env::set_var("HTTP_PROXY", "http://127.0.0.1:7890");
assert!(has_system_proxy_env());
std::env::remove_var("HTTP_PROXY");
}
}