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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::{
cell::{Ref, RefCell, RefMut},
fmt, mem, net,
rc::Rc,
str,
};
use http::{header, Method, Uri, Version};
use crate::{
extensions::Extensions,
header::HeaderMap,
message::{Message, RequestHead},
payload::{Payload, PayloadStream},
HttpMessage,
};
pub struct Request<P = PayloadStream> {
pub(crate) payload: Payload<P>,
pub(crate) head: Message<RequestHead>,
pub(crate) conn_data: Option<Rc<Extensions>>,
pub(crate) req_data: RefCell<Extensions>,
}
impl<P> HttpMessage for Request<P> {
type Stream = P;
#[inline]
fn headers(&self) -> &HeaderMap {
&self.head().headers
}
fn take_payload(&mut self) -> Payload<P> {
mem::replace(&mut self.payload, Payload::None)
}
#[inline]
fn extensions(&self) -> Ref<'_, Extensions> {
self.req_data.borrow()
}
#[inline]
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
self.req_data.borrow_mut()
}
}
impl From<Message<RequestHead>> for Request<PayloadStream> {
fn from(head: Message<RequestHead>) -> Self {
Request {
head,
payload: Payload::None,
req_data: RefCell::new(Extensions::default()),
conn_data: None,
}
}
}
impl Request<PayloadStream> {
pub fn new() -> Request<PayloadStream> {
Request {
head: Message::new(),
payload: Payload::None,
req_data: RefCell::new(Extensions::default()),
conn_data: None,
}
}
}
impl<P> Request<P> {
pub fn with_payload(payload: Payload<P>) -> Request<P> {
Request {
payload,
head: Message::new(),
req_data: RefCell::new(Extensions::default()),
conn_data: None,
}
}
pub fn replace_payload<P1>(self, payload: Payload<P1>) -> (Request<P1>, Payload<P>) {
let pl = self.payload;
(
Request {
payload,
head: self.head,
req_data: self.req_data,
conn_data: self.conn_data,
},
pl,
)
}
pub fn payload(&mut self) -> &mut Payload<P> {
&mut self.payload
}
pub fn take_payload(&mut self) -> Payload<P> {
mem::replace(&mut self.payload, Payload::None)
}
pub fn into_parts(self) -> (Message<RequestHead>, Payload<P>) {
(self.head, self.payload)
}
#[inline]
pub fn head(&self) -> &RequestHead {
&*self.head
}
#[inline]
#[doc(hidden)]
pub fn head_mut(&mut self) -> &mut RequestHead {
&mut *self.head
}
pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.head.headers
}
#[inline]
pub fn uri(&self) -> &Uri {
&self.head().uri
}
#[inline]
pub fn uri_mut(&mut self) -> &mut Uri {
&mut self.head.uri
}
#[inline]
pub fn method(&self) -> &Method {
&self.head().method
}
#[inline]
pub fn version(&self) -> Version {
self.head().version
}
#[inline]
pub fn path(&self) -> &str {
self.head().uri.path()
}
#[inline]
pub fn upgrade(&self) -> bool {
if let Some(conn) = self.head().headers.get(header::CONNECTION) {
if let Ok(s) = conn.to_str() {
return s.to_lowercase().contains("upgrade");
}
}
self.head().method == Method::CONNECT
}
#[inline]
pub fn peer_addr(&self) -> Option<net::SocketAddr> {
self.head().peer_addr
}
pub fn conn_data<T: 'static>(&self) -> Option<&T> {
self.conn_data
.as_deref()
.and_then(|container| container.get::<T>())
}
pub fn take_conn_data(&mut self) -> Option<Rc<Extensions>> {
self.conn_data.take()
}
pub fn take_req_data(&mut self) -> Extensions {
mem::take(&mut self.req_data.get_mut())
}
}
impl<P> fmt::Debug for Request<P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"\nRequest {:?} {}:{}",
self.version(),
self.method(),
self.path()
)?;
if let Some(q) = self.uri().query().as_ref() {
writeln!(f, " query: ?{:?}", q)?;
}
writeln!(f, " headers:")?;
for (key, val) in self.headers().iter() {
writeln!(f, " {:?}: {:?}", key, val)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::convert::TryFrom;
#[test]
fn test_basics() {
let msg = Message::new();
let mut req = Request::from(msg);
req.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("text/plain"),
);
assert!(req.headers().contains_key(header::CONTENT_TYPE));
*req.uri_mut() = Uri::try_from("/index.html?q=1").unwrap();
assert_eq!(req.uri().path(), "/index.html");
assert_eq!(req.uri().query(), Some("q=1"));
let s = format!("{:?}", req);
assert!(s.contains("Request HTTP/1.1 GET:/index.html"));
}
}