Skip to content

Commit f051028

Browse files
author
bors-servo
committed
Auto merge of servo#9942 - jdm:load_error, r=ms2ger
Moving the error handling out of network loader Rebase of servo#8851. Fixes servo#8678. Fixes servo#9944. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9942) <!-- Reviewable:end -->
2 parents 89b276c + d888ed3 commit f051028

30 files changed

+396
-189
lines changed

components/gfx/font_cache_thread.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,21 @@ impl FontCache {
183183
ROUTER.add_route(data_receiver.to_opaque(), box move |message| {
184184
let response: ResponseAction = message.to().unwrap();
185185
match response {
186-
ResponseAction::HeadersAvailable(metadata) => {
187-
let is_response_valid =
188-
metadata.content_type.as_ref().map_or(false, |content_type| {
189-
let mime = &content_type.0;
190-
is_supported_font_type(&mime.0, &mime.1)
191-
});
192-
info!("{} font with MIME type {:?}",
186+
ResponseAction::HeadersAvailable(meta_result) => {
187+
let is_response_valid = match meta_result {
188+
Ok(ref metadata) => {
189+
metadata.content_type.as_ref().map_or(false, |content_type| {
190+
let mime = &content_type.0;
191+
is_supported_font_type(&mime.0, &mime.1)
192+
})
193+
}
194+
Err(_) => false,
195+
};
196+
197+
info!("{} font with MIME type {}",
193198
if is_response_valid { "Loading" } else { "Ignoring" },
194-
metadata.content_type);
199+
meta_result.map(|ref meta| format!("{:?}", meta.content_type))
200+
.unwrap_or(format!("<Network Error>")));
195201
*response_valid.lock().unwrap() = is_response_valid;
196202
}
197203
ResponseAction::DataAvailable(new_bytes) => {

components/net/about_loader.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ use hyper::mime::{Mime, SubLevel, TopLevel};
99
use mime_classifier::MIMEClassifier;
1010
use net_traits::ProgressMsg::Done;
1111
use net_traits::response::HttpsState;
12-
use net_traits::{LoadConsumer, LoadData, Metadata};
12+
use net_traits::{LoadConsumer, LoadData, Metadata, NetworkError};
1313
use resource_thread::{CancellationListener, send_error, start_sending_sniffed_opt};
1414
use std::sync::Arc;
1515
use url::Url;
1616
use util::resource_files::resources_dir_path;
1717

18+
fn url_from_non_relative_scheme(load_data: &mut LoadData, filename: &str) {
19+
let mut path = resources_dir_path();
20+
path.push(filename);
21+
assert!(path.exists());
22+
load_data.url = Url::from_file_path(&*path).unwrap();
23+
}
24+
1825
pub fn factory(mut load_data: LoadData,
1926
start_chan: LoadConsumer,
2027
classifier: Arc<MIMEClassifier>,
@@ -41,15 +48,11 @@ pub fn factory(mut load_data: LoadData,
4148
return
4249
}
4350
"crash" => panic!("Loading the about:crash URL."),
44-
"failure" | "not-found" => {
45-
let mut path = resources_dir_path();
46-
let file_name = non_relative_scheme_data.to_owned() + ".html";
47-
path.push(&file_name);
48-
assert!(path.exists());
49-
load_data.url = Url::from_file_path(&*path).unwrap();
50-
}
51+
"failure" | "not-found" =>
52+
url_from_non_relative_scheme(&mut load_data, &(non_relative_scheme_data.to_owned() + ".html")),
53+
"sslfail" => url_from_non_relative_scheme(&mut load_data, "badcert.html"),
5154
_ => {
52-
send_error(load_data.url, "Unknown about: URL.".to_owned(), start_chan);
55+
send_error(load_data.url, NetworkError::Internal("Unknown about: URL.".to_owned()), start_chan);
5356
return
5457
}
5558
};

components/net/chrome_loader.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
use file_loader;
6+
use mime_classifier::MIMEClassifier;
7+
use net_traits::{LoadConsumer, LoadData, NetworkError};
8+
use resource_thread::{CancellationListener, send_error};
9+
use std::path::Path;
10+
use std::sync::Arc;
11+
use url::Url;
12+
use util::resource_files::resources_dir_path;
13+
14+
pub fn resolve_chrome_url(url: &Url) -> Result<Url, ()> {
15+
assert_eq!(url.scheme, "chrome");
16+
// Skip the initial //
17+
let non_relative_scheme_data = &url.non_relative_scheme_data().unwrap()[2..];
18+
let relative_path = Path::new(non_relative_scheme_data);
19+
// Don't allow chrome URLs access to files outside of the resources directory.
20+
if non_relative_scheme_data.find("..").is_some() ||
21+
relative_path.is_absolute() ||
22+
relative_path.has_root() {
23+
return Err(());
24+
}
25+
26+
let mut path = resources_dir_path();
27+
path.push(non_relative_scheme_data);
28+
assert!(path.exists());
29+
return Ok(Url::from_file_path(&*path).unwrap());
30+
}
31+
32+
pub fn factory(mut load_data: LoadData,
33+
start_chan: LoadConsumer,
34+
classifier: Arc<MIMEClassifier>,
35+
cancel_listener: CancellationListener) {
36+
let file_url = match resolve_chrome_url(&load_data.url) {
37+
Ok(url) => url,
38+
Err(_) => {
39+
send_error(load_data.url,
40+
NetworkError::Internal("Invalid chrome URL.".to_owned()),
41+
start_chan);
42+
return;
43+
}
44+
};
45+
load_data.url = file_url;
46+
file_loader::factory(load_data, start_chan, classifier, cancel_listener)
47+
}

components/net/data_loader.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value};
66
use mime_classifier::MIMEClassifier;
77
use net_traits::LoadConsumer;
88
use net_traits::ProgressMsg::{Payload, Done};
9-
use net_traits::{LoadData, Metadata};
9+
use net_traits::{LoadData, Metadata, NetworkError};
1010
use resource_thread::{CancellationListener, send_error, start_sending_sniffed_opt};
1111
use rustc_serialize::base64::FromBase64;
1212
use std::sync::Arc;
@@ -110,7 +110,9 @@ pub fn load(load_data: LoadData,
110110
let _ = chan.send(Done(Ok(())));
111111
}
112112
},
113-
Err(DecodeError::InvalidDataUri) => send_error(url, "invalid data uri".to_owned(), start_chan),
114-
Err(DecodeError::NonBase64DataUri) => send_error(url, "non-base64 data uri".to_owned(), start_chan),
113+
Err(DecodeError::InvalidDataUri) =>
114+
send_error(url, NetworkError::Internal("invalid data uri".to_owned()), start_chan),
115+
Err(DecodeError::NonBase64DataUri) =>
116+
send_error(url, NetworkError::Internal("non-base64 data uri".to_owned()), start_chan),
115117
}
116118
}

components/net/file_loader.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use about_loader;
66
use mime_classifier::MIMEClassifier;
77
use mime_guess::guess_mime_type;
88
use net_traits::ProgressMsg::{Done, Payload};
9-
use net_traits::{LoadConsumer, LoadData, Metadata};
9+
use net_traits::{LoadConsumer, LoadData, Metadata, NetworkError};
1010
use resource_thread::{CancellationListener, ProgressSender};
1111
use resource_thread::{send_error, start_sending_sniffed_opt};
1212
use std::borrow::ToOwned;
@@ -50,7 +50,7 @@ fn read_all(reader: &mut File, progress_chan: &ProgressSender, cancel_listener:
5050
ReadStatus::EOF => return Ok(LoadResult::Finished),
5151
}
5252
}
53-
let _ = progress_chan.send(Done(Err("load cancelled".to_owned())));
53+
let _ = progress_chan.send(Done(Err(NetworkError::Internal("load cancelled".to_owned()))));
5454
Ok(LoadResult::Cancelled)
5555
}
5656

@@ -72,7 +72,7 @@ pub fn factory(load_data: LoadData,
7272
let file_path = match load_data.url.to_file_path() {
7373
Ok(file_path) => file_path,
7474
Err(_) => {
75-
send_error(load_data.url, "Could not parse path".to_owned(), senders);
75+
send_error(load_data.url, NetworkError::Internal("Could not parse path".to_owned()), senders);
7676
return;
7777
},
7878
};
@@ -92,7 +92,7 @@ pub fn factory(load_data: LoadData,
9292
if cancel_listener.is_cancelled() {
9393
if let Ok(progress_chan) = get_progress_chan(load_data, file_path,
9494
senders, classifier, &[]) {
95-
let _ = progress_chan.send(Done(Err("load cancelled".to_owned())));
95+
let _ = progress_chan.send(Done(Err(NetworkError::Internal("load cancelled".to_owned()))));
9696
}
9797
return;
9898
}
@@ -116,7 +116,7 @@ pub fn factory(load_data: LoadData,
116116
}
117117
}
118118
Err(e) => {
119-
send_error(load_data.url, e, senders);
119+
send_error(load_data.url, NetworkError::Internal(e), senders);
120120
}
121121
}
122122
});

0 commit comments

Comments
 (0)