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
use crate::Body;
use futures::TryStreamExt;
use http::{HeaderValue, Method, Uri, Version};
use hyper::HeaderMap;
use std::io;
use std::ops::{Deref, DerefMut};
pub struct Request {
pub method: Method,
pub uri: Uri,
pub version: Version,
pub headers: HeaderMap<HeaderValue>,
body: Body,
}
impl Request {
pub fn new() -> Self {
Self {
method: Method::default(),
uri: Uri::default(),
version: Version::default(),
headers: HeaderMap::default(),
body: Body::new(),
}
}
}
impl Default for Request {
fn default() -> Self {
Self::new()
}
}
impl Deref for Request {
type Target = Body;
fn deref(&self) -> &Self::Target {
&self.body
}
}
impl DerefMut for Request {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.body
}
}
impl From<http::Request<Body>> for Request {
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,
body,
}
}
}
impl From<http::Request<hyper::Body>> for Request {
fn from(req: http::Request<hyper::Body>) -> Self {
let (parts, body) = req.into_parts();
let mut new_req: Self = http::Request::from_parts(parts, Body::new()).into();
new_req.write(
body.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
.into_async_read(),
);
new_req
}
}
#[cfg(test)]
mod tests {
use crate::{Group, Request};
use futures::AsyncReadExt;
#[tokio::test]
async fn body_read() -> Result<(), Box<dyn std::error::Error>> {
let app = Group::new()
.handle_fn(|mut ctx, _next| {
async move {
let mut data = String::new();
ctx.request.read_to_string(&mut data).await?;
assert_eq!("Hello, World!", data);
Ok(())
}
})
.app(());
let mut request = Request::new();
request.write_str("Hello, World!");
let _resp = app.serve(request, "127.0.0.1:8080".parse()?)
.await?;
Ok(())
}
}