@@ -20,8 +20,9 @@ handle the case when someone asks for a route we don't know!
2020Before we get started we need to add some new imports:
2121
2222``` rust
23- use hyper :: {body :: Body , Method , Request , Response , StatusCode };
24- use http_body_util :: {combinators :: BoxBody , BodyExt , Full };
23+ use hyper :: body :: Frame ;
24+ use hyper :: {Method , StatusCode };
25+ use http_body_util :: {combinators :: BoxBody , BodyExt };
2526# fn main () {}
2627```
2728
@@ -36,14 +37,9 @@ this we will change the type of the `Body` in our `Response` to a boxed trait ob
3637We only care that the response body implements the [ Body] ( https://docs.rs/http-body/1.0.0-rc1/http_body/trait.Body.html ) trait, that its data is ` Bytes ` and its error is a ` hyper::Error ` .
3738
3839``` rust
39- # use std :: net :: SocketAddr ;
40- #
4140# use bytes :: Bytes ;
4241# use http_body_util :: {combinators :: BoxBody , BodyExt , Empty , Full };
43- # use hyper :: server :: conn :: http1;
44- # use hyper :: service :: service_fn;
45- # use hyper :: {body :: Body , Method , Request , Response , StatusCode };
46- # use tokio :: net :: TcpListener ;
42+ # use hyper :: {Method , Request , Response , StatusCode };
4743async fn echo (
4844 req : Request <hyper :: body :: Incoming >,
4945) -> Result <Response <BoxBody <Bytes , hyper :: Error >>, hyper :: Error > {
@@ -101,14 +97,9 @@ First up, plain echo. Both the `Request` and the `Response` have body streams,
10197and by default, you can easily pass the ` Body ` of the ` Request ` into a ` Response ` .
10298
10399``` rust
104- # use std :: net :: SocketAddr ;
105- #
106100# use bytes :: Bytes ;
107101# use http_body_util :: {combinators :: BoxBody , BodyExt , Empty , Full };
108- # use hyper :: server :: conn :: http1;
109- # use hyper :: service :: service_fn;
110- # use hyper :: {body :: Body , Method , Request , Response , StatusCode };
111- # use tokio :: net :: TcpListener ;
102+ # use hyper :: {Method , Request , Response , StatusCode };
112103# async fn echo (
113104# req : Request <hyper :: body :: Incoming >,
114105# ) -> Result <Response <BoxBody <Bytes , hyper :: Error >>, hyper :: Error > {
@@ -134,14 +125,10 @@ Next, let's add a new `/echo/uppercase` route, mapping each byte in the data `Fr
134125of our request body to uppercase, and returning the stream in our ` Response ` :
135126
136127``` rust
137- # use std :: net :: SocketAddr ;
138- #
139128# use bytes :: Bytes ;
140129# use http_body_util :: {combinators :: BoxBody , BodyExt , Empty , Full };
141- # use hyper :: server :: conn :: http1;
142- # use hyper :: service :: service_fn;
143- # use hyper :: {body :: Body , Method , Request , Response , StatusCode };
144- # use tokio :: net :: TcpListener ;
130+ # use hyper :: body :: Frame ;
131+ # use hyper :: {Method , Request , Response , StatusCode };
145132# async fn echo (
146133# req : Request <hyper :: body :: Incoming >,
147134# ) -> Result <Response <BoxBody <Bytes , hyper :: Error >>, hyper :: Error > {
@@ -188,14 +175,9 @@ stream to completion, collecting all the data and trailer frames into a `Collect
188175We can easily turn the ` Collected ` body into bytes by calling its ` into_bytes ` method.
189176
190177``` rust
191- # use std :: net :: SocketAddr ;
192- #
193178# use bytes :: Bytes ;
194179# use http_body_util :: {combinators :: BoxBody , BodyExt , Empty , Full };
195- # use hyper :: server :: conn :: http1;
196- # use hyper :: service :: service_fn;
197- # use hyper :: {body :: Body , Method , Request , Response , StatusCode };
198- # use tokio :: net :: TcpListener ;
180+ # use hyper :: {Method , Request , Response , StatusCode };
199181# async fn echo (
200182# req : Request <hyper :: body :: Incoming >,
201183# ) -> Result <Response <BoxBody <Bytes , hyper :: Error >>, hyper :: Error > {
@@ -206,7 +188,7 @@ We can easily turn the `Collected` body into bytes by calling its `into_bytes` m
206188 let whole_body = req . collect (). await ? . to_bytes ();
207189
208190 // Iterate the whole body in reverse order and collect into a new Vec.
209- let reversed = whole_body . iter ()
191+ let reversed_body = whole_body . iter ()
210192 . rev ()
211193 . cloned ()
212194 . collect :: <Vec <u8 >>();
0 commit comments