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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::fmt;
use std::net::SocketAddr;
use version::HttpVersion;
use method::Method;
use header::Headers;
use http::{RequestHead, MessageHead, RequestLine, Body};
use uri::RequestUri;
pub struct Request {
method: Method,
uri: RequestUri,
version: HttpVersion,
headers: Headers,
remote_addr: SocketAddr,
body: Body,
}
impl Request {
#[inline]
pub fn method(&self) -> &Method { &self.method }
#[inline]
pub fn headers(&self) -> &Headers { &self.headers }
#[inline]
pub fn uri(&self) -> &RequestUri { &self.uri }
#[inline]
pub fn version(&self) -> &HttpVersion { &self.version }
#[inline]
pub fn remote_addr(&self) -> &SocketAddr { &self.remote_addr }
#[inline]
pub fn path(&self) -> Option<&str> {
match self.uri {
RequestUri::AbsolutePath { path: ref p, .. } => Some(p.as_str()),
RequestUri::AbsoluteUri(ref url) => Some(url.path()),
_ => None,
}
}
#[inline]
pub fn query(&self) -> Option<&str> {
match self.uri {
RequestUri::AbsolutePath { query: ref q, .. } => q.as_ref().map(|x| x.as_str()),
RequestUri::AbsoluteUri(ref url) => url.query(),
_ => None,
}
}
#[inline]
pub fn body(self) -> Body {
self.body
}
#[inline]
pub fn deconstruct(self) -> (Method, RequestUri, HttpVersion, Headers, Body) {
(self.method, self.uri, self.version, self.headers, self.body)
}
}
impl fmt::Debug for Request {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Request")
.field("method", &self.method)
.field("uri", &self.uri)
.field("version", &self.version)
.field("remote_addr", &self.remote_addr)
.field("headers", &self.headers)
.finish()
}
}
pub fn new(addr: SocketAddr, incoming: RequestHead, body: Body) -> Request {
let MessageHead { version, subject: RequestLine(method, uri), headers } = incoming;
debug!("Request Line: {:?} {:?} {:?}", method, uri, version);
debug!("{:#?}", headers);
Request {
method: method,
uri: uri,
headers: headers,
version: version,
remote_addr: addr,
body: body,
}
}