generate ActualDest https string on the fly

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-28 06:49:25 +00:00 committed by strawberry
parent 7a09ac81e0
commit 52e356d780
3 changed files with 24 additions and 28 deletions

View file

@ -18,34 +18,33 @@ use crate::resolver::{
pub(crate) struct ActualDest { pub(crate) struct ActualDest {
pub(crate) dest: FedDest, pub(crate) dest: FedDest,
pub(crate) host: String, pub(crate) host: String,
pub(crate) string: String,
pub(crate) cached: bool, pub(crate) cached: bool,
} }
impl ActualDest {
#[inline]
pub(crate) fn string(&self) -> String { self.dest.https_string() }
}
impl super::Service { impl super::Service {
#[tracing::instrument(skip_all, name = "resolve")] #[tracing::instrument(skip_all, name = "resolve")]
pub(crate) async fn get_actual_dest(&self, server_name: &ServerName) -> Result<ActualDest> { pub(crate) async fn get_actual_dest(&self, server_name: &ServerName) -> Result<ActualDest> {
let cached; let (result, cached) = if let Some(result) = self.get_cached_destination(server_name) {
let cached_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 { let CachedDest {
dest, dest,
host, host,
.. ..
} = if let Some(result) = cached_result { } = result;
cached = true;
result
} else {
cached = false;
self.validate_dest(server_name)?;
self.resolve_actual_dest(server_name, true).await?
};
let string = dest.clone().into_https_string();
Ok(ActualDest { Ok(ActualDest {
dest, dest,
host, host,
string,
cached, cached,
}) })
} }
@ -89,7 +88,7 @@ impl super::Service {
debug!("Actual destination: {actual_dest:?} hostname: {host:?}"); debug!("Actual destination: {actual_dest:?} hostname: {host:?}");
Ok(CachedDest { Ok(CachedDest {
dest: actual_dest, dest: actual_dest,
host: host.into_uri_string(), host: host.uri_string(),
expire: CachedDest::default_expire(), 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<FedDest> { async fn actual_dest_3(&self, host: &mut String, cache: bool, delegated: String) -> Result<FedDest> {
debug!("3: A .well-known file is available"); 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) { match get_ip_with_port(&delegated) {
Some(host_and_port) => Self::actual_dest_3_1(host_and_port), Some(host_and_port) => Self::actual_dest_3_1(host_and_port),
None => { None => {

View file

@ -1,4 +1,5 @@
use std::{ use std::{
borrow::Cow,
fmt, fmt,
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
}; };
@ -29,24 +30,25 @@ pub(crate) fn add_port_to_hostname(dest_str: &str) -> FedDest {
} }
impl FedDest { impl FedDest {
pub(crate) fn into_https_string(self) -> String { pub(crate) fn https_string(&self) -> String {
match self { match self {
Self::Literal(addr) => format!("https://{addr}"), Self::Literal(addr) => format!("https://{addr}"),
Self::Named(host, port) => format!("https://{host}{port}"), 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 { match self {
Self::Literal(addr) => addr.to_string(), Self::Literal(addr) => addr.to_string(),
Self::Named(host, port) => format!("{host}{port}"), Self::Named(host, port) => format!("{host}{port}"),
} }
} }
pub(crate) fn hostname(&self) -> String { #[inline]
pub(crate) fn hostname(&self) -> Cow<'_, str> {
match &self { match &self {
Self::Literal(addr) => addr.ip().to_string(), Self::Literal(addr) => addr.ip().to_string().into(),
Self::Named(host, _) => host.clone(), Self::Named(host, _) => host.into(),
} }
} }
@ -61,10 +63,5 @@ impl FedDest {
} }
impl fmt::Display for FedDest { impl fmt::Display for FedDest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.uri_string().as_str()) }
match self {
Self::Named(host, port) => write!(f, "{host}{port}"),
Self::Literal(addr) => write!(f, "{addr}"),
}
}
} }

View file

@ -71,7 +71,7 @@ impl super::Service {
trace!("Preparing request"); trace!("Preparing request");
let mut http_request = req let mut http_request = req
.try_into_http_request::<Vec<u8>>(&actual.string, SATIR, &VERSIONS) .try_into_http_request::<Vec<u8>>(actual.string().as_str(), SATIR, &VERSIONS)
.map_err(|e| err!(BadServerResponse("Invalid destination: {e:?}")))?; .map_err(|e| err!(BadServerResponse("Invalid destination: {e:?}")))?;
self.sign_request::<T>(dest, &mut http_request); self.sign_request::<T>(dest, &mut http_request);
@ -107,7 +107,7 @@ where
request_url = ?url, request_url = ?url,
response_url = ?response.url(), response_url = ?response.url(),
"Received response from {}", "Received response from {}",
actual.string, actual.string(),
); );
let mut http_response_builder = http::Response::builder() let mut http_response_builder = http::Response::builder()