Update static website 404 page

This commit is contained in:
2026-04-21 21:26:50 +08:00
parent c77c592832
commit 8935188c8f
4 changed files with 12 additions and 33 deletions

View File

@@ -1,6 +1,5 @@
from __future__ import annotations
import html as html_module
import itertools
import logging
import mimetypes
@@ -720,9 +719,10 @@ def _configure_logging(app: Flask) -> None:
return _website_error_response(status_code, "Not Found")
def _website_error_response(status_code, message):
safe_msg = html_module.escape(str(message))
safe_code = html_module.escape(str(status_code))
body = f"<html><head><title>{safe_code} {safe_msg}</title></head><body><h1>{safe_code} {safe_msg}</h1></body></html>"
if status_code == 404:
body = "404 page not found"
else:
body = f"{status_code} {message}"
return Response(body, status=status_code, mimetype="text/html")
@app.after_request

View File

@@ -439,4 +439,4 @@ class TestWebsiteServing:
store.set_mapping("noerr.example.com", "no-err")
resp = website_client.get("/missing.html", headers={"Host": "noerr.example.com"})
assert resp.status_code == 404
assert b"Not Found" in resp.data
assert resp.data == b"404 page not found"

View File

@@ -44,32 +44,12 @@ fn website_error_response(
fn default_website_error_body(status: StatusCode) -> String {
let code = status.as_u16();
if status == StatusCode::NOT_FOUND {
"404 page not found".to_string()
} else {
let reason = status.canonical_reason().unwrap_or("Error");
format!(
"<!doctype html>\
<html lang=\"en\">\
<head>\
<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>"
)
format!("{code} {reason}")
}
}
fn parse_range_header(range_header: &str, total_size: u64) -> Option<(u64, u64)> {

View File

@@ -3323,8 +3323,7 @@ async fn test_static_website_default_404_returns_html_body() {
)
.unwrap();
assert_eq!(body.len(), content_length);
assert!(body.contains("<h1>404 Not Found</h1>"));
assert!(body.len() > 512);
assert_eq!(body, "404 page not found");
let head_resp = app
.oneshot(website_request(Method::HEAD, "/missing.html"))