format common.rs

Signed-off-by: 21pages <sunboeasy@gmail.com>
This commit is contained in:
21pages
2026-03-29 16:22:21 +08:00
parent c737611538
commit a965e8cf8f

View File

@@ -1263,11 +1263,7 @@ fn parse_simple_header(header: &str) -> Vec<HeaderEntry> {
} }
/// POST request via TCP proxy. /// POST request via TCP proxy.
async fn post_request_via_tcp_proxy( async fn post_request_via_tcp_proxy(url: &str, body: &str, header: &str) -> ResultType<String> {
url: &str,
body: &str,
header: &str,
) -> ResultType<String> {
let headers = parse_simple_header(header); let headers = parse_simple_header(header);
let resp = tcp_proxy_request("POST", url, body.as_bytes(), headers).await?; let resp = tcp_proxy_request("POST", url, body.as_bytes(), headers).await?;
if !resp.error.is_empty() { if !resp.error.is_empty() {
@@ -1283,10 +1279,7 @@ fn http_proxy_response_to_json(resp: HttpProxyResponse) -> ResultType<String> {
let mut response_headers = Map::new(); let mut response_headers = Map::new();
for entry in resp.headers.iter() { for entry in resp.headers.iter() {
response_headers.insert( response_headers.insert(entry.name.to_lowercase(), json!(entry.value));
entry.name.to_lowercase(),
json!(entry.value),
);
} }
let mut result = Map::new(); let mut result = Map::new();
@@ -1322,11 +1315,7 @@ fn tcp_proxy_fallback_log_condition() -> &'static str {
} }
/// Returns (status_code, body_text). Separating status so the wrapper can decide on fallback. /// Returns (status_code, body_text). Separating status so the wrapper can decide on fallback.
async fn post_request_http( async fn post_request_http(url: String, body: String, header: &str) -> ResultType<(u16, String)> {
url: String,
body: String,
header: &str,
) -> ResultType<(u16, String)> {
let proxy_conf = Config::get_socks(); let proxy_conf = Config::get_socks();
let tls_url = get_url_for_tls(&url, &proxy_conf); let tls_url = get_url_for_tls(&url, &proxy_conf);
let tls_type = get_cached_tls_type(tls_url); let tls_type = get_cached_tls_type(tls_url);
@@ -1368,7 +1357,10 @@ pub async fn post_request(url: String, body: String, header: &str) -> ResultType
"HTTP POST to {} {} (result: {:?}), trying TCP proxy fallback", "HTTP POST to {} {} (result: {:?}), trying TCP proxy fallback",
tcp_proxy_log_target(&url), tcp_proxy_log_target(&url),
tcp_proxy_fallback_log_condition(), tcp_proxy_fallback_log_condition(),
http_result.as_ref().map(|(s, _)| *s).map_err(|e| e.to_string()), http_result
.as_ref()
.map(|(s, _)| *s)
.map_err(|e| e.to_string()),
); );
match post_request_via_tcp_proxy(&url, &body, header).await { match post_request_via_tcp_proxy(&url, &body, header).await {
Ok(resp) => return Ok(resp), Ok(resp) => return Ok(resp),
@@ -1605,10 +1597,7 @@ async fn http_request_http(
// Serialize response headers // Serialize response headers
let mut response_headers = Map::new(); let mut response_headers = Map::new();
for (key, value) in response.headers() { for (key, value) in response.headers() {
response_headers.insert( response_headers.insert(key.to_string(), json!(value.to_str().unwrap_or("")));
key.to_string(),
json!(value.to_str().unwrap_or("")),
);
} }
let status_code = response.status().as_u16(); let status_code = response.status().as_u16();
@@ -1617,15 +1606,12 @@ async fn http_request_http(
// Construct the JSON object // Construct the JSON object
let mut result = Map::new(); let mut result = Map::new();
result.insert("status_code".to_string(), json!(status_code)); result.insert("status_code".to_string(), json!(status_code));
result.insert( result.insert("headers".to_string(), Value::Object(response_headers));
"headers".to_string(),
Value::Object(response_headers),
);
result.insert("body".to_string(), json!(response_body)); result.insert("body".to_string(), json!(response_body));
// Convert map to JSON string // Convert map to JSON string
let json_str = let json_str = serde_json::to_string(&result)
serde_json::to_string(&result).map_err(|e| anyhow!("Failed to serialize response: {}", e))?; .map_err(|e| anyhow!("Failed to serialize response: {}", e))?;
Ok((status_code, json_str)) Ok((status_code, json_str))
} }
@@ -2829,8 +2815,9 @@ mod tests {
} }
} }
let _restore = let _restore = RestoreCustomRendezvousServer(Config::get_option(
RestoreCustomRendezvousServer(Config::get_option(keys::OPTION_CUSTOM_RENDEZVOUS_SERVER)); keys::OPTION_CUSTOM_RENDEZVOUS_SERVER,
));
Config::set_option( Config::set_option(
keys::OPTION_CUSTOM_RENDEZVOUS_SERVER.to_string(), keys::OPTION_CUSTOM_RENDEZVOUS_SERVER.to_string(),
"1:2".to_string(), "1:2".to_string(),