Skip to main content

net_traits/
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::sync::Arc;
6
7use content_security_policy::{self as csp};
8use http::header::{AUTHORIZATION, HeaderName};
9use http::{HeaderMap, Method};
10use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
11use ipc_channel::router::ROUTER;
12use log::error;
13use malloc_size_of_derive::MallocSizeOf;
14use mime::Mime;
15use parking_lot::Mutex;
16use rustc_hash::FxHashMap;
17use serde::{Deserialize, Serialize};
18use servo_base::generic_channel::GenericSharedMemory;
19use servo_base::id::{PipelineId, WebViewId};
20use servo_url::{ImmutableOrigin, ServoUrl};
21use tokio::sync::oneshot::Sender as TokioSender;
22use url::Position;
23use uuid::Uuid;
24
25use crate::ReferrerPolicy;
26use crate::blob_url_store::UrlWithBlobClaim;
27use crate::policy_container::{PolicyContainer, RequestPolicyContainer};
28use crate::pub_domains::is_same_site;
29use crate::resource_fetch_timing::ResourceTimingType;
30use crate::response::{RedirectTaint, Response};
31
32#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
33/// An id to differentiate one network request from another.
34pub struct RequestId(pub Uuid);
35
36impl Default for RequestId {
37    fn default() -> Self {
38        Self(Uuid::new_v4())
39    }
40}
41
42/// An [initiator](https://fetch.spec.whatwg.org/#concept-request-initiator)
43#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
44pub enum Initiator {
45    None,
46    Download,
47    ImageSet,
48    Manifest,
49    XSLT,
50    Prefetch,
51    Link,
52}
53
54/// A request [destination](https://fetch.spec.whatwg.org/#concept-request-destination)
55pub use csp::Destination;
56
57/// A request [origin](https://fetch.spec.whatwg.org/#concept-request-origin)
58#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
59pub enum Origin {
60    Client,
61    Origin(ImmutableOrigin),
62}
63
64impl Origin {
65    pub fn is_opaque(&self) -> bool {
66        matches!(self, Origin::Origin(ImmutableOrigin::Opaque(_)))
67    }
68}
69
70/// A [referer](https://fetch.spec.whatwg.org/#concept-request-referrer)
71#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
72pub enum Referrer {
73    NoReferrer,
74    /// Contains the url that "client" would be resolved to. See
75    /// [https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer](https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer)
76    ///
77    /// If you are unsure you should probably use
78    /// [`GlobalScope::get_referrer`](https://doc.servo.org/script/dom/globalscope/struct.GlobalScope.html#method.get_referrer)
79    Client(ServoUrl),
80    ReferrerUrl(ServoUrl),
81}
82
83/// A [request mode](https://fetch.spec.whatwg.org/#concept-request-mode)
84#[derive(Clone, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
85pub enum RequestMode {
86    Navigate,
87    SameOrigin,
88    NoCors,
89    CorsMode,
90    WebSocket {
91        protocols: Vec<String>,
92        original_url: ServoUrl,
93    },
94}
95
96/// Request [credentials mode](https://fetch.spec.whatwg.org/#concept-request-credentials-mode)
97#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
98pub enum CredentialsMode {
99    Omit,
100    CredentialsSameOrigin,
101    Include,
102}
103
104/// [Cache mode](https://fetch.spec.whatwg.org/#concept-request-cache-mode)
105#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
106pub enum CacheMode {
107    Default,
108    NoStore,
109    Reload,
110    NoCache,
111    ForceCache,
112    OnlyIfCached,
113}
114
115/// [Service-workers mode](https://fetch.spec.whatwg.org/#request-service-workers-mode)
116#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
117pub enum ServiceWorkersMode {
118    All,
119    None,
120}
121
122/// [Redirect mode](https://fetch.spec.whatwg.org/#concept-request-redirect-mode)
123#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
124pub enum RedirectMode {
125    Follow,
126    Error,
127    Manual,
128}
129
130/// [Response tainting](https://fetch.spec.whatwg.org/#concept-request-response-tainting)
131#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
132pub enum ResponseTainting {
133    Basic,
134    CorsTainting,
135    Opaque,
136}
137
138/// <https://html.spec.whatwg.org/multipage/#preload-key>
139#[derive(Clone, Debug, Eq, Hash, Deserialize, MallocSizeOf, Serialize, PartialEq)]
140pub struct PreloadKey {
141    /// <https://html.spec.whatwg.org/multipage/#preload-url>
142    pub url: ServoUrl,
143    /// <https://html.spec.whatwg.org/multipage/#preload-destination>
144    pub destination: Destination,
145    /// <https://html.spec.whatwg.org/multipage/#preload-mode>
146    pub mode: RequestMode,
147    /// <https://html.spec.whatwg.org/multipage/#preload-credentials-mode>
148    pub credentials_mode: CredentialsMode,
149}
150
151impl PreloadKey {
152    pub fn new(request: &RequestBuilder) -> Self {
153        Self {
154            url: request.url.url(),
155            destination: request.destination,
156            mode: request.mode.clone(),
157            credentials_mode: request.credentials_mode,
158        }
159    }
160}
161
162#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize, Hash, MallocSizeOf)]
163pub struct PreloadId(pub Uuid);
164
165impl Default for PreloadId {
166    fn default() -> Self {
167        Self(Uuid::new_v4())
168    }
169}
170
171/// <https://html.spec.whatwg.org/multipage/#preload-entry>
172#[derive(Debug, MallocSizeOf)]
173pub struct PreloadEntry {
174    /// <https://html.spec.whatwg.org/multipage/#preload-integrity-metadata>
175    pub integrity_metadata: String,
176    /// <https://html.spec.whatwg.org/multipage/#preload-response>
177    pub response: Option<Response>,
178    /// <https://html.spec.whatwg.org/multipage/#preload-on-response-available>
179    pub on_response_available: Option<TokioSender<Response>>,
180}
181
182impl PreloadEntry {
183    pub fn new(integrity_metadata: String) -> Self {
184        Self {
185            integrity_metadata,
186            response: None,
187            on_response_available: None,
188        }
189    }
190
191    /// Part of step 11.5 of <https://html.spec.whatwg.org/multipage/#preload>
192    pub fn with_response(&mut self, response: Response) {
193        // Step 11.5. If entry's on response available is null, then set entry's response to response;
194        // otherwise call entry's on response available given response.
195        if let Some(sender) = self.on_response_available.take() {
196            let _ = sender.send(response);
197        } else {
198            self.response = Some(response);
199        }
200    }
201}
202
203pub type PreloadedResources = FxHashMap<PreloadKey, PreloadId>;
204
205/// <https://fetch.spec.whatwg.org/#concept-request-client>
206#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
207pub struct RequestClient {
208    /// <https://html.spec.whatwg.org/multipage/#map-of-preloaded-resources>
209    pub preloaded_resources: PreloadedResources,
210    /// <https://html.spec.whatwg.org/multipage/#concept-settings-object-policy-container>
211    pub policy_container: RequestPolicyContainer,
212    /// <https://html.spec.whatwg.org/multipage/#concept-settings-object-origin>
213    pub origin: Origin,
214    /// <https://html.spec.whatwg.org/multipage/#nested-browsing-context>
215    pub is_nested_browsing_context: bool,
216    /// <https://w3c.github.io/webappsec-upgrade-insecure-requests/#insecure-requests-policy>
217    pub insecure_requests_policy: InsecureRequestsPolicy,
218}
219
220/// <https://html.spec.whatwg.org/multipage/#system-visibility-state>
221#[derive(Clone, Copy, Default, MallocSizeOf, PartialEq)]
222pub enum SystemVisibilityState {
223    #[default]
224    Hidden,
225    Visible,
226}
227
228/// <https://html.spec.whatwg.org/multipage/#traversable-navigable>
229#[derive(Clone, Copy, Default, MallocSizeOf, PartialEq)]
230pub struct TraversableNavigable {
231    /// <https://html.spec.whatwg.org/multipage/#tn-current-session-history-step>
232    current_session_history_step: u8,
233    // TODO: https://html.spec.whatwg.org/multipage/#tn-session-history-entries
234    // TODO: https://html.spec.whatwg.org/multipage/#tn-session-history-traversal-queue
235    /// <https://html.spec.whatwg.org/multipage/#tn-running-nested-apply-history-step>
236    running_nested_apply_history_step: bool,
237    /// <https://html.spec.whatwg.org/multipage/#system-visibility-state>
238    system_visibility_state: SystemVisibilityState,
239    /// <https://html.spec.whatwg.org/multipage/#is-created-by-web-content>
240    is_created_by_web_content: bool,
241}
242
243/// <https://fetch.spec.whatwg.org/#concept-request-window>
244#[derive(Clone, Copy, MallocSizeOf, PartialEq)]
245pub enum TraversableForUserPrompts {
246    NoTraversable,
247    Client,
248    TraversableNavigable(TraversableNavigable),
249}
250
251/// [CORS settings attribute](https://html.spec.whatwg.org/multipage/#attr-crossorigin-anonymous)
252#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
253pub enum CorsSettings {
254    Anonymous,
255    UseCredentials,
256}
257
258impl CorsSettings {
259    /// <https://html.spec.whatwg.org/multipage/#cors-settings-attribute>
260    pub fn from_enumerated_attribute(value: &str) -> CorsSettings {
261        match value.to_ascii_lowercase().as_str() {
262            "anonymous" => CorsSettings::Anonymous,
263            "use-credentials" => CorsSettings::UseCredentials,
264            _ => CorsSettings::Anonymous,
265        }
266    }
267}
268
269/// [Parser Metadata](https://fetch.spec.whatwg.org/#concept-request-parser-metadata)
270#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
271pub enum ParserMetadata {
272    Default,
273    ParserInserted,
274    NotParserInserted,
275}
276
277/// <https://fetch.spec.whatwg.org/#concept-body-source>
278#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
279pub enum BodySource {
280    Null,
281    Object,
282}
283
284/// Messages used to implement <https://fetch.spec.whatwg.org/#concept-request-transmit-body>
285/// which are sent from script to net.
286#[derive(Debug, Deserialize, Serialize)]
287pub enum BodyChunkResponse {
288    /// A chunk of bytes.
289    Chunk(GenericSharedMemory),
290    /// The body is done.
291    Done,
292    /// There was an error streaming the body,
293    /// terminate fetch.
294    Error,
295}
296
297/// Messages used to implement <https://fetch.spec.whatwg.org/#concept-request-transmit-body>
298/// which are sent from net to script
299/// (with the exception of Done, which is sent from script to script).
300#[derive(Debug, Deserialize, Serialize)]
301pub enum BodyChunkRequest {
302    /// Connect a fetch in `net`, with a stream of bytes from `script`.
303    Connect(IpcSender<BodyChunkResponse>),
304    /// Re-extract a new stream from the source, following a redirect.
305    Extract(IpcReceiver<BodyChunkRequest>),
306    /// Ask for another chunk.
307    Chunk,
308    /// Signal the stream is done(sent from script to script).
309    Done,
310    /// Signal the stream has errored(sent from script to script).
311    Error,
312}
313
314/// A process local view into <https://fetch.spec.whatwg.org/#bodies>.
315/// After IPC serialization, each process gets its own shared sender state for the same body
316/// stream. the net side fetch entry points own clearing their local copy once that fetch invocation
317/// reaches its terminal state. Redirect replay can later deserialize a fresh "RequestBody", so
318/// lower level fetch steps cannot always clean up immediately.
319#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
320pub struct RequestBody {
321    /// Net's channel to communicate with script re this body.
322    #[conditional_malloc_size_of]
323    body_chunk_request_channel: Arc<Mutex<Option<IpcSender<BodyChunkRequest>>>>,
324    /// <https://fetch.spec.whatwg.org/#concept-body-source>
325    source: BodySource,
326    /// <https://fetch.spec.whatwg.org/#concept-body-total-bytes>
327    total_bytes: Option<usize>,
328}
329
330impl RequestBody {
331    pub fn new(
332        body_chunk_request_channel: IpcSender<BodyChunkRequest>,
333        source: BodySource,
334        total_bytes: Option<usize>,
335    ) -> Self {
336        RequestBody {
337            body_chunk_request_channel: Arc::new(Mutex::new(Some(body_chunk_request_channel))),
338            source,
339            total_bytes,
340        }
341    }
342
343    /// Step 12 of <https://fetch.spec.whatwg.org/#concept-http-redirect-fetch>
344    pub fn extract_source(&mut self) {
345        match self.source {
346            BodySource::Null => panic!("Null sources should never be re-directed."),
347            BodySource::Object => {
348                let (chan, port) = ipc::channel().unwrap();
349                let mut lock = self.body_chunk_request_channel.lock();
350                let Some(selfchan) = lock.as_mut() else {
351                    error!(
352                        "Could not re-extract the request body source because the body stream has already been closed."
353                    );
354                    return;
355                };
356                if let Err(error) = selfchan.send(BodyChunkRequest::Extract(port)) {
357                    error!(
358                        "Could not re-extract the request body source because the body stream has already been closed: {error}"
359                    );
360                    return;
361                }
362                *selfchan = chan;
363            },
364        }
365    }
366
367    /// This is the current process shared optional sender for requesting body chunks.
368    pub fn clone_stream(&self) -> Arc<Mutex<Option<IpcSender<BodyChunkRequest>>>> {
369        self.body_chunk_request_channel.clone()
370    }
371
372    /// Clears the current process shared sender state for this "RequestBody" copy.
373    ///
374    /// This does not notify or mutate other deserialized "RequestBody" values in other processes.
375    /// Can be called multiple times.
376    pub fn close_stream(&self) {
377        self.body_chunk_request_channel.lock().take();
378    }
379
380    pub fn source_is_null(&self) -> bool {
381        self.source == BodySource::Null
382    }
383
384    #[expect(clippy::len_without_is_empty)]
385    pub fn len(&self) -> Option<usize> {
386        self.total_bytes
387    }
388}
389
390trait RequestBodySize {
391    fn body_length(&self) -> usize;
392}
393
394impl RequestBodySize for Option<RequestBody> {
395    fn body_length(&self) -> usize {
396        self.as_ref()
397            .and_then(|body| body.len())
398            .unwrap_or_default()
399    }
400}
401
402#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
403pub enum InsecureRequestsPolicy {
404    DoNotUpgrade,
405    Upgrade,
406}
407
408pub trait RequestHeadersSize {
409    fn total_size(&self) -> usize;
410}
411
412impl RequestHeadersSize for HeaderMap {
413    fn total_size(&self) -> usize {
414        self.iter()
415            .map(|(name, value)| name.as_str().len() + value.len())
416            .sum()
417    }
418}
419
420#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
421pub struct RequestBuilder {
422    pub id: RequestId,
423
424    pub preload_id: Option<PreloadId>,
425
426    /// <https://fetch.spec.whatwg.org/#concept-request-method>
427    #[serde(
428        deserialize_with = "::hyper_serde::deserialize",
429        serialize_with = "::hyper_serde::serialize"
430    )]
431    pub method: Method,
432
433    /// <https://fetch.spec.whatwg.org/#concept-request-url>
434    pub url: UrlWithBlobClaim,
435
436    /// <https://fetch.spec.whatwg.org/#concept-request-header-list>
437    #[serde(
438        deserialize_with = "::hyper_serde::deserialize",
439        serialize_with = "::hyper_serde::serialize"
440    )]
441    pub headers: HeaderMap,
442
443    /// <https://fetch.spec.whatwg.org/#unsafe-request-flag>
444    pub unsafe_request: bool,
445
446    /// <https://fetch.spec.whatwg.org/#concept-request-body>
447    pub body: Option<RequestBody>,
448
449    /// <https://fetch.spec.whatwg.org/#request-service-workers-mode>
450    pub service_workers_mode: ServiceWorkersMode,
451    pub client: Option<RequestClient>,
452    /// <https://fetch.spec.whatwg.org/#concept-request-destination>
453    pub destination: Destination,
454    pub synchronous: bool,
455    pub mode: RequestMode,
456
457    /// <https://fetch.spec.whatwg.org/#concept-request-cache-mode>
458    pub cache_mode: CacheMode,
459
460    /// <https://fetch.spec.whatwg.org/#use-cors-preflight-flag>
461    pub use_cors_preflight: bool,
462
463    /// <https://fetch.spec.whatwg.org/#request-keepalive-flag>
464    pub keep_alive: bool,
465
466    /// <https://fetch.spec.whatwg.org/#concept-request-credentials-mode>
467    pub credentials_mode: CredentialsMode,
468    pub use_url_credentials: bool,
469
470    /// <https://fetch.spec.whatwg.org/#concept-request-origin>
471    pub origin: Origin,
472
473    /// <https://fetch.spec.whatwg.org/#concept-request-policy-container>
474    pub policy_container: RequestPolicyContainer,
475    pub insecure_requests_policy: InsecureRequestsPolicy,
476    pub has_trustworthy_ancestor_origin: bool,
477
478    /// <https://fetch.spec.whatwg.org/#concept-request-referrer>
479    pub referrer: Referrer,
480
481    /// <https://fetch.spec.whatwg.org/#concept-request-referrer-policy>
482    pub referrer_policy: ReferrerPolicy,
483    pub pipeline_id: Option<PipelineId>,
484    pub target_webview_id: Option<WebViewId>,
485
486    /// <https://fetch.spec.whatwg.org/#concept-request-redirect-mode>
487    pub redirect_mode: RedirectMode,
488
489    /// <https://fetch.spec.whatwg.org/#concept-request-integrity-metadata>
490    pub integrity_metadata: String,
491
492    /// <https://fetch.spec.whatwg.org/#concept-request-nonce-metadata>
493    pub cryptographic_nonce_metadata: String,
494
495    /// <https://fetch.spec.whatwg.org/#concept-request-url-list>
496    pub url_list: Vec<ServoUrl>,
497
498    /// <https://fetch.spec.whatwg.org/#concept-request-parser-metadata>
499    pub parser_metadata: ParserMetadata,
500
501    /// <https://fetch.spec.whatwg.org/#concept-request-initiator>
502    pub initiator: Initiator,
503    pub response_tainting: ResponseTainting,
504    /// Servo internal: if crash details are present, trigger a crash error page with these details.
505    pub crash: Option<String>,
506}
507
508impl RequestBuilder {
509    pub fn new(
510        webview_id: Option<WebViewId>,
511        url: UrlWithBlobClaim,
512        referrer: Referrer,
513    ) -> RequestBuilder {
514        RequestBuilder {
515            id: RequestId::default(),
516            preload_id: None,
517            method: Method::GET,
518            url,
519            headers: HeaderMap::new(),
520            unsafe_request: false,
521            body: None,
522            service_workers_mode: ServiceWorkersMode::All,
523            destination: Destination::None,
524            synchronous: false,
525            mode: RequestMode::NoCors,
526            cache_mode: CacheMode::Default,
527            use_cors_preflight: false,
528            keep_alive: false,
529            credentials_mode: CredentialsMode::CredentialsSameOrigin,
530            use_url_credentials: false,
531            origin: Origin::Client,
532            client: None,
533            policy_container: RequestPolicyContainer::default(),
534            insecure_requests_policy: InsecureRequestsPolicy::DoNotUpgrade,
535            has_trustworthy_ancestor_origin: false,
536            referrer,
537            referrer_policy: ReferrerPolicy::EmptyString,
538            pipeline_id: None,
539            target_webview_id: webview_id,
540            redirect_mode: RedirectMode::Follow,
541            integrity_metadata: "".to_owned(),
542            cryptographic_nonce_metadata: "".to_owned(),
543            url_list: vec![],
544            parser_metadata: ParserMetadata::Default,
545            initiator: Initiator::None,
546            response_tainting: ResponseTainting::Basic,
547            crash: None,
548        }
549    }
550
551    pub fn preload_id(mut self, preload_id: PreloadId) -> RequestBuilder {
552        self.preload_id = Some(preload_id);
553        self
554    }
555
556    /// <https://fetch.spec.whatwg.org/#concept-request-initiator>
557    pub fn initiator(mut self, initiator: Initiator) -> RequestBuilder {
558        self.initiator = initiator;
559        self
560    }
561
562    /// <https://fetch.spec.whatwg.org/#concept-request-method>
563    pub fn method(mut self, method: Method) -> RequestBuilder {
564        self.method = method;
565        self
566    }
567
568    /// <https://fetch.spec.whatwg.org/#concept-request-header-list>
569    pub fn headers(mut self, headers: HeaderMap) -> RequestBuilder {
570        self.headers = headers;
571        self
572    }
573
574    /// <https://fetch.spec.whatwg.org/#unsafe-request-flag>
575    pub fn unsafe_request(mut self, unsafe_request: bool) -> RequestBuilder {
576        self.unsafe_request = unsafe_request;
577        self
578    }
579
580    /// <https://fetch.spec.whatwg.org/#concept-request-body>
581    pub fn body(mut self, body: Option<RequestBody>) -> RequestBuilder {
582        self.body = body;
583        self
584    }
585
586    /// <https://fetch.spec.whatwg.org/#concept-request-destination>
587    pub fn destination(mut self, destination: Destination) -> RequestBuilder {
588        self.destination = destination;
589        self
590    }
591
592    pub fn synchronous(mut self, synchronous: bool) -> RequestBuilder {
593        self.synchronous = synchronous;
594        self
595    }
596
597    pub fn mode(mut self, mode: RequestMode) -> RequestBuilder {
598        self.mode = mode;
599        self
600    }
601
602    /// <https://fetch.spec.whatwg.org/#use-cors-preflight-flag>
603    pub fn use_cors_preflight(mut self, use_cors_preflight: bool) -> RequestBuilder {
604        self.use_cors_preflight = use_cors_preflight;
605        self
606    }
607
608    /// <https://fetch.spec.whatwg.org/#request-keepalive-flag>
609    pub fn keep_alive(mut self, keep_alive: bool) -> RequestBuilder {
610        self.keep_alive = keep_alive;
611        self
612    }
613
614    /// <https://fetch.spec.whatwg.org/#concept-request-credentials-mode>
615    pub fn credentials_mode(mut self, credentials_mode: CredentialsMode) -> RequestBuilder {
616        self.credentials_mode = credentials_mode;
617        self
618    }
619
620    pub fn use_url_credentials(mut self, use_url_credentials: bool) -> RequestBuilder {
621        self.use_url_credentials = use_url_credentials;
622        self
623    }
624
625    /// <https://fetch.spec.whatwg.org/#concept-request-origin>
626    pub fn origin(mut self, origin: ImmutableOrigin) -> RequestBuilder {
627        self.origin = Origin::Origin(origin);
628        self
629    }
630
631    /// <https://fetch.spec.whatwg.org/#concept-request-referrer-policy>
632    pub fn referrer_policy(mut self, referrer_policy: ReferrerPolicy) -> RequestBuilder {
633        self.referrer_policy = referrer_policy;
634        self
635    }
636
637    /// <https://fetch.spec.whatwg.org/#concept-request-url-list>
638    pub fn url_list(mut self, url_list: Vec<ServoUrl>) -> RequestBuilder {
639        self.url_list = url_list;
640        self
641    }
642
643    pub fn pipeline_id(mut self, pipeline_id: Option<PipelineId>) -> RequestBuilder {
644        self.pipeline_id = pipeline_id;
645        self
646    }
647
648    /// <https://fetch.spec.whatwg.org/#concept-request-redirect-mode>
649    pub fn redirect_mode(mut self, redirect_mode: RedirectMode) -> RequestBuilder {
650        self.redirect_mode = redirect_mode;
651        self
652    }
653
654    /// <https://fetch.spec.whatwg.org/#concept-request-integrity-metadata>
655    pub fn integrity_metadata(mut self, integrity_metadata: String) -> RequestBuilder {
656        self.integrity_metadata = integrity_metadata;
657        self
658    }
659
660    /// <https://fetch.spec.whatwg.org/#concept-request-nonce-metadata>
661    pub fn cryptographic_nonce_metadata(mut self, nonce_metadata: String) -> RequestBuilder {
662        self.cryptographic_nonce_metadata = nonce_metadata;
663        self
664    }
665
666    /// <https://fetch.spec.whatwg.org/#concept-request-parser-metadata>
667    pub fn parser_metadata(mut self, parser_metadata: ParserMetadata) -> RequestBuilder {
668        self.parser_metadata = parser_metadata;
669        self
670    }
671
672    pub fn response_tainting(mut self, response_tainting: ResponseTainting) -> RequestBuilder {
673        self.response_tainting = response_tainting;
674        self
675    }
676
677    pub fn crash(mut self, crash: Option<String>) -> Self {
678        self.crash = crash;
679        self
680    }
681
682    /// <https://fetch.spec.whatwg.org/#concept-request-policy-container>
683    pub fn policy_container(mut self, policy_container: PolicyContainer) -> RequestBuilder {
684        self.policy_container = RequestPolicyContainer::PolicyContainer(policy_container);
685        self
686    }
687
688    /// <https://fetch.spec.whatwg.org/#concept-request-client>
689    pub fn client(mut self, client: RequestClient) -> RequestBuilder {
690        self.client = Some(client);
691        self
692    }
693
694    pub fn insecure_requests_policy(
695        mut self,
696        insecure_requests_policy: InsecureRequestsPolicy,
697    ) -> RequestBuilder {
698        self.insecure_requests_policy = insecure_requests_policy;
699        self
700    }
701
702    pub fn has_trustworthy_ancestor_origin(
703        mut self,
704        has_trustworthy_ancestor_origin: bool,
705    ) -> RequestBuilder {
706        self.has_trustworthy_ancestor_origin = has_trustworthy_ancestor_origin;
707        self
708    }
709
710    /// <https://fetch.spec.whatwg.org/#request-service-workers-mode>
711    pub fn service_workers_mode(
712        mut self,
713        service_workers_mode: ServiceWorkersMode,
714    ) -> RequestBuilder {
715        self.service_workers_mode = service_workers_mode;
716        self
717    }
718
719    /// <https://fetch.spec.whatwg.org/#concept-request-cache-mode>
720    pub fn cache_mode(mut self, cache_mode: CacheMode) -> RequestBuilder {
721        self.cache_mode = cache_mode;
722        self
723    }
724
725    pub fn build(self) -> Request {
726        let mut request = Request::new(
727            self.id,
728            self.url.clone(),
729            Some(self.origin),
730            self.referrer,
731            self.pipeline_id,
732            self.target_webview_id,
733        );
734        request.preload_id = self.preload_id;
735        request.initiator = self.initiator;
736        request.method = self.method;
737        request.headers = self.headers;
738        request.unsafe_request = self.unsafe_request;
739        request.body = self.body;
740        request.service_workers_mode = self.service_workers_mode;
741        request.destination = self.destination;
742        request.synchronous = self.synchronous;
743        request.mode = self.mode;
744        request.use_cors_preflight = self.use_cors_preflight;
745        request.keep_alive = self.keep_alive;
746        request.credentials_mode = self.credentials_mode;
747        request.use_url_credentials = self.use_url_credentials;
748        request.cache_mode = self.cache_mode;
749        request.referrer_policy = self.referrer_policy;
750        request.redirect_mode = self.redirect_mode;
751        let mut url_list: Vec<_> = self
752            .url_list
753            .into_iter()
754            .map(UrlWithBlobClaim::from_url_without_having_claimed_blob)
755            .collect();
756        if url_list.is_empty() {
757            url_list.push(self.url);
758        }
759        request.redirect_count = url_list.len() as u32 - 1;
760        request.url_list = url_list;
761        request.integrity_metadata = self.integrity_metadata;
762        request.cryptographic_nonce_metadata = self.cryptographic_nonce_metadata;
763        request.parser_metadata = self.parser_metadata;
764        request.response_tainting = self.response_tainting;
765        request.crash = self.crash;
766        request.client = self.client;
767        request.policy_container = self.policy_container;
768        request.insecure_requests_policy = self.insecure_requests_policy;
769        request.has_trustworthy_ancestor_origin = self.has_trustworthy_ancestor_origin;
770        request
771    }
772
773    /// The body length for a keep-alive request. Is 0 if this request is not keep-alive
774    pub fn keep_alive_body_length(&self) -> u64 {
775        assert!(self.keep_alive);
776        self.body.body_length() as u64
777    }
778}
779
780/// A [Request](https://fetch.spec.whatwg.org/#concept-request) as defined by
781/// the Fetch spec.
782#[derive(Clone, MallocSizeOf)]
783pub struct Request {
784    /// The unique id of this request so that the task that triggered it can route
785    /// messages to the correct listeners. This is a UUID that is generated when a request
786    /// is being built.
787    pub id: RequestId,
788    pub preload_id: Option<PreloadId>,
789    /// <https://fetch.spec.whatwg.org/#concept-request-method>
790    pub method: Method,
791    /// <https://fetch.spec.whatwg.org/#local-urls-only-flag>
792    pub local_urls_only: bool,
793    /// <https://fetch.spec.whatwg.org/#concept-request-header-list>
794    pub headers: HeaderMap,
795    /// <https://fetch.spec.whatwg.org/#unsafe-request-flag>
796    pub unsafe_request: bool,
797    /// <https://fetch.spec.whatwg.org/#concept-request-body>
798    pub body: Option<RequestBody>,
799    /// <https://fetch.spec.whatwg.org/#concept-request-client>
800    pub client: Option<RequestClient>,
801    /// <https://fetch.spec.whatwg.org/#concept-request-window>
802    pub traversable_for_user_prompts: TraversableForUserPrompts,
803    pub target_webview_id: Option<WebViewId>,
804    /// <https://fetch.spec.whatwg.org/#request-keepalive-flag>
805    pub keep_alive: bool,
806    /// <https://fetch.spec.whatwg.org/#request-service-workers-mode>
807    pub service_workers_mode: ServiceWorkersMode,
808    /// <https://fetch.spec.whatwg.org/#concept-request-initiator>
809    pub initiator: Initiator,
810    /// <https://fetch.spec.whatwg.org/#concept-request-destination>
811    pub destination: Destination,
812    // TODO: priority object
813    /// <https://fetch.spec.whatwg.org/#concept-request-origin>
814    pub origin: Origin,
815    /// <https://fetch.spec.whatwg.org/#concept-request-referrer>
816    pub referrer: Referrer,
817    /// <https://fetch.spec.whatwg.org/#concept-request-referrer-policy>
818    pub referrer_policy: ReferrerPolicy,
819    pub pipeline_id: Option<PipelineId>,
820    /// <https://fetch.spec.whatwg.org/#synchronous-flag>
821    pub synchronous: bool,
822    /// <https://fetch.spec.whatwg.org/#concept-request-mode>
823    pub mode: RequestMode,
824    /// <https://fetch.spec.whatwg.org/#use-cors-preflight-flag>
825    pub use_cors_preflight: bool,
826    /// <https://fetch.spec.whatwg.org/#concept-request-credentials-mode>
827    pub credentials_mode: CredentialsMode,
828    /// <https://fetch.spec.whatwg.org/#concept-request-use-url-credentials-flag>
829    pub use_url_credentials: bool,
830    /// <https://fetch.spec.whatwg.org/#concept-request-cache-mode>
831    pub cache_mode: CacheMode,
832    /// <https://fetch.spec.whatwg.org/#concept-request-redirect-mode>
833    pub redirect_mode: RedirectMode,
834    /// <https://fetch.spec.whatwg.org/#concept-request-integrity-metadata>
835    pub integrity_metadata: String,
836    /// <https://fetch.spec.whatwg.org/#concept-request-nonce-metadata>
837    pub cryptographic_nonce_metadata: String,
838    /// <https://fetch.spec.whatwg.org/#concept-request-url-list>
839    pub url_list: Vec<UrlWithBlobClaim>,
840    /// <https://fetch.spec.whatwg.org/#concept-request-redirect-count>
841    pub redirect_count: u32,
842    /// <https://fetch.spec.whatwg.org/#concept-request-response-tainting>
843    pub response_tainting: ResponseTainting,
844    /// <https://fetch.spec.whatwg.org/#concept-request-parser-metadata>
845    pub parser_metadata: ParserMetadata,
846    /// <https://fetch.spec.whatwg.org/#concept-request-policy-container>
847    pub policy_container: RequestPolicyContainer,
848    /// <https://w3c.github.io/webappsec-upgrade-insecure-requests/#insecure-requests-policy>
849    pub insecure_requests_policy: InsecureRequestsPolicy,
850    pub has_trustworthy_ancestor_origin: bool,
851    /// Servo internal: if crash details are present, trigger a crash error page with these details.
852    pub crash: Option<String>,
853}
854
855impl Request {
856    pub fn new(
857        id: RequestId,
858        url: UrlWithBlobClaim,
859        origin: Option<Origin>,
860        referrer: Referrer,
861        pipeline_id: Option<PipelineId>,
862        webview_id: Option<WebViewId>,
863    ) -> Request {
864        Request {
865            id,
866            preload_id: None,
867            method: Method::GET,
868            local_urls_only: false,
869            headers: HeaderMap::new(),
870            unsafe_request: false,
871            body: None,
872            client: None,
873            traversable_for_user_prompts: TraversableForUserPrompts::Client,
874            keep_alive: false,
875            service_workers_mode: ServiceWorkersMode::All,
876            initiator: Initiator::None,
877            destination: Destination::None,
878            origin: origin.unwrap_or(Origin::Client),
879            referrer,
880            referrer_policy: ReferrerPolicy::EmptyString,
881            pipeline_id,
882            target_webview_id: webview_id,
883            synchronous: false,
884            mode: RequestMode::NoCors,
885            use_cors_preflight: false,
886            credentials_mode: CredentialsMode::CredentialsSameOrigin,
887            use_url_credentials: false,
888            cache_mode: CacheMode::Default,
889            redirect_mode: RedirectMode::Follow,
890            integrity_metadata: String::new(),
891            cryptographic_nonce_metadata: String::new(),
892            url_list: vec![url],
893            parser_metadata: ParserMetadata::Default,
894            redirect_count: 0,
895            response_tainting: ResponseTainting::Basic,
896            policy_container: RequestPolicyContainer::Client,
897            insecure_requests_policy: InsecureRequestsPolicy::DoNotUpgrade,
898            has_trustworthy_ancestor_origin: false,
899            crash: None,
900        }
901    }
902
903    /// <https://fetch.spec.whatwg.org/#concept-request-url>
904    pub fn url(&self) -> ServoUrl {
905        self.url_list.first().unwrap().url()
906    }
907
908    pub fn url_with_blob_claim(&self) -> UrlWithBlobClaim {
909        self.url_list.first().unwrap().clone()
910    }
911
912    pub fn original_url(&self) -> ServoUrl {
913        match self.mode {
914            RequestMode::WebSocket {
915                protocols: _,
916                ref original_url,
917            } => original_url.clone(),
918            _ => self.url(),
919        }
920    }
921
922    /// <https://fetch.spec.whatwg.org/#concept-request-current-url>
923    pub fn current_url(&self) -> ServoUrl {
924        self.current_url_with_blob_claim().url()
925    }
926
927    /// <https://fetch.spec.whatwg.org/#concept-request-current-url>
928    pub fn current_url_with_blob_claim(&self) -> UrlWithBlobClaim {
929        self.url_list.last().unwrap().clone()
930    }
931
932    /// <https://fetch.spec.whatwg.org/#concept-request-current-url>
933    pub fn current_url_mut(&mut self) -> &mut ServoUrl {
934        self.url_list.last_mut().unwrap()
935    }
936
937    /// <https://fetch.spec.whatwg.org/#navigation-request>
938    pub fn is_navigation_request(&self) -> bool {
939        matches!(
940            self.destination,
941            Destination::Document |
942                Destination::Embed |
943                Destination::Frame |
944                Destination::IFrame |
945                Destination::Object
946        )
947    }
948
949    /// <https://fetch.spec.whatwg.org/#subresource-request>
950    pub fn is_subresource_request(&self) -> bool {
951        matches!(
952            self.destination,
953            Destination::Audio |
954                Destination::Font |
955                Destination::Image |
956                Destination::Manifest |
957                Destination::Script |
958                Destination::Style |
959                Destination::Track |
960                Destination::Video |
961                Destination::Xslt |
962                Destination::None
963        )
964    }
965
966    pub fn timing_type(&self) -> ResourceTimingType {
967        if self.is_navigation_request() {
968            ResourceTimingType::Navigation
969        } else {
970            ResourceTimingType::Resource
971        }
972    }
973
974    /// <https://fetch.spec.whatwg.org/#populate-request-from-client>
975    pub fn populate_request_from_client(&mut self) {
976        // Step 1. If request’s traversable for user prompts is "client":
977        if self.traversable_for_user_prompts == TraversableForUserPrompts::Client {
978            // Step 1.1. Set request’s traversable for user prompts to "no-traversable".
979            self.traversable_for_user_prompts = TraversableForUserPrompts::NoTraversable;
980            // Step 1.2. If request’s client is non-null:
981            if self.client.is_some() {
982                // Step 1.2.1. Let global be request’s client’s global object.
983                // TODO
984                // Step 1.2.2. If global is a Window object and global’s navigable is not null,
985                // then set request’s traversable for user prompts to global’s navigable’s traversable navigable.
986                self.traversable_for_user_prompts =
987                    TraversableForUserPrompts::TraversableNavigable(Default::default());
988            }
989        }
990        // Step 2. If request’s origin is "client":
991        if self.origin == Origin::Client {
992            let Some(client) = self.client.as_ref() else {
993                // Step 2.1. Assert: request’s client is non-null.
994                unreachable!();
995            };
996            // Step 2.2. Set request’s origin to request’s client’s origin.
997            self.origin = client.origin.clone();
998        }
999        // Step 3. If request’s policy container is "client":
1000        if matches!(self.policy_container, RequestPolicyContainer::Client) {
1001            // Step 3.1. If request’s client is non-null, then set request’s
1002            // policy container to a clone of request’s client’s policy container. [HTML]
1003            if let Some(client) = self.client.as_ref() {
1004                self.policy_container = client.policy_container.clone();
1005            } else {
1006                // Step 3.2. Otherwise, set request’s policy container to a new policy container.
1007                self.policy_container =
1008                    RequestPolicyContainer::PolicyContainer(PolicyContainer::default());
1009            }
1010        }
1011    }
1012
1013    /// The body length for a keep-alive request. Is 0 if this request is not keep-alive
1014    pub fn keep_alive_body_length(&self) -> u64 {
1015        assert!(self.keep_alive);
1016        self.body.body_length() as u64
1017    }
1018
1019    /// <https://fetch.spec.whatwg.org/#total-request-length>
1020    pub fn total_request_length(&self) -> usize {
1021        // Step 1. Let totalRequestLength be the length of request’s URL, serialized with exclude fragment set to true.
1022        let mut total_request_length = self.url()[..Position::AfterQuery].len();
1023        // Step 2. Increment totalRequestLength by the length of request’s referrer, serialized.
1024        total_request_length += self
1025            .referrer
1026            .to_url()
1027            .map(|url| url.as_str().len())
1028            .unwrap_or_default();
1029        // Step 3. For each (name, value) of request’s header list, increment totalRequestLength
1030        // by name’s length + value’s length.
1031        total_request_length += self.headers.total_size();
1032        // Step 4. Increment totalRequestLength by request’s body’s length.
1033        total_request_length += self.body.body_length();
1034        // Step 5. Return totalRequestLength.
1035        total_request_length
1036    }
1037
1038    /// <https://fetch.spec.whatwg.org/#concept-request-tainted-origin>
1039    pub fn redirect_taint_for_request(&self) -> RedirectTaint {
1040        // Step 1. Assert: request’s origin is not "client".
1041        let Origin::Origin(request_origin) = &self.origin else {
1042            unreachable!("origin cannot be \"client\" at this point in time");
1043        };
1044
1045        // Step 2. Let lastURL be null.
1046        let mut last_url = None;
1047
1048        // Step 3. Let taint be "same-origin".
1049        let mut taint = RedirectTaint::SameOrigin;
1050
1051        // Step 4. For each url of request’s URL list:
1052        for url in &self.url_list {
1053            // Step 4.1 If lastURL is null, then set lastURL to url and continue.
1054            let Some(last_url) = &mut last_url else {
1055                last_url = Some(url);
1056                continue;
1057            };
1058
1059            // Step 4.2. If url’s origin is not same site with lastURL’s origin and
1060            // request’s origin is not same site with lastURL’s origin, then return "cross-site".
1061            if !is_same_site(&url.origin(), &last_url.origin()) &&
1062                !is_same_site(request_origin, &last_url.origin())
1063            {
1064                return RedirectTaint::CrossSite;
1065            }
1066
1067            // Step 4.3. If url’s origin is not same origin with lastURL’s origin
1068            // and request’s origin is not same origin with lastURL’s origin, then set taint to "same-site".
1069            if url.origin() != last_url.origin() && *request_origin != last_url.origin() {
1070                taint = RedirectTaint::SameSite;
1071            }
1072
1073            // Step 4.4 Set lastURL to url.
1074            *last_url = url;
1075        }
1076
1077        // Step 5. Return taint.
1078        taint
1079    }
1080}
1081
1082impl Referrer {
1083    pub fn to_url(&self) -> Option<&ServoUrl> {
1084        match *self {
1085            Referrer::NoReferrer => None,
1086            Referrer::Client(ref url) => Some(url),
1087            Referrer::ReferrerUrl(ref url) => Some(url),
1088        }
1089    }
1090}
1091
1092// https://fetch.spec.whatwg.org/#cors-unsafe-request-header-byte
1093// TODO: values in the control-code range are being quietly stripped out by
1094// HeaderMap and never reach this function to be loudly rejected!
1095fn is_cors_unsafe_request_header_byte(value: &u8) -> bool {
1096    matches!(value,
1097        0x00..=0x08 |
1098        0x10..=0x19 |
1099        0x22 |
1100        0x28 |
1101        0x29 |
1102        0x3A |
1103        0x3C |
1104        0x3E |
1105        0x3F |
1106        0x40 |
1107        0x5B |
1108        0x5C |
1109        0x5D |
1110        0x7B |
1111        0x7D |
1112        0x7F
1113    )
1114}
1115
1116// https://fetch.spec.whatwg.org/#cors-safelisted-request-header
1117// subclause `accept`
1118fn is_cors_safelisted_request_accept(value: &[u8]) -> bool {
1119    !(value.iter().any(is_cors_unsafe_request_header_byte))
1120}
1121
1122// https://fetch.spec.whatwg.org/#cors-safelisted-request-header
1123// subclauses `accept-language`, `content-language`
1124fn is_cors_safelisted_language(value: &[u8]) -> bool {
1125    value.iter().all(|&x| {
1126        matches!(x,
1127            0x30..=0x39 |
1128            0x41..=0x5A |
1129            0x61..=0x7A |
1130            0x20 |
1131            0x2A |
1132            0x2C |
1133            0x2D |
1134            0x2E |
1135            0x3B |
1136            0x3D
1137        )
1138    })
1139}
1140
1141// https://fetch.spec.whatwg.org/#cors-safelisted-request-header
1142// subclause `content-type`
1143pub fn is_cors_safelisted_request_content_type(value: &[u8]) -> bool {
1144    // step 1
1145    if value.iter().any(is_cors_unsafe_request_header_byte) {
1146        return false;
1147    }
1148    // step 2
1149    let value_string = if let Ok(s) = std::str::from_utf8(value) {
1150        s
1151    } else {
1152        return false;
1153    };
1154    let value_mime_result: Result<Mime, _> = value_string.parse();
1155    match value_mime_result {
1156        Err(_) => false, // step 3
1157        Ok(value_mime) => match (value_mime.type_(), value_mime.subtype()) {
1158            (mime::APPLICATION, mime::WWW_FORM_URLENCODED) |
1159            (mime::MULTIPART, mime::FORM_DATA) |
1160            (mime::TEXT, mime::PLAIN) => true,
1161            _ => false, // step 4
1162        },
1163    }
1164}
1165
1166// TODO: "DPR", "Downlink", "Save-Data", "Viewport-Width", "Width":
1167// ... once parsed, the value should not be failure.
1168// https://fetch.spec.whatwg.org/#cors-safelisted-request-header
1169pub fn is_cors_safelisted_request_header<N: AsRef<str>, V: AsRef<[u8]>>(
1170    name: &N,
1171    value: &V,
1172) -> bool {
1173    let name: &str = name.as_ref();
1174    let value: &[u8] = value.as_ref();
1175    if value.len() > 128 {
1176        return false;
1177    }
1178    match name {
1179        "accept" => is_cors_safelisted_request_accept(value),
1180        "accept-language" | "content-language" => is_cors_safelisted_language(value),
1181        "content-type" => is_cors_safelisted_request_content_type(value),
1182        "range" => is_cors_safelisted_request_range(value),
1183        _ => false,
1184    }
1185}
1186
1187pub fn is_cors_safelisted_request_range(value: &[u8]) -> bool {
1188    if let Ok(value_str) = std::str::from_utf8(value) {
1189        return validate_range_header(value_str);
1190    }
1191    false
1192}
1193
1194fn validate_range_header(value: &str) -> bool {
1195    let trimmed = value.trim();
1196    if !trimmed.starts_with("bytes=") {
1197        return false;
1198    }
1199
1200    if let Some(range) = trimmed.strip_prefix("bytes=") {
1201        let mut parts = range.split('-');
1202        let start = parts.next();
1203        let end = parts.next();
1204
1205        if let Some(start) = start &&
1206            let Ok(start_num) = start.parse::<u64>()
1207        {
1208            return match end {
1209                Some(e) if !e.is_empty() => {
1210                    e.parse::<u64>().is_ok_and(|end_num| start_num <= end_num)
1211                },
1212                _ => true,
1213            };
1214        }
1215    }
1216    false
1217}
1218
1219/// <https://fetch.spec.whatwg.org/#cors-safelisted-method>
1220pub fn is_cors_safelisted_method(method: &Method) -> bool {
1221    matches!(*method, Method::GET | Method::HEAD | Method::POST)
1222}
1223
1224/// <https://fetch.spec.whatwg.org/#cors-non-wildcard-request-header-name>
1225pub fn is_cors_non_wildcard_request_header_name(name: &HeaderName) -> bool {
1226    name == AUTHORIZATION
1227}
1228
1229/// <https://fetch.spec.whatwg.org/#cors-unsafe-request-header-names>
1230pub fn get_cors_unsafe_header_names(headers: &HeaderMap) -> Vec<HeaderName> {
1231    // Step 1
1232    let mut unsafe_names: Vec<&HeaderName> = vec![];
1233    // Step 2
1234    let mut potentillay_unsafe_names: Vec<&HeaderName> = vec![];
1235    // Step 3
1236    let mut safelist_value_size = 0;
1237
1238    // Step 4
1239    for (name, value) in headers.iter() {
1240        if !is_cors_safelisted_request_header(&name, &value) {
1241            unsafe_names.push(name);
1242        } else {
1243            potentillay_unsafe_names.push(name);
1244            safelist_value_size += value.as_ref().len();
1245        }
1246    }
1247
1248    // Step 5
1249    if safelist_value_size > 1024 {
1250        unsafe_names.extend_from_slice(&potentillay_unsafe_names);
1251    }
1252
1253    // Step 6
1254    convert_header_names_to_sorted_lowercase_set(unsafe_names)
1255}
1256
1257/// <https://fetch.spec.whatwg.org/#ref-for-convert-header-names-to-a-sorted-lowercase-set>
1258pub fn convert_header_names_to_sorted_lowercase_set(
1259    header_names: Vec<&HeaderName>,
1260) -> Vec<HeaderName> {
1261    // HeaderName does not implement the needed traits to use a BTreeSet
1262    // So create a new Vec, sort, then dedup
1263    let mut ordered_set = header_names.to_vec();
1264    ordered_set.sort_by(|a, b| a.as_str().partial_cmp(b.as_str()).unwrap());
1265    ordered_set.dedup();
1266    ordered_set.into_iter().cloned().collect()
1267}
1268
1269pub fn create_request_body_with_content(content: &str) -> RequestBody {
1270    let content_bytes = GenericSharedMemory::from_bytes(content.as_bytes());
1271    let content_len = content_bytes.len();
1272
1273    let (chunk_request_sender, chunk_request_receiver) = ipc::channel().unwrap();
1274    ROUTER.add_typed_route(
1275        chunk_request_receiver,
1276        Box::new(move |message| {
1277            let request = message.unwrap();
1278            if let BodyChunkRequest::Connect(sender) = request {
1279                let _ = sender.send(BodyChunkResponse::Chunk(content_bytes.clone()));
1280                let _ = sender.send(BodyChunkResponse::Done);
1281            }
1282        }),
1283    );
1284
1285    RequestBody::new(chunk_request_sender, BodySource::Object, Some(content_len))
1286}