2021-06-08 12:43:16 +02:00
|
|
|
//! Handler future types.
|
|
|
|
|
|
2021-08-15 20:27:13 +02:00
|
|
|
use crate::body::{box_body, BoxBody};
|
2021-08-16 09:19:37 +02:00
|
|
|
use crate::util::{Either, EitherProj};
|
|
|
|
|
use futures_util::{future::BoxFuture, ready};
|
2021-08-15 20:27:13 +02:00
|
|
|
use http::{Method, Request, Response};
|
|
|
|
|
use http_body::Empty;
|
2021-08-07 22:27:27 +02:00
|
|
|
use pin_project_lite::pin_project;
|
|
|
|
|
use std::{
|
2021-08-15 23:01:26 +02:00
|
|
|
fmt,
|
2021-08-07 22:27:27 +02:00
|
|
|
future::Future,
|
|
|
|
|
pin::Pin,
|
|
|
|
|
task::{Context, Poll},
|
|
|
|
|
};
|
2021-08-15 23:01:26 +02:00
|
|
|
use tower::{util::Oneshot, Service};
|
2021-08-07 22:27:27 +02:00
|
|
|
|
|
|
|
|
pin_project! {
|
|
|
|
|
/// The response future for [`OnMethod`](super::OnMethod).
|
2021-08-15 23:01:26 +02:00
|
|
|
pub struct OnMethodFuture<F, B>
|
2021-08-07 22:27:27 +02:00
|
|
|
where
|
|
|
|
|
F: Service<Request<B>>
|
|
|
|
|
{
|
|
|
|
|
#[pin]
|
2021-08-15 23:01:26 +02:00
|
|
|
pub(super) inner: Either<
|
2021-08-16 09:19:37 +02:00
|
|
|
BoxFuture<'static, Response<BoxBody>>,
|
2021-08-15 23:01:26 +02:00
|
|
|
Oneshot<F, Request<B>>,
|
|
|
|
|
>,
|
2021-08-15 20:27:13 +02:00
|
|
|
pub(super) req_method: Method,
|
2021-08-07 22:27:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-15 23:01:26 +02:00
|
|
|
impl<F, B> Future for OnMethodFuture<F, B>
|
2021-08-07 22:27:27 +02:00
|
|
|
where
|
2021-08-15 23:01:26 +02:00
|
|
|
F: Service<Request<B>, Response = Response<BoxBody>>,
|
2021-08-07 22:27:27 +02:00
|
|
|
{
|
2021-08-15 23:01:26 +02:00
|
|
|
type Output = Result<Response<BoxBody>, F::Error>;
|
2021-08-07 22:27:27 +02:00
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2021-08-15 20:27:13 +02:00
|
|
|
let this = self.project();
|
2021-08-16 09:19:37 +02:00
|
|
|
let response = match this.inner.project() {
|
|
|
|
|
EitherProj::A { inner } => ready!(inner.poll(cx)),
|
|
|
|
|
EitherProj::B { inner } => ready!(inner.poll(cx))?,
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-15 20:27:13 +02:00
|
|
|
if this.req_method == &Method::HEAD {
|
|
|
|
|
let response = response.map(|_| box_body(Empty::new()));
|
|
|
|
|
Poll::Ready(Ok(response))
|
|
|
|
|
} else {
|
|
|
|
|
Poll::Ready(Ok(response))
|
|
|
|
|
}
|
2021-08-07 22:27:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-08-15 23:01:26 +02:00
|
|
|
|
|
|
|
|
impl<F, B> fmt::Debug for OnMethodFuture<F, B>
|
|
|
|
|
where
|
|
|
|
|
F: Service<Request<B>>,
|
|
|
|
|
{
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("OnMethodFuture").finish()
|
|
|
|
|
}
|
|
|
|
|
}
|