@@ -20,6 +20,8 @@ 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+ # extern crate hyper;
24+ # extern crate http_body_util;
2325use hyper :: body :: Frame ;
2426use hyper :: {Method , StatusCode };
2527use http_body_util :: {combinators :: BoxBody , BodyExt };
@@ -37,7 +39,9 @@ this we will change the type of the `Body` in our `Response` to a boxed trait ob
3739We 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 ` .
3840
3941``` rust
40- # use bytes :: Bytes ;
42+ # extern crate hyper;
43+ # extern crate http_body_util;
44+ # use hyper :: body :: Bytes ;
4145# use http_body_util :: {combinators :: BoxBody , BodyExt , Empty , Full };
4246# use hyper :: {Method , Request , Response , StatusCode };
4347async fn echo (
@@ -49,6 +53,7 @@ async fn echo(
4953 ))),
5054 (& Method :: POST , " /echo" ) => {
5155 // we'll be back
56+ # Ok (Response :: new (req . into_body (). boxed ()))
5257 },
5358
5459 // Return 404 Not Found for other routes.
@@ -86,9 +91,9 @@ case will instead send back `404 Not Found`.
8691
8792## Body Streams
8893
89- Now let's get that echo in place. An HTTP body is a stream of
90- ` Frames ` , each [ Frame] ( https://docs.rs/http-body/1.0.0-rc1/http_body/struct.Frame.html )
91- containing parts of the ` Body ` data or trailers. So rather than reading the entire ` Body `
94+ Now let's get that echo in place. An HTTP body is a stream of ` Frames ` , each
95+ [ Frame] ( https://docs.rs/http-body/1.0.0-rc1/http_body/struct.Frame.html ) containing
96+ parts of the ` Body ` data or trailers. So rather than reading the entire ` Body `
9297into a buffer before sending our response, we can stream each frame as it arrives.
9398We'll start with the simplest solution, and then make alterations exercising more complex
9499things you can do with the ` Body ` streams.
@@ -97,7 +102,9 @@ First up, plain echo. Both the `Request` and the `Response` have body streams,
97102and by default, you can easily pass the ` Body ` of the ` Request ` into a ` Response ` .
98103
99104``` rust
100- # use bytes :: Bytes ;
105+ # extern crate hyper;
106+ # extern crate http_body_util;
107+ # use hyper :: body :: Bytes ;
101108# use http_body_util :: {combinators :: BoxBody , BodyExt , Empty , Full };
102109# use hyper :: {Method , Request , Response , StatusCode };
103110# async fn echo (
@@ -125,7 +132,9 @@ Next, let's add a new `/echo/uppercase` route, mapping each byte in the data `Fr
125132of our request body to uppercase, and returning the stream in our ` Response ` :
126133
127134``` rust
128- # use bytes :: Bytes ;
135+ # extern crate hyper;
136+ # extern crate http_body_util;
137+ # use hyper :: body :: Bytes ;
129138# use http_body_util :: {combinators :: BoxBody , BodyExt , Empty , Full };
130139# use hyper :: body :: Frame ;
131140# use hyper :: {Method , Request , Response , StatusCode };
@@ -154,6 +163,11 @@ of our request body to uppercase, and returning the stream in our `Response`:
154163# _ => unreachable! (),
155164# }
156165# }
166+ # fn full <T : Into <Bytes >>(chunk : T ) -> BoxBody <Bytes , hyper :: Error > {
167+ # Full :: new (chunk . into ())
168+ # . map_err (| never | match never {})
169+ # . boxed ()
170+ # }
157171# fn main () {}
158172```
159173
@@ -176,7 +190,9 @@ We can easily turn the `Collected` body into a single `Bytes` by calling its `in
176190method.
177191
178192``` rust
179- # use bytes :: Bytes ;
193+ # extern crate hyper;
194+ # extern crate http_body_util;
195+ # use hyper :: body :: Bytes ;
180196# use http_body_util :: {combinators :: BoxBody , BodyExt , Empty , Full };
181197# use hyper :: {Method , Request , Response , StatusCode };
182198# async fn echo (
@@ -199,6 +215,11 @@ method.
199215# _ => unreachable! (),
200216# }
201217# }
218+ # fn full <T : Into <Bytes >>(chunk : T ) -> BoxBody <Bytes , hyper :: Error > {
219+ # Full :: new (chunk . into ())
220+ # . map_err (| never | match never {})
221+ # . boxed ()
222+ # }
202223# fn main () {}
203224```
204225
0 commit comments