Struct ServeDir
pub struct ServeDir<F = DefaultServeDirFallback> { /* private fields */ }Expand description
Service that serves files from a given directory and all its sub directories.
The Content-Type will be guessed from the file extension.
An empty response with status 404 Not Found will be returned if:
- The file doesn’t exist
- Any segment of the path contains
.. - Any segment of the path contains a backslash
- On unix, any segment of the path referenced as directory is actually an
existing file (
/file.html/something) - We don’t have necessary permissions to read the file
Implementations§
§impl<F> ServeDir<F>
impl<F> ServeDir<F>
pub fn with_directory_serve_mode(self, mode: DirectoryServeMode) -> ServeDir<F>
pub fn with_directory_serve_mode(self, mode: DirectoryServeMode) -> ServeDir<F>
Set the DirectoryServeMode.
pub fn set_directory_serve_mode(
&mut self,
mode: DirectoryServeMode,
) -> &mut ServeDir<F>
pub fn set_directory_serve_mode( &mut self, mode: DirectoryServeMode, ) -> &mut ServeDir<F>
Set the DirectoryServeMode.
pub fn with_buf_chunk_size(self, chunk_size: usize) -> ServeDir<F>
pub fn with_buf_chunk_size(self, chunk_size: usize) -> ServeDir<F>
Set a specific read buffer chunk size.
The default capacity is 64kb.
pub fn set_buf_chunk_size(&mut self, chunk_size: usize) -> &mut ServeDir<F>
pub fn set_buf_chunk_size(&mut self, chunk_size: usize) -> &mut ServeDir<F>
Set a specific read buffer chunk size.
The default capacity is 64kb.
pub fn precompressed_gzip(self) -> ServeDir<F>
pub fn precompressed_gzip(self) -> ServeDir<F>
Informs the service that it should also look for a precompressed gzip version of any file in the directory.
Assuming the dir directory is being served and dir/foo.txt is requested,
a client with an Accept-Encoding header that allows the gzip encoding
will receive the file dir/foo.txt.gz instead of dir/foo.txt.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
pub fn set_precompressed_gzip(&mut self) -> &mut ServeDir<F>
pub fn set_precompressed_gzip(&mut self) -> &mut ServeDir<F>
Informs the service that it should also look for a precompressed gzip version of any file in the directory.
Assuming the dir directory is being served and dir/foo.txt is requested,
a client with an Accept-Encoding header that allows the gzip encoding
will receive the file dir/foo.txt.gz instead of dir/foo.txt.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
pub fn precompressed_br(self) -> ServeDir<F>
pub fn precompressed_br(self) -> ServeDir<F>
Informs the service that it should also look for a precompressed brotli version of any file in the directory.
Assuming the dir directory is being served and dir/foo.txt is requested,
a client with an Accept-Encoding header that allows the brotli encoding
will receive the file dir/foo.txt.br instead of dir/foo.txt.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
pub fn set_precompressed_br(&mut self) -> &mut ServeDir<F>
pub fn set_precompressed_br(&mut self) -> &mut ServeDir<F>
Informs the service that it should also look for a precompressed brotli version of any file in the directory.
Assuming the dir directory is being served and dir/foo.txt is requested,
a client with an Accept-Encoding header that allows the brotli encoding
will receive the file dir/foo.txt.br instead of dir/foo.txt.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
pub fn precompressed_deflate(self) -> ServeDir<F>
pub fn precompressed_deflate(self) -> ServeDir<F>
Informs the service that it should also look for a precompressed deflate version of any file in the directory.
Assuming the dir directory is being served and dir/foo.txt is requested,
a client with an Accept-Encoding header that allows the deflate encoding
will receive the file dir/foo.txt.zz instead of dir/foo.txt.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
pub fn set_precompressed_deflate(&mut self) -> &mut ServeDir<F>
pub fn set_precompressed_deflate(&mut self) -> &mut ServeDir<F>
Informs the service that it should also look for a precompressed deflate version of any file in the directory.
Assuming the dir directory is being served and dir/foo.txt is requested,
a client with an Accept-Encoding header that allows the deflate encoding
will receive the file dir/foo.txt.zz instead of dir/foo.txt.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
pub fn precompressed_zstd(self) -> ServeDir<F>
pub fn precompressed_zstd(self) -> ServeDir<F>
Informs the service that it should also look for a precompressed zstd version of any file in the directory.
Assuming the dir directory is being served and dir/foo.txt is requested,
a client with an Accept-Encoding header that allows the zstd encoding
will receive the file dir/foo.txt.zst instead of dir/foo.txt.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
pub fn set_precompressed_zstd(&mut self) -> &mut ServeDir<F>
pub fn set_precompressed_zstd(&mut self) -> &mut ServeDir<F>
Informs the service that it should also look for a precompressed zstd version of any file in the directory.
Assuming the dir directory is being served and dir/foo.txt is requested,
a client with an Accept-Encoding header that allows the zstd encoding
will receive the file dir/foo.txt.zst instead of dir/foo.txt.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
pub fn fallback<F2>(self, new_fallback: F2) -> ServeDir<F2>
pub fn fallback<F2>(self, new_fallback: F2) -> ServeDir<F2>
Set the fallback service.
This service will be called if there is no file at the path of the request.
The status code returned by the fallback will not be altered. Use
ServeDir::not_found_service to set a fallback and always respond with 404 Not Found.
pub fn not_found_service<F2>(self, new_fallback: F2) -> ServeDir<SetStatus<F2>>
pub fn not_found_service<F2>(self, new_fallback: F2) -> ServeDir<SetStatus<F2>>
Set the fallback service and override the fallback’s status code to 404 Not Found.
This service will be called if there is no file at the path of the request.
pub fn call_fallback_on_method_not_allowed(
self,
call_fallback: bool,
) -> ServeDir<F>
pub fn call_fallback_on_method_not_allowed( self, call_fallback: bool, ) -> ServeDir<F>
Customize whether or not to call the fallback for requests that aren’t GET or HEAD.
Defaults to not calling the fallback and instead returning 405 Method Not Allowed.
pub fn set_call_fallback_on_method_not_allowed(
&mut self,
call_fallback: bool,
) -> &mut ServeDir<F>
pub fn set_call_fallback_on_method_not_allowed( &mut self, call_fallback: bool, ) -> &mut ServeDir<F>
Customize whether or not to call the fallback for requests that aren’t GET or HEAD.
Defaults to not calling the fallback and instead returning 405 Method Not Allowed.
pub async fn try_call<ReqBody, FResBody>(
&self,
req: Request<ReqBody>,
) -> Result<Response, Error>
pub async fn try_call<ReqBody, FResBody>( &self, req: Request<ReqBody>, ) -> Result<Response, Error>
Call the service and get a future that contains any std::io::Error that might have
happened.
By default <ServeDir as Service<_>>::call will handle IO errors and convert them into
responses. It does that by converting std::io::ErrorKind::NotFound and
std::io::ErrorKind::PermissionDenied to 404 Not Found and any other error to 500 Internal Server Error. The error will also be logged with tracing.
If you want to manually control how the error response is generated you can make a new
service that wraps a ServeDir and calls try_call instead of call.
Trait Implementations§
§impl<ReqBody, F, FResBody> Service<Request<ReqBody>> for ServeDir<F>
impl<ReqBody, F, FResBody> Service<Request<ReqBody>> for ServeDir<F>
§type Error = Infallible
type Error = Infallible
§async fn serve(
&self,
req: Request<ReqBody>,
) -> Result<<ServeDir<F> as Service<Request<ReqBody>>>::Response, <ServeDir<F> as Service<Request<ReqBody>>>::Error>
async fn serve( &self, req: Request<ReqBody>, ) -> Result<<ServeDir<F> as Service<Request<ReqBody>>>::Response, <ServeDir<F> as Service<Request<ReqBody>>>::Error>
§fn boxed(self) -> BoxService<Request, Self::Response, Self::Error>
fn boxed(self) -> BoxService<Request, Self::Response, Self::Error>
Auto Trait Implementations§
impl<F> Freeze for ServeDir<F>where
F: Freeze,
impl<F> RefUnwindSafe for ServeDir<F>where
F: RefUnwindSafe,
impl<F> Send for ServeDir<F>where
F: Send,
impl<F> Sync for ServeDir<F>where
F: Sync,
impl<F> Unpin for ServeDir<F>where
F: Unpin,
impl<F> UnwindSafe for ServeDir<F>where
F: UnwindSafe,
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<S, Body> HttpClientExt for S
impl<S, Body> HttpClientExt for S
§type ExecuteResponse = Response<Body>
type ExecuteResponse = Response<Body>
execute method.§type ExecuteError = <S as Service<Request>>::Error
type ExecuteError = <S as Service<Request>>::Error
execute method.§fn get(
&self,
url: impl IntoUrl,
) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
fn get( &self, url: impl IntoUrl, ) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
GET request to a URL. Read more§fn post(
&self,
url: impl IntoUrl,
) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
fn post( &self, url: impl IntoUrl, ) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
POST request to a URL. Read more§fn put(
&self,
url: impl IntoUrl,
) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
fn put( &self, url: impl IntoUrl, ) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
PUT request to a URL. Read more§fn patch(
&self,
url: impl IntoUrl,
) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
fn patch( &self, url: impl IntoUrl, ) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
PATCH request to a URL. Read more§fn delete(
&self,
url: impl IntoUrl,
) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
fn delete( &self, url: impl IntoUrl, ) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
DELETE request to a URL. Read more§fn head(
&self,
url: impl IntoUrl,
) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
fn head( &self, url: impl IntoUrl, ) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
HEAD request to a URL. Read more§fn connect(
&self,
url: impl IntoUrl,
) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
fn connect( &self, url: impl IntoUrl, ) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
CONNECT request to a URL. Read more§fn request(
&self,
method: Method,
url: impl IntoUrl,
) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
fn request( &self, method: Method, url: impl IntoUrl, ) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
§fn build_from_request<RequestBody>(
&self,
request: Request<RequestBody>,
) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
fn build_from_request<RequestBody>( &self, request: Request<RequestBody>, ) -> RequestBuilder<'_, S, <S as HttpClientExt>::ExecuteResponse>
§fn execute(
&self,
request: Request,
) -> impl Future<Output = Result<<S as HttpClientExt>::ExecuteResponse, <S as HttpClientExt>::ExecuteError>>
fn execute( &self, request: Request, ) -> impl Future<Output = Result<<S as HttpClientExt>::ExecuteResponse, <S as HttpClientExt>::ExecuteError>>
Request. Read more§impl<S, Body> HttpClientWebSocketExt<Body> for S
impl<S, Body> HttpClientWebSocketExt<Body> for S
§fn websocket(
&self,
url: impl IntoUrl,
) -> WebSocketRequestBuilder<WithService<'_, S, Body>>
fn websocket( &self, url: impl IntoUrl, ) -> WebSocketRequestBuilder<WithService<'_, S, Body>>
WebSocketRequestBuilder] to be used to establish a WebSocket connection over http/1.1.§fn websocket_h2(
&self,
url: impl IntoUrl,
) -> WebSocketRequestBuilder<WithService<'_, S, Body>>
fn websocket_h2( &self, url: impl IntoUrl, ) -> WebSocketRequestBuilder<WithService<'_, S, Body>>
WebSocketRequestBuilder to be used to establish a WebSocket connection over h2.§fn websocket_with_request<RequestBody>(
&self,
req: Request<RequestBody>,
) -> WebSocketRequestBuilder<WithService<'_, S, Body>>
fn websocket_with_request<RequestBody>( &self, req: Request<RequestBody>, ) -> WebSocketRequestBuilder<WithService<'_, S, Body>>
WebSocketRequestBuilder starting from the given request. Read more§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<S, R> IntoEndpointService<(R,)> for Swhere
S: Service<Request, Response = R, Error = Infallible>,
R: IntoResponse + Send + Sync + 'static,
impl<S, R> IntoEndpointService<(R,)> for Swhere
S: Service<Request, Response = R, Error = Infallible>,
R: IntoResponse + Send + Sync + 'static,
§fn into_endpoint_service(
self,
) -> impl Service<Request, Response = Response, Error = Infallible>
fn into_endpoint_service( self, ) -> impl Service<Request, Response = Response, Error = Infallible>
rama_core::Service.§impl<S, R, State> IntoEndpointServiceWithState<(R,), State> for Swhere
S: Service<Request, Response = R, Error = Infallible>,
R: IntoResponse + Send + Sync + 'static,
impl<S, R, State> IntoEndpointServiceWithState<(R,), State> for Swhere
S: Service<Request, Response = R, Error = Infallible>,
R: IntoResponse + Send + Sync + 'static,
§fn into_endpoint_service_with_state(
self,
_state: State,
) -> impl Service<Request, Response = Response, Error = Infallible>
fn into_endpoint_service_with_state( self, _state: State, ) -> impl Service<Request, Response = Response, Error = Infallible>
rama_core::Service with state.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more