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
69
70
71
use ::resolve::ResolveResult;
use ::util::FileResponseBuilder;
use http::response::Builder as HttpResponseBuilder;
use http::{Request, Response, Result, StatusCode, header};
use hyper::Body;

/// Utility to build the default response for a `resolve` result.
///
/// This struct allows direct access to its fields, but these fields are typically initialized by
/// the accessors, using the builder pattern. The fields are basically a bunch of settings that
/// determine the response details.
#[derive(Clone,Debug,Default)]
pub struct ResponseBuilder {
    /// Whether to send cache headers, and what lifespan to indicate.
    pub cache_headers: Option<u32>,
}

impl ResponseBuilder {
    /// Create a new builder with a default configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add cache headers to responses for the given lifespan.
    pub fn cache_headers(&mut self, value: Option<u32>) -> &mut Self {
        self.cache_headers = value;
        self
    }

    /// Build a response for the given request and `resolve` result.
    ///
    /// This function may error if it response could not be constructed, but this should be a
    /// seldom occurrence.
    pub fn build<B>(&self, req: &Request<B>, result: ResolveResult) -> Result<Response<Body>> {
        match result {
            ResolveResult::MethodNotMatched => {
                HttpResponseBuilder::new()
                    .status(StatusCode::BAD_REQUEST)
                    .body(Body::empty())
            },
            ResolveResult::UriNotMatched | ResolveResult::NotFound => {
                HttpResponseBuilder::new()
                    .status(StatusCode::NOT_FOUND)
                    .body(Body::empty())
            },
            ResolveResult::PermissionDenied => {
                HttpResponseBuilder::new()
                    .status(StatusCode::FORBIDDEN)
                    .body(Body::empty())
            },
            ResolveResult::IsDirectory => {
                let mut target = req.uri().path().to_owned();
                target.push('/');
                if let Some(query) = req.uri().query() {
                    target.push('?');
                    target.push_str(query);
                }

                HttpResponseBuilder::new()
                    .status(StatusCode::MOVED_PERMANENTLY)
                    .header(header::LOCATION, target.as_str())
                    .body(Body::empty())
            },
            ResolveResult::Found(file, metadata) => {
                FileResponseBuilder::from_request(req)
                    .cache_headers(self.cache_headers)
                    .build(file, metadata)
            },
        }
    }
}