1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*!
# Trillium handler for HEAD requests

This simple handler rewrites HEAD requests to be GET requests, and
then before sending the response, removes the outbound body but sets a
content length header. Any handlers subsequent to this one see a GET
request.
*/
#![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};

/**
Trillium handler for HEAD requests

See crate-level docs for an explanation
*/
#[derive(Default, Clone, Copy, Debug)]
pub struct Head {
    _my_private_things: (),
}

impl Head {
    /// constructs a new Head handler
    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.set_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_header(ContentLength, len.to_string())
    }
}

/// Alias for [`Head::new`]
pub fn head() -> Head {
    Head::new()
}