feat: Bytes body type

This commit is contained in:
Compositr 2024-11-01 22:50:56 +11:00
parent 3d8837cdb1
commit 3d7bfd490a
Signed by: compositr
GPG key ID: 91E3DE20129A0B4A

View file

@ -5,6 +5,7 @@ use super::requests::Request;
pub enum Body {
String(String),
Static(&'static str),
Bytes(Vec<u8>, &'static str),
}
pub struct Response {
@ -40,14 +41,30 @@ fn send_response(mut stream: TcpStream, status: u16, body: Body) -> UnitOrBoxedE
let mut response = format!("HTTP/1.1 {}\r\nServer: luciders/0.1.0\r\n", status_line);
// Headers
response.push_str(&format!(
"Content-Length: {}\r\n",
match &body {
Body::String(s) => s.len(),
Body::Static(s) => s.len(),
Body::Bytes(b, _) => b.len(),
}
));
if let Body::Bytes(_, content_type) = &body {
response.push_str(&format!("Content-Type: {}\r\n", content_type));
}
response.push_str("\r\n");
// Body
match body {
Body::String(s) => response.push_str(&s),
Body::Static(s) => response.push_str(s),
Body::Bytes(b, _) => {
for byte in b {
response.push(byte as char);
}
}
}
match stream.write(response.as_bytes()) {
Ok(_) => {}