Passes an Arc<Mutex<HSTSList>> to threads instead of cloning

This commit is contained in:
Sam Gibson 2015-07-19 13:32:24 +10:00
parent 11f5be6d85
commit 82cafc4274
2 changed files with 9 additions and 7 deletions

View file

@ -24,6 +24,7 @@ use std::error::Error;
use openssl::ssl::{SslContext, SslMethod, SSL_VERIFY_PEER}; use openssl::ssl::{SslContext, SslMethod, SSL_VERIFY_PEER};
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex;
use std::sync::mpsc::{Sender, channel}; use std::sync::mpsc::{Sender, channel};
use util::task::spawn_named; use util::task::spawn_named;
use util::resource_files::resources_dir_path; use util::resource_files::resources_dir_path;
@ -36,7 +37,7 @@ use std::boxed::FnBox;
pub fn factory(cookies_chan: Sender<ControlMsg>, pub fn factory(cookies_chan: Sender<ControlMsg>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>, devtools_chan: Option<Sender<DevtoolsControlMsg>>,
hsts_list: HSTSList) hsts_list: Arc<Mutex<HSTSList>>)
-> Box<FnBox(LoadData, LoadConsumer, Arc<MIMEClassifier>) + Send> { -> Box<FnBox(LoadData, LoadConsumer, Arc<MIMEClassifier>) + Send> {
box move |load_data, senders, classifier| { box move |load_data, senders, classifier| {
spawn_named("http_loader".to_owned(), spawn_named("http_loader".to_owned(),
@ -86,7 +87,7 @@ fn load(mut load_data: LoadData,
classifier: Arc<MIMEClassifier>, classifier: Arc<MIMEClassifier>,
cookies_chan: Sender<ControlMsg>, cookies_chan: Sender<ControlMsg>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>, devtools_chan: Option<Sender<DevtoolsControlMsg>>,
hsts_list: HSTSList) { hsts_list: Arc<Mutex<HSTSList>>) {
// FIXME: At the time of writing this FIXME, servo didn't have any central // FIXME: At the time of writing this FIXME, servo didn't have any central
// location for configuration. If you're reading this and such a // location for configuration. If you're reading this and such a
// repository DOES exist, please update this constant to use it. // repository DOES exist, please update this constant to use it.
@ -117,7 +118,7 @@ fn load(mut load_data: LoadData,
loop { loop {
iters = iters + 1; iters = iters + 1;
if request_must_be_secured(&hsts_list, &url) { if request_must_be_secured(&hsts_list.lock().unwrap(), &url) {
info!("{} is in the strict transport security list, requesting secure host", url); info!("{} is in the strict transport security list, requesting secure host", url);
url = secure_url(&url); url = secure_url(&url);
} }

View file

@ -33,6 +33,7 @@ use std::env;
use std::fs::File; use std::fs::File;
use std::io::{BufReader, Read}; use std::io::{BufReader, Read};
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex;
use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::mpsc::{channel, Receiver, Sender};
static mut HOST_TABLE: Option<*mut HashMap<String, String>> = None; static mut HOST_TABLE: Option<*mut HashMap<String, String>> = None;
@ -260,7 +261,7 @@ pub struct ResourceManager {
resource_task: Sender<ControlMsg>, resource_task: Sender<ControlMsg>,
mime_classifier: Arc<MIMEClassifier>, mime_classifier: Arc<MIMEClassifier>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>, devtools_chan: Option<Sender<DevtoolsControlMsg>>,
hsts_list: HSTSList hsts_list: Arc<Mutex<HSTSList>>
} }
impl ResourceManager { impl ResourceManager {
@ -274,7 +275,7 @@ impl ResourceManager {
resource_task: resource_task, resource_task: resource_task,
mime_classifier: Arc::new(MIMEClassifier::new()), mime_classifier: Arc::new(MIMEClassifier::new()),
devtools_chan: devtools_channel, devtools_chan: devtools_channel,
hsts_list: hsts_list hsts_list: Arc::new(Mutex::new(hsts_list))
} }
} }
} }
@ -293,11 +294,11 @@ impl ResourceManager {
} }
pub fn add_hsts_entry(&mut self, entry: HSTSEntry) { pub fn add_hsts_entry(&mut self, entry: HSTSEntry) {
self.hsts_list.push(entry); self.hsts_list.lock().unwrap().push(entry);
} }
pub fn is_host_sts(&self, host: &str) -> bool { pub fn is_host_sts(&self, host: &str) -> bool {
self.hsts_list.is_host_secure(host) self.hsts_list.lock().unwrap().is_host_secure(host)
} }
fn load(&mut self, mut load_data: LoadData, consumer: LoadConsumer) { fn load(&mut self, mut load_data: LoadData, consumer: LoadConsumer) {