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

@ -112,6 +112,7 @@ pub struct LoadData {
pub headers: Headers,
#[ignore_heap_size_of = "Defined in hyper"]
/// Headers that will apply to the initial request and any redirects
/// Unused in fetch
pub preserved_headers: Headers,
pub data: Option<Vec<u8>>,
pub cors: Option<ResourceCORSData>,
@ -154,6 +155,16 @@ pub trait LoadOrigin {
fn pipeline_id(&self) -> Option<PipelineId>;
}
#[derive(Deserialize, Serialize)]
pub enum FetchResponseMsg {
// todo: should have fields for transmitted/total bytes
ProcessRequestBody,
ProcessRequestEOF,
// todo: send more info about the response (or perhaps the entire Response)
ProcessResponse(Result<Metadata, NetworkError>),
ProcessResponseEOF(Result<Vec<u8>, NetworkError>),
}
pub trait FetchTaskTarget {
/// https://fetch.spec.whatwg.org/#process-request-body
///
@ -176,6 +187,36 @@ pub trait FetchTaskTarget {
fn process_response_eof(&mut self, response: &response::Response);
}
impl FetchTaskTarget for IpcSender<FetchResponseMsg> {
fn process_request_body(&mut self, _: &request::Request) {
let _ = self.send(FetchResponseMsg::ProcessRequestBody);
}
fn process_request_eof(&mut self, _: &request::Request) {
let _ = self.send(FetchResponseMsg::ProcessRequestEOF);
}
fn process_response(&mut self, response: &response::Response) {
let _ = self.send(FetchResponseMsg::ProcessResponse(response.metadata()));
}
fn process_response_eof(&mut self, response: &response::Response) {
if response.is_network_error() {
// todo: finer grained errors
let _ = self.send(FetchResponseMsg::ProcessResponse(Err(NetworkError::Internal("Network error".into()))));
}
if let Ok(ref guard) = response.body.lock() {
if let response::ResponseBody::Done(ref vec) = **guard {
let _ = self.send(FetchResponseMsg::ProcessResponseEOF(Ok(vec.clone())));
return;
}
}
// If something goes wrong, log it instead of crashing the resource thread
let _ = self.send(FetchResponseMsg::ProcessResponseEOF(Err(NetworkError::Internal("Incomplete body".into()))));
}
}
/// A listener for asynchronous network events. Cancelling the underlying request is unsupported.
pub trait AsyncResponseListener {
/// The response headers for a request have been received.
@ -348,6 +389,7 @@ pub struct WebSocketConnectData {
pub enum CoreResourceMsg {
/// Request the data associated with a particular URL
Load(LoadData, LoadConsumer, Option<IpcSender<ResourceId>>),
Fetch(LoadData, IpcSender<FetchResponseMsg>),
/// Try to make a websocket connection to a URL.
WebsocketConnect(WebSocketCommunicate, WebSocketConnectData),
/// Store a set of cookies for a given originating URL

View file

@ -43,6 +43,7 @@ pub enum Origin {
#[derive(Clone, PartialEq)]
pub enum Referer {
NoReferer,
/// Default referer if nothing is specified
Client,
RefererUrl(Url)
}

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);
}
}