Definition of Response and related enums/concepts

This commit is contained in:
Manish Goregaokar 2014-07-30 23:28:49 +05:30
parent 56e735ee79
commit e678943939
3 changed files with 105 additions and 4 deletions

View file

@ -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,

View file

@ -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;
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<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 {
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
}
}

View file

@ -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;