Add basic_fetch and a skeleton http_fetch

This commit is contained in:
Manish Goregaokar 2014-08-23 12:13:59 +05:30
parent 2ce5b46bba
commit 8cd572de95
2 changed files with 80 additions and 6 deletions

View file

@ -5,6 +5,7 @@
use url::Url; use url::Url;
use http::method::{Get, Method}; use http::method::{Get, Method};
use http::headers::request::HeaderCollection; use http::headers::request::HeaderCollection;
use fetch::response::Response;
/// A [request context](http://fetch.spec.whatwg.org/#concept-request-context) /// A [request context](http://fetch.spec.whatwg.org/#concept-request-context)
pub enum Context { pub enum Context {
@ -105,4 +106,41 @@ impl Request {
response_tainting: Basic 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
}
} }

View file

@ -8,8 +8,9 @@ use StatusOk = http::status::Ok;
use http::headers::HeaderEnum; use http::headers::HeaderEnum;
use http::headers::response::HeaderCollection; use http::headers::response::HeaderCollection;
use std::ascii::OwnedStrAsciiExt; 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)] #[deriving(Clone, PartialEq)]
pub enum ResponseType { pub enum ResponseType {
Basic, Basic,
@ -19,7 +20,7 @@ pub enum ResponseType {
Opaque 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)] #[deriving(Clone)]
pub enum TerminationReason { pub enum TerminationReason {
EndUserAbort, EndUserAbort,
@ -27,7 +28,30 @@ pub enum TerminationReason {
Timeout 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<u8>),
Done(Vec<u8>),
}
#[unstable = "I haven't yet decided exactly how the interface for this will be"]
pub enum ResponseMsg {
Chunk(Vec<u8>),
Finished,
Errored
}
#[unstable = "I haven't yet decided exactly how the interface for this will be"]
pub struct ResponseLoader {
response: Response,
chan: Receiver<ResponseMsg>
}
/// A [Response](http://fetch.spec.whatwg.org/#concept-response) as defined by the Fetch spec
#[deriving(Clone)] #[deriving(Clone)]
pub struct Response { pub struct Response {
pub response_type: ResponseType, pub response_type: ResponseType,
@ -35,7 +59,7 @@ pub struct Response {
pub url: Option<Url>, pub url: Option<Url>,
pub status: Status, pub status: Status,
pub headers: HeaderCollection, pub headers: HeaderCollection,
pub body: Option<Vec<u8>>, pub body: ResponseBody,
/// [Internal response](http://fetch.spec.whatwg.org/#concept-internal-response), only used if the Response is a filtered response /// [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>>, pub internal_response: Option<Box<Response>>,
} }
@ -48,7 +72,19 @@ impl Response {
url: None, url: None,
status: StatusOk, status: StatusOk,
headers: HeaderCollection::new(), 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 internal_response: None
} }
} }
@ -100,7 +136,7 @@ impl Response {
Opaque => { Opaque => {
response.headers = HeaderCollection::new(); response.headers = HeaderCollection::new();
response.status = UnregisteredStatus(0, "".to_string()); response.status = UnregisteredStatus(0, "".to_string());
response.body = None; response.body = Empty;
} }
} }
response response