-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
echo.rs
80 lines (66 loc) · 2.65 KB
/
echo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#![deny(warnings)]
extern crate futures;
extern crate hyper;
use futures::future;
use hyper::rt::{Future, Stream};
use hyper::service::service_fn;
use hyper::{Body, Method, Request, Response, Server, StatusCode};
/// We need to return different futures depending on the route matched,
/// and we can do that with an enum, such as `futures::Either`, or with
/// trait objects.
///
/// A boxed Future (trait object) is used as it is easier to understand
/// and extend with more types. Advanced users could switch to `Either`.
type BoxFut = Box<dyn Future<Item = Response<Body>, Error = hyper::Error> + Send>;
/// This is our service handler. It receives a Request, routes on its
/// path, and returns a Future of a Response.
fn echo(req: Request<Body>) -> BoxFut {
let mut response = Response::new(Body::empty());
match (req.method(), req.uri().path()) {
// Serve some instructions at /
(&Method::GET, "/") => {
*response.body_mut() = Body::from("Try POSTing data to /echo");
}
// Simply echo the body back to the client.
(&Method::POST, "/echo") => {
*response.body_mut() = req.into_body();
}
// Convert to uppercase before sending back to client.
(&Method::POST, "/echo/uppercase") => {
let mapping = req.into_body().map(|chunk| {
chunk
.iter()
.map(|byte| byte.to_ascii_uppercase())
.collect::<Vec<u8>>()
});
*response.body_mut() = Body::wrap_stream(mapping);
}
// Reverse the entire body before sending back to the client.
//
// Since we don't know the end yet, we can't simply stream
// the chunks as they arrive. So, this returns a different
// future, waiting on concatenating the full body, so that
// it can be reversed. Only then can we return a `Response`.
(&Method::POST, "/echo/reversed") => {
let reversed = req.into_body().concat2().map(move |chunk| {
let body = chunk.iter().rev().cloned().collect::<Vec<u8>>();
*response.body_mut() = Body::from(body);
response
});
return Box::new(reversed);
}
// The 404 Not Found route...
_ => {
*response.status_mut() = StatusCode::NOT_FOUND;
}
};
Box::new(future::ok(response))
}
fn main() {
let addr = ([127, 0, 0, 1], 3000).into();
let server = Server::bind(&addr)
.serve(|| service_fn(echo))
.map_err(|e| eprintln!("server error: {}", e));
println!("Listening on http://{}", addr);
hyper::rt::run(server);
}