Skip to content

Commit a392686

Browse files
author
Fuyang Liu
committed
Fix up - WIP
1 parent 1b23ffd commit a392686

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

examples/echo.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33

44
extern crate hyper;
55

6-
use futures_util::stream::StreamExt; // TODO: is this correct or?
7-
86
use hyper::{Body, Method, Request, Response, Server, StatusCode};
97
use hyper::service::{make_service_fn, service_fn};
10-
use hyper::body::Chunk;
8+
use futures_util::TryStreamExt;
119

1210
/// This is our service handler. It receives a Request, routes on its
1311
/// path, and returns a Future of a Response.
@@ -27,16 +25,14 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
2725

2826
// Convert to uppercase before sending back to client.
2927
(&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
3330
.iter()
3431
.map(|byte| byte.to_ascii_uppercase())
35-
.collect::<Vec<_>>();
36-
x
37-
}).collect::<Vec<_>>().await;
32+
.collect::<Vec<u8>>()
33+
});
3834

39-
*response.body_mut() = Body::from(mapping);
35+
*response.body_mut() = Body::wrap_stream(mapping);
4036
}
4137

4238
// 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> {
4541
// the chunks as they arrive. So, this returns a different
4642
// future, waiting on concatenating the full body, so that
4743
// 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+
}
5753

5854
// The 404 Not Found route...
5955
_ => {

0 commit comments

Comments
 (0)