use super::collection::HashMapCollection;
#[derive(Debug, Clone, Default)]
pub struct ResponseData {
pub status: u16,
pub protocol: String,
pub headers: HashMapCollection,
pub body: Vec<u8>,
pub content_type: String,
}
impl ResponseData {
pub fn new() -> Self {
Self::default()
}
pub fn set_status(&mut self, status: u16) {
self.status = status;
}
pub fn set_protocol(&mut self, protocol: &str) {
self.protocol = protocol.to_string();
}
pub fn add_header(&mut self, name: &str, value: &str) {
self.headers.add(name.to_lowercase(), value.to_string());
if name.eq_ignore_ascii_case("content-type") {
self.content_type = value.to_string();
}
}
pub fn append_body(&mut self, data: &[u8]) {
self.body.extend_from_slice(data);
}
pub fn body_str(&self) -> String {
String::from_utf8_lossy(&self.body).to_string()
}
pub fn body_length(&self) -> usize {
self.body.len()
}
}