1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use super::Uri;
use http::uri::{Scheme, Authority, PathAndQuery};
pub use form_urlencoded::Parse as QueryIter;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Url {
scheme: Scheme,
authority: Authority,
path_and_query: PathAndQuery
}
impl Url {
pub fn from_inner(inner: Uri) -> Option<Self> {
let parts = inner.into_parts();
Some(Self {
scheme: parts.scheme?,
authority: parts.authority?,
path_and_query: parts.path_and_query
.unwrap_or_else(|| PathAndQuery::from_static("/"))
})
}
pub fn scheme(&self) -> &str {
self.scheme.as_str()
}
pub fn is_https(&self) -> bool {
self.scheme == Scheme::HTTPS
}
pub fn is_http(&self) -> bool {
self.scheme == Scheme::HTTP
}
pub fn host(&self) -> &str {
self.authority.host()
}
pub fn port(&self) -> Option<u16> {
self.authority.port_u16()
}
pub fn path(&self) -> &str {
self.path_and_query.path()
}
pub fn path_segments(&self) -> std::str::Split<'_, char> {
let path = self.path();
let path = path.strip_prefix('/').unwrap_or(path);
let path = path.strip_suffix('/').unwrap_or(path);
path.split('/')
}
pub fn query(&self) -> Option<&str> {
self.path_and_query.query()
}
pub fn parse_query_pairs(&self) -> QueryIter {
form_urlencoded::parse(self.query().unwrap_or("").as_bytes())
}
}