aboutsummaryrefslogtreecommitdiff
path: root/src/origins.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/origins.rs')
-rw-r--r--src/origins.rs33
1 files changed, 33 insertions, 0 deletions
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(),
+ })
+ }
+}