Added global state struct

Replace hsts_list, auth_cache, and cookie_jar with http_state

Reverted a few changes

Moved http_state back to factory
Removed unnecessary Arc

Removed Arc for http_state
This commit is contained in:
Connor Brewster 2016-03-24 16:34:33 -06:00
parent 7f944afee8
commit b09570b2b6
3 changed files with 122 additions and 204 deletions

View file

@ -78,9 +78,7 @@ pub fn create_http_connector() -> Arc<Pool<Connector>> {
}
pub fn factory(user_agent: String,
hsts_list: Arc<RwLock<HSTSList>>,
cookie_jar: Arc<RwLock<CookieStorage>>,
auth_cache: Arc<RwLock<HashMap<Url, AuthCacheEntry>>>,
http_state: HttpState,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
connector: Arc<Pool<Connector>>)
-> Box<FnBox(LoadData,
@ -93,9 +91,7 @@ pub fn factory(user_agent: String,
senders,
classifier,
connector,
hsts_list,
cookie_jar,
auth_cache,
http_state,
devtools_chan,
cancel_listener,
user_agent)
@ -126,13 +122,27 @@ fn inner_url(url: &Url) -> Url {
Url::parse(inner_url).unwrap()
}
pub struct HttpState {
pub hsts_list: Arc<RwLock<HSTSList>>,
pub cookie_jar: Arc<RwLock<CookieStorage>>,
pub auth_cache: Arc<RwLock<HashMap<Url, AuthCacheEntry>>>,
}
impl HttpState {
pub fn new() -> HttpState {
HttpState {
hsts_list: Arc::new(RwLock::new(HSTSList::new())),
cookie_jar: Arc::new(RwLock::new(CookieStorage::new())),
auth_cache: Arc::new(RwLock::new(HashMap::new())),
}
}
}
fn load_for_consumer(load_data: LoadData,
start_chan: LoadConsumer,
classifier: Arc<MIMEClassifier>,
connector: Arc<Pool<Connector>>,
hsts_list: Arc<RwLock<HSTSList>>,
cookie_jar: Arc<RwLock<CookieStorage>>,
auth_cache: Arc<RwLock<HashMap<Url, AuthCacheEntry>>>,
http_state: HttpState,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
cancel_listener: CancellationListener,
user_agent: String) {
@ -141,8 +151,7 @@ fn load_for_consumer(load_data: LoadData,
connector: connector,
};
let context = load_data.context.clone();
match load::<WrappedHttpRequest>(load_data, hsts_list,
cookie_jar, auth_cache,
match load::<WrappedHttpRequest>(load_data, &http_state,
devtools_chan, &factory,
user_agent, &cancel_listener) {
Err(LoadError::UnsupportedScheme(url)) => {
@ -551,8 +560,8 @@ pub fn modify_request_headers(headers: &mut Headers,
}
fn set_auth_header(headers: &mut Headers,
url: &Url,
auth_cache: &Arc<RwLock<HashMap<Url, AuthCacheEntry>>>) {
url: &Url,
auth_cache: &Arc<RwLock<HashMap<Url, AuthCacheEntry>>>) {
if !headers.has::<Authorization<Basic>>() {
if let Some(auth) = auth_from_url(url) {
@ -686,9 +695,7 @@ pub fn obtain_response<A>(request_factory: &HttpRequestFactory<R=A>,
}
pub fn load<A>(load_data: LoadData,
hsts_list: Arc<RwLock<HSTSList>>,
cookie_jar: Arc<RwLock<CookieStorage>>,
auth_cache: Arc<RwLock<HashMap<Url, AuthCacheEntry>>>,
http_state: &HttpState,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
request_factory: &HttpRequestFactory<R=A>,
user_agent: String,
@ -721,7 +728,7 @@ pub fn load<A>(load_data: LoadData,
loop {
iters = iters + 1;
if &*doc_url.scheme == "http" && request_must_be_secured(&doc_url, &hsts_list) {
if &*doc_url.scheme == "http" && request_must_be_secured(&doc_url, &http_state.hsts_list) {
info!("{} is in the strict transport security list, requesting secure host", doc_url);
doc_url = secure_url(&doc_url);
}
@ -755,14 +762,14 @@ pub fn load<A>(load_data: LoadData,
let request_id = uuid::Uuid::new_v4().to_simple_string();
modify_request_headers(&mut request_headers, &doc_url,
&user_agent, &cookie_jar,
&auth_cache, &load_data);
&user_agent, &http_state.cookie_jar,
&http_state.auth_cache, &load_data);
let response = try!(obtain_response(request_factory, &doc_url, &method, &request_headers,
&cancel_listener, &load_data.data, &load_data.method,
&load_data.pipeline_id, iters, &devtools_chan, &request_id));
process_response_headers(&response, &doc_url, &cookie_jar, &hsts_list, &load_data);
process_response_headers(&response, &doc_url, &http_state.cookie_jar, &http_state.hsts_list, &load_data);
// --- Loop if there's a redirect
if response.status().class() == StatusClass::Redirection {