implementing working demonstration of calling Fetch asynchronously

This commit is contained in:
Nikki 2016-02-23 12:38:51 -07:00
parent 872ee19534
commit 3f79667050
5 changed files with 264 additions and 169 deletions

View file

@ -4,10 +4,10 @@
//! The [Response](https://fetch.spec.whatwg.org/#responses) object
//! resulting from a [fetch operation](https://fetch.spec.whatwg.org/#concept-fetch)
use hyper::header::Headers;
use hyper::header::{AccessControlExposeHeaders, Headers};
use hyper::status::StatusCode;
use std::cell::RefCell;
use std::rc::Rc;
use std::ascii::AsciiExt;
use std::cell::{Cell, RefCell};
use url::Url;
/// [Response type](https://fetch.spec.whatwg.org/#concept-response-type)
@ -38,6 +38,16 @@ pub enum ResponseBody {
Done(Vec<u8>),
}
impl ResponseBody {
pub fn is_done(&self) -> bool {
match *self {
ResponseBody::Done(..) => true,
ResponseBody::Empty | ResponseBody::Receiving(..) => false
}
}
}
/// [Cache state](https://fetch.spec.whatwg.org/#concept-response-cache-state)
#[derive(Clone, Debug)]
pub enum CacheState {
@ -76,7 +86,9 @@ pub struct Response {
pub https_state: HttpsState,
/// [Internal response](https://fetch.spec.whatwg.org/#concept-internal-response), only used if the Response
/// is a filtered response
pub internal_response: Option<Rc<Response>>,
pub internal_response: Option<Box<Response>>,
/// whether or not to try to return the internal_response when asked for actual_response
pub return_internal: Cell<bool>,
}
impl Response {
@ -91,7 +103,8 @@ impl Response {
body: RefCell::new(ResponseBody::Empty),
cache_state: CacheState::None,
https_state: HttpsState::None,
internal_response: None
internal_response: None,
return_internal: Cell::new(true)
}
}
@ -101,4 +114,92 @@ impl Response {
_ => false
}
}
pub fn get_actual_response(&self) -> &Response {
if self.return_internal.get() && self.internal_response.is_some() {
&**self.internal_response.as_ref().unwrap()
} else {
self
}
}
pub fn to_actual(self) -> Response {
if self.return_internal.get() && self.internal_response.is_some() {
*self.internal_response.unwrap()
} else {
self
}
}
/// 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 != ResponseType::Error);
assert!(filter_type != ResponseType::Default);
let old_response = self.to_actual();
if Response::is_network_error(&old_response) {
return Response::network_error();
}
let old_headers = old_response.headers.clone();
let mut response = old_response.clone();
response.internal_response = Some(Box::new(old_response));
response.response_type = filter_type;
match filter_type {
ResponseType::Default | ResponseType::Error => unreachable!(),
ResponseType::Basic => {
let headers = old_headers.iter().filter(|header| {
match &*header.name().to_ascii_lowercase() {
"set-cookie" | "set-cookie2" => false,
_ => true
}
}).collect();
response.headers = headers;
},
ResponseType::CORS => {
let access = old_headers.get::<AccessControlExposeHeaders>();
let allowed_headers = access.as_ref().map(|v| &v[..]).unwrap_or(&[]);
let headers = old_headers.iter().filter(|header| {
match &*header.name().to_ascii_lowercase() {
"cache-control" | "content-language" | "content-type" |
"expires" | "last-modified" | "pragma" => true,
"set-cookie" | "set-cookie2" => false,
header => {
let result =
allowed_headers.iter().find(|h| *header == *h.to_ascii_lowercase());
result.is_some()
}
}
}).collect();
response.headers = headers;
},
ResponseType::Opaque => {
response.url_list = RefCell::new(vec![]);
response.url = None;
response.headers = Headers::new();
response.status = None;
response.body = RefCell::new(ResponseBody::Empty);
response.cache_state = CacheState::None;
},
ResponseType::OpaqueRedirect => {
response.headers = Headers::new();
response.status = None;
response.body = RefCell::new(ResponseBody::Empty);
response.cache_state = CacheState::None;
}
}
response
}
}