Struct hyper_staticfile::FileResponseBuilder
source · 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§
source§impl FileResponseBuilder
impl FileResponseBuilder
sourcepub fn request<B>(&mut self, req: &Request<B>) -> &mut Self
pub fn request<B>(&mut self, req: &Request<B>) -> &mut Self
Apply parameters based on a request.
sourcepub fn request_parts(&mut self, method: &Method, headers: &HeaderMap) -> &mut Self
pub fn request_parts(&mut self, method: &Method, headers: &HeaderMap) -> &mut Self
Apply parameters based on request parts.
Examples found in repository?
More examples
sourcepub fn request_method(&mut self, method: &Method) -> &mut Self
pub fn request_method(&mut self, method: &Method) -> &mut Self
Apply parameters based on a request method.
sourcepub fn request_headers(&mut self, headers: &HeaderMap) -> &mut Self
pub fn request_headers(&mut self, headers: &HeaderMap) -> &mut Self
Apply parameters based on request headers.
sourcepub fn cache_headers(&mut self, value: Option<u32>) -> &mut Self
pub fn cache_headers(&mut self, value: Option<u32>) -> &mut Self
Add cache headers to responses for the given lifespan.
sourcepub fn is_head(&mut self, value: bool) -> &mut Self
pub fn is_head(&mut self, value: bool) -> &mut Self
Set whether this is a HEAD
request, with no response body.
sourcepub fn if_modified_since(&mut self, value: Option<SystemTime>) -> &mut Self
pub fn if_modified_since(&mut self, value: Option<SystemTime>) -> &mut Self
Build responses for the given If-Modified-Since
date-time.
sourcepub fn if_modified_since_header(
&mut self,
value: Option<&HeaderValue>
) -> &mut Self
pub fn if_modified_since_header(
&mut self,
value: Option<&HeaderValue>
) -> &mut Self
Build responses for the given If-Modified-Since
request header value.
sourcepub fn if_range(&mut self, value: Option<&HeaderValue>) -> &mut Self
pub fn if_range(&mut self, value: Option<&HeaderValue>) -> &mut Self
Build responses for the given If-Range
request header value.
sourcepub fn range_header(&mut self, value: Option<&HeaderValue>) -> &mut Self
pub fn range_header(&mut self, value: Option<&HeaderValue>) -> &mut Self
Build responses for the given Range
request header value.
sourcepub fn build(
&self,
file: File,
metadata: Metadata,
content_type: String
) -> Result<Response<Body>>
pub fn build(
&self,
file: File,
metadata: Metadata,
content_type: String
) -> Result<Response<Body>>
Build a response for the given file and metadata.
Examples found in repository?
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§
source§impl Clone for FileResponseBuilder
impl Clone for FileResponseBuilder
source§fn clone(&self) -> FileResponseBuilder
fn clone(&self) -> FileResponseBuilder
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more