Implement default values for RequestInit

This commit is contained in:
Keith Yeung 2016-07-06 22:23:22 +08:00
parent 5729a4a3d4
commit 2bb95989c5
2 changed files with 30 additions and 52 deletions

View file

@ -6,6 +6,7 @@ use hyper::header::Headers;
use hyper::method::Method; use hyper::method::Method;
use msg::constellation_msg::{PipelineId, ReferrerPolicy}; use msg::constellation_msg::{PipelineId, ReferrerPolicy};
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::default::Default;
use std::mem::swap; use std::mem::swap;
use url::{Origin as UrlOrigin, Url}; use url::{Origin as UrlOrigin, Url};
@ -20,7 +21,7 @@ pub enum Initiator {
} }
/// A request [type](https://fetch.spec.whatwg.org/#concept-request-type) /// A request [type](https://fetch.spec.whatwg.org/#concept-request-type)
#[derive(Copy, Clone, PartialEq, HeapSizeOf)] #[derive(Copy, Clone, PartialEq, Serialize, Deserialize, HeapSizeOf)]
pub enum Type { pub enum Type {
None, Audio, Font, Image, None, Audio, Font, Image,
Script, Style, Track, Video Script, Style, Track, Video
@ -103,7 +104,7 @@ pub enum Window {
} }
/// [CORS settings attribute](https://html.spec.whatwg.org/multipage/#attr-crossorigin-anonymous) /// [CORS settings attribute](https://html.spec.whatwg.org/multipage/#attr-crossorigin-anonymous)
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq, Serialize, Deserialize)]
pub enum CORSSettings { pub enum CORSSettings {
Anonymous, Anonymous,
UseCredentials UseCredentials
@ -122,6 +123,7 @@ pub struct RequestInit {
pub same_origin_data: bool, pub same_origin_data: bool,
pub body: Option<Vec<u8>>, pub body: Option<Vec<u8>>,
// TODO: client object // TODO: client object
pub type_: Type,
pub destination: Destination, pub destination: Destination,
pub synchronous: bool, pub synchronous: bool,
pub mode: RequestMode, pub mode: RequestMode,
@ -137,6 +139,30 @@ pub struct RequestInit {
pub pipeline_id: Option<PipelineId>, pub pipeline_id: Option<PipelineId>,
} }
impl Default for RequestInit {
fn default() -> RequestInit {
RequestInit {
method: Method::Get,
url: Url::parse("about:blank").unwrap(),
headers: Headers::new(),
unsafe_request: false,
same_origin_data: false,
body: None,
type_: Type::None,
destination: Destination::None,
synchronous: false,
mode: RequestMode::NoCORS,
use_cors_preflight: false,
credentials_mode: CredentialsMode::Omit,
use_url_credentials: false,
origin: Url::parse("about:blank").unwrap(),
referrer_url: None,
referrer_policy: None,
pipeline_id: None,
}
}
}
/// 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
#[derive(Clone, HeapSizeOf)] #[derive(Clone, HeapSizeOf)]
pub struct Request { pub struct Request {
@ -232,6 +258,7 @@ impl Request {
req.unsafe_request = init.unsafe_request; req.unsafe_request = init.unsafe_request;
req.same_origin_data.set(init.same_origin_data); req.same_origin_data.set(init.same_origin_data);
*req.body.borrow_mut() = init.body; *req.body.borrow_mut() = init.body;
req.type_ = init.type_;
req.destination = init.destination; req.destination = init.destination;
req.synchronous = init.synchronous; req.synchronous = init.synchronous;
req.mode = init.mode; req.mode = init.mode;
@ -248,56 +275,6 @@ impl Request {
req req
} }
/// https://html.spec.whatwg.org/multipage/#create-a-potential-cors-request
pub fn potential_cors_request(url: Url,
cors_attribute_state: Option<CORSSettings>,
is_service_worker_global_scope: bool,
same_origin_fallback: bool,
pipeline_id: Option<PipelineId>) -> Request {
Request {
method: RefCell::new(Method::Get),
local_urls_only: false,
sandboxed_storage_area_urls: false,
headers: RefCell::new(Headers::new()),
unsafe_request: false,
body: RefCell::new(None),
is_service_worker_global_scope: is_service_worker_global_scope,
window: Cell::new(Window::Client),
keep_alive: Cell::new(false),
skip_service_worker: Cell::new(false),
initiator: Initiator::None,
type_: Type::None,
destination: Destination::None,
origin: RefCell::new(Origin::Client),
omit_origin_header: Cell::new(false),
same_origin_data: Cell::new(false),
referrer: RefCell::new(Referrer::Client),
referrer_policy: Cell::new(None),
synchronous: false,
// Step 1-2
mode: match cors_attribute_state {
Some(_) => RequestMode::CORSMode,
None if same_origin_fallback => RequestMode::SameOrigin,
None => RequestMode::NoCORS
},
use_cors_preflight: false,
// Step 3-4
credentials_mode: match cors_attribute_state {
Some(CORSSettings::Anonymous) => CredentialsMode::CredentialsSameOrigin,
_ => CredentialsMode::Include,
},
use_url_credentials: true,
cache_mode: Cell::new(CacheMode::Default),
redirect_mode: Cell::new(RedirectMode::Follow),
integrity_metadata: RefCell::new(String::new()),
url_list: RefCell::new(vec![url]),
redirect_count: Cell::new(0),
response_tainting: Cell::new(ResponseTainting::Basic),
pipeline_id: Cell::new(pipeline_id),
done: Cell::new(false)
}
}
pub fn url(&self) -> Url { pub fn url(&self) -> Url {
self.url_list.borrow().first().unwrap().clone() self.url_list.borrow().first().unwrap().clone()
} }

View file

@ -602,6 +602,7 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
referrer_url: self.referrer_url.clone(), referrer_url: self.referrer_url.clone(),
referrer_policy: self.referrer_policy.clone(), referrer_policy: self.referrer_policy.clone(),
pipeline_id: self.pipeline_id(), pipeline_id: self.pipeline_id(),
.. RequestInit::default()
}; };
if bypass_cross_origin_check { if bypass_cross_origin_check {