hyper_staticfile/
response_builder.rs

1use http::{
2    header, response::Builder as HttpResponseBuilder, HeaderMap, Method, Request, Response, Result,
3    StatusCode, Uri,
4};
5
6use crate::{resolve::ResolveResult, util::FileResponseBuilder, vfs::IntoFileAccess, Body};
7
8/// Utility to build the default response for a `resolve` result.
9///
10/// This struct allows direct access to its fields, but these fields are typically initialized by
11/// the accessors, using the builder pattern. The fields are basically a bunch of settings that
12/// determine the response details.
13#[derive(Clone, Debug, Default)]
14pub struct ResponseBuilder<'a> {
15    /// The request path.
16    pub path: &'a str,
17    /// The request query string.
18    pub query: Option<&'a str>,
19    /// Inner file response builder.
20    pub file_response_builder: FileResponseBuilder,
21}
22
23impl<'a> ResponseBuilder<'a> {
24    /// Create a new builder with a default configuration.
25    pub fn new() -> Self {
26        Self::default()
27    }
28
29    /// Apply parameters based on a request.
30    pub fn request<B>(&mut self, req: &'a Request<B>) -> &mut Self {
31        self.request_parts(req.method(), req.uri(), req.headers());
32        self
33    }
34
35    /// Apply parameters based on request parts.
36    pub fn request_parts(
37        &mut self,
38        method: &Method,
39        uri: &'a Uri,
40        headers: &'a HeaderMap,
41    ) -> &mut Self {
42        self.request_uri(uri);
43        self.file_response_builder.request_parts(method, headers);
44        self
45    }
46
47    /// Apply parameters based on a request URI.
48    pub fn request_uri(&mut self, uri: &'a Uri) -> &mut Self {
49        self.path(uri.path());
50        self.query(uri.query());
51        self
52    }
53
54    /// Add cache headers to responses for the given lifespan.
55    pub fn cache_headers(&mut self, value: Option<u32>) -> &mut Self {
56        self.file_response_builder.cache_headers(value);
57        self
58    }
59
60    /// Set the request path.
61    pub fn path(&mut self, value: &'a str) -> &mut Self {
62        self.path = value;
63        self
64    }
65
66    /// Set the request query string.
67    pub fn query(&mut self, value: Option<&'a str>) -> &mut Self {
68        self.query = value;
69        self
70    }
71
72    /// Build a response for the given request and `resolve` result.
73    ///
74    /// This function may error if it response could not be constructed, but this should be a
75    /// seldom occurrence.
76    pub fn build<F: IntoFileAccess>(
77        &self,
78        result: ResolveResult<F>,
79    ) -> Result<Response<Body<F::Output>>> {
80        match result {
81            ResolveResult::MethodNotMatched => HttpResponseBuilder::new()
82                .status(StatusCode::BAD_REQUEST)
83                .body(Body::Empty),
84            ResolveResult::NotFound => HttpResponseBuilder::new()
85                .status(StatusCode::NOT_FOUND)
86                .body(Body::Empty),
87            ResolveResult::PermissionDenied => HttpResponseBuilder::new()
88                .status(StatusCode::FORBIDDEN)
89                .body(Body::Empty),
90            ResolveResult::IsDirectory {
91                redirect_to: mut target,
92            } => {
93                // Preserve any query string from the original request.
94                if let Some(query) = self.query {
95                    target.push('?');
96                    target.push_str(query);
97                }
98
99                HttpResponseBuilder::new()
100                    .status(StatusCode::MOVED_PERMANENTLY)
101                    .header(header::LOCATION, target)
102                    .body(Body::Empty)
103            }
104            ResolveResult::Found(file) => self.file_response_builder.build(file),
105        }
106    }
107}