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-05-05 17:11:30 +03:00
|
|
|
#![feature(slice_patterns)]
|
|
|
|
#![feature(step_by)]
|
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;
|
2017-01-03 19:11:09 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate heapsize_derive;
|
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;
|
2016-10-10 04:12:38 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2016-12-15 03:48:42 +03:00
|
|
|
extern crate servo_config;
|
2016-11-18 00:34:47 +03:00
|
|
|
extern crate servo_url;
|
2015-04-03 22:00:46 +03:00
|
|
|
extern crate url;
|
2016-05-11 14:28:58 +03:00
|
|
|
extern crate uuid;
|
2016-10-21 18:46:39 +03:00
|
|
|
extern crate webrender_traits;
|
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;
|
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-11-02 18:59:18 +03:00
|
|
|
use ipc_channel::router::ROUTER;
|
2016-06-12 03:30:28 +03:00
|
|
|
use request::{Request, RequestInit};
|
|
|
|
use response::{HttpsState, Response};
|
2016-11-18 00:34:47 +03:00
|
|
|
use servo_url::ServoUrl;
|
2016-05-20 05:15:08 +03:00
|
|
|
use std::io::Error as IOError;
|
2016-05-20 16:25:13 +03:00
|
|
|
use storage_thread::StorageThreadMsg;
|
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-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-10-01 05:57:36 +03:00
|
|
|
pub mod pub_domains;
|
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,
|
2017-01-03 19:11:09 +03:00
|
|
|
pub body: Vec<u8>,
|
2016-05-21 11:12:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CustomResponse {
|
|
|
|
pub fn new(headers: Headers, raw_status: RawStatus, body: Vec<u8>) -> CustomResponse {
|
2017-01-03 19:11:09 +03:00
|
|
|
CustomResponse {
|
|
|
|
headers: headers,
|
|
|
|
raw_status: raw_status,
|
|
|
|
body: body,
|
|
|
|
}
|
2016-05-21 11:12:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>>,
|
2016-11-18 00:34:47 +03:00
|
|
|
pub load_url: ServoUrl,
|
2016-05-21 11:12:54 +03:00
|
|
|
}
|
|
|
|
|
2016-11-04 13:26:27 +03:00
|
|
|
/// [Policies](https://w3c.github.io/webappsec-referrer-policy/#referrer-policy-states)
|
|
|
|
/// for providing a referrer header for a request
|
|
|
|
#[derive(Clone, Copy, Debug, Deserialize, HeapSizeOf, Serialize)]
|
|
|
|
pub enum ReferrerPolicy {
|
|
|
|
/// "no-referrer"
|
|
|
|
NoReferrer,
|
|
|
|
/// "no-referrer-when-downgrade"
|
|
|
|
NoReferrerWhenDowngrade,
|
|
|
|
/// "origin"
|
|
|
|
Origin,
|
|
|
|
/// "same-origin"
|
|
|
|
SameOrigin,
|
|
|
|
/// "origin-when-cross-origin"
|
|
|
|
OriginWhenCrossOrigin,
|
|
|
|
/// "unsafe-url"
|
|
|
|
UnsafeUrl,
|
2016-11-07 13:37:35 +03:00
|
|
|
/// "strict-origin"
|
|
|
|
StrictOrigin,
|
|
|
|
/// "strict-origin-when-cross-origin"
|
|
|
|
StrictOriginWhenCrossOrigin,
|
2016-11-04 13:26:27 +03:00
|
|
|
}
|
|
|
|
|
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,
|
2017-01-03 19:11:09 +03:00
|
|
|
Transparent(Metadata),
|
2016-09-22 02:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub enum FetchMetadata {
|
|
|
|
Unfiltered(Metadata),
|
|
|
|
Filtered {
|
|
|
|
filtered: FilteredMetadata,
|
2017-01-03 19:11:09 +03:00
|
|
|
unsafe_: Metadata,
|
|
|
|
},
|
2016-09-22 02:49:33 +03:00
|
|
|
}
|
|
|
|
|
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
|
2017-01-03 19:11:09 +03:00
|
|
|
let _ =
|
|
|
|
self.send(FetchResponseMsg::ProcessResponseEOF(Err(NetworkError::Internal("Network error".into()))));
|
2016-06-12 03:30:28 +03:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2017-01-03 19:11:09 +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 {
|
2017-01-03 19:11:09 +03:00
|
|
|
pub fn new(c: CoreResourceThread, 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 {
|
2017-01-03 19:11:09 +03:00
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
0
|
|
|
|
}
|
2016-05-20 05:15:08 +03:00
|
|
|
}
|
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,
|
2017-01-03 19:11:09 +03:00
|
|
|
NotIncluded,
|
2015-07-22 19:23:05 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-01-03 19:11:09 +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 {
|
2016-11-18 00:34:47 +03:00
|
|
|
pub resource_url: ServoUrl,
|
2015-12-08 18:10:50 +03:00
|
|
|
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 {
|
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),
|
2016-12-16 02:54:39 +03:00
|
|
|
/// Store a cookie for a given originating URL
|
2017-01-03 19:11:09 +03:00
|
|
|
SetCookieForUrl(ServoUrl,
|
|
|
|
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
|
|
|
serialize_with = "::hyper_serde::serialize")]
|
|
|
|
Cookie,
|
|
|
|
CookieSource),
|
2016-12-16 02:54:39 +03:00
|
|
|
/// Store cookies for a given originating URL
|
|
|
|
SetCookiesForUrl(ServoUrl, Vec<Serde<Cookie>>, CookieSource),
|
2015-04-03 22:00:46 +03:00
|
|
|
/// Retrieve the stored cookies for a given URL
|
2016-11-18 00:34:47 +03:00
|
|
|
GetCookiesForUrl(ServoUrl, IpcSender<Option<String>>, CookieSource),
|
2016-06-26 11:19:14 +03:00
|
|
|
/// Get a cookie by name for a given originating URL
|
2016-11-18 00:34:47 +03:00
|
|
|
GetCookiesDataForUrl(ServoUrl, 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
|
2016-12-09 03:10:08 +03:00
|
|
|
/// and exit
|
2016-06-01 14:46:58 +03:00
|
|
|
Exit(IpcSender<()>),
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
|
2016-10-04 16:05:44 +03:00
|
|
|
/// Instruct the resource thread to make a new request.
|
2017-01-03 19:11:09 +03:00
|
|
|
pub fn fetch_async<F>(request: RequestInit, core_resource_thread: &CoreResourceThread, f: F)
|
|
|
|
where F: Fn(FetchResponseMsg) + Send + 'static,
|
2016-11-02 18:59:18 +03:00
|
|
|
{
|
|
|
|
let (action_sender, action_receiver) = ipc::channel().unwrap();
|
2017-01-03 19:11:09 +03:00
|
|
|
ROUTER.add_route(action_receiver.to_opaque(),
|
|
|
|
box move |message| f(message.to().unwrap()));
|
2016-11-02 18:59:18 +03:00
|
|
|
core_resource_thread.send(CoreResourceMsg::Fetch(request, action_sender)).unwrap();
|
2015-05-11 22:35:33 +03:00
|
|
|
}
|
|
|
|
|
2015-08-13 22:16:14 +03:00
|
|
|
#[derive(Clone, Deserialize, Serialize, HeapSizeOf)]
|
2016-11-10 23:43:36 +03:00
|
|
|
pub struct ResourceCorsData {
|
2015-04-03 22:00:46 +03:00
|
|
|
/// CORS Preflight flag
|
|
|
|
pub preflight: bool,
|
|
|
|
/// Origin of CORS Request
|
2016-11-18 00:34:47 +03:00
|
|
|
pub origin: ServoUrl,
|
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.
|
2016-11-18 00:34:47 +03:00
|
|
|
pub final_url: ServoUrl,
|
2015-04-03 22:00:46 +03:00
|
|
|
|
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
|
2016-11-18 00:34:47 +03:00
|
|
|
pub referrer: Option<ServoUrl>,
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Metadata {
|
|
|
|
/// Metadata with defaults for everything optional.
|
2016-11-18 00:34:47 +03:00
|
|
|
pub fn default(url: ServoUrl) -> Self {
|
2015-04-03 22:00:46 +03:00
|
|
|
Metadata {
|
2017-01-03 19:11:09 +03:00
|
|
|
final_url: url,
|
2015-04-03 22:00:46 +03:00
|
|
|
content_type: None,
|
2017-01-03 19:11:09 +03:00
|
|
|
charset: None,
|
2015-04-03 22:00:46 +03:00
|
|
|
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-12-25 22:04:21 +03:00
|
|
|
if self.headers.is_none() {
|
|
|
|
self.headers = Some(Serde(Headers::new()));
|
2016-04-03 18:15:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-25 22:04:21 +03:00
|
|
|
if let Some(mime) = content_type {
|
|
|
|
self.headers.as_mut().unwrap().set(ContentType(mime.clone()));
|
|
|
|
self.content_type = Some(Serde(ContentType(mime.clone())));
|
|
|
|
let Mime(_, _, ref parameters) = *mime;
|
|
|
|
for &(ref k, ref v) in parameters {
|
|
|
|
if Attr::Charset == *k {
|
|
|
|
self.charset = Some(v.to_string());
|
2015-04-03 22:00:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function for synchronously loading a whole resource.
|
2016-11-02 18:59:18 +03:00
|
|
|
pub fn load_whole_resource(request: RequestInit,
|
|
|
|
core_resource_thread: &CoreResourceThread)
|
|
|
|
-> Result<(Metadata, Vec<u8>), NetworkError> {
|
|
|
|
let (action_sender, action_receiver) = ipc::channel().unwrap();
|
|
|
|
core_resource_thread.send(CoreResourceMsg::Fetch(request, action_sender)).unwrap();
|
2015-04-03 22:00:46 +03:00
|
|
|
|
2017-01-03 19:11:09 +03:00
|
|
|
let mut buf = vec![];
|
2016-11-02 18:59:18 +03:00
|
|
|
let mut metadata = None;
|
2015-04-03 22:00:46 +03:00
|
|
|
loop {
|
2016-11-02 18:59:18 +03:00
|
|
|
match action_receiver.recv().unwrap() {
|
|
|
|
FetchResponseMsg::ProcessRequestBody |
|
|
|
|
FetchResponseMsg::ProcessRequestEOF => (),
|
|
|
|
FetchResponseMsg::ProcessResponse(Ok(m)) => {
|
|
|
|
metadata = Some(match m {
|
|
|
|
FetchMetadata::Unfiltered(m) => m,
|
2017-01-03 19:11:09 +03:00
|
|
|
FetchMetadata::Filtered { unsafe_, .. } => unsafe_,
|
2016-11-02 18:59:18 +03:00
|
|
|
})
|
|
|
|
},
|
|
|
|
FetchResponseMsg::ProcessResponseChunk(data) => buf.extend_from_slice(&data),
|
|
|
|
FetchResponseMsg::ProcessResponseEOF(Ok(())) => return Ok((metadata.unwrap(), buf)),
|
|
|
|
FetchResponseMsg::ProcessResponse(Err(e)) |
|
2017-01-03 19:11:09 +03:00
|
|
|
FetchResponseMsg::ProcessResponseEOF(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-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-11-18 00:34:47 +03:00
|
|
|
SslValidation(ServoUrl, 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() {
|
2017-01-03 19:11:09 +03:00
|
|
|
Some((first, remainder)) if HTTP_WS_BYTES.contains(first) => slice = remainder,
|
2016-06-07 19:09:14 +03:00
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match slice.split_last() {
|
2017-01-03 19:11:09 +03:00
|
|
|
Some((last, remainder)) if HTTP_WS_BYTES.contains(last) => slice = remainder,
|
2016-06-07 19:09:14 +03:00
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
slice
|
|
|
|
}
|