|
1 | | -use hyper::{HeaderMap, Uri}; |
| 1 | +use http::{HeaderMap, HeaderValue}; |
2 | 2 |
|
3 | | -use crate::{HttpFailResult, HttpRequestBody, HttpRequestHeaders}; |
| 3 | +use hyper::Uri; |
4 | 4 |
|
5 | | -use super::ContentEncoding; |
| 5 | +use crate::{HttpFailResult, HttpRequestBody, HttpRequestBodyMode, MyHyperHttpRequest}; |
6 | 6 |
|
7 | | -pub enum RequestData { |
8 | | - Incoming(Option<hyper::Request<hyper::body::Incoming>>), |
9 | | - AsBody { |
10 | | - uri: Uri, |
11 | | - headers: HeaderMap, |
12 | | - body: Option<HttpRequestBody>, |
13 | | - }, |
14 | | - Taken, |
| 7 | +pub struct RequestData { |
| 8 | + parts: hyper::http::request::Parts, |
| 9 | + body: Option<HttpRequestBodyMode>, |
15 | 10 | } |
16 | 11 |
|
17 | 12 | impl RequestData { |
18 | | - pub async fn convert_to_body_if_requires( |
19 | | - &mut self, |
20 | | - ) -> Result<Option<&HttpRequestBody>, HttpFailResult> { |
21 | | - let (uri, headers, bytes) = match self { |
22 | | - Self::Incoming(incoming) => { |
23 | | - let incoming = incoming.take().unwrap(); |
24 | | - let (parts, incoming) = incoming.into_parts(); |
25 | | - |
26 | | - let headers = parts.headers; |
27 | | - |
28 | | - let content_encoding = headers.get_content_encoding()?; |
29 | | - |
30 | | - let body = read_bytes(content_encoding, incoming).await?; |
31 | | - |
32 | | - (parts.uri, headers, body) |
33 | | - } |
34 | | - Self::AsBody { body, .. } => match body.as_ref() { |
35 | | - Some(itm) => { |
36 | | - return Ok(Some(itm)); |
37 | | - } |
38 | | - None => { |
39 | | - panic!("You are trying to access body content after receiving ownership of it") |
40 | | - } |
41 | | - }, |
42 | | - Self::Taken => { |
43 | | - panic!("Body is taken by some middleware before") |
44 | | - } |
45 | | - }; |
46 | | - |
47 | | - let content_type = headers.try_get_case_insensitive("content-type"); |
48 | | - |
49 | | - let content_type = match content_type { |
50 | | - Some(content_type) => Some(content_type.as_str()?.to_string()), |
51 | | - None => None, |
52 | | - }; |
53 | | - |
54 | | - let body = HttpRequestBody::new(bytes, content_type)?; |
55 | | - *self = Self::AsBody { |
56 | | - body: Some(body), |
57 | | - uri, |
58 | | - headers, |
59 | | - }; |
60 | | - Ok(self.try_unwrap_as_body()) |
| 13 | + pub fn new(req: hyper::Request<hyper::body::Incoming>) -> Self { |
| 14 | + let parts = req.into_parts(); |
| 15 | + Self { |
| 16 | + parts: parts.0, |
| 17 | + body: Some(HttpRequestBodyMode::Incoming(Some(parts.1))), |
| 18 | + } |
61 | 19 | } |
62 | 20 |
|
63 | | - pub async fn receive_body(&mut self) -> Result<HttpRequestBody, HttpFailResult> { |
64 | | - match self { |
65 | | - Self::Incoming(incoming) => { |
66 | | - let incoming = incoming.take().unwrap(); |
67 | | - |
68 | | - let content_encoding = incoming.headers().get_content_encoding()?; |
69 | | - |
70 | | - let bytes = read_bytes(content_encoding, incoming).await?; |
71 | | - |
72 | | - let body = HttpRequestBody::new(bytes, None)?; |
73 | | - return Ok(body); |
74 | | - } |
75 | | - Self::AsBody { body, .. } => match body.take() { |
76 | | - Some(itm) => { |
77 | | - return Ok(itm); |
78 | | - } |
79 | | - None => { |
80 | | - panic!("You are trying to receive body for a second time") |
81 | | - } |
82 | | - }, |
83 | | - Self::Taken => { |
84 | | - panic!("Body is taken by some middleware before") |
| 21 | + pub async fn get_body(&mut self) -> Result<&HttpRequestBody, HttpFailResult> { |
| 22 | + match self.body.as_mut() { |
| 23 | + Some(body) => body.get_http_request_body().await, |
| 24 | + None => { |
| 25 | + panic!("Body is removed and can not be accessed") |
85 | 26 | } |
86 | | - }; |
| 27 | + } |
87 | 28 | } |
88 | 29 |
|
89 | | - fn try_unwrap_as_body(&self) -> Option<&HttpRequestBody> { |
90 | | - match self { |
91 | | - Self::Incoming(_) => None, |
92 | | - Self::AsBody { body, .. } => { |
93 | | - let body = body.as_ref()?; |
94 | | - return Some(body); |
95 | | - } |
96 | | - Self::Taken => { |
| 30 | + pub async fn receive_body(&mut self) -> Result<HttpRequestBody, HttpFailResult> { |
| 31 | + match self.body.take() { |
| 32 | + Some(body) => return body.into_http_request_body().await, |
| 33 | + None => { |
97 | 34 | panic!("Body is taken by some middleware before") |
98 | 35 | } |
99 | 36 | } |
100 | 37 | } |
101 | 38 |
|
102 | 39 | pub fn uri(&self) -> &Uri { |
103 | | - match self { |
104 | | - Self::Incoming(incoming) => incoming.as_ref().unwrap().uri(), |
105 | | - Self::AsBody { uri, .. } => uri, |
106 | | - Self::Taken => { |
107 | | - panic!("Body is taken by some middleware before") |
108 | | - } |
109 | | - } |
| 40 | + &self.parts.uri |
110 | 41 | } |
111 | 42 |
|
112 | | - pub fn headers(&self) -> &impl HttpRequestHeaders { |
113 | | - match self { |
114 | | - Self::Incoming(incoming) => incoming.as_ref().unwrap().headers(), |
115 | | - Self::AsBody { headers, .. } => headers, |
116 | | - Self::Taken => { |
117 | | - panic!("Body is taken by some middleware before") |
118 | | - } |
119 | | - } |
| 43 | + pub fn headers(&self) -> &HeaderMap<HeaderValue> { |
| 44 | + &self.parts.headers |
120 | 45 | } |
121 | 46 |
|
122 | | - pub fn take_incoming_body(&mut self) -> hyper::Request<hyper::body::Incoming> { |
123 | | - match self { |
124 | | - Self::Incoming(incoming) => { |
125 | | - let result = incoming.take().unwrap(); |
126 | | - *self = Self::Taken; |
127 | | - result |
128 | | - } |
129 | | - Self::AsBody { .. } => { |
130 | | - panic!("Body is taken by some middleware before") |
131 | | - } |
132 | | - Self::Taken => { |
| 47 | + pub fn take_my_hyper_http_request(&mut self) -> MyHyperHttpRequest { |
| 48 | + match self.body.take() { |
| 49 | + Some(body) => match body { |
| 50 | + HttpRequestBodyMode::Incoming(mut incoming) => { |
| 51 | + let result = |
| 52 | + hyper::Request::from_parts(self.parts.clone(), incoming.take().unwrap()); |
| 53 | + |
| 54 | + return MyHyperHttpRequest::Incoming(result); |
| 55 | + } |
| 56 | + HttpRequestBodyMode::Full(body) => { |
| 57 | + let body = body.as_slice().to_vec(); |
| 58 | + |
| 59 | + let body = http_body_util::Full::new(bytes::Bytes::from(body)); |
| 60 | + |
| 61 | + let req = hyper::Request::from_parts(self.parts.clone(), body); |
| 62 | + |
| 63 | + MyHyperHttpRequest::Full(req) |
| 64 | + } |
| 65 | + }, |
| 66 | + None => { |
133 | 67 | panic!("Body is taken by some middleware before") |
134 | 68 | } |
135 | 69 | } |
136 | 70 | } |
137 | 71 | } |
138 | | - |
139 | | -async fn read_bytes( |
140 | | - body_compression: ContentEncoding, |
141 | | - incoming: impl hyper::body::Body<Data = hyper::body::Bytes, Error = hyper::Error>, |
142 | | -) -> Result<Vec<u8>, HttpFailResult> { |
143 | | - use http_body_util::BodyExt; |
144 | | - |
145 | | - let collected = incoming.collect().await?; |
146 | | - let bytes = collected.to_bytes(); |
147 | | - |
148 | | - body_compression.decompress_if_needed(bytes) |
149 | | -} |
0 commit comments