use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct Request {
raw_headers: Vec<String>,
status_line: Vec<String>,
body: Vec<u8>,
wildcard: Option<String>,
is_http2: bool,
}
#[derive(Clone, Debug)]
pub enum BodyType {
ASCII(String),
Bytes(Vec<u8>),
}
impl Request {
pub fn new(
raw_body: &[u8],
raw_headers: Vec<String>,
status_line: Vec<String>,
wildcard: Option<String>,
) -> Request {
Request {
body: raw_body.to_vec(),
raw_headers,
status_line,
wildcard,
is_http2: false,
}
}
pub(crate) fn set_wildcard(&mut self, w: Option<String>) -> &Self {
self.wildcard = w;
self
}
pub fn get_raw_body(&self) -> &Vec<u8> {
&self.body
}
pub fn get_parsed_body(&self) -> Option<String> {
if let Ok(s) = std::str::from_utf8(&self.body) {
Some(s.to_string())
} else {
None
}
}
pub fn get_headers(&self) -> HashMap<String, String> {
let mut headers: HashMap<String, String> = HashMap::new();
#[cfg(feature = "log")]
log::trace!("Headers: {:#?}", self.raw_headers);
for i in self.raw_headers.iter() {
let mut iter = i.split(": ");
let key = iter.next().unwrap();
let value = iter.next().unwrap();
headers.insert(key.to_string(), value.to_string());
}
headers
}
pub fn get_status_line(&self) -> &Vec<String> {
&self.status_line
}
pub fn get_wildcard(&self) -> Option<&String> {
self.wildcard.as_ref()
}
pub fn get_http2(&self) -> bool {
self.is_http2
}
pub fn set_http2(mut self, w: bool) -> Self {
self.is_http2 = w;
self
}
}
use thiserror::Error;
#[derive(Error, Debug)]
pub enum RequestError {
#[error("failed to parse status line")]
StatusLineErr,
#[error("failed to parse headers")]
HeadersErr
}