From 8d3c4319c672c742d662d9ff4c258d83697bf53a Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 19 Jan 2025 09:02:39 +0200 Subject: [PATCH] fix `lookup` query detection --- .../browser/window/tab/item/page/client.rs | 2 +- .../window/tab/item/page/client/request.rs | 50 ++++++++++--------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/app/browser/window/tab/item/page/client.rs b/src/app/browser/window/tab/item/page/client.rs index 138c3c8a..97383021 100644 --- a/src/app/browser/window/tab/item/page/client.rs +++ b/src/app/browser/window/tab/item/page/client.rs @@ -86,7 +86,7 @@ impl Client { Error::Unsupported => callback(Response::Failure(Failure::Error { message: "Request scheme yet not supported".to_string(), })), - // try async resolver + // try async resolver (slow method) _ => Request::lookup(query, Some(&cancellable), |result| { callback(match result { // redirection with scheme auto-complete or default search provider diff --git a/src/app/browser/window/tab/item/page/client/request.rs b/src/app/browser/window/tab/item/page/client/request.rs index 624e6e73..ca2caba8 100644 --- a/src/app/browser/window/tab/item/page/client/request.rs +++ b/src/app/browser/window/tab/item/page/client/request.rs @@ -78,34 +78,36 @@ impl Request { prelude::{NetworkAddressExt, ResolverExt}, }; - const DEFAULT_SCHEME: &str = "gemini://"; + const DEFAULT_SCHEME: &str = "gemini"; const DEFAULT_PORT: u16 = 1965; - const TIMEOUT: u32 = 1000; + const TIMEOUT: u32 = 250; // ms - let request = match query.trim().strip_prefix(DEFAULT_SCHEME) { - Some(postfix) => format!("{DEFAULT_SCHEME}{postfix}"), - None => query.to_string(), - }; + let query = query.trim(); - let resolver = Resolver::default(); - resolver.set_timeout(TIMEOUT); + match Uri::parse(query, UriFlags::NONE) { + Ok(uri) => callback(Self::from_uri(uri, None, None)), + Err(_) => { + // try default scheme suggestion + let suggestion = format!("{DEFAULT_SCHEME}://{query}"); - match NetworkAddress::parse_uri(&request, DEFAULT_PORT) { - Ok(connectable) => resolver.lookup_by_name_async( - &connectable.hostname(), - cancellable, - move |resolve| { - callback(if resolve.is_ok() { - match Uri::parse(&request, UriFlags::NONE) { - Ok(uri) => Self::from_uri(uri, None, None), - Err(e) => Err(Error::Glib(e)), - } - } else { - Ok(Self::search(&request)) - }) - }, - ), - Err(_) => callback(Ok(Self::search(&request))), // @TODO not completed yet, fix invalid URI issue + let resolver = Resolver::default(); + resolver.set_timeout(TIMEOUT); + + match NetworkAddress::parse_uri(&suggestion, DEFAULT_PORT) { + Ok(connectable) => resolver.lookup_by_name_async( + &connectable.hostname(), + cancellable, + move |resolve| { + callback(if resolve.is_ok() { + Self::parse(&suggestion, None) + } else { + Ok(Self::search(&suggestion)) + }) + }, + ), + Err(_) => callback(Ok(Self::search(&suggestion))), + } + } } }