-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start to make use of forked hyper version
- Loading branch information
glendc
committed
Nov 27, 2023
1 parent
6c326d9
commit 16b9ffb
Showing
6 changed files
with
267 additions
and
79 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.