Skip to main content

rws_server/app/controller/not_found/
mod.rs

1use file_ext::FileExt;
2use crate::controller::Controller;
3use crate::header::Header;
4use crate::mime_type::MimeType;
5use crate::range::Range;
6use crate::request::Request;
7use crate::response::{Response, STATUS_CODE_REASON_PHRASE};
8use crate::server::ConnectionInfo;
9
10pub struct NotFoundController;
11
12impl Controller for NotFoundController {
13    fn is_matching(_request: &Request, _connection: &ConnectionInfo) -> bool {
14        true
15    }
16
17    fn process(request: &Request, mut response: Response, _connection: &ConnectionInfo) -> Response {
18        // A file that exists, asked for with a method static files don't
19        // take (POST, PUT, DELETE…): 405 with the methods that work, not
20        // 404 (RFC 9110 §15.5.6, #56).
21        if NotFoundController::is_static_path_with_other_method(request) {
22            response.status_code = *STATUS_CODE_REASON_PHRASE.n405_method_not_allowed.status_code;
23            response.reason_phrase = STATUS_CODE_REASON_PHRASE.n405_method_not_allowed.reason_phrase.to_string();
24            response.headers.push(Header { name: Header::_ALLOW.into(), value: NotFoundController::STATIC_ALLOW.into() });
25            response.content_range_list = vec![Range::get_content_range(
26                b"405 Method Not Allowed".to_vec(),
27                MimeType::TEXT_PLAIN.to_string(),
28            )];
29            return response;
30        }
31
32        response.status_code = *STATUS_CODE_REASON_PHRASE.n404_not_found.status_code;
33        response.reason_phrase = STATUS_CODE_REASON_PHRASE.n404_not_found.reason_phrase.to_string();
34
35
36        if FileExt::does_file_exist(NotFoundController::NOT_FOUND_FILEPATH) {
37            let boxed_content_range =
38                Range::get_content_range_of_a_file(NotFoundController::NOT_FOUND_FILEPATH);
39
40            if boxed_content_range.is_ok() {
41                let content_range = boxed_content_range.unwrap();
42                let content_range_list = vec![content_range];
43                response.content_range_list = content_range_list;
44            } else {
45                let error = boxed_content_range.err().unwrap();
46                let mime_type = MimeType::TEXT_HTML.to_string();
47                let content_range = Range::get_content_range(
48                    Vec::from(error.as_bytes()),
49                    mime_type
50                );
51
52                let content_range_list = vec![content_range];
53                response.content_range_list = content_range_list;
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            // The built-in page (18.37.0: the same template as rws's other
59            // error pages); a 404.html in the working directory replaces it.
60            let page = crate::error::page::html(404, "Not Found", "");
61            let content_range =
62                Range::get_content_range(page.into_bytes(), MimeType::TEXT_HTML.to_string());
63
64
65            let content_range_list = vec![content_range];
66            response.content_range_list = content_range_list;
67
68        }
69
70        response
71    }
72}
73
74impl NotFoundController {
75    /// The `Allow` value for a static file.
76    pub const STATIC_ALLOW: &'static str = "GET, HEAD, OPTIONS";
77
78    /// `true` when the request names a file or directory the static server
79    /// would serve, with a method other than GET, HEAD or OPTIONS.
80    fn is_static_path_with_other_method(request: &Request) -> bool {
81        use crate::request::METHOD;
82        if request.method == METHOD.get || request.method == METHOD.head || request.method == METHOD.options {
83            return false;
84        }
85        let Ok(components) = crate::url::URL::parse_request_target(&request.request_uri) else { return false };
86        if components.path == "/" {
87            return false;
88        }
89        let Ok(fs_path) = crate::app::controller::static_resource::static_fs_path(&components.path) else { return false };
90        std::fs::metadata(&fs_path).is_ok() && crate::app::controller::static_resource::inside_root(&fs_path)
91    }
92}
93
94// backward compatability
95impl NotFoundController {
96    pub const NOT_FOUND_FILEPATH: &'static str = "404.html";
97
98    pub fn is_matching_request(_request: &Request) -> bool {
99        true
100    }
101
102    pub fn process_request(_request: &Request, mut response: Response) -> Response {
103        response.status_code = *STATUS_CODE_REASON_PHRASE.n404_not_found.status_code;
104        response.reason_phrase = STATUS_CODE_REASON_PHRASE.n404_not_found.reason_phrase.to_string();
105
106
107        if FileExt::does_file_exist(NotFoundController::NOT_FOUND_FILEPATH) {
108            let boxed_content_range =
109                Range::get_content_range_of_a_file(NotFoundController::NOT_FOUND_FILEPATH);
110
111            if boxed_content_range.is_ok() {
112                let content_range = boxed_content_range.unwrap();
113                let content_range_list = vec![content_range];
114                response.content_range_list = content_range_list;
115            } else {
116                let error = boxed_content_range.err().unwrap();
117                let mime_type = MimeType::TEXT_HTML.to_string();
118                let content_range = Range::get_content_range(
119                    Vec::from(error.as_bytes()),
120                    mime_type
121                );
122
123                let content_range_list = vec![content_range];
124                response.content_range_list = content_range_list;
125                response.status_code = *STATUS_CODE_REASON_PHRASE.n500_internal_server_error.status_code;
126                response.reason_phrase = STATUS_CODE_REASON_PHRASE.n500_internal_server_error.reason_phrase.to_string();
127            }
128        } else {
129            // The built-in page (18.37.0: the same template as rws's other
130            // error pages); a 404.html in the working directory replaces it.
131            let page = crate::error::page::html(404, "Not Found", "");
132            let content_range =
133                Range::get_content_range(page.into_bytes(), MimeType::TEXT_HTML.to_string());
134
135
136            let content_range_list = vec![content_range];
137            response.content_range_list = content_range_list;
138
139        }
140
141        response
142    }
143}