use crate::prelude::*;
pub async fn sanitize_error_into_response(error: SanitizeError, host: &Host) -> FatResponse {
default_response(
match error {
SanitizeError::UnsafePath => StatusCode::BAD_REQUEST,
SanitizeError::RangeNotSatisfiable => StatusCode::RANGE_NOT_SATISFIABLE,
},
host,
match error {
SanitizeError::UnsafePath => Some("path contains illegal segments (e.g. `./`)"),
SanitizeError::RangeNotSatisfiable => None,
},
)
.await
}
#[inline]
pub async fn default(
code: StatusCode,
host: Option<&Host>,
message: Option<&[u8]>,
) -> Response<Bytes> {
let body = match host {
Some(host) => {
let path = utils::make_path(
&host.path,
host.options.get_errors_dir(),
code.as_str(),
Some("html"),
);
if host.options.disable_fs {
utils::hardcoded_error_body(code, message)
} else {
match read_file_cached(&path, host.file_cache.as_ref()).await {
Some(file) => file,
None => utils::hardcoded_error_body(code, message),
}
}
}
None => utils::hardcoded_error_body(code, message),
};
let mut builder = Response::builder()
.status(code)
.header("content-type", "text/html; charset=utf-8")
.header("content-encoding", "identity");
if let Some(message) = message.map(HeaderValue::from_bytes).and_then(Result::ok) {
builder = builder.header("reason", message);
}
builder.body(body).unwrap()