Update static website 404 page
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import html as html_module
|
|
||||||
import itertools
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
import mimetypes
|
import mimetypes
|
||||||
@@ -720,9 +719,10 @@ def _configure_logging(app: Flask) -> None:
|
|||||||
return _website_error_response(status_code, "Not Found")
|
return _website_error_response(status_code, "Not Found")
|
||||||
|
|
||||||
def _website_error_response(status_code, message):
|
def _website_error_response(status_code, message):
|
||||||
safe_msg = html_module.escape(str(message))
|
if status_code == 404:
|
||||||
safe_code = html_module.escape(str(status_code))
|
body = "404 page not found"
|
||||||
body = f"<html><head><title>{safe_code} {safe_msg}</title></head><body><h1>{safe_code} {safe_msg}</h1></body></html>"
|
else:
|
||||||
|
body = f"{status_code} {message}"
|
||||||
return Response(body, status=status_code, mimetype="text/html")
|
return Response(body, status=status_code, mimetype="text/html")
|
||||||
|
|
||||||
@app.after_request
|
@app.after_request
|
||||||
|
|||||||
@@ -439,4 +439,4 @@ class TestWebsiteServing:
|
|||||||
store.set_mapping("noerr.example.com", "no-err")
|
store.set_mapping("noerr.example.com", "no-err")
|
||||||
resp = website_client.get("/missing.html", headers={"Host": "noerr.example.com"})
|
resp = website_client.get("/missing.html", headers={"Host": "noerr.example.com"})
|
||||||
assert resp.status_code == 404
|
assert resp.status_code == 404
|
||||||
assert b"Not Found" in resp.data
|
assert resp.data == b"404 page not found"
|
||||||
|
|||||||
@@ -44,32 +44,12 @@ fn website_error_response(
|
|||||||
|
|
||||||
fn default_website_error_body(status: StatusCode) -> String {
|
fn default_website_error_body(status: StatusCode) -> String {
|
||||||
let code = status.as_u16();
|
let code = status.as_u16();
|
||||||
let reason = status.canonical_reason().unwrap_or("Error");
|
if status == StatusCode::NOT_FOUND {
|
||||||
format!(
|
"404 page not found".to_string()
|
||||||
"<!doctype html>\
|
} else {
|
||||||
<html lang=\"en\">\
|
let reason = status.canonical_reason().unwrap_or("Error");
|
||||||
<head>\
|
format!("{code} {reason}")
|
||||||
<meta charset=\"utf-8\">\
|
}
|
||||||
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
|
|
||||||
<title>{code} {reason}</title>\
|
|
||||||
<style>\
|
|
||||||
html{{font-family:Arial,Helvetica,sans-serif;background:#f8fafc;color:#172033}}\
|
|
||||||
body{{margin:0;min-height:100vh;display:grid;place-items:center}}\
|
|
||||||
main{{max-width:42rem;padding:3rem 2rem}}\
|
|
||||||
p.code{{font-size:.78rem;text-transform:uppercase;letter-spacing:.12em;color:#64748b;margin:0 0 .75rem}}\
|
|
||||||
h1{{font-size:2rem;line-height:1.15;margin:0 0 1rem}}\
|
|
||||||
p{{font-size:1rem;line-height:1.6;margin:0;color:#334155}}\
|
|
||||||
</style>\
|
|
||||||
</head>\
|
|
||||||
<body>\
|
|
||||||
<main>\
|
|
||||||
<p class=\"code\">HTTP {code}</p>\
|
|
||||||
<h1>{code} {reason}</h1>\
|
|
||||||
<p>The requested page could not be found. Check the URL, or return to the site root.</p>\
|
|
||||||
</main>\
|
|
||||||
</body>\
|
|
||||||
</html>"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_range_header(range_header: &str, total_size: u64) -> Option<(u64, u64)> {
|
fn parse_range_header(range_header: &str, total_size: u64) -> Option<(u64, u64)> {
|
||||||
|
|||||||
@@ -3323,8 +3323,7 @@ async fn test_static_website_default_404_returns_html_body() {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(body.len(), content_length);
|
assert_eq!(body.len(), content_length);
|
||||||
assert!(body.contains("<h1>404 Not Found</h1>"));
|
assert_eq!(body, "404 page not found");
|
||||||
assert!(body.len() > 512);
|
|
||||||
|
|
||||||
let head_resp = app
|
let head_resp = app
|
||||||
.oneshot(website_request(Method::HEAD, "/missing.html"))
|
.oneshot(website_request(Method::HEAD, "/missing.html"))
|
||||||
|
|||||||
Reference in New Issue
Block a user