Struct hyper_staticfile::Resolver
source · pub struct Resolver<O = TokioFileOpener> {
pub opener: Arc<O>,
pub allowed_encodings: AcceptEncoding,
pub rewrite: Option<Arc<dyn Fn(ResolveParams) -> BoxRewriteFuture + Send + Sync>>,
}
Expand description
Resolves request paths to files.
This struct resolves files based on the request path. The path is first sanitized, then mapped to a file on the filesystem. If the path corresponds to a directory, it will try to look for a directory index.
Cloning this struct is a cheap operation.
Fields§
§opener: Arc<O>
The (virtual) filesystem used to open files.
allowed_encodings: AcceptEncoding
Encodings the client is allowed to request with Accept-Encoding
.
This only supports pre-encoded files, that exist adjacent to the original file, but with an
additional .br
or .gz
suffix (after the original extension).
Typically initialized with AcceptEncoding::all()
or AcceptEncoding::none()
.
rewrite: Option<Arc<dyn Fn(ResolveParams) -> BoxRewriteFuture + Send + Sync>>
Optional function that can rewrite requests.
This function is called after parsing the request and before querying the filesystem.
See set_rewrite
for a convenience setter that simplifies these types.
Implementations§
source§impl<O: FileOpener> Resolver<O>
impl<O: FileOpener> Resolver<O>
sourcepub fn with_opener(opener: O) -> Self
pub fn with_opener(opener: O) -> Self
Create a resolver with a custom file opener.
sourcepub fn set_rewrite<R, F>(&mut self, rewrite: F) -> &mut Selfwhere
R: Future<Output = IoResult<ResolveParams>> + Send + 'static,
F: Fn(ResolveParams) -> R + Send + Sync + 'static,
pub fn set_rewrite<R, F>(&mut self, rewrite: F) -> &mut Selfwhere R: Future<Output = IoResult<ResolveParams>> + Send + 'static, F: Fn(ResolveParams) -> R + Send + Sync + 'static,
Configure a function that can rewrite requests.
This function is called after parsing the request and before querying the filesystem.
let mut resolver = hyper_staticfile::Resolver::new("/");
resolver.set_rewrite(|mut params| async move {
if params.path.extension() == Some("htm".as_ref()) {
params.path.set_extension("html");
}
Ok(params)
});
sourcepub async fn resolve_request<B>(
&self,
req: &Request<B>
) -> IoResult<ResolveResult<O::File>>
pub async fn resolve_request<B>( &self, req: &Request<B> ) -> IoResult<ResolveResult<O::File>>
Resolve the request by trying to find the file in the root.
The returned future may error for unexpected IO errors, passing on the std::io::Error
.
Certain expected IO errors are handled, though, and simply reflected in the result. These are
NotFound
and PermissionDenied
.
sourcepub async fn resolve_path(
&self,
request_path: &str,
accept_encoding: AcceptEncoding
) -> IoResult<ResolveResult<O::File>>
pub async fn resolve_path( &self, request_path: &str, accept_encoding: AcceptEncoding ) -> IoResult<ResolveResult<O::File>>
Resolve the request path by trying to find the file in the given root.
The returned future may error for unexpected IO errors, passing on the std::io::Error
.
Certain expected IO errors are handled, though, and simply reflected in the result. These are
NotFound
and PermissionDenied
.
Note that, unlike resolve_request
, it is up to the caller to check the request method and
optionally the ‘Accept-Encoding’ header.