3
3
4
4
extern crate hyper;
5
5
6
- use futures_util:: stream:: StreamExt ; // TODO: is this correct or?
7
-
8
6
use hyper:: { Body , Method , Request , Response , Server , StatusCode } ;
9
7
use hyper:: service:: { make_service_fn, service_fn} ;
10
- use hyper :: body :: Chunk ;
8
+ use futures_util :: TryStreamExt ;
11
9
12
10
/// This is our service handler. It receives a Request, routes on its
13
11
/// path, and returns a Future of a Response.
@@ -27,16 +25,14 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
27
25
28
26
// Convert to uppercase before sending back to client.
29
27
( & Method :: POST , "/echo/uppercase" ) => {
30
- let mapping = req. into_body ( ) . map ( |chunk| {
31
- let chunk= chunk. unwrap ( ) ;
32
- let x = chunk
28
+ let mapping = req. into_body ( ) . map_ok ( |chunk| {
29
+ chunk
33
30
. iter ( )
34
31
. map ( |byte| byte. to_ascii_uppercase ( ) )
35
- . collect :: < Vec < _ > > ( ) ;
36
- x
37
- } ) . collect :: < Vec < _ > > ( ) . await ;
32
+ . collect :: < Vec < u8 > > ( )
33
+ } ) ;
38
34
39
- * response. body_mut ( ) = Body :: from ( mapping) ;
35
+ * response. body_mut ( ) = Body :: wrap_stream ( mapping) ;
40
36
}
41
37
42
38
// Reverse the entire body before sending back to the client.
@@ -45,15 +41,15 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
45
41
// the chunks as they arrive. So, this returns a different
46
42
// future, waiting on concatenating the full body, so that
47
43
// it can be reversed. Only then can we return a `Response`.
48
- // (&Method::POST, "/echo/reversed") => {
49
- // let reversed = req.into_body().concat2().map(move |chunk| {
50
- // let body = chunk.iter().rev().cloned().collect::<Vec<u8>>();
51
- // *response.body_mut() = Body::from(body);
52
- // response
53
- // });
54
- //
55
- // return Ok(reversed);
56
- // }
44
+ ( & Method :: POST , "/echo/reversed" ) => {
45
+ let reversed = req. into_body ( ) . concat2 ( ) . map ( move |chunk| {
46
+ let body = chunk. iter ( ) . rev ( ) . cloned ( ) . collect :: < Vec < u8 > > ( ) ;
47
+ * response. body_mut ( ) = Body :: from ( body) ;
48
+ response
49
+ } ) ;
50
+
51
+ return Ok ( reversed) ;
52
+ }
57
53
58
54
// The 404 Not Found route...
59
55
_ => {
0 commit comments