This commit is contained in:
Mateusz Gruszczyński committed 2026-10-19 15:40:37 +02:00
1 parent 34fb534e02
commit b2b5b45d8a
22 files changed
+2146 -107

No files matched your search

+34 -33
View File
@@ -21,7 +21,8 @@ use tokio::io::{AsyncReadExt, AsyncSeekExt};
use tokio_util::io::ReaderStream;
pub(super) fn status_response(status: StatusCode, detail: &'static str) -> Response {
// Keep internal diagnostics in server logs, not in public HTTP bodies.
// Keep implementation diagnostics in logs; use the existing embedded
// application-style pages for every Web Server-generated error.
if status.is_server_error() {
tracing::warn!(
status = status.as_u16(),
@@ -29,46 +30,46 @@ pub(super) fn status_response(status: StatusCode, detail: &'static str) -> Respo
"Web Server error"
);
}
let message = format!(
"{} {}\n",
status.as_u16(),
status.canonical_reason().unwrap_or("Error")
);
let mut response = Response::new(Body::from(message.clone()));
*response.status_mut() = status;
response.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_static("text/plain; charset=utf-8"),
);
response.headers_mut().insert(
CONTENT_LENGTH,
HeaderValue::from_str(&message.len().to_string()).unwrap(),
);
response
.headers_mut()
.insert(CACHE_CONTROL, HeaderValue::from_static("no-store"));
response.headers_mut().insert(
"x-content-type-options",
HeaderValue::from_static("nosniff"),
);
response
// The public serve() facade strips bodies from HEAD responses while
// retaining the same Content-Length as the corresponding GET.
builtin_error_response(status, &Method::GET)
}
pub(super) fn builtin_error_response(status: StatusCode, method: &Method) -> Response {
let html = crate::pages::builtin_gateway_error(status)
.unwrap_or("<!doctype html><html><body><h1>Error</h1></body></html>");
let mut response = Response::new(if *method == Method::HEAD {
Body::empty()
} else {
Body::from(html)
});
// Reuse the bundled pages wherever one exists. For statuses such as 405,
// use the shared styled application fallback instead of a plain-text or
// unstyled generic error. Do not mark these as PageServed so normal Vhost
// error-page policy can still replace them.
let (body, length) = match crate::pages::builtin_gateway_error(status) {
Some(html) => (
if *method == Method::HEAD {
Body::empty()
} else {
Body::from(html)
},
html.len(),
),
None => {
let html = crate::pages::builtin_application_page(status);
let length = html.len();
(
if *method == Method::HEAD {
Body::empty()
} else {
Body::from(html)
},
length,
)
}
};
let mut response = Response::new(body);
*response.status_mut() = status;
response.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);
if let Ok(length) = HeaderValue::from_str(&html.len().to_string()) {
response.headers_mut().insert(CONTENT_LENGTH, length);
if let Ok(value) = HeaderValue::from_str(&length.to_string()) {
response.headers_mut().insert(CONTENT_LENGTH, value);
}
response
.headers_mut()