Skip to content
Permalink
Browse files

Make http_loader more robust against hyperium parse errors

  • Loading branch information...
asajeffrey committed Sep 9, 2019
1 parent 1aeb97b commit aeac382058edd6e8c3f98378b612e0ea2df8e1f8
Showing with 29 additions and 17 deletions.
  1. +29 −17 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::<Referer>(http_request_referrer.to_string().parse().unwrap()),
Referrer::ReferrerUrl(ref http_request_referrer) => {
if let Ok(referer) = http_request_referrer.to_string().parse::<Referer>() {
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::<Authority>()
.unwrap(),
);
if let Ok(host) = format!(
"{}{}",
current_url.host_str().unwrap(),
current_url
.port()
.map(|v| format!(":{}", v))
.unwrap_or("".into())
)
.parse::<Authority>()
.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::<Vec<HeaderName>>();

// Step 4

0 comments on commit aeac382

Please sign in to comment.
You can’t perform that action at this time.