chuchi_core/header/
url.rs

1use super::Uri;
2
3pub use form_urlencoded::Parse as QueryIter;
4pub use http::uri::{Authority, PathAndQuery, Scheme};
5
6/// Contains a request url.
7///
8/// This is a wrapper around `Uri` with the caveat that a scheme
9/// and an authority is set, which makes it a Url.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct Url {
12	scheme: Scheme,
13	authority: Authority,
14	path_and_query: PathAndQuery,
15}
16
17impl Url {
18	/// Creates a new `Uri` from an `http::Uri`
19	///
20	/// Returns None if the `http::Uri` does not contain a scheme or authority.
21	pub fn from_inner(inner: Uri) -> Option<Self> {
22		let parts = inner.into_parts();
23		Some(Self {
24			scheme: parts.scheme?,
25			authority: parts.authority?,
26			path_and_query: parts
27				.path_and_query
28				.unwrap_or_else(|| PathAndQuery::from_static("/")),
29		})
30	}
31
32	/// Returns the used scheme.
33	pub fn scheme(&self) -> &str {
34		self.scheme.as_str()
35	}
36
37	/// Returns true if the used scheme is https.
38	pub fn is_https(&self) -> bool {
39		self.scheme == Scheme::HTTPS
40	}
41
42	/// Returns true if the used scheme is http.
43	pub fn is_http(&self) -> bool {
44		self.scheme == Scheme::HTTP
45	}
46
47	/// Returns the host.
48	pub fn host(&self) -> &str {
49		self.authority.host()
50	}
51
52	/// Returns the used port if any.
53	pub fn port(&self) -> Option<u16> {
54		self.authority.port_u16()
55	}
56
57	/// Returns the path.
58	pub fn path(&self) -> &str {
59		self.path_and_query.path()
60	}
61
62	/// Returns the path as segments divided by a slash, first starting and
63	/// ending slash removed.
64	pub fn path_segments(&self) -> std::str::Split<'_, char> {
65		let path = self.path();
66		let path = path.strip_prefix('/').unwrap_or(path);
67		let path = path.strip_suffix('/').unwrap_or(path);
68		path.split('/')
69	}
70
71	/// Returns the query string.
72	pub fn query(&self) -> Option<&str> {
73		self.path_and_query.query()
74	}
75
76	// named as parse_query_pairs since maybe it would make sense
77	// to make a separate type which allows to lookup pairs
78	// and deserialize values in it which would be in `query_pairs`
79	//
80	/// Returns an iterator with the Item `(Cow<'_, str>, Cow<'_, str>)`
81	///
82	/// Key and values are percent decoded.
83	pub fn parse_query_pairs(&self) -> QueryIter {
84		form_urlencoded::parse(self.query().unwrap_or("").as_bytes())
85	}
86}