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
use super::RequestContext;
use crate::data_map::SharedDataMap;
use hyper::{Body, HeaderMap, Method, Request, Uri, Version};
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;
#[derive(Clone)]
pub struct RequestInfo {
pub(crate) req_info_inner: Arc<RequestInfoInner>,
pub(crate) shared_data_maps: Option<Vec<SharedDataMap>>,
pub(crate) context: RequestContext,
}
#[derive(Debug)]
pub(crate) struct RequestInfoInner {
headers: HeaderMap,
method: Method,
uri: Uri,
version: Version,
}
impl RequestInfo {
pub(crate) fn new_from_req(req: &Request<Body>, ctx: RequestContext) -> Self {
let inner = RequestInfoInner {
headers: req.headers().clone(),
method: req.method().clone(),
uri: req.uri().clone(),
version: req.version(),
};
RequestInfo {
req_info_inner: Arc::new(inner),
shared_data_maps: None,
context: ctx,
}
}
pub fn headers(&self) -> &HeaderMap {
&self.req_info_inner.headers
}
pub fn method(&self) -> &Method {
&self.req_info_inner.method
}
pub fn uri(&self) -> &Uri {
&self.req_info_inner.uri
}
pub fn version(&self) -> Version {
self.req_info_inner.version
}
pub fn data<T: Send + Sync + 'static>(&self) -> Option<&T> {
if let Some(ref shared_data_maps) = self.shared_data_maps {
for shared_data_map in shared_data_maps.iter() {
if let Some(data) = shared_data_map.inner.get::<T>() {
return Some(data);
}
}
}
None
}
pub fn context<T: Send + Sync + Clone + 'static>(&self) -> Option<T> {
self.context.get::<T>()
}
}
impl Debug for RequestInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.req_info_inner)
}
}