Skip to main content

static_web_server/static_files/
opts.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// This file is part of Static Web Server.
3// See https://static-web-server.net/ for more information
4// Copyright (C) 2019-present Jose Quintana <joseluisq.net>
5
6//! Options and response types for the static-files handler.
7
8use headers::{HeaderMap, HeaderValue};
9use hyper::{Method, Response};
10use std::path::PathBuf;
11
12use crate::body::Body;
13
14#[cfg(feature = "mem-cache")]
15use crate::mem_cache::cache::MemCacheOpts;
16
17#[cfg(feature = "directory-listing")]
18use crate::directory_listing::DirListFmt;
19
20#[cfg(feature = "directory-listing-download")]
21use crate::directory_listing::download::DirDownloadFmt;
22
23/// Default index file used when no index files are configured.
24pub(super) const DEFAULT_INDEX_FILES: &[&str; 1] = &["index.html"];
25
26/// Defines all options needed by the static-files handler.
27pub struct HandleOpts<'a> {
28    /// Request method.
29    pub method: &'a Method,
30    /// In-memory files cache feature.
31    #[cfg(feature = "mem-cache")]
32    pub memory_cache: Option<&'a MemCacheOpts>,
33    /// Request headers.
34    pub headers: &'a HeaderMap<HeaderValue>,
35    /// Request base path.
36    pub base_path: &'a PathBuf,
37    /// Request base path.
38    pub uri_path: &'a str,
39    /// Index files.
40    pub index_files: &'a [&'a str],
41    /// Request URI query.
42    pub uri_query: Option<&'a str>,
43    /// Directory listing feature.
44    #[cfg(feature = "directory-listing")]
45    #[cfg_attr(docsrs, doc(cfg(feature = "directory-listing")))]
46    pub dir_listing: bool,
47    /// Directory listing order feature.
48    #[cfg(feature = "directory-listing")]
49    #[cfg_attr(docsrs, doc(cfg(feature = "directory-listing")))]
50    pub dir_listing_order: u8,
51    /// Directory listing format feature.
52    #[cfg(feature = "directory-listing")]
53    #[cfg_attr(docsrs, doc(cfg(feature = "directory-listing")))]
54    pub dir_listing_format: &'a DirListFmt,
55    /// Directory listing download feature.
56    #[cfg(feature = "directory-listing-download")]
57    #[cfg_attr(docsrs, doc(cfg(feature = "directory-listing-download")))]
58    pub dir_listing_download: &'a [DirDownloadFmt],
59    /// Redirect trailing slash feature.
60    pub redirect_trailing_slash: bool,
61    /// Compression static feature.
62    pub compression_static: bool,
63    /// Weak ETag header feature.
64    pub etag: bool,
65    /// Ignore hidden files feature.
66    pub include_hidden: bool,
67    /// Prevent following symlinks for files and directories.
68    pub follow_symlinks: bool,
69}
70
71/// Static file response type with additional data.
72pub struct StaticFileResponse {
73    /// Inner HTTP response.
74    pub resp: Response<Body>,
75    /// The file path of the inner HTTP response.
76    pub file_path: PathBuf,
77}
78
79impl StaticFileResponse {
80    /// Pairs a response body with the file path that produced it.
81    pub(super) fn new(resp: Response<Body>, file_path: PathBuf) -> Self {
82        Self { resp, file_path }
83    }
84}