use std::rc::Rc;
use std::sync::{Arc, Mutex};
use ipc_channel::ipc;
use net_traits::request::{
CorsSettings, CredentialsMode, Destination, Referrer, Request as NetTraitsRequest,
RequestBuilder, RequestId, RequestMode, ServiceWorkersMode,
};
use net_traits::{
CoreResourceMsg, CoreResourceThread, FetchChannels, FetchMetadata, FetchResponseListener,
FetchResponseMsg, FilteredMetadata, Metadata, NetworkError, ResourceFetchTiming,
ResourceTimingType,
};
use servo_url::ServoUrl;
use crate::dom::bindings::codegen::Bindings::RequestBinding::{
RequestInfo, RequestInit, RequestMethods,
};
use crate::dom::bindings::codegen::Bindings::ResponseBinding::ResponseType as DOMResponseType;
use crate::dom::bindings::codegen::Bindings::ResponseBinding::Response_Binding::ResponseMethods;
use crate::dom::bindings::error::Error;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::trace::RootedTraceableBox;
use crate::dom::globalscope::GlobalScope;
use crate::dom::headers::Guard;
use crate::dom::performanceresourcetiming::InitiatorType;
use crate::dom::promise::Promise;
use crate::dom::request::Request;
use crate::dom::response::Response;
use crate::dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
use crate::network_listener::{self, submit_timing_data, PreInvoke, ResourceTimingListener};
use crate::realms::{enter_realm, InRealm};
use crate::script_runtime::CanGc;
struct FetchContext {
fetch_promise: Option<TrustedPromise>,
response_object: Trusted<Response>,
resource_timing: ResourceFetchTiming,
}
#[derive(Default, JSTraceable, MallocSizeOf)]
pub struct FetchCanceller {
#[ignore_malloc_size_of = "channels are hard"]
#[no_trace]
cancel_chan: Option<ipc::IpcSender<()>>,
}
impl FetchCanceller {
pub fn new() -> Self {
Default::default()
}
pub fn initialize(&mut self) -> ipc::IpcReceiver<()> {
self.cancel();
let (rx, tx) = ipc::channel().unwrap();
self.cancel_chan = Some(rx);
tx
}
pub fn cancel(&mut self) {
if let Some(chan) = self.cancel_chan.take() {
let _ = chan.send(());
}
}
pub fn ignore(&mut self) {
let _ = self.cancel_chan.take();
}
}
impl Drop for FetchCanceller {
fn drop(&mut self) {
self.cancel()
}
}
fn request_init_from_request(request: NetTraitsRequest) -> RequestBuilder {
RequestBuilder {
id: request.id,
method: request.method.clone(),
url: request.url(),
headers: request.headers.clone(),
unsafe_request: request.unsafe_request,
body: request.body.clone(),
service_workers_mode: ServiceWorkersMode::All,
destination: request.destination,
synchronous: request.synchronous,
mode: request.mode.clone(),
cache_mode: request.cache_mode,
use_cors_preflight: request.use_cors_preflight,
credentials_mode: request.credentials_mode,
use_url_credentials: request.use_url_credentials,
origin: GlobalScope::current()
.expect("No current global object")
.origin()
.immutable()
.clone(),
referrer: request.referrer.clone(),
referrer_policy: request.referrer_policy,
pipeline_id: request.pipeline_id,
redirect_mode: request.redirect_mode,
integrity_metadata: request.integrity_metadata.clone(),
url_list: vec![],
parser_metadata: request.parser_metadata,
initiator: request.initiator,
csp_list: None,
https_state: request.https_state,
response_tainting: request.response_tainting,
crash: None,
}
}
#[allow(crown::unrooted_must_root, non_snake_case)]
pub fn Fetch(
global: &GlobalScope,
input: RequestInfo,
init: RootedTraceableBox<RequestInit>,
comp: InRealm,
can_gc: CanGc,
) -> Rc<Promise> {
let promise = Promise::new_in_current_realm(comp, can_gc);
let response = Response::new(global, can_gc);
response.Headers(can_gc).set_guard(Guard::Immutable);
let request = match Request::Constructor(global, None, can_gc, input, init) {
Err(e) => {
response.error_stream(e.clone());
promise.reject_error(e);
return promise;
},
Ok(r) => {
r.get_request()
},
};
let timing_type = request.timing_type();
let mut request_init = request_init_from_request(request);
request_init.csp_list.clone_from(&global.get_csp_list());
if global.is::<ServiceWorkerGlobalScope>() {
request_init.service_workers_mode = ServiceWorkersMode::None;
}
let fetch_context = Arc::new(Mutex::new(FetchContext {
fetch_promise: Some(TrustedPromise::new(promise.clone())),
response_object: Trusted::new(&*response),
resource_timing: ResourceFetchTiming::new(timing_type),
}));
global.fetch(
request_init,
fetch_context,
global.networking_task_source(),
None,
);
promise
}
impl PreInvoke for FetchContext {}
impl FetchResponseListener for FetchContext {
fn process_request_body(&mut self, _: RequestId) {
}
fn process_request_eof(&mut self, _: RequestId) {
}
#[allow(crown::unrooted_must_root)]
fn process_response(
&mut self,
_: RequestId,
fetch_metadata: Result<FetchMetadata, NetworkError>,
) {
let promise = self
.fetch_promise
.take()
.expect("fetch promise is missing")
.root();
let _ac = enter_realm(&*promise);
match fetch_metadata {
Err(_) => {
promise.reject_error(Error::Type("Network error occurred".to_string()));
self.fetch_promise = Some(TrustedPromise::new(promise));
let response = self.response_object.root();
response.set_type(DOMResponseType::Error, CanGc::note());
response.error_stream(Error::Type("Network error occurred".to_string()));
return;
},
Ok(metadata) => match metadata {
FetchMetadata::Unfiltered(m) => {
fill_headers_with_metadata(self.response_object.root(), m, CanGc::note());
self.response_object
.root()
.set_type(DOMResponseType::Default, CanGc::note());
},
FetchMetadata::Filtered { filtered, .. } => match filtered {
FilteredMetadata::Basic(m) => {
fill_headers_with_metadata(self.response_object.root(), m, CanGc::note());
self.response_object
.root()
.set_type(DOMResponseType::Basic, CanGc::note());
},
FilteredMetadata::Cors(m) => {
fill_headers_with_metadata(self.response_object.root(), m, CanGc::note());
self.response_object
.root()
.set_type(DOMResponseType::Cors, CanGc::note());
},
FilteredMetadata::Opaque => {
self.response_object
.root()
.set_type(DOMResponseType::Opaque, CanGc::note());
},
FilteredMetadata::OpaqueRedirect(url) => {
let r = self.response_object.root();
r.set_type(DOMResponseType::Opaqueredirect, CanGc::note());
r.set_final_url(url);
},
},
},
}
promise.resolve_native(&self.response_object.root());
self.fetch_promise = Some(TrustedPromise::new(promise));
}
fn process_response_chunk(&mut self, _: RequestId, chunk: Vec<u8>) {
let response = self.response_object.root();
response.stream_chunk(chunk, CanGc::note());
}
fn process_response_eof(
&mut self,
_: RequestId,
_response: Result<ResourceFetchTiming, NetworkError>,
) {
let response = self.response_object.root();
let _ac = enter_realm(&*response);
response.finish();
}
fn resource_timing_mut(&mut self) -> &mut ResourceFetchTiming {
&mut self.resource_timing
}
fn resource_timing(&self) -> &ResourceFetchTiming {
&self.resource_timing
}
fn submit_resource_timing(&mut self) {
if self.resource_timing.timing_type == ResourceTimingType::Resource {
network_listener::submit_timing(self, CanGc::note())
}
}
}
impl ResourceTimingListener for FetchContext {
fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
(
InitiatorType::Fetch,
self.resource_timing_global().get_url().clone(),
)
}
fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
self.response_object.root().global()
}
}
fn fill_headers_with_metadata(r: DomRoot<Response>, m: Metadata, can_gc: CanGc) {
r.set_headers(m.headers, can_gc);
r.set_status(&m.status);
r.set_final_url(m.final_url);
r.set_redirected(m.redirected);
}
pub fn load_whole_resource(
request: RequestBuilder,
core_resource_thread: &CoreResourceThread,
global: &GlobalScope,
can_gc: CanGc,
) -> Result<(Metadata, Vec<u8>), NetworkError> {
let request = request.https_state(global.get_https_state());
let (action_sender, action_receiver) = ipc::channel().unwrap();
let url = request.url.clone();
core_resource_thread
.send(CoreResourceMsg::Fetch(
request,
FetchChannels::ResponseMsg(action_sender, None),
))
.unwrap();
let mut buf = vec![];
let mut metadata = None;
loop {
match action_receiver.recv().unwrap() {
FetchResponseMsg::ProcessRequestBody(..) | FetchResponseMsg::ProcessRequestEOF(..) => {
},
FetchResponseMsg::ProcessResponse(_, Ok(m)) => {
metadata = Some(match m {
FetchMetadata::Unfiltered(m) => m,
FetchMetadata::Filtered { unsafe_, .. } => unsafe_,
})
},
FetchResponseMsg::ProcessResponseChunk(_, data) => buf.extend_from_slice(&data),
FetchResponseMsg::ProcessResponseEOF(_, Ok(_)) => {
let metadata = metadata.unwrap();
if let Some(timing) = &metadata.timing {
submit_timing_data(global, url, InitiatorType::Other, timing, can_gc);
}
return Ok((metadata, buf));
},
FetchResponseMsg::ProcessResponse(_, Err(e)) |
FetchResponseMsg::ProcessResponseEOF(_, Err(e)) => return Err(e),
}
}
}
pub(crate) fn create_a_potential_cors_request(
url: ServoUrl,
destination: Destination,
cors_setting: Option<CorsSettings>,
same_origin_fallback: Option<bool>,
referrer: Referrer,
) -> RequestBuilder {
RequestBuilder::new(url, referrer)
.mode(match cors_setting {
Some(_) => RequestMode::CorsMode,
None if same_origin_fallback == Some(true) => RequestMode::SameOrigin,
None => RequestMode::NoCors,
})
.credentials_mode(match cors_setting {
Some(CorsSettings::Anonymous) => CredentialsMode::CredentialsSameOrigin,
_ => CredentialsMode::Include,
})
.destination(destination)
.use_url_credentials(true)
}