From c80b8af8cceb1ee78d46e783e18cce6b06409cc3 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 30 Jul 2014 18:46:25 +0530 Subject: [PATCH 1/4] Stubs for fetch module --- src/components/net/fetch/request.rs | 5 +++++ src/components/net/fetch/response.rs | 5 +++++ src/components/net/net.rs | 4 ++++ 3 files changed, 14 insertions(+) create mode 100644 src/components/net/fetch/request.rs create mode 100644 src/components/net/fetch/response.rs diff --git a/src/components/net/fetch/request.rs b/src/components/net/fetch/request.rs new file mode 100644 index 00000000000..12c21ed4427 --- /dev/null +++ b/src/components/net/fetch/request.rs @@ -0,0 +1,5 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +pub struct Request; \ No newline at end of file diff --git a/src/components/net/fetch/response.rs b/src/components/net/fetch/response.rs new file mode 100644 index 00000000000..f6b926bb725 --- /dev/null +++ b/src/components/net/fetch/response.rs @@ -0,0 +1,5 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + pub struct Response; \ No newline at end of file diff --git a/src/components/net/net.rs b/src/components/net/net.rs index 9b9692184e7..d796672244f 100644 --- a/src/components/net/net.rs +++ b/src/components/net/net.rs @@ -39,3 +39,7 @@ pub mod image_cache_task; pub mod local_image_cache; pub mod resource_task; +pub mod fetch { + pub mod request; + pub mod response; +} From 56e735ee7931096b8c63b3d064d5eb0fcf409665 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 30 Jul 2014 20:31:30 +0530 Subject: [PATCH 2/4] Definition of Request and related enums --- src/components/net/fetch/request.rs | 107 +++++++++++++++++++++++++++- src/components/net/net.rs | 1 + 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/src/components/net/fetch/request.rs b/src/components/net/fetch/request.rs index 12c21ed4427..4c5e2d55cdf 100644 --- a/src/components/net/fetch/request.rs +++ b/src/components/net/fetch/request.rs @@ -2,4 +2,109 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -pub struct Request; \ No newline at end of file +use url::Url; +use http::method::{Get, Method}; +use http::headers::request::HeaderCollection; + +/// A [request context](http://fetch.spec.whatwg.org/#concept-request-context) +pub enum Context { + Audio, Beacon, CSPreport, Download, Embed, Eventsource, + Favicon, Fetch, Font, Form, Frame, Hyperlink, IFrame, Image, + ImageSet, Import, Internal, Location, Manifest, Object, Ping, + Plugin, Prefetch, Script, ServiceWorker, SharedWorker, Subresource, + Style, Track, Video, Worker, XMLHttpRequest, XSLT +} + +/// A [request context frame type](http://fetch.spec.whatwg.org/#concept-request-context-frame-type) +pub enum ContextFrameType { + Auxiliary, + TopLevel, + Nested, + ContextNone +} + +/// A [referer](http://fetch.spec.whatwg.org/#concept-request-referrer) +pub enum Referer { + RefererNone, + Client, + RefererUrl(Url) +} + +/// A [request mode](http://fetch.spec.whatwg.org/#concept-request-mode) +pub enum RequestMode { + SameOrigin, // same-origin + NoCORS, // No CORS + CORSMode, // CORS + ForcedPreflightMode // CORS-with-forced-preflight +} + +/// Request [credentials mode](http://fetch.spec.whatwg.org/#concept-request-credentials-mode) +pub enum CredentialsMode { + Omit, // omit + CredentialsSameOrigin, // same-origin + Include // include +} + + +// [Response tainting](http://fetch.spec.whatwg.org/#concept-request-response-tainting) +pub enum ResponseTainting { + Basic, // basic + CORSTainting, // CORS + Opaque // Opaque +} + +/// A Request as defined by the [Fetch spec](http://fetch.spec.whatwg.org/#requests) +/// +pub struct Request { + pub method: Method, + pub url: Url, + pub headers: HeaderCollection, + pub unsafe_request: bool, + pub body: Option>, + pub preserve_content_codings: bool, + // pub client: GlobalRef, // XXXManishearth copy over only the relevant fields of the global scope, + // not the entire scope to avoid the libscript dependency + pub skip_service_worker: bool, + pub context: Context, + pub context_frame_type: ContextFrameType, + pub origin: Option, + pub force_origin_header: bool, + pub same_origin_data: bool, + pub referer: Referer, + pub authentication: bool, + pub sync: bool, + pub mode: RequestMode, + pub credentials_mode: CredentialsMode, + pub use_url_credentials: bool, + pub manual_redirect: bool, + pub redirect_count: uint, + pub response_tainting: ResponseTainting +} + +impl Request { + fn new(url: Url, context: Context) -> Request { + Request { + method: Get, + url: url, + headers: HeaderCollection::new(), + unsafe_request: false, + body: None, + preserve_content_codings: false, + skip_service_worker: false, + context: context, + context_frame_type: ContextNone, + origin: None, + force_origin_header: false, + same_origin_data: false, + referer: Client, + authentication: false, + sync: false, + mode: NoCORS, + credentials_mode: Omit, + use_url_credentials: false, + manual_redirect: false, + redirect_count: 0, + response_tainting: Basic + } + } +} \ No newline at end of file diff --git a/src/components/net/net.rs b/src/components/net/net.rs index d796672244f..c0eb24e1176 100644 --- a/src/components/net/net.rs +++ b/src/components/net/net.rs @@ -40,6 +40,7 @@ pub mod local_image_cache; pub mod resource_task; pub mod fetch { + #![allow(dead_code)] // XXXManishearth this is only temporary until the Fetch mod starts being used pub mod request; pub mod response; } From e678943939ba3cff5ff0c7f3555d948f3d220454 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 30 Jul 2014 23:28:49 +0530 Subject: [PATCH 3/4] Definition of Response and related enums/concepts --- src/components/net/fetch/request.rs | 5 +- src/components/net/fetch/response.rs | 103 ++++++++++++++++++++++++++- src/components/net/net.rs | 1 + 3 files changed, 105 insertions(+), 4 deletions(-) diff --git a/src/components/net/fetch/request.rs b/src/components/net/fetch/request.rs index 4c5e2d55cdf..069c30541ea 100644 --- a/src/components/net/fetch/request.rs +++ b/src/components/net/fetch/request.rs @@ -53,8 +53,7 @@ pub enum ResponseTainting { Opaque // Opaque } -/// A Request as defined by the [Fetch spec](http://fetch.spec.whatwg.org/#requests) -/// +/// A [Request](http://fetch.spec.whatwg.org/#requests) as defined by the Fetch spec pub struct Request { pub method: Method, pub url: Url, @@ -82,7 +81,7 @@ pub struct Request { } impl Request { - fn new(url: Url, context: Context) -> Request { + pub fn new(url: Url, context: Context) -> Request { Request { method: Get, url: url, diff --git a/src/components/net/fetch/response.rs b/src/components/net/fetch/response.rs index f6b926bb725..57a44c468f6 100644 --- a/src/components/net/fetch/response.rs +++ b/src/components/net/fetch/response.rs @@ -2,4 +2,105 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - pub struct Response; \ No newline at end of file +use url::Url; +use http::status::{Status, UnregisteredStatus}; +use StatusOk = http::status::Ok; +use http::headers::HeaderEnum; +use http::headers::response::HeaderCollection; +use std::ascii::OwnedStrAsciiExt; + +// [Response type](http://fetch.spec.whatwg.org/#concept-response-type) +#[deriving(Clone)] +pub enum ResponseType { + Basic, + CORS, + Default, + Error, + Opaque +} + +// [Response termination reason](http://fetch.spec.whatwg.org/#concept-response-termination-reason) +#[deriving(Clone)] +pub enum TerminationReason { + EndUserAbort, + Fatal, + Timeout +} + +// A [Response](http://fetch.spec.whatwg.org/#concept-response) as defined by the Fetch spec +#[deriving(Clone)] +pub struct Response { + pub response_type: ResponseType, + pub termination_reason: Option, + pub url: Option, + pub status: Status, + pub headers: HeaderCollection, + pub body: Option>, + /// [Internal response](http://fetch.spec.whatwg.org/#concept-internal-response), only used if the Response is a filtered response + pub internal_response: Option>, +} + +impl Response { + pub fn new() -> Response { + Response { + response_type: Default, + termination_reason: None, + url: None, + status: StatusOk, + headers: HeaderCollection::new(), + body: None, + internal_response: None + } + } + + pub fn is_network_error(&self) -> bool { + match self.response_type { + Error => true, + _ => false + } + } + + /// Convert to a filtered response, of type `filter_type` + /// Do not use with type Error or Default + pub fn to_filtered(self, filter_type: ResponseType) -> Response { + if self.is_network_error() { + return self; + } + let old_headers = self.headers.clone(); + let mut response = self.clone(); + response.internal_response = Some(box self); + match filter_type { + Default | Error => unreachable!(), + Basic => { + let mut headers = HeaderCollection::new(); + for h in old_headers.iter() { + match h.header_name().into_ascii_lower().as_slice() { + "set-cookie" | "set-cookie2" => {}, + _ => headers.insert(h) + } + } + response.headers = headers; + response.response_type = filter_type; + }, + CORS => { + let mut headers = HeaderCollection::new(); + for h in old_headers.iter() { + match h.header_name().into_ascii_lower().as_slice() { + "cache-control" | "content-language" | + "content-type" | "expires" | "last-modified" | "Pragma" => {}, + // XXXManishearth handle Access-Control-Expose-Headers + _ => headers.insert(h) + } + } + response.headers = headers; + response.response_type = filter_type; + }, + Opaque => { + response.headers = HeaderCollection::new(); + response.status = UnregisteredStatus(0, "".to_string()); + response.body = None; + } + } + response + } +} \ No newline at end of file diff --git a/src/components/net/net.rs b/src/components/net/net.rs index c0eb24e1176..d528b151345 100644 --- a/src/components/net/net.rs +++ b/src/components/net/net.rs @@ -39,6 +39,7 @@ pub mod image_cache_task; pub mod local_image_cache; pub mod resource_task; +/// An implementation of the [Fetch spec](http://fetch.spec.whatwg.org/) pub mod fetch { #![allow(dead_code)] // XXXManishearth this is only temporary until the Fetch mod starts being used pub mod request; From 7a2627f795ed27be722acf4694428d5d9ecc7ab4 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sun, 3 Aug 2014 21:39:41 +0530 Subject: [PATCH 4/4] Fix stuff in fetch crate --- src/components/net/fetch/request.rs | 27 +++++++++++++-------------- src/components/net/fetch/response.rs | 10 ++++++---- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/components/net/fetch/request.rs b/src/components/net/fetch/request.rs index 069c30541ea..2de606a1a31 100644 --- a/src/components/net/fetch/request.rs +++ b/src/components/net/fetch/request.rs @@ -32,25 +32,24 @@ pub enum Referer { /// A [request mode](http://fetch.spec.whatwg.org/#concept-request-mode) pub enum RequestMode { - SameOrigin, // same-origin - NoCORS, // No CORS - CORSMode, // CORS - ForcedPreflightMode // CORS-with-forced-preflight + SameOrigin, + NoCORS, + CORSMode, + ForcedPreflightMode } /// Request [credentials mode](http://fetch.spec.whatwg.org/#concept-request-credentials-mode) pub enum CredentialsMode { - Omit, // omit - CredentialsSameOrigin, // same-origin - Include // include + Omit, + CredentialsSameOrigin, + Include } - -// [Response tainting](http://fetch.spec.whatwg.org/#concept-request-response-tainting) +/// [Response tainting](http://fetch.spec.whatwg.org/#concept-request-response-tainting) pub enum ResponseTainting { - Basic, // basic - CORSTainting, // CORS - Opaque // Opaque + Basic, + CORSTainting, + Opaque } /// A [Request](http://fetch.spec.whatwg.org/#requests) as defined by the Fetch spec @@ -104,6 +103,6 @@ impl Request { manual_redirect: false, redirect_count: 0, response_tainting: Basic - } + } } -} \ No newline at end of file +} diff --git a/src/components/net/fetch/response.rs b/src/components/net/fetch/response.rs index 57a44c468f6..ee98f388f7b 100644 --- a/src/components/net/fetch/response.rs +++ b/src/components/net/fetch/response.rs @@ -10,7 +10,7 @@ use http::headers::response::HeaderCollection; use std::ascii::OwnedStrAsciiExt; // [Response type](http://fetch.spec.whatwg.org/#concept-response-type) -#[deriving(Clone)] +#[deriving(Clone, PartialEq)] pub enum ResponseType { Basic, CORS, @@ -37,7 +37,7 @@ pub struct Response { pub headers: HeaderCollection, pub body: Option>, /// [Internal response](http://fetch.spec.whatwg.org/#concept-internal-response), only used if the Response is a filtered response - pub internal_response: Option>, + pub internal_response: Option>, } impl Response { @@ -60,9 +60,11 @@ impl Response { } } - /// Convert to a filtered response, of type `filter_type` + /// Convert to a filtered response, of type `filter_type`. /// Do not use with type Error or Default pub fn to_filtered(self, filter_type: ResponseType) -> Response { + assert!(filter_type != Error); + assert!(filter_type != Default); if self.is_network_error() { return self; } @@ -103,4 +105,4 @@ impl Response { } response } -} \ No newline at end of file +}