Implementing the builder pattern for RequestInit

This commit is contained in:
Lucas Fantacuci 2018-12-21 17:38:22 -02:00 committed by Lucas Sanches Fantacuci
parent dd2deeabca
commit 6b2be9b31d
27 changed files with 578 additions and 546 deletions

View file

@ -22,7 +22,7 @@ use embedder_traits::EmbedderProxy;
use hyper_serde::Serde;
use ipc_channel::ipc::{self, IpcReceiver, IpcReceiverSet, IpcSender};
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use net_traits::request::{Destination, Request, RequestInit};
use net_traits::request::{Destination, RequestBuilder};
use net_traits::response::{Response, ResponseInit};
use net_traits::storage_thread::StorageThreadMsg;
use net_traits::WebSocketNetworkEvent;
@ -430,7 +430,7 @@ impl CoreResourceManager {
fn fetch(
&self,
req_init: RequestInit,
request_builder: RequestBuilder,
res_init_: Option<ResponseInit>,
mut sender: IpcSender<FetchResponseMsg>,
http_state: &Arc<HttpState>,
@ -441,15 +441,15 @@ impl CoreResourceManager {
let dc = self.devtools_chan.clone();
let filemanager = self.filemanager.clone();
let timing_type = match req_init.destination {
let timing_type = match request_builder.destination {
Destination::Document => ResourceTimingType::Navigation,
_ => ResourceTimingType::Resource,
};
thread::Builder::new()
.name(format!("fetch thread for {}", req_init.url))
.name(format!("fetch thread for {}", request_builder.url))
.spawn(move || {
let mut request = Request::from_init(req_init);
let mut request = request_builder.build();
// XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed)
// todo load context / mimesniff in fetch
// todo referrer policy?
@ -486,7 +486,7 @@ impl CoreResourceManager {
fn websocket_connect(
&self,
request: RequestInit,
request: RequestBuilder,
event_sender: IpcSender<WebSocketNetworkEvent>,
action_receiver: IpcReceiver<WebSocketDomAction>,
http_state: &Arc<HttpState>,

View file

@ -29,7 +29,7 @@ use net::cookie::Cookie;
use net::cookie_storage::CookieStorage;
use net::resource_thread::AuthCacheEntry;
use net::test::replace_host_table;
use net_traits::request::{CredentialsMode, Destination, Request, RequestInit, RequestMode};
use net_traits::request::{CredentialsMode, Destination, RequestBuilder, RequestMode};
use net_traits::response::ResponseBody;
use net_traits::{CookieSource, NetworkError};
use servo_url::{ImmutableOrigin, ServoUrl};
@ -135,14 +135,13 @@ fn test_check_default_headers_loaded_in_every_request() {
*expected_headers.lock().unwrap() = Some(headers.clone());
// Testing for method.GET
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
destination: Destination::Document,
origin: url.clone().origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.destination(Destination::Document)
.origin(url.clone().origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
assert!(response
.internal_response
@ -163,14 +162,13 @@ fn test_check_default_headers_loaded_in_every_request() {
HeaderValue::from_str(&url_str[..url_str.len() - 1]).unwrap(),
);
*expected_headers.lock().unwrap() = Some(post_headers);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::POST,
destination: Destination::Document,
origin: url.clone().origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::POST)
.destination(Destination::Document)
.origin(url.clone().origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
assert!(response
.internal_response
@ -194,15 +192,14 @@ fn test_load_when_request_is_not_get_or_head_and_there_is_no_body_content_length
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::POST,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::POST)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
assert!(response
.internal_response
@ -227,16 +224,15 @@ fn test_request_and_response_data_with_network_messages() {
let mut request_headers = HeaderMap::new();
request_headers.typed_insert(Host::from("bar.foo".parse::<Authority>().unwrap()));
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
headers: request_headers,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.headers(request_headers)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let (devtools_chan, devtools_port) = unbounded();
let response = fetch(&mut request, Some(devtools_chan));
assert!(response
@ -328,14 +324,13 @@ fn test_request_and_response_message_from_devtool_without_pipeline_id() {
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: None,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(None)
.build();
let (devtools_chan, devtools_port) = unbounded();
let response = fetch(&mut request, Some(devtools_chan));
assert!(response
@ -371,13 +366,12 @@ fn test_redirected_request_to_devtools() {
};
let (pre_server, pre_url) = make_server(pre_handler);
let mut request = Request::from_init(RequestInit {
url: pre_url.clone(),
method: Method::POST,
destination: Destination::Document,
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(pre_url.clone())
.method(Method::POST)
.destination(Destination::Document)
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let (devtools_chan, devtools_port) = unbounded();
fetch(&mut request, Some(devtools_chan));
@ -421,14 +415,13 @@ fn test_load_when_redirecting_from_a_post_should_rewrite_next_request_as_get() {
};
let (pre_server, pre_url) = make_server(pre_handler);
let mut request = Request::from_init(RequestInit {
url: pre_url.clone(),
method: Method::POST,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(pre_url.clone())
.method(Method::POST)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = pre_server.close();
@ -452,15 +445,14 @@ fn test_load_should_decode_the_response_as_deflate_when_response_headers_have_co
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = server.close();
@ -486,15 +478,14 @@ fn test_load_should_decode_the_response_as_gzip_when_response_headers_have_conte
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = server.close();
@ -539,15 +530,14 @@ fn test_load_doesnt_send_request_body_on_any_redirect() {
};
let (pre_server, pre_url) = make_server(pre_handler);
let mut request = Request::from_init(RequestInit {
url: pre_url.clone(),
body: Some(b"Body on POST!".to_vec()),
method: Method::POST,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(pre_url.clone())
.body(Some(b"Body on POST!".to_vec()))
.method(Method::POST)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = pre_server.close();
@ -568,15 +558,14 @@ fn test_load_doesnt_add_host_to_sts_list_when_url_is_http_even_if_sts_headers_ar
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let mut context = new_fetch_context(None, None);
let response = fetch_with_context(&mut request, &mut context);
@ -615,16 +604,15 @@ fn test_load_sets_cookies_in_the_resource_manager_when_it_get_set_cookie_header_
assert_cookie_for_domain(&context.state.cookie_jar, url.as_str(), None);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
credentials_mode: CredentialsMode::Include,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.credentials_mode(CredentialsMode::Include)
.build();
let response = fetch_with_context(&mut request, &mut context);
let _ = server.close();
@ -668,16 +656,15 @@ fn test_load_sets_requests_cookies_header_for_url_by_getting_cookies_from_the_re
cookie_jar.push(cookie, &url, CookieSource::HTTP);
}
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
credentials_mode: CredentialsMode::Include,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.credentials_mode(CredentialsMode::Include)
.build();
let response = fetch_with_context(&mut request, &mut context);
let _ = server.close();
@ -715,16 +702,15 @@ fn test_load_sends_cookie_if_nonhttp() {
cookie_jar.push(cookie, &url, CookieSource::HTTP);
}
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
credentials_mode: CredentialsMode::Include,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.credentials_mode(CredentialsMode::Include)
.build();
let response = fetch_with_context(&mut request, &mut context);
let _ = server.close();
@ -753,16 +739,15 @@ fn test_cookie_set_with_httponly_should_not_be_available_using_getcookiesforurl(
assert_cookie_for_domain(&context.state.cookie_jar, url.as_str(), None);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
credentials_mode: CredentialsMode::Include,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.credentials_mode(CredentialsMode::Include)
.build();
let response = fetch_with_context(&mut request, &mut context);
let _ = server.close();
@ -801,16 +786,15 @@ fn test_when_cookie_received_marked_secure_is_ignored_for_http() {
assert_cookie_for_domain(&context.state.cookie_jar, url.as_str(), None);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
credentials_mode: CredentialsMode::Include,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.credentials_mode(CredentialsMode::Include)
.build();
let response = fetch_with_context(&mut request, &mut context);
let _ = server.close();
@ -839,15 +823,14 @@ fn test_load_sets_content_length_to_length_of_request_body() {
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::POST,
body: Some(content.to_vec()),
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::POST)
.body(Some(content.to_vec()))
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = server.close();
@ -879,15 +862,14 @@ fn test_load_uses_explicit_accept_from_headers_in_load_data() {
let mut accept_headers = HeaderMap::new();
accept_headers.insert(header::ACCEPT, HeaderValue::from_static("text/html"));
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
headers: accept_headers,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.headers(accept_headers)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = server.close();
@ -917,14 +899,13 @@ fn test_load_sets_default_accept_to_html_xhtml_xml_and_then_anything_else() {
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = server.close();
@ -956,15 +937,14 @@ fn test_load_uses_explicit_accept_encoding_from_load_data_headers() {
let mut accept_encoding_headers = HeaderMap::new();
accept_encoding_headers.insert(header::ACCEPT_ENCODING, HeaderValue::from_static("chunked"));
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
headers: accept_encoding_headers,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.headers(accept_encoding_headers)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = server.close();
@ -994,14 +974,13 @@ fn test_load_sets_default_accept_encoding_to_gzip_and_deflate() {
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = server.close();
@ -1048,14 +1027,13 @@ fn test_load_errors_when_there_a_redirect_loop() {
*url_b_for_a.lock().unwrap() = Some(url_b.clone());
let mut request = Request::from_init(RequestInit {
url: url_a.clone(),
method: Method::GET,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url_a.clone())
.method(Method::GET)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = server_a.close();
@ -1105,14 +1083,13 @@ fn test_load_succeeds_with_a_redirect_loop() {
*url_b_for_a.lock().unwrap() = Some(url_b.clone());
let mut request = Request::from_init(RequestInit {
url: url_a.clone(),
method: Method::GET,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url_a.clone())
.method(Method::GET)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = server_a.close();
@ -1145,14 +1122,13 @@ fn test_load_follows_a_redirect() {
};
let (pre_server, pre_url) = make_server(pre_handler);
let mut request = Request::from_init(RequestInit {
url: pre_url.clone(),
method: Method::GET,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
..RequestInit::default()
});
let mut request = RequestBuilder::new(pre_url.clone())
.method(Method::GET)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.build();
let response = fetch(&mut request, None);
let _ = pre_server.close();
@ -1229,15 +1205,14 @@ fn test_redirect_from_x_to_y_provides_y_cookies_from_y() {
cookie_jar.push(cookie_y, &url_y, CookieSource::HTTP);
}
let mut request = Request::from_init(RequestInit {
url: url_x.clone(),
method: Method::GET,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
credentials_mode: CredentialsMode::Include,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url_x.clone())
.method(Method::GET)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.credentials_mode(CredentialsMode::Include)
.build();
let response = fetch_with_context(&mut request, &mut context);
let _ = server.close();
@ -1279,15 +1254,14 @@ fn test_redirect_from_x_to_x_provides_x_with_cookie_from_first_response() {
let url = url.join("/initial/").unwrap();
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
credentials_mode: CredentialsMode::Include,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.credentials_mode(CredentialsMode::Include)
.build();
let response = fetch(&mut request, None);
let _ = server.close();
@ -1311,16 +1285,15 @@ fn test_if_auth_creds_not_in_url_but_in_cache_it_sets_it() {
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
credentials_mode: CredentialsMode::Include,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.credentials_mode(CredentialsMode::Include)
.build();
let mut context = new_fetch_context(None, None);
let auth_entry = AuthCacheEntry {
@ -1356,16 +1329,14 @@ fn test_auth_ui_needs_www_auth() {
};
let (server, url) = make_server(handler);
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
body: None,
destination: Destination::Document,
origin: mock_origin(),
pipeline_id: Some(TEST_PIPELINE_ID),
credentials_mode: CredentialsMode::Include,
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.body(None)
.destination(Destination::Document)
.origin(mock_origin())
.pipeline_id(Some(TEST_PIPELINE_ID))
.credentials_mode(CredentialsMode::Include)
.build();
let response = fetch(&mut request, None);
@ -1395,13 +1366,12 @@ fn test_origin_set() {
let mut origin =
Origin::try_from_parts(url.scheme(), url.host_str().unwrap(), url.port()).unwrap();
*origin_header_clone.lock().unwrap() = Some(origin.clone());
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::POST,
body: None,
origin: url.clone().origin(),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::POST)
.body(None)
.origin(url.clone().origin())
.build();
let response = fetch(&mut request, None);
assert!(response
.internal_response
@ -1415,14 +1385,12 @@ fn test_origin_set() {
origin =
Origin::try_from_parts(origin_url.scheme(), origin_url.host_str().unwrap(), None).unwrap();
// Test Origin header is set on Get request with CORS mode
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::GET,
mode: RequestMode::CorsMode,
body: None,
origin: origin_url.clone().origin(),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::GET)
.mode(RequestMode::CorsMode)
.body(None)
.origin(origin_url.clone().origin())
.build();
*origin_header_clone.lock().unwrap() = Some(origin.clone());
let response = fetch(&mut request, None);
@ -1435,13 +1403,11 @@ fn test_origin_set() {
.is_success());
// Test Origin header is not set on method Head
let mut request = Request::from_init(RequestInit {
url: url.clone(),
method: Method::HEAD,
body: None,
origin: url.clone().origin(),
..RequestInit::default()
});
let mut request = RequestBuilder::new(url.clone())
.method(Method::HEAD)
.body(None)
.origin(url.clone().origin())
.build();
*origin_header_clone.lock().unwrap() = None;
let response = fetch(&mut request, None);

View file

@ -12,7 +12,7 @@ use headers_ext::Host;
use http::header::{self, HeaderMap, HeaderName, HeaderValue};
use http::uri::Authority;
use ipc_channel::ipc::{IpcReceiver, IpcSender};
use net_traits::request::{RequestInit, RequestMode};
use net_traits::request::{RequestBuilder, RequestMode};
use net_traits::{CookieSource, MessageData};
use net_traits::{WebSocketDomAction, WebSocketNetworkEvent};
use openssl::ssl::SslStream;
@ -174,21 +174,23 @@ impl<'a> Handler for Client<'a> {
}
pub fn init(
req_init: RequestInit,
req_builder: RequestBuilder,
resource_event_sender: IpcSender<WebSocketNetworkEvent>,
dom_action_receiver: IpcReceiver<WebSocketDomAction>,
http_state: Arc<HttpState>,
) {
thread::Builder::new()
.name(format!("WebSocket connection to {}", req_init.url))
.name(format!("WebSocket connection to {}", req_builder.url))
.spawn(move || {
let protocols = match req_init.mode {
let protocols = match req_builder.mode {
RequestMode::WebSocket { protocols } => protocols,
_ => panic!("Received a RequestInit with a non-websocket mode in websocket_loader"),
_ => panic!(
"Received a RequestBuilder with a non-websocket mode in websocket_loader"
),
};
let scheme = req_init.url.scheme();
let mut req_url = req_init.url.clone();
let scheme = req_builder.url.scheme();
let mut req_url = req_builder.url.clone();
if scheme == "ws" {
req_url.as_mut_url().set_scheme("http").unwrap();
} else if scheme == "wss" {
@ -201,15 +203,15 @@ pub fn init(
return;
}
let host = replace_host(req_init.url.host_str().unwrap());
let mut net_url = req_init.url.clone().into_url();
let host = replace_host(req_builder.url.host_str().unwrap());
let mut net_url = req_builder.url.clone().into_url();
net_url.set_host(Some(&host)).unwrap();
let host = Host::from(
format!(
"{}{}",
req_init.url.host_str().unwrap(),
req_init
req_builder.url.host_str().unwrap(),
req_builder
.url
.port_or_known_default()
.map(|v| format!(":{}", v))
@ -220,11 +222,11 @@ pub fn init(
);
let client = Client {
origin: &req_init.origin.ascii_serialization(),
origin: &req_builder.origin.ascii_serialization(),
host: &host,
protocols: &protocols,
http_state: &http_state,
resource_url: &req_init.url,
resource_url: &req_builder.url,
event_sender: &resource_event_sender,
protocol_in_use: None,
};