Skip to content

Commit

Permalink
Auto merge of #10188 - ConnorGBrewster:http-global-state, r=jdm
Browse files Browse the repository at this point in the history
Add Http Global State Object

This adds a new HttpState object which holds common http state(#10175). This reduces the amount of work that is required to add extra things to the Http state.

The HttpState object currently holds:
```
hsts_list: Arc::new(RwLock::new(HSTSList::new())),
cookie_jar: Arc::new(RwLock::new(CookieStorage::new())),
auth_cache: Arc::new(RwLock::new(HashMap::new())),
```

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10188)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Mar 26, 2016
2 parents bed91b3 + b09570b commit d82f97a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 204 deletions.
47 changes: 27 additions & 20 deletions components/net/http_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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)) => {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 15 additions & 11 deletions components/net/resource_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use data_loader;
use devtools_traits::{DevtoolsControlMsg};
use file_loader;
use hsts::{HSTSList, preload_hsts_domains};
use http_loader::{self, Connector, create_http_connector};
use http_loader::{self, Connector, create_http_connector, HttpState};
use hyper::client::pool::Pool;
use hyper::header::{ContentType, Header, SetCookie};
use hyper::mime::{Mime, SubLevel, TopLevel};
Expand Down Expand Up @@ -185,7 +185,7 @@ impl ResourceChannelManager {
ControlMsg::SetCookiesForUrl(request, cookie_list, source) =>
self.resource_manager.set_cookies_for_url(request, cookie_list, source),
ControlMsg::GetCookiesForUrl(url, consumer, source) => {
let cookie_jar = &self.resource_manager.cookie_storage;
let cookie_jar = &self.resource_manager.cookie_jar;
let mut cookie_jar = cookie_jar.write().unwrap();
consumer.send(cookie_jar.cookies_for_url(&url, source)).unwrap();
}
Expand Down Expand Up @@ -276,7 +276,7 @@ pub struct AuthCacheEntry {

pub struct ResourceManager {
user_agent: String,
cookie_storage: Arc<RwLock<CookieStorage>>,
cookie_jar: Arc<RwLock<CookieStorage>>,
auth_cache: Arc<RwLock<HashMap<Url, AuthCacheEntry>>>,
mime_classifier: Arc<MIMEClassifier>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
Expand All @@ -292,7 +292,7 @@ impl ResourceManager {
devtools_channel: Option<Sender<DevtoolsControlMsg>>) -> ResourceManager {
ResourceManager {
user_agent: user_agent,
cookie_storage: Arc::new(RwLock::new(CookieStorage::new())),
cookie_jar: Arc::new(RwLock::new(CookieStorage::new())),
auth_cache: Arc::new(RwLock::new(HashMap::new())),
mime_classifier: Arc::new(MIMEClassifier::new()),
devtools_chan: devtools_channel,
Expand All @@ -308,7 +308,7 @@ 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) {
let cookie_jar = &self.cookie_storage;
let cookie_jar = &self.cookie_jar;
let mut cookie_jar = cookie_jar.write().unwrap();
cookie_jar.push(cookie, source);
}
Expand Down Expand Up @@ -344,13 +344,17 @@ impl ResourceManager {
let cancel_listener = CancellationListener::new(cancel_resource);
let loader = match &*load_data.url.scheme {
"file" => from_factory(file_loader::factory),
"http" | "https" | "view-source" =>
"http" | "https" | "view-source" => {
let http_state = HttpState {
hsts_list: self.hsts_list.clone(),
cookie_jar: self.cookie_jar.clone(),
auth_cache: self.auth_cache.clone()
};
http_loader::factory(self.user_agent.clone(),
self.hsts_list.clone(),
self.cookie_storage.clone(),
self.auth_cache.clone(),
http_state,
self.devtools_chan.clone(),
self.connector.clone()),
self.connector.clone())
},
"data" => from_factory(data_loader::factory),
"about" => from_factory(about_loader::factory),
_ => {
Expand All @@ -370,6 +374,6 @@ impl ResourceManager {
fn websocket_connect(&self,
connect: WebSocketCommunicate,
connect_data: WebSocketConnectData) {
websocket_loader::init(connect, connect_data, self.cookie_storage.clone());
websocket_loader::init(connect, connect_data, self.cookie_jar.clone());
}
}
Loading

0 comments on commit d82f97a

Please sign in to comment.