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(cx.into(), abort_reason, CanGc::from_cx(cx));
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(e, CanGc::from_cx(cx));
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.reject_error(
569                    Error::Type(cformat!("Network error: {:?}", error)),
570                    CanGc::from_cx(cx),
571                );
572                self.fetch_promise = Some(TrustedPromise::new(promise));
573                let response = self.response_object.root();
574                response.set_type(DOMResponseType::Error, CanGc::from_cx(cx));
575                response.error_stream(cx, Error::Type(c"Network error occurred".to_owned()));
576                return;
577            },
578            // Step 12.4. Set responseObject to the result of creating a Response object,
579            // given response, "immutable", and relevantRealm.
580            Ok(metadata) => match metadata {
581                FetchMetadata::Unfiltered(m) => {
582                    fill_headers_with_metadata(self.response_object.root(), m, CanGc::from_cx(cx));
583                    self.response_object
584                        .root()
585                        .set_type(DOMResponseType::Default, CanGc::from_cx(cx));
586                },
587                FetchMetadata::Filtered { filtered, .. } => match filtered {
588                    FilteredMetadata::Basic(m) => {
589                        fill_headers_with_metadata(
590                            self.response_object.root(),
591                            m,
592                            CanGc::from_cx(cx),
593                        );
594                        self.response_object
595                            .root()
596                            .set_type(DOMResponseType::Basic, CanGc::from_cx(cx));
597                    },
598                    FilteredMetadata::Cors(m) => {
599                        fill_headers_with_metadata(
600                            self.response_object.root(),
601                            m,
602                            CanGc::from_cx(cx),
603                        );
604                        self.response_object
605                            .root()
606                            .set_type(DOMResponseType::Cors, CanGc::from_cx(cx));
607                    },
608                    FilteredMetadata::Opaque => {
609                        self.response_object
610                            .root()
611                            .set_type(DOMResponseType::Opaque, CanGc::from_cx(cx));
612                    },
613                    FilteredMetadata::OpaqueRedirect(url) => {
614                        let r = self.response_object.root();
615                        r.set_type(DOMResponseType::Opaqueredirect, CanGc::from_cx(cx));
616                        r.set_final_url(url);
617                    },
618                },
619            },
620        }
621
622        // Step 12.5. Resolve p with responseObject.
623        promise.resolve_native(&self.response_object.root(), CanGc::from_cx(cx));
624        self.fetch_promise = Some(TrustedPromise::new(promise));
625    }
626
627    fn process_response_chunk(
628        &mut self,
629        cx: &mut js::context::JSContext,
630        _: RequestId,
631        chunk: Vec<u8>,
632    ) {
633        let response = self.response_object.root();
634        response.stream_chunk(cx, chunk);
635    }
636
637    fn process_response_eof(
638        self,
639        cx: &mut js::context::JSContext,
640        _: RequestId,
641        response: Result<(), NetworkError>,
642        timing: ResourceFetchTiming,
643    ) {
644        let response_object = self.response_object.root();
645        let _ac = enter_realm(&*response_object);
646        if let Err(ref error) = response &&
647            *error == NetworkError::DecompressionError
648        {
649            response_object.error_stream(cx, Error::Type(c"Network error occurred".to_owned()));
650        }
651        response_object.finish(cx);
652        // TODO
653        // ... trailerObject is not supported in Servo yet.
654
655        // navigation submission is handled in servoparser/mod.rs
656        network_listener::submit_timing(cx, &self, &response, &timing);
657    }
658
659    fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
660        let global = &self.resource_timing_global();
661        global.report_csp_violations(violations, None, None);
662    }
663}
664
665impl ResourceTimingListener for FetchContext {
666    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
667        (InitiatorType::Fetch, self.url.clone())
668    }
669
670    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
671        self.response_object.root().global()
672    }
673}
674
675struct FetchLaterListener {
676    /// URL of this request.
677    url: ServoUrl,
678    /// The global object fetching the report uri violation
679    global: Trusted<GlobalScope>,
680}
681
682impl FetchResponseListener for FetchLaterListener {
683    fn process_request_body(&mut self, _: RequestId) {}
684
685    fn process_response(
686        &mut self,
687        _: &mut js::context::JSContext,
688        _: RequestId,
689        fetch_metadata: Result<FetchMetadata, NetworkError>,
690    ) {
691        _ = fetch_metadata;
692    }
693
694    fn process_response_chunk(
695        &mut self,
696        _: &mut js::context::JSContext,
697        _: RequestId,
698        chunk: Vec<u8>,
699    ) {
700        _ = chunk;
701    }
702
703    fn process_response_eof(
704        self,
705        cx: &mut js::context::JSContext,
706        _: RequestId,
707        response: Result<(), NetworkError>,
708        timing: ResourceFetchTiming,
709    ) {
710        network_listener::submit_timing(cx, &self, &response, &timing);
711    }
712
713    fn process_csp_violations(&mut self, _request_id: RequestId, violations: Vec<Violation>) {
714        let global = self.resource_timing_global();
715        global.report_csp_violations(violations, None, None);
716    }
717}
718
719impl ResourceTimingListener for FetchLaterListener {
720    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
721        (InitiatorType::Fetch, self.url.clone())
722    }
723
724    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
725        self.global.root()
726    }
727}
728
729fn fill_headers_with_metadata(r: DomRoot<Response>, m: Metadata, can_gc: CanGc) {
730    r.set_headers(m.headers, can_gc);
731    r.set_status(&m.status);
732    r.set_final_url(m.final_url);
733    r.set_redirected(m.redirected);
734}
735
736pub(crate) trait CspViolationsProcessor {
737    fn process_csp_violations(&self, violations: Vec<Violation>);
738}
739
740/// Convenience function for synchronously loading a whole resource.
741pub(crate) fn load_whole_resource(
742    request: RequestBuilder,
743    core_resource_thread: &CoreResourceThread,
744    global: &GlobalScope,
745    csp_violations_processor: &dyn CspViolationsProcessor,
746    cx: &mut js::context::JSContext,
747) -> Result<(Metadata, Vec<u8>, bool), NetworkError> {
748    let (action_sender, action_receiver) = ipc::channel().unwrap();
749    let url = request.url.url();
750    core_resource_thread
751        .send(CoreResourceMsg::Fetch(
752            request,
753            FetchChannels::ResponseMsg(action_sender),
754        ))
755        .unwrap();
756
757    let mut buf = vec![];
758    let mut metadata = None;
759    let mut muted_errors = false;
760    loop {
761        match action_receiver.recv().unwrap() {
762            FetchResponseMsg::ProcessRequestBody(..) => {},
763            FetchResponseMsg::ProcessResponse(_, Ok(m)) => {
764                muted_errors = m.is_cors_cross_origin();
765                metadata = Some(match m {
766                    FetchMetadata::Unfiltered(m) => m,
767                    FetchMetadata::Filtered { unsafe_, .. } => unsafe_,
768                })
769            },
770            FetchResponseMsg::ProcessResponseChunk(_, data) => buf.extend_from_slice(&data),
771            FetchResponseMsg::ProcessResponseEOF(_, Ok(_), _) => {
772                let metadata = metadata.unwrap();
773                if let Some(timing) = &metadata.timing {
774                    submit_timing_data(cx, global, url, InitiatorType::Other, timing);
775                }
776                return Ok((metadata, buf, muted_errors));
777            },
778            FetchResponseMsg::ProcessResponse(_, Err(e)) |
779            FetchResponseMsg::ProcessResponseEOF(_, Err(e), _) => return Err(e),
780            FetchResponseMsg::ProcessCspViolations(_, violations) => {
781                csp_violations_processor.process_csp_violations(violations);
782            },
783        }
784    }
785}
786
787pub(crate) trait RequestWithGlobalScope {
788    fn with_global_scope(self, global: &GlobalScope) -> Self;
789}
790
791impl RequestWithGlobalScope for RequestBuilder {
792    fn with_global_scope(self, global: &GlobalScope) -> Self {
793        self.insecure_requests_policy(global.insecure_requests_policy())
794            .has_trustworthy_ancestor_origin(global.has_trustworthy_ancestor_or_current_origin())
795            .policy_container(global.policy_container())
796            .client(global.request_client())
797            .pipeline_id(Some(global.pipeline_id()))
798            .origin(global.origin().immutable().clone())
799    }
800}
801
802/// <https://html.spec.whatwg.org/multipage/#create-a-potential-cors-request>
803#[allow(clippy::too_many_arguments)]
804pub(crate) fn create_a_potential_cors_request(
805    webview_id: Option<WebViewId>,
806    url: ServoUrl,
807    destination: Destination,
808    cors_setting: Option<CorsSettings>,
809    same_origin_fallback: Option<bool>,
810    referrer: Referrer,
811) -> RequestBuilder {
812    RequestBuilder::new(
813        webview_id,
814        UrlWithBlobClaim::from_url_without_having_claimed_blob(url),
815        referrer,
816    )
817    // Step 1. Let mode be "no-cors" if corsAttributeState is No CORS, and "cors" otherwise.
818    .mode(match cors_setting {
819        Some(_) => RequestMode::CorsMode,
820        // Step 2. If same-origin fallback flag is set and mode is "no-cors", set mode to "same-origin".
821        None if same_origin_fallback == Some(true) => RequestMode::SameOrigin,
822        None => RequestMode::NoCors,
823    })
824    .credentials_mode(match cors_setting {
825        // Step 4. If corsAttributeState is Anonymous, set credentialsMode to "same-origin".
826        Some(CorsSettings::Anonymous) => CredentialsMode::CredentialsSameOrigin,
827        // Step 3. Let credentialsMode be "include".
828        _ => CredentialsMode::Include,
829    })
830    // Step 5. Return a new request whose URL is url, destination is destination,
831    // mode is mode, credentials mode is credentialsMode, and whose use-URL-credentials flag is set.
832    .destination(destination)
833    .use_url_credentials(true)
834}