41 lines
927 B
Rust
41 lines
927 B
Rust
use crate::Status;
|
|
use std::collections::HashMap;
|
|
use wetstring::Response;
|
|
|
|
pub struct HttpResponse {
|
|
pub status: Status,
|
|
pub headers: HashMap<String, String>,
|
|
pub body: Option<Vec<u8>>,
|
|
}
|
|
|
|
impl HttpResponse {
|
|
pub fn new(status: Status) -> Self {
|
|
Self {
|
|
status,
|
|
headers: HashMap::new(),
|
|
body: None,
|
|
}
|
|
}
|
|
|
|
pub fn with_utf8_body(mut self, body: &str) -> Self {
|
|
self.body = Some(body.as_bytes().to_vec());
|
|
return self;
|
|
}
|
|
|
|
pub fn client_error() -> Self {
|
|
Self::new(Status::ClientError).with_utf8_body("Client error")
|
|
}
|
|
}
|
|
|
|
impl Response for HttpResponse {
|
|
fn to_bytes(self) -> Vec<u8> {
|
|
let mut bytes = format!("HTTP/1.1 {}\n\n", self.status.code())
|
|
.as_bytes()
|
|
.to_vec();
|
|
if let Some(body) = self.body {
|
|
bytes.extend(body);
|
|
}
|
|
return bytes;
|
|
}
|
|
}
|