diff --git a/src/main.rs b/src/main.rs index d9441c3..8e89c93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { + 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), OctetStream (Vec) } @@ -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![