use std::str::FromStr; use url::Url; #[derive(Clone, Debug)] pub(crate) struct HttpOrigin { pub origin: String, pub host_idx: usize, } impl HttpOrigin { /// Returns the Host header value (e.g. `example.com` for the origin `https://example.com`). pub fn host(&self) -> &str { &self.origin[self.host_idx..] } } impl FromStr for HttpOrigin { type Err = &'static str; fn from_str(s: &str) -> Result { let url = Url::parse(s).map_err(|_| "invalid URL")?; if url.scheme() != "http" && url.scheme() != "https" { return Err("expected http:// or https:// scheme"); } if url.path() != "/" { return Err("path must be /"); } Ok(HttpOrigin { origin: url.origin().ascii_serialization(), host_idx: url.scheme().len() + "://".len(), }) } }