diff --git a/components/net/fetch/methods.rs b/components/net/fetch/methods.rs index 052e5e31ced..1a2c4ae0f4b 100644 --- a/components/net/fetch/methods.rs +++ b/components/net/fetch/methods.rs @@ -25,7 +25,7 @@ use std::mem; use std::rc::Rc; use std::sync::mpsc::{Sender, Receiver}; -pub type Target = Option>; +pub type Target<'a> = &'a mut (FetchTaskTarget + Send); pub enum Data { Payload(Vec), @@ -43,7 +43,7 @@ pub type DoneChannel = Option<(Sender, Receiver)>; /// [Fetch](https://fetch.spec.whatwg.org#concept-fetch) pub fn fetch(request: Rc, - target: &mut Target, + target: Target, context: &FetchContext) -> Response { fetch_with_cors_cache(request, &mut CorsCache::new(), target, context) @@ -51,7 +51,7 @@ pub fn fetch(request: Rc, pub fn fetch_with_cors_cache(request: Rc, cache: &mut CorsCache, - target: &mut Target, + target: Target, context: &FetchContext) -> Response { // Step 1 @@ -120,7 +120,7 @@ pub fn main_fetch(request: Rc, cache: &mut CorsCache, cors_flag: bool, recursive_flag: bool, - target: &mut Target, + target: Target, done_chan: &mut DoneChannel, context: &FetchContext) -> Response { @@ -297,20 +297,16 @@ pub fn main_fetch(request: Rc, // Step 19 if request.synchronous { - if let Some(ref mut target) = *target { - // process_response is not supposed to be used - // by sync fetch, but we overload it here for simplicity - target.process_response(&response); - } + // process_response is not supposed to be used + // by sync fetch, but we overload it here for simplicity + target.process_response(&response); if let Some(ref ch) = *done_chan { loop { match ch.1.recv() .expect("fetch worker should always send Done before terminating") { Data::Payload(vec) => { - if let Some(ref mut target) = *target { - target.process_response_chunk(vec); - } + target.process_response_chunk(vec); } Data::Done => break, } @@ -321,37 +317,29 @@ pub fn main_fetch(request: Rc, // in case there was no channel to wait for, the body was // obtained synchronously via basic_fetch for data/file/about/etc // We should still send the body across as a chunk - if let Some(ref mut target) = *target { - target.process_response_chunk(vec.clone()); - } + target.process_response_chunk(vec.clone()); } else { assert!(*body == ResponseBody::Empty) } } // overloaded similarly to process_response - if let Some(ref mut target) = *target { - target.process_response_eof(&response); - } + target.process_response_eof(&response); return response; } // Step 20 if request.body.borrow().is_some() && matches!(request.current_url().scheme(), "http" | "https") { - if let Some(ref mut target) = *target { - // XXXManishearth: We actually should be calling process_request - // in http_network_fetch. However, we can't yet follow the request - // upload progress, so I'm keeping it here for now and pretending - // the body got sent in one chunk - target.process_request_body(&request); - target.process_request_eof(&request); - } + // XXXManishearth: We actually should be calling process_request + // in http_network_fetch. However, we can't yet follow the request + // upload progress, so I'm keeping it here for now and pretending + // the body got sent in one chunk + target.process_request_body(&request); + target.process_request_eof(&request); } // Step 21 - if let Some(ref mut target) = *target { - target.process_response(&response); - } + target.process_response(&response); // Step 22 if let Some(ref ch) = *done_chan { @@ -359,14 +347,12 @@ pub fn main_fetch(request: Rc, match ch.1.recv() .expect("fetch worker should always send Done before terminating") { Data::Payload(vec) => { - if let Some(ref mut target) = *target { - target.process_response_chunk(vec); - } + target.process_response_chunk(vec); } Data::Done => break, } } - } else if let Some(ref mut target) = *target { + } else { let body = response.body.lock().unwrap(); if let ResponseBody::Done(ref vec) = *body { // in case there was no channel to wait for, the body was @@ -379,9 +365,7 @@ pub fn main_fetch(request: Rc, } // Step 24 - if let Some(ref mut target) = *target { - target.process_response_eof(&response); - } + target.process_response_eof(&response); // TODO remove this line when only asynchronous fetches are used return response; @@ -390,7 +374,7 @@ pub fn main_fetch(request: Rc, /// [Basic fetch](https://fetch.spec.whatwg.org#basic-fetch) fn basic_fetch(request: Rc, cache: &mut CorsCache, - target: &mut Target, + target: Target, done_chan: &mut DoneChannel, context: &FetchContext) -> Response { diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index 2c745d7e5ec..7904e9f995f 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -533,7 +533,7 @@ pub fn http_fetch(request: Rc, cors_flag: bool, cors_preflight_flag: bool, authentication_fetch_flag: bool, - target: &mut Target, + target: Target, done_chan: &mut DoneChannel, context: &FetchContext) -> Response { @@ -707,7 +707,7 @@ fn http_redirect_fetch(request: Rc, cache: &mut CorsCache, response: Response, cors_flag: bool, - target: &mut Target, + target: Target, done_chan: &mut DoneChannel, context: &FetchContext) -> Response { diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index 7055f928ad7..6eeb3aa6122 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -18,7 +18,7 @@ use hyper::header::{Header, SetCookie}; use hyper_serde::Serde; use ipc_channel::ipc::{self, IpcReceiver, IpcReceiverSet, IpcSender}; use net_traits::{CookieSource, CoreResourceThread}; -use net_traits::{CoreResourceMsg, FetchResponseMsg, FetchTaskTarget}; +use net_traits::{CoreResourceMsg, FetchResponseMsg}; use net_traits::{CustomResponseMediator, ResourceId}; use net_traits::{ResourceThreads, WebSocketCommunicate, WebSocketConnectData}; use net_traits::request::{Request, RequestInit}; @@ -332,7 +332,7 @@ impl CoreResourceManager { fn fetch(&self, init: RequestInit, - sender: IpcSender, + mut sender: IpcSender, group: &ResourceGroup) { let http_state = HttpState { hsts_list: group.hsts_list.clone(), @@ -349,14 +349,13 @@ impl CoreResourceManager { // todo load context / mimesniff in fetch // todo referrer policy? // todo service worker stuff - let mut target = Some(Box::new(sender) as Box); let context = FetchContext { state: http_state, user_agent: ua, devtools_chan: dc, filemanager: filemanager, }; - fetch(Rc::new(request), &mut target, &context); + fetch(Rc::new(request), &mut sender, &context); }).expect("Thread spawning failed"); } diff --git a/tests/unit/net/lib.rs b/tests/unit/net/lib.rs index c698145a074..6c2f8c7036f 100644 --- a/tests/unit/net/lib.rs +++ b/tests/unit/net/lib.rs @@ -78,22 +78,22 @@ fn fetch(request: Request, dc: Option>) -> Response { fn fetch_with_context(request: Request, context: &FetchContext) -> Response { let (sender, receiver) = channel(); - let target = Box::new(FetchResponseCollector { + let mut target = FetchResponseCollector { sender: sender, - }); + }; - methods::fetch(Rc::new(request), &mut Some(target), context); + methods::fetch(Rc::new(request), &mut target, context); receiver.recv().unwrap() } fn fetch_with_cors_cache(request: Rc, cache: &mut CorsCache) -> Response { let (sender, receiver) = channel(); - let target = Box::new(FetchResponseCollector { + let mut target = FetchResponseCollector { sender: sender, - }); + }; - methods::fetch_with_cors_cache(request, cache, &mut Some(target), &new_fetch_context(None)); + methods::fetch_with_cors_cache(request, cache, &mut target, &new_fetch_context(None)); receiver.recv().unwrap() }