diff options
author | Philip Lamb <phil.git@eden.net.nz> | 2024-04-12 09:51:23 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-11 21:51:23 +0000 |
commit | 10ec8565eaa0e9e86d3a6506ab1e83d5a41f51c8 (patch) | |
tree | 107d39dc9ceb56eb44c0c65982f3e21c60502835 /components | |
parent | 62a25fdcc4165ecafc6f40adc8a3e2c47838e4a3 (diff) | |
download | servo-10ec8565eaa0e9e86d3a6506ab1e83d5a41f51c8.tar.gz servo-10ec8565eaa0e9e86d3a6506ab1e83d5a41f51c8.zip |
Fixes for HTTP header compliance. (#32024)
- Fix 400 errors from nginx in response to Servo requests by implementing conformant albeit non-normative removal of whitespace from `Accept` and `Accept-Language` HTTP headers. (To match behaviour of Firefox, Safari, and Chrome) https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
- Provide `Host` header as REQUIRED by HTTP protocol https://www.rfc-editor.org/rfc/rfc9110#field.host
- Update tests.
Diffstat (limited to 'components')
-rw-r--r-- | components/net/http_loader.rs | 17 | ||||
-rw-r--r-- | components/net/tests/fetch.rs | 21 | ||||
-rw-r--r-- | components/net/tests/http_loader.rs | 24 | ||||
-rw-r--r-- | components/shared/net/quality.rs | 6 |
4 files changed, 44 insertions, 24 deletions
diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index 26d886375d4..3044526c1c9 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -177,7 +177,7 @@ pub fn set_default_accept_language(headers: &mut HeaderMap) { // TODO(eijebong): Change this once typed headers are done headers.insert( header::ACCEPT_LANGUAGE, - HeaderValue::from_static("en-US, en; q=0.5"), + HeaderValue::from_static("en-US,en;q=0.5"), ); } @@ -1225,7 +1225,20 @@ async fn http_network_or_cache_fetch( // Step 5.16 let current_url = http_request.current_url(); - http_request.headers.remove(header::HOST); + if !http_request.headers.contains_key(header::HOST) { + let host = if current_url.port().is_none() { + current_url.host_str().unwrap().to_string() + } else { + format!( + "{}:{}", + current_url.host_str().unwrap(), + current_url.port().unwrap() + ) + }; + http_request.headers.typed_insert(headers::Host::from( + host.parse::<http::uri::Authority>().unwrap(), + )); + } // unlike http_loader, we should not set the accept header // here, according to the fetch spec diff --git a/components/net/tests/fetch.rs b/components/net/tests/fetch.rs index c4409e7d6d5..7b9a140bcb4 100644 --- a/components/net/tests/fetch.rs +++ b/components/net/tests/fetch.rs @@ -1350,20 +1350,29 @@ fn test_fetch_with_devtools() { //Creating default headers for request let mut headers = HeaderMap::new(); - headers.insert( - header::ACCEPT_ENCODING, - HeaderValue::from_static("gzip, deflate, br"), - ); - headers.insert(header::ACCEPT, HeaderValue::from_static("*/*")); headers.insert( header::ACCEPT_LANGUAGE, - HeaderValue::from_static("en-US, en; q=0.5"), + HeaderValue::from_static("en-US,en;q=0.5"), ); headers.typed_insert::<UserAgent>(DEFAULT_USER_AGENT.parse().unwrap()); + let host = if url.port().is_none() { + url.host_str().unwrap().to_string() + } else { + format!("{}:{}", url.host_str().unwrap(), url.port().unwrap()) + }; + headers.typed_insert(headers::Host::from( + host.parse::<http::uri::Authority>().unwrap(), + )); + + headers.insert( + header::ACCEPT_ENCODING, + HeaderValue::from_static("gzip, deflate, br"), + ); + let httprequest = DevtoolsHttpRequest { url: url, method: Method::GET, diff --git a/components/net/tests/http_loader.rs b/components/net/tests/http_loader.rs index b60ac325d0a..6c551440a82 100644 --- a/components/net/tests/http_loader.rs +++ b/components/net/tests/http_loader.rs @@ -136,14 +136,12 @@ fn test_check_default_headers_loaded_in_every_request() { headers.insert( header::ACCEPT, - HeaderValue::from_static( - "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8", - ), + HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"), ); headers.insert( header::ACCEPT_LANGUAGE, - HeaderValue::from_static("en-US, en; q=0.5"), + HeaderValue::from_static("en-US,en;q=0.5"), ); headers.typed_insert::<UserAgent>(crate::DEFAULT_USER_AGENT.parse().unwrap()); @@ -268,25 +266,25 @@ fn test_request_and_response_data_with_network_messages() { //Creating default headers for request let mut headers = HeaderMap::new(); - headers.insert( - header::ACCEPT_ENCODING, - HeaderValue::from_static("gzip, deflate, br"), - ); + headers.insert(header::HOST, HeaderValue::from_static("bar.foo")); headers.insert( header::ACCEPT, - HeaderValue::from_static( - "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8", - ), + HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"), ); headers.insert( header::ACCEPT_LANGUAGE, - HeaderValue::from_static("en-US, en; q=0.5"), + HeaderValue::from_static("en-US,en;q=0.5"), ); headers.typed_insert::<UserAgent>(crate::DEFAULT_USER_AGENT.parse().unwrap()); + headers.insert( + header::ACCEPT_ENCODING, + HeaderValue::from_static("gzip, deflate, br"), + ); + let httprequest = DevtoolsHttpRequest { url: url, method: Method::GET, @@ -902,7 +900,7 @@ fn test_load_sets_default_accept_to_html_xhtml_xml_and_then_anything_else() { .unwrap() .to_str() .unwrap(), - "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8" + "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" ); *response.body_mut() = b"Yay!".to_vec().into(); }; diff --git a/components/shared/net/quality.rs b/components/shared/net/quality.rs index 095cd121bad..aedc2722340 100644 --- a/components/shared/net/quality.rs +++ b/components/shared/net/quality.rs @@ -59,9 +59,9 @@ where fmt::Display::fmt(&self.item, fmt)?; match self.quality.0 { 1000 => Ok(()), - 0 => fmt.write_str("; q=0"), + 0 => fmt.write_str(";q=0"), mut x => { - fmt.write_str("; q=0.")?; + fmt.write_str(";q=0.")?; let mut digits = *b"000"; digits[2] = (x % 10) as u8 + b'0'; x /= 10; @@ -81,7 +81,7 @@ pub fn quality_to_value(q: Vec<QualityItem<Mime>>) -> HeaderValue { &q.iter() .map(|q| q.to_string()) .collect::<Vec<String>>() - .join(", "), + .join(","), ) .unwrap() } |