Make http_loader more robust against hyperium parse errors

This commit is contained in:
Alan Jeffrey 2019-09-09 17:33:27 -05:00
parent 1aeb97b281
commit aeac382058

View file

@ -881,9 +881,16 @@ fn http_network_or_cache_fetch(
// Step 5.9 // Step 5.9
match http_request.referrer { match http_request.referrer {
Referrer::NoReferrer => (), Referrer::NoReferrer => (),
Referrer::ReferrerUrl(ref http_request_referrer) => http_request Referrer::ReferrerUrl(ref http_request_referrer) => {
.headers if let Ok(referer) = http_request_referrer.to_string().parse::<Referer>() {
.typed_insert::<Referer>(http_request_referrer.to_string().parse().unwrap()), 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 => Referrer::Client =>
// it should be impossible for referrer to be anything else during fetching // it should be impossible for referrer to be anything else during fetching
// https://fetch.spec.whatwg.org/#concept-request-referrer // https://fetch.spec.whatwg.org/#concept-request-referrer
@ -946,8 +953,7 @@ fn http_network_or_cache_fetch(
// Step 5.16 // Step 5.16
let current_url = http_request.current_url(); let current_url = http_request.current_url();
let host = Host::from( if let Ok(host) = format!(
format!(
"{}{}", "{}{}",
current_url.host_str().unwrap(), current_url.host_str().unwrap(),
current_url current_url
@ -956,10 +962,16 @@ fn http_network_or_cache_fetch(
.unwrap_or("".into()) .unwrap_or("".into())
) )
.parse::<Authority>() .parse::<Authority>()
.unwrap(), .map(Host::from)
); {
http_request.headers.typed_insert(host); 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);
}
// unlike http_loader, we should not set the accept header // unlike http_loader, we should not set the accept header
// here, according to the fetch spec // here, according to the fetch spec
set_default_accept_encoding(&mut http_request.headers); set_default_accept_encoding(&mut http_request.headers);
@ -1484,7 +1496,7 @@ fn cors_preflight_fetch(
headers.sort(); headers.sort();
let headers = headers let headers = headers
.iter() .iter()
.map(|name| HeaderName::from_str(name).unwrap()) .filter_map(|name| HeaderName::from_str(name).ok())
.collect::<Vec<HeaderName>>(); .collect::<Vec<HeaderName>>();
// Step 4 // Step 4