Files
axum/src/service/future.rs
T

63 lines
1.7 KiB
Rust
Raw Normal View History

//! [`Service`](tower::Service) future types.
2021-07-22 13:23:50 +02:00
use crate::{
body::{box_body, BoxBody},
2021-08-15 23:01:26 +02:00
util::{Either, EitherProj},
2021-08-21 15:01:30 +02:00
BoxError,
2021-07-22 13:23:50 +02:00
};
use bytes::Bytes;
use futures_util::ready;
2021-08-15 20:27:13 +02:00
use http::{Method, Request, Response};
use http_body::Empty;
use pin_project_lite::pin_project;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
2021-08-21 15:01:30 +02:00
use tower::util::Oneshot;
use tower_service::Service;
pin_project! {
/// The response future for [`OnMethod`](super::OnMethod).
pub struct OnMethodFuture<S, F, B>
where
S: Service<Request<B>>,
F: Service<Request<B>>
{
#[pin]
2021-08-15 23:01:26 +02:00
pub(super) inner: Either<
Oneshot<S, Request<B>>,
Oneshot<F, Request<B>>,
>,
// pub(super) inner: crate::routing::future::RouteFuture<S, F, B>,
2021-08-15 20:27:13 +02:00
pub(super) req_method: Method,
}
}
2021-08-15 23:01:26 +02:00
impl<S, F, B, ResBody> Future for OnMethodFuture<S, F, B>
where
2021-08-15 23:01:26 +02:00
S: Service<Request<B>, Response = Response<ResBody>> + Clone,
ResBody: http_body::Body<Data = Bytes> + Send + Sync + 'static,
ResBody::Error: Into<BoxError>,
F: Service<Request<B>, Response = Response<BoxBody>, Error = S::Error>,
{
type Output = Result<Response<BoxBody>, S::Error>;
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-15 23:01:26 +02:00
let response = match this.inner.project() {
EitherProj::A { inner } => ready!(inner.poll(cx))?.map(box_body),
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))
}
}
}