diff options
-rw-r--r-- | src/main.rs | 35 | ||||
-rw-r--r-- | src/origins.rs | 33 |
2 files changed, 35 insertions, 33 deletions
diff --git a/src/main.rs b/src/main.rs index 8514b29..e8869c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use hyper::service::{make_service_fn, service_fn}; use hyper::Method; use hyper::StatusCode; use hyper::{Body, Server}; +use origins::HttpOrigin; use percent_encoding::percent_decode_str; use pulldown_cmark::html; use pulldown_cmark::Options; @@ -27,9 +28,7 @@ use std::env; use std::path::Component; use std::path::Path; use std::path::PathBuf; -use std::str::FromStr; use std::sync::Arc; -use url::Url; #[cfg(unix)] use { @@ -46,6 +45,7 @@ mod diff; mod error; mod forms; mod get_routes; +mod origins; mod post_routes; mod shares; @@ -115,37 +115,6 @@ async fn main() { } } -#[derive(Clone, Debug)] -struct HttpOrigin { - origin: String, - host_idx: usize, -} - -impl HttpOrigin { - /// Returns the Host header value (e.g. `example.com` for the origin `https://example.com`). - fn host(&self) -> &str { - &self.origin[self.host_idx..] - } -} - -impl FromStr for HttpOrigin { - type Err = &'static str; - - fn from_str(s: &str) -> Result<Self, Self::Err> { - 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 /".into()); - } - Ok(HttpOrigin { - origin: url.origin().ascii_serialization(), - host_idx: url.scheme().len() + "://".len(), - }) - } -} - async fn serve<C: Controller + Send + Sync + 'static>(controller: C, args: Args) { let controller = Arc::new(controller); diff --git a/src/origins.rs b/src/origins.rs new file mode 100644 index 0000000..c2a7dc1 --- /dev/null +++ b/src/origins.rs @@ -0,0 +1,33 @@ +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<Self, Self::Err> { + 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 /".into()); + } + Ok(HttpOrigin { + origin: url.origin().ascii_serialization(), + host_idx: url.scheme().len() + "://".len(), + }) + } +} |