-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrequest.rs
More file actions
74 lines (62 loc) · 1.7 KB
/
request.rs
File metadata and controls
74 lines (62 loc) · 1.7 KB
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
use std::ops;
use anyhow::*;
use percent_encoding::percent_decode_str;
use uriparse::URIReference;
use rustls::Certificate;
pub struct Request {
uri: URIReference<'static>,
input: Option<String>,
certificate: Option<Certificate>,
}
impl Request {
pub fn from_uri(uri: URIReference<'static>) -> Result<Self> {
Self::with_certificate(uri, None)
}
pub fn with_certificate(
mut uri: URIReference<'static>,
certificate: Option<Certificate>
) -> Result<Self> {
uri.normalize();
let input = match uri.query() {
None => None,
Some(query) => {
let input = percent_decode_str(query.as_str())
.decode_utf8()
.context("Request URI query contains invalid UTF-8")?
.into_owned();
Some(input)
}
};
Ok(Self {
uri,
input,
certificate,
})
}
pub fn uri(&self) -> &URIReference {
&self.uri
}
pub fn path_segments(&self) -> Vec<String> {
self.uri()
.path()
.segments()
.iter()
.map(|segment| percent_decode_str(segment.as_str()).decode_utf8_lossy().into_owned())
.collect::<Vec<String>>()
}
pub fn input(&self) -> Option<&str> {
self.input.as_deref()
}
pub fn set_cert(&mut self, cert: Option<Certificate>) {
self.certificate = cert;
}
pub fn certificate(&self) -> Option<&Certificate> {
self.certificate.as_ref()
}
}
impl ops::Deref for Request {
type Target = URIReference<'static>;
fn deref(&self) -> &Self::Target {
&self.uri
}
}