diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index dfae8624041..225751e2cd5 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -881,9 +881,16 @@ fn http_network_or_cache_fetch( // Step 5.9 match http_request.referrer { Referrer::NoReferrer => (), - Referrer::ReferrerUrl(ref http_request_referrer) => http_request - .headers - .typed_insert::(http_request_referrer.to_string().parse().unwrap()), + Referrer::ReferrerUrl(ref http_request_referrer) => { + if let Ok(referer) = http_request_referrer.to_string().parse::() { + http_request.headers.typed_insert(referer); + } else { + // This error should only happen in cases where hyper and rust-url disagree + // about how to parse a referer. + // https://github.com/servo/servo/issues/24175 + error!("Failed to parse {} as referer", http_request_referrer); + } + }, Referrer::Client => // it should be impossible for referrer to be anything else during fetching // https://fetch.spec.whatwg.org/#concept-request-referrer @@ -946,20 +953,25 @@ fn http_network_or_cache_fetch( // Step 5.16 let current_url = http_request.current_url(); - let host = Host::from( - format!( - "{}{}", - current_url.host_str().unwrap(), - current_url - .port() - .map(|v| format!(":{}", v)) - .unwrap_or("".into()) - ) - .parse::() - .unwrap(), - ); + if let Ok(host) = format!( + "{}{}", + current_url.host_str().unwrap(), + current_url + .port() + .map(|v| format!(":{}", v)) + .unwrap_or("".into()) + ) + .parse::() + .map(Host::from) + { + http_request.headers.typed_insert(host); + } else { + // This error should only happen in cases where hyper and rust-url disagree + // about how to parse an authority. + // https://github.com/servo/servo/issues/24175 + error!("Failed to parse {} as authority", current_url); + } - http_request.headers.typed_insert(host); // unlike http_loader, we should not set the accept header // here, according to the fetch spec set_default_accept_encoding(&mut http_request.headers); @@ -1484,7 +1496,7 @@ fn cors_preflight_fetch( headers.sort(); let headers = headers .iter() - .map(|name| HeaderName::from_str(name).unwrap()) + .filter_map(|name| HeaderName::from_str(name).ok()) .collect::>(); // Step 4