From 75f9f796095ed6ca67270312e8c39c451213f326 Mon Sep 17 00:00:00 2001 From: myyrakle Date: Tue, 1 Oct 2024 01:37:00 +0900 Subject: [PATCH] =?UTF-8?q?[#154]=20deflate=20=EC=95=95=EC=B6=95=20?= =?UTF-8?q?=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rupring/src/core/mod.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/rupring/src/core/mod.rs b/rupring/src/core/mod.rs index 01ea01a..3f1e0a0 100644 --- a/rupring/src/core/mod.rs +++ b/rupring/src/core/mod.rs @@ -298,6 +298,30 @@ fn post_process_response( .to_string(), ); } + "deflate" => { + // compression + let compressed_bytes = compress_with_deflate(&response.body); + + let compressed_bytes = match compressed_bytes { + Ok(compressed_bytes) => compressed_bytes, + Err(err) => { + eprintln!("Error compressing response body: {:?}", err); + return response; + } + }; + + response.body = compressed_bytes; + + // add header for compression + response.headers.insert( + crate::HeaderName::from_static(header::CONTENT_ENCODING), + application_properties + .server + .compression + .algorithm + .to_string(), + ); + } _ => {} } @@ -313,3 +337,14 @@ fn compress_with_gzip(body: &[u8]) -> anyhow::Result> { Ok(compressed) } + +fn compress_with_deflate(body: &[u8]) -> anyhow::Result> { + use std::io::Write; + + let mut encoder = + flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default()); + encoder.write_all(body)?; + let compressed = encoder.finish()?; + + Ok(compressed) +}