refactor: dry'd HTTP/1.1

This commit is contained in:
Compositr 2024-11-01 21:15:34 +11:00
parent 8d8cfe33fa
commit 6744b6dee1
Signed by: compositr
GPG key ID: 91E3DE20129A0B4A

View file

@ -24,15 +24,15 @@ pub type UnitOrBoxedError = Result<(), Box<dyn Error>>;
fn send_response(mut stream: TcpStream, status: u16, body: &str) -> UnitOrBoxedError {
let status_line = match status {
200 => "HTTP/1.1 200 OK",
400 => "HTTP/1.1 400 BAD REQUEST",
404 => "HTTP/1.1 404 NOT FOUND",
405 => "HTTP/1.1 405 METHOD NOT ALLOWED",
_ => "HTTP/1.1 500 INTERNAL SERVER ERROR",
200 => "200 OK",
400 => "400 BAD REQUEST",
404 => "404 NOT FOUND",
405 => "405 METHOD NOT ALLOWED",
_ => "500 INTERNAL SERVER ERROR",
};
let response = format!(
"{}\r\nContent-Length: {}\r\nServer: luciders/0.1.0\r\n\r\n{}",
"{}\r\nContent-Length: {}\r\nServer: luciders/0.1.0\r\n\r\nHTTP/1.1 {}",
status_line,
body.len(),
body