diff --git a/moli-fetch/src/content_coding.rs b/moli-fetch/src/content_coding.rs index d5904292e4c..a4d2fbc5687 100644 --- a/moli-fetch/src/content_coding.rs +++ b/moli-fetch/src/content_coding.rs @@ -1,3 +1,5 @@ +use moli_header_field::{HeaderFieldTokenMode, HeaderFieldTokenizer}; + fn supported_content_codings() -> impl Iterator { let version = curl::Version::get(); [ @@ -20,3 +22,38 @@ pub(crate) fn default_accept_encoding() -> String { value } } + +/// Resource Timing exposes registered, supported content codings rather than +/// reflecting an arbitrary response header into the timing entry. +pub fn resource_timing_content_encoding(headers: &[(String, Vec)]) -> &'static str { + let mut codings = Vec::new(); + for (_, value) in headers + .iter() + .filter(|(name, _)| name.eq_ignore_ascii_case("content-encoding")) + { + let value = crate::decode_header_value(value); + for part in crate::split_http_header_list(&value) { + if part.is_empty() { + codings.push(String::new()); + continue; + } + let mut tokenizer = HeaderFieldTokenizer::new(part); + let Some(coding) = tokenizer.consume_token(HeaderFieldTokenMode::Normal) else { + return ""; + }; + if !tokenizer.is_consumed() { + return ""; + } + codings.push(coding.to_ascii_lowercase()); + } + } + let [coding] = codings.as_slice() else { + return if codings.is_empty() { "" } else { "multiple" }; + }; + if coding.is_empty() { + return ""; + } + supported_content_codings() + .find(|supported| *supported == coding) + .unwrap_or("@unknown") +} diff --git a/moli-fetch/src/lib.rs b/moli-fetch/src/lib.rs index 05393c29f5e..fff5f2c2bab 100644 --- a/moli-fetch/src/lib.rs +++ b/moli-fetch/src/lib.rs @@ -44,6 +44,7 @@ pub use blocking::{ pub use cancellation::{FetchCancelHandle, FetchResponseBodySizes, FetchResponseTiming}; pub use client::{FetchClient, FetchClientHandle}; pub use config::FetchConfig; +pub use content_coding::resource_timing_content_encoding; pub use cors::validate_cors_response_for_origin; pub use cross_origin_resource_policy::CrossOriginResourcePolicy; pub use error::{ @@ -92,7 +93,7 @@ pub use request_policy::{is_bad_port, should_request_be_blocked_due_to_bad_port} pub use response::{ DispatchedRequest, NegotiatedHttpVersion, NetworkRequestExtraInfo, NetworkResponseExtraInfo, PreloadResponseFilter, RawResponse, RedirectInfo, RedirectSource, Response, ResponseBody, - ResponseCacheState, ResponseHead, ResponsePreloadState, resource_timing_content_encoding, + ResponseCacheState, ResponseHead, ResponsePreloadState, }; pub use runtime::PendingStreamingRawResponse; pub use runtime::{ diff --git a/moli-fetch/src/response.rs b/moli-fetch/src/response.rs index e5917f77705..16c07e49e36 100644 --- a/moli-fetch/src/response.rs +++ b/moli-fetch/src/response.rs @@ -6,46 +6,6 @@ use url::Url; use crate::{StreamingHtmlResponse, StreamingRawResponse}; -/// Resource Timing exposes registered, supported content codings rather than -/// reflecting an arbitrary response header into the timing entry. -pub fn resource_timing_content_encoding(headers: &[(String, Vec)]) -> &'static str { - use moli_header_field::{HeaderFieldTokenMode, HeaderFieldTokenizer}; - - let mut codings = Vec::new(); - for (_, value) in headers - .iter() - .filter(|(name, _)| name.eq_ignore_ascii_case("content-encoding")) - { - let value = crate::decode_header_value(value); - for part in crate::split_http_header_list(&value) { - if part.is_empty() { - codings.push(String::new()); - continue; - } - let mut tokenizer = HeaderFieldTokenizer::new(part); - let Some(coding) = tokenizer.consume_token(HeaderFieldTokenMode::Normal) else { - return ""; - }; - if !tokenizer.is_consumed() { - return ""; - } - codings.push(coding.to_ascii_lowercase()); - } - } - let [coding] = codings.as_slice() else { - return if codings.is_empty() { "" } else { "multiple" }; - }; - let version = curl::Version::get(); - match coding.as_str() { - "" => "", - "gzip" if version.feature_libz() => "gzip", - "deflate" if version.feature_libz() => "deflate", - "br" if version.feature_brotli() => "br", - "zstd" if version.feature_zstd() => "zstd", - _ => "@unknown", - } -} - /// Fetch's response cache state, retained separately from the legacy /// `from_cache` flag so Resource Timing can distinguish a 304 validation from /// a response served without a network request. @@ -1024,7 +984,7 @@ pub struct RedirectInfo { #[cfg(test)] mod resource_timing_tests { - use super::resource_timing_content_encoding; + use crate::resource_timing_content_encoding; #[test] fn content_encoding_reports_supported_codings_without_reflecting_private_values() {