Correctly handle HEAD requests (#129)

This commit is contained in:
David Pedersen
2021-08-15 20:27:13 +02:00
committed by GitHub
parent 9cd543401f
commit 995ffc1aa2
9 changed files with 184 additions and 22 deletions
+11 -2
View File
@@ -6,7 +6,8 @@ use crate::{
};
use bytes::Bytes;
use futures_util::ready;
use http::{Request, Response};
use http::{Method, Request, Response};
use http_body::Empty;
use pin_project_lite::pin_project;
use std::{
future::Future,
@@ -85,6 +86,7 @@ pin_project! {
{
#[pin]
pub(super) inner: crate::routing::future::RouteFuture<S, F, B>,
pub(super) req_method: Method,
}
}
@@ -96,6 +98,13 @@ where
type Output = Result<Response<BoxBody>, S::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().inner.poll(cx)
let this = self.project();
let response = futures_util::ready!(this.inner.poll(cx))?;
if this.req_method == &Method::HEAD {
let response = response.map(|_| box_body(Empty::new()));
Poll::Ready(Ok(response))
} else {
Poll::Ready(Ok(response))
}
}
}