Skip to content

Commit 5a5db00

Browse files
authored
docs(examples): add a length check to reversed echo route (#2952)
1 parent 5e20688 commit 5a5db00

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

examples/echo.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use std::net::SocketAddr;
44

5+
use hyper::body::HttpBody as _;
56
use hyper::server::conn::Http;
67
use hyper::service::service_fn;
78
use hyper::{Body, Method, Request, Response, StatusCode};
@@ -38,6 +39,15 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
3839
// So here we do `.await` on the future, waiting on concatenating the full body,
3940
// then afterwards the content can be reversed. Only then can we return a `Response`.
4041
(&Method::POST, "/echo/reversed") => {
42+
// To protect our server, reject requests with bodies larger than
43+
// 64kbs of data.
44+
let max = req.body().size_hint().upper().unwrap_or(u64::MAX);
45+
if max > 1024 * 64 {
46+
let mut resp = Response::new(Body::from("Body too big"));
47+
*resp.status_mut() = hyper::StatusCode::PAYLOAD_TOO_LARGE;
48+
return Ok(resp);
49+
}
50+
4151
let whole_body = hyper::body::to_bytes(req.into_body()).await?;
4252

4353
let reversed_body = whole_body.iter().rev().cloned().collect::<Vec<u8>>();

0 commit comments

Comments
 (0)