gecko-dev/servo/components/net/about_loader.rs
Manish Goregaokar 1aea46318e servo: Merge #12535 - Use Result instead of panicking when the resource dir can't be found (from Manishearth:try-resource); r=KiChjang
<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #12520 (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because it's a refactoring

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: a39bd7db5ba26fa3c95e4bbbb5f1a358bc01965f
2016-07-21 04:08:49 -05:00

67 lines
2.7 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use file_loader;
use hyper::header::ContentType;
use hyper::http::RawStatus;
use hyper::mime::{Mime, SubLevel, TopLevel};
use mime_classifier::MimeClassifier;
use net_traits::ProgressMsg::Done;
use net_traits::response::HttpsState;
use net_traits::{LoadConsumer, LoadData, Metadata, NetworkError};
use resource_thread::{CancellationListener, send_error, start_sending_sniffed_opt};
use std::io;
use std::sync::Arc;
use url::Url;
use util::resource_files::resources_dir_path;
fn url_from_non_relative_scheme(load_data: &mut LoadData, filename: &str) -> io::Result<()> {
let mut path = try!(resources_dir_path());
path.push(filename);
assert!(path.exists());
load_data.url = Url::from_file_path(&*path).unwrap();
Ok(())
}
pub fn factory(mut load_data: LoadData,
start_chan: LoadConsumer,
classifier: Arc<MimeClassifier>,
cancel_listener: CancellationListener) {
let url = load_data.url.clone();
let res = match url.path() {
"blank" => {
let metadata = Metadata {
final_url: load_data.url,
content_type: Some(ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![]))),
charset: Some("utf-8".to_owned()),
headers: None,
status: Some(RawStatus(200, "OK".into())),
https_state: HttpsState::None,
referrer: None,
};
if let Ok(chan) = start_sending_sniffed_opt(start_chan,
metadata,
classifier,
&[],
load_data.context) {
let _ = chan.send(Done(Ok(())));
}
return
}
"crash" => panic!("Loading the about:crash URL."),
"failure" | "not-found" =>
url_from_non_relative_scheme(&mut load_data, &(url.path().to_owned() + ".html")),
"sslfail" => url_from_non_relative_scheme(&mut load_data, "badcert.html"),
_ => {
send_error(load_data.url, NetworkError::Internal("Unknown about: URL.".to_owned()), start_chan);
return
}
};
if res.is_ok() {
file_loader::factory(load_data, start_chan, classifier, cancel_listener)
} else {
send_error(load_data.url, NetworkError::Internal("Could not access resource folder".to_owned()), start_chan);
}
}