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
use std::io;
use bytes::Bytes;
use futures::stream::{Stream, TryStreamExt};
use http::{Extensions, HeaderMap, HeaderValue, Method, Uri, Version};
use hyper::Body;
use tokio::io::AsyncRead;
use tokio_util::io::StreamReader;
pub struct Request {
pub method: Method,
pub uri: Uri,
pub version: Version,
pub headers: HeaderMap<HeaderValue>,
extensions: Extensions,
body: Body,
}
impl Request {
#[inline]
pub fn take_raw(&mut self) -> http::Request<Body> {
let mut builder = http::Request::builder()
.method(self.method.clone())
.uri(self.uri.clone());
*builder.extensions_mut().expect("fail to get extensions") =
std::mem::take(&mut self.extensions);
*builder.headers_mut().expect("fail to get headers") = self.headers.clone();
builder
.body(self.raw_body())
.expect("fail to build raw body")
}
#[inline]
pub fn raw_body(&mut self) -> Body {
std::mem::take(&mut self.body)
}
#[inline]
pub fn stream(
&mut self,
) -> impl Stream<Item = io::Result<Bytes>> + Sync + Send + Unpin + 'static {
self.raw_body()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
}
#[inline]
pub fn reader(&mut self) -> impl AsyncRead + Sync + Send + Unpin + 'static {
StreamReader::new(self.stream())
}
}
impl From<http::Request<Body>> for Request {
#[inline]
fn from(req: http::Request<Body>) -> Self {
let (parts, body) = req.into_parts();
Self {
method: parts.method,
uri: parts.uri,
version: parts.version,
headers: parts.headers,
extensions: parts.extensions,
body,
}
}
}
impl Default for Request {
#[inline]
fn default() -> Self {
http::Request::new(Body::empty()).into()
}
}
#[cfg(all(test, feature = "runtime"))]
mod tests {
use http::StatusCode;
use hyper::Body;
use tokio::io::AsyncReadExt;
use crate::{App, Context, Request, Status};
async fn test(ctx: &mut Context) -> Result<(), Status> {
let mut data = String::new();
ctx.req.reader().read_to_string(&mut data).await?;
assert_eq!("Hello, World!", data);
Ok(())
}
#[tokio::test]
async fn body_read() -> Result<(), Box<dyn std::error::Error>> {
let app = App::new().end(test);
let service = app.http_service();
let req = Request::from(http::Request::new(Body::from("Hello, World!")));
let resp = service.serve(req).await;
assert_eq!(StatusCode::OK, resp.status);
Ok(())
}
}