From d4efcbca375de023be79ecc4516caa8a6dd44515 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 19 Jan 2025 07:19:37 +0200 Subject: [PATCH] connect dns lookup detector to handle scheme-less uri without search redirection --- .../browser/window/tab/item/page/client.rs | 26 ++++++++++++++----- .../window/tab/item/page/client/request.rs | 15 ++++++----- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/app/browser/window/tab/item/page/client.rs b/src/app/browser/window/tab/item/page/client.rs index 1826235a..44600284 100644 --- a/src/app/browser/window/tab/item/page/client.rs +++ b/src/app/browser/window/tab/item/page/client.rs @@ -73,16 +73,30 @@ impl Client { time: now(), value: query.to_string(), }); + + use request::Error; + use response::{Failure, Redirect}; + + let cancellable = self.new_cancellable(); + match Request::parse(query, None) { - Ok(request) => request.handle(self, self.new_cancellable(), callback), - Err(e) => callback(match e { + Ok(request) => request.handle(self, cancellable, callback), + Err(e) => match e { // return failure response on unsupported scheme detected - request::Error::Unsupported => Response::Failure(response::Failure::Error { + Error::Unsupported => callback(Response::Failure(Failure::Error { message: "Request scheme yet not supported".to_string(), + })), + _ => request::lookup(query, Some(&cancellable), |result| { + callback(match result { + // redirection with scheme auto-complete or default search provider + Ok(request) => Response::Redirect(Redirect::Foreground(request)), + // unresolvable request issue + Err(e) => Response::Failure(Failure::Error { + message: e.to_string(), + }), + }) }), - // request redirection to default search provider - _ => Response::Redirect(response::Redirect::Foreground(request::search(query))), - }), + }, } } 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 a50fcf04..3c6c6145 100644 --- a/src/app/browser/window/tab/item/page/client/request.rs +++ b/src/app/browser/window/tab/item/page/client/request.rs @@ -120,10 +120,10 @@ pub fn search(query: &str) -> Request { /// Asynchronously check request string contain resolvable query /// * useful for scheme-less requests, before do search redirect -fn lookup( +pub fn lookup( query: &str, cancellable: Option<&Cancellable>, - callback: impl FnOnce(Request) + 'static, + callback: impl FnOnce(Result) + 'static, ) { use gtk::{ gio::{NetworkAddress, Resolver}, @@ -132,7 +132,7 @@ fn lookup( const DEFAULT_SCHEME: &str = "gemini://"; const DEFAULT_PORT: u16 = 1965; - const TIMEOUT: u32 = 500; + const TIMEOUT: u32 = 1000; let request = match query.trim().strip_prefix(DEFAULT_SCHEME) { Some(postfix) => format!("{DEFAULT_SCHEME}{postfix}"), @@ -146,12 +146,15 @@ fn lookup( Ok(connectable) => { resolver.lookup_by_name_async(&connectable.hostname(), cancellable, move |resolve| { callback(if resolve.is_ok() { - search(&request) // @TODO begin direct request + match Uri::parse(&request, UriFlags::NONE) { + Ok(uri) => Request::from_uri(uri, None, None), + Err(e) => Err(Error::Glib(e)), + } } else { - search(&request) + Ok(search(&request)) }) }) } - Err(_) => callback(search(&request)), + Err(_) => callback(Ok(search(&request))), // @TODO not completed yet, fix invalid URI issue } }