Auto merge of #14465 - servo:http, r=nox

Make the fetch target non-optional.

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14465)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-12-15 14:02:42 -08:00 committed by GitHub
commit 1b2daae453
7 changed files with 120 additions and 142 deletions

View file

@ -25,7 +25,7 @@ use std::mem;
use std::rc::Rc;
use std::sync::mpsc::{Sender, Receiver};
pub type Target = Option<Box<FetchTaskTarget + Send>>;
pub type Target<'a> = &'a mut (FetchTaskTarget + Send);
pub enum Data {
Payload(Vec<u8>),
@ -43,17 +43,15 @@ pub 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 {
fetch_with_cors_cache(request, &mut CorsCache::new(), target, context)
target: Target,
context: &FetchContext) {
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 {
target: Target,
context: &FetchContext) {
// Step 1
if request.window.get() == Window::Client {
// TODO: Set window to request's client object if client is a Window object
@ -112,7 +110,7 @@ pub fn fetch_with_cors_cache(request: Rc<Request>,
}
// Step 7
main_fetch(request, cache, false, false, target, &mut None, &context)
main_fetch(request, cache, false, false, target, &mut None, &context);
}
/// [Main fetch](https://fetch.spec.whatwg.org/#concept-main-fetch)
@ -120,7 +118,7 @@ pub fn main_fetch(request: Rc<Request>,
cache: &mut CorsCache,
cors_flag: bool,
recursive_flag: bool,
target: &mut Target,
target: Target,
done_chan: &mut DoneChannel,
context: &FetchContext)
-> Response {
@ -297,20 +295,16 @@ pub fn main_fetch(request: Rc<Request>,
// 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 +315,29 @@ pub fn main_fetch(request: Rc<Request>,
// 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 +345,12 @@ pub fn main_fetch(request: Rc<Request>,
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 +363,7 @@ pub fn main_fetch(request: Rc<Request>,
}
// 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 +372,7 @@ pub fn main_fetch(request: Rc<Request>,
/// [Basic fetch](https://fetch.spec.whatwg.org#basic-fetch)
fn basic_fetch(request: Rc<Request>,
cache: &mut CorsCache,
target: &mut Target,
target: Target,
done_chan: &mut DoneChannel,
context: &FetchContext)
-> Response {

View file

@ -42,7 +42,7 @@ use std::collections::HashSet;
use std::error::Error;
use std::io::{self, Read, Write};
use std::iter::FromIterator;
use std::mem::swap;
use std::mem;
use std::ops::Deref;
use std::rc::Rc;
use std::sync::{Arc, RwLock};
@ -533,7 +533,7 @@ pub fn http_fetch(request: Rc<Request>,
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<Request>,
cache: &mut CorsCache,
response: Response,
cors_flag: bool,
target: &mut Target,
target: Target,
done_chan: &mut DoneChannel,
context: &FetchContext)
-> Response {
@ -1101,16 +1101,14 @@ fn http_network_fetch(request: Rc<Request>,
}
},
Ok(Data::Done) | Err(_) => {
let mut empty_vec = Vec::new();
let completed_body = match *res_body.lock().unwrap() {
let mut body = res_body.lock().unwrap();
let completed_body = match *body {
ResponseBody::Receiving(ref mut body) => {
// avoid cloning the body
swap(body, &mut empty_vec);
empty_vec
mem::replace(body, vec![])
},
_ => empty_vec,
_ => vec![],
};
*res_body.lock().unwrap() = ResponseBody::Done(completed_body);
*body = ResponseBody::Done(completed_body);
let _ = done_sender.send(Data::Done);
break;
}

View file

@ -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<FetchResponseMsg>,
mut sender: IpcSender<FetchResponseMsg>,
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<FetchTaskTarget + Send + 'static>);
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");
}