http compressions stage 2

This commit is contained in:
YK 2024-05-11 04:42:02 +03:00
parent 38ded8a8c3
commit 16b90fe61c

View File

@ -3,6 +3,7 @@
use std::{ collections::HashMap, path::PathBuf, sync::Arc };
use anyhow::{bail, Result};
use itertools::Itertools;
use tokio::{
fs::File,
io::{ AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader },
@ -69,7 +70,7 @@ async fn process (mut stream: TcpStream, args: A) -> Result<()> {
let mut data = data.into_inner();
let headers = Headers::parse(&mut data).await;
let encoding = Encoding::from(headers.get("Accept-Encoding"));
let encoding = Encoding::parse(headers.get("Accept-Encoding"));
println!("{:?}", headers);
println!("{:?}", encoding);
@ -153,10 +154,14 @@ enum Encoding {
impl Encoding {
pub fn header (&self) -> &'static str {
match self {
Self::Gzip => "Content-Encoding: gzip",
Self::Gzip => "gzip",
_ => d!()
}
}
pub fn parse (s: &str) -> Vec<Self> {
s.split(',').map(str::trim).map(From::from).collect()
}
}
impl From<&str> for Encoding {
@ -202,7 +207,7 @@ enum Response {
_404,
_500,
Empty,
TextPlain (String, Encoding),
TextPlain (String, Vec<Encoding>),
OctetStream (Vec<u8>)
}
@ -245,11 +250,8 @@ impl Response {
f!("Content-Type: text/plain"),
format!("Content-Length: {}", text.len()),
];
let enc = enc.header();
if !enc.is_empty() { v.push(enc.to_string()) }
let enc = enc.into_iter().map(Encoding::header).filter(|e| !e.is_empty()).join(", ");
if !enc.is_empty() { v.push(f!("Content-Encoding: {enc}")) }
v
},
Self::OctetStream(bytes) => vec![