Something went wrong. Try again.
Chris’s PDS implementation
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147use crate::error::ErrorContext;use std::fmt;
use url::Host;use url::Url;
use crate::error::{Error, Result};
use super::DeploymentMode;
/// A canonical HTTP origin for public protocol metadata, request proofs, or relay requests.#[derive(Clone, Debug, Eq, PartialEq)]pub struct HttpOrigin(Url);
impl HttpOrigin { /// Parse an HTTP origin, rejecting URL components that are not part of an /// origin. Production origins must use HTTPS. /// /// # Errors /// /// Returns a configuration error for malformed, non-HTTP, non-origin, or /// non-HTTPS production values. pub fn parse_public(value: &str, deployment_mode: DeploymentMode) -> Result<Self> { let url = Url::parse(value).context(Error::Configuration, "invalid CPDS_PUBLIC_URL")?; if !is_http_origin(&url) { return Err(Error::Configuration( "CPDS_PUBLIC_URL must be an HTTP origin without credentials, path, query, or fragment" .to_owned(), )); } if deployment_mode == DeploymentMode::Production && url.scheme() != "https" { return Err(Error::Configuration( "CPDS_PUBLIC_URL must use HTTPS in production".to_owned(), )); } Ok(Self(url)) }
/// A configured relay origin eligible for explicit crawl requests. pub(super) fn parse_relay(value: &str, deployment_mode: DeploymentMode) -> Result<Self> { let url = Url::parse(value).context(Error::Configuration, "invalid CPDS_RELAYS entry")?; if !is_http_origin(&url) { return Err(Error::Configuration( "CPDS_RELAYS entries must be HTTP origins without credentials, path, query, or fragment" .to_owned(), )); } if deployment_mode == DeploymentMode::Production && url.scheme() != "https" { return Err(Error::Configuration( "CPDS_RELAYS entries must use HTTPS in production".to_owned(), )); } Ok(Self(url)) }
#[must_use] pub fn as_url(&self) -> &Url { &self.0 }
#[must_use] pub fn endpoint(&self, path: &str) -> Url { let mut endpoint = self.0.clone(); endpoint.set_path(path); endpoint }
#[must_use] /// Return the externally reachable host and explicit port. /// /// # Panics /// /// Panics only if the private validated-origin invariant is broken. pub fn hostname(&self) -> String { let host = match self.0.host().expect("validated public origin has a host") { Host::Domain(host) => host.to_owned(), Host::Ipv4(host) => host.to_string(), Host::Ipv6(host) => format!("[{host}]"), }; self.0 .port() .map_or_else(|| host.clone(), |port| format!("{host}:{port}")) }}
impl fmt::Display for HttpOrigin { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(formatter) }}
fn is_http_origin(url: &Url) -> bool { matches!(url.scheme(), "http" | "https") && url.host().is_some() && url.username().is_empty() && url.password().is_none() && url.path() == "/" && url.query().is_none() && url.fragment().is_none()}
#[cfg(test)]mod tests { use super::*;
#[test] fn accepts_canonical_local_and_production_origins() { let local = HttpOrigin::parse_public("http://127.0.0.1:3000", DeploymentMode::Local).unwrap(); assert_eq!(local.to_string(), "http://127.0.0.1:3000/"); assert_eq!(local.hostname(), "127.0.0.1:3000"); assert_eq!( local.endpoint("/xrpc/example").as_str(), "http://127.0.0.1:3000/xrpc/example" );
let production = HttpOrigin::parse_public("https://pds.example.com/", DeploymentMode::Production) .unwrap(); assert_eq!(production.to_string(), "https://pds.example.com/"); assert_eq!(production.hostname(), "pds.example.com"); assert_eq!( HttpOrigin::parse_public("http://[::1]:3000", DeploymentMode::Local) .unwrap() .hostname(), "[::1]:3000" ); }
#[test] fn rejects_non_origin_components_and_unsafe_schemes() { for value in [ "ftp://pds.example.com/", "https://user@pds.example.com/", "https://pds.example.com/path", "https://pds.example.com/?query=yes", "https://pds.example.com/#fragment", ] { assert!(HttpOrigin::parse_public(value, DeploymentMode::Local).is_err()); } assert!( HttpOrigin::parse_public("http://pds.example.com/", DeploymentMode::Production) .is_err() ); }}