use hotaru_core::protocol::ProtocolError;
use crate::message::http_value::StatusCode;
use crate::message::request::HttpRequest;
use crate::message::response::{HttpResponse, response_templates};
use crate::protocol::error::HttpError;
pub fn is_keep_alive(request: &HttpRequest) -> bool {
if let Some(connection) = request.meta.header.get("connection") {
connection.as_str().to_lowercase() != "close"
} else {
true
}
}
pub fn is_response_keep_alive(response: &HttpResponse) -> bool {
if let Some(connection) = response.meta.header.get("connection") {
connection.as_str().to_lowercase() != "close"
} else {
true
}
}
fn html_error_body(status: &StatusCode) -> String {
let code = status.as_u16();
let reason = status.reason_phrase();
format!(
"<!DOCTYPE html>\n\
<html>\n\
<head><title>{code} {reason}</title></head>\n\
<body><h1>{code} {reason}</h1></body>\n\
</html>\n"
)
}
fn html_status_response(status: StatusCode) -> HttpResponse {
let body = html_error_body(&status).into_bytes();
response_templates::html_response(body).status(status)
}
pub fn not_found_response() -> HttpResponse {
html_status_response(StatusCode::NOT_FOUND)
}
pub fn error_response_from(err: &dyn ProtocolError) -> HttpResponse {
let status = if let Some(http_err) =
(err as &dyn std::error::Error).downcast_ref::<HttpError>()
{
http_err.into()
} else {
StatusCode::INTERNAL_SERVER_ERROR
};
html_status_response(status)
}