servo: Merge #7654 - Move HSTS/CookieStorage to Arc<RwLock> from Ipc (from samfoo:hsts-cookie-arc); r=jdm

servo/servo#7421

Source-Repo: https://github.com/servo/servo
Source-Revision: 7f2d81933a6158926ff6d03acbc05f191cd5adb3
This commit is contained in:
Sam Gibson 2015-09-19 02:01:06 -06:00
Родитель 20434e217e
Коммит cbd0551cde
6 изменённых файлов: 252 добавлений и 189 удалений

Просмотреть файл

@ -3,26 +3,27 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cookie;
use cookie_storage::CookieStorage;
use devtools_traits::{ChromeToDevtoolsControlMsg, DevtoolsControlMsg, NetworkEvent};
use file_loader;
use flate2::read::{DeflateDecoder, GzDecoder};
use hsts::secure_url;
use hsts::{secure_url, HSTSList, HSTSEntry};
use hyper::Error as HttpError;
use hyper::client::{Request, Response, Pool};
use hyper::header::{AcceptEncoding, Accept, ContentLength, ContentType, Host};
use hyper::header::{Location, qitem, StrictTransportSecurity, UserAgent};
use hyper::header::{Quality, QualityItem, Headers, ContentEncoding, Encoding};
use hyper::header::{Location, qitem, StrictTransportSecurity, UserAgent, SetCookie};
use hyper::header::{Quality, QualityItem, Headers, ContentEncoding, Encoding, Header};
use hyper::http::RawStatus;
use hyper::method::Method;
use hyper::mime::{Mime, TopLevel, SubLevel};
use hyper::net::{Fresh, HttpsConnector, Openssl};
use hyper::status::{StatusCode, StatusClass};
use ipc_channel::ipc::{self, IpcSender};
use log;
use mime_classifier::MIMEClassifier;
use net_traits::ProgressMsg::{Payload, Done};
use net_traits::hosts::replace_hosts;
use net_traits::{ControlMsg, CookieSource, LoadData, Metadata, LoadConsumer, IncludeSubdomains};
use net_traits::{CookieSource, LoadData, Metadata, LoadConsumer, IncludeSubdomains};
use openssl::ssl::{SslContext, SslMethod, SSL_VERIFY_PEER};
use resource_task::{start_sending_opt, start_sending_sniffed_opt};
use std::borrow::ToOwned;
@ -30,8 +31,8 @@ use std::boxed::FnBox;
use std::collections::HashSet;
use std::error::Error;
use std::io::{self, Read, Write};
use std::sync::Arc;
use std::sync::mpsc::{Sender, channel};
use std::sync::mpsc::Sender;
use std::sync::{Arc, RwLock};
use url::{Url, UrlParser};
use util::resource_files::resources_dir_path;
use util::task::spawn_named;
@ -50,13 +51,21 @@ pub fn create_http_connector() -> Arc<Pool<Connector>> {
Arc::new(Pool::with_connector(Default::default(), connector))
}
pub fn factory(resource_mgr_chan: IpcSender<ControlMsg>,
pub fn factory(hsts_list: Arc<RwLock<HSTSList>>,
cookie_jar: Arc<RwLock<CookieStorage>>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
connector: Arc<Pool<Connector>>)
-> Box<FnBox(LoadData, LoadConsumer, Arc<MIMEClassifier>, String) + Send> {
box move |load_data: LoadData, senders, classifier, user_agent| {
spawn_named(format!("http_loader for {}", load_data.url.serialize()), move || {
load_for_consumer(load_data, senders, classifier, connector, resource_mgr_chan, devtools_chan, user_agent)
load_for_consumer(load_data,
senders,
classifier,
connector,
hsts_list,
cookie_jar,
devtools_chan,
user_agent)
})
}
}
@ -98,14 +107,15 @@ fn load_for_consumer(load_data: LoadData,
start_chan: LoadConsumer,
classifier: Arc<MIMEClassifier>,
connector: Arc<Pool<Connector>>,
resource_mgr_chan: IpcSender<ControlMsg>,
hsts_list: Arc<RwLock<HSTSList>>,
cookie_jar: Arc<RwLock<CookieStorage>>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
user_agent: String) {
let factory = NetworkHttpRequestFactory {
connector: connector,
};
match load::<WrappedHttpRequest>(load_data, resource_mgr_chan, devtools_chan, &factory, user_agent) {
match load::<WrappedHttpRequest>(load_data, hsts_list, cookie_jar, devtools_chan, &factory, user_agent) {
Err(LoadError::UnsupportedScheme(url)) => {
let s = format!("{} request, but we don't support that scheme", &*url.scheme);
send_error(url, s, start_chan)
@ -313,61 +323,66 @@ fn set_default_accept(headers: &mut Headers) {
}
}
fn set_request_cookies(url: Url, headers: &mut Headers, resource_mgr_chan: &IpcSender<ControlMsg>) {
let (tx, rx) = ipc::channel().unwrap();
resource_mgr_chan.send(ControlMsg::GetCookiesForUrl(url, tx, CookieSource::HTTP)).unwrap();
if let Some(cookie_list) = rx.recv().unwrap() {
fn set_request_cookies(url: Url, headers: &mut Headers, cookie_jar: &Arc<RwLock<CookieStorage>>) {
let mut cookie_jar = cookie_jar.write().unwrap();
if let Some(cookie_list) = cookie_jar.cookies_for_url(&url, CookieSource::HTTP) {
let mut v = Vec::new();
v.push(cookie_list.into_bytes());
headers.set_raw("Cookie".to_owned(), v);
}
}
fn set_cookies_from_response(url: Url, response: &HttpResponse, resource_mgr_chan: &IpcSender<ControlMsg>) {
if let Some(cookies) = response.headers().get_raw("set-cookie") {
for cookie in cookies.iter() {
if let Ok(cookies) = String::from_utf8(cookie.clone()) {
resource_mgr_chan.send(ControlMsg::SetCookiesForUrl(url.clone(),
cookies,
CookieSource::HTTP)).unwrap();
fn set_cookie_for_url(cookie_jar: &Arc<RwLock<CookieStorage>>,
request: Url,
cookie_val: String) {
let mut cookie_jar = cookie_jar.write().unwrap();
let source = CookieSource::HTTP;
let header = Header::parse_header(&[cookie_val.into_bytes()]);
if let Ok(SetCookie(cookies)) = header {
for bare_cookie in cookies {
if let Some(cookie) = cookie::Cookie::new_wrapped(bare_cookie, &request, source) {
cookie_jar.push(cookie, source);
}
}
}
}
fn request_must_be_secured(url: &Url, resource_mgr_chan: &IpcSender<ControlMsg>) -> bool {
let (tx, rx) = ipc::channel().unwrap();
resource_mgr_chan.send(
ControlMsg::GetHostMustBeSecured(url.domain().unwrap().to_string(), tx)
).unwrap();
rx.recv().unwrap()
fn set_cookies_from_response(url: Url, response: &HttpResponse, cookie_jar: &Arc<RwLock<CookieStorage>>) {
if let Some(cookies) = response.headers().get_raw("set-cookie") {
for cookie in cookies.iter() {
if let Ok(cookie_value) = String::from_utf8(cookie.clone()) {
set_cookie_for_url(&cookie_jar,
url.clone(),
cookie_value);
}
}
}
}
fn update_sts_list_from_response(url: &Url, response: &HttpResponse, resource_mgr_chan: &IpcSender<ControlMsg>) {
fn update_sts_list_from_response(url: &Url, response: &HttpResponse, hsts_list: &Arc<RwLock<HSTSList>>) {
if url.scheme != "https" {
return;
}
if let Some(header) = response.headers().get::<StrictTransportSecurity>() {
if let Some(host) = url.domain() {
info!("adding host {} to the strict transport security list", host);
info!("- max-age {}", header.max_age);
let mut hsts_list = hsts_list.write().unwrap();
let include_subdomains = if header.include_subdomains {
info!("- includeSubdomains");
IncludeSubdomains::Included
} else {
IncludeSubdomains::NotIncluded
};
let msg = ControlMsg::SetHSTSEntryForHost(
host.to_string(),
include_subdomains,
header.max_age
);
if let Some(entry) = HSTSEntry::new(host.to_string(), include_subdomains, Some(header.max_age)) {
info!("adding host {} to the strict transport security list", host);
info!("- max-age {}", header.max_age);
if header.include_subdomains {
info!("- includeSubdomains");
}
resource_mgr_chan.send(msg).unwrap();
hsts_list.push(entry);
}
}
}
}
@ -451,8 +466,16 @@ fn send_response_to_devtools(devtools_chan: Option<Sender<DevtoolsControlMsg>>,
}
}
fn request_must_be_secured(url: &Url, hsts_list: &Arc<RwLock<HSTSList>>) -> bool {
match url.domain() {
Some(domain) => hsts_list.read().unwrap().is_host_secure(domain),
None => false
}
}
pub fn load<A>(load_data: LoadData,
resource_mgr_chan: IpcSender<ControlMsg>,
hsts_list: Arc<RwLock<HSTSList>>,
cookie_jar: Arc<RwLock<CookieStorage>>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
request_factory: &HttpRequestFactory<R=A>,
user_agent: String)
@ -484,7 +507,7 @@ pub fn load<A>(load_data: LoadData,
loop {
iters = iters + 1;
if &*url.scheme == "http" && request_must_be_secured(&url, &resource_mgr_chan) {
if &*url.scheme == "http" && request_must_be_secured(&url, &hsts_list) {
info!("{} is in the strict transport security list, requesting secure host", url);
url = secure_url(&url);
}
@ -523,7 +546,7 @@ pub fn load<A>(load_data: LoadData,
set_default_accept(&mut request_headers);
set_default_accept_encoding(&mut request_headers);
set_request_cookies(doc_url.clone(), &mut request_headers, &resource_mgr_chan);
set_request_cookies(doc_url.clone(), &mut request_headers, &cookie_jar);
let request_id = uuid::Uuid::new_v4().to_simple_string();
@ -601,8 +624,8 @@ pub fn load<A>(load_data: LoadData,
}
}
set_cookies_from_response(doc_url.clone(), &response, &resource_mgr_chan);
update_sts_list_from_response(&url, &response, &resource_mgr_chan);
set_cookies_from_response(doc_url.clone(), &response, &cookie_jar);
update_sts_list_from_response(&url, &response, &hsts_list);
// --- Loop if there's a redirect
if response.status().class() == StatusClass::Redirection {

Просмотреть файл

@ -8,30 +8,25 @@ use about_loader;
use cookie;
use cookie_storage::CookieStorage;
use data_loader;
use file_loader;
use http_loader::{self, create_http_connector, Connector};
use mime_classifier::{ApacheBugFlag, MIMEClassifier, NoSniffFlag};
use net_traits::ProgressMsg::Done;
use net_traits::{ControlMsg, LoadData, LoadResponse, LoadConsumer, CookieSource};
use net_traits::{Metadata, ProgressMsg, ResourceTask, AsyncResponseTarget, ResponseAction};
use url::Url;
use util::opts;
use util::task::spawn_named;
use hsts::{HSTSList, HSTSEntry, preload_hsts_domains};
use devtools_traits::{DevtoolsControlMsg};
use file_loader;
use hsts::{HSTSList, preload_hsts_domains};
use http_loader::{self, create_http_connector, Connector};
use hyper::client::pool::Pool;
use hyper::header::{ContentType, Header, SetCookie};
use hyper::mime::{Mime, TopLevel, SubLevel};
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use mime_classifier::{ApacheBugFlag, MIMEClassifier, NoSniffFlag};
use net_traits::ProgressMsg::Done;
use net_traits::{ControlMsg, LoadData, LoadResponse, LoadConsumer, CookieSource};
use net_traits::{Metadata, ProgressMsg, ResourceTask, AsyncResponseTarget, ResponseAction};
use std::borrow::ToOwned;
use std::boxed::FnBox;
use std::sync::{Arc};
use std::sync::mpsc::{channel, Sender};
use std::sync::{Arc, RwLock};
use url::Url;
use util::opts;
use util::task::spawn_named;
pub enum ProgressSender {
Channel(IpcSender<ProgressMsg>),
@ -147,10 +142,9 @@ pub fn new_resource_task(user_agent: String,
};
let (setup_chan, setup_port) = ipc::channel().unwrap();
let setup_chan_clone = setup_chan.clone();
spawn_named("ResourceManager".to_owned(), move || {
let resource_manager = ResourceManager::new(
user_agent, setup_chan_clone, hsts_preload, devtools_chan
user_agent, hsts_preload, devtools_chan
);
let mut channel_manager = ResourceChannelManager {
@ -179,15 +173,9 @@ impl ResourceChannelManager {
self.resource_manager.set_cookies_for_url(request, cookie_list, source)
}
ControlMsg::GetCookiesForUrl(url, consumer, source) => {
consumer.send(self.resource_manager.cookie_storage.cookies_for_url(&url, source)).unwrap();
}
ControlMsg::SetHSTSEntryForHost(host, include_subdomains, max_age) => {
if let Some(entry) = HSTSEntry::new(host, include_subdomains, Some(max_age)) {
self.resource_manager.add_hsts_entry(entry)
}
}
ControlMsg::GetHostMustBeSecured(host, consumer) => {
consumer.send(self.resource_manager.is_host_sts(&*host)).unwrap();
let ref cookie_jar = self.resource_manager.cookie_storage;
let mut cookie_jar = cookie_jar.write().unwrap();
consumer.send(cookie_jar.cookies_for_url(&url, source)).unwrap();
}
ControlMsg::Exit => {
break
@ -199,26 +187,23 @@ impl ResourceChannelManager {
pub struct ResourceManager {
user_agent: String,
cookie_storage: CookieStorage,
resource_task: IpcSender<ControlMsg>,
cookie_storage: Arc<RwLock<CookieStorage>>,
mime_classifier: Arc<MIMEClassifier>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
hsts_list: HSTSList,
hsts_list: Arc<RwLock<HSTSList>>,
connector: Arc<Pool<Connector>>,
}
impl ResourceManager {
pub fn new(user_agent: String,
resource_task: IpcSender<ControlMsg>,
hsts_list: HSTSList,
devtools_channel: Option<Sender<DevtoolsControlMsg>>) -> ResourceManager {
ResourceManager {
user_agent: user_agent,
cookie_storage: CookieStorage::new(),
resource_task: resource_task,
cookie_storage: Arc::new(RwLock::new(CookieStorage::new())),
mime_classifier: Arc::new(MIMEClassifier::new()),
devtools_chan: devtools_channel,
hsts_list: hsts_list,
hsts_list: Arc::new(RwLock::new(hsts_list)),
connector: create_http_connector(),
}
}
@ -230,20 +215,14 @@ impl ResourceManager {
if let Ok(SetCookie(cookies)) = header {
for bare_cookie in cookies {
if let Some(cookie) = cookie::Cookie::new_wrapped(bare_cookie, &request, source) {
self.cookie_storage.push(cookie, source);
let ref cookie_jar = self.cookie_storage;
let mut cookie_jar = cookie_jar.write().unwrap();
cookie_jar.push(cookie, source);
}
}
}
}
pub fn add_hsts_entry(&mut self, entry: HSTSEntry) {
self.hsts_list.push(entry);
}
pub fn is_host_sts(&self, host: &str) -> bool {
self.hsts_list.is_host_secure(host)
}
fn load(&mut self, load_data: LoadData, consumer: LoadConsumer) {
fn from_factory(factory: fn(LoadData, LoadConsumer, Arc<MIMEClassifier>))
@ -256,7 +235,8 @@ impl ResourceManager {
let loader = match &*load_data.url.scheme {
"file" => from_factory(file_loader::factory),
"http" | "https" | "view-source" =>
http_loader::factory(self.resource_task.clone(),
http_loader::factory(self.hsts_list.clone(),
self.cookie_storage.clone(),
self.devtools_chan.clone(),
self.connector.clone()),
"data" => from_factory(data_loader::factory),

Просмотреть файл

@ -159,9 +159,6 @@ pub enum ControlMsg {
SetCookiesForUrl(Url, String, CookieSource),
/// Retrieve the stored cookies for a given URL
GetCookiesForUrl(Url, IpcSender<Option<String>>, CookieSource),
/// Store a domain's STS information
SetHSTSEntryForHost(String, IncludeSubdomains, u64),
GetHostMustBeSecured(String, IpcSender<bool>),
Exit
}

Просмотреть файл

@ -2,35 +2,12 @@
* 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 ipc_channel::ipc;
use net::hsts::{HSTSList, HSTSEntry};
use net::hsts::{secure_url, preload_hsts_domains};
use net::resource_task::ResourceManager;
use net_traits::IncludeSubdomains;
use std::sync::mpsc::channel;
use time;
use url::Url;
#[test]
fn test_add_hsts_entry_to_resource_manager_adds_an_hsts_entry() {
let list = HSTSList {
entries: Vec::new()
};
let (tx, _) = ipc::channel().unwrap();
let mut manager = ResourceManager::new("".to_owned(), tx, list, None);
let entry = HSTSEntry::new(
"mozilla.org".to_string(), IncludeSubdomains::NotIncluded, None
);
assert!(!manager.is_host_sts("mozilla.org"));
manager.add_hsts_entry(entry.unwrap());
assert!(manager.is_host_sts("mozilla.org"))
}
#[test]
fn test_hsts_entry_is_not_expired_when_it_has_no_timestamp() {
let entry = HSTSEntry {

Просмотреть файл

@ -2,19 +2,21 @@
* 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 cookie_rs;
use flate2::Compression;
use flate2::write::{GzEncoder, DeflateEncoder};
use hyper::header::{Headers, Location, ContentLength};
use hyper::http::RawStatus;
use hyper::method::Method;
use hyper::status::StatusCode;
use ipc_channel::ipc;
use net::cookie::Cookie;
use net::cookie_storage::CookieStorage;
use net::hsts::{HSTSList};
use net::http_loader::{load, LoadError, HttpRequestFactory, HttpRequest, HttpResponse};
use net::resource_task::new_resource_task;
use net_traits::LoadData;
use net_traits::{ResourceTask, ControlMsg, CookieSource};
use net_traits::{LoadData, CookieSource};
use std::borrow::Cow;
use std::io::{self, Write, Read, Cursor};
use std::sync::{Arc, RwLock};
use url::Url;
const DEFAULT_USER_AGENT: &'static str = "Test-agent";
@ -174,12 +176,12 @@ impl HttpRequestFactory for AssertMustHaveHeadersRequestFactory {
}
}
fn assert_cookie_for_domain(resource_mgr: &ResourceTask, domain: &str, cookie: &str) {
let (tx, rx) = ipc::channel().unwrap();
resource_mgr.send(ControlMsg::GetCookiesForUrl(Url::parse(&*domain).unwrap(),
tx,
CookieSource::HTTP)).unwrap();
if let Some(cookie_list) = rx.recv().unwrap() {
fn assert_cookie_for_domain(cookie_jar: Arc<RwLock<CookieStorage>>, domain: &str, cookie: &str) {
let mut cookie_jar = cookie_jar.write().unwrap();
let url = Url::parse(&*domain).unwrap();
let cookies = cookie_jar.cookies_for_url(&url, CookieSource::HTTP);
if let Some(cookie_list) = cookies {
assert_eq!(cookie.to_string(), cookie_list);
} else {
assert_eq!(cookie.len(), 0);
@ -213,7 +215,9 @@ impl HttpRequest for AssertMustHaveBodyRequest {
#[test]
fn test_load_when_request_is_not_get_or_head_and_there_is_no_body_content_length_should_be_set_to_0() {
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let mut load_data = LoadData::new(url.clone(), None);
load_data.data = None;
@ -223,7 +227,7 @@ fn test_load_when_request_is_not_get_or_head_and_there_is_no_body_content_length
content_length.set_raw("Content-Length".to_owned(), vec![<[_]>::to_vec("0".as_bytes())]);
let _ = load::<AssertRequestMustHaveHeaders>(
load_data.clone(), resource_mgr, None,
load_data.clone(), hsts_list, cookie_jar, None,
&AssertMustHaveHeadersRequestFactory {
expected_headers: content_length,
body: <[_]>::to_vec(&[])
@ -249,11 +253,13 @@ fn test_load_when_redirecting_from_a_post_should_rewrite_next_request_as_get() {
}
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let mut load_data = LoadData::new(url.clone(), None);
load_data.method = Method::Post;
let _ = load::<MockRequest>(load_data, resource_mgr, None, &Factory, DEFAULT_USER_AGENT.to_string());
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let _ = load::<MockRequest>(load_data, hsts_list, cookie_jar, None, &Factory, DEFAULT_USER_AGENT.to_string());
}
#[test]
@ -275,10 +281,13 @@ fn test_load_should_decode_the_response_as_deflate_when_response_headers_have_co
}
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let load_data = LoadData::new(url.clone(), None);
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let mut response = load::<MockRequest>(
load_data, resource_mgr.clone(), None,
load_data, hsts_list, cookie_jar, None,
&Factory,
DEFAULT_USER_AGENT.to_string())
.unwrap();
@ -305,11 +314,14 @@ fn test_load_should_decode_the_response_as_gzip_when_response_headers_have_conte
}
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let load_data = LoadData::new(url.clone(), None);
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let mut response = load::<MockRequest>(
load_data,
resource_mgr.clone(),
hsts_list,
cookie_jar,
None, &Factory,
DEFAULT_USER_AGENT.to_string())
.unwrap();
@ -344,13 +356,14 @@ fn test_load_doesnt_send_request_body_on_any_redirect() {
}
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let mut load_data = LoadData::new(url.clone(), None);
load_data.data = Some(<[_]>::to_vec("Body on POST!".as_bytes()));
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let _ = load::<AssertMustHaveBodyRequest>(
load_data, resource_mgr,
load_data, hsts_list, cookie_jar,
None,
&Factory,
DEFAULT_USER_AGENT.to_string());
@ -372,16 +385,20 @@ fn test_load_doesnt_add_host_to_sts_list_when_url_is_http_even_if_sts_headers_ar
}
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let load_data = LoadData::new(url.clone(), None);
let _ = load::<MockRequest>(load_data, resource_mgr.clone(), None, &Factory, DEFAULT_USER_AGENT.to_string());
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let (tx, rx) = ipc::channel().unwrap();
resource_mgr.send(ControlMsg::GetHostMustBeSecured("mozilla.com".to_string(), tx)).unwrap();
let _ = load::<MockRequest>(load_data,
hsts_list.clone(),
cookie_jar,
None,
&Factory,
DEFAULT_USER_AGENT.to_string());
assert_eq!(rx.recv().unwrap(), false);
assert_eq!(hsts_list.read().unwrap().is_host_secure("mozilla.com"), false);
}
#[test]
@ -400,16 +417,20 @@ fn test_load_adds_host_to_sts_list_when_url_is_https_and_sts_headers_are_present
}
let url = Url::parse("https://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let load_data = LoadData::new(url.clone(), None);
let _ = load::<MockRequest>(load_data, resource_mgr.clone(), None, &Factory, DEFAULT_USER_AGENT.to_string());
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let (tx, rx) = ipc::channel().unwrap();
resource_mgr.send(ControlMsg::GetHostMustBeSecured("mozilla.com".to_string(), tx)).unwrap();
let _ = load::<MockRequest>(load_data,
hsts_list.clone(),
cookie_jar,
None,
&Factory,
DEFAULT_USER_AGENT.to_string());
assert!(rx.recv().unwrap());
assert!(hsts_list.read().unwrap().is_host_secure("mozilla.com"));
}
#[test]
@ -428,32 +449,50 @@ fn test_load_sets_cookies_in_the_resource_manager_when_it_get_set_cookie_header_
}
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
assert_cookie_for_domain(&resource_mgr, "http://mozilla.com", "");
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
assert_cookie_for_domain(cookie_jar.clone(), "http://mozilla.com", "");
let load_data = LoadData::new(url.clone(), None);
let _ = load::<MockRequest>(load_data, resource_mgr.clone(), None, &Factory, DEFAULT_USER_AGENT.to_string());
let _ = load::<MockRequest>(load_data,
hsts_list,
cookie_jar.clone(),
None,
&Factory,
DEFAULT_USER_AGENT.to_string());
assert_cookie_for_domain(&resource_mgr, "http://mozilla.com", "mozillaIs=theBest");
assert_cookie_for_domain(cookie_jar.clone(), "http://mozilla.com", "mozillaIs=theBest");
}
#[test]
fn test_load_sets_requests_cookies_header_for_url_by_getting_cookies_from_the_resource_manager() {
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
resource_mgr.send(ControlMsg::SetCookiesForUrl(Url::parse("http://mozilla.com").unwrap(),
"mozillaIs=theBest".to_string(),
CookieSource::HTTP)).unwrap();
let mut load_data = LoadData::new(url.clone(), None);
load_data.data = Some(<[_]>::to_vec("Yay!".as_bytes()));
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
{
let mut cookie_jar = cookie_jar.write().unwrap();
let cookie_url = url.clone();
let cookie = Cookie::new_wrapped(
cookie_rs::Cookie::parse("mozillaIs=theBest").unwrap(),
&cookie_url,
CookieSource::HTTP
).unwrap();
cookie_jar.push(cookie, CookieSource::HTTP);
}
let mut cookie = Headers::new();
cookie.set_raw("Cookie".to_owned(), vec![<[_]>::to_vec("mozillaIs=theBest".as_bytes())]);
let _ = load::<AssertRequestMustHaveHeaders>(
load_data.clone(), resource_mgr, None,
load_data.clone(), hsts_list, cookie_jar, None,
&AssertMustHaveHeadersRequestFactory {
expected_headers: cookie,
body: <[_]>::to_vec(&*load_data.data.unwrap())
@ -465,7 +504,6 @@ fn test_load_sets_content_length_to_length_of_request_body() {
let content = "This is a request body";
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let mut load_data = LoadData::new(url.clone(), None);
load_data.data = Some(<[_]>::to_vec(content.as_bytes()));
@ -475,8 +513,11 @@ fn test_load_sets_content_length_to_length_of_request_body() {
"Content-Length".to_owned(), vec![<[_]>::to_vec(&*content_len.as_bytes())]
);
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let _ = load::<AssertRequestMustHaveHeaders>(
load_data.clone(), resource_mgr, None,
load_data.clone(), hsts_list, cookie_jar, None,
&AssertMustHaveHeadersRequestFactory {
expected_headers: content_len_headers,
body: <[_]>::to_vec(&*load_data.data.unwrap())
@ -489,15 +530,21 @@ fn test_load_uses_explicit_accept_from_headers_in_load_data() {
accept_headers.set_raw("Accept".to_owned(), vec![b"text/html".to_vec()]);
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let mut load_data = LoadData::new(url.clone(), None);
load_data.data = Some(<[_]>::to_vec("Yay!".as_bytes()));
load_data.headers.set_raw("Accept".to_owned(), vec![b"text/html".to_vec()]);
let _ = load::<AssertRequestMustHaveHeaders>(load_data, resource_mgr, None, &AssertMustHaveHeadersRequestFactory {
expected_headers: accept_headers,
body: <[_]>::to_vec("Yay!".as_bytes())
}, DEFAULT_USER_AGENT.to_string());
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let _ = load::<AssertRequestMustHaveHeaders>(load_data,
hsts_list,
cookie_jar,
None,
&AssertMustHaveHeadersRequestFactory {
expected_headers: accept_headers,
body: <[_]>::to_vec("Yay!".as_bytes())
}, DEFAULT_USER_AGENT.to_string());
}
#[test]
@ -508,14 +555,20 @@ fn test_load_sets_default_accept_to_html_xhtml_xml_and_then_anything_else() {
);
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let mut load_data = LoadData::new(url.clone(), None);
load_data.data = Some(<[_]>::to_vec("Yay!".as_bytes()));
let _ = load::<AssertRequestMustHaveHeaders>(load_data, resource_mgr, None, &AssertMustHaveHeadersRequestFactory {
expected_headers: accept_headers,
body: <[_]>::to_vec("Yay!".as_bytes())
}, DEFAULT_USER_AGENT.to_string());
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let _ = load::<AssertRequestMustHaveHeaders>(load_data,
hsts_list,
cookie_jar,
None,
&AssertMustHaveHeadersRequestFactory {
expected_headers: accept_headers,
body: <[_]>::to_vec("Yay!".as_bytes())
}, DEFAULT_USER_AGENT.to_string());
}
#[test]
@ -524,15 +577,21 @@ fn test_load_uses_explicit_accept_encoding_from_load_data_headers() {
accept_encoding_headers.set_raw("Accept-Encoding".to_owned(), vec![b"chunked".to_vec()]);
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let mut load_data = LoadData::new(url.clone(), None);
load_data.data = Some(<[_]>::to_vec("Yay!".as_bytes()));
load_data.headers.set_raw("Accept-Encoding".to_owned(), vec![b"chunked".to_vec()]);
let _ = load::<AssertRequestMustHaveHeaders>(load_data, resource_mgr, None, &AssertMustHaveHeadersRequestFactory {
expected_headers: accept_encoding_headers,
body: <[_]>::to_vec("Yay!".as_bytes())
}, DEFAULT_USER_AGENT.to_string());
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let _ = load::<AssertRequestMustHaveHeaders>(load_data,
hsts_list,
cookie_jar,
None,
&AssertMustHaveHeadersRequestFactory {
expected_headers: accept_encoding_headers,
body: <[_]>::to_vec("Yay!".as_bytes())
}, DEFAULT_USER_AGENT.to_string());
}
#[test]
@ -541,14 +600,20 @@ fn test_load_sets_default_accept_encoding_to_gzip_and_deflate() {
accept_encoding_headers.set_raw("Accept-Encoding".to_owned(), vec![b"gzip, deflate".to_vec()]);
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let mut load_data = LoadData::new(url.clone(), None);
load_data.data = Some(<[_]>::to_vec("Yay!".as_bytes()));
let _ = load::<AssertRequestMustHaveHeaders>(load_data, resource_mgr, None, &AssertMustHaveHeadersRequestFactory {
expected_headers: accept_encoding_headers,
body: <[_]>::to_vec("Yay!".as_bytes())
}, DEFAULT_USER_AGENT.to_string());
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
let _ = load::<AssertRequestMustHaveHeaders>(load_data,
hsts_list,
cookie_jar,
None,
&AssertMustHaveHeadersRequestFactory {
expected_headers: accept_encoding_headers,
body: <[_]>::to_vec("Yay!".as_bytes())
}, DEFAULT_USER_AGENT.to_string());
}
#[test]
@ -570,10 +635,12 @@ fn test_load_errors_when_there_a_redirect_loop() {
}
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let load_data = LoadData::new(url.clone(), None);
match load::<MockRequest>(load_data, resource_mgr, None, &Factory, DEFAULT_USER_AGENT.to_string()) {
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
match load::<MockRequest>(load_data, hsts_list, cookie_jar, None, &Factory, DEFAULT_USER_AGENT.to_string()) {
Err(LoadError::InvalidRedirect(_, msg)) => {
assert_eq!(msg, "redirect loop");
},
@ -598,10 +665,12 @@ fn test_load_errors_when_there_is_too_many_redirects() {
}
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let load_data = LoadData::new(url.clone(), None);
match load::<MockRequest>(load_data, resource_mgr, None, &Factory, DEFAULT_USER_AGENT.to_string()) {
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
match load::<MockRequest>(load_data, hsts_list, cookie_jar, None, &Factory, DEFAULT_USER_AGENT.to_string()) {
Err(LoadError::MaxRedirects(url)) => {
assert_eq!(url.domain().unwrap(), "mozilla.com")
},
@ -634,10 +703,12 @@ fn test_load_follows_a_redirect() {
}
let url = Url::parse("http://mozilla.com").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let load_data = LoadData::new(url.clone(), None);
match load::<MockRequest>(load_data, resource_mgr, None, &Factory, DEFAULT_USER_AGENT.to_string()) {
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
match load::<MockRequest>(load_data, hsts_list, cookie_jar, None, &Factory, DEFAULT_USER_AGENT.to_string()) {
Err(e) => panic!("expected to follow a redirect {:?}", e),
Ok(mut lr) => {
let response = read_response(&mut lr);
@ -659,10 +730,17 @@ impl HttpRequestFactory for DontConnectFactory {
#[test]
fn test_load_errors_when_scheme_is_not_http_or_https() {
let url = Url::parse("ftp://not-supported").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let load_data = LoadData::new(url.clone(), None);
match load::<MockRequest>(load_data, resource_mgr, None, &DontConnectFactory, DEFAULT_USER_AGENT.to_string()) {
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
match load::<MockRequest>(load_data,
hsts_list,
cookie_jar,
None,
&DontConnectFactory,
DEFAULT_USER_AGENT.to_string()) {
Err(LoadError::UnsupportedScheme(_)) => {}
_ => panic!("expected ftp scheme to be unsupported")
}
@ -671,10 +749,17 @@ fn test_load_errors_when_scheme_is_not_http_or_https() {
#[test]
fn test_load_errors_when_viewing_source_and_inner_url_scheme_is_not_http_or_https() {
let url = Url::parse("view-source:ftp://not-supported").unwrap();
let resource_mgr = new_resource_task(DEFAULT_USER_AGENT.to_string(), None);
let load_data = LoadData::new(url.clone(), None);
match load::<MockRequest>(load_data, resource_mgr, None, &DontConnectFactory, DEFAULT_USER_AGENT.to_string()) {
let hsts_list = Arc::new(RwLock::new(HSTSList::new()));
let cookie_jar = Arc::new(RwLock::new(CookieStorage::new()));
match load::<MockRequest>(load_data,
hsts_list,
cookie_jar,
None,
&DontConnectFactory,
DEFAULT_USER_AGENT.to_string()) {
Err(LoadError::UnsupportedScheme(_)) => {}
_ => panic!("expected ftp scheme to be unsupported")
}

Просмотреть файл

@ -4,14 +4,15 @@
#![cfg_attr(test, feature(box_raw))]
extern crate cookie as cookie_rs;
extern crate flate2;
extern crate hyper;
extern crate ipc_channel;
extern crate net;
extern crate net_traits;
extern crate time;
extern crate url;
extern crate util;
extern crate time;
extern crate hyper;
extern crate flate2;
#[cfg(test)] mod cookie;
#[cfg(test)] mod data_loader;