pub struct Resolver<O = TokioFileOpener> {
    pub opener: Arc<O>,
    pub allowed_encodings: AcceptEncoding,
}
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().

Implementations§

Create a resolver that resolves files inside a root directory on the regular filesystem.

Examples found in repository?
src/service.rs (line 40)
38
39
40
41
42
43
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self {
            resolver: Resolver::new(root),
            cache_headers: None,
        }
    }

Create a resolver with a custom file opener.

Examples found in repository?
src/resolve.rs (line 101)
100
101
102
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self::with_opener(TokioFileOpener::new(root))
    }
More examples
Hide additional examples
src/service.rs (line 50)
48
49
50
51
52
53
    pub fn with_opener(opener: O) -> Self {
        Self {
            resolver: Resolver::with_opener(opener),
            cache_headers: None,
        }
    }

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.

Examples found in repository?
src/service.rs (line 76)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    pub async fn serve<B>(
        self,
        request: Request<B>,
    ) -> Result<Response<Body<<O::File as IntoFileAccess>::Output>>, IoError> {
        let Self {
            resolver,
            cache_headers,
        } = self;
        resolver.resolve_request(&request).await.map(|result| {
            ResponseBuilder::new()
                .request(&request)
                .cache_headers(cache_headers)
                .build(result)
                .expect("unable to build response")
        })
    }

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.

Examples found in repository?
src/resolve.rs (line 139)
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    pub async fn resolve_request<B>(
        &self,
        req: &Request<B>,
    ) -> Result<ResolveResult<O::File>, IoError> {
        // Handle only `GET`/`HEAD` and absolute paths.
        match *req.method() {
            Method::HEAD | Method::GET => {}
            _ => {
                return Ok(ResolveResult::MethodNotMatched);
            }
        }

        // Parse `Accept-Encoding` header.
        let accept_encoding = self.allowed_encodings
            & req
                .headers()
                .get(header::ACCEPT_ENCODING)
                .map(AcceptEncoding::from_header_value)
                .unwrap_or(AcceptEncoding::none());

        self.resolve_path(req.uri().path(), accept_encoding).await
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. 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