Skip to main content

rust_web_server/app/controller/directory_listing/
mod.rs

1use file_ext::FileExt;
2use crate::controller::Controller;
3use crate::mime_type::MimeType;
4use crate::range::Range;
5use crate::request::{METHOD, Request};
6use crate::response::{Response, STATUS_CODE_REASON_PHRASE};
7use crate::server::ConnectionInfo;
8
9#[cfg(test)]
10mod tests;
11
12/// Serves the CSS/JS assets for the directory listing page generated by
13/// [`crate::app::controller::static_resource::StaticResourceController`].
14///
15/// These are same-origin `<link>`/`<script src>` assets rather than inline
16/// `<style>`/`<script>` blocks so the listing page renders correctly under
17/// the framework's default `Content-Security-Policy: default-src 'self'` —
18/// inline blocks would otherwise be silently blocked by that policy.
19///
20/// Mirrors [`StyleController`](crate::app::controller::style::StyleController) /
21/// [`ScriptController`](crate::app::controller::script::ScriptController):
22/// a file on disk at the same relative path overrides the compiled-in default,
23/// so deployments can restyle the listing without recompiling.
24pub struct DirectoryListingAssetsController;
25
26impl DirectoryListingAssetsController {
27    pub const CSS_FILEPATH: &'static str = "rws-directory-listing.css";
28    pub const JS_FILEPATH: &'static str = "rws-directory-listing.js";
29
30    fn css_path() -> String {
31        format!("/{}", Self::CSS_FILEPATH)
32    }
33
34    fn js_path() -> String {
35        format!("/{}", Self::JS_FILEPATH)
36    }
37
38    fn serve(filepath: &'static str, embedded: &'static [u8], mime_type: &str, mut response: Response) -> Response {
39        response.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
40        response.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
41
42        if FileExt::does_file_exist(filepath) {
43            let boxed_content_range = Range::get_content_range_of_a_file(filepath);
44
45            if boxed_content_range.is_ok() {
46                response.content_range_list = vec![boxed_content_range.unwrap()];
47            } else {
48                let error = boxed_content_range.err().unwrap();
49                let content_range = Range::get_content_range(
50                    Vec::from(error.as_bytes()),
51                    MimeType::TEXT_HTML.to_string(),
52                );
53                response.content_range_list = vec![content_range];
54                response.status_code = *STATUS_CODE_REASON_PHRASE.n500_internal_server_error.status_code;
55                response.reason_phrase = STATUS_CODE_REASON_PHRASE.n500_internal_server_error.reason_phrase.to_string();
56            }
57        } else {
58            let content_range = Range::get_content_range(embedded.to_vec(), mime_type.to_string());
59            response.content_range_list = vec![content_range];
60        }
61
62        response
63    }
64}
65
66impl Controller for DirectoryListingAssetsController {
67    fn is_matching(request: &Request, _connection: &ConnectionInfo) -> bool {
68        request.method == METHOD.get
69            && (request.request_uri == Self::css_path() || request.request_uri == Self::js_path())
70    }
71
72    fn process(request: &Request, response: Response, _connection: &ConnectionInfo) -> Response {
73        if request.request_uri == Self::css_path() {
74            let embedded = include_bytes!("directory_listing.css");
75            Self::serve(Self::CSS_FILEPATH, embedded, MimeType::TEXT_CSS, response)
76        } else {
77            let embedded = include_bytes!("directory_listing.js");
78            Self::serve(Self::JS_FILEPATH, embedded, MimeType::TEXT_JAVASCRIPT, response)
79        }
80    }
81}