diff options
author | Levente Kurusa <lkurusa@kernelstuff.org> | 2023-05-24 19:54:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-24 19:54:47 +0200 |
commit | 1174715f9904b7e7b95c16ff34da6e9094c3003d (patch) | |
tree | 85b04cba416489f7080c493c41fb82aaaa39a47c /ext/http/http_next.rs | |
parent | e56695daa89b7c53a88a691f35ee9a498caffbdf (diff) |
feat(ext/http): Brotli Compression (#19216)
Add Brotli streaming compression to HTTP
Diffstat (limited to 'ext/http/http_next.rs')
-rw-r--r-- | ext/http/http_next.rs | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/ext/http/http_next.rs b/ext/http/http_next.rs index 30a8c9d51..d479d4a91 100644 --- a/ext/http/http_next.rs +++ b/ext/http/http_next.rs @@ -338,23 +338,30 @@ fn is_request_compressible(headers: &HeaderMap) -> Compression { let Some(accept_encoding) = headers.get(ACCEPT_ENCODING) else { return Compression::None; }; - // Firefox and Chrome send this -- no need to parse - if accept_encoding == "gzip, deflate, br" { - return Compression::GZip; - } - if accept_encoding == "gzip" { - return Compression::GZip; + + match accept_encoding.to_str().unwrap() { + // Firefox and Chrome send this -- no need to parse + "gzip, deflate, br" => return Compression::Brotli, + "gzip" => return Compression::GZip, + "br" => return Compression::Brotli, + _ => (), } + // Fall back to the expensive parser let accepted = fly_accept_encoding::encodings_iter(headers).filter(|r| { - matches!(r, Ok((Some(Encoding::Identity | Encoding::Gzip), _))) + matches!( + r, + Ok(( + Some(Encoding::Identity | Encoding::Gzip | Encoding::Brotli), + _ + )) + ) }); - #[allow(clippy::single_match)] match fly_accept_encoding::preferred(accepted) { - Ok(Some(fly_accept_encoding::Encoding::Gzip)) => return Compression::GZip, - _ => {} + Ok(Some(fly_accept_encoding::Encoding::Gzip)) => Compression::GZip, + Ok(Some(fly_accept_encoding::Encoding::Brotli)) => Compression::Brotli, + _ => Compression::None, } - Compression::None } fn is_response_compressible(headers: &HeaderMap) -> bool { @@ -402,9 +409,14 @@ fn modify_compressibility_from_response( if !is_response_compressible(headers) { return Compression::None; } + let encoding = match compression { + Compression::Brotli => "br", + Compression::GZip => "gzip", + _ => unreachable!(), + }; weaken_etag(headers); headers.remove(CONTENT_LENGTH); - headers.insert(CONTENT_ENCODING, HeaderValue::from_static("gzip")); + headers.insert(CONTENT_ENCODING, HeaderValue::from_static(encoding)); compression } |