Skip to content

Commit

Permalink
start to make use of forked hyper version
Browse files Browse the repository at this point in the history
  • Loading branch information
glendc committed Nov 27, 2023
1 parent 6c326d9 commit 16b9ffb
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 79 deletions.
139 changes: 71 additions & 68 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ http = "1"
http-body = "1"
http-body-util = "0.1"
httparse = "1.8"
hyper = { version = "1", features = ["http1", "http2", "server"] }
hyper-util = { version = "0.1", features = ["tokio", "server-auto"] }
matchit = "0.7"
hyper = { package = "rama-hyper", version = "0.1000001", features = ["http1", "http2", "server"] }
hyper-util = { package = "rama-hyper-util", version = "0.1001", features = ["tokio", "server-auto"] }
matchit = "0.4.2"
pin-project-lite = "0.2.13"
rustls = "0.22.0-alpha.3"
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -44,7 +44,6 @@ tower-async = { version = "0.2", features = [
"limit",
] }
tower-async-http = { version = "0.2", features = ["full"] }
tower-async-hyper = "0.1"
tracing = "0.1"

[dev-dependencies]
Expand Down
82 changes: 82 additions & 0 deletions src/service/hyper/body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::{
pin::Pin,
task::{Context, Poll},
};

use http_body::{Body as HttpBody, Frame, SizeHint};
use hyper::body::Incoming;

pin_project_lite::pin_project! {
/// A wrapper around `hyper::body::Incoming` that implements `http_body::Body`.
///
/// This type is used to bridge the `hyper` and `tower-async` ecosystems.
/// Reason is that a lot of middlewares in `tower-async-http` that
/// operate on `http_body::Body` which also have to implement `Default`.
#[derive(Debug, Default)]
pub struct Body {
#[pin]
inner: Option<Incoming>,
}
}

impl From<Incoming> for Body {
fn from(inner: Incoming) -> Self {
Self { inner: Some(inner) }
}
}

impl Body {
/// Return a reference to the inner [`hyper::body::Incoming`] value.
///
/// This is normally not needed,
/// but in case you do ever need it, it's here.
pub fn as_ref(&self) -> Option<&Incoming> {
self.inner.as_ref()
}

/// Return a mutable reference to the inner [`hyper::body::Incoming`] value.
///
/// This is normally not needed,
/// but in case you do ever need it, it's here.
pub fn as_mut(&mut self) -> Option<&mut Incoming> {
self.inner.as_mut()
}

/// Turn this [`Body`] into the inner [`hyper::body::Incoming`] value.
///
/// This is normally not needed,
/// but in case you do ever need it, it's here.
pub fn into_inner(self) -> Option<Incoming> {
self.inner
}
}

impl HttpBody for Body {
type Data = <Incoming as HttpBody>::Data;
type Error = <Incoming as HttpBody>::Error;

fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
self.project()
.inner
.as_pin_mut()
.map(|incoming| incoming.poll_frame(cx))
.unwrap_or_else(|| Poll::Ready(None))
}

fn is_end_stream(&self) -> bool {
self.inner
.as_ref()
.map(|incoming| incoming.is_end_stream())
.unwrap_or(true)
}

fn size_hint(&self) -> SizeHint {
self.inner
.as_ref()
.map(|incoming| incoming.size_hint())
.unwrap_or_default()
}
}
7 changes: 7 additions & 0 deletions src/service/hyper/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Bridges a `tower-async` `Service` to be used within a `hyper` (1.x) environment.

mod service;
pub use service::{BoxFuture, HyperServiceWrapper, TowerHyperServiceExt};

mod body;
pub use body::Body as HyperBody;
Loading

0 comments on commit 16b9ffb

Please sign in to comment.