#![forbid(unsafe_code)]
#![deny(
missing_copy_implementations,
rustdoc::missing_crate_level_docs,
missing_debug_implementations,
missing_docs,
nonstandard_style,
unused_qualifications
)]
use trillium::{async_trait, conn_unwrap, Conn, Handler, KnownHeaderName::ContentLength, Method};
#[derive(Default, Clone, Copy, Debug)]
pub struct Head {
_my_private_things: (),
}
impl Head {
pub fn new() -> Self {
Self::default()
}
}
#[derive(Clone, Copy, Debug)]
struct RequestWasHead;
#[async_trait]
impl Handler for Head {
async fn run(&self, mut conn: Conn) -> Conn {
if conn.method() == Method::Head {
conn.inner_mut().set_method(Method::Get);
conn.insert_state(RequestWasHead);
}
conn
}
async fn before_send(&self, mut conn: Conn) -> Conn {
conn_unwrap!(conn.state::<RequestWasHead>(), conn);
conn.inner_mut().set_method(Method::Head);
let len = conn_unwrap!(
conn.inner_mut()
.take_response_body()
.and_then(|body| body.len()),
conn
);
conn.with_response_header(ContentLength, len.to_string())
}
}
pub fn head() -> Head {
Head::new()
}