Test fixes; update for changes in spec

This commit is contained in:
Manish Goregaokar 2016-06-06 18:58:50 +05:30
parent f4e3e8e38e
commit fd6f9bd411
22 changed files with 320 additions and 269 deletions

View file

@ -7,6 +7,7 @@ use data_loader::decode;
use fetch::cors_cache::CORSCache; use fetch::cors_cache::CORSCache;
use http_loader::{HttpState, set_default_accept_encoding, set_request_cookies}; use http_loader::{HttpState, set_default_accept_encoding, set_request_cookies};
use http_loader::{NetworkHttpRequestFactory, ReadResult, StreamedResponse, obtain_response, read_block}; use http_loader::{NetworkHttpRequestFactory, ReadResult, StreamedResponse, obtain_response, read_block};
use http_loader::{auth_from_cache, determine_request_referrer};
use hyper::header::{Accept, AcceptLanguage, Authorization, AccessControlAllowCredentials}; use hyper::header::{Accept, AcceptLanguage, Authorization, AccessControlAllowCredentials};
use hyper::header::{AccessControlAllowOrigin, AccessControlAllowHeaders, AccessControlAllowMethods}; use hyper::header::{AccessControlAllowOrigin, AccessControlAllowHeaders, AccessControlAllowMethods};
use hyper::header::{AccessControlRequestHeaders, AccessControlMaxAge, AccessControlRequestMethod, Basic}; use hyper::header::{AccessControlRequestHeaders, AccessControlMaxAge, AccessControlRequestMethod, Basic};
@ -17,6 +18,7 @@ use hyper::method::Method;
use hyper::mime::{Mime, SubLevel, TopLevel}; use hyper::mime::{Mime, SubLevel, TopLevel};
use hyper::status::StatusCode; use hyper::status::StatusCode;
use mime_guess::guess_mime_type; use mime_guess::guess_mime_type;
use msg::constellation_msg::ReferrerPolicy;
use net_traits::FetchTaskTarget; use net_traits::FetchTaskTarget;
use net_traits::request::{CacheMode, CredentialsMode}; use net_traits::request::{CacheMode, CredentialsMode};
use net_traits::request::{RedirectMode, Referer, Request, RequestMode, ResponseTainting}; use net_traits::request::{RedirectMode, Referer, Request, RequestMode, ResponseTainting};
@ -42,17 +44,22 @@ enum Data {
Done, Done,
} }
pub struct FetchContext {
pub state: HttpState,
pub user_agent: String,
}
type DoneChannel = Option<(Sender<Data>, Receiver<Data>)>; type DoneChannel = Option<(Sender<Data>, Receiver<Data>)>;
/// [Fetch](https://fetch.spec.whatwg.org#concept-fetch) /// [Fetch](https://fetch.spec.whatwg.org#concept-fetch)
pub fn fetch(request: Rc<Request>, target: &mut Target, state: HttpState) -> Response { pub fn fetch(request: Rc<Request>, target: &mut Target, context: FetchContext) -> Response {
fetch_with_cors_cache(request, &mut CORSCache::new(), target, state) fetch_with_cors_cache(request, &mut CORSCache::new(), target, context)
} }
pub fn fetch_with_cors_cache(request: Rc<Request>, pub fn fetch_with_cors_cache(request: Rc<Request>,
cache: &mut CORSCache, cache: &mut CORSCache,
target: &mut Target, target: &mut Target,
state: HttpState) -> Response { context: FetchContext) -> Response {
// Step 1 // Step 1
if request.window.get() == Window::Client { if request.window.get() == Window::Client {
// TODO: Set window to request's client object if client is a Window object // TODO: Set window to request's client object if client is a Window object
@ -113,13 +120,13 @@ pub fn fetch_with_cors_cache(request: Rc<Request>,
} }
// Step 7 // Step 7
main_fetch(request, cache, false, false, target, &mut None, &state) main_fetch(request, cache, false, false, target, &mut None, &context)
} }
/// [Main fetch](https://fetch.spec.whatwg.org/#concept-main-fetch) /// [Main fetch](https://fetch.spec.whatwg.org/#concept-main-fetch)
fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool, fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
recursive_flag: bool, target: &mut Target, done_chan: &mut DoneChannel, recursive_flag: bool, target: &mut Target, done_chan: &mut DoneChannel,
state: &HttpState) -> Response { context: &FetchContext) -> Response {
// TODO: Implement main fetch spec // TODO: Implement main fetch spec
// Step 1 // Step 1
@ -142,14 +149,25 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
// Step 5 // Step 5
// TODO this step (CSP port/content blocking) // TODO this step (CSP port/content blocking)
// Step 6-7 // Step 6
// TODO this step (referer policy) // TODO this step (referer policy)
// currently the clients themselves set referer policy in RequestInit
// Step 7
if request.referrer_policy.get().is_none() {
request.referrer_policy.set(Some(ReferrerPolicy::NoRefWhenDowngrade));
}
// Step 8 // Step 8
if *request.referer.borrow() != Referer::NoReferer { if *request.referer.borrow() != Referer::NoReferer {
// TODO be able to invoke "determine request's referer" // remove Referer headers set in past redirects/preflights
// once this is filled in be sure to update the match // this stops the assertion in determine_request_referrer from failing
// referer below to have an unreachable branch for client request.headers.borrow_mut().remove::<RefererHeader>();
let referrer_url = determine_request_referrer(&mut *request.headers.borrow_mut(),
request.referrer_policy.get(),
request.referer.borrow_mut().take(),
request.current_url().clone());
*request.referer.borrow_mut() = Referer::from_url(referrer_url);
} }
// Step 9 // Step 9
@ -174,14 +192,14 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
(current_url.scheme() == "file" && request.same_origin_data.get()) || (current_url.scheme() == "file" && request.same_origin_data.get()) ||
current_url.scheme() == "about" || current_url.scheme() == "about" ||
request.mode == RequestMode::Navigate { request.mode == RequestMode::Navigate {
basic_fetch(request.clone(), cache, target, done_chan, state) basic_fetch(request.clone(), cache, target, done_chan, context)
} else if request.mode == RequestMode::SameOrigin { } else if request.mode == RequestMode::SameOrigin {
Response::network_error() Response::network_error()
} else if request.mode == RequestMode::NoCORS { } else if request.mode == RequestMode::NoCORS {
request.response_tainting.set(ResponseTainting::Opaque); request.response_tainting.set(ResponseTainting::Opaque);
basic_fetch(request.clone(), cache, target, done_chan, state) basic_fetch(request.clone(), cache, target, done_chan, context)
} else if !matches!(current_url.scheme(), "http" | "https") { } else if !matches!(current_url.scheme(), "http" | "https") {
Response::network_error() Response::network_error()
@ -192,7 +210,7 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
request.headers.borrow().iter().any(|h| !is_simple_header(&h)))) { request.headers.borrow().iter().any(|h| !is_simple_header(&h)))) {
request.response_tainting.set(ResponseTainting::CORSTainting); request.response_tainting.set(ResponseTainting::CORSTainting);
request.redirect_mode.set(RedirectMode::Error); request.redirect_mode.set(RedirectMode::Error);
let response = http_fetch(request.clone(), cache, true, true, false, target, done_chan, state); let response = http_fetch(request.clone(), cache, true, true, false, target, done_chan, context);
if response.is_network_error() { if response.is_network_error() {
// TODO clear cache entries using request // TODO clear cache entries using request
} }
@ -200,7 +218,7 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
} else { } else {
request.response_tainting.set(ResponseTainting::CORSTainting); request.response_tainting.set(ResponseTainting::CORSTainting);
http_fetch(request.clone(), cache, true, false, false, target, done_chan, state) http_fetch(request.clone(), cache, true, false, false, target, done_chan, context)
} }
} }
}; };
@ -233,9 +251,14 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
}; };
// Step 15 // Step 15
// TODO this step (CSP/blocking) if internal_response.url_list.borrow().is_empty() {
*internal_response.url_list.borrow_mut() = request.url_list.borrow().clone();
}
// Step 16 // Step 16
// TODO this step (CSP/blocking)
// Step 17
if !response.is_network_error() && (is_null_body_status(&internal_response.status) || if !response.is_network_error() && (is_null_body_status(&internal_response.status) ||
match *request.method.borrow() { match *request.method.borrow() {
Method::Head | Method::Connect => true, Method::Head | Method::Connect => true,
@ -247,7 +270,7 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
*body = ResponseBody::Empty; *body = ResponseBody::Empty;
} }
// Step 17 // Step 18
// TODO be able to compare response integrity against request integrity metadata // TODO be able to compare response integrity against request integrity metadata
// if !response.is_network_error() { // if !response.is_network_error() {
@ -262,7 +285,7 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
// } // }
} }
// Step 18 // Step 19
if request.synchronous { if request.synchronous {
if let Some(ref mut target) = *target { if let Some(ref mut target) = *target {
// process_response is not supposed to be used // process_response is not supposed to be used
@ -282,14 +305,12 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
Data::Done => break, Data::Done => break,
} }
} }
} else { } else if let ResponseBody::Done(ref vec) = *response.body.lock().unwrap() {
if let ResponseBody::Done(ref vec) = *response.body.lock().unwrap() { // in case there was no channel to wait for, the body was
// in case there was no channel to wait for, the body was // obtained synchronously via basic_fetch for data/file/about/etc
// obtained synchronously via basic_fetch for data/file/about/etc // We should still send the body across as a chunk
// We should still send the body across as a chunk if let Some(ref mut target) = *target {
if let Some(ref mut target) = *target { target.process_response_chunk(vec.clone());
target.process_response_chunk(vec.clone());
}
} }
} }
@ -300,7 +321,7 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
return response; return response;
} }
// Step 19 // Step 20
if request.body.borrow().is_some() && matches!(request.current_url().scheme(), "http" | "https") { if request.body.borrow().is_some() && matches!(request.current_url().scheme(), "http" | "https") {
if let Some(ref mut target) = *target { if let Some(ref mut target) = *target {
// XXXManishearth: We actually should be calling process_request // XXXManishearth: We actually should be calling process_request
@ -312,12 +333,12 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
} }
} }
// Step 20 // Step 21
if let Some(ref mut target) = *target { if let Some(ref mut target) = *target {
target.process_response(&response); target.process_response(&response);
} }
// Step 21 // Step 22
if let Some(ref ch) = *done_chan { if let Some(ref ch) = *done_chan {
loop { loop {
match ch.1.recv() match ch.1.recv()
@ -330,21 +351,19 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
Data::Done => break, Data::Done => break,
} }
} }
} else { } else if let Some(ref mut target) = *target {
if let Some(ref mut target) = *target { if let ResponseBody::Done(ref vec) = *response.body.lock().unwrap() {
if let ResponseBody::Done(ref vec) = *response.body.lock().unwrap() { // in case there was no channel to wait for, the body was
// in case there was no channel to wait for, the body was // obtained synchronously via basic_fetch for data/file/about/etc
// obtained synchronously via basic_fetch for data/file/about/etc // We should still send the body across as a chunk
// We should still send the body across as a chunk target.process_response_chunk(vec.clone());
target.process_response_chunk(vec.clone());
}
} }
} }
// Step 22 // Step 23
request.done.set(true); request.done.set(true);
// Step 23 // Step 24
if let Some(ref mut target) = *target { if let Some(ref mut target) = *target {
target.process_response_eof(&response); target.process_response_eof(&response);
} }
@ -356,7 +375,7 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
/// [Basic fetch](https://fetch.spec.whatwg.org#basic-fetch) /// [Basic fetch](https://fetch.spec.whatwg.org#basic-fetch)
fn basic_fetch(request: Rc<Request>, cache: &mut CORSCache, fn basic_fetch(request: Rc<Request>, cache: &mut CORSCache,
target: &mut Target, done_chan: &mut DoneChannel, target: &mut Target, done_chan: &mut DoneChannel,
state: &HttpState) -> Response { context: &FetchContext) -> Response {
let url = request.current_url(); let url = request.current_url();
match url.scheme() { match url.scheme() {
@ -370,7 +389,7 @@ fn basic_fetch(request: Rc<Request>, cache: &mut CORSCache,
}, },
"http" | "https" => { "http" | "https" => {
http_fetch(request.clone(), cache, false, false, false, target, done_chan, state) http_fetch(request.clone(), cache, false, false, false, target, done_chan, context)
}, },
"data" => { "data" => {
@ -432,8 +451,7 @@ fn http_fetch(request: Rc<Request>,
authentication_fetch_flag: bool, authentication_fetch_flag: bool,
target: &mut Target, target: &mut Target,
done_chan: &mut DoneChannel, done_chan: &mut DoneChannel,
state: &HttpState) -> Response { context: &FetchContext) -> Response {
// This is a new async fetch, reset the channel we are waiting on // This is a new async fetch, reset the channel we are waiting on
*done_chan = None; *done_chan = None;
// Step 1 // Step 1
@ -463,17 +481,18 @@ fn http_fetch(request: Rc<Request>,
} }
// Substep 4 // Substep 4
let actual_response = res.actual_response();
if actual_response.url_list.borrow().is_empty() {
*actual_response.url_list.borrow_mut() = request.url_list.borrow().clone();
}
// Substep 5
// TODO: set response's CSP list on actual_response // TODO: set response's CSP list on actual_response
} }
} }
// Step 4 // Step 4
let credentials = match request.credentials_mode {
CredentialsMode::Include => true,
CredentialsMode::CredentialsSameOrigin if request.response_tainting.get() == ResponseTainting::Basic
=> true,
_ => false
};
// Step 5
if response.is_none() { if response.is_none() {
// Substep 1 // Substep 1
if cors_preflight_flag { if cors_preflight_flag {
@ -488,7 +507,7 @@ fn http_fetch(request: Rc<Request>,
// Sub-substep 1 // Sub-substep 1
if method_mismatch || header_mismatch { if method_mismatch || header_mismatch {
let preflight_result = cors_preflight_fetch(request.clone(), cache, state); let preflight_result = cors_preflight_fetch(request.clone(), cache, context);
// Sub-substep 2 // Sub-substep 2
if preflight_result.response_type == ResponseType::Error { if preflight_result.response_type == ResponseType::Error {
return Response::network_error(); return Response::network_error();
@ -500,18 +519,10 @@ fn http_fetch(request: Rc<Request>,
request.skip_service_worker.set(true); request.skip_service_worker.set(true);
// Substep 3 // Substep 3
let credentials = match request.credentials_mode { let fetch_result = http_network_or_cache_fetch(request.clone(), credentials, authentication_fetch_flag,
CredentialsMode::Include => true, done_chan, context);
CredentialsMode::CredentialsSameOrigin if request.response_tainting.get() == ResponseTainting::Basic
=> true,
_ => false
};
// Substep 4 // Substep 4
let fetch_result = http_network_or_cache_fetch(request.clone(), credentials, authentication_fetch_flag,
done_chan, state);
// Substep 5
if cors_flag && cors_check(request.clone(), &fetch_result).is_err() { if cors_flag && cors_check(request.clone(), &fetch_result).is_err() {
return Response::network_error(); return Response::network_error();
} }
@ -537,7 +548,7 @@ fn http_fetch(request: Rc<Request>,
// set back to default // set back to default
response.return_internal.set(true); response.return_internal.set(true);
http_redirect_fetch(request, cache, Rc::new(response), http_redirect_fetch(request, cache, Rc::new(response),
cors_flag, target, done_chan, state) cors_flag, target, done_chan, context)
} }
} }
}, },
@ -546,7 +557,7 @@ fn http_fetch(request: Rc<Request>,
StatusCode::Unauthorized => { StatusCode::Unauthorized => {
// Step 1 // Step 1
// FIXME: Figure out what to do with request window objects // FIXME: Figure out what to do with request window objects
if cors_flag || request.credentials_mode != CredentialsMode::Include { if cors_flag || !credentials {
return response; return response;
} }
@ -556,11 +567,15 @@ fn http_fetch(request: Rc<Request>,
// Step 3 // Step 3
if !request.use_url_credentials || authentication_fetch_flag { if !request.use_url_credentials || authentication_fetch_flag {
// TODO: Prompt the user for username and password from the window // TODO: Prompt the user for username and password from the window
// Wrong, but will have to do until we are able to prompt the user
// otherwise this creates an infinite loop
// We basically pretend that the user declined to enter credentials
return response;
} }
// Step 4 // Step 4
return http_fetch(request, cache, cors_flag, cors_preflight_flag, return http_fetch(request, cache, cors_flag, cors_preflight_flag,
true, target, done_chan, state); true, target, done_chan, context);
} }
// Code 407 // Code 407
@ -573,12 +588,16 @@ fn http_fetch(request: Rc<Request>,
// Step 3 // Step 3
// TODO: Prompt the user for proxy authentication credentials // TODO: Prompt the user for proxy authentication credentials
// Wrong, but will have to do until we are able to prompt the user
// otherwise this creates an infinite loop
// We basically pretend that the user declined to enter credentials
return response;
// Step 4 // Step 4
return http_fetch(request, cache, // return http_fetch(request, cache,
cors_flag, cors_preflight_flag, // cors_flag, cors_preflight_flag,
authentication_fetch_flag, target, // authentication_fetch_flag, target,
done_chan, state); // done_chan, context);
} }
_ => { } _ => { }
@ -602,7 +621,7 @@ fn http_redirect_fetch(request: Rc<Request>,
cors_flag: bool, cors_flag: bool,
target: &mut Target, target: &mut Target,
done_chan: &mut DoneChannel, done_chan: &mut DoneChannel,
state: &HttpState) -> Response { context: &FetchContext) -> Response {
// Step 1 // Step 1
assert_eq!(response.return_internal.get(), true); assert_eq!(response.return_internal.get(), true);
@ -676,7 +695,7 @@ fn http_redirect_fetch(request: Rc<Request>,
request.url_list.borrow_mut().push(location_url); request.url_list.borrow_mut().push(location_url);
// Step 15 // Step 15
main_fetch(request, cache, cors_flag, true, target, done_chan, state) main_fetch(request, cache, cors_flag, true, target, done_chan, context)
} }
/// [HTTP network or cache fetch](https://fetch.spec.whatwg.org#http-network-or-cache-fetch) /// [HTTP network or cache fetch](https://fetch.spec.whatwg.org#http-network-or-cache-fetch)
@ -684,14 +703,14 @@ fn http_network_or_cache_fetch(request: Rc<Request>,
credentials_flag: bool, credentials_flag: bool,
authentication_fetch_flag: bool, authentication_fetch_flag: bool,
done_chan: &mut DoneChannel, done_chan: &mut DoneChannel,
state: &HttpState) -> Response { context: &FetchContext) -> Response {
// TODO: Implement Window enum for Request // TODO: Implement Window enum for Request
let request_has_no_window = true; let request_has_no_window = true;
// Step 1 // Step 1
let http_request = if request_has_no_window && let http_request = if request_has_no_window &&
request.redirect_mode.get() != RedirectMode::Follow { request.redirect_mode.get() == RedirectMode::Error {
request.clone() request
} else { } else {
Rc::new((*request).clone()) Rc::new((*request).clone())
}; };
@ -716,16 +735,13 @@ fn http_network_or_cache_fetch(request: Rc<Request>,
// Step 6 // Step 6
match *http_request.referer.borrow() { match *http_request.referer.borrow() {
// Referer::Client should not be here, but we don't set Referer::NoReferer => (),
// the referer yet, so we club it with NoReferer
Referer::NoReferer | Referer::Client =>
http_request.headers.borrow_mut().set(RefererHeader("".to_owned())),
Referer::RefererUrl(ref http_request_referer) => Referer::RefererUrl(ref http_request_referer) =>
http_request.headers.borrow_mut().set(RefererHeader(http_request_referer.to_string())), http_request.headers.borrow_mut().set(RefererHeader(http_request_referer.to_string())),
// Referer::Client => Referer::Client =>
// // it should be impossible for referer to be anything else during fetching // it should be impossible for referer to be anything else during fetching
// // https://fetch.spec.whatwg.org/#concept-request-referrer // https://fetch.spec.whatwg.org/#concept-request-referrer
// unreachable!() unreachable!()
}; };
// Step 7 // Step 7
@ -736,7 +752,7 @@ fn http_network_or_cache_fetch(request: Rc<Request>,
// Step 8 // Step 8
if !http_request.headers.borrow().has::<UserAgent>() { if !http_request.headers.borrow().has::<UserAgent>() {
http_request.headers.borrow_mut().set(UserAgent(global_user_agent().to_owned())); http_request.headers.borrow_mut().set(UserAgent(context.user_agent.clone()));
} }
match http_request.cache_mode.get() { match http_request.cache_mode.get() {
@ -780,7 +796,8 @@ fn http_network_or_cache_fetch(request: Rc<Request>,
port: current_url.port_or_known_default() port: current_url.port_or_known_default()
}; };
headers.set(host); headers.set(host);
// accept header should not be set here, unlike http // unlike http_loader, we should not set the accept header
// here, according to the fetch spec
set_default_accept_encoding(headers); set_default_accept_encoding(headers);
} }
@ -789,28 +806,27 @@ fn http_network_or_cache_fetch(request: Rc<Request>,
if credentials_flag { if credentials_flag {
// Substep 1 // Substep 1
// TODO http://mxr.mozilla.org/servo/source/components/net/http_loader.rs#504 // TODO http://mxr.mozilla.org/servo/source/components/net/http_loader.rs#504
// XXXManishearth http_loader has block_cookies, should we do this too? // XXXManishearth http_loader has block_cookies: support content blocking here too
set_request_cookies(&current_url, set_request_cookies(&current_url,
&mut *http_request.headers.borrow_mut(), &mut *http_request.headers.borrow_mut(),
&state.cookie_jar); &context.state.cookie_jar);
// Substep 2 // Substep 2
if !http_request.headers.borrow().has::<Authorization<String>>() { if !http_request.headers.borrow().has::<Authorization<String>>() {
// Substep 3 // Substep 3
let mut authorization_value = None; let mut authorization_value = None;
// Substep 4 // Substep 4
// TODO be able to retrieve https://fetch.spec.whatwg.org/#authentication-entry if let Some(basic) = auth_from_cache(&context.state.auth_cache, &current_url) {
if !http_request.use_url_credentials || !has_credentials(&current_url) {
// Substep 5 authorization_value = Some(basic);
if authentication_fetch_flag { }
} else if authentication_fetch_flag {
authorization_value = if has_credentials(&current_url) { // Substep 5
Some(Basic { if has_credentials(&current_url) {
authorization_value = Some(Basic {
username: current_url.username().to_owned(), username: current_url.username().to_owned(),
password: current_url.password().map(str::to_owned) password: current_url.password().map(str::to_owned)
}) })
} else {
None
} }
} }
@ -866,7 +882,7 @@ fn http_network_or_cache_fetch(request: Rc<Request>,
// Step 18 // Step 18
if response.is_none() { if response.is_none() {
response = Some(http_network_fetch(http_request.clone(), http_request.clone(), credentials_flag, done_chan)); response = Some(http_network_fetch(http_request.clone(), credentials_flag, done_chan));
} }
let response = response.unwrap(); let response = response.unwrap();
@ -901,7 +917,6 @@ fn http_network_or_cache_fetch(request: Rc<Request>,
/// [HTTP network fetch](https://fetch.spec.whatwg.org/#http-network-fetch) /// [HTTP network fetch](https://fetch.spec.whatwg.org/#http-network-fetch)
fn http_network_fetch(request: Rc<Request>, fn http_network_fetch(request: Rc<Request>,
_http_request: Rc<Request>,
_credentials_flag: bool, _credentials_flag: bool,
done_chan: &mut DoneChannel) -> Response { done_chan: &mut DoneChannel) -> Response {
// TODO: Implement HTTP network fetch spec // TODO: Implement HTTP network fetch spec
@ -926,12 +941,12 @@ fn http_network_fetch(request: Rc<Request>,
let wrapped_response = obtain_response(&factory, &url, &request.method.borrow(), let wrapped_response = obtain_response(&factory, &url, &request.method.borrow(),
&request.headers.borrow(), &request.headers.borrow(),
&cancellation_listener, &request.body.borrow(), &request.method.borrow(), &cancellation_listener, &request.body.borrow(), &request.method.borrow(),
&None, request.redirect_count.get()+1, &None, ""); &None, request.redirect_count.get() + 1, &None, "");
let mut response = Response::new(); let mut response = Response::new();
match wrapped_response { match wrapped_response {
Ok((mut res, _)) => { Ok((res, _)) => {
response.url = Some(res.response.url.clone()); response.url = Some(url.clone());
response.status = Some(res.response.status); response.status = Some(res.response.status);
response.raw_status = Some(res.response.status_raw().clone()); response.raw_status = Some(res.response.status_raw().clone());
response.headers = res.response.headers.clone(); response.headers = res.response.headers.clone();
@ -945,12 +960,11 @@ fn http_network_fetch(request: Rc<Request>,
spawn_named(format!("fetch worker thread"), move || { spawn_named(format!("fetch worker thread"), move || {
match StreamedResponse::from_http_response(box res, meta) { match StreamedResponse::from_http_response(box res, meta) {
Ok(mut res) => { Ok(mut res) => {
*res_body.lock().unwrap() = ResponseBody::Receiving(vec![]); *res_body.lock().unwrap() = ResponseBody::Receiving(vec![]);
loop { loop {
match read_block(&mut res) { match read_block(&mut res) {
Ok(ReadResult::Payload(chunk)) => { Ok(ReadResult::Payload(chunk)) => {
if let ResponseBody::Receiving(ref mut body) = *res_body.lock().unwrap() { if let ResponseBody::Receiving(ref mut body) = *res_body.lock().unwrap() {
body.extend_from_slice(&chunk); body.extend_from_slice(&chunk);
if let Some(ref sender) = done_sender { if let Some(ref sender) = done_sender {
let _ = sender.send(Data::Payload(chunk)); let _ = sender.send(Data::Payload(chunk));
@ -1003,7 +1017,10 @@ fn http_network_fetch(request: Rc<Request>,
// TODO Read request // TODO Read request
// Step 5 // Step 5-9
// (needs stream bodies)
// Step 10
// TODO when https://bugzilla.mozilla.org/show_bug.cgi?id=1030660 // TODO when https://bugzilla.mozilla.org/show_bug.cgi?id=1030660
// is resolved, this step will become uneccesary // is resolved, this step will become uneccesary
// TODO this step // TODO this step
@ -1015,22 +1032,19 @@ fn http_network_fetch(request: Rc<Request>,
} }
}; };
// Step 6 // Step 11
*response.url_list.borrow_mut() = request.url_list.borrow().clone(); // TODO this step isn't possible yet (CSP)
// Step 7 // Step 12
// TODO this step isn't possible yet
// Step 8
if response.is_network_error() && request.cache_mode.get() == CacheMode::NoStore { if response.is_network_error() && request.cache_mode.get() == CacheMode::NoStore {
// TODO update response in the HTTP cache for request // TODO update response in the HTTP cache for request
} }
// TODO this step isn't possible yet // TODO this step isn't possible yet
// Step 9 // Step 13
// TODO these steps // TODO these steps
// Step 10 // Step 14
// Substep 1 // Substep 1
// Substep 2 // Substep 2
// Sub-substep 1 // Sub-substep 1
@ -1039,12 +1053,12 @@ fn http_network_fetch(request: Rc<Request>,
// Sub-substep 4 // Sub-substep 4
// Substep 3 // Substep 3
// Step 11 // Step 15
response response
} }
/// [CORS preflight fetch](https://fetch.spec.whatwg.org#cors-preflight-fetch) /// [CORS preflight fetch](https://fetch.spec.whatwg.org#cors-preflight-fetch)
fn cors_preflight_fetch(request: Rc<Request>, cache: &mut CORSCache, state: &HttpState) -> Response { fn cors_preflight_fetch(request: Rc<Request>, cache: &mut CORSCache, context: &FetchContext) -> Response {
// Step 1 // Step 1
let mut preflight = Request::new(request.current_url(), Some(request.origin.borrow().clone()), false); let mut preflight = Request::new(request.current_url(), Some(request.origin.borrow().clone()), false);
*preflight.method.borrow_mut() = Method::Options; *preflight.method.borrow_mut() = Method::Options;
@ -1052,6 +1066,7 @@ fn cors_preflight_fetch(request: Rc<Request>, cache: &mut CORSCache, state: &Htt
preflight.type_ = request.type_.clone(); preflight.type_ = request.type_.clone();
preflight.destination = request.destination.clone(); preflight.destination = request.destination.clone();
*preflight.referer.borrow_mut() = request.referer.borrow().clone(); *preflight.referer.borrow_mut() = request.referer.borrow().clone();
preflight.referrer_policy.set(preflight.referrer_policy.get());
// Step 2 // Step 2
preflight.headers.borrow_mut().set::<AccessControlRequestMethod>( preflight.headers.borrow_mut().set::<AccessControlRequestMethod>(
@ -1072,7 +1087,7 @@ fn cors_preflight_fetch(request: Rc<Request>, cache: &mut CORSCache, state: &Htt
// Step 6 // Step 6
let preflight = Rc::new(preflight); let preflight = Rc::new(preflight);
let response = http_network_or_cache_fetch(preflight.clone(), false, false, &mut None, state); let response = http_network_or_cache_fetch(preflight.clone(), false, false, &mut None, context);
// Step 7 // Step 7
if cors_check(request.clone(), &response).is_ok() && if cors_check(request.clone(), &response).is_ok() &&
@ -1105,12 +1120,16 @@ fn cors_preflight_fetch(request: Rc<Request>, cache: &mut CORSCache, state: &Htt
} }
// Substep 5 // Substep 5
debug!("CORS check: Allowed methods: {:?}, current method: {:?}",
methods, request.method.borrow());
if methods.iter().all(|method| *method != *request.method.borrow()) && if methods.iter().all(|method| *method != *request.method.borrow()) &&
!is_simple_method(&*request.method.borrow()) { !is_simple_method(&*request.method.borrow()) {
return Response::network_error(); return Response::network_error();
} }
// Substep 6 // Substep 6
debug!("CORS check: Allowed headers: {:?}, current headers: {:?}",
header_names, request.headers.borrow());
let set: HashSet<&UniCase<String>> = HashSet::from_iter(header_names.iter()); let set: HashSet<&UniCase<String>> = HashSet::from_iter(header_names.iter());
if request.headers.borrow().iter().any(|ref hv| !set.contains(&UniCase(hv.name().to_owned())) && if request.headers.borrow().iter().any(|ref hv| !set.contains(&UniCase(hv.name().to_owned())) &&
!is_simple_header(hv)) { !is_simple_header(hv)) {
@ -1183,12 +1202,6 @@ fn cors_check(request: Rc<Request>, response: &Response) -> Result<(), ()> {
Err(()) Err(())
} }
fn global_user_agent() -> String {
// TODO have a better useragent string
const USER_AGENT_STRING: &'static str = "Servo";
USER_AGENT_STRING.to_owned()
}
fn has_credentials(url: &Url) -> bool { fn has_credentials(url: &Url) -> bool {
!url.username().is_empty() || url.password().is_some() !url.username().is_empty() || url.password().is_some()
} }
@ -1199,6 +1212,7 @@ fn is_no_store_cache(headers: &Headers) -> bool {
headers.has::<IfRange>() headers.has::<IfRange>()
} }
/// https://fetch.spec.whatwg.org/#cors-safelisted-request-header
fn is_simple_header(h: &HeaderView) -> bool { fn is_simple_header(h: &HeaderView) -> bool {
if h.is::<ContentType>() { if h.is::<ContentType>() {
match h.value() { match h.value() {

View file

@ -434,7 +434,7 @@ fn strip_url(mut referrer_url: Url, origin_only: bool) -> Option<Url> {
} }
/// https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer /// https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer
fn determine_request_referrer(headers: &mut Headers, pub fn determine_request_referrer(headers: &mut Headers,
referrer_policy: Option<ReferrerPolicy>, referrer_policy: Option<ReferrerPolicy>,
referrer_url: Option<Url>, referrer_url: Option<Url>,
url: Url) -> Option<Url> { url: Url) -> Option<Url> {
@ -667,18 +667,21 @@ fn set_auth_header(headers: &mut Headers,
if let Some(auth) = auth_from_url(url) { if let Some(auth) = auth_from_url(url) {
headers.set(auth); headers.set(auth);
} else { } else {
if let Some(ref auth_entry) = auth_cache.read().unwrap().entries.get(url) { if let Some(basic) = auth_from_cache(auth_cache, url) {
auth_from_entry(&auth_entry, headers); headers.set(Authorization(basic));
} }
} }
} }
} }
fn auth_from_entry(auth_entry: &AuthCacheEntry, headers: &mut Headers) { pub fn auth_from_cache(auth_cache: &Arc<RwLock<AuthCache>>, url: &Url) -> Option<Basic> {
let user_name = auth_entry.user_name.clone(); if let Some(ref auth_entry) = auth_cache.read().unwrap().entries.get(url) {
let password = Some(auth_entry.password.clone()); let user_name = auth_entry.user_name.clone();
let password = Some(auth_entry.password.clone());
headers.set(Authorization(Basic { username: user_name, password: password })); Some(Basic { username: user_name, password: password })
} else {
None
}
} }
fn auth_from_url(doc_url: &Url) -> Option<Authorization<Basic>> { fn auth_from_url(doc_url: &Url) -> Option<Authorization<Basic>> {
@ -955,6 +958,7 @@ pub fn load<A, B>(load_data: &LoadData,
// https://fetch.spec.whatwg.org/#http-network-or-cache-fetch step 12 // https://fetch.spec.whatwg.org/#http-network-or-cache-fetch step 12
set_auth_header(&mut request_headers, &doc_url, &http_state.auth_cache); set_auth_header(&mut request_headers, &doc_url, &http_state.auth_cache);
} }
//if there is a new auth header then set the request headers with it //if there is a new auth header then set the request headers with it
if let Some(ref auth_header) = new_auth_header { if let Some(ref auth_header) = new_auth_header {
request_headers.set(auth_header.clone()); request_headers.set(auth_header.clone());

View file

@ -11,7 +11,7 @@ use cookie;
use cookie_storage::CookieStorage; use cookie_storage::CookieStorage;
use data_loader; use data_loader;
use devtools_traits::DevtoolsControlMsg; use devtools_traits::DevtoolsControlMsg;
use fetch::methods::fetch; use fetch::methods::{fetch, FetchContext};
use file_loader; use file_loader;
use filemanager_thread::FileManagerThreadFactory; use filemanager_thread::FileManagerThreadFactory;
use hsts::HstsList; use hsts::HstsList;
@ -23,11 +23,11 @@ use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use mime_classifier::{ApacheBugFlag, MIMEClassifier, NoSniffFlag}; use mime_classifier::{ApacheBugFlag, MIMEClassifier, NoSniffFlag};
use net_traits::LoadContext; use net_traits::LoadContext;
use net_traits::ProgressMsg::Done; use net_traits::ProgressMsg::Done;
use net_traits::request::{Request, RequestInit};
use net_traits::{AsyncResponseTarget, Metadata, ProgressMsg, ResponseAction, CoreResourceThread}; use net_traits::{AsyncResponseTarget, Metadata, ProgressMsg, ResponseAction, CoreResourceThread};
use net_traits::{CoreResourceMsg, CookieSource, FetchResponseMsg, FetchTaskTarget, LoadConsumer}; use net_traits::{CoreResourceMsg, CookieSource, FetchResponseMsg, FetchTaskTarget, LoadConsumer};
use net_traits::{LoadData, LoadResponse, NetworkError, ResourceId}; use net_traits::{LoadData, LoadResponse, NetworkError, ResourceId};
use net_traits::{WebSocketCommunicate, WebSocketConnectData, ResourceThreads}; use net_traits::{WebSocketCommunicate, WebSocketConnectData, ResourceThreads};
use net_traits::request::{Request, RequestInit};
use profile_traits::time::ProfilerChan; use profile_traits::time::ProfilerChan;
use rustc_serialize::json; use rustc_serialize::json;
use rustc_serialize::{Decodable, Encodable}; use rustc_serialize::{Decodable, Encodable};
@ -493,14 +493,16 @@ impl CoreResourceManager {
auth_cache: self.auth_cache.clone(), auth_cache: self.auth_cache.clone(),
blocked_content: BLOCKED_CONTENT_RULES.clone(), blocked_content: BLOCKED_CONTENT_RULES.clone(),
}; };
let ua = self.user_agent.clone();
spawn_named(format!("fetch thread for {}", init.url), move || { spawn_named(format!("fetch thread for {}", init.url), move || {
let request = Request::from_init(init); let request = Request::from_init(init);
// XXXManishearth: Check origin against pipeline id // XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed)
// todo load context / mimesniff in fetch // todo load context / mimesniff in fetch
// todo referrer policy? // todo referrer policy?
// todo service worker stuff // todo service worker stuff
let mut target = Some(Box::new(sender) as Box<FetchTaskTarget + Send + 'static>); let mut target = Some(Box::new(sender) as Box<FetchTaskTarget + Send + 'static>);
fetch(Rc::new(request), &mut target, http_state); let context = FetchContext { state: http_state, user_agent: ua };
fetch(Rc::new(request), &mut target, context);
}) })
} }

View file

@ -213,6 +213,7 @@ impl FetchTaskTarget for IpcSender<FetchResponseMsg> {
fn process_response(&mut self, response: &Response) { fn process_response(&mut self, response: &Response) {
let _ = self.send(FetchResponseMsg::ProcessResponse(response.metadata())); let _ = self.send(FetchResponseMsg::ProcessResponse(response.metadata()));
} }
fn process_response_chunk(&mut self, chunk: Vec<u8>) { fn process_response_chunk(&mut self, chunk: Vec<u8>) {
let _ = self.send(FetchResponseMsg::ProcessResponseChunk(chunk)); let _ = self.send(FetchResponseMsg::ProcessResponseChunk(chunk));
} }
@ -220,7 +221,8 @@ impl FetchTaskTarget for IpcSender<FetchResponseMsg> {
fn process_response_eof(&mut self, response: &Response) { fn process_response_eof(&mut self, response: &Response) {
if response.is_network_error() { if response.is_network_error() {
// todo: finer grained errors // todo: finer grained errors
let _ = self.send(FetchResponseMsg::ProcessResponseEOF(Err(NetworkError::Internal("Network error".into())))); let _ = self.send(FetchResponseMsg::ProcessResponseEOF(
Err(NetworkError::Internal("Network error".into()))));
} else { } else {
let _ = self.send(FetchResponseMsg::ProcessResponseEOF(Ok(()))); let _ = self.send(FetchResponseMsg::ProcessResponseEOF(Ok(())));
} }

View file

@ -4,7 +4,9 @@
use hyper::header::Headers; use hyper::header::Headers;
use hyper::method::Method; use hyper::method::Method;
use msg::constellation_msg::ReferrerPolicy;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::mem::swap;
use url::{Origin as UrlOrigin, Url}; use url::{Origin as UrlOrigin, Url};
/// An [initiator](https://fetch.spec.whatwg.org/#concept-request-initiator) /// An [initiator](https://fetch.spec.whatwg.org/#concept-request-initiator)
@ -125,6 +127,9 @@ pub struct RequestInit {
// this should actually be set by fetch, but fetch // this should actually be set by fetch, but fetch
// doesn't have info about the client right now // doesn't have info about the client right now
pub origin: Url, pub origin: Url,
// XXXManishearth these should be part of the client object
pub referer_url: Option<Url>,
pub referrer_policy: Option<ReferrerPolicy>,
} }
/// A [Request](https://fetch.spec.whatwg.org/#requests) as defined by the Fetch spec /// A [Request](https://fetch.spec.whatwg.org/#requests) as defined by the Fetch spec
@ -151,8 +156,9 @@ pub struct Request {
pub origin: RefCell<Origin>, pub origin: RefCell<Origin>,
pub omit_origin_header: Cell<bool>, pub omit_origin_header: Cell<bool>,
pub same_origin_data: Cell<bool>, pub same_origin_data: Cell<bool>,
/// https://fetch.spec.whatwg.org/#concept-request-referrer
pub referer: RefCell<Referer>, pub referer: RefCell<Referer>,
// TODO: referrer policy pub referrer_policy: Cell<Option<ReferrerPolicy>>,
pub synchronous: bool, pub synchronous: bool,
pub mode: RequestMode, pub mode: RequestMode,
pub use_cors_preflight: bool, pub use_cors_preflight: bool,
@ -191,6 +197,7 @@ impl Request {
omit_origin_header: Cell::new(false), omit_origin_header: Cell::new(false),
same_origin_data: Cell::new(false), same_origin_data: Cell::new(false),
referer: RefCell::new(Referer::Client), referer: RefCell::new(Referer::Client),
referrer_policy: Cell::new(None),
synchronous: false, synchronous: false,
mode: RequestMode::NoCORS, mode: RequestMode::NoCORS,
use_cors_preflight: false, use_cors_preflight: false,
@ -220,6 +227,12 @@ impl Request {
req.credentials_mode = init.credentials_mode; req.credentials_mode = init.credentials_mode;
req.use_url_credentials = init.use_url_credentials; req.use_url_credentials = init.use_url_credentials;
*req.origin.borrow_mut() = Origin::Origin(init.origin.origin()); *req.origin.borrow_mut() = Origin::Origin(init.origin.origin());
*req.referer.borrow_mut() = if let Some(url) = init.referer_url {
Referer::RefererUrl(url)
} else {
Referer::NoReferer
};
req.referrer_policy.set(init.referrer_policy);
req req
} }
@ -246,6 +259,7 @@ impl Request {
omit_origin_header: Cell::new(false), omit_origin_header: Cell::new(false),
same_origin_data: Cell::new(false), same_origin_data: Cell::new(false),
referer: RefCell::new(Referer::Client), referer: RefCell::new(Referer::Client),
referrer_policy: Cell::new(None),
synchronous: false, synchronous: false,
// Step 1-2 // Step 1-2
mode: match cors_attribute_state { mode: match cors_attribute_state {
@ -296,3 +310,27 @@ impl Request {
} }
} }
} }
impl Referer {
pub fn to_url(&self) -> Option<&Url> {
match *self {
Referer::NoReferer | Referer::Client => None,
Referer::RefererUrl(ref url) => Some(url)
}
}
pub fn from_url(url: Option<Url>) -> Self {
if let Some(url) = url {
Referer::RefererUrl(url)
} else {
Referer::NoReferer
}
}
pub fn take(&mut self) -> Option<Url> {
let mut new = Referer::Client;
swap(self, &mut new);
match new {
Referer::NoReferer | Referer::Client => None,
Referer::RefererUrl(url) => Some(url)
}
}
}

View file

@ -7,11 +7,11 @@
use hyper::header::{AccessControlExposeHeaders, ContentType, Headers}; use hyper::header::{AccessControlExposeHeaders, ContentType, Headers};
use hyper::http::RawStatus; use hyper::http::RawStatus;
use hyper::status::StatusCode; use hyper::status::StatusCode;
use {Metadata, NetworkError};
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use url::Url; use url::Url;
use {Metadata, NetworkError};
/// [Response type](https://fetch.spec.whatwg.org/#concept-response-type) /// [Response type](https://fetch.spec.whatwg.org/#concept-response-type)
#[derive(Clone, PartialEq, Copy, Debug, Deserialize, Serialize)] #[derive(Clone, PartialEq, Copy, Debug, Deserialize, Serialize)]

View file

@ -36,7 +36,7 @@ use hyper::header::Headers;
use hyper::header::{ContentLength, ContentType}; use hyper::header::{ContentLength, ContentType};
use hyper::http::RawStatus; use hyper::http::RawStatus;
use hyper::method::Method; use hyper::method::Method;
use hyper::mime::{self, Mime}; use hyper::mime::{self, Mime, Attr as MimeAttr, Value as MimeValue};
use ipc_channel::ipc; use ipc_channel::ipc;
use ipc_channel::router::ROUTER; use ipc_channel::router::ROUTER;
use js::jsapi::JS_ClearPendingException; use js::jsapi::JS_ClearPendingException;
@ -44,10 +44,10 @@ use js::jsapi::{JSContext, JS_ParseJSON, RootedValue};
use js::jsval::{JSVal, NullValue, UndefinedValue}; use js::jsval::{JSVal, NullValue, UndefinedValue};
use msg::constellation_msg::{PipelineId, ReferrerPolicy}; use msg::constellation_msg::{PipelineId, ReferrerPolicy};
use net_traits::CoreResourceMsg::Fetch; use net_traits::CoreResourceMsg::Fetch;
use net_traits::trim_http_whitespace;
use net_traits::{FetchResponseListener, Metadata, NetworkError, RequestSource};
use net_traits::{CoreResourceThread, LoadOrigin};
use net_traits::request::{CredentialsMode, Destination, RequestInit, RequestMode}; use net_traits::request::{CredentialsMode, Destination, RequestInit, RequestMode};
use net_traits::trim_http_whitespace;
use net_traits::{CoreResourceThread, LoadOrigin};
use net_traits::{FetchResponseListener, Metadata, NetworkError, RequestSource};
use network_listener::{NetworkListener, PreInvoke}; use network_listener::{NetworkListener, PreInvoke};
use parse::html::{ParseContext, parse_html}; use parse::html::{ParseContext, parse_html};
use parse::xml::{self, parse_xml}; use parse::xml::{self, parse_xml};
@ -62,6 +62,7 @@ use string_cache::Atom;
use time; use time;
use timers::{OneshotTimerCallback, OneshotTimerHandle}; use timers::{OneshotTimerCallback, OneshotTimerHandle};
use url::{Url, Position}; use url::{Url, Position};
use util::prefs::mozbrowser_enabled;
#[derive(JSTraceable, PartialEq, Copy, Clone, HeapSizeOf)] #[derive(JSTraceable, PartialEq, Copy, Clone, HeapSizeOf)]
enum XMLHttpRequestState { enum XMLHttpRequestState {
@ -521,7 +522,7 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
Method::Get | Method::Head => None, Method::Get | Method::Head => None,
_ => data _ => data
}; };
// Step 4 // Step 4 (first half)
let extracted = data.as_ref().map(|d| d.extract()); let extracted = data.as_ref().map(|d| d.extract());
self.request_body_len.set(extracted.as_ref().map_or(0, |e| e.0.len())); self.request_body_len.set(extracted.as_ref().map_or(0, |e| e.0.len()));
@ -572,6 +573,22 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
} else { } else {
unreachable!() unreachable!()
}; };
let bypass_cross_origin_check = {
// We want to be able to do cross-origin requests in browser.html.
// If the XHR happens in a top level window and the mozbrowser
// preference is enabled, we allow bypassing the CORS check.
// This is a temporary measure until we figure out Servo privilege
// story. See https://github.com/servo/servo/issues/9582
if let GlobalRoot::Window(win) = self.global() {
let is_root_pipeline = win.parent_info().is_none();
let is_mozbrowser_enabled = mozbrowser_enabled();
is_root_pipeline && is_mozbrowser_enabled
} else {
false
}
};
let mut request = RequestInit { let mut request = RequestInit {
method: self.request_method.borrow().clone(), method: self.request_method.borrow().clone(),
url: self.request_url.borrow().clone().unwrap(), url: self.request_url.borrow().clone().unwrap(),
@ -589,18 +606,59 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
credentials_mode: credentials_mode, credentials_mode: credentials_mode,
use_url_credentials: use_url_credentials, use_url_credentials: use_url_credentials,
origin: self.global().r().get_url(), origin: self.global().r().get_url(),
referer_url: self.referrer_url.clone(),
referrer_policy: self.referrer_policy.clone(),
}; };
// XHR spec differs from http, and says UTF-8 should be in capitals,
// instead of "utf-8", which is what Hyper defaults to. So not if bypass_cross_origin_check {
// using content types provided by Hyper. request.mode = RequestMode::Navigate;
let n = "content-type"; }
// step 4 (second half)
match extracted { match extracted {
Some((_, Some(ref content_type))) => Some((_, ref content_type)) => {
request.headers.set_raw(n.to_owned(), vec![content_type.bytes().collect()]), // this should handle Document bodies too, not just BodyInit
let encoding = if let Some(BodyInit::String(_)) = data {
// XHR spec differs from http, and says UTF-8 should be in capitals,
// instead of "utf-8", which is what Hyper defaults to. So not
// using content types provided by Hyper.
Some(MimeValue::Ext("UTF-8".to_string()))
} else {
None
};
let mut content_type_set = false;
if let Some(ref ct) = *content_type {
if !request.headers.has::<ContentType>() {
request.headers.set_raw("content-type", vec![ct.bytes().collect()]);
content_type_set = true;
}
}
if !content_type_set {
let ct = request.headers.get::<ContentType>().map(|x| x.clone());
if let Some(mut ct) = ct {
if let Some(encoding) = encoding {
for param in &mut (ct.0).2 {
if param.0 == MimeAttr::Charset {
if !param.0.as_str().eq_ignore_ascii_case(encoding.as_str()) {
*param = (MimeAttr::Charset, encoding.clone());
}
}
}
}
// remove instead of mutate in place
// https://github.com/hyperium/hyper/issues/821
request.headers.remove_raw("content-type");
request.headers.set(ct);
}
}
}
_ => (), _ => (),
} }
debug!("request_headers = {:?}", *self.request_headers.borrow()); debug!("request.headers = {:?}", request.headers);
self.fetch_time.set(time::now().to_timespec().sec); self.fetch_time.set(time::now().to_timespec().sec);
@ -1233,7 +1291,6 @@ impl XMLHttpRequest {
fn fetch(&self, fn fetch(&self,
init: RequestInit, init: RequestInit,
global: GlobalRef) -> ErrorResult { global: GlobalRef) -> ErrorResult {
let xhr = Trusted::new(self); let xhr = Trusted::new(self);
let context = Arc::new(Mutex::new(XHRContext { let context = Arc::new(Mutex::new(XHRContext {

View file

@ -17,7 +17,7 @@ pub struct NetworkListener<Listener: PreInvoke + Send + 'static> {
} }
impl<Listener: PreInvoke + Send + 'static> NetworkListener<Listener> { impl<Listener: PreInvoke + Send + 'static> NetworkListener<Listener> {
pub fn notify<A: Action<Listener>+Send+'static>(&self, action: A) { pub fn notify<A: Action<Listener> + Send + 'static>(&self, action: A) {
if let Err(err) = self.script_chan.send(CommonScriptMsg::RunnableMsg(NetworkEvent, box ListenerRunnable { if let Err(err) = self.script_chan.send(CommonScriptMsg::RunnableMsg(NetworkEvent, box ListenerRunnable {
context: self.context.clone(), context: self.context.clone(),
action: action, action: action,
@ -51,12 +51,12 @@ pub trait PreInvoke {
} }
/// A runnable for moving the async network events between threads. /// A runnable for moving the async network events between threads.
struct ListenerRunnable<A: Action<Listener>+Send+'static, Listener: PreInvoke + Send> { struct ListenerRunnable<A: Action<Listener> + Send + 'static, Listener: PreInvoke + Send> {
context: Arc<Mutex<Listener>>, context: Arc<Mutex<Listener>>,
action: A, action: A,
} }
impl<A: Action<Listener>+Send+'static, Listener: PreInvoke + Send> Runnable for ListenerRunnable<A, Listener> { impl<A: Action<Listener> + Send + 'static, Listener: PreInvoke + Send> Runnable for ListenerRunnable<A, Listener> {
fn handler(self: Box<ListenerRunnable<A, Listener>>) { fn handler(self: Box<ListenerRunnable<A, Listener>>) {
let this = *self; let this = *self;
let mut context = this.context.lock().unwrap(); let mut context = this.context.lock().unwrap();

View file

@ -14,7 +14,7 @@ use hyper::server::{Request as HyperRequest, Response as HyperResponse};
use hyper::status::StatusCode; use hyper::status::StatusCode;
use hyper::uri::RequestUri; use hyper::uri::RequestUri;
use net::fetch::cors_cache::CORSCache; use net::fetch::cors_cache::CORSCache;
use net::fetch::methods::{fetch, fetch_with_cors_cache}; use net::fetch::methods::{FetchContext, fetch, fetch_with_cors_cache};
use net::http_loader::HttpState; use net::http_loader::HttpState;
use net_traits::FetchTaskTarget; use net_traits::FetchTaskTarget;
use net_traits::request::{Origin, RedirectMode, Referer, Request, RequestMode}; use net_traits::request::{Origin, RedirectMode, Referer, Request, RequestMode};
@ -22,10 +22,10 @@ use net_traits::response::{CacheState, Response, ResponseBody, ResponseType};
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::rc::Rc; use std::rc::Rc;
use std::thread;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{Sender, channel}; use std::sync::mpsc::{Sender, channel};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread;
use time::{self, Duration}; use time::{self, Duration};
use unicase::UniCase; use unicase::UniCase;
use url::{Origin as UrlOrigin, Url}; use url::{Origin as UrlOrigin, Url};
@ -37,6 +37,12 @@ struct FetchResponseCollector {
sender: Sender<Response>, sender: Sender<Response>,
} }
fn new_fetch_context() -> FetchContext {
FetchContext {
state: HttpState::new(),
user_agent: "Such Browser. Very Layout. Wow.".into(),
}
}
impl FetchTaskTarget for FetchResponseCollector { impl FetchTaskTarget for FetchResponseCollector {
fn process_request_body(&mut self, _: &Request) {} fn process_request_body(&mut self, _: &Request) {}
fn process_request_eof(&mut self, _: &Request) {} fn process_request_eof(&mut self, _: &Request) {}
@ -50,7 +56,7 @@ impl FetchTaskTarget for FetchResponseCollector {
fn fetch_async(request: Request, target: Box<FetchTaskTarget + Send>) { fn fetch_async(request: Request, target: Box<FetchTaskTarget + Send>) {
thread::spawn(move || { thread::spawn(move || {
fetch(Rc::new(request), &mut Some(target), HttpState::new()); fetch(Rc::new(request), &mut Some(target), new_fetch_context());
}); });
} }
@ -77,7 +83,7 @@ fn test_fetch_response_is_not_network_error() {
*request.referer.borrow_mut() = Referer::NoReferer; *request.referer.borrow_mut() = Referer::NoReferer;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
if fetch_response.is_network_error() { if fetch_response.is_network_error() {
@ -98,7 +104,7 @@ fn test_fetch_response_body_matches_const_message() {
*request.referer.borrow_mut() = Referer::NoReferer; *request.referer.borrow_mut() = Referer::NoReferer;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
assert!(!fetch_response.is_network_error()); assert!(!fetch_response.is_network_error());
@ -120,7 +126,7 @@ fn test_fetch_aboutblank() {
*request.referer.borrow_mut() = Referer::NoReferer; *request.referer.borrow_mut() = Referer::NoReferer;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
assert!(!fetch_response.is_network_error()); assert!(!fetch_response.is_network_error());
assert!(*fetch_response.body.lock().unwrap() == ResponseBody::Done(vec![])); assert!(*fetch_response.body.lock().unwrap() == ResponseBody::Done(vec![]));
} }
@ -132,7 +138,7 @@ fn test_fetch_data() {
let request = Request::new(url, Some(origin), false); let request = Request::new(url, Some(origin), false);
request.same_origin_data.set(true); request.same_origin_data.set(true);
let expected_resp_body = "<p>Servo</p>".to_owned(); let expected_resp_body = "<p>Servo</p>".to_owned();
let fetch_response = fetch(Rc::new(request), &mut None, HttpState::new()); let fetch_response = fetch(Rc::new(request), &mut None, new_fetch_context());
assert!(!fetch_response.is_network_error()); assert!(!fetch_response.is_network_error());
assert_eq!(fetch_response.headers.len(), 1); assert_eq!(fetch_response.headers.len(), 1);
@ -161,7 +167,7 @@ fn test_fetch_file() {
let request = Request::new(url, Some(origin), false); let request = Request::new(url, Some(origin), false);
request.same_origin_data.set(true); request.same_origin_data.set(true);
let fetch_response = fetch(Rc::new(request), &mut None, HttpState::new()); let fetch_response = fetch(Rc::new(request), &mut None, new_fetch_context());
assert!(!fetch_response.is_network_error()); assert!(!fetch_response.is_network_error());
assert_eq!(fetch_response.headers.len(), 1); assert_eq!(fetch_response.headers.len(), 1);
let content_type: &ContentType = fetch_response.headers.get().unwrap(); let content_type: &ContentType = fetch_response.headers.get().unwrap();
@ -205,7 +211,7 @@ fn test_cors_preflight_fetch() {
request.mode = RequestMode::CORSMode; request.mode = RequestMode::CORSMode;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
assert!(!fetch_response.is_network_error()); assert!(!fetch_response.is_network_error());
@ -245,8 +251,8 @@ fn test_cors_preflight_cache_fetch() {
let wrapped_request0 = Rc::new(request.clone()); let wrapped_request0 = Rc::new(request.clone());
let wrapped_request1 = Rc::new(request); let wrapped_request1 = Rc::new(request);
let fetch_response0 = fetch_with_cors_cache(wrapped_request0.clone(), &mut cache, &mut None, HttpState::new()); let fetch_response0 = fetch_with_cors_cache(wrapped_request0.clone(), &mut cache, &mut None, new_fetch_context());
let fetch_response1 = fetch_with_cors_cache(wrapped_request1.clone(), &mut cache, &mut None, HttpState::new()); let fetch_response1 = fetch_with_cors_cache(wrapped_request1.clone(), &mut cache, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
assert!(!fetch_response0.is_network_error() && !fetch_response1.is_network_error()); assert!(!fetch_response0.is_network_error() && !fetch_response1.is_network_error());
@ -294,7 +300,7 @@ fn test_cors_preflight_fetch_network_error() {
request.mode = RequestMode::CORSMode; request.mode = RequestMode::CORSMode;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
assert!(fetch_response.is_network_error()); assert!(fetch_response.is_network_error());
@ -317,7 +323,7 @@ fn test_fetch_response_is_basic_filtered() {
*request.referer.borrow_mut() = Referer::NoReferer; *request.referer.borrow_mut() = Referer::NoReferer;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
assert!(!fetch_response.is_network_error()); assert!(!fetch_response.is_network_error());
@ -365,7 +371,7 @@ fn test_fetch_response_is_cors_filtered() {
request.mode = RequestMode::CORSMode; request.mode = RequestMode::CORSMode;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
assert!(!fetch_response.is_network_error()); assert!(!fetch_response.is_network_error());
@ -398,7 +404,7 @@ fn test_fetch_response_is_opaque_filtered() {
*request.referer.borrow_mut() = Referer::NoReferer; *request.referer.borrow_mut() = Referer::NoReferer;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
assert!(!fetch_response.is_network_error()); assert!(!fetch_response.is_network_error());
@ -448,7 +454,7 @@ fn test_fetch_response_is_opaque_redirect_filtered() {
request.redirect_mode.set(RedirectMode::Manual); request.redirect_mode.set(RedirectMode::Manual);
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
assert!(!fetch_response.is_network_error()); assert!(!fetch_response.is_network_error());
@ -486,7 +492,7 @@ fn test_fetch_with_local_urls_only() {
request.local_urls_only = true; request.local_urls_only = true;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
fetch(wrapped_request, &mut None, HttpState::new()) fetch(wrapped_request, &mut None, new_fetch_context())
}; };
let local_url = Url::parse("about:blank").unwrap(); let local_url = Url::parse("about:blank").unwrap();
@ -525,7 +531,7 @@ fn setup_server_and_fetch(message: &'static [u8], redirect_cap: u32) -> Response
*request.referer.borrow_mut() = Referer::NoReferer; *request.referer.borrow_mut() = Referer::NoReferer;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request, &mut None, HttpState::new()); let fetch_response = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
fetch_response fetch_response
} }
@ -611,7 +617,7 @@ fn test_fetch_redirect_updates_method_runner(tx: Sender<bool>, status_code: Stat
*request.method.borrow_mut() = method; *request.method.borrow_mut() = method;
let wrapped_request = Rc::new(request); let wrapped_request = Rc::new(request);
let _ = fetch(wrapped_request, &mut None, HttpState::new()); let _ = fetch(wrapped_request, &mut None, new_fetch_context());
let _ = server.close(); let _ = server.close();
} }

View file

@ -1,5 +0,0 @@
[send-authentication-basic-setrequestheader-existing-session.htm]
type: testharness
[XMLHttpRequest: send() - "Basic" authenticated request using setRequestHeader() when there is an existing session]
expected: FAIL

View file

@ -1,5 +0,0 @@
[send-authentication-basic.htm]
type: testharness
[XMLHttpRequest: send() - "Basic" authenticated requests with user name and password passed to open()]
expected: FAIL

View file

@ -0,0 +1,8 @@
[send-conditional.htm]
type: testharness
[XMLHttpRequest: send() - conditional requests (tag)]
expected: FAIL
[XMLHttpRequest: send() - conditional requests (date)]
expected: FAIL

View file

@ -15,9 +15,12 @@
[charset given but wrong, fix it (known MIME, bogus charset)] [charset given but wrong, fix it (known MIME, bogus charset)]
expected: FAIL expected: FAIL
[charset given but wrong, fix it (known MIME, actual charset)]
expected: FAIL
[If multiple charset parameters are given, all should be rewritten] [If multiple charset parameters are given, all should be rewritten]
expected: FAIL expected: FAIL
[Correct text/plain MIME with charset]
expected: FAIL
[charset given but wrong, fix it (known MIME, actual charset)]
expected: FAIL

View file

@ -1,5 +0,0 @@
[send-entity-body-get-head.htm]
type: testharness
[XMLHttpRequest: send() - non-empty data argument and GET/HEAD (HEAD)]
expected: FAIL

View file

@ -0,0 +1,14 @@
[send-redirect-to-cors.htm]
type: testharness
[XMLHttpRequest: send() - Redirect to CORS-enabled resource (301)]
expected: FAIL
[XMLHttpRequest: send() - Redirect to CORS-enabled resource (302)]
expected: FAIL
[XMLHttpRequest: send() - Redirect to CORS-enabled resource (303)]
expected: FAIL
[XMLHttpRequest: send() - Redirect to CORS-enabled resource (307)]
expected: FAIL

View file

@ -1,14 +0,0 @@
[send-redirect-to-non-cors.htm]
type: testharness
[XMLHttpRequest: send() - Redirect to cross-origin resource, not CORS-enabled (301)]
expected: FAIL
[XMLHttpRequest: send() - Redirect to cross-origin resource, not CORS-enabled (302)]
expected: FAIL
[XMLHttpRequest: send() - Redirect to cross-origin resource, not CORS-enabled (303)]
expected: FAIL
[XMLHttpRequest: send() - Redirect to cross-origin resource, not CORS-enabled (307)]
expected: FAIL

View file

@ -33,6 +33,3 @@
[Allow origin: [tab\]http://web-platform.test:8000] [Allow origin: [tab\]http://web-platform.test:8000]
expected: FAIL expected: FAIL
[Disallow origin: http://web-platform.test:8000/]
expected: FAIL

View file

@ -33,12 +33,3 @@
[Allow origin: [tab\]http://web-platform.test:8000] [Allow origin: [tab\]http://web-platform.test:8000]
expected: FAIL expected: FAIL
[Disallow origin: http://web-platform.test:8000/]
expected: FAIL
[Disallow multiple headers (, *)]
expected: FAIL
[Disallow multiple headers (*, )]
expected: FAIL

View file

@ -1,17 +1,8 @@
[preflight-cache.htm] [preflight-cache.htm]
type: testharness type: testharness
[Test preflight]
expected: FAIL
[preflight for x-print should be cached] [preflight for x-print should be cached]
expected: FAIL expected: FAIL
[age = 0, should not be cached]
expected: FAIL
[age = -1, should not be cached]
expected: FAIL
[preflight first request, second from cache, wait, third should preflight again] [preflight first request, second from cache, wait, third should preflight again]
expected: FAIL expected: FAIL

View file

@ -62,24 +62,12 @@
[local (*) to remote (http://web-platform.test:8000), expect origin=http://web-platform.test:8000] [local (*) to remote (http://web-platform.test:8000), expect origin=http://web-platform.test:8000]
expected: FAIL expected: FAIL
[local (*) to remote (null), expect to fail]
expected: FAIL
[local (*) to remote (none), expect to fail]
expected: FAIL
[local (http://web-platform.test:8000) to remote (*), expect origin=http://web-platform.test:8000] [local (http://web-platform.test:8000) to remote (*), expect origin=http://web-platform.test:8000]
expected: FAIL expected: FAIL
[local (http://web-platform.test:8000) to remote (http://web-platform.test:8000), expect origin=http://web-platform.test:8000] [local (http://web-platform.test:8000) to remote (http://web-platform.test:8000), expect origin=http://web-platform.test:8000]
expected: FAIL expected: FAIL
[local (http://web-platform.test:8000) to remote (null), expect to fail]
expected: FAIL
[local (http://web-platform.test:8000) to remote (none), expect to fail]
expected: FAIL
[local (null) to remote (*), expect origin=http://web-platform.test:8000] [local (null) to remote (*), expect origin=http://web-platform.test:8000]
expected: FAIL expected: FAIL
@ -110,27 +98,3 @@
[remote (http://web-platform.test:8000) to remote2 (null), expect origin=null] [remote (http://web-platform.test:8000) to remote2 (null), expect origin=null]
expected: FAIL expected: FAIL
[remote (http://www1.web-platform.test:8000) to remote (*), expect to fail]
expected: FAIL
[remote (null) to remote2 (*), expect to fail]
expected: FAIL
[remote (none) to remote2 (*), expect to fail]
expected: FAIL
[remote (none) to remote2 (*), expect to fail]
expected: FAIL
[remote (null) to remote (*), expect to fail]
expected: FAIL
[remote (none) to remote (*), expect to fail]
expected: FAIL
[remote (none) to local (*), expect to fail]
expected: FAIL
[remote (null) to local (*), expect to fail]
expected: FAIL

View file

@ -1,8 +0,0 @@
[redirect-preflight-2.htm]
type: testharness
[Same-origin custom-header request, redirect to cross-origin succeeds after doing a preflight]
expected: FAIL
[Same-origin custom-header request, redirect to cross-origin fails after doing a non-successful preflight]
expected: FAIL

View file

@ -9,6 +9,3 @@
[getResponse: don't expose x-nonexposed] [getResponse: don't expose x-nonexposed]
expected: FAIL expected: FAIL
[getAllResponseHeaders: don't expose x-nonexposed]
expected: FAIL