From 0cbf702abad949180713300220fd00220b04c862 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 31 Mar 2026 16:57:26 +0300 Subject: [PATCH] handle "empty" domain --- src/main.rs | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 64f2e48..0bb1211 100644 --- a/src/main.rs +++ b/src/main.rs @@ -570,24 +570,35 @@ where return Err((BAD_REQUEST, "URL contains fragment or userinfo")); } - // correct host - let Some(domain) = url.domain() else { - return Err((BAD_REQUEST, "URL does not contain a domain")); + 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 + } }; - // 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?"); + // do not use "contains" here since it requires the same type and does // not allow to check for Host<&str> if the vec contains Hostname - if !ARGS.hostnames.is_empty() && !ARGS.hostnames.iter().any(|h| h == &host) { + if !ARGS.hostnames.is_empty() + && !ARGS + .hostnames + .iter() + .any(|h| host.as_ref().is_some_and(|this| this == h)) + { return Err((PROXY_REQUEST_REFUSED, "Proxy request refused")); }