Skip to main content

script/
fetch.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6use std::rc::Rc;
7use std::time::Duration;
8
9use ipc_channel::ipc;
10use js::jsapi::{ExceptionStackBehavior, JS_IsExceptionPending};
11use js::jsval::UndefinedValue;
12use js::realm::CurrentRealm;
13use js::rust::HandleValue;
14use js::rust::wrappers::JS_SetPendingException;
15use net_traits::blob_url_store::UrlWithBlobClaim;
16use net_traits::request::{
17    CorsSettings, CredentialsMode, Destination, Referrer, Request as NetTraitsRequest,
18    RequestBuilder, RequestId, RequestMode, ServiceWorkersMode,
19};
20use net_traits::{
21    CoreResourceMsg, CoreResourceThread, FetchChannels, FetchMetadata, FetchResponseMsg,
22    FilteredMetadata, Metadata, NetworkError, ResourceFetchTiming, cancel_async_fetch,
23};
24use rustc_hash::FxHashMap;
25use script_bindings::cformat;
26use serde::{Deserialize, Serialize};
27use servo_base::id::WebViewId;
28use servo_url::ServoUrl;
29use timers::TimerEventRequest;
30use uuid::Uuid;
31
32use crate::body::BodyMixin;
33use crate::dom::abortsignal::AbortAlgorithm;
34use crate::dom::bindings::codegen::Bindings::AbortSignalBinding::AbortSignalMethods;
35use crate::dom::bindings::codegen::Bindings::RequestBinding::{
36    RequestInfo, RequestInit, RequestMethods,
37};
38use crate::dom::bindings::codegen::Bindings::ResponseBinding::Response_Binding::ResponseMethods;
39use crate::dom::bindings::codegen::Bindings::ResponseBinding::ResponseType as DOMResponseType;
40use crate::dom::bindings::codegen::Bindings::WindowBinding::{DeferredRequestInit, WindowMethods};
41use crate::dom::bindings::error::{Error, Fallible};
42use crate::dom::bindings::inheritance::Castable;
43use crate::dom::bindings::num::Finite;
44use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
45use crate::dom::bindings::reflector::DomGlobal;
46use crate::dom::bindings::root::DomRoot;
47use crate::dom::bindings::trace::RootedTraceableBox;
48use crate::dom::csp::{GlobalCspReporting, Violation};
49use crate::dom::fetchlaterresult::FetchLaterResult;
50use crate::dom::globalscope::GlobalScope;
51use crate::dom::headers::Guard;
52use crate::dom::performance::performanceresourcetiming::InitiatorType;
53use crate::dom::promise::Promise;
54use crate::dom::request::Request;
55use crate::dom::response::Response;
56use crate::dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
57use crate::dom::window::Window;
58use crate::network_listener::{
59    self, FetchResponseListener, NetworkListener, ResourceTimingListener, submit_timing_data,
60};
61use crate::realms::{enter_auto_realm, enter_realm};
62use crate::script_runtime::CanGc;
63
64/// Fetch canceller object. By default initialized to having a
65/// request associated with it, which can be aborted or terminated.
66/// Calling `ignore` will sever the relationship with the request,
67/// meaning it cannot be cancelled through this canceller from that point on.
68#[derive(Default, JSTraceable, MallocSizeOf)]
69pub(crate) struct FetchCanceller {
70    #[no_trace]
71    request_id: Option<RequestId>,
72    #[no_trace]
73    core_resource_thread: Option<CoreResourceThread>,
74    keep_alive: bool,
75}
76
77impl FetchCanceller {
78    /// Create a FetchCanceller associated with a request,
79    /// and a particular(public vs private) resource thread.
80    pub(crate) fn new(
81        request_id: RequestId,
82        keep_alive: bool,
83        core_resource_thread: CoreResourceThread,
84    ) -> Self {
85        Self {
86            request_id: Some(request_id),
87            core_resource_thread: Some(core_resource_thread),
88            keep_alive,
89        }
90    }
91
92    pub(crate) fn keep_alive(&self) -> bool {
93        self.keep_alive
94    }
95
96    fn cancel(&mut self) {
97        if let Some(request_id) = self.request_id.take() {
98            // stop trying to make fetch happen
99            // it's not going to happen
100
101            if let Some(ref core_resource_thread) = self.core_resource_thread {
102                // No error handling here. Cancellation is a courtesy call,
103                // we don't actually care if the other side heard.
104                cancel_async_fetch(vec![request_id], core_resource_thread);
105            }
106        }
107    }
108
109    /// Use this if you don't want it to send a cancellation request
110    /// on drop (e.g. if the fetch completes)
111    pub(crate) fn ignore(&mut self) {
112        let _ = self.request_id.take();
113    }
114
115    /// <https://fetch.spec.whatwg.org/#fetch-controller-abort>
116    pub(crate) fn abort(&mut self) {
117        self.cancel();
118    }
119
120    /// <https://fetch.spec.whatwg.org/#fetch-controller-terminate>
121    pub(crate) fn terminate(&mut self) {
122        self.cancel();
123    }
124}
125
126#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
127/// An id to differentiate one deferred fetch record from another.
128pub(crate) struct DeferredFetchRecordId(Uuid);
129
130impl Default for DeferredFetchRecordId {
131    fn default() -> Self {
132        Self(Uuid::new_v4())
133    }
134}
135
136pub(crate) type QueuedDeferredFetchRecord = Rc<DeferredFetchRecord>;
137
138/// <https://fetch.spec.whatwg.org/#concept-fetch-group>
139#[derive(Default, MallocSizeOf)]
140pub(crate) struct FetchGroup {
141    /// <https://fetch.spec.whatwg.org/#fetch-group-deferred-fetch-records>
142    #[conditional_malloc_size_of]
143    pub(crate) deferred_fetch_records: FxHashMap<DeferredFetchRecordId, QueuedDeferredFetchRecord>,
144}
145
146fn request_init_from_request(request: NetTraitsRequest, global: &GlobalScope) -> RequestBuilder {
147    let mut builder = RequestBuilder::new(
148        request.target_webview_id,
149        request.url_with_blob_claim(),
150        request.referrer,
151    )
152    .method(request.method)
153    .headers(request.headers)
154    .unsafe_request(request.unsafe_request)
155    .body(request.body)
156    .destination(request.destination)
157    .synchronous(request.synchronous)
158    .mode(request.mode)
159    .cache_mode(request.cache_mode)
160    .use_cors_preflight(request.use_cors_preflight)
161    .credentials_mode(request.credentials_mode)
162    .use_url_credentials(request.use_url_credentials)
163    .referrer_policy(request.referrer_policy)
164    .pipeline_id(request.pipeline_id)
165    .redirect_mode(request.redirect_mode)
166    .integrity_metadata(request.integrity_metadata)
167    .cryptographic_nonce_metadata(request.cryptographic_nonce_metadata)
168    .parser_metadata(request.parser_metadata)
169    .initiator(request.initiator)
170    .client(global.request_client())
171    .insecure_requests_policy(request.insecure_requests_policy)
172    .has_trustworthy_ancestor_origin(request.has_trustworthy_ancestor_origin)
173    .response_tainting(request.response_tainting);
174    builder.id = request.id;
175    builder
176}
177
178/// <https://fetch.spec.whatwg.org/#abort-fetch>
179fn abort_fetch_call(
180    promise: Rc<Promise>,
181    request: &Request,
182    response_object: Option<&Response>,
183    abort_reason: HandleValue,
184    global: &GlobalScope,
185    cx: &mut js::context::JSContext,
186) {
187    // Step 1. Reject promise with error.
188    promise.reject_with_cx(cx, abort_reason);
189    // Step 2. If request’s body is non-null and is readable, then cancel request’s body with error.
190    if let Some(body) = request.body() &&
191        body.is_readable()
192    {
193        body.cancel(cx, global, abort_reason);
194    }
195    // Step 3. If responseObject is null, then return.
196    // Step 4. Let response be responseObject’s response.
197    let Some(response) = response_object else {
198        return;
199    };
200    // Step 5. If response’s body is non-null and is readable, then error response’s body with error.
201    if let Some(body) = response.body() &&
202        body.is_readable()
203    {
204        body.error(cx, abort_reason);
205    }
206}
207
208/// <https://fetch.spec.whatwg.org/#dom-global-fetch>
209#[expect(non_snake_case)]
210pub(crate) fn Fetch(
211    global: &GlobalScope,
212    input: RequestInfo,
213    init: RootedTraceableBox<RequestInit>,
214    cx: &mut CurrentRealm,
215) -> Rc<Promise> {
216    // Step 1. Let p be a new promise.
217    let promise = Promise::new_in_realm(cx);
218
219    // Step 7. Let responseObject be null.
220    // NOTE: We do initialize the object earlier earlier so we can use it to track errors
221    let response = Response::new(cx, global);
222    response
223        .Headers(CanGc::from_cx(cx))
224        .set_guard(Guard::Immutable);
225
226    // Step 2. Let requestObject be the result of invoking the initial value of Request as constructor
227    //         with input and init as arguments. If this throws an exception, reject p with it and return p.
228    let request_object = match Request::Constructor(cx, global, None, input, init) {
229        Err(e) => {
230            response.error_stream(cx, e.clone());
231            promise.reject_error_with_cx(cx, e);
232            return promise;
233        },
234        Ok(r) => r,
235    };
236    // Step 3. Let request be requestObject’s request.
237    let request = request_object.get_request();
238    let request_id = request.id;
239
240    // Step 4. If requestObject’s signal is aborted, then:
241    let signal = request_object.Signal();
242    if signal.aborted() {
243        // Step 4.1. Abort the fetch() call with p, request, null, and requestObject’s signal’s abort reason.
244        rooted!(&in(cx) let mut abort_reason = UndefinedValue());
245        signal.Reason(cx.into(), abort_reason.handle_mut());
246        abort_fetch_call(
247            promise.clone(),
248            &request_object,
249            None,
250            abort_reason.handle(),
251            global,
252            cx,
253        );
254        // Step 4.2. Return p.
255        return promise;
256    }
257
258    let keep_alive = request.keep_alive;
259    // Step 5. Let globalObject be request’s client’s global object.
260    // NOTE:   We already get the global object as an argument
261    let mut request_init = request_init_from_request(request, global);
262
263    // Step 6. If globalObject is a ServiceWorkerGlobalScope object, then set request’s
264    //         service-workers mode to "none".
265    if global.is::<ServiceWorkerGlobalScope>() {
266        request_init.service_workers_mode = ServiceWorkersMode::None;
267    }
268
269    // Step 8. Let relevantRealm be this’s relevant realm.
270    //
271    // Is `comp` as argument
272
273    // Step 9. Let locallyAborted be false.
274    // Step 10. Let controller be null.
275    let fetch_context = FetchContext {
276        fetch_promise: Some(TrustedPromise::new(promise.clone())),
277        response_object: Trusted::new(&*response),
278        request: Trusted::new(&*request_object),
279        global: Trusted::new(global),
280        locally_aborted: false,
281        canceller: FetchCanceller::new(request_id, keep_alive, global.core_resource_thread()),
282        url: request_init.url.url(),
283    };
284    let network_listener = NetworkListener::new(
285        fetch_context,
286        global.task_manager().networking_task_source().to_sendable(),
287    );
288    let fetch_context = network_listener.context.clone();
289
290    // Step 11. Add the following abort steps to requestObject’s signal:
291    signal.add(&AbortAlgorithm::Fetch(fetch_context));
292
293    // Step 12. Set controller to the result of calling fetch given request and
294    // processResponse given response being these steps:
295    global.fetch_with_network_listener(request_init, network_listener);
296
297    // Step 13. Return p.
298    promise
299}
300
301/// <https://fetch.spec.whatwg.org/#queue-a-deferred-fetch>
302fn queue_deferred_fetch(
303    request: NetTraitsRequest,
304    activate_after: Finite<f64>,
305    global: &GlobalScope,
306) -> DeferredFetchRecordId {
307    let trusted_global = Trusted::new(global);
308    let mut request = request;
309    // Step 1. Populate request from client given request.
310    request.client = Some(global.request_client());
311    request.populate_request_from_client();
312    // Step 2. Set request’s service-workers mode to "none".
313    request.service_workers_mode = ServiceWorkersMode::None;
314    // Step 3. Set request’s keepalive to true.
315    request.keep_alive = true;
316    // Step 4. Let deferredRecord be a new deferred fetch record whose request is request, and whose notify invoked is onActivatedWithoutTermination.
317    let deferred_record = Rc::new(DeferredFetchRecord {
318        request,
319        invoke_state: Cell::new(DeferredFetchRecordInvokeState::Pending),
320        activated: Cell::new(false),
321    });
322    // Step 5. Append deferredRecord to request’s client’s fetch group’s deferred fetch records.
323    let deferred_fetch_record_id = global.append_deferred_fetch(deferred_record);
324    // Step 6. If activateAfter is non-null, then run the following steps in parallel:
325    global.schedule_timer(TimerEventRequest {
326        callback: Box::new(move || {
327            // Step 6.2. Process deferredRecord.
328            let global = trusted_global.root();
329            global.deferred_fetch_record_for_id(&deferred_fetch_record_id).process(&global);
330
331            // Last step of https://fetch.spec.whatwg.org/#process-a-deferred-fetch
332            //
333            // Step 4. Queue a global task on the deferred fetch task source with
334            // deferredRecord’s request’s client’s global object to run deferredRecord’s notify invoked.
335            let trusted_global = trusted_global.clone();
336            global.task_manager().deferred_fetch_task_source().queue(
337                task!(notify_deferred_record: move || {
338                    trusted_global.root().deferred_fetch_record_for_id(&deferred_fetch_record_id).activate();
339                }),
340            );
341        }),
342        // Step 6.1. The user agent should wait until any of the following conditions is met:
343        duration: Duration::from_millis(*activate_after as u64),
344    });
345    // Step 7. Return deferredRecord.
346    deferred_fetch_record_id
347}
348
349/// <https://fetch.spec.whatwg.org/#dom-window-fetchlater>
350#[expect(non_snake_case, unsafe_code)]
351pub(crate) fn FetchLater(
352    cx: &mut js::context::JSContext,
353    window: &Window,
354    input: RequestInfo,
355    init: RootedTraceableBox<DeferredRequestInit>,
356) -> Fallible<DomRoot<FetchLaterResult>> {
357    let global_scope = window.upcast();
358    let document = window.Document();
359    // Step 1. Let requestObject be the result of invoking the initial value
360    // of Request as constructor with input and init as arguments.
361    let request_object = Request::constructor(cx, global_scope, None, input, &init.parent)?;
362    // Step 2. If requestObject’s signal is aborted, then throw signal’s abort reason.
363    let signal = request_object.Signal();
364    if signal.aborted() {
365        rooted!(&in(cx) let mut abort_reason = UndefinedValue());
366        signal.Reason(cx.into(), abort_reason.handle_mut());
367        unsafe {
368            assert!(!JS_IsExceptionPending(cx.raw_cx()));
369            JS_SetPendingException(
370                cx.raw_cx(),
371                abort_reason.handle(),
372                ExceptionStackBehavior::Capture,
373            );
374        }
375        return Err(Error::JSFailed);
376    }
377    // Step 3. Let request be requestObject’s request.
378    let request = request_object.get_request();
379    // Step 4. Let activateAfter be null.
380    let mut activate_after = Finite::wrap(0_f64);
381    // Step 5. If init is given and init["activateAfter"] exists, then set
382    // activateAfter to init["activateAfter"].
383    if let Some(init_activate_after) = init.activateAfter.as_ref() {
384        activate_after = *init_activate_after;
385    }
386    // Step 6. If activateAfter is less than 0, then throw a RangeError.
387    if *activate_after < 0.0 {
388        return Err(Error::Range(c"activateAfter must be at least 0".to_owned()));
389    }
390    // Step 7. If this’s relevant global object’s associated document is not fully active, then throw a TypeError.
391    if !document.is_fully_active() {
392        return Err(Error::Type(c"Document is not fully active".to_owned()));
393    }
394    let url = request.url();
395    // Step 8. If request’s URL’s scheme is not an HTTP(S) scheme, then throw a TypeError.
396    if !matches!(url.scheme(), "http" | "https") {
397        return Err(Error::Type(c"URL is not http(s)".to_owned()));
398    }
399    // Step 9. If request’s URL is not a potentially trustworthy URL, then throw a SecurityError.
400    if !url.is_potentially_trustworthy() {
401        return Err(Error::Type(c"URL is not trustworthy".to_owned()));
402    }
403    // Step 10. If request’s body is not null, and request’s body length is null, then throw a TypeError.
404    if request
405        .body
406        .as_ref()
407        .is_some_and(|body| body.len().is_none())
408    {
409        return Err(Error::Type(c"Body is empty".to_owned()));
410    }
411    // Step 11. If the available deferred-fetch quota given request’s client and request’s URL’s
412    // origin is less than request’s total request length, then throw a "QuotaExceededError" DOMException.
413    let quota = document.available_deferred_fetch_quota(request.url().origin());
414    let requested = request.total_request_length() as isize;
415    if quota < requested {
416        return Err(Error::QuotaExceeded {
417            quota: Some(Finite::wrap(quota as f64)),
418            requested: Some(Finite::wrap(requested as f64)),
419        });
420    }
421    // Step 12. Let activated be false.
422    // Step 13. Let deferredRecord be the result of calling queue a deferred fetch given request,
423    // activateAfter, and the following step: set activated to true.
424    let deferred_record_id = queue_deferred_fetch(request, activate_after, global_scope);
425    // Step 14. Add the following abort steps to requestObject’s signal: Set deferredRecord’s invoke state to "aborted".
426    signal.add(&AbortAlgorithm::FetchLater(deferred_record_id));
427    // Step 15. Return a new FetchLaterResult whose activated getter steps are to return activated.
428    Ok(FetchLaterResult::new(
429        window,
430        deferred_record_id,
431        CanGc::from_cx(cx),
432    ))
433}
434
435/// <https://fetch.spec.whatwg.org/#deferred-fetch-record-invoke-state>
436#[derive(Clone, Copy, MallocSizeOf, PartialEq)]
437pub(crate) enum DeferredFetchRecordInvokeState {
438    Pending,
439    Sent,
440    Aborted,
441}
442
443/// <https://fetch.spec.whatwg.org/#deferred-fetch-record>
444#[derive(MallocSizeOf)]
445pub(crate) struct DeferredFetchRecord {
446    /// <https://fetch.spec.whatwg.org/#deferred-fetch-record-request>
447    pub(crate) request: NetTraitsRequest,
448    /// <https://fetch.spec.whatwg.org/#deferred-fetch-record-invoke-state>
449    pub(crate) invoke_state: Cell<DeferredFetchRecordInvokeState>,
450    activated: Cell<bool>,
451}
452
453impl DeferredFetchRecord {
454    /// Part of step 13 of <https://fetch.spec.whatwg.org/#dom-window-fetchlater>
455    fn activate(&self) {
456        // and the following step: set activated to true.
457        self.activated.set(true);
458    }
459    /// Part of step 14 of <https://fetch.spec.whatwg.org/#dom-window-fetchlater>
460    pub(crate) fn abort(&self) {
461        // Set deferredRecord’s invoke state to "aborted".
462        self.invoke_state
463            .set(DeferredFetchRecordInvokeState::Aborted);
464    }
465    /// Part of step 15 of <https://fetch.spec.whatwg.org/#dom-window-fetchlater>
466    pub(crate) fn activated_getter_steps(&self) -> bool {
467        // whose activated getter steps are to return activated.
468        self.activated.get()
469    }
470    /// <https://fetch.spec.whatwg.org/#process-a-deferred-fetch>
471    pub(crate) fn process(&self, global: &GlobalScope) {
472        // Step 1. If deferredRecord’s invoke state is not "pending", then return.
473        if self.invoke_state.get() != DeferredFetchRecordInvokeState::Pending {
474            return;
475        }
476        // Step 2. Set deferredRecord’s invoke state to "sent".
477        self.invoke_state.set(DeferredFetchRecordInvokeState::Sent);
478        // Step 3. Fetch deferredRecord’s request.
479        let fetch_later_listener = FetchLaterListener {
480            url: self.request.url(),
481            global: Trusted::new(global),
482        };
483        let request_init = request_init_from_request(self.request.clone(), global);
484        global.fetch(
485            request_init,
486            fetch_later_listener,
487            global.task_manager().networking_task_source().to_sendable(),
488        );
489        // Step 4 is handled by caller
490    }
491}
492
493#[derive(JSTraceable, MallocSizeOf)]
494pub(crate) struct FetchContext {
495    #[ignore_malloc_size_of = "unclear ownership semantics"]
496    fetch_promise: Option<TrustedPromise>,
497    response_object: Trusted<Response>,
498    request: Trusted<Request>,
499    global: Trusted<GlobalScope>,
500    locally_aborted: bool,
501    canceller: FetchCanceller,
502    #[no_trace]
503    url: ServoUrl,
504}
505
506impl FetchContext {
507    /// Step 11 of <https://fetch.spec.whatwg.org/#dom-global-fetch>
508    pub(crate) fn abort_fetch(
509        &mut self,
510        abort_reason: HandleValue,
511        cx: &mut js::context::JSContext,
512    ) {
513        // Step 11.1. Set locallyAborted to true.
514        self.locally_aborted = true;
515        // Step 11.2. Assert: controller is non-null.
516        //
517        // N/a, that's self
518
519        // Step 11.3. Abort controller with requestObject’s signal’s abort reason.
520        self.canceller.abort();
521
522        // Step 11.4. Abort the fetch() call with p, request, responseObject,
523        // and requestObject’s signal’s abort reason.
524        let promise = self
525            .fetch_promise
526            .take()
527            .expect("fetch promise is missing")
528            .root();
529        abort_fetch_call(
530            promise,
531            &self.request.root(),
532            Some(&self.response_object.root()),
533            abort_reason,
534            &self.global.root(),
535            cx,
536        );
537    }
538}
539
540/// Step 12 of <https://fetch.spec.whatwg.org/#dom-global-fetch>
541impl FetchResponseListener for FetchContext {
542    fn process_request_body(&mut self, _: RequestId) {
543        // TODO
544    }
545
546    fn process_response(
547        &mut self,
548        cx: &mut js::context::JSContext,
549        _: RequestId,
550        fetch_metadata: Result<FetchMetadata, NetworkError>,
551    ) {
552        // Step 12.1. If locallyAborted is true, then abort these steps.
553        if self.locally_aborted {
554            return;
555        }
556        let promise = self
557            .fetch_promise
558            .take()
559            .expect("fetch promise is missing")
560            .root();
561
562        let mut realm = enter_auto_realm(cx, &*promise);
563        let cx = &mut realm.current_realm();
564        match fetch_metadata {
565            // Step 12.3. If response is a network error, then reject
566            // p with a TypeError and abort these steps.
567            Err(error) => {
568                promise
569                    .reject_error_with_cx(cx, Error::Type(cformat!("Network error: {:?}", error)));
570                self.fetch_promise = Some(TrustedPromise::new(promise));
571                let response = self.response_object.root();
572                response.set_type(DOMResponseType::Error, CanGc::from_cx(cx));
573                response.error_stream(cx, Error::Type(c"Network error occurred".to_owned()));
574                return;
575            },
576            // Step 12.4. Set responseObject to the result of creating a Response object,
577            // given response, "immutable", and relevantRealm.
578            Ok(metadata) => match metadata {
579                FetchMetadata::Unfiltered(m) => {
580                    fill_headers_with_metadata(self.response_object.root(), m, CanGc::from_cx(cx));
581                    self.response_object
582                        .root()
583                        .set_type(DOMResponseType::Default, CanGc::from_cx(cx));
584                },
585                FetchMetadata::Filtered { filtered, .. } => match filtered {
586                    FilteredMetadata::Basic(m) => {
587                        fill_headers_with_metadata(
588                            self.response_object.root(),
589                            m,
590                            CanGc::from_cx(cx),
591                        );
592                        self.response_object
593                            .root()
594                            .set_type(DOMResponseType::Basic, CanGc::from_cx(cx));
595                    },
596                    FilteredMetadata::Cors(m) => {
597                        fill_headers_with_metadata(
598                            self.response_object.root(),
599                            m,
600                            CanGc::from_cx(cx),
601                        );
602                        self.response_object
603                            .root()
604                            .set_type(DOMResponseType::Cors, CanGc::from_cx(cx));
605                    },
606                    FilteredMetadata::Opaque => {
607                        self.response_object
608                            .root()
609                            .set_type(DOMResponseType::Opaque, CanGc::from_cx(cx));
610                    },
611                    FilteredMetadata::OpaqueRedirect(url) => {
612                        let r = self.response_object.root();
613                        r.set_type(DOMResponseType::Opaqueredirect, CanGc::from_cx(cx));
614                        r.set_final_url(url);
615                    },
616                },
617            },
618        }
619
620        // Step 12.5. Resolve p with responseObject.
621        promise.resolve_native_with_cx(cx, &self.response_object.root());
622        self.fetch_promise = Some(TrustedPromise::new(promise));
623    }
624
625    fn process_response_chunk(
626        &mut self,
627        cx: &mut js::context::JSContext,
628        _: RequestId,
629        chunk: Vec<u8>,
630    ) {
631        let response = self.response_object.root();
632        response.stream_chunk(cx, chunk);
633    }
634
635    fn process_response_eof(
636        self,
637        cx: &mut js::context::JSContext,
638        _: RequestId,
639        response: Result<(), NetworkError>,
640        timing: ResourceFetchTiming,
641    ) {
642        let response_object = self.response_object.root();
643        let _ac = enter_realm(&*response_object);
644        if let Err(ref error) = response &&
645            *error == NetworkError::DecompressionError
646        {
647            response_object.error_stream(cx, Error::Type(c"Network error occurred".to_owned()));
648        }
649        response_object.finish(cx);
650        // TODO
651        // ... trailerObject is not supported in Servo yet.
652
653        // navigation submission is handled in servoparser/mod.rs
654        network_listener::submit_timing(cx, &self, &response, &timing);
655    }
656
657    fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
658        let global = &self.resource_timing_global();
659        global.report_csp_violations(violations, None, None);
660    }
661}
662
663impl ResourceTimingListener for FetchContext {
664    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
665        (InitiatorType::Fetch, self.url.clone())
666    }
667
668    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
669        self.response_object.root().global()
670    }
671}
672
673struct FetchLaterListener {
674    /// URL of this request.
675    url: ServoUrl,
676    /// The global object fetching the report uri violation
677    global: Trusted<GlobalScope>,
678}
679
680impl FetchResponseListener for FetchLaterListener {
681    fn process_request_body(&mut self, _: RequestId) {}
682
683    fn process_response(
684        &mut self,
685        _: &mut js::context::JSContext,
686        _: RequestId,
687        fetch_metadata: Result<FetchMetadata, NetworkError>,
688    ) {
689        _ = fetch_metadata;
690    }
691
692    fn process_response_chunk(
693        &mut self,
694        _: &mut js::context::JSContext,
695        _: RequestId,
696        chunk: Vec<u8>,
697    ) {
698        _ = chunk;
699    }
700
701    fn process_response_eof(
702        self,
703        cx: &mut js::context::JSContext,
704        _: RequestId,
705        response: Result<(), NetworkError>,
706        timing: ResourceFetchTiming,
707    ) {
708        network_listener::submit_timing(cx, &self, &response, &timing);
709    }
710
711    fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
712        let global = self.resource_timing_global();
713        global.report_csp_violations(violations, None, None);
714    }
715}
716
717impl ResourceTimingListener for FetchLaterListener {
718    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
719        (InitiatorType::Fetch, self.url.clone())
720    }
721
722    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
723        self.global.root()
724    }
725}
726
727fn fill_headers_with_metadata(r: DomRoot<Response>, m: Metadata, can_gc: CanGc) {
728    r.set_headers(m.headers, can_gc);
729    r.set_status(&m.status);
730    r.set_final_url(m.final_url);
731    r.set_redirected(m.redirected);
732}
733
734pub(crate) trait CspViolationsProcessor {
735    fn process_csp_violations(&self, violations: Vec<Violation>);
736}
737
738/// Convenience function for synchronously loading a whole resource.
739pub(crate) fn load_whole_resource(
740    request: RequestBuilder,
741    core_resource_thread: &CoreResourceThread,
742    global: &GlobalScope,
743    csp_violations_processor: &dyn CspViolationsProcessor,
744    cx: &mut js::context::JSContext,
745) -> Result<(Metadata, Vec<u8>, bool), NetworkError> {
746    let (action_sender, action_receiver) = ipc::channel().unwrap();
747    let url = request.url.url();
748    core_resource_thread
749        .send(CoreResourceMsg::Fetch(
750            request,
751            FetchChannels::ResponseMsg(action_sender),
752        ))
753        .unwrap();
754
755    let mut buf = vec![];
756    let mut metadata = None;
757    let mut muted_errors = false;
758    loop {
759        match action_receiver.recv().unwrap() {
760            FetchResponseMsg::ProcessRequestBody(..) => {},
761            FetchResponseMsg::ProcessResponse(_, Ok(m)) => {
762                muted_errors = m.is_cors_cross_origin();
763                metadata = Some(match m {
764                    FetchMetadata::Unfiltered(m) => m,
765                    FetchMetadata::Filtered { unsafe_, .. } => unsafe_,
766                })
767            },
768            FetchResponseMsg::ProcessResponseChunk(_, data) => buf.extend_from_slice(&data),
769            FetchResponseMsg::ProcessResponseEOF(_, Ok(_), _) => {
770                let metadata = metadata.unwrap();
771                if let Some(timing) = &metadata.timing {
772                    submit_timing_data(cx, global, url, InitiatorType::Other, timing);
773                }
774                return Ok((metadata, buf, muted_errors));
775            },
776            FetchResponseMsg::ProcessResponse(_, Err(e)) |
777            FetchResponseMsg::ProcessResponseEOF(_, Err(e), _) => return Err(e),
778            FetchResponseMsg::ProcessCspViolations(_, violations) => {
779                csp_violations_processor.process_csp_violations(violations);
780            },
781        }
782    }
783}
784
785pub(crate) trait RequestWithGlobalScope {
786    fn with_global_scope(self, global: &GlobalScope) -> Self;
787}
788
789impl RequestWithGlobalScope for RequestBuilder {
790    fn with_global_scope(self, global: &GlobalScope) -> Self {
791        self.insecure_requests_policy(global.insecure_requests_policy())
792            .has_trustworthy_ancestor_origin(global.has_trustworthy_ancestor_or_current_origin())
793            .policy_container(global.policy_container())
794            .client(global.request_client())
795            .pipeline_id(Some(global.pipeline_id()))
796            .origin(global.origin().immutable().clone())
797    }
798}
799
800/// <https://html.spec.whatwg.org/multipage/#create-a-potential-cors-request>
801#[allow(clippy::too_many_arguments)]
802pub(crate) fn create_a_potential_cors_request(
803    webview_id: Option<WebViewId>,
804    url: ServoUrl,
805    destination: Destination,
806    cors_setting: Option<CorsSettings>,
807    same_origin_fallback: Option<bool>,
808    referrer: Referrer,
809) -> RequestBuilder {
810    RequestBuilder::new(
811        webview_id,
812        UrlWithBlobClaim::from_url_without_having_claimed_blob(url),
813        referrer,
814    )
815    // Step 1. Let mode be "no-cors" if corsAttributeState is No CORS, and "cors" otherwise.
816    .mode(match cors_setting {
817        Some(_) => RequestMode::CorsMode,
818        // Step 2. If same-origin fallback flag is set and mode is "no-cors", set mode to "same-origin".
819        None if same_origin_fallback == Some(true) => RequestMode::SameOrigin,
820        None => RequestMode::NoCors,
821    })
822    .credentials_mode(match cors_setting {
823        // Step 4. If corsAttributeState is Anonymous, set credentialsMode to "same-origin".
824        Some(CorsSettings::Anonymous) => CredentialsMode::CredentialsSameOrigin,
825        // Step 3. Let credentialsMode be "include".
826        _ => CredentialsMode::Include,
827    })
828    // Step 5. Return a new request whose URL is url, destination is destination,
829    // mode is mode, credentials mode is credentialsMode, and whose use-URL-credentials flag is set.
830    .destination(destination)
831    .use_url_credentials(true)
832}