mirror of
https://github.com/lexmount/moli.git
synced 2026-11-22 00:02:21 +00:00
fix(fetch): match encoding cache variants against actual request headers
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use moli_header_field::{HeaderFieldTokenMode, HeaderFieldTokenizer};
|
||||
|
||||
fn supported_content_codings() -> impl Iterator<Item = &'static str> {
|
||||
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<u8>)]) -> &'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")
|
||||
}
|
||||
|
||||
@@ -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::{
|
||||
|
||||
@@ -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<u8>)]) -> &'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() {
|
||||
|
||||
Reference in New Issue
Block a user