diff --git a/src/http/responses.rs b/src/http/responses.rs index d6ce308..191d636 100644 --- a/src/http/responses.rs +++ b/src/http/responses.rs @@ -5,6 +5,7 @@ use super::requests::Request; pub enum Body { String(String), Static(&'static str), + Bytes(Vec, &'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(_) => {}