diff --git a/src/http/responses.rs b/src/http/responses.rs index 191d636..0e22a4d 100644 --- a/src/http/responses.rs +++ b/src/http/responses.rs @@ -50,20 +50,20 @@ fn send_response(mut stream: TcpStream, status: u16, body: Body) -> UnitOrBoxedE Body::Bytes(b, _) => b.len(), } )); - if let Body::Bytes(_, content_type) = &body { - response.push_str(&format!("Content-Type: {}\r\n", content_type)); + match &body { + Body::Bytes(_, content_type) => { + response.push_str(&format!("Content-Type: {}\r\n", content_type)) + } + Body::Static(_) => response.push_str("Content-Type: text/plain\r\n"), + _ => {} } response.push_str("\r\n"); - + // Body - match body { - Body::String(s) => response.push_str(&s), + 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()) { @@ -73,6 +73,16 @@ fn send_response(mut stream: TcpStream, status: u16, body: Body) -> UnitOrBoxedE } } + // Special handling for Bytes + if let Body::Bytes(b, _) = &body { + match stream.write(b) { + Ok(_) => {} + Err(e) => { + return Err(Box::new(e)); + } + } + } + match stream.flush() { Ok(_) => {} Err(e) => {