fix: properly handle Bytes by writing to stream

This commit is contained in:
Compositr 2024-11-02 11:13:50 +11:00
parent c5e8c151ce
commit 28cf0bbc15

View file

@ -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) => {