mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
Update hyper to 0.12
This commit is contained in:
parent
95bfaa0a77
commit
024b40b39d
122 changed files with 3835 additions and 3448 deletions
|
@ -12,16 +12,20 @@ test = false
|
|||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
cookie = "0.10"
|
||||
cookie = "0.11"
|
||||
embedder_traits = { path = "../embedder_traits" }
|
||||
hyper = "0.10"
|
||||
hyper_serde = "0.8"
|
||||
headers-core = "0.0.1"
|
||||
headers-ext = "0.0.3"
|
||||
http = "0.1"
|
||||
hyper = "0.12"
|
||||
hyper_serde = "0.9"
|
||||
image = "0.19"
|
||||
ipc-channel = "0.11"
|
||||
lazy_static = "1"
|
||||
log = "0.4"
|
||||
malloc_size_of = { path = "../malloc_size_of" }
|
||||
malloc_size_of_derive = { path = "../malloc_size_of_derive" }
|
||||
mime = "0.3"
|
||||
msg = {path = "../msg"}
|
||||
num-traits = "0.2"
|
||||
pixels = {path = "../pixels"}
|
||||
|
|
|
@ -7,6 +7,9 @@
|
|||
|
||||
extern crate cookie as cookie_rs;
|
||||
extern crate embedder_traits;
|
||||
extern crate headers_core;
|
||||
extern crate headers_ext;
|
||||
extern crate http;
|
||||
extern crate hyper;
|
||||
extern crate hyper_serde;
|
||||
extern crate image as piston_image;
|
||||
|
@ -15,38 +18,43 @@ extern crate ipc_channel;
|
|||
#[macro_use] extern crate log;
|
||||
#[macro_use] extern crate malloc_size_of;
|
||||
#[macro_use] extern crate malloc_size_of_derive;
|
||||
extern crate mime;
|
||||
extern crate msg;
|
||||
extern crate num_traits;
|
||||
extern crate pixels;
|
||||
#[macro_use] extern crate serde;
|
||||
extern crate servo_arc;
|
||||
extern crate servo_url;
|
||||
extern crate url;
|
||||
#[macro_use] extern crate url;
|
||||
extern crate uuid;
|
||||
extern crate webrender_api;
|
||||
|
||||
use cookie_rs::Cookie;
|
||||
use filemanager_thread::FileManagerThreadMsg;
|
||||
use headers_core::HeaderMapExt;
|
||||
use headers_ext::{ContentType, ReferrerPolicy as ReferrerPolicyHeader};
|
||||
use http::{Error as HttpError, HeaderMap};
|
||||
use hyper::Error as HyperError;
|
||||
use hyper::header::{ContentType, Headers, ReferrerPolicy as ReferrerPolicyHeader};
|
||||
use hyper::http::RawStatus;
|
||||
use hyper::mime::{Attr, Mime};
|
||||
use hyper::StatusCode;
|
||||
use hyper_serde::Serde;
|
||||
use ipc_channel::Error as IpcError;
|
||||
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
|
||||
use ipc_channel::router::ROUTER;
|
||||
use mime::Mime;
|
||||
use msg::constellation_msg::HistoryStateId;
|
||||
use request::{Request, RequestInit};
|
||||
use response::{HttpsState, Response, ResponseInit};
|
||||
use servo_url::ServoUrl;
|
||||
use std::error::Error;
|
||||
use storage_thread::StorageThreadMsg;
|
||||
use url::percent_encoding;
|
||||
|
||||
pub mod blob_url_store;
|
||||
pub mod filemanager_thread;
|
||||
pub mod image_cache;
|
||||
pub mod net_error_list;
|
||||
pub mod pub_domains;
|
||||
pub mod quality;
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
pub mod storage_thread;
|
||||
|
@ -80,16 +88,16 @@ pub struct CustomResponse {
|
|||
#[ignore_malloc_size_of = "Defined in hyper"]
|
||||
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
||||
serialize_with = "::hyper_serde::serialize")]
|
||||
pub headers: Headers,
|
||||
pub headers: HeaderMap,
|
||||
#[ignore_malloc_size_of = "Defined in hyper"]
|
||||
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
||||
serialize_with = "::hyper_serde::serialize")]
|
||||
pub raw_status: RawStatus,
|
||||
pub raw_status: (StatusCode, String),
|
||||
pub body: Vec<u8>,
|
||||
}
|
||||
|
||||
impl CustomResponse {
|
||||
pub fn new(headers: Headers, raw_status: RawStatus, body: Vec<u8>) -> CustomResponse {
|
||||
pub fn new(headers: HeaderMap, raw_status: (StatusCode, String), body: Vec<u8>) -> CustomResponse {
|
||||
CustomResponse {
|
||||
headers: headers,
|
||||
raw_status: raw_status,
|
||||
|
@ -126,24 +134,24 @@ pub enum ReferrerPolicy {
|
|||
StrictOriginWhenCrossOrigin,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ReferrerPolicyHeader> for ReferrerPolicy {
|
||||
fn from(policy: &'a ReferrerPolicyHeader) -> Self {
|
||||
match *policy {
|
||||
ReferrerPolicyHeader::NoReferrer =>
|
||||
impl From<ReferrerPolicyHeader> for ReferrerPolicy {
|
||||
fn from(policy: ReferrerPolicyHeader) -> Self {
|
||||
match policy {
|
||||
ReferrerPolicyHeader::NO_REFERRER =>
|
||||
ReferrerPolicy::NoReferrer,
|
||||
ReferrerPolicyHeader::NoReferrerWhenDowngrade =>
|
||||
ReferrerPolicyHeader::NO_REFERRER_WHEN_DOWNGRADE =>
|
||||
ReferrerPolicy::NoReferrerWhenDowngrade,
|
||||
ReferrerPolicyHeader::SameOrigin =>
|
||||
ReferrerPolicyHeader::SAME_ORIGIN =>
|
||||
ReferrerPolicy::SameOrigin,
|
||||
ReferrerPolicyHeader::Origin =>
|
||||
ReferrerPolicyHeader::ORIGIN =>
|
||||
ReferrerPolicy::Origin,
|
||||
ReferrerPolicyHeader::OriginWhenCrossOrigin =>
|
||||
ReferrerPolicyHeader::ORIGIN_WHEN_CROSS_ORIGIN =>
|
||||
ReferrerPolicy::OriginWhenCrossOrigin,
|
||||
ReferrerPolicyHeader::UnsafeUrl =>
|
||||
ReferrerPolicyHeader::UNSAFE_URL =>
|
||||
ReferrerPolicy::UnsafeUrl,
|
||||
ReferrerPolicyHeader::StrictOrigin =>
|
||||
ReferrerPolicyHeader::STRICT_ORIGIN =>
|
||||
ReferrerPolicy::StrictOrigin,
|
||||
ReferrerPolicyHeader::StrictOriginWhenCrossOrigin =>
|
||||
ReferrerPolicyHeader::STRICT_ORIGIN_WHEN_CROSS_ORIGIN =>
|
||||
ReferrerPolicy::StrictOriginWhenCrossOrigin,
|
||||
}
|
||||
}
|
||||
|
@ -419,7 +427,7 @@ pub struct Metadata {
|
|||
|
||||
#[ignore_malloc_size_of = "Defined in hyper"]
|
||||
/// Headers
|
||||
pub headers: Option<Serde<Headers>>,
|
||||
pub headers: Option<Serde<HeaderMap>>,
|
||||
|
||||
/// HTTP Status
|
||||
pub status: Option<(u16, Vec<u8>)>,
|
||||
|
@ -454,16 +462,15 @@ impl Metadata {
|
|||
/// Extract the parts of a Mime that we care about.
|
||||
pub fn set_content_type(&mut self, content_type: Option<&Mime>) {
|
||||
if self.headers.is_none() {
|
||||
self.headers = Some(Serde(Headers::new()));
|
||||
self.headers = Some(Serde(HeaderMap::new()));
|
||||
}
|
||||
|
||||
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());
|
||||
self.headers.as_mut().unwrap().typed_insert(ContentType::from(mime.clone()));
|
||||
self.content_type = Some(Serde(ContentType::from(mime.clone())));
|
||||
for (name, value) in mime.params() {
|
||||
if mime::CHARSET == name {
|
||||
self.charset = Some(value.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -518,18 +525,16 @@ pub enum NetworkError {
|
|||
}
|
||||
|
||||
impl NetworkError {
|
||||
pub fn from_hyper_error(url: &ServoUrl, error: HyperError) -> Self {
|
||||
if let HyperError::Ssl(ref ssl_error) = error {
|
||||
return NetworkError::from_ssl_error(url, &**ssl_error);
|
||||
}
|
||||
pub fn from_hyper_error(error: &HyperError) -> Self {
|
||||
NetworkError::Internal(error.description().to_owned())
|
||||
}
|
||||
|
||||
pub fn from_ssl_error(url: &ServoUrl, error: &Error) -> Self {
|
||||
NetworkError::SslValidation(url.clone(), error.description().to_owned())
|
||||
pub fn from_http_error(error: &HttpError) -> Self {
|
||||
NetworkError::Internal(error.description().to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 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] {
|
||||
|
@ -551,3 +556,17 @@ pub fn trim_http_whitespace(mut slice: &[u8]) -> &[u8] {
|
|||
|
||||
slice
|
||||
}
|
||||
|
||||
pub fn http_percent_encode(bytes: &[u8]) -> String {
|
||||
define_encode_set! {
|
||||
// This encode set is used for HTTP header values and is defined at
|
||||
// https://tools.ietf.org/html/rfc5987#section-3.2
|
||||
pub HTTP_VALUE = [percent_encoding::SIMPLE_ENCODE_SET] | {
|
||||
' ', '"', '%', '\'', '(', ')', '*', ',', '/', ':', ';', '<', '-', '>', '?',
|
||||
'[', '\\', ']', '{', '}'
|
||||
}
|
||||
}
|
||||
|
||||
url::percent_encoding::percent_encode(bytes, HTTP_VALUE).to_string()
|
||||
}
|
||||
|
||||
|
|
80
components/net_traits/quality.rs
Normal file
80
components/net_traits/quality.rs
Normal file
|
@ -0,0 +1,80 @@
|
|||
/* 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/. */
|
||||
|
||||
//TODO(eijebong): Remove this once typed headers figure out quality
|
||||
// This is copy pasted from the old hyper headers to avoid hardcoding everything
|
||||
// (I would probably also make some silly mistakes while migrating...)
|
||||
|
||||
use http::header::HeaderValue;
|
||||
use mime::Mime;
|
||||
use std::{fmt, str};
|
||||
|
||||
/// A quality value, as specified in [RFC7231].
|
||||
///
|
||||
/// Quality values are decimal numbers between 0 and 1 (inclusive) with up to 3 fractional digits of precision.
|
||||
///
|
||||
/// [RFC7231]: https://tools.ietf.org/html/rfc7231#section-5.3.1
|
||||
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
|
||||
pub struct Quality(u16);
|
||||
|
||||
impl Quality {
|
||||
/// Creates a quality value from a value between 0 and 1000 inclusive.
|
||||
///
|
||||
/// This is semantically divided by 1000 to produce a value between 0 and 1.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the value is greater than 1000.
|
||||
pub fn from_u16(quality: u16) -> Quality {
|
||||
assert!(quality <= 1000);
|
||||
Quality(quality)
|
||||
}
|
||||
}
|
||||
|
||||
/// A value paired with its "quality" as defined in [RFC7231].
|
||||
///
|
||||
/// Quality items are used in content negotiation headers such as `Accept` and `Accept-Encoding`.
|
||||
///
|
||||
/// [RFC7231]: https://tools.ietf.org/html/rfc7231#section-5.3
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct QualityItem<T> {
|
||||
pub item: T,
|
||||
pub quality: Quality,
|
||||
}
|
||||
|
||||
impl<T> QualityItem<T> {
|
||||
/// Creates a new quality item.
|
||||
pub fn new(item: T, quality: Quality) -> QualityItem<T> {
|
||||
QualityItem { item, quality }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Display for QualityItem<T>
|
||||
where
|
||||
T: fmt::Display,
|
||||
{
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Display::fmt(&self.item, fmt)?;
|
||||
match self.quality.0 {
|
||||
1000 => Ok(()),
|
||||
0 => fmt.write_str("; q=0"),
|
||||
mut x => {
|
||||
fmt.write_str("; q=0.")?;
|
||||
let mut digits = *b"000";
|
||||
digits[2] = (x % 10) as u8 + b'0';
|
||||
x /= 10;
|
||||
digits[1] = (x % 10) as u8 + b'0';
|
||||
x /= 10;
|
||||
digits[0] = (x % 10) as u8 + b'0';
|
||||
|
||||
let s = str::from_utf8(&digits[..]).unwrap();
|
||||
fmt.write_str(s.trim_right_matches('0'))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn quality_to_value(q: Vec<QualityItem<Mime>>) -> HeaderValue {
|
||||
HeaderValue::from_str(&q.iter().map(|q| q.to_string()).collect::<Vec<String>>().join(", ")).unwrap()
|
||||
}
|
|
@ -3,8 +3,8 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use ReferrerPolicy;
|
||||
use hyper::header::Headers;
|
||||
use hyper::method::Method;
|
||||
use http::HeaderMap;
|
||||
use hyper::Method;
|
||||
use msg::constellation_msg::PipelineId;
|
||||
use servo_url::{ImmutableOrigin, ServoUrl};
|
||||
use std::default::Default;
|
||||
|
@ -145,7 +145,7 @@ pub struct RequestInit {
|
|||
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
||||
serialize_with = "::hyper_serde::serialize")]
|
||||
#[ignore_malloc_size_of = "Defined in hyper"]
|
||||
pub headers: Headers,
|
||||
pub headers: HeaderMap,
|
||||
pub unsafe_request: bool,
|
||||
pub body: Option<Vec<u8>>,
|
||||
pub service_workers_mode: ServiceWorkersMode,
|
||||
|
@ -171,9 +171,9 @@ pub struct RequestInit {
|
|||
impl Default for RequestInit {
|
||||
fn default() -> RequestInit {
|
||||
RequestInit {
|
||||
method: Method::Get,
|
||||
method: Method::GET,
|
||||
url: ServoUrl::parse("about:blank").unwrap(),
|
||||
headers: Headers::new(),
|
||||
headers: HeaderMap::new(),
|
||||
unsafe_request: false,
|
||||
body: None,
|
||||
service_workers_mode: ServiceWorkersMode::All,
|
||||
|
@ -208,7 +208,7 @@ pub struct Request {
|
|||
pub sandboxed_storage_area_urls: bool,
|
||||
/// <https://fetch.spec.whatwg.org/#concept-request-header-list>
|
||||
#[ignore_malloc_size_of = "Defined in hyper"]
|
||||
pub headers: Headers,
|
||||
pub headers: HeaderMap,
|
||||
/// <https://fetch.spec.whatwg.org/#unsafe-request-flag>
|
||||
pub unsafe_request: bool,
|
||||
/// <https://fetch.spec.whatwg.org/#concept-request-body>
|
||||
|
@ -264,10 +264,10 @@ impl Request {
|
|||
pipeline_id: Option<PipelineId>)
|
||||
-> Request {
|
||||
Request {
|
||||
method: Method::Get,
|
||||
method: Method::GET,
|
||||
local_urls_only: false,
|
||||
sandboxed_storage_area_urls: false,
|
||||
headers: Headers::new(),
|
||||
headers: HeaderMap::new(),
|
||||
unsafe_request: false,
|
||||
body: None,
|
||||
window: Window::Client,
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
//! The [Response](https://fetch.spec.whatwg.org/#responses) object
|
||||
//! resulting from a [fetch operation](https://fetch.spec.whatwg.org/#concept-fetch)
|
||||
use {FetchMetadata, FilteredMetadata, Metadata, NetworkError, ReferrerPolicy};
|
||||
use hyper::header::{AccessControlExposeHeaders, ContentType, Headers};
|
||||
use hyper::status::StatusCode;
|
||||
use headers_core::HeaderMapExt;
|
||||
use headers_ext::{AccessControlExposeHeaders, ContentType};
|
||||
use http::{HeaderMap, StatusCode};
|
||||
use hyper_serde::Serde;
|
||||
use servo_arc::Arc;
|
||||
use servo_url::ServoUrl;
|
||||
|
@ -81,7 +82,7 @@ pub struct ResponseInit {
|
|||
#[serde(deserialize_with = "::hyper_serde::deserialize",
|
||||
serialize_with = "::hyper_serde::serialize")]
|
||||
#[ignore_malloc_size_of = "Defined in hyper"]
|
||||
pub headers: Headers,
|
||||
pub headers: HeaderMap,
|
||||
pub status_code: u16,
|
||||
pub referrer: Option<ServoUrl>,
|
||||
pub location_url: Option<Result<ServoUrl, String>>,
|
||||
|
@ -96,10 +97,10 @@ pub struct Response {
|
|||
pub url_list: Vec<ServoUrl>,
|
||||
/// `None` can be considered a StatusCode of `0`.
|
||||
#[ignore_malloc_size_of = "Defined in hyper"]
|
||||
pub status: Option<StatusCode>,
|
||||
pub status: Option<(StatusCode, String)>,
|
||||
pub raw_status: Option<(u16, Vec<u8>)>,
|
||||
#[ignore_malloc_size_of = "Defined in hyper"]
|
||||
pub headers: Headers,
|
||||
pub headers: HeaderMap,
|
||||
#[ignore_malloc_size_of = "Mutex heap size undefined"]
|
||||
pub body: Arc<Mutex<ResponseBody>>,
|
||||
pub cache_state: CacheState,
|
||||
|
@ -127,9 +128,9 @@ impl Response {
|
|||
termination_reason: None,
|
||||
url: Some(url),
|
||||
url_list: vec![],
|
||||
status: Some(StatusCode::Ok),
|
||||
status: Some((StatusCode::OK, "OK".to_string())),
|
||||
raw_status: Some((200, b"OK".to_vec())),
|
||||
headers: Headers::new(),
|
||||
headers: HeaderMap::new(),
|
||||
body: Arc::new(Mutex::new(ResponseBody::Empty)),
|
||||
cache_state: CacheState::None,
|
||||
https_state: HttpsState::None,
|
||||
|
@ -148,7 +149,7 @@ impl Response {
|
|||
res.location_url = init.location_url;
|
||||
res.headers = init.headers;
|
||||
res.referrer = init.referrer;
|
||||
res.status = Some(StatusCode::from_u16(init.status_code));
|
||||
res.status = StatusCode::from_u16(init.status_code).map(|s| (s, s.to_string())).ok();
|
||||
res
|
||||
}
|
||||
|
||||
|
@ -160,7 +161,7 @@ impl Response {
|
|||
url_list: vec![],
|
||||
status: None,
|
||||
raw_status: None,
|
||||
headers: Headers::new(),
|
||||
headers: HeaderMap::new(),
|
||||
body: Arc::new(Mutex::new(ResponseBody::Empty)),
|
||||
cache_state: CacheState::None,
|
||||
https_state: HttpsState::None,
|
||||
|
@ -242,45 +243,43 @@ impl Response {
|
|||
ResponseType::Error(..) => unreachable!(),
|
||||
|
||||
ResponseType::Basic => {
|
||||
let headers = old_headers.iter().filter(|header| {
|
||||
match &*header.name().to_ascii_lowercase() {
|
||||
let headers = old_headers.iter().filter(|(name, _)| {
|
||||
match &*name.as_str().to_ascii_lowercase() {
|
||||
"set-cookie" | "set-cookie2" => false,
|
||||
_ => true
|
||||
}
|
||||
}).collect();
|
||||
}).map(|(n, v)| (n.clone(), v.clone())).collect();
|
||||
response.headers = headers;
|
||||
},
|
||||
|
||||
ResponseType::Cors => {
|
||||
let access = old_headers.get::<AccessControlExposeHeaders>();
|
||||
let allowed_headers = access.as_ref().map(|v| &v[..]).unwrap_or(&[]);
|
||||
|
||||
let headers = old_headers.iter().filter(|header| {
|
||||
match &*header.name().to_ascii_lowercase() {
|
||||
let headers = old_headers.iter().filter(|(name, _)| {
|
||||
match &*name.as_str().to_ascii_lowercase() {
|
||||
"cache-control" | "content-language" | "content-type" |
|
||||
"expires" | "last-modified" | "pragma" => true,
|
||||
"set-cookie" | "set-cookie2" => false,
|
||||
header => {
|
||||
let result =
|
||||
allowed_headers.iter().find(|h| *header == *h.to_ascii_lowercase());
|
||||
let access = old_headers.typed_get::<AccessControlExposeHeaders>();
|
||||
let result = access
|
||||
.and_then(|v| v.iter().find(|h| *header == h.as_str().to_ascii_lowercase()));
|
||||
result.is_some()
|
||||
}
|
||||
}
|
||||
}).collect();
|
||||
}).map(|(n, v)| (n.clone(), v.clone())).collect();
|
||||
response.headers = headers;
|
||||
},
|
||||
|
||||
ResponseType::Opaque => {
|
||||
response.url_list = vec![];
|
||||
response.url = None;
|
||||
response.headers = Headers::new();
|
||||
response.headers = HeaderMap::new();
|
||||
response.status = None;
|
||||
response.body = Arc::new(Mutex::new(ResponseBody::Empty));
|
||||
response.cache_state = CacheState::None;
|
||||
},
|
||||
|
||||
ResponseType::OpaqueRedirect => {
|
||||
response.headers = Headers::new();
|
||||
response.headers = HeaderMap::new();
|
||||
response.status = None;
|
||||
response.body = Arc::new(Mutex::new(ResponseBody::Empty));
|
||||
response.cache_state = CacheState::None;
|
||||
|
@ -293,10 +292,7 @@ impl Response {
|
|||
pub fn metadata(&self) -> Result<FetchMetadata, NetworkError> {
|
||||
fn init_metadata(response: &Response, url: &ServoUrl) -> 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.set_content_type(response.headers.typed_get::<ContentType>().map(|v| v.into()).as_ref());
|
||||
metadata.location_url = response.location_url.clone();
|
||||
metadata.headers = Some(Serde(response.headers.clone()));
|
||||
metadata.status = response.raw_status.clone();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue