roa_core/
request.rs

1use std::io;
2
3use bytes::Bytes;
4use futures::stream::{Stream, TryStreamExt};
5use http::{Extensions, HeaderMap, HeaderValue, Method, Uri, Version};
6use hyper::Body;
7use tokio::io::AsyncRead;
8use tokio_util::io::StreamReader;
9/// Http request type of roa.
10pub struct Request {
11    /// The request's method
12    pub method: Method,
13
14    /// The request's URI
15    pub uri: Uri,
16
17    /// The request's version
18    pub version: Version,
19
20    /// The request's headers
21    pub headers: HeaderMap<HeaderValue>,
22
23    extensions: Extensions,
24
25    body: Body,
26}
27
28impl Request {
29    /// Take raw hyper request.
30    /// This method will consume inner body and extensions.
31    #[inline]
32    pub fn take_raw(&mut self) -> http::Request<Body> {
33        let mut builder = http::Request::builder()
34            .method(self.method.clone())
35            .uri(self.uri.clone());
36        *builder.extensions_mut().expect("fail to get extensions") =
37            std::mem::take(&mut self.extensions);
38        *builder.headers_mut().expect("fail to get headers") = self.headers.clone();
39        builder
40            .body(self.raw_body())
41            .expect("fail to build raw body")
42    }
43
44    /// Gake raw hyper body.
45    /// This method will consume inner body.
46    #[inline]
47    pub fn raw_body(&mut self) -> Body {
48        std::mem::take(&mut self.body)
49    }
50    /// Get body as Stream.
51    /// This method will consume inner body.
52    #[inline]
53    pub fn stream(
54        &mut self,
55    ) -> impl Stream<Item = io::Result<Bytes>> + Sync + Send + Unpin + 'static {
56        self.raw_body()
57            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))
58    }
59
60    /// Get body as AsyncRead.
61    /// This method will consume inner body.
62    #[inline]
63    pub fn reader(&mut self) -> impl AsyncRead + Sync + Send + Unpin + 'static {
64        StreamReader::new(self.stream())
65    }
66}
67
68impl From<http::Request<Body>> for Request {
69    #[inline]
70    fn from(req: http::Request<Body>) -> Self {
71        let (parts, body) = req.into_parts();
72        Self {
73            method: parts.method,
74            uri: parts.uri,
75            version: parts.version,
76            headers: parts.headers,
77            extensions: parts.extensions,
78            body,
79        }
80    }
81}
82
83impl Default for Request {
84    #[inline]
85    fn default() -> Self {
86        http::Request::new(Body::empty()).into()
87    }
88}
89
90#[cfg(all(test, feature = "runtime"))]
91mod tests {
92    use http::StatusCode;
93    use hyper::Body;
94    use tokio::io::AsyncReadExt;
95
96    use crate::{App, Context, Request, Status};
97
98    async fn test(ctx: &mut Context) -> Result<(), Status> {
99        let mut data = String::new();
100        ctx.req.reader().read_to_string(&mut data).await?;
101        assert_eq!("Hello, World!", data);
102        Ok(())
103    }
104
105    #[tokio::test]
106    async fn body_read() -> Result<(), Box<dyn std::error::Error>> {
107        let app = App::new().end(test);
108        let service = app.http_service();
109        let req = Request::from(http::Request::new(Body::from("Hello, World!")));
110        let resp = service.serve(req).await;
111        assert_eq!(StatusCode::OK, resp.status);
112        Ok(())
113    }
114}