diff --git a/src/client/certificate/scope.rs b/src/client/certificate/scope.rs index b50b051..5c25cc2 100644 --- a/src/client/certificate/scope.rs +++ b/src/client/certificate/scope.rs @@ -1,6 +1,8 @@ pub mod error; pub use error::Error; +use crate::client::DEFAULT_PORT; +use gio::NetworkAddress; use glib::{GString, Uri, UriFlags, UriHideFlags}; /// Scope implement path prefix to apply TLS authorization for @@ -38,4 +40,14 @@ impl Scope { self.uri .to_string_partial(UriHideFlags::QUERY | UriHideFlags::FRAGMENT) } + + /// Get [NetworkAddress](https://docs.gtk.org/gio/class.NetworkAddress.html) + /// implement [SocketConnectable](https://docs.gtk.org/gio/iface.SocketConnectable.html) interface + /// * useful as [SNI](https://geminiprotocol.net/docs/protocol-specification.gmi#server-name-indication) in TLS context + pub fn to_network_address(&self) -> Result { + match crate::gio::network_address::from_uri(&self.uri, DEFAULT_PORT) { + Ok(network_address) => Ok(network_address), + Err(reason) => Err(Error::NetworkAddress(reason)), + } + } } diff --git a/src/client/certificate/scope/error.rs b/src/client/certificate/scope/error.rs index ea32f87..f41c3b3 100644 --- a/src/client/certificate/scope/error.rs +++ b/src/client/certificate/scope/error.rs @@ -3,6 +3,7 @@ use std::fmt::{Display, Formatter, Result}; #[derive(Debug)] pub enum Error { Host, + NetworkAddress(crate::gio::network_address::Error), Scheme, Uri(glib::Error), } @@ -13,6 +14,9 @@ impl Display for Error { Self::Host => { write!(f, "Host required") } + Self::NetworkAddress(reason) => { + write!(f, "Could not parse network address: {reason}") + } Self::Scheme => { write!(f, "Scope does not match `gemini`") }