Skip to content

Commit

Permalink
Merge branch 'master' into feat-create-service-trait
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkarw committed Aug 19, 2022
2 parents e0d384b + 84f6ae7 commit fa8d0c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/echo.rs
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
1 change: 1 addition & 0 deletions examples/http_proxy.rs
Expand Up @@ -32,6 +32,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.http1_preserve_header_case(true)
.http1_title_case_headers(true)
.serve_connection(stream, service_fn(proxy))
.with_upgrades()
.await
{
println!("Failed to serve connection: {:?}", err);
Expand Down

0 comments on commit fa8d0c5

Please sign in to comment.