Skip to main content

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