http compressions stage 1 attempt 1

This commit is contained in:
YK 2024-05-11 04:23:33 +03:00
parent d916e58f20
commit ef899b461e

View File

@ -71,12 +71,15 @@ async fn process (mut stream: TcpStream, args: A) -> Result<()> {
let headers = Headers::parse(&mut data).await; let headers = Headers::parse(&mut data).await;
let encoding = Encoding::from(headers.get("Accept-Encoding"));
use Method as M; use Method as M;
let response = match (method, target.as_str()) { let response = match (method, target.as_str()) {
(M::GET, "/") => Response::Empty, (M::GET, "/") => Response::Empty,
(M::GET, "/user-agent") => Response::TextPlain(headers.get("User-Agent").to_owned()), (M::GET, "/user-agent") => Response::TextPlain(headers.get("User-Agent").to_owned(), encoding),
// p if let Some(echo) = p.strip_prefix("/echo/") => Response::TextPlain(echo), // a nicer way to do that, not available in stable yet // p if let Some(echo) = p.strip_prefix("/echo/") => Response::TextPlain(echo), // a nicer way to do that, not available in stable yet
(M::GET, r) if r.starts_with("/echo/") => Response::TextPlain(r.trim_start_matches("/echo/").to_owned()), (M::GET, r) if r.starts_with("/echo/") => Response::TextPlain(r.trim_start_matches("/echo/").to_owned(), encoding),
(M::GET, r) if r.starts_with("/files/") => 'file : { (M::GET, r) if r.starts_with("/files/") => 'file : {
let Some(path) = &args.directory else { break 'file Response::_500; }; let Some(path) = &args.directory else { break 'file Response::_500; };
let path = path.join(r.trim_start_matches("/files/")); let path = path.join(r.trim_start_matches("/files/"));
@ -136,17 +139,35 @@ impl Headers {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Response { enum Encoding {
_201, Gzip,
_400, Invalid,
_404, None,
_500,
Empty,
TextPlain (String),
OctetStream (Vec<u8>)
} }
impl Encoding {
pub fn header (&self) -> &'static str {
match self {
Self::Gzip => "Content-Encoding: gzip",
_ => d!()
}
}
}
impl From<&str> for Encoding {
fn from (s: &str) -> Self {
match s.to_ascii_lowercase().as_str() {
"" => Self::None,
"gzip" => Self::Gzip,
_ => Self::Invalid
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
enum Method { enum Method {
GET, GET,
@ -172,6 +193,16 @@ impl TryFrom<&str> for Method {
} }
} }
#[derive(Debug, Clone)]
enum Response {
_201,
_400,
_404,
_500,
Empty,
TextPlain (String, Encoding),
OctetStream (Vec<u8>)
}
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
impl Response { impl Response {
@ -186,7 +217,7 @@ impl Response {
Self::OctetStream(bytes) => { Self::OctetStream(bytes) => {
v.extend_from_slice(&bytes); v.extend_from_slice(&bytes);
}, },
Self::TextPlain(text) => { Self::TextPlain(text, _) => {
v.extend_from_slice(text.as_bytes()); v.extend_from_slice(text.as_bytes());
}, },
_ => () _ => ()
@ -207,7 +238,8 @@ impl Response {
fn headers (&self) -> Vec<String> { fn headers (&self) -> Vec<String> {
match self { match self {
Self::TextPlain(text) => vec![ Self::TextPlain(text, enc) => vec![
enc.header().to_string(),
f!("Content-Type: text/plain"), f!("Content-Type: text/plain"),
format!("Content-Length: {}", text.len()) format!("Content-Length: {}", text.len())
], ],