Skip to content

Commit

Permalink
[#154] deflate 압축 알고리즘 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Sep 30, 2024
1 parent 41a4f3c commit 75f9f79
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions rupring/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
_ => {}
}

Expand All @@ -313,3 +337,14 @@ fn compress_with_gzip(body: &[u8]) -> anyhow::Result<Vec<u8>> {

Ok(compressed)
}

fn compress_with_deflate(body: &[u8]) -> anyhow::Result<Vec<u8>> {
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)
}

0 comments on commit 75f9f79

Please sign in to comment.