use std::collections::HashMap;
use crate::{Response, StatusCode};
impl Response {
pub fn get_headers(&self) -> &HashMap<String, String> {
&self.headers
}
pub fn get_header(&self, key: &str) -> Option<&String> {
self.headers.get(key)
}
pub fn set_header(&mut self, key: &str, value: &str) -> &mut Self {
self.headers.insert(key.to_string(), value.to_string());
self
}
pub fn get_body(&self) -> &Vec<u8> {
&self.body
}
pub fn get_body_mut(&mut self) -> &mut Vec<u8> {
&mut self.body
}
pub fn set_body(&mut self, body: Vec<u8>) -> Result<(), String> {
if self.body.is_empty() {
return Err("Request has no body.".to_string());
}
self.body = body;
return Ok(());
}
pub fn get_local(&self, key: &str) -> Option<&String> {
self.locals.get(key)
}
pub fn set_local(&mut self, key: &str, value: &str) -> Option<std::string::String> {
self.locals.insert(key.to_string(), value.to_string())
}
pub fn get_status(&self) -> StatusCode {
self.status_code
}
pub fn is_error(&self) -> bool {
match self.status_code {
StatusCode::BadRequest => true,
StatusCode::Unauthorized => true,
StatusCode::Forbidden => true,
StatusCode::NotFound => true,
StatusCode::MethodNotAllowed => true,
StatusCode::RequestTimeout => true,
StatusCode::LengthRequired => true,
StatusCode::UnsupportedMediaType => true,
StatusCode::IAmATeapot => true,
StatusCode::TooManyRequests => true,
StatusCode::InternalServerError => true,
StatusCode::NotImplemented => true,
StatusCode::ServiceUnavailable => true,
StatusCode::GatewayTimeout => true,
StatusCode::HttpVersionNotSupported => true,
_ => false,
}
}
}