http compressions stage 2
This commit is contained in:
parent
38ded8a8c3
commit
16b90fe61c
18
src/main.rs
18
src/main.rs
@ -3,6 +3,7 @@
|
|||||||
use std::{ collections::HashMap, path::PathBuf, sync::Arc };
|
use std::{ collections::HashMap, path::PathBuf, sync::Arc };
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
|
|
||||||
|
use itertools::Itertools;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{ AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader },
|
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 mut data = data.into_inner();
|
||||||
|
|
||||||
let headers = Headers::parse(&mut data).await;
|
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!("{:?}", headers);
|
||||||
println!("{:?}", encoding);
|
println!("{:?}", encoding);
|
||||||
@ -153,10 +154,14 @@ enum Encoding {
|
|||||||
impl Encoding {
|
impl Encoding {
|
||||||
pub fn header (&self) -> &'static str {
|
pub fn header (&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
Self::Gzip => "Content-Encoding: gzip",
|
Self::Gzip => "gzip",
|
||||||
_ => d!()
|
_ => d!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse (s: &str) -> Vec<Self> {
|
||||||
|
s.split(',').map(str::trim).map(From::from).collect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for Encoding {
|
impl From<&str> for Encoding {
|
||||||
@ -202,7 +207,7 @@ enum Response {
|
|||||||
_404,
|
_404,
|
||||||
_500,
|
_500,
|
||||||
Empty,
|
Empty,
|
||||||
TextPlain (String, Encoding),
|
TextPlain (String, Vec<Encoding>),
|
||||||
OctetStream (Vec<u8>)
|
OctetStream (Vec<u8>)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,11 +250,8 @@ impl Response {
|
|||||||
f!("Content-Type: text/plain"),
|
f!("Content-Type: text/plain"),
|
||||||
format!("Content-Length: {}", text.len()),
|
format!("Content-Length: {}", text.len()),
|
||||||
];
|
];
|
||||||
|
let enc = enc.into_iter().map(Encoding::header).filter(|e| !e.is_empty()).join(", ");
|
||||||
let enc = enc.header();
|
if !enc.is_empty() { v.push(f!("Content-Encoding: {enc}")) }
|
||||||
|
|
||||||
if !enc.is_empty() { v.push(enc.to_string()) }
|
|
||||||
|
|
||||||
v
|
v
|
||||||
},
|
},
|
||||||
Self::OctetStream(bytes) => vec![
|
Self::OctetStream(bytes) => vec![
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user