1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/http): unify default gzip compression level (#20050)

This tweaks the HTTP response-writer in order to align the two possible
execution flows into using the same gzip default compression level, that
is `1` (otherwise the implicit default level is `6`).
This commit is contained in:
Luca Bruno 2023-08-04 17:28:32 +02:00 committed by Ryan Dahl
parent 89ba3f820c
commit 69af418487

View file

@ -2,6 +2,7 @@
use async_compression::tokio::write::BrotliEncoder;
use async_compression::tokio::write::GzipEncoder;
use async_compression::Level;
use cache_control::CacheControl;
use deno_core::error::custom_error;
use deno_core::error::AnyError;
@ -700,6 +701,11 @@ fn http_response(
compressing: bool,
encoding: Encoding,
) -> Result<(HttpResponseWriter, hyper::Body), AnyError> {
// Gzip, after level 1, doesn't produce significant size difference.
// This default matches nginx default gzip compression level (1):
// https://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_comp_level
const GZIP_DEFAULT_COMPRESSION_LEVEL: u8 = 1;
match data {
Some(data) if compressing => match encoding {
Encoding::Brotli => {
@ -713,11 +719,10 @@ fn http_response(
Ok((HttpResponseWriter::Closed, writer.into_inner().into()))
}
Encoding::Gzip => {
// Gzip, after level 1, doesn't produce significant size difference.
// Probably the reason why nginx's default gzip compression level is
// 1.
// https://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_comp_level
let mut writer = GzEncoder::new(Vec::new(), Compression::new(1));
let mut writer = GzEncoder::new(
Vec::new(),
Compression::new(GZIP_DEFAULT_COMPRESSION_LEVEL.into()),
);
writer.write_all(&data)?;
Ok((HttpResponseWriter::Closed, writer.finish()?.into()))
}
@ -737,7 +742,10 @@ fn http_response(
let (_, writer) = tokio::io::split(b);
let writer: Pin<Box<dyn tokio::io::AsyncWrite>> = match encoding {
Encoding::Brotli => Box::pin(BrotliEncoder::new(writer)),
Encoding::Gzip => Box::pin(GzipEncoder::new(writer)),
Encoding::Gzip => Box::pin(GzipEncoder::with_quality(
writer,
Level::Precise(GZIP_DEFAULT_COMPRESSION_LEVEL.into()),
)),
_ => unreachable!(), // forbidden by accepts_compression
};
let (stream, shutdown_handle) =