ferron/util/
error_pages.rs

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