From 23d4167511f89c4a1cc93ce2411032072128739b Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 3 Apr 2026 08:45:56 +0300 Subject: [PATCH] set host ip if not the domain (pr#433 review) --- src/main.rs | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0bb1211..8d4e9da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -570,25 +570,28 @@ where return Err((BAD_REQUEST, "URL contains fragment or userinfo")); } - let host = match url.domain() { - Some(domain) => { - // because the gemini scheme is not special enough for WHATWG, normalize - // it ourselves - let host = Host::parse( - &percent_decode_str(domain) - .decode_utf8() - .or(Err((BAD_REQUEST, "Invalid URL")))?, - ) - .or(Err((BAD_REQUEST, "Invalid URL")))?; - // TODO: simplify when resolved - url.set_host(Some(&host.to_string())) - .expect("invalid domain?"); - Some(host) - } - None => { - url.set_host(None).expect("invalid domain?"); - None - } + match url.host() { + Some(host) => match host { + Host::Domain(domain) => { + // because the gemini scheme is not special enough for WHATWG, normalize + // it ourselves + let d = Host::parse( + &percent_decode_str(domain) + .decode_utf8() + .or(Err((BAD_REQUEST, "Invalid URL")))?, + ) + .or(Err((BAD_REQUEST, "Invalid URL")))?; + // TODO: simplify when resolved + url.set_host(Some(&d.to_string())).expect("invalid domain?"); + } + Host::Ipv4(ipv4_addr) => url + .set_ip_host(IpAddr::V4(ipv4_addr)) + .expect("invalid IPv4?"), + Host::Ipv6(ipv6_addr) => url + .set_ip_host(IpAddr::V6(ipv6_addr)) + .expect("invalid IPv6?"), + }, + None => url.set_host(None).expect("invalid domain?"), }; // do not use "contains" here since it requires the same type and does @@ -597,7 +600,7 @@ where && !ARGS .hostnames .iter() - .any(|h| host.as_ref().is_some_and(|this| this == h)) + .any(|host| url.host().as_ref().is_some_and(|this| this == host)) { return Err((PROXY_REQUEST_REFUSED, "Proxy request refused")); }