Skip to content

Commit

Permalink
Add a FileManager to FetchContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed Oct 14, 2016
1 parent d820387 commit fc68e0a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 38 deletions.
87 changes: 53 additions & 34 deletions components/net/fetch/methods.rs
Expand Up @@ -6,6 +6,7 @@ use connector::create_http_connector;
use data_loader::decode;
use devtools_traits::DevtoolsControlMsg;
use fetch::cors_cache::CORSCache;
use filemanager_thread::{FileManager, UIProvider};
use http_loader::{HttpState, set_default_accept_encoding, set_request_cookies};
use http_loader::{NetworkHttpRequestFactory, ReadResult, StreamedResponse, obtain_response, read_block};
use http_loader::{auth_from_cache, determine_request_referrer};
Expand Down Expand Up @@ -50,23 +51,28 @@ enum Data {
Done,
}

pub struct FetchContext {
pub struct FetchContext<UI: 'static + UIProvider> {
pub state: HttpState,
pub user_agent: Cow<'static, str>,
pub devtools_chan: Option<Sender<DevtoolsControlMsg>>,
pub filemanager: FileManager<UI>,
}

type DoneChannel = Option<(Sender<Data>, Receiver<Data>)>;

/// [Fetch](https://fetch.spec.whatwg.org#concept-fetch)
pub fn fetch(request: Rc<Request>, target: &mut Target, context: FetchContext) -> Response {
pub fn fetch<UI: 'static + UIProvider>(request: Rc<Request>,
target: &mut Target,
context: FetchContext<UI>)
-> Response {
fetch_with_cors_cache(request, &mut CORSCache::new(), target, context)
}

pub fn fetch_with_cors_cache(request: Rc<Request>,
cache: &mut CORSCache,
target: &mut Target,
context: FetchContext) -> Response {
pub fn fetch_with_cors_cache<UI: 'static + UIProvider>(request: Rc<Request>,
cache: &mut CORSCache,
target: &mut Target,
context: FetchContext<UI>)
-> Response {
// Step 1
if request.window.get() == Window::Client {
// TODO: Set window to request's client object if client is a Window object
Expand Down Expand Up @@ -131,9 +137,14 @@ pub fn fetch_with_cors_cache(request: Rc<Request>,
}

/// [Main fetch](https://fetch.spec.whatwg.org/#concept-main-fetch)
fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
recursive_flag: bool, target: &mut Target, done_chan: &mut DoneChannel,
context: &FetchContext) -> Response {
fn main_fetch<UI: 'static + UIProvider>(request: Rc<Request>,
cache: &mut CORSCache,
cors_flag: bool,
recursive_flag: bool,
target: &mut Target,
done_chan: &mut DoneChannel,
context: &FetchContext<UI>)
-> Response {
// TODO: Implement main fetch spec

// Step 1
Expand Down Expand Up @@ -389,9 +400,12 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
}

/// [Basic fetch](https://fetch.spec.whatwg.org#basic-fetch)
fn basic_fetch(request: Rc<Request>, cache: &mut CORSCache,
target: &mut Target, done_chan: &mut DoneChannel,
context: &FetchContext) -> Response {
fn basic_fetch<UI: 'static + UIProvider>(request: Rc<Request>,
cache: &mut CORSCache,
target: &mut Target,
done_chan: &mut DoneChannel,
context: &FetchContext<UI>)
-> Response {
let url = request.current_url();

match url.scheme() {
Expand Down Expand Up @@ -460,14 +474,15 @@ fn basic_fetch(request: Rc<Request>, cache: &mut CORSCache,
}

/// [HTTP fetch](https://fetch.spec.whatwg.org#http-fetch)
fn http_fetch(request: Rc<Request>,
cache: &mut CORSCache,
cors_flag: bool,
cors_preflight_flag: bool,
authentication_fetch_flag: bool,
target: &mut Target,
done_chan: &mut DoneChannel,
context: &FetchContext) -> Response {
fn http_fetch<UI: 'static + UIProvider>(request: Rc<Request>,
cache: &mut CORSCache,
cors_flag: bool,
cors_preflight_flag: bool,
authentication_fetch_flag: bool,
target: &mut Target,
done_chan: &mut DoneChannel,
context: &FetchContext<UI>)
-> Response {
// This is a new async fetch, reset the channel we are waiting on
*done_chan = None;
// Step 1
Expand Down Expand Up @@ -631,13 +646,14 @@ fn http_fetch(request: Rc<Request>,
}

/// [HTTP redirect fetch](https://fetch.spec.whatwg.org#http-redirect-fetch)
fn http_redirect_fetch(request: Rc<Request>,
cache: &mut CORSCache,
response: Rc<Response>,
cors_flag: bool,
target: &mut Target,
done_chan: &mut DoneChannel,
context: &FetchContext) -> Response {
fn http_redirect_fetch<UI: 'static + UIProvider>(request: Rc<Request>,
cache: &mut CORSCache,
response: Rc<Response>,
cors_flag: bool,
target: &mut Target,
done_chan: &mut DoneChannel,
context: &FetchContext<UI>)
-> Response {
// Step 1
assert_eq!(response.return_internal.get(), true);

Expand Down Expand Up @@ -711,11 +727,12 @@ fn http_redirect_fetch(request: Rc<Request>,
}

/// [HTTP network or cache fetch](https://fetch.spec.whatwg.org#http-network-or-cache-fetch)
fn http_network_or_cache_fetch(request: Rc<Request>,
credentials_flag: bool,
authentication_fetch_flag: bool,
done_chan: &mut DoneChannel,
context: &FetchContext) -> Response {
fn http_network_or_cache_fetch<UI: 'static + UIProvider>(request: Rc<Request>,
credentials_flag: bool,
authentication_fetch_flag: bool,
done_chan: &mut DoneChannel,
context: &FetchContext<UI>)
-> Response {
// TODO: Implement Window enum for Request
let request_has_no_window = true;

Expand Down Expand Up @@ -1108,8 +1125,10 @@ fn http_network_fetch(request: Rc<Request>,
}

/// [CORS preflight fetch](https://fetch.spec.whatwg.org#cors-preflight-fetch)
fn cors_preflight_fetch(request: Rc<Request>, cache: &mut CORSCache,
context: &FetchContext) -> Response {
fn cors_preflight_fetch<UI: 'static + UIProvider>(request: Rc<Request>,
cache: &mut CORSCache,
context: &FetchContext<UI>)
-> Response {
// Step 1
let mut preflight = Request::new(request.current_url(), Some(request.origin.borrow().clone()),
request.is_service_worker_global_scope, request.pipeline_id.get());
Expand Down
8 changes: 7 additions & 1 deletion components/net/resource_thread.rs
Expand Up @@ -592,14 +592,20 @@ impl CoreResourceManager {
};
let ua = self.user_agent.clone();
let dc = self.devtools_chan.clone();
let filemanager = self.filemanager.clone();
spawn_named(format!("fetch thread for {}", init.url), move || {
let request = Request::from_init(init);
// XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed)
// todo load context / mimesniff in fetch
// todo referrer policy?
// todo service worker stuff
let mut target = Some(Box::new(sender) as Box<FetchTaskTarget + Send + 'static>);
let context = FetchContext { state: http_state, user_agent: ua, devtools_chan: dc };
let context = FetchContext {
state: http_state,
user_agent: ua,
devtools_chan: dc,
filemanager: filemanager,
};
fetch(Rc::new(request), &mut target, context);
})
}
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/net/fetch.rs
Expand Up @@ -5,6 +5,7 @@
use devtools_traits::DevtoolsControlMsg;
use devtools_traits::HttpRequest as DevtoolsHttpRequest;
use devtools_traits::HttpResponse as DevtoolsHttpResponse;
use filemanager_thread::{TestProvider, TEST_PROVIDER};
use http_loader::{expect_devtools_http_request, expect_devtools_http_response};
use hyper::LanguageTag;
use hyper::header::{Accept, AccessControlAllowCredentials, AccessControlAllowHeaders, AccessControlAllowOrigin};
Expand All @@ -22,6 +23,7 @@ use hyper::uri::RequestUri;
use msg::constellation_msg::{ReferrerPolicy, TEST_PIPELINE_ID};
use net::fetch::cors_cache::CORSCache;
use net::fetch::methods::{FetchContext, fetch, fetch_with_cors_cache};
use net::filemanager_thread::FileManager;
use net::http_loader::HttpState;
use net_traits::FetchTaskTarget;
use net_traits::request::{Origin, RedirectMode, Referrer, Request, RequestMode};
Expand All @@ -46,11 +48,12 @@ struct FetchResponseCollector {
sender: Sender<Response>,
}

fn new_fetch_context(dc: Option<Sender<DevtoolsControlMsg>>) -> FetchContext {
fn new_fetch_context(dc: Option<Sender<DevtoolsControlMsg>>) -> FetchContext<TestProvider> {
FetchContext {
state: HttpState::new(),
user_agent: DEFAULT_USER_AGENT.into(),
devtools_chan: dc,
filemanager: FileManager::new(TEST_PROVIDER),
}
}
impl FetchTaskTarget for FetchResponseCollector {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/net/filemanager_thread.rs
Expand Up @@ -10,9 +10,9 @@ use std::fs::File;
use std::io::Read;
use std::path::PathBuf;

const TEST_PROVIDER: &'static TestProvider = &TestProvider;
pub const TEST_PROVIDER: &'static TestProvider = &TestProvider;

struct TestProvider;
pub struct TestProvider;

impl UIProvider for TestProvider {
fn open_file_dialog(&self, _path: &str, _patterns: Vec<FilterPattern>) -> Option<String> {
Expand Down

0 comments on commit fc68e0a

Please sign in to comment.