ferron/util/
error_pages.rs1use crate::ferron_util::anti_xss::anti_xss;
2
3pub fn generate_default_error_page(
4 status_code: hyper::StatusCode,
5 server_administrator_email: Option<&str>,
6) -> String {
7 let status_code_name = match status_code.canonical_reason() {
8 Some(reason) => format!("{} {}", status_code.as_u16(), reason),
9 None => format!("{}", status_code.as_u16()),
10 };
11
12 let error_500 = format!(
13 "The server encountered an unexpected error. \
14 You may need to contact the server administrator{} to resolve the error.",
15 match server_administrator_email {
16 Some(email_address) => format!(" at {email_address}"),
17 None => String::from(""),
18 }
19 );
20 let status_code_description = String::from(match status_code.as_u16() {
21 200 => "The request was successful!",
22 201 => "A new resource was successfully created.",
23 202 => "The request was accepted but hasn't been fully processed yet.",
24 400 => "The request was invalid.",
25 401 => "Authentication is required to access the resource.",
26 402 => "Payment is required to access the resource.",
27 403 => "You're not authorized to access this resource.",
28 404 => "The requested resource wasn't found. Double-check the URL if entered manually.",
29 405 => "The request method is not allowed for this resource.",
30 406 => "The server cannot provide a response in an acceptable format.",
31 407 => "Proxy authentication is required.",
32 408 => "The request took too long and timed out.",
33 409 => "There's a conflict with the current state of the server.",
34 410 => "The requested resource has been permanently removed.",
35 411 => "The request must include a Content-Length header.",
36 412 => "The request doesn't meet the server's preconditions.",
37 413 => "The request is too large for the server to process.",
38 414 => "The requested URL is too long.",
39 415 => "The server doesn't support the request's media type.",
40 416 => "The requested content range is invalid or unavailable.",
41 417 => "The expectation in the Expect header couldn't be met.",
42 418 => "This server (a teapot) refuses to make coffee! ☕",
43 421 => "The request was directed to the wrong server.",
44 422 => "The server couldn't process the provided content.",
45 423 => "The requested resource is locked.",
46 424 => "The request failed due to a dependency on another failed request.",
47 425 => "The server refuses to process a request that might be replayed.",
48 426 => "The client must upgrade its protocol to proceed.",
49 428 => "A precondition is required for this request, but it wasn't included.",
50 429 => "Too many requests were sent in a short period.",
51 431 => "The request headers are too large.",
52 451 => "Access to this resource is restricted due to legal reasons.",
53 497 => "A non-TLS request was sent to an HTTPS server.",
54 500 => &error_500,
55 501 => "The server doesn't support the requested functionality.",
56 502 => "The server, acting as a gateway, received an invalid response.",
57 503 => {
58 "The server is temporarily unavailable (e.g., maintenance or overload). Try again later."
59 }
60 504 => "The server, acting as a gateway, timed out waiting for a response.",
61 505 => "The HTTP version used in the request isn't supported.",
62 506 => "The Variant header caused a content negotiation loop.",
63 507 => "The server lacks sufficient storage to complete the request.",
64 508 => "The server detected an infinite loop while processing the request.",
65 509 => "Bandwidth limit exceeded on the server.",
66 510 => "The server requires an extended HTTP request, but the client didn't send one.",
67 511 => "Authentication is required to access the network.",
68 598 => "The proxy server didn't receive a response in time.",
69 599 => "The proxy server couldn't establish a connection in time.",
70 _ => "No description found for the status code.",
71 });
72
73 format!(
74 "<!DOCTYPE html>
75<html lang=\"en\">
76<head>
77 <meta charset=\"UTF-8\">
78 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
79 <title>{}</title>
80</head>
81<body>
82 <h1>{}</h1>
83 <p>{}</p>
84</body>
85</html>",
86 anti_xss(&status_code_name),
87 anti_xss(&status_code_name),
88 anti_xss(&status_code_description)
89 )
90}