From 8cd572de958864f5ec964d676ffd06757722b800 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 23 Aug 2014 12:13:59 +0530 Subject: [PATCH] Add basic_fetch and a skeleton http_fetch --- src/components/net/fetch/request.rs | 38 ++++++++++++++++++++++ src/components/net/fetch/response.rs | 48 ++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/components/net/fetch/request.rs b/src/components/net/fetch/request.rs index 2de606a1a31..2dfd5572a97 100644 --- a/src/components/net/fetch/request.rs +++ b/src/components/net/fetch/request.rs @@ -5,6 +5,7 @@ use url::Url; use http::method::{Get, Method}; use http::headers::request::HeaderCollection; +use fetch::response::Response; /// A [request context](http://fetch.spec.whatwg.org/#concept-request-context) pub enum Context { @@ -105,4 +106,41 @@ impl Request { response_tainting: Basic } } + + /// [Basic fetch](http://fetch.spec.whatwg.org#basic-fetch) + /// + pub fn basic_fetch(&mut self) -> Response { + match self.url.scheme.as_slice() { + "about" => match self.url.non_relative_scheme_data() { + Some(s) if s.as_slice() == "blank" => { + let mut response = Response::new(); + let _ = response.headers.insert_raw("Content-Type".to_string(), b"text/html;charset=utf-8"); + response + }, + _ => Response::network_error() + }, + "http" | "https" => { + self.http_fetch(false, false, false) + }, + "blob" | "data" | "file" | "ftp" => { + // XXXManishearth handle these + fail!("Unimplemented scheme for Fetch") + }, + + _ => Response::network_error() + } + } + // [Basic fetch](http://fetch.spec.whatwg.org#http-fetch) + pub fn http_fetch(&mut self, _cors_flag: bool, cors_preflight_flag: bool, _authentication_fetch_flag: bool) -> Response { + let response = Response::new(); + // TODO: Service worker fetch + // Step 3 + // Substep 1 + self.skip_service_worker = true; + // Substep 2 + if cors_preflight_flag { + // XXXManishearth stuff goes here + } + response + } } diff --git a/src/components/net/fetch/response.rs b/src/components/net/fetch/response.rs index ee98f388f7b..359ec6aa394 100644 --- a/src/components/net/fetch/response.rs +++ b/src/components/net/fetch/response.rs @@ -8,8 +8,9 @@ use StatusOk = http::status::Ok; use http::headers::HeaderEnum; use http::headers::response::HeaderCollection; use std::ascii::OwnedStrAsciiExt; +use std::comm::Receiver; -// [Response type](http://fetch.spec.whatwg.org/#concept-response-type) +/// [Response type](http://fetch.spec.whatwg.org/#concept-response-type) #[deriving(Clone, PartialEq)] pub enum ResponseType { Basic, @@ -19,7 +20,7 @@ pub enum ResponseType { Opaque } -// [Response termination reason](http://fetch.spec.whatwg.org/#concept-response-termination-reason) +/// [Response termination reason](http://fetch.spec.whatwg.org/#concept-response-termination-reason) #[deriving(Clone)] pub enum TerminationReason { EndUserAbort, @@ -27,7 +28,30 @@ pub enum TerminationReason { Timeout } -// A [Response](http://fetch.spec.whatwg.org/#concept-response) as defined by the Fetch spec +/// The response body can still be pushed to after fetch +/// This provides a way to store unfinished response bodies +#[unstable = "I haven't yet decided exactly how the interface for this will be"] +#[deriving(Clone)] +pub enum ResponseBody { + Empty, // XXXManishearth is this necessary, or is Done(vec![]) enough? + Receiving(Vec), + Done(Vec), +} + +#[unstable = "I haven't yet decided exactly how the interface for this will be"] +pub enum ResponseMsg { + Chunk(Vec), + Finished, + Errored +} + +#[unstable = "I haven't yet decided exactly how the interface for this will be"] +pub struct ResponseLoader { + response: Response, + chan: Receiver +} + +/// A [Response](http://fetch.spec.whatwg.org/#concept-response) as defined by the Fetch spec #[deriving(Clone)] pub struct Response { pub response_type: ResponseType, @@ -35,7 +59,7 @@ pub struct Response { pub url: Option, pub status: Status, pub headers: HeaderCollection, - pub body: Option>, + pub body: ResponseBody, /// [Internal response](http://fetch.spec.whatwg.org/#concept-internal-response), only used if the Response is a filtered response pub internal_response: Option>, } @@ -48,7 +72,19 @@ impl Response { url: None, status: StatusOk, headers: HeaderCollection::new(), - body: None, + body: Empty, + internal_response: None + } + } + + pub fn network_error() -> Response { + Response { + response_type: Error, + termination_reason: None, + url: None, + status: UnregisteredStatus(0, "".to_string()), + headers: HeaderCollection::new(), + body: Empty, internal_response: None } } @@ -100,7 +136,7 @@ impl Response { Opaque => { response.headers = HeaderCollection::new(); response.status = UnregisteredStatus(0, "".to_string()); - response.body = None; + response.body = Empty; } } response