aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2014-08-03 22:48:02 +0530
committerManish Goregaokar <manishsmail@gmail.com>2014-08-03 22:48:02 +0530
commit405ee65708ff5312afa3427972d4ade98b63c983 (patch)
tree2cbb2d28900595f9fdd3c8839fcaeac7511873ad /src
parentad86d0beae0723c9e511455b62a2127ecb0e6dfe (diff)
parent7a2627f795ed27be722acf4694428d5d9ecc7ab4 (diff)
downloadservo-405ee65708ff5312afa3427972d4ade98b63c983.tar.gz
servo-405ee65708ff5312afa3427972d4ade98b63c983.zip
Merge pull request #2962 from Manishearth/fetch
Start of fetch module; r=jdm
Diffstat (limited to 'src')
-rw-r--r--src/components/net/fetch/request.rs108
-rw-r--r--src/components/net/fetch/response.rs108
-rw-r--r--src/components/net/net.rs6
3 files changed, 222 insertions, 0 deletions
diff --git a/src/components/net/fetch/request.rs b/src/components/net/fetch/request.rs
new file mode 100644
index 00000000000..2de606a1a31
--- /dev/null
+++ b/src/components/net/fetch/request.rs
@@ -0,0 +1,108 @@
+/* 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/. */
+
+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,
+ NoCORS,
+ CORSMode,
+ ForcedPreflightMode
+}
+
+/// Request [credentials mode](http://fetch.spec.whatwg.org/#concept-request-credentials-mode)
+pub enum CredentialsMode {
+ Omit,
+ CredentialsSameOrigin,
+ Include
+}
+
+/// [Response tainting](http://fetch.spec.whatwg.org/#concept-request-response-tainting)
+pub enum ResponseTainting {
+ Basic,
+ CORSTainting,
+ Opaque
+}
+
+/// A [Request](http://fetch.spec.whatwg.org/#requests) as defined by the Fetch spec
+pub struct Request {
+ pub method: Method,
+ pub url: Url,
+ pub headers: HeaderCollection,
+ pub unsafe_request: bool,
+ pub body: Option<Vec<u8>>,
+ 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<Url>,
+ 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 {
+ pub 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
+ }
+ }
+}
diff --git a/src/components/net/fetch/response.rs b/src/components/net/fetch/response.rs
new file mode 100644
index 00000000000..ee98f388f7b
--- /dev/null
+++ b/src/components/net/fetch/response.rs
@@ -0,0 +1,108 @@
+/* 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/. */
+
+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, PartialEq)]
+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<TerminationReason>,
+ pub url: Option<Url>,
+ pub status: Status,
+ pub headers: HeaderCollection,
+ pub body: Option<Vec<u8>>,
+ /// [Internal response](http://fetch.spec.whatwg.org/#concept-internal-response), only used if the Response is a filtered response
+ pub internal_response: Option<Box<Response>>,
+}
+
+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 {
+ assert!(filter_type != Error);
+ assert!(filter_type != Default);
+ 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
+ }
+}
diff --git a/src/components/net/net.rs b/src/components/net/net.rs
index 454c8295c95..497215e4ba2 100644
--- a/src/components/net/net.rs
+++ b/src/components/net/net.rs
@@ -37,3 +37,9 @@ 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;
+ pub mod response;
+}