feat: Bytes body type
This commit is contained in:
parent
3d8837cdb1
commit
3d7bfd490a
1 changed files with 18 additions and 1 deletions
|
@ -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(_) => {}
|
||||
|
|
Loading…
Reference in a new issue