connect dns lookup detector to handle scheme-less uri without search redirection

This commit is contained in:
yggverse 2025-01-19 07:19:37 +02:00
parent 22bfec8c24
commit d4efcbca37
2 changed files with 29 additions and 12 deletions

View File

@ -73,16 +73,30 @@ impl Client {
time: now(), time: now(),
value: query.to_string(), value: query.to_string(),
}); });
use request::Error;
use response::{Failure, Redirect};
let cancellable = self.new_cancellable();
match Request::parse(query, None) { match Request::parse(query, None) {
Ok(request) => request.handle(self, self.new_cancellable(), callback), Ok(request) => request.handle(self, cancellable, callback),
Err(e) => callback(match e { Err(e) => match e {
// return failure response on unsupported scheme detected // 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(), 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))),
}), }),
},
} }
} }

View File

@ -120,10 +120,10 @@ pub fn search(query: &str) -> Request {
/// Asynchronously check request string contain resolvable query /// Asynchronously check request string contain resolvable query
/// * useful for scheme-less requests, before do search redirect /// * useful for scheme-less requests, before do search redirect
fn lookup( pub fn lookup(
query: &str, query: &str,
cancellable: Option<&Cancellable>, cancellable: Option<&Cancellable>,
callback: impl FnOnce(Request) + 'static, callback: impl FnOnce(Result<Request, Error>) + 'static,
) { ) {
use gtk::{ use gtk::{
gio::{NetworkAddress, Resolver}, gio::{NetworkAddress, Resolver},
@ -132,7 +132,7 @@ fn lookup(
const DEFAULT_SCHEME: &str = "gemini://"; const DEFAULT_SCHEME: &str = "gemini://";
const DEFAULT_PORT: u16 = 1965; const DEFAULT_PORT: u16 = 1965;
const TIMEOUT: u32 = 500; const TIMEOUT: u32 = 1000;
let request = match query.trim().strip_prefix(DEFAULT_SCHEME) { let request = match query.trim().strip_prefix(DEFAULT_SCHEME) {
Some(postfix) => format!("{DEFAULT_SCHEME}{postfix}"), Some(postfix) => format!("{DEFAULT_SCHEME}{postfix}"),
@ -146,12 +146,15 @@ fn lookup(
Ok(connectable) => { Ok(connectable) => {
resolver.lookup_by_name_async(&connectable.hostname(), cancellable, move |resolve| { resolver.lookup_by_name_async(&connectable.hostname(), cancellable, move |resolve| {
callback(if resolve.is_ok() { 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 { } else {
search(&request) Ok(search(&request))
}) })
}) })
} }
Err(_) => callback(search(&request)), Err(_) => callback(Ok(search(&request))), // @TODO not completed yet, fix invalid URI issue
} }
} }