Add FetchMetadata and update corresponding methods

This commit is contained in:
Keith Yeung 2016-09-14 00:30:59 -07:00
parent 4dcf693a75
commit 07c9cfecec
5 changed files with 75 additions and 29 deletions

View file

@ -170,7 +170,7 @@ pub enum FetchResponseMsg {
ProcessRequestBody,
ProcessRequestEOF,
// todo: send more info about the response (or perhaps the entire Response)
ProcessResponse(Result<Metadata, NetworkError>),
ProcessResponse(Result<FetchMetadata, NetworkError>),
ProcessResponseChunk(Vec<u8>),
ProcessResponseEOF(Result<(), NetworkError>),
}
@ -200,10 +200,25 @@ pub trait FetchTaskTarget {
fn process_response_eof(&mut self, response: &Response);
}
#[derive(Serialize, Deserialize)]
pub enum FilteredMetadata {
Opaque,
Transparent(Metadata)
}
#[derive(Serialize, Deserialize)]
pub enum FetchMetadata {
Unfiltered(Metadata),
Filtered {
filtered: FilteredMetadata,
unsafe_: Metadata
}
}
pub trait FetchResponseListener {
fn process_request_body(&mut self);
fn process_request_eof(&mut self);
fn process_response(&mut self, metadata: Result<Metadata, NetworkError>);
fn process_response(&mut self, metadata: Result<FetchMetadata, NetworkError>);
fn process_response_chunk(&mut self, chunk: Vec<u8>);
fn process_response_eof(&mut self, response: Result<(), NetworkError>);
}

View file

@ -4,7 +4,7 @@
//! The [Response](https://fetch.spec.whatwg.org/#responses) object
//! resulting from a [fetch operation](https://fetch.spec.whatwg.org/#concept-fetch)
use {Metadata, NetworkError};
use {FetchMetadata, FilteredMetadata, Metadata, NetworkError};
use hyper::header::{AccessControlExposeHeaders, ContentType, Headers};
use hyper::status::StatusCode;
use hyper_serde::Serde;
@ -225,24 +225,42 @@ 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 in response".to_string()));
pub fn metadata(&self) -> Result<FetchMetadata, NetworkError> {
fn init_metadata(response: &Response, url: &Url) -> Metadata {
let mut metadata = Metadata::default(url.clone());
metadata.set_content_type(match response.headers.get() {
Some(&ContentType(ref mime)) => Some(mime),
None => None
});
metadata.headers = Some(Serde(response.headers.clone()));
metadata.status = response.raw_status.clone();
metadata.https_state = response.https_state;
metadata
};
if self.is_network_error() {
return Err(NetworkError::Internal("Cannot extract metadata from network error".to_string()));
return Err(NetworkError::Internal("Cannot extract metadata from network error".to_owned()));
}
metadata.set_content_type(match self.headers.get() {
Some(&ContentType(ref mime)) => Some(mime),
None => None
});
metadata.headers = Some(Serde(self.headers.clone()));
metadata.status = self.raw_status.clone();
metadata.https_state = self.https_state;
return Ok(metadata);
let metadata = self.url.as_ref().map(|url| init_metadata(self, url));
if let Some(ref response) = self.internal_response {
match response.url {
Some(ref url) => {
let unsafe_metadata = init_metadata(response, url);
Ok(FetchMetadata::Filtered {
filtered: match metadata {
Some(m) => FilteredMetadata::Transparent(m),
None => FilteredMetadata::Opaque
},
unsafe_: unsafe_metadata
})
}
None => Err(NetworkError::Internal("No url found in unsafe response".to_owned()))
}
} else {
Ok(FetchMetadata::Unfiltered(metadata.unwrap()))
}
}
}