script/dom/
request.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::rc::Rc;
6use std::str::FromStr;
7
8use cssparser::match_ignore_ascii_case;
9use dom_struct::dom_struct;
10use http::Method as HttpMethod;
11use http::header::{HeaderName, HeaderValue};
12use http::method::InvalidMethod;
13use js::rust::HandleObject;
14use net_traits::ReferrerPolicy as MsgReferrerPolicy;
15use net_traits::fetch::headers::is_forbidden_method;
16use net_traits::request::{
17    CacheMode as NetTraitsRequestCache, CredentialsMode as NetTraitsRequestCredentials,
18    Destination as NetTraitsRequestDestination, Origin, RedirectMode as NetTraitsRequestRedirect,
19    Referrer as NetTraitsRequestReferrer, Request as NetTraitsRequest, RequestBuilder,
20    RequestMode as NetTraitsRequestMode, TraversableForUserPrompts,
21};
22use servo_url::ServoUrl;
23
24use crate::body::{BodyMixin, BodyType, Extractable, consume_body};
25use crate::conversions::Convert;
26use crate::dom::abortsignal::AbortSignal;
27use crate::dom::bindings::cell::DomRefCell;
28use crate::dom::bindings::codegen::Bindings::HeadersBinding::{HeadersInit, HeadersMethods};
29use crate::dom::bindings::codegen::Bindings::RequestBinding::{
30    ReferrerPolicy, RequestCache, RequestCredentials, RequestDestination, RequestInfo, RequestInit,
31    RequestMethods, RequestMode, RequestRedirect,
32};
33use crate::dom::bindings::error::{Error, Fallible};
34use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
35use crate::dom::bindings::root::{DomRoot, MutNullableDom};
36use crate::dom::bindings::str::{ByteString, DOMString, USVString};
37use crate::dom::bindings::trace::RootedTraceableBox;
38use crate::dom::globalscope::GlobalScope;
39use crate::dom::headers::{Guard, Headers};
40use crate::dom::promise::Promise;
41use crate::dom::readablestream::ReadableStream;
42use crate::script_runtime::CanGc;
43
44#[dom_struct]
45pub(crate) struct Request {
46    reflector_: Reflector,
47    #[no_trace]
48    /// <https://fetch.spec.whatwg.org/#concept-request-request>
49    request: DomRefCell<NetTraitsRequest>,
50    /// <https://fetch.spec.whatwg.org/#concept-request-body>
51    body_stream: MutNullableDom<ReadableStream>,
52    /// <https://fetch.spec.whatwg.org/#request-headers>
53    headers: MutNullableDom<Headers>,
54    /// <https://fetch.spec.whatwg.org/#request-signal>
55    signal: MutNullableDom<AbortSignal>,
56}
57
58impl Request {
59    fn new_inherited(global: &GlobalScope, url: ServoUrl) -> Request {
60        Request {
61            reflector_: Reflector::new(),
62            request: DomRefCell::new(net_request_from_global(global, url)),
63            body_stream: MutNullableDom::new(None),
64            headers: Default::default(),
65            signal: MutNullableDom::new(None),
66        }
67    }
68
69    fn new(
70        global: &GlobalScope,
71        proto: Option<HandleObject>,
72        url: ServoUrl,
73        can_gc: CanGc,
74    ) -> DomRoot<Request> {
75        reflect_dom_object_with_proto(
76            Box::new(Request::new_inherited(global, url)),
77            global,
78            proto,
79            can_gc,
80        )
81    }
82
83    fn from_net_request(
84        global: &GlobalScope,
85        proto: Option<HandleObject>,
86        net_request: NetTraitsRequest,
87        can_gc: CanGc,
88    ) -> DomRoot<Request> {
89        let r = Request::new(global, proto, net_request.current_url(), can_gc);
90        *r.request.borrow_mut() = net_request;
91        r
92    }
93
94    // https://fetch.spec.whatwg.org/#dom-request
95    pub(crate) fn constructor(
96        global: &GlobalScope,
97        proto: Option<HandleObject>,
98        can_gc: CanGc,
99        mut input: RequestInfo,
100        init: &RequestInit,
101    ) -> Fallible<DomRoot<Request>> {
102        // Step 1. Let request be null.
103        let temporary_request: NetTraitsRequest;
104
105        // Step 2. Let fallbackMode be null.
106        let mut fallback_mode: Option<NetTraitsRequestMode> = None;
107
108        // Step 3. Let baseURL be this’s relevant settings object’s API base URL.
109        let base_url = global.api_base_url();
110
111        // Step 4. Let signal be null.
112        let mut signal: Option<DomRoot<AbortSignal>> = None;
113
114        // Required later for step 41.1
115        let mut input_body_is_unusable = false;
116
117        match input {
118            // Step 5. If input is a string, then:
119            RequestInfo::USVString(USVString(ref usv_string)) => {
120                // Step 5.1. Let parsedURL be the result of parsing input with baseURL.
121                let parsed_url = base_url.join(usv_string);
122                // Step 5.2. If parsedURL is failure, then throw a TypeError.
123                if parsed_url.is_err() {
124                    return Err(Error::Type("Url could not be parsed".to_string()));
125                }
126                // Step 5.3. If parsedURL includes credentials, then throw a TypeError.
127                let url = parsed_url.unwrap();
128                if includes_credentials(&url) {
129                    return Err(Error::Type("Url includes credentials".to_string()));
130                }
131                // Step 5.4. Set request to a new request whose URL is parsedURL.
132                temporary_request = net_request_from_global(global, url);
133                // Step 5.5. Set fallbackMode to "cors".
134                fallback_mode = Some(NetTraitsRequestMode::CorsMode);
135            },
136            // Step 6. Otherwise:
137            // Step 6.1. Assert: input is a Request object.
138            RequestInfo::Request(ref input_request) => {
139                // Preparation for step 41.1
140                input_body_is_unusable = input_request.is_unusable();
141                // Step 6.2. Set request to input’s request.
142                temporary_request = input_request.request.borrow().clone();
143                // Step 6.3. Set signal to input’s signal.
144                signal = Some(input_request.Signal());
145            },
146        }
147
148        // Step 7. Let origin be this’s relevant settings object’s origin.
149        // TODO: `entry settings object` is not implemented yet.
150        let origin = base_url.origin();
151
152        // Step 8. Let traversableForUserPrompts be "client".
153        let mut traversable_for_user_prompts = TraversableForUserPrompts::Client;
154
155        // Step 9. If request’s traversable for user prompts is an environment settings object
156        // and its origin is same origin with origin, then set traversableForUserPrompts
157        // to request’s traversable for user prompts.
158        // TODO: `environment settings object` is not implemented in Servo yet.
159
160        // Step 10. If init["window"] exists and is non-null, then throw a TypeError.
161        if !init.window.handle().is_null_or_undefined() {
162            return Err(Error::Type("Window is present and is not null".to_string()));
163        }
164
165        // Step 11. If init["window"] exists, then set traversableForUserPrompts to "no-traversable".
166        if !init.window.handle().is_undefined() {
167            traversable_for_user_prompts = TraversableForUserPrompts::NoTraversable;
168        }
169
170        // Step 12. Set request to a new request with the following properties:
171        let mut request: NetTraitsRequest;
172        request = net_request_from_global(global, temporary_request.current_url());
173        request.method = temporary_request.method;
174        request.headers = temporary_request.headers.clone();
175        request.unsafe_request = true;
176        request.traversable_for_user_prompts = traversable_for_user_prompts;
177        // TODO: `entry settings object` is not implemented in Servo yet.
178        request.origin = Origin::Client;
179        request.referrer = temporary_request.referrer;
180        request.referrer_policy = temporary_request.referrer_policy;
181        request.mode = temporary_request.mode;
182        request.credentials_mode = temporary_request.credentials_mode;
183        request.cache_mode = temporary_request.cache_mode;
184        request.redirect_mode = temporary_request.redirect_mode;
185        request.integrity_metadata = temporary_request.integrity_metadata;
186
187        // Step 13. If init is not empty, then:
188        if init.body.is_some() ||
189            init.cache.is_some() ||
190            init.credentials.is_some() ||
191            init.integrity.is_some() ||
192            init.headers.is_some() ||
193            init.method.is_some() ||
194            init.mode.is_some() ||
195            init.redirect.is_some() ||
196            init.referrer.is_some() ||
197            init.referrerPolicy.is_some() ||
198            !init.window.handle().is_undefined()
199        {
200            // Step 13.1. If request’s mode is "navigate", then set it to "same-origin".
201            if request.mode == NetTraitsRequestMode::Navigate {
202                request.mode = NetTraitsRequestMode::SameOrigin;
203            }
204            // Step 13.2. Unset request’s reload-navigation flag.
205            // TODO
206            // Step 13.3. Unset request’s history-navigation flag.
207            // TODO
208            // Step 13.4. Set request’s origin to "client".
209            // TODO
210            // Step 13.5. Set request’s referrer to "client".
211            request.referrer = global.get_referrer();
212            // Step 13.6. Set request’s referrer policy to the empty string.
213            request.referrer_policy = MsgReferrerPolicy::EmptyString;
214            // Step 13.7. Set request’s URL to request’s current URL.
215            // TODO
216            // Step 13.8. Set request’s URL list to « request’s URL ».
217            // TODO
218        }
219
220        // Step 14. If init["referrer"] exists, then:
221        if let Some(init_referrer) = init.referrer.as_ref() {
222            // Step 14.1. Let referrer be init["referrer"].
223            let referrer = &init_referrer.0;
224            // Step 14.2. If referrer is the empty string, then set request’s referrer to "no-referrer".
225            if referrer.is_empty() {
226                request.referrer = NetTraitsRequestReferrer::NoReferrer;
227            // Step 14.3. Otherwise:
228            } else {
229                // Step 14.3.1. Let parsedReferrer be the result of parsing referrer with baseURL.
230                let parsed_referrer = base_url.join(referrer);
231                // Step 14.3.2. If parsedReferrer is failure, then throw a TypeError.
232                if parsed_referrer.is_err() {
233                    return Err(Error::Type("Failed to parse referrer url".to_string()));
234                }
235                // Step 14.3.3. If one of the following is true
236                // parsedReferrer’s scheme is "about" and path is the string "client"
237                // parsedReferrer’s origin is not same origin with origin
238                if let Ok(parsed_referrer) = parsed_referrer {
239                    if (parsed_referrer.cannot_be_a_base() &&
240                        parsed_referrer.scheme() == "about" &&
241                        parsed_referrer.path() == "client") ||
242                        parsed_referrer.origin() != origin
243                    {
244                        // then set request’s referrer to "client".
245                        request.referrer = global.get_referrer();
246                    } else {
247                        // Step 14.3.4. Otherwise, set request’s referrer to parsedReferrer.
248                        request.referrer = NetTraitsRequestReferrer::ReferrerUrl(parsed_referrer);
249                    }
250                }
251            }
252        }
253
254        // Step 15. If init["referrerPolicy"] exists, then set request’s referrer policy to it.
255        if let Some(init_referrerpolicy) = init.referrerPolicy.as_ref() {
256            let init_referrer_policy = (*init_referrerpolicy).convert();
257            request.referrer_policy = init_referrer_policy;
258        }
259
260        // Step 16. Let mode be init["mode"] if it exists, and fallbackMode otherwise.
261        let mode = init.mode.as_ref().map(|m| (*m).convert()).or(fallback_mode);
262
263        // Step 17. If mode is "navigate", then throw a TypeError.
264        if let Some(NetTraitsRequestMode::Navigate) = mode {
265            return Err(Error::Type("Request mode is Navigate".to_string()));
266        }
267
268        // Step 18. If mode is non-null, set request’s mode to mode.
269        if let Some(m) = mode {
270            request.mode = m;
271        }
272
273        // Step 19. If init["credentials"] exists, then set request’s credentials mode to it.
274        if let Some(init_credentials) = init.credentials.as_ref() {
275            let credentials = (*init_credentials).convert();
276            request.credentials_mode = credentials;
277        }
278
279        // Step 20. If init["cache"] exists, then set request’s cache mode to it.
280        if let Some(init_cache) = init.cache.as_ref() {
281            let cache = (*init_cache).convert();
282            request.cache_mode = cache;
283        }
284
285        // Step 21. If request’s cache mode is "only-if-cached" and request’s mode
286        // is not "same-origin", then throw a TypeError.
287        if request.cache_mode == NetTraitsRequestCache::OnlyIfCached &&
288            request.mode != NetTraitsRequestMode::SameOrigin
289        {
290            return Err(Error::Type(
291                "Cache is 'only-if-cached' and mode is not 'same-origin'".to_string(),
292            ));
293        }
294
295        // Step 22. If init["redirect"] exists, then set request’s redirect mode to it.
296        if let Some(init_redirect) = init.redirect.as_ref() {
297            let redirect = (*init_redirect).convert();
298            request.redirect_mode = redirect;
299        }
300
301        // Step 23. If init["integrity"] exists, then set request’s integrity metadata to it.
302        if let Some(init_integrity) = init.integrity.as_ref() {
303            let integrity = init_integrity.clone().to_string();
304            request.integrity_metadata = integrity;
305        }
306
307        // Step 24.If init["keepalive"] exists, then set request’s keepalive to it.
308        // TODO
309
310        // Step 25. If init["method"] exists, then:
311        // Step 25.1. Let method be init["method"].
312        if let Some(init_method) = init.method.as_ref() {
313            // Step 25.2. If method is not a method or method is a forbidden method, then throw a TypeError.
314            if !is_method(init_method) {
315                return Err(Error::Type("Method is not a method".to_string()));
316            }
317            if is_forbidden_method(init_method) {
318                return Err(Error::Type("Method is forbidden".to_string()));
319            }
320            // Step 25.3. Normalize method.
321            let method = match init_method.as_str() {
322                Some(s) => normalize_method(s)
323                    .map_err(|e| Error::Type(format!("Method is not valid: {:?}", e)))?,
324                None => return Err(Error::Type("Method is not a valid UTF8".to_string())),
325            };
326            // Step 25.4. Set request’s method to method.
327            request.method = method;
328        }
329
330        // Step 26. If init["signal"] exists, then set signal to it.
331        if let Some(init_signal) = init.signal.as_ref() {
332            signal = init_signal.clone();
333        }
334        // Step 27. If init["priority"] exists, then:
335        // TODO
336        // Step 27.1. If request’s internal priority is not null,
337        // then update request’s internal priority in an implementation-defined manner.
338        // TODO
339        // Step 27.2. Otherwise, set request’s priority to init["priority"].
340        // TODO
341
342        // Step 28. Set this’s request to request.
343        let r = Request::from_net_request(global, proto, request, can_gc);
344
345        // Step 29. Let signals be « signal » if signal is non-null; otherwise « ».
346        let signals = signal.map_or(vec![], |s| vec![s]);
347        // Step 30. Set this’s signal to the result of creating a dependent
348        // abort signal from signals, using AbortSignal and this’s relevant realm.
349        r.signal
350            .set(Some(&AbortSignal::create_dependent_abort_signal(
351                signals, global, can_gc,
352            )));
353
354        // Step 31. Set this’s headers to a new Headers object with this’s relevant realm,
355        // whose header list is request’s header list and guard is "request".
356        //
357        // "or_init" looks unclear here, but it always enters the block since r
358        // hasn't had any other way to initialize its headers
359        r.headers
360            .or_init(|| Headers::for_request(&r.global(), can_gc));
361
362        // Step 33. If init is not empty, then:
363        //
364        // but spec says this should only be when non-empty init?
365        let headers_copy = init
366            .headers
367            .as_ref()
368            .map(|possible_header| match possible_header {
369                HeadersInit::ByteStringSequenceSequence(init_sequence) => {
370                    HeadersInit::ByteStringSequenceSequence(init_sequence.clone())
371                },
372                HeadersInit::ByteStringByteStringRecord(init_map) => {
373                    HeadersInit::ByteStringByteStringRecord(init_map.clone())
374                },
375            });
376
377        // Step 33.3
378        // We cannot empty `r.Headers().header_list` because
379        // we would undo the Step 25 above.  One alternative is to set
380        // `headers_copy` as a deep copy of `r.Headers()`. However,
381        // `r.Headers()` is a `DomRoot<T>`, and therefore it is difficult
382        // to obtain a mutable reference to `r.Headers()`. Without the
383        // mutable reference, we cannot mutate `r.Headers()` to be the
384        // deep copied headers in Step 25.
385
386        // Step 32. If this’s request’s mode is "no-cors", then:
387        if r.request.borrow().mode == NetTraitsRequestMode::NoCors {
388            let borrowed_request = r.request.borrow();
389            // Step 32.1. If this’s request’s method is not a CORS-safelisted method, then throw a TypeError.
390            if !is_cors_safelisted_method(&borrowed_request.method) {
391                return Err(Error::Type(
392                    "The mode is 'no-cors' but the method is not a cors-safelisted method"
393                        .to_string(),
394                ));
395            }
396            // Step 32.2. Set this’s headers’s guard to "request-no-cors".
397            r.Headers(can_gc).set_guard(Guard::RequestNoCors);
398        }
399
400        match headers_copy {
401            None => {
402                // Step 33.4. If headers is a Headers object, then for each header of its header list, append header to this’s headers.
403                //
404                // This is equivalent to the specification's concept of
405                // "associated headers list". If an init headers is not given,
406                // but an input with headers is given, set request's
407                // headers as the input's Headers.
408                if let RequestInfo::Request(ref input_request) = input {
409                    r.Headers(can_gc)
410                        .copy_from_headers(input_request.Headers(can_gc))?;
411                }
412            },
413            // Step 33.5. Otherwise, fill this’s headers with headers.
414            Some(headers_copy) => r.Headers(can_gc).fill(Some(headers_copy))?,
415        }
416
417        // Step 33.5 depending on how we got here
418        // Copy the headers list onto the headers of net_traits::Request
419        r.request.borrow_mut().headers = r.Headers(can_gc).get_headers_list();
420
421        // Step 34. Let inputBody be input’s request’s body if input is a Request object; otherwise null.
422        let input_body = if let RequestInfo::Request(ref mut input_request) = input {
423            let mut input_request_request = input_request.request.borrow_mut();
424            r.body_stream.set(input_request.body().as_deref());
425            input_request_request.body.take()
426        } else {
427            None
428        };
429
430        // Step 35. If either init["body"] exists and is non-null or inputBody is non-null,
431        // and request’s method is `GET` or `HEAD`, then throw a TypeError.
432        if init.body.as_ref().is_some_and(|body| body.is_some()) || input_body.is_some() {
433            let req = r.request.borrow();
434            let req_method = &req.method;
435            match *req_method {
436                HttpMethod::GET => {
437                    return Err(Error::Type(
438                        "Init's body is non-null, and request method is GET".to_string(),
439                    ));
440                },
441                HttpMethod::HEAD => {
442                    return Err(Error::Type(
443                        "Init's body is non-null, and request method is HEAD".to_string(),
444                    ));
445                },
446                _ => {},
447            }
448        }
449
450        // Step 36. Let initBody be null.
451        let mut init_body = None;
452        // Step 37. If init["body"] exists and is non-null, then:
453        if let Some(Some(ref input_init_body)) = init.body {
454            // Step 37.1. Let bodyWithType be the result of extracting init["body"], with keepalive set to request’s keepalive.
455            let mut body_with_type = input_init_body.extract(global, can_gc)?;
456
457            // Step 37.3. Let type be bodyWithType’s type.
458            if let Some(contents) = body_with_type.content_type.take() {
459                let ct_header_name = b"Content-Type";
460                // Step 37.4. If type is non-null and this’s headers’s header list
461                // does not contain `Content-Type`, then append (`Content-Type`, type) to this’s headers.
462                if !r
463                    .Headers(can_gc)
464                    .Has(ByteString::new(ct_header_name.to_vec()))
465                    .unwrap()
466                {
467                    let ct_header_val = contents.as_bytes();
468                    r.Headers(can_gc).Append(
469                        ByteString::new(ct_header_name.to_vec()),
470                        ByteString::new(ct_header_val.to_vec()),
471                    )?;
472
473                    // In Servo r.Headers's header list isn't a pointer to
474                    // the same actual list as r.request's, and so we need to
475                    // append to both lists to keep them in sync.
476                    if let Ok(v) = HeaderValue::from_bytes(&ct_header_val) {
477                        r.request
478                            .borrow_mut()
479                            .headers
480                            .insert(HeaderName::from_bytes(ct_header_name).unwrap(), v);
481                    }
482                }
483            }
484
485            // Step 37.2. Set initBody to bodyWithType’s body.
486            let (net_body, stream) = body_with_type.into_net_request_body();
487            r.body_stream.set(Some(&*stream));
488            init_body = Some(net_body);
489        }
490
491        // Step 38. Let inputOrInitBody be initBody if it is non-null; otherwise inputBody.
492        // Step 40. Let finalBody be inputOrInitBody.
493        // Step 41.2. Set finalBody to the result of creating a proxy for inputBody.
494        //
495        // There are multiple reassignments to similar values. In the end, all end up as
496        // final_body. Therefore, final_body is equivalent to inputOrInitBody
497        let final_body = init_body.or(input_body);
498
499        // Step 39. If inputOrInitBody is non-null and inputOrInitBody’s source is null, then:
500        if final_body
501            .as_ref()
502            .is_some_and(|body| body.source_is_null())
503        {
504            // Step 39.1. If initBody is non-null and init["duplex"] does not exist, then throw a TypeError.
505            // TODO
506            // Step 39.2. If this’s request’s mode is neither "same-origin" nor "cors", then throw a TypeError.
507            let request_mode = &r.request.borrow().mode;
508            if *request_mode != NetTraitsRequestMode::CorsMode &&
509                *request_mode != NetTraitsRequestMode::SameOrigin
510            {
511                return Err(Error::Type(
512                    "Request mode must be Cors or SameOrigin".to_string(),
513                ));
514            }
515            // Step 39.3. Set this’s request’s use-CORS-preflight flag.
516            // TODO
517        }
518
519        // Step 41. If initBody is null and inputBody is non-null, then:
520        // Step 41.1. If inputBody is unusable, then throw a TypeError.
521        //
522        // We only perform this check on input_body. However, we already
523        // processed the input body. Therefore, we check it all the way
524        // above and throw the error at the last possible moment
525        if input_body_is_unusable {
526            return Err(Error::Type("Input body is unusable".to_string()));
527        }
528
529        // Step 42. Set this’s request’s body to finalBody.
530        r.request.borrow_mut().body = final_body;
531
532        Ok(r)
533    }
534
535    /// <https://fetch.spec.whatwg.org/#concept-request-clone>
536    fn clone_from(r: &Request, can_gc: CanGc) -> Fallible<DomRoot<Request>> {
537        // Step 1. Let newRequest be a copy of request, except for its body.
538        let req = r.request.borrow();
539        let url = req.url();
540        let headers_guard = r.Headers(can_gc).get_guard();
541        let r_clone = Request::new(&r.global(), None, url, can_gc);
542        r_clone.request.borrow_mut().pipeline_id = req.pipeline_id;
543        {
544            let mut borrowed_r_request = r_clone.request.borrow_mut();
545            borrowed_r_request.origin = req.origin.clone();
546        }
547        *r_clone.request.borrow_mut() = req.clone();
548        r_clone
549            .Headers(can_gc)
550            .copy_from_headers(r.Headers(can_gc))?;
551        r_clone.Headers(can_gc).set_guard(headers_guard);
552        // Step 2. If request’s body is non-null, set newRequest’s body to the result of cloning request’s body.
553        // TODO
554        // Step 3. Return newRequest.
555        Ok(r_clone)
556    }
557
558    pub(crate) fn get_request(&self) -> NetTraitsRequest {
559        self.request.borrow().clone()
560    }
561}
562
563fn net_request_from_global(global: &GlobalScope, url: ServoUrl) -> NetTraitsRequest {
564    RequestBuilder::new(global.webview_id(), url, global.get_referrer())
565        .origin(global.get_url().origin())
566        .pipeline_id(Some(global.pipeline_id()))
567        .https_state(global.get_https_state())
568        .insecure_requests_policy(global.insecure_requests_policy())
569        .has_trustworthy_ancestor_origin(global.has_trustworthy_ancestor_or_current_origin())
570        .policy_container(global.policy_container())
571        .build()
572}
573
574/// <https://fetch.spec.whatwg.org/#concept-method-normalize>
575fn normalize_method(m: &str) -> Result<HttpMethod, InvalidMethod> {
576    match_ignore_ascii_case! { m,
577        "delete" => return Ok(HttpMethod::DELETE),
578        "get" => return Ok(HttpMethod::GET),
579        "head" => return Ok(HttpMethod::HEAD),
580        "options" => return Ok(HttpMethod::OPTIONS),
581        "post" => return Ok(HttpMethod::POST),
582        "put" => return Ok(HttpMethod::PUT),
583        _ => (),
584    }
585    debug!("Method: {:?}", m);
586    HttpMethod::from_str(m)
587}
588
589/// <https://fetch.spec.whatwg.org/#concept-method>
590fn is_method(m: &ByteString) -> bool {
591    m.as_str().is_some()
592}
593
594/// <https://fetch.spec.whatwg.org/#cors-safelisted-method>
595fn is_cors_safelisted_method(m: &HttpMethod) -> bool {
596    m == HttpMethod::GET || m == HttpMethod::HEAD || m == HttpMethod::POST
597}
598
599/// <https://url.spec.whatwg.org/#include-credentials>
600fn includes_credentials(input: &ServoUrl) -> bool {
601    !input.username().is_empty() || input.password().is_some()
602}
603
604impl RequestMethods<crate::DomTypeHolder> for Request {
605    /// <https://fetch.spec.whatwg.org/#dom-request>
606    fn Constructor(
607        global: &GlobalScope,
608        proto: Option<HandleObject>,
609        can_gc: CanGc,
610        input: RequestInfo,
611        init: RootedTraceableBox<RequestInit>,
612    ) -> Fallible<DomRoot<Request>> {
613        Self::constructor(global, proto, can_gc, input, &init)
614    }
615
616    /// <https://fetch.spec.whatwg.org/#dom-request-method>
617    fn Method(&self) -> ByteString {
618        let r = self.request.borrow();
619        ByteString::new(r.method.as_ref().as_bytes().into())
620    }
621
622    /// <https://fetch.spec.whatwg.org/#dom-request-url>
623    fn Url(&self) -> USVString {
624        let r = self.request.borrow();
625        USVString(r.url_list.first().map_or("", |u| u.as_str()).into())
626    }
627
628    /// <https://fetch.spec.whatwg.org/#dom-request-headers>
629    fn Headers(&self, can_gc: CanGc) -> DomRoot<Headers> {
630        self.headers
631            .or_init(|| Headers::new(&self.global(), can_gc))
632    }
633
634    /// <https://fetch.spec.whatwg.org/#dom-request-destination>
635    fn Destination(&self) -> RequestDestination {
636        self.request.borrow().destination.convert()
637    }
638
639    /// <https://fetch.spec.whatwg.org/#dom-request-referrer>
640    fn Referrer(&self) -> USVString {
641        let r = self.request.borrow();
642        USVString(match r.referrer {
643            NetTraitsRequestReferrer::NoReferrer => String::from(""),
644            NetTraitsRequestReferrer::Client(_) => String::from("about:client"),
645            NetTraitsRequestReferrer::ReferrerUrl(ref u) => {
646                let u_c = u.clone();
647                u_c.into_string()
648            },
649        })
650    }
651
652    /// <https://fetch.spec.whatwg.org/#dom-request-referrerpolicy>
653    fn ReferrerPolicy(&self) -> ReferrerPolicy {
654        self.request.borrow().referrer_policy.convert()
655    }
656
657    /// <https://fetch.spec.whatwg.org/#dom-request-mode>
658    fn Mode(&self) -> RequestMode {
659        self.request.borrow().mode.clone().convert()
660    }
661
662    /// <https://fetch.spec.whatwg.org/#dom-request-credentials>
663    fn Credentials(&self) -> RequestCredentials {
664        let r = self.request.borrow().clone();
665        r.credentials_mode.convert()
666    }
667
668    /// <https://fetch.spec.whatwg.org/#dom-request-cache>
669    fn Cache(&self) -> RequestCache {
670        let r = self.request.borrow().clone();
671        r.cache_mode.convert()
672    }
673
674    /// <https://fetch.spec.whatwg.org/#dom-request-redirect>
675    fn Redirect(&self) -> RequestRedirect {
676        let r = self.request.borrow().clone();
677        r.redirect_mode.convert()
678    }
679
680    /// <https://fetch.spec.whatwg.org/#dom-request-integrity>
681    fn Integrity(&self) -> DOMString {
682        let r = self.request.borrow();
683        DOMString::from_string(r.integrity_metadata.clone())
684    }
685
686    /// <https://fetch.spec.whatwg.org/#dom-body-body>
687    fn GetBody(&self) -> Option<DomRoot<ReadableStream>> {
688        self.body()
689    }
690
691    /// <https://fetch.spec.whatwg.org/#dom-body-bodyused>
692    fn BodyUsed(&self) -> bool {
693        self.is_body_used()
694    }
695
696    /// <https://fetch.spec.whatwg.org/#dom-request-signal>
697    fn Signal(&self) -> DomRoot<AbortSignal> {
698        self.signal
699            .get()
700            .expect("Should always be initialized in constructor and clone")
701    }
702
703    /// <https://fetch.spec.whatwg.org/#dom-request-clone>
704    fn Clone(&self, can_gc: CanGc) -> Fallible<DomRoot<Request>> {
705        // Step 1. If this is unusable, then throw a TypeError.
706        if self.is_unusable() {
707            return Err(Error::Type("Request is unusable".to_string()));
708        }
709
710        // Step 2. Let clonedRequest be the result of cloning this’s request.
711        let cloned_request = Request::clone_from(self, can_gc)?;
712        // Step 3. Assert: this’s signal is non-null.
713        let signal = self.signal.get().expect("Should always be initialized");
714        // Step 4. Let clonedSignal be the result of creating a dependent
715        // abort signal from « this’s signal », using AbortSignal and this’s relevant realm.
716        let cloned_signal =
717            AbortSignal::create_dependent_abort_signal(vec![signal], &self.global(), can_gc);
718        // Step 5. Let clonedRequestObject be the result of creating a Request object,
719        // given clonedRequest, this’s headers’s guard, clonedSignal and this’s relevant realm.
720        //
721        // These steps already happen in `clone_from`
722        cloned_request.signal.set(Some(&cloned_signal));
723        // Step 6. Return clonedRequestObject.
724        Ok(cloned_request)
725    }
726
727    /// <https://fetch.spec.whatwg.org/#dom-body-text>
728    fn Text(&self, can_gc: CanGc) -> Rc<Promise> {
729        consume_body(self, BodyType::Text, can_gc)
730    }
731
732    /// <https://fetch.spec.whatwg.org/#dom-body-blob>
733    fn Blob(&self, can_gc: CanGc) -> Rc<Promise> {
734        consume_body(self, BodyType::Blob, can_gc)
735    }
736
737    /// <https://fetch.spec.whatwg.org/#dom-body-formdata>
738    fn FormData(&self, can_gc: CanGc) -> Rc<Promise> {
739        consume_body(self, BodyType::FormData, can_gc)
740    }
741
742    /// <https://fetch.spec.whatwg.org/#dom-body-json>
743    fn Json(&self, can_gc: CanGc) -> Rc<Promise> {
744        consume_body(self, BodyType::Json, can_gc)
745    }
746
747    /// <https://fetch.spec.whatwg.org/#dom-body-arraybuffer>
748    fn ArrayBuffer(&self, can_gc: CanGc) -> Rc<Promise> {
749        consume_body(self, BodyType::ArrayBuffer, can_gc)
750    }
751
752    /// <https://fetch.spec.whatwg.org/#dom-body-bytes>
753    fn Bytes(&self, can_gc: CanGc) -> std::rc::Rc<Promise> {
754        consume_body(self, BodyType::Bytes, can_gc)
755    }
756}
757
758impl BodyMixin for Request {
759    fn is_body_used(&self) -> bool {
760        let body_stream = self.body_stream.get();
761        body_stream
762            .as_ref()
763            .is_some_and(|stream| stream.is_disturbed())
764    }
765
766    fn is_unusable(&self) -> bool {
767        let body_stream = self.body_stream.get();
768        body_stream
769            .as_ref()
770            .is_some_and(|stream| stream.is_disturbed() || stream.is_locked())
771    }
772
773    fn body(&self) -> Option<DomRoot<ReadableStream>> {
774        self.body_stream.get()
775    }
776
777    fn get_mime_type(&self, can_gc: CanGc) -> Vec<u8> {
778        let headers = self.Headers(can_gc);
779        headers.extract_mime_type()
780    }
781}
782
783impl Convert<NetTraitsRequestCache> for RequestCache {
784    fn convert(self) -> NetTraitsRequestCache {
785        match self {
786            RequestCache::Default => NetTraitsRequestCache::Default,
787            RequestCache::No_store => NetTraitsRequestCache::NoStore,
788            RequestCache::Reload => NetTraitsRequestCache::Reload,
789            RequestCache::No_cache => NetTraitsRequestCache::NoCache,
790            RequestCache::Force_cache => NetTraitsRequestCache::ForceCache,
791            RequestCache::Only_if_cached => NetTraitsRequestCache::OnlyIfCached,
792        }
793    }
794}
795
796impl Convert<RequestCache> for NetTraitsRequestCache {
797    fn convert(self) -> RequestCache {
798        match self {
799            NetTraitsRequestCache::Default => RequestCache::Default,
800            NetTraitsRequestCache::NoStore => RequestCache::No_store,
801            NetTraitsRequestCache::Reload => RequestCache::Reload,
802            NetTraitsRequestCache::NoCache => RequestCache::No_cache,
803            NetTraitsRequestCache::ForceCache => RequestCache::Force_cache,
804            NetTraitsRequestCache::OnlyIfCached => RequestCache::Only_if_cached,
805        }
806    }
807}
808
809impl Convert<NetTraitsRequestCredentials> for RequestCredentials {
810    fn convert(self) -> NetTraitsRequestCredentials {
811        match self {
812            RequestCredentials::Omit => NetTraitsRequestCredentials::Omit,
813            RequestCredentials::Same_origin => NetTraitsRequestCredentials::CredentialsSameOrigin,
814            RequestCredentials::Include => NetTraitsRequestCredentials::Include,
815        }
816    }
817}
818
819impl Convert<RequestCredentials> for NetTraitsRequestCredentials {
820    fn convert(self) -> RequestCredentials {
821        match self {
822            NetTraitsRequestCredentials::Omit => RequestCredentials::Omit,
823            NetTraitsRequestCredentials::CredentialsSameOrigin => RequestCredentials::Same_origin,
824            NetTraitsRequestCredentials::Include => RequestCredentials::Include,
825        }
826    }
827}
828
829impl Convert<NetTraitsRequestDestination> for RequestDestination {
830    fn convert(self) -> NetTraitsRequestDestination {
831        match self {
832            RequestDestination::_empty => NetTraitsRequestDestination::None,
833            RequestDestination::Audio => NetTraitsRequestDestination::Audio,
834            RequestDestination::Document => NetTraitsRequestDestination::Document,
835            RequestDestination::Embed => NetTraitsRequestDestination::Embed,
836            RequestDestination::Font => NetTraitsRequestDestination::Font,
837            RequestDestination::Frame => NetTraitsRequestDestination::Frame,
838            RequestDestination::Iframe => NetTraitsRequestDestination::IFrame,
839            RequestDestination::Image => NetTraitsRequestDestination::Image,
840            RequestDestination::Manifest => NetTraitsRequestDestination::Manifest,
841            RequestDestination::Json => NetTraitsRequestDestination::Json,
842            RequestDestination::Object => NetTraitsRequestDestination::Object,
843            RequestDestination::Report => NetTraitsRequestDestination::Report,
844            RequestDestination::Script => NetTraitsRequestDestination::Script,
845            RequestDestination::Sharedworker => NetTraitsRequestDestination::SharedWorker,
846            RequestDestination::Style => NetTraitsRequestDestination::Style,
847            RequestDestination::Track => NetTraitsRequestDestination::Track,
848            RequestDestination::Video => NetTraitsRequestDestination::Video,
849            RequestDestination::Worker => NetTraitsRequestDestination::Worker,
850            RequestDestination::Xslt => NetTraitsRequestDestination::Xslt,
851        }
852    }
853}
854
855impl Convert<RequestDestination> for NetTraitsRequestDestination {
856    fn convert(self) -> RequestDestination {
857        match self {
858            NetTraitsRequestDestination::None => RequestDestination::_empty,
859            NetTraitsRequestDestination::Audio => RequestDestination::Audio,
860            NetTraitsRequestDestination::Document => RequestDestination::Document,
861            NetTraitsRequestDestination::Embed => RequestDestination::Embed,
862            NetTraitsRequestDestination::Font => RequestDestination::Font,
863            NetTraitsRequestDestination::Frame => RequestDestination::Frame,
864            NetTraitsRequestDestination::IFrame => RequestDestination::Iframe,
865            NetTraitsRequestDestination::Image => RequestDestination::Image,
866            NetTraitsRequestDestination::Manifest => RequestDestination::Manifest,
867            NetTraitsRequestDestination::Json => RequestDestination::Json,
868            NetTraitsRequestDestination::Object => RequestDestination::Object,
869            NetTraitsRequestDestination::Report => RequestDestination::Report,
870            NetTraitsRequestDestination::Script => RequestDestination::Script,
871            NetTraitsRequestDestination::ServiceWorker |
872            NetTraitsRequestDestination::AudioWorklet |
873            NetTraitsRequestDestination::PaintWorklet => {
874                panic!("ServiceWorker request destination should not be exposed to DOM")
875            },
876            NetTraitsRequestDestination::SharedWorker => RequestDestination::Sharedworker,
877            NetTraitsRequestDestination::Style => RequestDestination::Style,
878            NetTraitsRequestDestination::Track => RequestDestination::Track,
879            NetTraitsRequestDestination::Video => RequestDestination::Video,
880            NetTraitsRequestDestination::Worker => RequestDestination::Worker,
881            NetTraitsRequestDestination::Xslt => RequestDestination::Xslt,
882            NetTraitsRequestDestination::WebIdentity => RequestDestination::_empty,
883        }
884    }
885}
886
887impl Convert<NetTraitsRequestMode> for RequestMode {
888    fn convert(self) -> NetTraitsRequestMode {
889        match self {
890            RequestMode::Navigate => NetTraitsRequestMode::Navigate,
891            RequestMode::Same_origin => NetTraitsRequestMode::SameOrigin,
892            RequestMode::No_cors => NetTraitsRequestMode::NoCors,
893            RequestMode::Cors => NetTraitsRequestMode::CorsMode,
894        }
895    }
896}
897
898impl Convert<RequestMode> for NetTraitsRequestMode {
899    fn convert(self) -> RequestMode {
900        match self {
901            NetTraitsRequestMode::Navigate => RequestMode::Navigate,
902            NetTraitsRequestMode::SameOrigin => RequestMode::Same_origin,
903            NetTraitsRequestMode::NoCors => RequestMode::No_cors,
904            NetTraitsRequestMode::CorsMode => RequestMode::Cors,
905            NetTraitsRequestMode::WebSocket { .. } => {
906                unreachable!("Websocket request mode should never be exposed to Dom")
907            },
908        }
909    }
910}
911
912impl Convert<MsgReferrerPolicy> for ReferrerPolicy {
913    fn convert(self) -> MsgReferrerPolicy {
914        match self {
915            ReferrerPolicy::_empty => MsgReferrerPolicy::EmptyString,
916            ReferrerPolicy::No_referrer => MsgReferrerPolicy::NoReferrer,
917            ReferrerPolicy::No_referrer_when_downgrade => {
918                MsgReferrerPolicy::NoReferrerWhenDowngrade
919            },
920            ReferrerPolicy::Origin => MsgReferrerPolicy::Origin,
921            ReferrerPolicy::Origin_when_cross_origin => MsgReferrerPolicy::OriginWhenCrossOrigin,
922            ReferrerPolicy::Unsafe_url => MsgReferrerPolicy::UnsafeUrl,
923            ReferrerPolicy::Same_origin => MsgReferrerPolicy::SameOrigin,
924            ReferrerPolicy::Strict_origin => MsgReferrerPolicy::StrictOrigin,
925            ReferrerPolicy::Strict_origin_when_cross_origin => {
926                MsgReferrerPolicy::StrictOriginWhenCrossOrigin
927            },
928        }
929    }
930}
931
932impl Convert<ReferrerPolicy> for MsgReferrerPolicy {
933    fn convert(self) -> ReferrerPolicy {
934        match self {
935            MsgReferrerPolicy::EmptyString => ReferrerPolicy::_empty,
936            MsgReferrerPolicy::NoReferrer => ReferrerPolicy::No_referrer,
937            MsgReferrerPolicy::NoReferrerWhenDowngrade => {
938                ReferrerPolicy::No_referrer_when_downgrade
939            },
940            MsgReferrerPolicy::Origin => ReferrerPolicy::Origin,
941            MsgReferrerPolicy::OriginWhenCrossOrigin => ReferrerPolicy::Origin_when_cross_origin,
942            MsgReferrerPolicy::UnsafeUrl => ReferrerPolicy::Unsafe_url,
943            MsgReferrerPolicy::SameOrigin => ReferrerPolicy::Same_origin,
944            MsgReferrerPolicy::StrictOrigin => ReferrerPolicy::Strict_origin,
945            MsgReferrerPolicy::StrictOriginWhenCrossOrigin => {
946                ReferrerPolicy::Strict_origin_when_cross_origin
947            },
948        }
949    }
950}
951
952impl Convert<NetTraitsRequestRedirect> for RequestRedirect {
953    fn convert(self) -> NetTraitsRequestRedirect {
954        match self {
955            RequestRedirect::Follow => NetTraitsRequestRedirect::Follow,
956            RequestRedirect::Error => NetTraitsRequestRedirect::Error,
957            RequestRedirect::Manual => NetTraitsRequestRedirect::Manual,
958        }
959    }
960}
961
962impl Convert<RequestRedirect> for NetTraitsRequestRedirect {
963    fn convert(self) -> RequestRedirect {
964        match self {
965            NetTraitsRequestRedirect::Follow => RequestRedirect::Follow,
966            NetTraitsRequestRedirect::Error => RequestRedirect::Error,
967            NetTraitsRequestRedirect::Manual => RequestRedirect::Manual,
968        }
969    }
970}