Parse a proxy URL string like "http://host:port" into a ProxyConfig
(url: &str)
| 404 | |
| 405 | /// Parse a proxy URL string like "http://host:port" into a ProxyConfig |
| 406 | fn parse_proxy_url(url: &str) -> Option<ProxyConfig> { |
| 407 | let url = url.trim(); |
| 408 | if url.is_empty() { |
| 409 | return None; |
| 410 | } |
| 411 | |
| 412 | // Parse scheme |
| 413 | let (scheme, rest) = if let Some(rest) = url.strip_prefix("socks5://") { |
| 414 | ("socks5", rest) |
| 415 | } else if let Some(rest) = url.strip_prefix("https://") { |
| 416 | ("https", rest) |
| 417 | } else if let Some(rest) = url.strip_prefix("http://") { |
| 418 | ("http", rest) |
| 419 | } else { |
| 420 | ("http", url) |
| 421 | }; |
| 422 | |
| 423 | // Parse host:port |
| 424 | let (host, port) = if let Some(colon_pos) = rest.rfind(':') { |
| 425 | let host = &rest[..colon_pos]; |
| 426 | let port_str = &rest[colon_pos + 1..]; |
| 427 | match port_str.parse::<u16>() { |
| 428 | Ok(p) => (host, p), |
| 429 | Err(_) => return None, |
| 430 | } |
| 431 | } else { |
| 432 | return None; |
| 433 | }; |
| 434 | |
| 435 | let mut config = ProxyConfig::new(host, port); |
| 436 | config = match scheme { |
| 437 | "socks5" => config.with_protocol(a3s_search::proxy::ProxyProtocol::Socks5), |
| 438 | "https" => config.with_protocol(a3s_search::proxy::ProxyProtocol::Https), |
| 439 | _ => config, // default is Http |
| 440 | }; |
| 441 | |
| 442 | Some(config) |
| 443 | } |
| 444 | |
| 445 | #[cfg(test)] |
| 446 | mod tests { |