diff --git a/src/service/resolver/actual.rs b/src/service/resolver/actual.rs index ea4b1100..660498f7 100644 --- a/src/service/resolver/actual.rs +++ b/src/service/resolver/actual.rs @@ -18,34 +18,33 @@ use crate::resolver::{ pub(crate) struct ActualDest { pub(crate) dest: FedDest, pub(crate) host: String, - pub(crate) string: String, pub(crate) cached: bool, } +impl ActualDest { + #[inline] + pub(crate) fn string(&self) -> String { self.dest.https_string() } +} + impl super::Service { #[tracing::instrument(skip_all, name = "resolve")] pub(crate) async fn get_actual_dest(&self, server_name: &ServerName) -> Result { - let cached; - let cached_result = self.get_cached_destination(server_name); + let (result, cached) = if let Some(result) = self.get_cached_destination(server_name) { + (result, true) + } else { + self.validate_dest(server_name)?; + (self.resolve_actual_dest(server_name, true).await?, false) + }; let CachedDest { dest, host, .. - } = if let Some(result) = cached_result { - cached = true; - result - } else { - cached = false; - self.validate_dest(server_name)?; - self.resolve_actual_dest(server_name, true).await? - }; + } = result; - let string = dest.clone().into_https_string(); Ok(ActualDest { dest, host, - string, cached, }) } @@ -89,7 +88,7 @@ impl super::Service { debug!("Actual destination: {actual_dest:?} hostname: {host:?}"); Ok(CachedDest { dest: actual_dest, - host: host.into_uri_string(), + host: host.uri_string(), expire: CachedDest::default_expire(), }) } @@ -109,7 +108,7 @@ impl super::Service { async fn actual_dest_3(&self, host: &mut String, cache: bool, delegated: String) -> Result { debug!("3: A .well-known file is available"); - *host = add_port_to_hostname(&delegated).into_uri_string(); + *host = add_port_to_hostname(&delegated).uri_string(); match get_ip_with_port(&delegated) { Some(host_and_port) => Self::actual_dest_3_1(host_and_port), None => { diff --git a/src/service/resolver/fed.rs b/src/service/resolver/fed.rs index 10cbbbdd..79f71f13 100644 --- a/src/service/resolver/fed.rs +++ b/src/service/resolver/fed.rs @@ -1,4 +1,5 @@ use std::{ + borrow::Cow, fmt, net::{IpAddr, SocketAddr}, }; @@ -29,24 +30,25 @@ pub(crate) fn add_port_to_hostname(dest_str: &str) -> FedDest { } impl FedDest { - pub(crate) fn into_https_string(self) -> String { + pub(crate) fn https_string(&self) -> String { match self { Self::Literal(addr) => format!("https://{addr}"), Self::Named(host, port) => format!("https://{host}{port}"), } } - pub(crate) fn into_uri_string(self) -> String { + pub(crate) fn uri_string(&self) -> String { match self { Self::Literal(addr) => addr.to_string(), Self::Named(host, port) => format!("{host}{port}"), } } - pub(crate) fn hostname(&self) -> String { + #[inline] + pub(crate) fn hostname(&self) -> Cow<'_, str> { match &self { - Self::Literal(addr) => addr.ip().to_string(), - Self::Named(host, _) => host.clone(), + Self::Literal(addr) => addr.ip().to_string().into(), + Self::Named(host, _) => host.into(), } } @@ -61,10 +63,5 @@ impl FedDest { } impl fmt::Display for FedDest { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Named(host, port) => write!(f, "{host}{port}"), - Self::Literal(addr) => write!(f, "{addr}"), - } - } + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.uri_string().as_str()) } } diff --git a/src/service/sending/send.rs b/src/service/sending/send.rs index 62da59ef..2fbb3919 100644 --- a/src/service/sending/send.rs +++ b/src/service/sending/send.rs @@ -71,7 +71,7 @@ impl super::Service { trace!("Preparing request"); let mut http_request = req - .try_into_http_request::>(&actual.string, SATIR, &VERSIONS) + .try_into_http_request::>(actual.string().as_str(), SATIR, &VERSIONS) .map_err(|e| err!(BadServerResponse("Invalid destination: {e:?}")))?; self.sign_request::(dest, &mut http_request); @@ -107,7 +107,7 @@ where request_url = ?url, response_url = ?response.url(), "Received response from {}", - actual.string, + actual.string(), ); let mut http_response_builder = http::Response::builder()