pub struct FileResponseBuilder {
    pub cache_headers: Option<u32>,
    pub is_head: bool,
    pub if_modified_since: Option<SystemTime>,
    pub range: Option<String>,
    pub if_range: Option<String>,
}
Expand description

Utility to build responses for serving a tokio::fs::File.

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.

Fields§

§cache_headers: Option<u32>

Whether to send cache headers, and what lifespan to indicate.

§is_head: bool

Whether this is a HEAD request, with no response body.

§if_modified_since: Option<SystemTime>

The parsed value of the If-Modified-Since request header.

§range: Option<String>

The file ranges to read, if any, otherwise we read from the beginning.

§if_range: Option<String>

The unparsed value of the If-Range request header. May match etag or last-modified.

Implementations§

Create a new builder with a default configuration.

Apply parameters based on a request.

Apply parameters based on request parts.

Examples found in repository?
src/util/file_response_builder.rs (line 49)
48
49
50
    pub fn request<B>(&mut self, req: &Request<B>) -> &mut Self {
        self.request_parts(req.method(), req.headers())
    }
More examples
Hide additional examples
src/response_builder.rs (line 42)
35
36
37
38
39
40
41
42
43
44
    pub fn request_parts(
        &mut self,
        method: &Method,
        uri: &'a Uri,
        headers: &'a HeaderMap,
    ) -> &mut Self {
        self.request_uri(uri);
        self.file_response_builder.request_parts(method, headers);
        self
    }

Apply parameters based on a request method.

Examples found in repository?
src/util/file_response_builder.rs (line 54)
53
54
55
56
57
    pub fn request_parts(&mut self, method: &Method, headers: &HeaderMap) -> &mut Self {
        self.request_method(method);
        self.request_headers(headers);
        self
    }

Apply parameters based on request headers.

Examples found in repository?
src/util/file_response_builder.rs (line 55)
53
54
55
56
57
    pub fn request_parts(&mut self, method: &Method, headers: &HeaderMap) -> &mut Self {
        self.request_method(method);
        self.request_headers(headers);
        self
    }

Add cache headers to responses for the given lifespan.

Examples found in repository?
src/response_builder.rs (line 55)
54
55
56
57
    pub fn cache_headers(&mut self, value: Option<u32>) -> &mut Self {
        self.file_response_builder.cache_headers(value);
        self
    }

Set whether this is a HEAD request, with no response body.

Build responses for the given If-Modified-Since date-time.

Build responses for the given If-Modified-Since request header value.

Examples found in repository?
src/util/file_response_builder.rs (line 67)
66
67
68
69
70
71
    pub fn request_headers(&mut self, headers: &HeaderMap) -> &mut Self {
        self.if_modified_since_header(headers.get(header::IF_MODIFIED_SINCE));
        self.range_header(headers.get(header::RANGE));
        self.if_range(headers.get(header::IF_RANGE));
        self
    }

Build responses for the given If-Range request header value.

Examples found in repository?
src/util/file_response_builder.rs (line 69)
66
67
68
69
70
71
    pub fn request_headers(&mut self, headers: &HeaderMap) -> &mut Self {
        self.if_modified_since_header(headers.get(header::IF_MODIFIED_SINCE));
        self.range_header(headers.get(header::RANGE));
        self.if_range(headers.get(header::IF_RANGE));
        self
    }

Build responses for the given Range request header value.

Examples found in repository?
src/util/file_response_builder.rs (line 68)
66
67
68
69
70
71
    pub fn request_headers(&mut self, headers: &HeaderMap) -> &mut Self {
        self.if_modified_since_header(headers.get(header::IF_MODIFIED_SINCE));
        self.range_header(headers.get(header::RANGE));
        self.if_range(headers.get(header::IF_RANGE));
        self
    }

Build a response for the given file and metadata.

Examples found in repository?
src/response_builder.rs (line 120)
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    pub fn build(&self, result: ResolveResult) -> Result<Response<Body>> {
        match result {
            ResolveResult::MethodNotMatched => HttpResponseBuilder::new()
                .status(StatusCode::BAD_REQUEST)
                .body(Body::empty()),
            ResolveResult::NotFound => HttpResponseBuilder::new()
                .status(StatusCode::NOT_FOUND)
                .body(Body::empty()),
            ResolveResult::PermissionDenied => HttpResponseBuilder::new()
                .status(StatusCode::FORBIDDEN)
                .body(Body::empty()),
            ResolveResult::IsDirectory => {
                // NOTE: We are doing an origin-relative redirect, but need to use the sanitized
                // path in order to prevent a malicious redirect to `//foo` (schema-relative).
                // With the current API, we have no other option here than to do sanitization
                // again, but a future version may reuse the earlier sanitization result.
                let resolved = RequestedPath::resolve(self.path);

                let mut target_len = resolved.sanitized.as_os_str().len() + 2;
                if let Some(ref query) = self.query {
                    target_len += query.len() + 1;
                }

                let mut target = String::with_capacity(target_len);
                target.push('/');
                // On Windows, we can't just append the entire path, because it contains Windows
                // path separators. Append per-component instead.
                for component in resolved.sanitized.components() {
                    target.push_str(&component.as_os_str().to_string_lossy());
                    target.push('/');
                }

                // Preserve any query string from the original request.
                if let Some(query) = self.query {
                    target.push('?');
                    target.push_str(query);
                }

                HttpResponseBuilder::new()
                    .status(StatusCode::MOVED_PERMANENTLY)
                    .header(header::LOCATION, target)
                    .body(Body::empty())
            }
            ResolveResult::Found(file, metadata, mime) => {
                self.file_response_builder
                    .build(file, metadata, mime.to_string())
            }
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more