Skip to content

Commit

Permalink
docs(examples): add a length check to reversed echo route (hyperium#2952
Browse files Browse the repository at this point in the history
)
  • Loading branch information
seanmonstar authored Aug 19, 2022
1 parent 5e20688 commit 5a5db00
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::net::SocketAddr;

use hyper::body::HttpBody as _;
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::{Body, Method, Request, Response, StatusCode};
Expand Down Expand Up @@ -38,6 +39,15 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
// So here we do `.await` on the future, waiting on concatenating the full body,
// then afterwards the content can be reversed. Only then can we return a `Response`.
(&Method::POST, "/echo/reversed") => {
// To protect our server, reject requests with bodies larger than
// 64kbs of data.
let max = req.body().size_hint().upper().unwrap_or(u64::MAX);
if max > 1024 * 64 {
let mut resp = Response::new(Body::from("Body too big"));
*resp.status_mut() = hyper::StatusCode::PAYLOAD_TOO_LARGE;
return Ok(resp);
}

let whole_body = hyper::body::to_bytes(req.into_body()).await?;

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

0 comments on commit 5a5db00

Please sign in to comment.