2015-04-03 22:00:46 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#![feature(box_syntax)]
|
2015-09-24 00:02:56 +03:00
|
|
|
#![feature(custom_attribute)]
|
2015-07-15 23:04:55 +03:00
|
|
|
#![feature(custom_derive)]
|
|
|
|
#![feature(plugin)]
|
2015-05-05 17:11:30 +03:00
|
|
|
#![feature(slice_patterns)]
|
|
|
|
#![feature(step_by)]
|
2016-02-05 01:10:36 +03:00
|
|
|
#![plugin(heapsize_plugin, serde_macros)]
|
2015-08-05 17:01:03 +03:00
|
|
|
|
2016-03-18 14:39:24 +03:00
|
|
|
#![deny(unsafe_code)]
|
|
|
|
|
2016-06-26 11:19:14 +03:00
|
|
|
extern crate cookie as cookie_rs;
|
2016-02-05 01:10:36 +03:00
|
|
|
extern crate heapsize;
|
2015-04-03 22:00:46 +03:00
|
|
|
extern crate hyper;
|
2016-08-12 20:23:10 +03:00
|
|
|
extern crate hyper_serde;
|
2015-10-13 00:06:31 +03:00
|
|
|
extern crate image as piston_image;
|
2015-11-28 17:04:11 +03:00
|
|
|
extern crate ipc_channel;
|
2016-04-22 23:40:38 +03:00
|
|
|
#[allow(unused_extern_crates)]
|
2015-11-28 17:04:11 +03:00
|
|
|
#[macro_use]
|
2016-03-15 14:03:03 +03:00
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
2015-11-28 17:04:11 +03:00
|
|
|
extern crate log;
|
2015-09-24 00:02:56 +03:00
|
|
|
extern crate msg;
|
2016-07-04 19:15:23 +03:00
|
|
|
extern crate num_traits;
|
2015-07-15 23:04:55 +03:00
|
|
|
extern crate serde;
|
2015-04-03 22:00:46 +03:00
|
|
|
extern crate url;
|
|
|
|
extern crate util;
|
2016-05-11 14:28:58 +03:00
|
|
|
extern crate uuid;
|
2015-12-08 18:10:50 +03:00
|
|
|
extern crate websocket;
|
2015-04-03 22:00:46 +03:00
|
|
|
|
2016-06-26 11:19:14 +03:00
|
|
|
use cookie_rs::Cookie;
|
2016-05-23 11:10:46 +03:00
|
|
|
use filemanager_thread::FileManagerThreadMsg;
|
2016-05-20 05:15:08 +03:00
|
|
|
use heapsize::HeapSizeOf;
|
2015-04-14 23:11:20 +03:00
|
|
|
use hyper::header::{ContentType, Headers};
|
2015-04-03 22:00:46 +03:00
|
|
|
use hyper::http::RawStatus;
|
|
|
|
use hyper::method::Method;
|
2015-09-24 00:02:56 +03:00
|
|
|
use hyper::mime::{Attr, Mime};
|
2016-08-12 20:23:10 +03:00
|
|
|
use hyper_serde::Serde;
|
2015-08-01 01:06:36 +03:00
|
|
|
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
|
2016-04-25 23:52:01 +03:00
|
|
|
use msg::constellation_msg::{PipelineId, ReferrerPolicy};
|
2016-06-12 03:30:28 +03:00
|
|
|
use request::{Request, RequestInit};
|
|
|
|
use response::{HttpsState, Response};
|
2016-05-20 05:15:08 +03:00
|
|
|
use std::io::Error as IOError;
|
2015-09-19 22:34:51 +03:00
|
|
|
use std::thread;
|
2016-05-20 16:25:13 +03:00
|
|
|
use storage_thread::StorageThreadMsg;
|
2015-04-03 22:00:46 +03:00
|
|
|
use url::Url;
|
2015-12-18 02:53:15 +03:00
|
|
|
use websocket::header;
|
2015-04-03 22:00:46 +03:00
|
|
|
|
2016-06-08 14:13:39 +03:00
|
|
|
pub mod blob_url_store;
|
2016-05-04 18:06:06 +03:00
|
|
|
pub mod bluetooth_scanfilter;
|
|
|
|
pub mod bluetooth_thread;
|
2016-05-11 14:28:58 +03:00
|
|
|
pub mod filemanager_thread;
|
2015-08-05 17:01:03 +03:00
|
|
|
pub mod hosts;
|
2016-01-10 13:19:04 +03:00
|
|
|
pub mod image_cache_thread;
|
2015-08-07 10:55:20 +03:00
|
|
|
pub mod net_error_list;
|
2016-01-18 13:35:56 +03:00
|
|
|
pub mod request;
|
2016-01-06 17:47:05 +03:00
|
|
|
pub mod response;
|
2016-01-10 13:19:04 +03:00
|
|
|
pub mod storage_thread;
|
2015-04-03 22:00:46 +03:00
|
|
|
|
|
|
|
/// Image handling.
|
|
|
|
///
|
|
|
|
/// It may be surprising that this goes in the network crate as opposed to the graphics crate.
|
|
|
|
/// However, image handling is generally very integrated with the network stack (especially where
|
|
|
|
/// caching is involved) and as a result it must live in here.
|
|
|
|
pub mod image {
|
|
|
|
pub mod base;
|
|
|
|
}
|
|
|
|
|
2015-12-31 18:43:10 +03:00
|
|
|
/// A loading context, for context-specific sniffing, as defined in
|
|
|
|
/// https://mimesniff.spec.whatwg.org/#context-specific-sniffing
|
|
|
|
#[derive(Clone, Deserialize, Serialize, HeapSizeOf)]
|
|
|
|
pub enum LoadContext {
|
|
|
|
Browsing,
|
|
|
|
Image,
|
|
|
|
AudioVideo,
|
|
|
|
Plugin,
|
|
|
|
Style,
|
|
|
|
Script,
|
|
|
|
Font,
|
|
|
|
TextTrack,
|
|
|
|
CacheManifest,
|
|
|
|
}
|
|
|
|
|
2016-05-21 11:12:54 +03:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, HeapSizeOf)]
|
|
|
|
pub struct CustomResponse {
|
|
|
|
#[ignore_heap_size_of = "Defined in hyper"]
|
2016-08-12 20:23:10 +03:00
|
|
|
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
|
|
|
serialize_with = "::hyper_serde::serialize")]
|
2016-05-21 11:12:54 +03:00
|
|
|
pub headers: Headers,
|
|
|
|
#[ignore_heap_size_of = "Defined in hyper"]
|
2016-08-12 20:23:10 +03:00
|
|
|
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
|
|
|
serialize_with = "::hyper_serde::serialize")]
|
2016-05-21 11:12:54 +03:00
|
|
|
pub raw_status: RawStatus,
|
|
|
|
pub body: Vec<u8>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CustomResponse {
|
|
|
|
pub fn new(headers: Headers, raw_status: RawStatus, body: Vec<u8>) -> CustomResponse {
|
|
|
|
CustomResponse { headers: headers, raw_status: raw_status, body: body }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
servo: Merge #11727 - Integrate service worker manager thread (from creativcoder:swmanager); r=jdm
<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes are part of #11091
<!-- Either: -->
- [X] There are tests for these changes at my [gh-pages](https://github.com/creativcoder/gsoc16/tree/gh-pages) branch to test the instantiation of service workers by their manager, but will need to discuss how that would integrate into master.
Changes:
- Introduces a `ServiceWorkerManager`, which maintains an map of registered service workers as well as a map of active workers keyed by their `scope_url`.
- Adds the initialization of ServiceWorkerManager, at the `script::init()`, which makes it available as a single entity listening for requests from different script threads.
- Adds a timeout thread in `serviceworkerglobalscope`, which terminates the workers, after a timeout of 60 secs, thereby removing it from the active workers list.
- Adds the matching of scope urls, in longest prefix way rather than path structural way, according to [spec](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#scope-match-algorithm).
- Make ServiceWorkerManager, the holder of network sender, instead of script thread, so it can send `CustomResponse`.
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 513811f6b40d522bc425c2588320b889614f2973
2016-07-18 19:50:59 +03:00
|
|
|
#[derive(Clone, Deserialize, Serialize)]
|
|
|
|
pub struct CustomResponseMediator {
|
|
|
|
pub response_chan: IpcSender<Option<CustomResponse>>,
|
|
|
|
pub load_url: Url
|
2016-05-21 11:12:54 +03:00
|
|
|
}
|
|
|
|
|
2015-08-13 22:16:14 +03:00
|
|
|
#[derive(Clone, Deserialize, Serialize, HeapSizeOf)]
|
2015-04-03 22:00:46 +03:00
|
|
|
pub struct LoadData {
|
|
|
|
pub url: Url,
|
2016-02-05 01:10:36 +03:00
|
|
|
#[ignore_heap_size_of = "Defined in hyper"]
|
2016-08-12 20:23:10 +03:00
|
|
|
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
|
|
|
serialize_with = "::hyper_serde::serialize")]
|
2015-04-03 22:00:46 +03:00
|
|
|
pub method: Method,
|
2015-08-13 22:16:14 +03:00
|
|
|
#[ignore_heap_size_of = "Defined in hyper"]
|
2016-08-12 20:23:10 +03:00
|
|
|
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
|
|
|
serialize_with = "::hyper_serde::serialize")]
|
2015-04-03 22:00:46 +03:00
|
|
|
/// Headers that will apply to the initial request only
|
|
|
|
pub headers: Headers,
|
2015-08-13 22:16:14 +03:00
|
|
|
#[ignore_heap_size_of = "Defined in hyper"]
|
2016-08-12 20:23:10 +03:00
|
|
|
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
|
|
|
serialize_with = "::hyper_serde::serialize")]
|
2015-04-03 22:00:46 +03:00
|
|
|
/// Headers that will apply to the initial request and any redirects
|
2016-06-12 03:30:28 +03:00
|
|
|
/// Unused in fetch
|
2015-04-03 22:00:46 +03:00
|
|
|
pub preserved_headers: Headers,
|
|
|
|
pub data: Option<Vec<u8>>,
|
|
|
|
pub cors: Option<ResourceCORSData>,
|
2015-05-05 19:52:44 +03:00
|
|
|
pub pipeline_id: Option<PipelineId>,
|
2015-12-05 02:43:01 +03:00
|
|
|
// https://fetch.spec.whatwg.org/#concept-http-fetch step 4.3
|
|
|
|
pub credentials_flag: bool,
|
2015-12-31 18:43:10 +03:00
|
|
|
pub context: LoadContext,
|
2016-04-25 23:52:01 +03:00
|
|
|
/// The policy and referring URL for the originator of this request
|
|
|
|
pub referrer_policy: Option<ReferrerPolicy>,
|
servo: Merge #11727 - Integrate service worker manager thread (from creativcoder:swmanager); r=jdm
<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes are part of #11091
<!-- Either: -->
- [X] There are tests for these changes at my [gh-pages](https://github.com/creativcoder/gsoc16/tree/gh-pages) branch to test the instantiation of service workers by their manager, but will need to discuss how that would integrate into master.
Changes:
- Introduces a `ServiceWorkerManager`, which maintains an map of registered service workers as well as a map of active workers keyed by their `scope_url`.
- Adds the initialization of ServiceWorkerManager, at the `script::init()`, which makes it available as a single entity listening for requests from different script threads.
- Adds a timeout thread in `serviceworkerglobalscope`, which terminates the workers, after a timeout of 60 secs, thereby removing it from the active workers list.
- Adds the matching of scope urls, in longest prefix way rather than path structural way, according to [spec](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#scope-match-algorithm).
- Make ServiceWorkerManager, the holder of network sender, instead of script thread, so it can send `CustomResponse`.
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 513811f6b40d522bc425c2588320b889614f2973
2016-07-18 19:50:59 +03:00
|
|
|
pub referrer_url: Option<Url>
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LoadData {
|
2016-04-25 23:52:01 +03:00
|
|
|
pub fn new(context: LoadContext,
|
|
|
|
url: Url,
|
2016-05-21 11:12:54 +03:00
|
|
|
load_origin: &LoadOrigin) -> LoadData {
|
2015-04-03 22:00:46 +03:00
|
|
|
LoadData {
|
|
|
|
url: url,
|
|
|
|
method: Method::Get,
|
|
|
|
headers: Headers::new(),
|
|
|
|
preserved_headers: Headers::new(),
|
|
|
|
data: None,
|
|
|
|
cors: None,
|
2016-05-21 11:12:54 +03:00
|
|
|
pipeline_id: load_origin.pipeline_id(),
|
2015-12-05 02:43:01 +03:00
|
|
|
credentials_flag: true,
|
2016-04-25 23:52:01 +03:00
|
|
|
context: context,
|
2016-05-21 11:12:54 +03:00
|
|
|
referrer_policy: load_origin.referrer_policy(),
|
2016-06-03 05:51:10 +03:00
|
|
|
referrer_url: load_origin.referrer_url().clone(),
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 11:12:54 +03:00
|
|
|
pub trait LoadOrigin {
|
|
|
|
fn referrer_url(&self) -> Option<Url>;
|
|
|
|
fn referrer_policy(&self) -> Option<ReferrerPolicy>;
|
|
|
|
fn pipeline_id(&self) -> Option<PipelineId>;
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
#[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)
|
2016-09-22 02:49:33 +03:00
|
|
|
ProcessResponse(Result<FetchMetadata, NetworkError>),
|
2016-06-12 03:30:28 +03:00
|
|
|
ProcessResponseChunk(Vec<u8>),
|
|
|
|
ProcessResponseEOF(Result<(), NetworkError>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait FetchTaskTarget {
|
|
|
|
/// https://fetch.spec.whatwg.org/#process-request-body
|
|
|
|
///
|
|
|
|
/// Fired when a chunk of the request body is transmitted
|
|
|
|
fn process_request_body(&mut self, request: &Request);
|
|
|
|
|
|
|
|
/// https://fetch.spec.whatwg.org/#process-request-end-of-file
|
|
|
|
///
|
|
|
|
/// Fired when the entire request finishes being transmitted
|
|
|
|
fn process_request_eof(&mut self, request: &Request);
|
|
|
|
|
|
|
|
/// https://fetch.spec.whatwg.org/#process-response
|
|
|
|
///
|
|
|
|
/// Fired when headers are received
|
|
|
|
fn process_response(&mut self, response: &Response);
|
|
|
|
|
|
|
|
/// Fired when a chunk of response content is received
|
|
|
|
fn process_response_chunk(&mut self, chunk: Vec<u8>);
|
|
|
|
|
|
|
|
/// https://fetch.spec.whatwg.org/#process-response-end-of-file
|
|
|
|
///
|
|
|
|
/// Fired when the response is fully fetched
|
|
|
|
fn process_response_eof(&mut self, response: &Response);
|
|
|
|
}
|
|
|
|
|
2016-09-22 02:49:33 +03:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub enum FilteredMetadata {
|
|
|
|
Opaque,
|
|
|
|
Transparent(Metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub enum FetchMetadata {
|
|
|
|
Unfiltered(Metadata),
|
|
|
|
Filtered {
|
|
|
|
filtered: FilteredMetadata,
|
|
|
|
unsafe_: Metadata
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
pub trait FetchResponseListener {
|
|
|
|
fn process_request_body(&mut self);
|
|
|
|
fn process_request_eof(&mut self);
|
2016-09-22 02:49:33 +03:00
|
|
|
fn process_response(&mut self, metadata: Result<FetchMetadata, NetworkError>);
|
2016-06-12 03:30:28 +03:00
|
|
|
fn process_response_chunk(&mut self, chunk: Vec<u8>);
|
|
|
|
fn process_response_eof(&mut self, response: Result<(), NetworkError>);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FetchTaskTarget for IpcSender<FetchResponseMsg> {
|
|
|
|
fn process_request_body(&mut self, _: &Request) {
|
|
|
|
let _ = self.send(FetchResponseMsg::ProcessRequestBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_request_eof(&mut self, _: &Request) {
|
|
|
|
let _ = self.send(FetchResponseMsg::ProcessRequestEOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_response(&mut self, response: &Response) {
|
|
|
|
let _ = self.send(FetchResponseMsg::ProcessResponse(response.metadata()));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_response_chunk(&mut self, chunk: Vec<u8>) {
|
|
|
|
let _ = self.send(FetchResponseMsg::ProcessResponseChunk(chunk));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_response_eof(&mut self, response: &Response) {
|
|
|
|
if response.is_network_error() {
|
|
|
|
// todo: finer grained errors
|
|
|
|
let _ = self.send(FetchResponseMsg::ProcessResponseEOF(
|
|
|
|
Err(NetworkError::Internal("Network error".into()))));
|
|
|
|
} else {
|
|
|
|
let _ = self.send(FetchResponseMsg::ProcessResponseEOF(Ok(())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub trait Action<Listener> {
|
|
|
|
fn process(self, listener: &mut Listener);
|
2015-09-28 22:59:08 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 19:33:06 +03:00
|
|
|
/// A listener for asynchronous network events. Cancelling the underlying request is unsupported.
|
|
|
|
pub trait AsyncResponseListener {
|
|
|
|
/// The response headers for a request have been received.
|
2016-04-20 19:49:22 +03:00
|
|
|
fn headers_available(&mut self, metadata: Result<Metadata, NetworkError>);
|
2015-04-16 19:33:06 +03:00
|
|
|
/// A portion of the response body has been received. This data is unavailable after
|
|
|
|
/// this method returned, and must be stored accordingly.
|
2015-10-16 17:49:13 +03:00
|
|
|
fn data_available(&mut self, payload: Vec<u8>);
|
2015-04-16 19:33:06 +03:00
|
|
|
/// The response is complete. If the provided status is an Err value, there is no guarantee
|
|
|
|
/// that the response body was completely read.
|
2016-04-20 19:49:22 +03:00
|
|
|
fn response_complete(&mut self, status: Result<(), NetworkError>);
|
2015-04-16 19:33:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Data for passing between threads/processes to indicate a particular action to
|
|
|
|
/// take on a provided network listener.
|
2015-08-01 01:06:36 +03:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2015-04-16 19:33:06 +03:00
|
|
|
pub enum ResponseAction {
|
|
|
|
/// Invoke headers_available
|
2016-04-20 19:49:22 +03:00
|
|
|
HeadersAvailable(Result<Metadata, NetworkError>),
|
2015-04-16 19:33:06 +03:00
|
|
|
/// Invoke data_available
|
|
|
|
DataAvailable(Vec<u8>),
|
|
|
|
/// Invoke response_complete
|
2016-04-20 19:49:22 +03:00
|
|
|
ResponseComplete(Result<(), NetworkError>)
|
2015-04-16 19:33:06 +03:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
impl<T: AsyncResponseListener> Action<T> for ResponseAction {
|
2015-04-16 19:33:06 +03:00
|
|
|
/// Execute the default action on a provided listener.
|
2016-06-12 03:30:28 +03:00
|
|
|
fn process(self, listener: &mut T) {
|
2015-04-16 19:33:06 +03:00
|
|
|
match self {
|
|
|
|
ResponseAction::HeadersAvailable(m) => listener.headers_available(m),
|
|
|
|
ResponseAction::DataAvailable(d) => listener.data_available(d),
|
|
|
|
ResponseAction::ResponseComplete(r) => listener.response_complete(r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
impl<T: FetchResponseListener> Action<T> for FetchResponseMsg {
|
|
|
|
/// Execute the default action on a provided listener.
|
|
|
|
fn process(self, listener: &mut T) {
|
|
|
|
match self {
|
|
|
|
FetchResponseMsg::ProcessRequestBody => listener.process_request_body(),
|
|
|
|
FetchResponseMsg::ProcessRequestEOF => listener.process_request_eof(),
|
|
|
|
FetchResponseMsg::ProcessResponse(meta) => listener.process_response(meta),
|
|
|
|
FetchResponseMsg::ProcessResponseChunk(data) => listener.process_response_chunk(data),
|
|
|
|
FetchResponseMsg::ProcessResponseEOF(data) => listener.process_response_eof(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 19:33:06 +03:00
|
|
|
/// A target for async networking events. Commonly used to dispatch a runnable event to another
|
|
|
|
/// thread storing the wrapped closure for later execution.
|
2015-08-01 01:06:36 +03:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct AsyncResponseTarget {
|
|
|
|
pub sender: IpcSender<ResponseAction>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncResponseTarget {
|
|
|
|
pub fn invoke_with_listener(&self, action: ResponseAction) {
|
|
|
|
self.sender.send(action).unwrap()
|
|
|
|
}
|
2015-04-16 19:33:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A wrapper for a network load that can either be channel or event-based.
|
2015-08-01 01:06:36 +03:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2015-04-16 19:33:06 +03:00
|
|
|
pub enum LoadConsumer {
|
2015-08-01 01:06:36 +03:00
|
|
|
Channel(IpcSender<LoadResponse>),
|
|
|
|
Listener(AsyncResponseTarget),
|
2015-04-16 19:33:06 +03:00
|
|
|
}
|
|
|
|
|
2016-01-10 13:19:04 +03:00
|
|
|
/// Handle to a resource thread
|
2016-05-20 05:15:08 +03:00
|
|
|
pub type CoreResourceThread = IpcSender<CoreResourceMsg>;
|
|
|
|
|
|
|
|
pub type IpcSendResult = Result<(), IOError>;
|
|
|
|
|
2016-05-23 11:10:46 +03:00
|
|
|
/// Abstraction of the ability to send a particular type of message,
|
|
|
|
/// used by net_traits::ResourceThreads to ease the use its IpcSender sub-fields
|
|
|
|
/// XXX: If this trait will be used more in future, some auto derive might be appealing
|
2016-05-20 05:15:08 +03:00
|
|
|
pub trait IpcSend<T> where T: serde::Serialize + serde::Deserialize {
|
2016-05-23 11:10:46 +03:00
|
|
|
/// send message T
|
2016-05-20 05:15:08 +03:00
|
|
|
fn send(&self, T) -> IpcSendResult;
|
2016-05-23 11:10:46 +03:00
|
|
|
/// get underlying sender
|
2016-05-20 05:15:08 +03:00
|
|
|
fn sender(&self) -> IpcSender<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Originally we will construct an Arc<ResourceThread> from ResourceThread
|
|
|
|
// in script_thread to avoid some performance pitfall. Now we decide to deal with
|
|
|
|
// the "Arc" hack implicitly in future.
|
|
|
|
// See discussion: http://logs.glob.uno/?c=mozilla%23servo&s=16+May+2016&e=16+May+2016#c430412
|
|
|
|
// See also: https://github.com/servo/servo/blob/735480/components/script/script_thread.rs#L313
|
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
|
|
|
pub struct ResourceThreads {
|
|
|
|
core_thread: CoreResourceThread,
|
2016-05-20 16:25:13 +03:00
|
|
|
storage_thread: IpcSender<StorageThreadMsg>,
|
2016-05-20 05:15:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ResourceThreads {
|
2016-05-23 11:10:46 +03:00
|
|
|
pub fn new(c: CoreResourceThread,
|
2016-08-03 00:57:46 +03:00
|
|
|
s: IpcSender<StorageThreadMsg>) -> ResourceThreads {
|
2016-05-20 05:15:08 +03:00
|
|
|
ResourceThreads {
|
|
|
|
core_thread: c,
|
|
|
|
storage_thread: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IpcSend<CoreResourceMsg> for ResourceThreads {
|
|
|
|
fn send(&self, msg: CoreResourceMsg) -> IpcSendResult {
|
|
|
|
self.core_thread.send(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sender(&self) -> IpcSender<CoreResourceMsg> {
|
|
|
|
self.core_thread.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IpcSend<StorageThreadMsg> for ResourceThreads {
|
|
|
|
fn send(&self, msg: StorageThreadMsg) -> IpcSendResult {
|
|
|
|
self.storage_thread.send(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sender(&self) -> IpcSender<StorageThreadMsg> {
|
|
|
|
self.storage_thread.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the sub-fields
|
|
|
|
impl HeapSizeOf for ResourceThreads {
|
|
|
|
fn heap_size_of_children(&self) -> usize { 0 }
|
|
|
|
}
|
2015-04-03 22:00:46 +03:00
|
|
|
|
2015-08-01 01:06:36 +03:00
|
|
|
#[derive(PartialEq, Copy, Clone, Deserialize, Serialize)]
|
2015-07-22 19:23:05 +03:00
|
|
|
pub enum IncludeSubdomains {
|
|
|
|
Included,
|
|
|
|
NotIncluded
|
|
|
|
}
|
|
|
|
|
2015-12-08 18:10:50 +03:00
|
|
|
#[derive(HeapSizeOf, Deserialize, Serialize)]
|
|
|
|
pub enum MessageData {
|
|
|
|
Text(String),
|
|
|
|
Binary(Vec<u8>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub enum WebSocketDomAction {
|
|
|
|
SendMessage(MessageData),
|
2016-01-19 18:38:09 +03:00
|
|
|
Close(Option<u16>, Option<String>),
|
2015-12-08 18:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub enum WebSocketNetworkEvent {
|
2016-08-12 20:23:10 +03:00
|
|
|
ConnectionEstablished(
|
|
|
|
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
|
|
|
serialize_with = "::hyper_serde::serialize")]
|
|
|
|
header::Headers,
|
|
|
|
Vec<String>
|
|
|
|
),
|
2015-12-08 18:10:50 +03:00
|
|
|
MessageReceived(MessageData),
|
2016-01-19 18:38:09 +03:00
|
|
|
Close(Option<u16>, String),
|
|
|
|
Fail,
|
2015-12-08 18:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct WebSocketCommunicate {
|
|
|
|
pub event_sender: IpcSender<WebSocketNetworkEvent>,
|
|
|
|
pub action_receiver: IpcReceiver<WebSocketDomAction>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct WebSocketConnectData {
|
|
|
|
pub resource_url: Url,
|
|
|
|
pub origin: String,
|
2015-12-18 02:53:15 +03:00
|
|
|
pub protocols: Vec<String>,
|
2015-12-08 18:10:50 +03:00
|
|
|
}
|
|
|
|
|
2015-08-01 01:06:36 +03:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2016-05-20 05:15:08 +03:00
|
|
|
pub enum CoreResourceMsg {
|
2015-04-03 22:00:46 +03:00
|
|
|
/// Request the data associated with a particular URL
|
2015-11-12 16:29:58 +03:00
|
|
|
Load(LoadData, LoadConsumer, Option<IpcSender<ResourceId>>),
|
2016-06-12 03:30:28 +03:00
|
|
|
Fetch(RequestInit, IpcSender<FetchResponseMsg>),
|
2015-12-08 18:10:50 +03:00
|
|
|
/// Try to make a websocket connection to a URL.
|
|
|
|
WebsocketConnect(WebSocketCommunicate, WebSocketConnectData),
|
2015-04-03 22:00:46 +03:00
|
|
|
/// Store a set of cookies for a given originating URL
|
|
|
|
SetCookiesForUrl(Url, String, CookieSource),
|
2016-06-26 11:19:14 +03:00
|
|
|
/// Store a set of cookies for a given originating URL
|
2016-08-12 20:23:10 +03:00
|
|
|
SetCookiesForUrlWithData(
|
|
|
|
Url,
|
|
|
|
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
|
|
|
serialize_with = "::hyper_serde::serialize")]
|
|
|
|
Cookie,
|
|
|
|
CookieSource
|
|
|
|
),
|
2015-04-03 22:00:46 +03:00
|
|
|
/// Retrieve the stored cookies for a given URL
|
2015-08-01 01:06:36 +03:00
|
|
|
GetCookiesForUrl(Url, IpcSender<Option<String>>, CookieSource),
|
2016-06-26 11:19:14 +03:00
|
|
|
/// Get a cookie by name for a given originating URL
|
2016-08-12 20:23:10 +03:00
|
|
|
GetCookiesDataForUrl(Url, IpcSender<Vec<Serde<Cookie>>>, CookieSource),
|
2015-11-12 16:29:58 +03:00
|
|
|
/// Cancel a network request corresponding to a given `ResourceId`
|
|
|
|
Cancel(ResourceId),
|
|
|
|
/// Synchronization message solely for knowing the state of the ResourceChannelManager loop
|
|
|
|
Synchronize(IpcSender<()>),
|
servo: Merge #11727 - Integrate service worker manager thread (from creativcoder:swmanager); r=jdm
<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes are part of #11091
<!-- Either: -->
- [X] There are tests for these changes at my [gh-pages](https://github.com/creativcoder/gsoc16/tree/gh-pages) branch to test the instantiation of service workers by their manager, but will need to discuss how that would integrate into master.
Changes:
- Introduces a `ServiceWorkerManager`, which maintains an map of registered service workers as well as a map of active workers keyed by their `scope_url`.
- Adds the initialization of ServiceWorkerManager, at the `script::init()`, which makes it available as a single entity listening for requests from different script threads.
- Adds a timeout thread in `serviceworkerglobalscope`, which terminates the workers, after a timeout of 60 secs, thereby removing it from the active workers list.
- Adds the matching of scope urls, in longest prefix way rather than path structural way, according to [spec](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#scope-match-algorithm).
- Make ServiceWorkerManager, the holder of network sender, instead of script thread, so it can send `CustomResponse`.
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 513811f6b40d522bc425c2588320b889614f2973
2016-07-18 19:50:59 +03:00
|
|
|
/// Send the network sender in constellation to CoreResourceThread
|
|
|
|
NetworkMediator(IpcSender<CustomResponseMediator>),
|
2016-08-03 00:57:46 +03:00
|
|
|
/// Message forwarded to file manager's handler
|
|
|
|
ToFileManager(FileManagerThreadMsg),
|
2016-06-01 14:46:58 +03:00
|
|
|
/// Break the load handler loop, send a reply when done cleaning up local resources
|
|
|
|
// and exit
|
|
|
|
Exit(IpcSender<()>),
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
|
2015-05-11 22:35:33 +03:00
|
|
|
/// Initialized but unsent request. Encapsulates everything necessary to instruct
|
2016-01-10 13:19:04 +03:00
|
|
|
/// the resource thread to make a new request. The `load` method *must* be called before
|
|
|
|
/// destruction or the thread will panic.
|
2015-05-11 22:35:33 +03:00
|
|
|
pub struct PendingAsyncLoad {
|
2016-05-20 05:15:08 +03:00
|
|
|
core_resource_thread: CoreResourceThread,
|
2015-05-11 22:35:33 +03:00
|
|
|
url: Url,
|
|
|
|
pipeline: Option<PipelineId>,
|
|
|
|
guard: PendingLoadGuard,
|
2015-12-31 18:43:10 +03:00
|
|
|
context: LoadContext,
|
2016-04-25 23:52:01 +03:00
|
|
|
referrer_policy: Option<ReferrerPolicy>,
|
servo: Merge #11727 - Integrate service worker manager thread (from creativcoder:swmanager); r=jdm
<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes are part of #11091
<!-- Either: -->
- [X] There are tests for these changes at my [gh-pages](https://github.com/creativcoder/gsoc16/tree/gh-pages) branch to test the instantiation of service workers by their manager, but will need to discuss how that would integrate into master.
Changes:
- Introduces a `ServiceWorkerManager`, which maintains an map of registered service workers as well as a map of active workers keyed by their `scope_url`.
- Adds the initialization of ServiceWorkerManager, at the `script::init()`, which makes it available as a single entity listening for requests from different script threads.
- Adds a timeout thread in `serviceworkerglobalscope`, which terminates the workers, after a timeout of 60 secs, thereby removing it from the active workers list.
- Adds the matching of scope urls, in longest prefix way rather than path structural way, according to [spec](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#scope-match-algorithm).
- Make ServiceWorkerManager, the holder of network sender, instead of script thread, so it can send `CustomResponse`.
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 513811f6b40d522bc425c2588320b889614f2973
2016-07-18 19:50:59 +03:00
|
|
|
referrer_url: Option<Url>
|
2015-05-11 22:35:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PendingLoadGuard {
|
|
|
|
loaded: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PendingLoadGuard {
|
|
|
|
fn neuter(&mut self) {
|
|
|
|
self.loaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for PendingLoadGuard {
|
|
|
|
fn drop(&mut self) {
|
2015-05-26 00:33:43 +03:00
|
|
|
if !thread::panicking() {
|
|
|
|
assert!(self.loaded)
|
|
|
|
}
|
2015-05-11 22:35:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 11:12:54 +03:00
|
|
|
impl LoadOrigin for PendingAsyncLoad {
|
|
|
|
fn referrer_url(&self) -> Option<Url> {
|
|
|
|
self.referrer_url.clone()
|
|
|
|
}
|
|
|
|
fn referrer_policy(&self) -> Option<ReferrerPolicy> {
|
|
|
|
self.referrer_policy.clone()
|
|
|
|
}
|
|
|
|
fn pipeline_id(&self) -> Option<PipelineId> {
|
|
|
|
self.pipeline
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 22:35:33 +03:00
|
|
|
impl PendingAsyncLoad {
|
2016-04-25 23:52:01 +03:00
|
|
|
pub fn new(context: LoadContext,
|
2016-05-20 05:15:08 +03:00
|
|
|
core_resource_thread: CoreResourceThread,
|
2016-04-25 23:52:01 +03:00
|
|
|
url: Url,
|
|
|
|
pipeline: Option<PipelineId>,
|
|
|
|
referrer_policy: Option<ReferrerPolicy>,
|
servo: Merge #11727 - Integrate service worker manager thread (from creativcoder:swmanager); r=jdm
<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes are part of #11091
<!-- Either: -->
- [X] There are tests for these changes at my [gh-pages](https://github.com/creativcoder/gsoc16/tree/gh-pages) branch to test the instantiation of service workers by their manager, but will need to discuss how that would integrate into master.
Changes:
- Introduces a `ServiceWorkerManager`, which maintains an map of registered service workers as well as a map of active workers keyed by their `scope_url`.
- Adds the initialization of ServiceWorkerManager, at the `script::init()`, which makes it available as a single entity listening for requests from different script threads.
- Adds a timeout thread in `serviceworkerglobalscope`, which terminates the workers, after a timeout of 60 secs, thereby removing it from the active workers list.
- Adds the matching of scope urls, in longest prefix way rather than path structural way, according to [spec](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#scope-match-algorithm).
- Make ServiceWorkerManager, the holder of network sender, instead of script thread, so it can send `CustomResponse`.
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 513811f6b40d522bc425c2588320b889614f2973
2016-07-18 19:50:59 +03:00
|
|
|
referrer_url: Option<Url>)
|
2015-05-11 22:35:33 +03:00
|
|
|
-> PendingAsyncLoad {
|
|
|
|
PendingAsyncLoad {
|
2016-05-20 05:15:08 +03:00
|
|
|
core_resource_thread: core_resource_thread,
|
2015-05-11 22:35:33 +03:00
|
|
|
url: url,
|
|
|
|
pipeline: pipeline,
|
|
|
|
guard: PendingLoadGuard { loaded: false, },
|
2016-04-25 23:52:01 +03:00
|
|
|
context: context,
|
|
|
|
referrer_policy: referrer_policy,
|
servo: Merge #11727 - Integrate service worker manager thread (from creativcoder:swmanager); r=jdm
<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes are part of #11091
<!-- Either: -->
- [X] There are tests for these changes at my [gh-pages](https://github.com/creativcoder/gsoc16/tree/gh-pages) branch to test the instantiation of service workers by their manager, but will need to discuss how that would integrate into master.
Changes:
- Introduces a `ServiceWorkerManager`, which maintains an map of registered service workers as well as a map of active workers keyed by their `scope_url`.
- Adds the initialization of ServiceWorkerManager, at the `script::init()`, which makes it available as a single entity listening for requests from different script threads.
- Adds a timeout thread in `serviceworkerglobalscope`, which terminates the workers, after a timeout of 60 secs, thereby removing it from the active workers list.
- Adds the matching of scope urls, in longest prefix way rather than path structural way, according to [spec](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#scope-match-algorithm).
- Make ServiceWorkerManager, the holder of network sender, instead of script thread, so it can send `CustomResponse`.
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 513811f6b40d522bc425c2588320b889614f2973
2016-07-18 19:50:59 +03:00
|
|
|
referrer_url: referrer_url
|
2015-05-11 22:35:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 20:37:06 +03:00
|
|
|
/// Initiate the network request associated with this pending load, using the provided target.
|
2015-08-01 01:06:36 +03:00
|
|
|
pub fn load_async(mut self, listener: AsyncResponseTarget) {
|
2015-05-21 20:37:06 +03:00
|
|
|
self.guard.neuter();
|
2016-05-21 11:12:54 +03:00
|
|
|
|
|
|
|
let load_data = LoadData::new(self.context.clone(),
|
|
|
|
self.url.clone(),
|
|
|
|
&self);
|
2015-05-21 20:37:06 +03:00
|
|
|
let consumer = LoadConsumer::Listener(listener);
|
2016-05-20 05:15:08 +03:00
|
|
|
self.core_resource_thread.send(CoreResourceMsg::Load(load_data, consumer, None)).unwrap();
|
2015-05-11 22:35:33 +03:00
|
|
|
}
|
2016-09-22 02:49:33 +03:00
|
|
|
|
|
|
|
/// Initiate the fetch associated with this pending load.
|
|
|
|
pub fn fetch_async(mut self, request: RequestInit, fetch_target: IpcSender<FetchResponseMsg>) {
|
|
|
|
self.guard.neuter();
|
|
|
|
|
|
|
|
self.core_resource_thread.send(CoreResourceMsg::Fetch(request, fetch_target)).unwrap();
|
|
|
|
}
|
2015-05-11 22:35:33 +03:00
|
|
|
}
|
|
|
|
|
2015-04-03 22:00:46 +03:00
|
|
|
/// Message sent in response to `Load`. Contains metadata, and a port
|
|
|
|
/// for receiving the data.
|
|
|
|
///
|
|
|
|
/// Even if loading fails immediately, we send one of these and the
|
|
|
|
/// progress_port will provide the error.
|
2015-08-01 01:06:36 +03:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2015-04-03 22:00:46 +03:00
|
|
|
pub struct LoadResponse {
|
|
|
|
/// Metadata, such as from HTTP headers.
|
|
|
|
pub metadata: Metadata,
|
|
|
|
/// Port for reading data.
|
2015-08-01 01:06:36 +03:00
|
|
|
pub progress_port: IpcReceiver<ProgressMsg>,
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
|
2015-08-13 22:16:14 +03:00
|
|
|
#[derive(Clone, Deserialize, Serialize, HeapSizeOf)]
|
2015-04-03 22:00:46 +03:00
|
|
|
pub struct ResourceCORSData {
|
|
|
|
/// CORS Preflight flag
|
|
|
|
pub preflight: bool,
|
|
|
|
/// Origin of CORS Request
|
2015-08-01 01:06:36 +03:00
|
|
|
pub origin: Url,
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Metadata about a loaded resource, such as is obtained from HTTP headers.
|
2015-08-13 22:16:14 +03:00
|
|
|
#[derive(Clone, Deserialize, Serialize, HeapSizeOf)]
|
2015-04-03 22:00:46 +03:00
|
|
|
pub struct Metadata {
|
|
|
|
/// Final URL after redirects.
|
|
|
|
pub final_url: Url,
|
|
|
|
|
2016-02-05 01:10:36 +03:00
|
|
|
#[ignore_heap_size_of = "Defined in hyper"]
|
2015-04-03 22:00:46 +03:00
|
|
|
/// MIME type / subtype.
|
2016-08-12 20:23:10 +03:00
|
|
|
pub content_type: Option<Serde<ContentType>>,
|
2015-04-03 22:00:46 +03:00
|
|
|
|
|
|
|
/// Character set.
|
|
|
|
pub charset: Option<String>,
|
|
|
|
|
2015-08-13 22:16:14 +03:00
|
|
|
#[ignore_heap_size_of = "Defined in hyper"]
|
2015-04-03 22:00:46 +03:00
|
|
|
/// Headers
|
2016-08-12 20:23:10 +03:00
|
|
|
pub headers: Option<Serde<Headers>>,
|
2015-04-03 22:00:46 +03:00
|
|
|
|
|
|
|
/// HTTP Status
|
servo: Merge #13058 - Response API (from malisas:malisa-responseAPI); r=Manishearth,jdm
<!-- Please describe your changes on the following line: -->
This PR adds the [dom::Response](https://fetch.spec.whatwg.org/#response-class) implementation and addresses #11896.
The relevant passing tests` expectations have been updated.
In order to allow non-UTF-8-encoded status messages, `net_traits::response::Response`'s `raw_status` field has been changed from type [`Option<RawStatus>`](https://doc.servo.org/hyper/http/struct.RawStatus.html) to type `Option<(u16, Vec<u8>)>`. As a result, a few other files which rely on the `raw_status` field were affected and updated.
TODOs:
- The `body` and `trailer` methods. Relies on implementation of `ReadableStream` and `Promise`s.
- Similarly, replace the dummy constructor `_body: Option<USVString>` argument with `body: ResponseBodyInit`.
- Currently, whenever `r's response's header list` or `r's Headers object` are mentioned, I always modify the `headers_reflector` field (of type dom::Headers, or `r's Headers object`) and not the corresponding hyper::Headers list in the net_traits::Response field. A completely accurate interpretation of the spec might consider making both of these lists the same thing via a reference. [Discussion](https://github.com/whatwg/fetch/issues/358) was [had](https://github.com/servo/servo/pull/12884).
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).
<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 5a5a76cc5db830d2e622d4e0924837383b64dfa2
2016-09-09 02:58:05 +03:00
|
|
|
pub status: Option<(u16, Vec<u8>)>,
|
2016-02-09 10:30:29 +03:00
|
|
|
|
|
|
|
/// Is successful HTTPS connection
|
2016-06-12 03:30:28 +03:00
|
|
|
pub https_state: HttpsState,
|
2016-07-14 21:55:44 +03:00
|
|
|
|
|
|
|
/// Referrer Url
|
|
|
|
pub referrer: Option<Url>,
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Metadata {
|
|
|
|
/// Metadata with defaults for everything optional.
|
|
|
|
pub fn default(url: Url) -> Self {
|
|
|
|
Metadata {
|
|
|
|
final_url: url,
|
|
|
|
content_type: None,
|
|
|
|
charset: None,
|
|
|
|
headers: None,
|
2015-04-14 10:57:41 +03:00
|
|
|
// https://fetch.spec.whatwg.org/#concept-response-status-message
|
servo: Merge #13058 - Response API (from malisas:malisa-responseAPI); r=Manishearth,jdm
<!-- Please describe your changes on the following line: -->
This PR adds the [dom::Response](https://fetch.spec.whatwg.org/#response-class) implementation and addresses #11896.
The relevant passing tests` expectations have been updated.
In order to allow non-UTF-8-encoded status messages, `net_traits::response::Response`'s `raw_status` field has been changed from type [`Option<RawStatus>`](https://doc.servo.org/hyper/http/struct.RawStatus.html) to type `Option<(u16, Vec<u8>)>`. As a result, a few other files which rely on the `raw_status` field were affected and updated.
TODOs:
- The `body` and `trailer` methods. Relies on implementation of `ReadableStream` and `Promise`s.
- Similarly, replace the dummy constructor `_body: Option<USVString>` argument with `body: ResponseBodyInit`.
- Currently, whenever `r's response's header list` or `r's Headers object` are mentioned, I always modify the `headers_reflector` field (of type dom::Headers, or `r's Headers object`) and not the corresponding hyper::Headers list in the net_traits::Response field. A completely accurate interpretation of the spec might consider making both of these lists the same thing via a reference. [Discussion](https://github.com/whatwg/fetch/issues/358) was [had](https://github.com/servo/servo/pull/12884).
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).
<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 5a5a76cc5db830d2e622d4e0924837383b64dfa2
2016-09-09 02:58:05 +03:00
|
|
|
status: Some((200, b"OK".to_vec())),
|
2016-06-12 03:30:28 +03:00
|
|
|
https_state: HttpsState::None,
|
2016-07-14 21:55:44 +03:00
|
|
|
referrer: None,
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract the parts of a Mime that we care about.
|
|
|
|
pub fn set_content_type(&mut self, content_type: Option<&Mime>) {
|
2016-04-03 18:15:36 +03:00
|
|
|
match self.headers {
|
2016-08-12 20:23:10 +03:00
|
|
|
None => self.headers = Some(Serde(Headers::new())),
|
2016-04-03 18:15:36 +03:00
|
|
|
Some(_) => (),
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:00:46 +03:00
|
|
|
match content_type {
|
|
|
|
None => (),
|
2015-04-14 23:11:20 +03:00
|
|
|
Some(mime) => {
|
2016-04-03 18:15:36 +03:00
|
|
|
if let Some(headers) = self.headers.as_mut() {
|
|
|
|
headers.set(ContentType(mime.clone()));
|
|
|
|
}
|
|
|
|
|
2016-08-12 20:23:10 +03:00
|
|
|
self.content_type = Some(Serde(ContentType(mime.clone())));
|
2015-04-14 23:11:20 +03:00
|
|
|
let &Mime(_, _, ref parameters) = mime;
|
2015-08-15 12:03:21 +03:00
|
|
|
for &(ref k, ref v) in parameters {
|
2015-04-03 22:00:46 +03:00
|
|
|
if &Attr::Charset == k {
|
|
|
|
self.charset = Some(v.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The creator of a given cookie
|
2015-08-01 01:06:36 +03:00
|
|
|
#[derive(PartialEq, Copy, Clone, Deserialize, Serialize)]
|
2015-04-03 22:00:46 +03:00
|
|
|
pub enum CookieSource {
|
|
|
|
/// An HTTP API
|
|
|
|
HTTP,
|
|
|
|
/// A non-HTTP API
|
|
|
|
NonHTTP,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Messages sent in response to a `Load` message
|
2015-08-01 01:06:36 +03:00
|
|
|
#[derive(PartialEq, Debug, Deserialize, Serialize)]
|
2015-04-03 22:00:46 +03:00
|
|
|
pub enum ProgressMsg {
|
|
|
|
/// Binary data - there may be multiple of these
|
|
|
|
Payload(Vec<u8>),
|
|
|
|
/// Indicates loading is complete, either successfully or not
|
2016-04-20 19:49:22 +03:00
|
|
|
Done(Result<(), NetworkError>),
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function for synchronously loading a whole resource.
|
2015-12-31 18:43:10 +03:00
|
|
|
pub fn load_whole_resource(context: LoadContext,
|
2016-05-20 05:15:08 +03:00
|
|
|
core_resource_thread: &CoreResourceThread,
|
2015-12-31 18:43:10 +03:00
|
|
|
url: Url,
|
2016-05-21 11:12:54 +03:00
|
|
|
load_origin: &LoadOrigin)
|
2016-04-20 19:49:22 +03:00
|
|
|
-> Result<(Metadata, Vec<u8>), NetworkError> {
|
2015-08-01 01:06:36 +03:00
|
|
|
let (start_chan, start_port) = ipc::channel().unwrap();
|
2016-05-21 11:12:54 +03:00
|
|
|
let load_data = LoadData::new(context, url, load_origin);
|
|
|
|
core_resource_thread.send(CoreResourceMsg::Load(load_data, LoadConsumer::Channel(start_chan), None)).unwrap();
|
2015-04-03 22:00:46 +03:00
|
|
|
let response = start_port.recv().unwrap();
|
|
|
|
|
|
|
|
let mut buf = vec!();
|
|
|
|
loop {
|
|
|
|
match response.progress_port.recv().unwrap() {
|
2015-12-11 02:27:58 +03:00
|
|
|
ProgressMsg::Payload(data) => buf.extend_from_slice(&data),
|
2015-08-01 01:06:36 +03:00
|
|
|
ProgressMsg::Done(Ok(())) => return Ok((response.metadata, buf)),
|
|
|
|
ProgressMsg::Done(Err(e)) => return Err(e)
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-12 16:29:58 +03:00
|
|
|
|
2015-12-18 02:53:15 +03:00
|
|
|
/// Defensively unwraps the protocol string from the response object's protocol
|
|
|
|
pub fn unwrap_websocket_protocol(wsp: Option<&header::WebSocketProtocol>) -> Option<&str> {
|
|
|
|
wsp.and_then(|protocol_list| protocol_list.get(0).map(|protocol| protocol.as_ref()))
|
|
|
|
}
|
|
|
|
|
2015-11-12 16:29:58 +03:00
|
|
|
/// An unique identifier to keep track of each load message in the resource handler
|
|
|
|
#[derive(Clone, PartialEq, Eq, Copy, Hash, Debug, Deserialize, Serialize, HeapSizeOf)]
|
|
|
|
pub struct ResourceId(pub u32);
|
2016-04-05 15:11:11 +03:00
|
|
|
|
2016-06-20 22:32:17 +03:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2016-04-05 15:11:11 +03:00
|
|
|
pub enum ConstellationMsg {
|
|
|
|
/// Queries whether a pipeline or its ancestors are private
|
2016-06-20 22:32:17 +03:00
|
|
|
IsPrivate(PipelineId, IpcSender<bool>),
|
2016-04-05 15:11:11 +03:00
|
|
|
}
|
2016-04-20 19:49:22 +03:00
|
|
|
|
|
|
|
/// Network errors that have to be exported out of the loaders
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize, HeapSizeOf)]
|
|
|
|
pub enum NetworkError {
|
2016-04-27 11:26:05 +03:00
|
|
|
/// Could be any of the internal errors, like unsupported scheme, connection errors, etc.
|
2016-04-20 19:49:22 +03:00
|
|
|
Internal(String),
|
2016-04-27 11:26:05 +03:00
|
|
|
LoadCancelled,
|
2016-04-20 19:49:22 +03:00
|
|
|
/// SSL validation error that has to be handled in the HTML parser
|
2016-06-30 00:47:57 +03:00
|
|
|
SslValidation(Url, String),
|
2016-04-20 19:49:22 +03:00
|
|
|
}
|
2016-06-07 19:09:14 +03:00
|
|
|
|
|
|
|
/// Normalize `slice`, as defined by
|
|
|
|
/// [the Fetch Spec](https://fetch.spec.whatwg.org/#concept-header-value-normalize).
|
|
|
|
pub fn trim_http_whitespace(mut slice: &[u8]) -> &[u8] {
|
|
|
|
const HTTP_WS_BYTES: &'static [u8] = b"\x09\x0A\x0D\x20";
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match slice.split_first() {
|
|
|
|
Some((first, remainder)) if HTTP_WS_BYTES.contains(first) =>
|
|
|
|
slice = remainder,
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match slice.split_last() {
|
|
|
|
Some((last, remainder)) if HTTP_WS_BYTES.contains(last) =>
|
|
|
|
slice = remainder,
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
slice
|
|
|
|
}
|