summaryrefslogtreecommitdiff
path: root/ext/http/fly_accept_encoding.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-12-22 01:54:28 +0100
committerGitHub <noreply@github.com>2023-12-22 01:54:28 +0100
commitf86456fc26d1c02f6c511125037efed576f87458 (patch)
treebe9615e8cec34cc21691132fa26d57ff47fde110 /ext/http/fly_accept_encoding.rs
parent3fb4f3fe5a18916aa95f8b035ca994c290c173dc (diff)
chore: update ext/http to hyper 1.0.1 and http 1.0 (#21588)
Closes https://github.com/denoland/deno/issues/21583.
Diffstat (limited to 'ext/http/fly_accept_encoding.rs')
-rw-r--r--ext/http/fly_accept_encoding.rs42
1 files changed, 33 insertions, 9 deletions
diff --git a/ext/http/fly_accept_encoding.rs b/ext/http/fly_accept_encoding.rs
index f3de9bee4..af687c254 100644
--- a/ext/http/fly_accept_encoding.rs
+++ b/ext/http/fly_accept_encoding.rs
@@ -3,8 +3,7 @@
// Forked from https://github.com/superfly/accept-encoding/blob/1cded757ec7ff3916e5bfe7441db76cdc48170dc/
// Forked to support both http 0.3 and http 1.0 crates.
-use http::header::HeaderMap;
-use http::header::ACCEPT_ENCODING;
+use http as http_02;
use itertools::Itertools;
/// A list enumerating the categories of errors in this crate.
@@ -76,13 +75,36 @@ pub fn preferred(
}
/// Parse a set of HTTP headers into an iterator containing tuples of options containing encodings and their corresponding q-values.
-pub fn encodings_iter(
- headers: &HeaderMap,
+///
+/// Compatible with `http` crate for version 0.2.x.
+pub fn encodings_iter_http_02(
+ headers: &http_02::HeaderMap,
) -> impl Iterator<Item = Result<(Option<Encoding>, f32), EncodingError>> + '_ {
- headers
- .get_all(ACCEPT_ENCODING)
+ let iter = headers
+ .get_all(http_02::header::ACCEPT_ENCODING)
+ .iter()
+ .map(|hval| hval.to_str().map_err(|_| EncodingError::InvalidEncoding));
+ encodings_iter_inner(iter)
+}
+
+/// Parse a set of HTTP headers into an iterator containing tuples of options containing encodings and their corresponding q-values.
+///
+/// Compatible with `http` crate for version 1.x.
+pub fn encodings_iter_http_1(
+ headers: &http_1::HeaderMap,
+) -> impl Iterator<Item = Result<(Option<Encoding>, f32), EncodingError>> + '_ {
+ let iter = headers
+ .get_all(http_1::header::ACCEPT_ENCODING)
.iter()
- .map(|hval| hval.to_str().map_err(|_| EncodingError::InvalidEncoding))
+ .map(|hval| hval.to_str().map_err(|_| EncodingError::InvalidEncoding));
+ encodings_iter_inner(iter)
+}
+
+/// Parse a set of HTTP headers into an iterator containing tuples of options containing encodings and their corresponding q-values.
+fn encodings_iter_inner<'s>(
+ headers: impl Iterator<Item = Result<&'s str, EncodingError>> + 's,
+) -> impl Iterator<Item = Result<(Option<Encoding>, f32), EncodingError>> + 's {
+ headers
.map_ok(|s| s.split(',').map(str::trim))
.flatten_ok()
.filter_map_ok(|v| {
@@ -104,16 +126,18 @@ pub fn encodings_iter(
#[cfg(test)]
mod tests {
use super::*;
+ use http::header::ACCEPT_ENCODING;
+ use http::HeaderMap;
use http::HeaderValue;
fn encodings(
headers: &HeaderMap,
) -> Result<Vec<(Option<Encoding>, f32)>, EncodingError> {
- encodings_iter(headers).collect()
+ encodings_iter_http_02(headers).collect()
}
fn parse(headers: &HeaderMap) -> Result<Option<Encoding>, EncodingError> {
- preferred(encodings_iter(headers))
+ preferred(encodings_iter_http_02(headers))
}
#[test]