diff options
author | Luca Bruno <lucab@lucabruno.net> | 2023-08-04 17:28:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-04 17:28:32 +0200 |
commit | 5abf4cd951792a8d5da8ecc70b219a66f70dbe3e (patch) | |
tree | a2ea3f533e3377f2aa8f387cfcece02ed1b71d00 /ext/http/lib.rs | |
parent | 8a175a780a43b4cec7adfff5adbc65d8784aa4ed (diff) |
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`).
Diffstat (limited to 'ext/http/lib.rs')
-rw-r--r-- | ext/http/lib.rs | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/ext/http/lib.rs b/ext/http/lib.rs index 5a8788f92..21c051cae 100644 --- a/ext/http/lib.rs +++ b/ext/http/lib.rs @@ -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; @@ -702,6 +703,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 => { @@ -715,11 +721,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())) } @@ -739,7 +744,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) = |