fix: properly handle Bytes by writing to stream
This commit is contained in:
parent
c5e8c151ce
commit
28cf0bbc15
1 changed files with 20 additions and 10 deletions
|
@ -50,20 +50,20 @@ fn send_response(mut stream: TcpStream, status: u16, body: Body) -> UnitOrBoxedE
|
||||||
Body::Bytes(b, _) => b.len(),
|
Body::Bytes(b, _) => b.len(),
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
if let Body::Bytes(_, content_type) = &body {
|
match &body {
|
||||||
response.push_str(&format!("Content-Type: {}\r\n", content_type));
|
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");
|
response.push_str("\r\n");
|
||||||
|
|
||||||
// Body
|
// Body
|
||||||
match body {
|
match &body {
|
||||||
Body::String(s) => response.push_str(&s),
|
Body::String(s) => response.push_str(s),
|
||||||
Body::Static(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()) {
|
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() {
|
match stream.flush() {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
Loading…
Reference in a new issue