Remove all internal mutability from Request

This commit is contained in:
Anthony Ramine 2017-04-01 00:31:00 +02:00
parent f42a63baea
commit cb2eb81208
11 changed files with 362 additions and 376 deletions

View file

@ -66,7 +66,7 @@ impl CorsCacheEntry {
}
fn match_headers(cors_cache: &CorsCacheEntry, cors_req: &Request) -> bool {
cors_cache.origin == *cors_req.origin.borrow() &&
cors_cache.origin == cors_req.origin &&
cors_cache.url == cors_req.current_url() &&
(cors_cache.credentials || cors_req.credentials_mode != CredentialsMode::Include)
}
@ -97,7 +97,7 @@ impl CorsCache {
pub fn clear(&mut self, request: &Request) {
let CorsCache(buf) = self.clone();
let new_buf: Vec<CorsCacheEntry> =
buf.into_iter().filter(|e| e.origin == *request.origin.borrow() &&
buf.into_iter().filter(|e| e.origin == request.origin &&
request.current_url() == e.url).collect();
*self = CorsCache(new_buf);
}
@ -127,7 +127,7 @@ impl CorsCache {
match self.find_entry_by_header(&request, header_name).map(|e| e.max_age = new_max_age) {
Some(_) => true,
None => {
self.insert(CorsCacheEntry::new(request.origin.borrow().clone(), request.current_url(), new_max_age,
self.insert(CorsCacheEntry::new(request.origin.clone(), request.current_url(), new_max_age,
request.credentials_mode == CredentialsMode::Include,
HeaderOrMethod::HeaderData(header_name.to_owned())));
false
@ -149,7 +149,7 @@ impl CorsCache {
match self.find_entry_by_method(&request, method.clone()).map(|e| e.max_age = new_max_age) {
Some(_) => true,
None => {
self.insert(CorsCacheEntry::new(request.origin.borrow().clone(), request.current_url(), new_max_age,
self.insert(CorsCacheEntry::new(request.origin.clone(), request.current_url(), new_max_age,
request.credentials_mode == CredentialsMode::Include,
HeaderOrMethod::MethodData(method)));
false

View file

@ -48,31 +48,31 @@ pub struct FetchContext {
pub type DoneChannel = Option<(Sender<Data>, Receiver<Data>)>;
/// [Fetch](https://fetch.spec.whatwg.org#concept-fetch)
pub fn fetch(request: &Request,
pub fn fetch(request: &mut Request,
target: Target,
context: &FetchContext) {
fetch_with_cors_cache(request, &mut CorsCache::new(), target, context);
}
pub fn fetch_with_cors_cache(request: &Request,
pub fn fetch_with_cors_cache(request: &mut Request,
cache: &mut CorsCache,
target: Target,
context: &FetchContext) {
// Step 1
if request.window.get() == Window::Client {
if request.window == Window::Client {
// TODO: Set window to request's client object if client is a Window object
} else {
request.window.set(Window::NoWindow);
request.window = Window::NoWindow;
}
// Step 2
if *request.origin.borrow() == Origin::Client {
if request.origin == Origin::Client {
// TODO: set request's origin to request's client's origin
unimplemented!()
}
// Step 3
if !request.headers.borrow().has::<Accept>() {
if !request.headers.has::<Accept>() {
let value = match request.type_ {
// Substep 2
_ if request.is_navigation_request() =>
@ -101,11 +101,11 @@ pub fn fetch_with_cors_cache(request: &Request,
};
// Substep 4
request.headers.borrow_mut().set(Accept(value));
request.headers.set(Accept(value));
}
// Step 4
set_default_accept_language(&mut request.headers.borrow_mut());
set_default_accept_language(&mut request.headers);
// Step 5
// TODO: Figure out what a Priority object is
@ -120,7 +120,7 @@ pub fn fetch_with_cors_cache(request: &Request,
}
/// [Main fetch](https://fetch.spec.whatwg.org/#concept-main-fetch)
pub fn main_fetch(request: &Request,
pub fn main_fetch(request: &mut Request,
cache: &mut CorsCache,
cors_flag: bool,
recursive_flag: bool,
@ -158,30 +158,30 @@ pub fn main_fetch(request: &Request,
// currently the clients themselves set referrer policy in RequestInit
// Step 7
let referrer_policy = request.referrer_policy.get().unwrap_or(ReferrerPolicy::NoReferrerWhenDowngrade);
request.referrer_policy.set(Some(referrer_policy));
let referrer_policy = request.referrer_policy.unwrap_or(ReferrerPolicy::NoReferrerWhenDowngrade);
request.referrer_policy = Some(referrer_policy);
// Step 8
{
let mut referrer = request.referrer.borrow_mut();
let referrer_url = match mem::replace(&mut *referrer, Referrer::NoReferrer) {
let referrer_url = match mem::replace(&mut request.referrer, Referrer::NoReferrer) {
Referrer::NoReferrer => None,
Referrer::Client => {
// FIXME(#14507): We should never get this value here; it should
// already have been handled in the script thread.
request.headers.borrow_mut().remove::<RefererHeader>();
request.headers.remove::<RefererHeader>();
None
},
Referrer::ReferrerUrl(url) => {
request.headers.borrow_mut().remove::<RefererHeader>();
determine_request_referrer(&mut *request.headers.borrow_mut(),
request.headers.remove::<RefererHeader>();
let current_url = request.current_url().clone();
determine_request_referrer(&mut request.headers,
referrer_policy,
url,
request.current_url().clone())
current_url)
}
};
if let Some(referrer_url) = referrer_url {
*referrer = Referrer::ReferrerUrl(referrer_url);
request.referrer = Referrer::ReferrerUrl(referrer_url);
}
}
@ -192,7 +192,7 @@ pub fn main_fetch(request: &Request,
.read()
.unwrap()
.is_host_secure(request.current_url().domain().unwrap()) {
request.url_list.borrow_mut().last_mut().unwrap().as_mut_url().set_scheme("https").unwrap();
request.url_list.last_mut().unwrap().as_mut_url().set_scheme("https").unwrap();
}
}
@ -204,7 +204,7 @@ pub fn main_fetch(request: &Request,
Some(response) => response,
None => {
let current_url = request.current_url();
let same_origin = if let Origin::Origin(ref origin) = *request.origin.borrow() {
let same_origin = if let Origin::Origin(ref origin) = request.origin {
*origin == current_url.origin()
} else {
false
@ -221,7 +221,7 @@ pub fn main_fetch(request: &Request,
Response::network_error(NetworkError::Internal("Cross-origin response".into()))
} else if request.mode == RequestMode::NoCors {
request.response_tainting.set(ResponseTainting::Opaque);
request.response_tainting = ResponseTainting::Opaque;
basic_fetch(request, cache, target, done_chan, context)
} else if !matches!(current_url.scheme(), "http" | "https") {
@ -229,9 +229,9 @@ pub fn main_fetch(request: &Request,
} else if request.use_cors_preflight ||
(request.unsafe_request &&
(!is_simple_method(&request.method.borrow()) ||
request.headers.borrow().iter().any(|h| !is_simple_header(&h)))) {
request.response_tainting.set(ResponseTainting::CorsTainting);
(!is_simple_method(&request.method) ||
request.headers.iter().any(|h| !is_simple_header(&h)))) {
request.response_tainting = ResponseTainting::CorsTainting;
let response = http_fetch(request, cache, true, true, false,
target, done_chan, context);
if response.is_network_error() {
@ -240,7 +240,7 @@ pub fn main_fetch(request: &Request,
response
} else {
request.response_tainting.set(ResponseTainting::CorsTainting);
request.response_tainting = ResponseTainting::CorsTainting;
http_fetch(request, cache, true, false, false, target, done_chan, context)
}
}
@ -254,7 +254,7 @@ pub fn main_fetch(request: &Request,
// Step 13
// no need to check if response is a network error, since the type would not be `Default`
let response = if response.response_type == ResponseType::Default {
let response_type = match request.response_tainting.get() {
let response_type = match request.response_tainting {
ResponseTainting::Basic => ResponseType::Basic,
ResponseTainting::CorsTainting => ResponseType::Cors,
ResponseTainting::Opaque => ResponseType::Opaque,
@ -276,7 +276,7 @@ pub fn main_fetch(request: &Request,
// Step 15
if internal_response.url_list.borrow().is_empty() {
*internal_response.url_list.borrow_mut() = request.url_list.borrow().clone();
*internal_response.url_list.borrow_mut() = request.url_list.clone();
}
// Step 16
@ -295,7 +295,7 @@ pub fn main_fetch(request: &Request,
// We check `internal_response` since we did not mutate `response` in the previous step.
let not_network_error = !response.is_network_error() && !internal_response.is_network_error();
if not_network_error && (is_null_body_status(&internal_response.status) ||
match *request.method.borrow() {
match request.method {
Method::Head | Method::Connect => true,
_ => false }) {
// when Fetch is used only asynchronously, we will need to make sure
@ -316,13 +316,13 @@ pub fn main_fetch(request: &Request,
// Step 18
let mut response_loaded = false;
let response = if !response.is_network_error() && *request.integrity_metadata.borrow() != "" {
let response = if !response.is_network_error() && request.integrity_metadata != "" {
// Substep 1
wait_for_response(&response, target, done_chan);
response_loaded = true;
// Substep 2
let ref integrity_metadata = *request.integrity_metadata.borrow();
let ref integrity_metadata = &request.integrity_metadata;
if response.termination_reason.is_none() &&
!is_response_integrity_valid(integrity_metadata, &response) {
Response::network_error(NetworkError::Internal("Subresource integrity validation failed".into()))
@ -347,7 +347,7 @@ pub fn main_fetch(request: &Request,
}
// Step 20
if request.body.borrow().is_some() && matches!(request.current_url().scheme(), "http" | "https") {
if request.body.is_some() && matches!(request.current_url().scheme(), "http" | "https") {
// 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
@ -396,7 +396,7 @@ fn wait_for_response(response: &Response, target: Target, done_chan: &mut DoneCh
}
/// [Basic fetch](https://fetch.spec.whatwg.org#basic-fetch)
fn basic_fetch(request: &Request,
fn basic_fetch(request: &mut Request,
cache: &mut CorsCache,
target: Target,
done_chan: &mut DoneChannel,
@ -417,7 +417,7 @@ fn basic_fetch(request: &Request,
},
"data" => {
if *request.method.borrow() == Method::Get {
if request.method == Method::Get {
match decode(&url) {
Ok((mime, bytes)) => {
let mut response = Response::new(url);
@ -433,7 +433,7 @@ fn basic_fetch(request: &Request,
},
"file" => {
if *request.method.borrow() == Method::Get {
if request.method == Method::Get {
match url.to_file_path() {
Ok(file_path) => {
match File::open(file_path.clone()) {
@ -460,7 +460,7 @@ fn basic_fetch(request: &Request,
"blob" => {
println!("Loading blob {}", url.as_str());
// Step 2.
if *request.method.borrow() != Method::Get {
if request.method != Method::Get {
return Response::network_error(NetworkError::Internal("Unexpected method for blob".into()));
}

View file

@ -476,7 +476,7 @@ fn obtain_response(request_factory: &NetworkHttpRequestFactory,
}
/// [HTTP fetch](https://fetch.spec.whatwg.org#http-fetch)
pub fn http_fetch(request: &Request,
pub fn http_fetch(request: &mut Request,
cache: &mut CorsCache,
cors_flag: bool,
cors_preflight_flag: bool,
@ -494,7 +494,7 @@ pub fn http_fetch(request: &Request,
// nothing to do, since actual_response is a function on response
// Step 3
if !request.skip_service_worker.get() && !request.is_service_worker_global_scope {
if !request.skip_service_worker && !request.is_service_worker_global_scope {
// Substep 1
// TODO (handle fetch unimplemented)
@ -506,9 +506,9 @@ pub fn http_fetch(request: &Request,
if (res.response_type == ResponseType::Opaque &&
request.mode != RequestMode::NoCors) ||
(res.response_type == ResponseType::OpaqueRedirect &&
request.redirect_mode.get() != RedirectMode::Manual) ||
request.redirect_mode != RedirectMode::Manual) ||
(res.url_list.borrow().len() > 1 &&
request.redirect_mode.get() != RedirectMode::Follow) ||
request.redirect_mode != RedirectMode::Follow) ||
res.is_network_error() {
return Response::network_error(NetworkError::Internal("Request failed".into()));
}
@ -521,7 +521,7 @@ pub fn http_fetch(request: &Request,
// Step 4
let credentials = match request.credentials_mode {
CredentialsMode::Include => true,
CredentialsMode::CredentialsSameOrigin if request.response_tainting.get() == ResponseTainting::Basic
CredentialsMode::CredentialsSameOrigin if request.response_tainting == ResponseTainting::Basic
=> true,
_ => false
};
@ -531,11 +531,11 @@ pub fn http_fetch(request: &Request,
// Substep 1
if cors_preflight_flag {
let method_cache_match = cache.match_method(&*request,
request.method.borrow().clone());
request.method.clone());
let method_mismatch = !method_cache_match && (!is_simple_method(&request.method.borrow()) ||
let method_mismatch = !method_cache_match && (!is_simple_method(&request.method) ||
request.use_cors_preflight);
let header_mismatch = request.headers.borrow().iter().any(|view|
let header_mismatch = request.headers.iter().any(|view|
!cache.match_header(&*request, view.name()) && !is_simple_header(&view)
);
@ -550,7 +550,7 @@ pub fn http_fetch(request: &Request,
}
// Substep 2
request.skip_service_worker.set(true);
request.skip_service_worker = true;
// Substep 3
let fetch_result = http_network_or_cache_fetch(request, authentication_fetch_flag,
@ -572,7 +572,7 @@ pub fn http_fetch(request: &Request,
match response.actual_response().status {
// Code 301, 302, 303, 307, 308
status if status.map_or(false, is_redirect_status) => {
response = match request.redirect_mode.get() {
response = match request.redirect_mode {
RedirectMode::Error => Response::network_error(NetworkError::Internal("Redirect mode error".into())),
RedirectMode::Manual => {
response.to_filtered(ResponseType::OpaqueRedirect)
@ -648,7 +648,7 @@ pub fn http_fetch(request: &Request,
}
/// [HTTP redirect fetch](https://fetch.spec.whatwg.org#http-redirect-fetch)
fn http_redirect_fetch(request: &Request,
fn http_redirect_fetch(request: &mut Request,
cache: &mut CorsCache,
response: Response,
cors_flag: bool,
@ -683,12 +683,12 @@ fn http_redirect_fetch(request: &Request,
}
// Step 5
if request.redirect_count.get() >= 20 {
if request.redirect_count >= 20 {
return Response::network_error(NetworkError::Internal("Too many redirects".into()));
}
// Step 6
request.redirect_count.set(request.redirect_count.get() + 1);
request.redirect_count += 1;
// Step 7
let same_origin = location_url.origin()== request.current_url().origin();
@ -705,20 +705,20 @@ fn http_redirect_fetch(request: &Request,
// Step 9
if cors_flag && !same_origin {
*request.origin.borrow_mut() = Origin::Origin(ImmutableOrigin::new_opaque());
request.origin = Origin::Origin(ImmutableOrigin::new_opaque());
}
// Step 10
let status_code = response.actual_response().status.unwrap();
if ((status_code == StatusCode::MovedPermanently || status_code == StatusCode::Found) &&
*request.method.borrow() == Method::Post) ||
request.method == Method::Post) ||
status_code == StatusCode::SeeOther {
*request.method.borrow_mut() = Method::Get;
*request.body.borrow_mut() = None;
request.method = Method::Get;
request.body = None;
}
// Step 11
request.url_list.borrow_mut().push(location_url);
request.url_list.push(location_url);
// Step 12
// TODO implement referrer policy
@ -737,7 +737,7 @@ fn try_immutable_origin_to_hyper_origin(url_origin: &ImmutableOrigin) -> Option<
}
/// [HTTP network or cache fetch](https://fetch.spec.whatwg.org#http-network-or-cache-fetch)
fn http_network_or_cache_fetch(request: &Request,
fn http_network_or_cache_fetch(request: &mut Request,
authentication_fetch_flag: bool,
cors_flag: bool,
done_chan: &mut DoneChannel,
@ -747,28 +747,28 @@ fn http_network_or_cache_fetch(request: &Request,
let request_has_no_window = true;
// Step 2
let http_request;
let mut http_request;
let http_request = if request_has_no_window &&
request.redirect_mode.get() == RedirectMode::Error {
request.redirect_mode == RedirectMode::Error {
request
} else {
// Step 3
// TODO Implement body source
http_request = request.clone();
&http_request
&mut http_request
};
// Step 4
let credentials_flag = match http_request.credentials_mode {
CredentialsMode::Include => true,
CredentialsMode::CredentialsSameOrigin if http_request.response_tainting.get() == ResponseTainting::Basic
CredentialsMode::CredentialsSameOrigin if http_request.response_tainting == ResponseTainting::Basic
=> true,
_ => false
};
let content_length_value = match *http_request.body.borrow() {
let content_length_value = match http_request.body {
None =>
match *http_request.method.borrow() {
match http_request.method {
// Step 6
Method::Post | Method::Put =>
Some(0),
@ -781,16 +781,16 @@ fn http_network_or_cache_fetch(request: &Request,
// Step 8
if let Some(content_length_value) = content_length_value {
http_request.headers.borrow_mut().set(ContentLength(content_length_value));
http_request.headers.set(ContentLength(content_length_value));
}
// Step 9 TODO: needs request's client object
// Step 10
match *http_request.referrer.borrow() {
match http_request.referrer {
Referrer::NoReferrer => (),
Referrer::ReferrerUrl(ref http_request_referrer) =>
http_request.headers.borrow_mut().set(Referer(http_request_referrer.to_string())),
http_request.headers.set(Referer(http_request_referrer.to_string())),
Referrer::Client =>
// it should be impossible for referrer to be anything else during fetching
// https://fetch.spec.whatwg.org/#concept-request-referrer
@ -798,45 +798,45 @@ fn http_network_or_cache_fetch(request: &Request,
};
// Step 11
if !http_request.omit_origin_header.get() {
let method = http_request.method.borrow();
if !http_request.omit_origin_header {
let method = &http_request.method;
if cors_flag || (*method != Method::Get && *method != Method::Head) {
debug_assert!(*http_request.origin.borrow() != Origin::Client);
if let Origin::Origin(ref url_origin) = *http_request.origin.borrow() {
debug_assert!(http_request.origin != Origin::Client);
if let Origin::Origin(ref url_origin) = http_request.origin {
if let Some(hyper_origin) = try_immutable_origin_to_hyper_origin(url_origin) {
http_request.headers.borrow_mut().set(hyper_origin)
http_request.headers.set(hyper_origin)
}
}
}
}
// Step 12
if !http_request.headers.borrow().has::<UserAgent>() {
if !http_request.headers.has::<UserAgent>() {
let user_agent = context.user_agent.clone().into_owned();
http_request.headers.borrow_mut().set(UserAgent(user_agent));
http_request.headers.set(UserAgent(user_agent));
}
match http_request.cache_mode.get() {
match http_request.cache_mode {
// Step 13
CacheMode::Default if is_no_store_cache(&http_request.headers.borrow()) => {
http_request.cache_mode.set(CacheMode::NoStore);
CacheMode::Default if is_no_store_cache(&http_request.headers) => {
http_request.cache_mode = CacheMode::NoStore;
},
// Step 14
CacheMode::NoCache if !http_request.headers.borrow().has::<CacheControl>() => {
http_request.headers.borrow_mut().set(CacheControl(vec![CacheDirective::MaxAge(0)]));
CacheMode::NoCache if !http_request.headers.has::<CacheControl>() => {
http_request.headers.set(CacheControl(vec![CacheDirective::MaxAge(0)]));
},
// Step 15
CacheMode::Reload | CacheMode::NoStore => {
// Substep 1
if !http_request.headers.borrow().has::<Pragma>() {
http_request.headers.borrow_mut().set(Pragma::NoCache);
if !http_request.headers.has::<Pragma>() {
http_request.headers.set(Pragma::NoCache);
}
// Substep 2
if !http_request.headers.borrow().has::<CacheControl>() {
http_request.headers.borrow_mut().set(CacheControl(vec![CacheDirective::NoCache]));
if !http_request.headers.has::<CacheControl>() {
http_request.headers.set(CacheControl(vec![CacheDirective::NoCache]));
}
},
@ -845,17 +845,14 @@ fn http_network_or_cache_fetch(request: &Request,
// Step 16
let current_url = http_request.current_url();
{
let headers = &mut *http_request.headers.borrow_mut();
let host = Host {
hostname: current_url.host_str().unwrap().to_owned(),
port: current_url.port()
};
headers.set(host);
// unlike http_loader, we should not set the accept header
// here, according to the fetch spec
set_default_accept_encoding(headers);
}
let host = Host {
hostname: current_url.host_str().unwrap().to_owned(),
port: current_url.port()
};
http_request.headers.set(host);
// unlike http_loader, we should not set the accept header
// here, according to the fetch spec
set_default_accept_encoding(&mut http_request.headers);
// Step 17
// TODO some of this step can't be implemented yet
@ -864,10 +861,10 @@ fn http_network_or_cache_fetch(request: &Request,
// TODO http://mxr.mozilla.org/servo/source/components/net/http_loader.rs#504
// XXXManishearth http_loader has block_cookies: support content blocking here too
set_request_cookies(&current_url,
&mut *http_request.headers.borrow_mut(),
&mut http_request.headers,
&context.state.cookie_jar);
// Substep 2
if !http_request.headers.borrow().has::<Authorization<String>>() {
if !http_request.headers.has::<Authorization<String>>() {
// Substep 3
let mut authorization_value = None;
@ -890,7 +887,7 @@ fn http_network_or_cache_fetch(request: &Request,
// Substep 6
if let Some(basic) = authorization_value {
http_request.headers.borrow_mut().set(Authorization(basic));
http_request.headers.set(Authorization(basic));
}
}
}
@ -907,8 +904,8 @@ fn http_network_or_cache_fetch(request: &Request,
// Step 21
// TODO have a HTTP cache to check for a completed response
let complete_http_response_from_cache: Option<Response> = None;
if http_request.cache_mode.get() != CacheMode::NoStore &&
http_request.cache_mode.get() != CacheMode::Reload &&
if http_request.cache_mode != CacheMode::NoStore &&
http_request.cache_mode != CacheMode::Reload &&
complete_http_response_from_cache.is_some() {
// TODO Substep 1 and 2. Select a response from HTTP cache.
@ -918,8 +915,8 @@ fn http_network_or_cache_fetch(request: &Request,
};
// Substep 4
if http_request.cache_mode.get() == CacheMode::ForceCache ||
http_request.cache_mode.get() == CacheMode::OnlyIfCached {
if http_request.cache_mode == CacheMode::ForceCache ||
http_request.cache_mode == CacheMode::OnlyIfCached {
// TODO pull response from HTTP cache
// response = http_request
}
@ -939,7 +936,7 @@ fn http_network_or_cache_fetch(request: &Request,
// Step 22
if response.is_none() {
// Substep 1
if http_request.cache_mode.get() == CacheMode::OnlyIfCached {
if http_request.cache_mode == CacheMode::OnlyIfCached {
return Response::network_error(
NetworkError::Internal("Couldn't find response in cache".into()))
}
@ -950,7 +947,7 @@ fn http_network_or_cache_fetch(request: &Request,
// Substep 3
Some((200...303, _)) |
Some((305...399, _)) => {
if !http_request.method.borrow().safe() {
if !http_request.method.safe() {
// TODO Invalidate HTTP cache response
}
},
@ -984,7 +981,7 @@ fn http_network_or_cache_fetch(request: &Request,
// TODO: Spec says requires testing on multiple WWW-Authenticate headers
// Substep 2
if http_request.body.borrow().is_some() {
if http_request.body.is_some() {
// TODO Implement body source
}
@ -1065,13 +1062,13 @@ fn http_network_fetch(request: &Request,
// do not. Once we support other kinds of fetches we'll need to be more fine grained here
// since things like image fetches are classified differently by devtools
let is_xhr = request.destination == Destination::None;
let wrapped_response = obtain_response(&factory, &url, &request.method.borrow(),
&request.headers.borrow(),
&request.body.borrow(), &request.method.borrow(),
&request.pipeline_id.get(), request.redirect_count.get() + 1,
let wrapped_response = obtain_response(&factory, &url, &request.method,
&request.headers,
&request.body, &request.method,
&request.pipeline_id, request.redirect_count + 1,
request_id.as_ref().map(Deref::deref), is_xhr);
let pipeline_id = request.pipeline_id.get();
let pipeline_id = request.pipeline_id;
let (res, msg) = match wrapped_response {
Ok(wrapped_response) => wrapped_response,
Err(error) => return Response::network_error(error),
@ -1082,7 +1079,7 @@ fn http_network_fetch(request: &Request,
response.raw_status = Some((res.response.status_raw().0,
res.response.status_raw().1.as_bytes().to_vec()));
response.headers = res.response.headers.clone();
response.referrer = request.referrer.borrow().to_url().cloned();
response.referrer = request.referrer.to_url().cloned();
let res_body = response.body.clone();
@ -1178,7 +1175,7 @@ fn http_network_fetch(request: &Request,
// TODO this step isn't possible yet (CSP)
// Step 12
if response.is_network_error() && request.cache_mode.get() == CacheMode::NoStore {
if response.is_network_error() && request.cache_mode == CacheMode::NoStore {
// TODO update response in the HTTP cache for request
}
@ -1210,32 +1207,33 @@ fn cors_preflight_fetch(request: &Request,
context: &FetchContext)
-> 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());
*preflight.method.borrow_mut() = Method::Options;
let mut preflight = Request::new(request.current_url(), Some(request.origin.clone()),
request.is_service_worker_global_scope, request.pipeline_id);
preflight.method = Method::Options;
preflight.initiator = request.initiator.clone();
preflight.type_ = request.type_.clone();
preflight.destination = request.destination.clone();
*preflight.referrer.borrow_mut() = request.referrer.borrow().clone();
preflight.referrer_policy.set(request.referrer_policy.get());
preflight.referrer = request.referrer.clone();
preflight.referrer_policy = request.referrer_policy;
// Step 2
preflight.headers.borrow_mut().set::<AccessControlRequestMethod>(
AccessControlRequestMethod(request.method.borrow().clone()));
preflight.headers.set::<AccessControlRequestMethod>(
AccessControlRequestMethod(request.method.clone()));
// Step 3, 4
let mut value = request.headers.borrow().iter()
.filter(|view| !is_simple_header(view))
.map(|view| UniCase(view.name().to_owned()))
.collect::<Vec<UniCase<String>>>();
let mut value = request.headers
.iter()
.filter(|view| !is_simple_header(view))
.map(|view| UniCase(view.name().to_owned()))
.collect::<Vec<UniCase<String>>>();
value.sort();
// Step 5
preflight.headers.borrow_mut().set::<AccessControlRequestHeaders>(
preflight.headers.set::<AccessControlRequestHeaders>(
AccessControlRequestHeaders(value));
// Step 6
let response = http_network_or_cache_fetch(&preflight, false, false, &mut None, context);
let response = http_network_or_cache_fetch(&mut preflight, false, false, &mut None, context);
// Step 7
if cors_check(&request, &response).is_ok() &&
@ -1264,23 +1262,21 @@ fn cors_preflight_fetch(request: &Request,
// Substep 4
if methods.is_empty() && request.use_cors_preflight {
methods = vec![request.method.borrow().clone()];
methods = vec![request.method.clone()];
}
// Substep 5
debug!("CORS check: Allowed methods: {:?}, current method: {:?}",
methods, request.method.borrow());
if methods.iter().all(|method| *method != *request.method.borrow()) &&
!is_simple_method(&*request.method.borrow()) {
methods, request.method);
if methods.iter().all(|method| *method != request.method) &&
!is_simple_method(&request.method) {
return Response::network_error(NetworkError::Internal("CORS method check failed".into()));
}
// Substep 6
debug!("CORS check: Allowed headers: {:?}, current headers: {:?}",
header_names, request.headers.borrow());
debug!("CORS check: Allowed headers: {:?}, current headers: {:?}", header_names, request.headers);
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())) &&
!is_simple_header(hv)) {
if request.headers.iter().any(|ref hv| !set.contains(&UniCase(hv.name().to_owned())) && !is_simple_header(hv)) {
return Response::network_error(NetworkError::Internal("CORS headers check failed".into()));
}
@ -1328,7 +1324,7 @@ fn cors_check(request: &Request, response: &Response) -> Result<(), ()> {
_ => return Err(())
};
match *request.origin.borrow() {
match request.origin {
Origin::Origin(ref o) if o.ascii_serialization() == origin => {},
_ => return Err(())
}

View file

@ -338,7 +338,7 @@ impl CoreResourceManager {
let filemanager = self.filemanager.clone();
thread::Builder::new().name(format!("fetch thread for {}", init.url)).spawn(move || {
let request = Request::from_init(init);
let mut 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?
@ -349,7 +349,7 @@ impl CoreResourceManager {
devtools_chan: dc,
filemanager: filemanager,
};
fetch(&request, &mut sender, &context);
fetch(&mut request, &mut sender, &context);
}).expect("Thread spawning failed");
}