diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2022-05-18 16:14:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-18 16:14:23 +0200 |
commit | f2410b4481ec4e2cda804fe05d30273bba3d4008 (patch) | |
tree | a28d8551eec89602f231a351e8c8d97388375f20 /ext/http/lib.rs | |
parent | c4b7bdb9d13e04bbca2ef1c30b094cb97e208002 (diff) |
perf(ext/http): faster accept-encoding parsing (#14654)
Diffstat (limited to 'ext/http/lib.rs')
-rw-r--r-- | ext/http/lib.rs | 36 |
1 files changed, 11 insertions, 25 deletions
diff --git a/ext/http/lib.rs b/ext/http/lib.rs index 4d19c5edb..918e48120 100644 --- a/ext/http/lib.rs +++ b/ext/http/lib.rs @@ -390,31 +390,17 @@ async fn op_http_accept( _ => unreachable!(), }; - { - let mut accept_encoding = stream.accept_encoding.borrow_mut(); - - // curl --compressed sends "Accept-Encoding: deflate, gzip". - // fly_accept_encoding::parse() returns Encoding::Deflate. - // Deno does not support Encoding::Deflate. - // So, Deno used no compression, although gzip was possible. - // This patch makes Deno use gzip instead in this case. - *accept_encoding = Encoding::Identity; - let mut max_qval = 0.0; - if let Ok(encodings) = fly_accept_encoding::encodings(request.headers()) { - for (encoding, qval) in encodings { - if let Some(enc @ (Encoding::Brotli | Encoding::Gzip)) = encoding { - // this logic came from fly_accept_encoding. - if (qval - 1.0f32).abs() < 0.01 { - *accept_encoding = enc; - break; - } else if qval > max_qval { - *accept_encoding = enc; - max_qval = qval; - } - } - } - } - } + stream.accept_encoding.replace({ + let encodings = fly_accept_encoding::encodings_iter(request.headers()) + .filter(|r| { + matches!(r, Ok((Some(Encoding::Brotli | Encoding::Gzip), _))) + }); + + fly_accept_encoding::preferred(encodings) + .ok() + .flatten() + .unwrap_or(Encoding::Identity) + }); let method = request.method().to_string(); let headers = req_headers(request); |