Skip to content

Commit

Permalink
feat: allow empty Body to be directly constructed
Browse files Browse the repository at this point in the history
  • Loading branch information
omjadas committed Apr 29, 2024
1 parent 1279433 commit d79401e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
3 changes: 1 addition & 2 deletions benches/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use http_body_util::Empty;
use hudsucker::{
certificate_authority::{CertificateAuthority, RcgenAuthority},
hyper::{body::Incoming, service::service_fn, Method, Request, Response},
Expand Down Expand Up @@ -40,7 +39,7 @@ fn build_ca() -> RcgenAuthority {
async fn test_server(req: Request<Incoming>) -> Result<Response<Body>, Infallible> {
match (req.method(), req.uri().path()) {
(&Method::GET, "/hello") => Ok(Response::new(Body::from("hello, world"))),
_ => Ok(Response::new(Body::from(Empty::new()))),
_ => Ok(Response::new(Body::empty())),
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::Error;
use futures::{Stream, TryStream, TryStreamExt};
use http_body_util::{combinators::BoxBody, Collected, Empty, Full, StreamBody};
use hyper::body::{Body as HttpBody, Bytes, Frame, Incoming, SizeHint};
use std::pin::Pin;
use std::{pin::Pin, task::Poll};

#[derive(Debug)]
enum Internal {
Expand All @@ -21,6 +21,10 @@ pub struct Body {
}

impl Body {
pub fn empty() -> Self {
Self::from(Empty::new())
}

pub fn from_stream<S>(stream: S) -> Self
where
S: TryStream + Send + Sync + 'static,
Expand All @@ -43,9 +47,9 @@ impl HttpBody for Body {
type Error = Error;

fn poll_frame(
mut self: std::pin::Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
match &mut self.inner {
Internal::BoxBody(body) => Pin::new(body).poll_frame(cx),
Internal::Collected(body) => Pin::new(body).poll_frame(cx).map_err(|e| match e {}),
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ mod rewind;
pub mod certificate_authority;

use futures::{Sink, SinkExt, Stream, StreamExt};
use http_body_util::Empty;
use hyper::{Request, Response, StatusCode, Uri};
use std::{future::Future, net::SocketAddr};
use tokio_tungstenite::tungstenite::{self, Message};
Expand Down Expand Up @@ -133,7 +132,7 @@ pub trait HttpHandler: Clone + Send + Sync + 'static {
error!("Failed to forward request: {}", err);
Response::builder()
.status(StatusCode::BAD_GATEWAY)
.body(Empty::new().into())
.body(Body::empty())
.expect("Failed to build response")
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/proxy/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
};
use futures::{Sink, Stream, StreamExt};
use http::uri::{Authority, Scheme};
use http_body_util::Empty;
use hyper::{
body::{Bytes, Incoming},
header::Entry,
Expand All @@ -29,7 +28,7 @@ use tracing::{error, info_span, instrument, warn, Instrument, Span};
fn bad_request() -> Response<Body> {
Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Empty::new().into())
.body(Body::empty())
.expect("Failed to build response")
}

Expand Down Expand Up @@ -230,7 +229,7 @@ where
};

spawn_with_trace(fut, span);
Response::new(Empty::new().into())
Response::new(Body::empty())
}
None => bad_request(),
}
Expand Down Expand Up @@ -478,7 +477,7 @@ mod tests {

let req = Request::builder()
.uri("/foo/bar?baz")
.body(Empty::new().into())
.body(Body::empty())
.unwrap();

let res = proxy.process_connect(req);
Expand All @@ -496,7 +495,7 @@ mod tests {

let req = Request::builder()
.uri("/foo/bar?baz")
.body(Empty::new().into())
.body(Body::empty())
.unwrap();

let res = proxy.upgrade_websocket(req);
Expand All @@ -510,7 +509,7 @@ mod tests {

let req = Request::builder()
.uri("http://example.com/foo/bar?baz")
.body(Empty::new().into())
.body(Body::empty())
.unwrap();

let res = proxy.upgrade_websocket(req);
Expand Down
3 changes: 1 addition & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use async_compression::tokio::bufread::GzipEncoder;
use futures::{SinkExt, StreamExt};
use http_body_util::Empty;
use hudsucker::{
certificate_authority::CertificateAuthority,
decode_request, decode_response,
Expand Down Expand Up @@ -67,7 +66,7 @@ async fn test_server(req: Request<Incoming>) -> Result<Response<Body>, Infallibl
))))
.unwrap()),
(&Method::POST, "/echo") => Ok(Response::new(req.into_body().into())),
_ => Ok(Response::new(Body::from(Empty::new()))),
_ => Ok(Response::new(Body::empty())),
}
}

Expand Down

0 comments on commit d79401e

Please sign in to comment.