Net side of XHR fetch integration

This commit is contained in:
Manish Goregaokar 2016-05-30 17:01:12 +05:30
parent b5255f011e
commit 2cbc8dee25
5 changed files with 107 additions and 6 deletions

View file

@ -4,15 +4,17 @@
//! The [Response](https://fetch.spec.whatwg.org/#responses) object
//! resulting from a [fetch operation](https://fetch.spec.whatwg.org/#concept-fetch)
use hyper::header::{AccessControlExposeHeaders, Headers};
use hyper::header::{AccessControlExposeHeaders, ContentType, Headers};
use hyper::http::RawStatus;
use hyper::status::StatusCode;
use {Metadata, NetworkError};
use std::ascii::AsciiExt;
use std::cell::{Cell, RefCell};
use std::sync::{Arc, Mutex};
use url::Url;
/// [Response type](https://fetch.spec.whatwg.org/#concept-response-type)
#[derive(Clone, PartialEq, Copy, Debug)]
#[derive(Clone, PartialEq, Copy, Debug, Deserialize, Serialize)]
pub enum ResponseType {
Basic,
CORS,
@ -23,7 +25,7 @@ pub enum ResponseType {
}
/// [Response termination reason](https://fetch.spec.whatwg.org/#concept-response-termination-reason)
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Deserialize, Serialize)]
pub enum TerminationReason {
EndUserAbort,
Fatal,
@ -50,7 +52,7 @@ impl ResponseBody {
/// [Cache state](https://fetch.spec.whatwg.org/#concept-response-cache-state)
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum CacheState {
None,
Local,
@ -81,6 +83,7 @@ pub struct Response {
pub url_list: RefCell<Vec<Url>>,
/// `None` can be considered a StatusCode of `0`.
pub status: Option<StatusCode>,
pub raw_status: Option<RawStatus>,
pub headers: Headers,
pub body: Arc<Mutex<ResponseBody>>,
pub cache_state: CacheState,
@ -100,6 +103,7 @@ impl Response {
url: None,
url_list: RefCell::new(Vec::new()),
status: Some(StatusCode::Ok),
raw_status: Some(RawStatus(200, "OK".into())),
headers: Headers::new(),
body: Arc::new(Mutex::new(ResponseBody::Empty)),
cache_state: CacheState::None,
@ -116,6 +120,7 @@ impl Response {
url: None,
url_list: RefCell::new(vec![]),
status: None,
raw_status: None,
headers: Headers::new(),
body: Arc::new(Mutex::new(ResponseBody::Empty)),
cache_state: CacheState::None,
@ -216,4 +221,21 @@ impl Response {
response
}
pub fn metadata(&self) -> Result<Metadata, NetworkError> {
let mut metadata = if let Some(ref url) = self.url {
Metadata::default(url.clone())
} else {
return Err(NetworkError::Internal("No url found".to_string()));
};
metadata.set_content_type(match self.headers.get() {
Some(&ContentType(ref mime)) => Some(mime),
None => None
});
metadata.headers = Some(self.headers.clone());
metadata.status = self.raw_status.clone();
metadata.https_state = self.https_state;
return Ok(metadata);
}
}