Skip to main content

script/
body.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::io::Cursor;
6use std::rc::Rc;
7use std::{fs, ptr, slice, str};
8
9use encoding_rs::{Encoding, UTF_8};
10use http::HeaderMap;
11use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
12use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
13use ipc_channel::router::ROUTER;
14use js::jsapi::{Heap, JSObject, Value as JSValue};
15use js::jsval::{JSVal, UndefinedValue};
16use js::realm::CurrentRealm;
17use js::rust::HandleValue;
18use js::rust::wrappers2::{JS_ClearPendingException, JS_GetPendingException, JS_ParseJSON};
19use js::typedarray::{ArrayBufferU8, Uint8};
20use mime::{self, Mime};
21use net_traits::request::{
22    BodyChunkRequest, BodyChunkResponse, BodySource as NetBodySource, RequestBody,
23};
24use script_bindings::reflector::DomObject;
25use servo_base::generic_channel::GenericSharedMemory;
26use servo_constellation_traits::BlobImpl;
27use url::form_urlencoded;
28
29use crate::dom::bindings::buffer_source::create_buffer_source;
30use crate::dom::bindings::codegen::Bindings::BlobBinding::Blob_Binding::BlobMethods;
31use crate::dom::bindings::codegen::Bindings::FormDataBinding::FormDataMethods;
32use crate::dom::bindings::codegen::Bindings::XMLHttpRequestBinding::BodyInit;
33use crate::dom::bindings::error::{Error, Fallible};
34use crate::dom::bindings::inheritance::Castable;
35use crate::dom::bindings::refcounted::Trusted;
36use crate::dom::bindings::reflector::DomGlobal;
37use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
38use crate::dom::bindings::str::{DOMString, USVString};
39use crate::dom::bindings::trace::RootedTraceableBox;
40use crate::dom::blob::{Blob, normalize_type_string};
41use crate::dom::file::File;
42use crate::dom::formdata::FormData;
43use crate::dom::globalscope::GlobalScope;
44use crate::dom::html::htmlformelement::{encode_multipart_form_data, generate_boundary};
45use crate::dom::promise::Promise;
46use crate::dom::promisenativehandler::{Callback, PromiseNativeHandler};
47use crate::dom::readablestream::{ReadableStream, get_read_promise_bytes, get_read_promise_done};
48use crate::dom::urlsearchparams::URLSearchParams;
49use crate::mime_multipart::{Node, read_multipart_body};
50use crate::realms::{InRealm, enter_auto_realm};
51use crate::script_runtime::CanGc;
52use crate::task_source::SendableTaskSource;
53
54/// <https://fetch.spec.whatwg.org/#concept-body-clone>
55pub(crate) fn clone_body_stream_for_dom_body(
56    cx: &mut js::context::JSContext,
57    original_body_stream: &MutNullableDom<ReadableStream>,
58    cloned_body_stream: &MutNullableDom<ReadableStream>,
59) -> Fallible<()> {
60    // To clone a body *body*, run these steps:
61
62    let Some(stream) = original_body_stream.get() else {
63        return Ok(());
64    };
65
66    // step 1. Let « out1, out2 » be the result of teeing body’s stream.
67    let branches = stream.tee(cx, true)?;
68    let out1 = &*branches[0];
69    let out2 = &*branches[1];
70
71    // step 2. Set body’s stream to out1.
72    // step 3. Return a body whose stream is out2 and other members are copied from body.
73    original_body_stream.set(Some(out1));
74    cloned_body_stream.set(Some(out2));
75
76    Ok(())
77}
78
79/// The Dom object, or ReadableStream, that is the source of a body.
80/// <https://fetch.spec.whatwg.org/#concept-body-source>
81#[derive(Clone, PartialEq)]
82pub(crate) enum BodySource {
83    /// A ReadableStream comes with a null-source.
84    Null,
85    /// Another Dom object as source,
86    /// TODO: store the actual object
87    /// and re-extract a stream on re-direct.
88    Object,
89}
90
91/// The reason to stop reading from the body.
92enum StopReading {
93    /// The stream has errored.
94    Error,
95    /// The stream is done.
96    Done,
97}
98
99/// The IPC route handler
100/// for <https://fetch.spec.whatwg.org/#concept-request-transmit-body>.
101/// This route runs in the script process,
102/// and will queue tasks to perform operations
103/// on the stream and transmit body chunks over IPC.
104#[derive(Clone)]
105struct TransmitBodyConnectHandler {
106    stream: Trusted<ReadableStream>,
107    task_source: SendableTaskSource,
108    bytes_sender: Option<IpcSender<BodyChunkResponse>>,
109    control_sender: Option<IpcSender<BodyChunkRequest>>,
110    in_memory: Option<GenericSharedMemory>,
111    in_memory_done: bool,
112    source: BodySource,
113}
114
115impl TransmitBodyConnectHandler {
116    pub(crate) fn new(
117        stream: Trusted<ReadableStream>,
118        task_source: SendableTaskSource,
119        control_sender: IpcSender<BodyChunkRequest>,
120        in_memory: Option<GenericSharedMemory>,
121        source: BodySource,
122    ) -> TransmitBodyConnectHandler {
123        TransmitBodyConnectHandler {
124            stream,
125            task_source,
126            bytes_sender: None,
127            control_sender: Some(control_sender),
128            in_memory,
129            in_memory_done: false,
130            source,
131        }
132    }
133
134    /// Reset `in_memory_done`, called when a stream is
135    /// re-extracted from the source to support a re-direct.
136    pub(crate) fn reset_in_memory_done(&mut self) {
137        self.in_memory_done = false;
138    }
139
140    /// Re-extract the source to support streaming it again for a re-direct.
141    /// TODO: actually re-extract the source, instead of just cloning data, to support Blob.
142    fn re_extract(&mut self, chunk_request_receiver: IpcReceiver<BodyChunkRequest>) {
143        let mut body_handler = self.clone();
144        body_handler.reset_in_memory_done();
145
146        ROUTER.add_typed_route(
147            chunk_request_receiver,
148            Box::new(move |message| {
149                let request = message.unwrap();
150                match request {
151                    BodyChunkRequest::Connect(sender) => {
152                        body_handler.start_reading(sender);
153                    },
154                    BodyChunkRequest::Extract(receiver) => {
155                        body_handler.re_extract(receiver);
156                    },
157                    BodyChunkRequest::Chunk => body_handler.transmit_source(),
158                    // Note: this is actually sent from this process
159                    // by the TransmitBodyPromiseHandler when reading stops.
160                    BodyChunkRequest::Done => {
161                        body_handler.stop_reading(StopReading::Done);
162                    },
163                    // Note: this is actually sent from this process
164                    // by the TransmitBodyPromiseHandler when the stream errors.
165                    BodyChunkRequest::Error => {
166                        body_handler.stop_reading(StopReading::Error);
167                    },
168                }
169            }),
170        );
171    }
172
173    /// In case of re-direct, and of a source available in memory,
174    /// send it all in one chunk.
175    ///
176    /// TODO: this method should be deprecated
177    /// in favor of making `re_extract` actually re-extract a stream from the source.
178    /// See #26686
179    fn transmit_source(&mut self) {
180        if self.in_memory_done {
181            // Step 5.1.3
182            self.stop_reading(StopReading::Done);
183            return;
184        }
185
186        if let BodySource::Null = self.source {
187            panic!("ReadableStream(Null) sources should not re-direct.");
188        }
189
190        if let Some(bytes) = self.in_memory.clone() {
191            // The memoized bytes are sent so we mark it as done again
192            self.in_memory_done = true;
193            let _ = self
194                .bytes_sender
195                .as_ref()
196                .expect("No bytes sender to transmit source.")
197                .send(BodyChunkResponse::Chunk(bytes));
198            return;
199        }
200        warn!("Re-directs for file-based Blobs not supported yet.");
201    }
202
203    /// Take the IPC sender sent by `net`, so we can send body chunks with it.
204    /// Also the entry point to <https://fetch.spec.whatwg.org/#concept-request-transmit-body>
205    fn start_reading(&mut self, sender: IpcSender<BodyChunkResponse>) {
206        self.bytes_sender = Some(sender);
207
208        // If we're using an actual ReadableStream, acquire a reader for it.
209        if self.source == BodySource::Null {
210            let stream = self.stream.clone();
211            self.task_source
212                .queue(task!(start_reading_request_body_stream: move |cx| {
213                    // Step 1, Let body be request’s body.
214                    let rooted_stream = stream.root();
215
216                    // TODO: Step 2, If body is null.
217
218                    // Step 3, get a reader for stream.
219                    rooted_stream.acquire_default_reader(CanGc::from_cx(cx))
220                        .expect("Couldn't acquire a reader for the body stream.");
221
222                    // Note: this algorithm continues when the first chunk is requested by `net`.
223                }));
224        }
225    }
226
227    /// Drop the IPC sender sent by `net`
228    /// It is important to drop the control_sender as this will allow us to clean ourselves up.
229    /// Otherwise, the following cycle will happen: The control sender is owned by us which keeps the control receiver
230    /// alive in the router which keeps us alive.
231    fn stop_reading(&mut self, reason: StopReading) {
232        let bytes_sender = self
233            .bytes_sender
234            .take()
235            .expect("Stop reading called multiple times on TransmitBodyConnectHandler.");
236        match reason {
237            StopReading::Error => {
238                let _ = bytes_sender.send(BodyChunkResponse::Error);
239            },
240            StopReading::Done => {
241                let _ = bytes_sender.send(BodyChunkResponse::Done);
242            },
243        }
244        let _ = self.control_sender.take();
245    }
246
247    /// Step 4 and following of <https://fetch.spec.whatwg.org/#concept-request-transmit-body>
248    fn transmit_body_chunk(&mut self) {
249        if self.in_memory_done {
250            // Step 5.1.3
251            self.stop_reading(StopReading::Done);
252            return;
253        }
254
255        let stream = self.stream.clone();
256        let control_sender = self.control_sender.clone();
257        let bytes_sender = self
258            .bytes_sender
259            .clone()
260            .expect("No bytes sender to transmit chunk.");
261
262        // In case of the data being in-memory, send everything in one chunk, by-passing SpiderMonkey.
263        if let Some(bytes) = self.in_memory.clone() {
264            let _ = bytes_sender.send(BodyChunkResponse::Chunk(bytes));
265            // Mark this body as `done` so that we can stop reading in the next tick,
266            // matching the behavior of the promise-based flow
267            self.in_memory_done = true;
268            return;
269        }
270
271        self.task_source.queue(
272            task!(setup_native_body_promise_handler: move |cx| {
273                let rooted_stream = stream.root();
274                let global = rooted_stream.global();
275
276                // Step 4, the result of reading a chunk from body’s stream with reader.
277                let promise = rooted_stream.read_a_chunk(cx);
278
279                // Step 5, the parallel steps waiting for and handling the result of the read promise,
280                // are a combination of the promise native handler here,
281                // and the corresponding IPC route in `component::net::http_loader`.
282                rooted!(&in(cx) let mut promise_handler = Some(TransmitBodyPromiseHandler {
283                    bytes_sender: bytes_sender.clone(),
284                    stream: Dom::from_ref(&rooted_stream),
285                    control_sender: control_sender.clone().unwrap(),
286                }));
287
288                rooted!(&in(cx) let mut rejection_handler = Some(TransmitBodyPromiseRejectionHandler {
289                    bytes_sender,
290                    stream: Dom::from_ref(&rooted_stream),
291                    control_sender: control_sender.unwrap(),
292                }));
293
294                let handler =
295                    PromiseNativeHandler::new(&global, promise_handler.take().map(|h| Box::new(h) as Box<_>), rejection_handler.take().map(|h| Box::new(h) as Box<_>), CanGc::from_cx(cx));
296
297                let mut realm = enter_auto_realm(cx, &*global);
298                let realm = &mut realm.current_realm();
299                promise.append_native_handler(realm, &handler);
300            })
301        );
302    }
303}
304
305/// The handler of read promises of body streams used in
306/// <https://fetch.spec.whatwg.org/#concept-request-transmit-body>.
307#[derive(Clone, JSTraceable, MallocSizeOf)]
308#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
309struct TransmitBodyPromiseHandler {
310    #[no_trace]
311    bytes_sender: IpcSender<BodyChunkResponse>,
312    stream: Dom<ReadableStream>,
313    #[no_trace]
314    control_sender: IpcSender<BodyChunkRequest>,
315}
316
317impl js::gc::Rootable for TransmitBodyPromiseHandler {}
318
319impl Callback for TransmitBodyPromiseHandler {
320    /// Step 5 of <https://fetch.spec.whatwg.org/#concept-request-transmit-body>
321    fn callback(&self, cx: &mut CurrentRealm, v: HandleValue) {
322        let _realm = InRealm::Already(&cx.into());
323        let is_done = match get_read_promise_done(cx.into(), &v, CanGc::from_cx(cx)) {
324            Ok(is_done) => is_done,
325            Err(_) => {
326                // Step 5.5, the "otherwise" steps.
327                // TODO: terminate fetch.
328                let _ = self.control_sender.send(BodyChunkRequest::Done);
329                return self.stream.stop_reading(cx);
330            },
331        };
332
333        if is_done {
334            // Step 5.3, the "done" steps.
335            // TODO: queue a fetch task on request to process request end-of-body.
336            let _ = self.control_sender.send(BodyChunkRequest::Done);
337            return self.stream.stop_reading(cx);
338        }
339
340        let chunk = match get_read_promise_bytes(cx.into(), &v, CanGc::from_cx(cx)) {
341            Ok(chunk) => chunk,
342            Err(_) => {
343                // Step 5.5, the "otherwise" steps.
344                let _ = self.control_sender.send(BodyChunkRequest::Error);
345                return self.stream.stop_reading(cx);
346            },
347        };
348
349        // Step 5.1 and 5.2, transmit chunk.
350        // Send the chunk to the body transmitter in net::http_loader::obtain_response.
351        // TODO: queue a fetch task on request to process request body for request.
352        let _ = self
353            .bytes_sender
354            .send(BodyChunkResponse::Chunk(GenericSharedMemory::from_bytes(
355                &chunk,
356            )));
357    }
358}
359
360/// The handler of read promises rejection of body streams used in
361/// <https://fetch.spec.whatwg.org/#concept-request-transmit-body>.
362#[derive(Clone, JSTraceable, MallocSizeOf)]
363#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
364struct TransmitBodyPromiseRejectionHandler {
365    #[no_trace]
366    bytes_sender: IpcSender<BodyChunkResponse>,
367    stream: Dom<ReadableStream>,
368    #[no_trace]
369    control_sender: IpcSender<BodyChunkRequest>,
370}
371
372impl js::gc::Rootable for TransmitBodyPromiseRejectionHandler {}
373
374impl Callback for TransmitBodyPromiseRejectionHandler {
375    /// <https://fetch.spec.whatwg.org/#concept-request-transmit-body>
376    fn callback(&self, cx: &mut CurrentRealm, _v: HandleValue) {
377        // Step 5.4, the "rejection" steps.
378        let _ = self.control_sender.send(BodyChunkRequest::Error);
379        self.stream.stop_reading(cx);
380    }
381}
382
383/// <https://fetch.spec.whatwg.org/#body-with-type>
384pub(crate) struct ExtractedBody {
385    /// <https://fetch.spec.whatwg.org/#concept-body-stream>
386    pub(crate) stream: DomRoot<ReadableStream>,
387    /// <https://fetch.spec.whatwg.org/#concept-body-source>
388    pub(crate) source: BodySource,
389    /// <https://fetch.spec.whatwg.org/#concept-body-total-bytes>
390    pub(crate) total_bytes: Option<usize>,
391    /// <https://fetch.spec.whatwg.org/#body-with-type-type>
392    pub(crate) content_type: Option<DOMString>,
393}
394
395impl ExtractedBody {
396    /// Build a request body from the extracted body,
397    /// to be sent over IPC to net to use with `concept-request-transmit-body`,
398    /// see <https://fetch.spec.whatwg.org/#concept-request-transmit-body>.
399    ///
400    /// Also returning the corresponding readable stream,
401    /// to be stored on the request in script,
402    /// and potentially used as part of `consume_body`,
403    /// see <https://fetch.spec.whatwg.org/#concept-body-consume-body>
404    ///
405    /// Transmitting a body over fetch, and consuming it in script,
406    /// are mutually exclusive operations, since each will lock the stream to a reader.
407    pub(crate) fn into_net_request_body(self) -> (RequestBody, DomRoot<ReadableStream>) {
408        let ExtractedBody {
409            stream,
410            total_bytes,
411            content_type: _,
412            source,
413        } = self;
414
415        // First, setup some infra to be used to transmit body
416        //  from `components::script` to `components::net`.
417        let (chunk_request_sender, chunk_request_receiver) = ipc::channel().unwrap();
418
419        let trusted_stream = Trusted::new(&*stream);
420
421        let global = stream.global();
422        let task_source = global.task_manager().networking_task_source();
423
424        // In case of the data being in-memory, send everything in one chunk, by-passing SM.
425        let in_memory = stream.get_in_memory_bytes();
426
427        let net_source = match source {
428            BodySource::Null => NetBodySource::Null,
429            _ => NetBodySource::Object,
430        };
431
432        let mut body_handler = TransmitBodyConnectHandler::new(
433            trusted_stream,
434            task_source.into(),
435            chunk_request_sender.clone(),
436            in_memory,
437            source,
438        );
439
440        ROUTER.add_typed_route(
441            chunk_request_receiver,
442            Box::new(move |message| {
443                match message.unwrap() {
444                    BodyChunkRequest::Connect(sender) => {
445                        body_handler.start_reading(sender);
446                    },
447                    BodyChunkRequest::Extract(receiver) => {
448                        body_handler.re_extract(receiver);
449                    },
450                    BodyChunkRequest::Chunk => body_handler.transmit_body_chunk(),
451                    // Note: this is actually sent from this process
452                    // by the TransmitBodyPromiseHandler when reading stops.
453                    BodyChunkRequest::Done => {
454                        body_handler.stop_reading(StopReading::Done);
455                    },
456                    // Note: this is actually sent from this process
457                    // by the TransmitBodyPromiseHandler when the stream errors.
458                    BodyChunkRequest::Error => {
459                        body_handler.stop_reading(StopReading::Error);
460                    },
461                }
462            }),
463        );
464
465        // Return `components::net` view into this request body,
466        // which can be used by `net` to transmit it over the network.
467        let request_body = RequestBody::new(chunk_request_sender, net_source, total_bytes);
468
469        // Also return the stream for this body, which can be used by script to consume it.
470        (request_body, stream)
471    }
472
473    /// Is the data of the stream of this extracted body available in memory?
474    pub(crate) fn in_memory(&self) -> bool {
475        self.stream.in_memory()
476    }
477}
478
479/// <https://fetch.spec.whatwg.org/#concept-bodyinit-extract>
480pub(crate) trait Extractable {
481    fn extract(
482        &self,
483        cx: &mut js::context::JSContext,
484        global: &GlobalScope,
485        keep_alive: bool,
486    ) -> Fallible<ExtractedBody>;
487}
488
489impl Extractable for BodyInit {
490    /// <https://fetch.spec.whatwg.org/#concept-bodyinit-extract>
491    fn extract(
492        &self,
493        cx: &mut js::context::JSContext,
494        global: &GlobalScope,
495        keep_alive: bool,
496    ) -> Fallible<ExtractedBody> {
497        match self {
498            BodyInit::String(s) => s.extract(cx, global, keep_alive),
499            BodyInit::URLSearchParams(usp) => usp.extract(cx, global, keep_alive),
500            BodyInit::Blob(b) => b.extract(cx, global, keep_alive),
501            BodyInit::FormData(formdata) => formdata.extract(cx, global, keep_alive),
502            BodyInit::ArrayBuffer(typedarray) => {
503                let bytes = typedarray.to_vec();
504                let total_bytes = bytes.len();
505                let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
506                Ok(ExtractedBody {
507                    stream,
508                    total_bytes: Some(total_bytes),
509                    content_type: None,
510                    source: BodySource::Object,
511                })
512            },
513            BodyInit::ArrayBufferView(typedarray) => {
514                let bytes = typedarray.to_vec();
515                let total_bytes = bytes.len();
516                let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
517                Ok(ExtractedBody {
518                    stream,
519                    total_bytes: Some(total_bytes),
520                    content_type: None,
521                    source: BodySource::Object,
522                })
523            },
524            BodyInit::ReadableStream(stream) => {
525                // If keepalive is true, then throw a TypeError.
526                if keep_alive {
527                    return Err(Error::Type(
528                        c"The body's stream is for a keepalive request".to_owned(),
529                    ));
530                }
531                // If object is disturbed or locked, then throw a TypeError.
532                if stream.is_locked() || stream.is_disturbed() {
533                    return Err(Error::Type(
534                        c"The body's stream is disturbed or locked".to_owned(),
535                    ));
536                }
537
538                Ok(ExtractedBody {
539                    stream: stream.clone(),
540                    total_bytes: None,
541                    content_type: None,
542                    source: BodySource::Null,
543                })
544            },
545        }
546    }
547}
548
549impl Extractable for Vec<u8> {
550    fn extract(
551        &self,
552        cx: &mut js::context::JSContext,
553        global: &GlobalScope,
554        _keep_alive: bool,
555    ) -> Fallible<ExtractedBody> {
556        let bytes = self.clone();
557        let total_bytes = self.len();
558        let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
559        Ok(ExtractedBody {
560            stream,
561            total_bytes: Some(total_bytes),
562            content_type: None,
563            // A vec is used only in `submit_entity_body`.
564            source: BodySource::Object,
565        })
566    }
567}
568
569impl Extractable for Blob {
570    fn extract(
571        &self,
572        cx: &mut js::context::JSContext,
573        _global: &GlobalScope,
574        _keep_alive: bool,
575    ) -> Fallible<ExtractedBody> {
576        let blob_type = self.Type();
577        let content_type = if blob_type.is_empty() {
578            None
579        } else {
580            Some(blob_type)
581        };
582        let total_bytes = self.Size() as usize;
583        let stream = self.get_stream(cx)?;
584        Ok(ExtractedBody {
585            stream,
586            total_bytes: Some(total_bytes),
587            content_type,
588            source: BodySource::Object,
589        })
590    }
591}
592
593impl Extractable for DOMString {
594    fn extract(
595        &self,
596        cx: &mut js::context::JSContext,
597        global: &GlobalScope,
598        _keep_alive: bool,
599    ) -> Fallible<ExtractedBody> {
600        let bytes = self.as_bytes().to_owned();
601        let total_bytes = bytes.len();
602        let content_type = Some(DOMString::from("text/plain;charset=UTF-8"));
603        let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
604        Ok(ExtractedBody {
605            stream,
606            total_bytes: Some(total_bytes),
607            content_type,
608            source: BodySource::Object,
609        })
610    }
611}
612
613impl Extractable for FormData {
614    fn extract(
615        &self,
616        cx: &mut js::context::JSContext,
617        global: &GlobalScope,
618        _keep_alive: bool,
619    ) -> Fallible<ExtractedBody> {
620        let boundary = generate_boundary();
621        let bytes = encode_multipart_form_data(&mut self.datums(), boundary.clone(), UTF_8);
622        let total_bytes = bytes.len();
623        let content_type = Some(DOMString::from(format!(
624            "multipart/form-data; boundary={}",
625            boundary
626        )));
627        let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
628        Ok(ExtractedBody {
629            stream,
630            total_bytes: Some(total_bytes),
631            content_type,
632            source: BodySource::Object,
633        })
634    }
635}
636
637impl Extractable for URLSearchParams {
638    fn extract(
639        &self,
640        cx: &mut js::context::JSContext,
641        global: &GlobalScope,
642        _keep_alive: bool,
643    ) -> Fallible<ExtractedBody> {
644        let bytes = self.serialize_utf8().into_bytes();
645        let total_bytes = bytes.len();
646        let content_type = Some(DOMString::from(
647            "application/x-www-form-urlencoded;charset=UTF-8",
648        ));
649        let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
650        Ok(ExtractedBody {
651            stream,
652            total_bytes: Some(total_bytes),
653            content_type,
654            source: BodySource::Object,
655        })
656    }
657}
658
659#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
660pub(crate) enum BodyType {
661    Blob,
662    Bytes,
663    FormData,
664    Json,
665    Text,
666    ArrayBuffer,
667}
668
669pub(crate) enum FetchedData {
670    Text(String),
671    Json(RootedTraceableBox<Heap<JSValue>>),
672    BlobData(DomRoot<Blob>),
673    Bytes(RootedTraceableBox<Heap<*mut JSObject>>),
674    FormData(DomRoot<FormData>),
675    ArrayBuffer(RootedTraceableBox<Heap<*mut JSObject>>),
676    JSException(RootedTraceableBox<Heap<JSVal>>),
677}
678
679/// <https://fetch.spec.whatwg.org/#concept-body-consume-body>
680/// <https://fetch.spec.whatwg.org/#body-fully-read>
681/// A combination of parts of both algorithms,
682/// `body-fully-read` can be fully implemented, and separated, later,
683/// see #36049.
684pub(crate) fn consume_body<T: BodyMixin + DomObject>(
685    cx: &mut js::context::JSContext,
686    object: &T,
687    body_type: BodyType,
688) -> Rc<Promise> {
689    let global = object.global();
690
691    // Enter the realm of the object whose body is being consumed.
692    let mut realm = enter_auto_realm(cx, &*global);
693    let cx: &mut _ = &mut realm.current_realm();
694
695    // Let promise be a new promise.
696    // Note: re-ordered so we can return the promise below.
697    let promise = Promise::new_in_realm(cx);
698
699    // If object is unusable, then return a promise rejected with a TypeError.
700    if object.is_unusable() {
701        promise.reject_error(
702            Error::Type(c"The body's stream is disturbed or locked".to_owned()),
703            CanGc::from_cx(cx),
704        );
705        return promise;
706    }
707
708    let stream = match object.body() {
709        Some(stream) => stream,
710        None => {
711            // If object’s body is null, then run successSteps with an empty byte sequence.
712            let mime_type = object.get_mime_type(cx);
713            resolve_result_promise(cx, body_type, &promise, mime_type, Vec::with_capacity(0));
714            return promise;
715        },
716    };
717
718    // <https://fetch.spec.whatwg.org/#concept-body-consume-body>
719    // Otherwise, fully read object’s body given successSteps, errorSteps, and object’s relevant global object.
720    //
721    // <https://fetch.spec.whatwg.org/#body-fully-read>
722    // Let reader be the result of getting a reader for body’s stream.
723    // Read all bytes from reader, given successSteps and errorSteps.
724    //
725    // <https://streams.spec.whatwg.org/#readable-stream-default-reader-read>
726    // Set stream.[[disturbed]] to true.
727    // Otherwise, if stream.[[state]] is "errored", perform readRequest’s error steps given stream.[[storedError]].
728    //
729    // If the body stream is already errored (for example, the fetch was aborted after the Response exists),
730    // the normal fully read path would reject with [[storedError]] but would also mark the stream disturbed.
731    // Once the stream is disturbed, later calls reject with TypeError ("disturbed or locked") instead of the
732    // original AbortError. This early return rejects with the same [[storedError]] without disturbing the
733    // stream, so repeated calls (for example, calling text() twice) keep rejecting with AbortError.
734    if stream.is_errored() {
735        rooted!(&in(cx) let mut stored_error = UndefinedValue());
736        stream.get_stored_error(stored_error.handle_mut());
737        promise.reject(cx.into(), stored_error.handle(), CanGc::from_cx(cx));
738        return promise;
739    }
740
741    // Note: from `fully_read`.
742    // Let reader be the result of getting a reader for body’s stream.
743    // If that threw an exception,
744    // then run errorSteps with that exception and return.
745    let reader = match stream.acquire_default_reader(CanGc::from_cx(cx)) {
746        Ok(r) => r,
747        Err(e) => {
748            promise.reject_error(e, CanGc::from_cx(cx));
749            return promise;
750        },
751    };
752
753    // Let errorSteps given error be to reject promise with error.
754    let error_promise = promise.clone();
755
756    // Let successSteps given a byte sequence data be to resolve promise
757    // with the result of running convertBytesToJSValue with data.
758    // If that threw an exception, then run errorSteps with that exception.
759    let mime_type = object.get_mime_type(cx);
760    let success_promise = promise.clone();
761
762    // Read all bytes from reader, given successSteps and errorSteps.
763    // Note: spec uses an intermediary concept of `fully_read`,
764    // which seems useful when invoking fetch from other places.
765    // TODO: #36049
766    reader.read_all_bytes(
767        cx,
768        Rc::new(move |cx, bytes: &[u8]| {
769            resolve_result_promise(
770                cx,
771                body_type,
772                &success_promise,
773                mime_type.clone(),
774                bytes.to_vec(),
775            );
776        }),
777        Rc::new(move |cx, v| {
778            error_promise.reject(cx.into(), v, CanGc::from_cx(cx));
779        }),
780    );
781
782    promise
783}
784
785/// The success steps of
786/// <https://fetch.spec.whatwg.org/#concept-body-consume-body>.
787fn resolve_result_promise(
788    cx: &mut js::context::JSContext,
789    body_type: BodyType,
790    promise: &Promise,
791    mime_type: Vec<u8>,
792    body: Vec<u8>,
793) {
794    let pkg_data_results = run_package_data_algorithm(cx, body, body_type, mime_type);
795
796    match pkg_data_results {
797        Ok(results) => {
798            match results {
799                FetchedData::Text(s) => promise.resolve_native(&USVString(s), CanGc::from_cx(cx)),
800                FetchedData::Json(j) => promise.resolve_native(&j, CanGc::from_cx(cx)),
801                FetchedData::BlobData(b) => promise.resolve_native(&b, CanGc::from_cx(cx)),
802                FetchedData::FormData(f) => promise.resolve_native(&f, CanGc::from_cx(cx)),
803                FetchedData::Bytes(b) => promise.resolve_native(&b, CanGc::from_cx(cx)),
804                FetchedData::ArrayBuffer(a) => promise.resolve_native(&a, CanGc::from_cx(cx)),
805                FetchedData::JSException(e) => {
806                    promise.reject_native(&e.handle(), CanGc::from_cx(cx))
807                },
808            };
809        },
810        Err(err) => promise.reject_error(err, CanGc::from_cx(cx)),
811    }
812}
813
814/// The algorithm that takes a byte sequence
815/// and returns a JavaScript value or throws an exception of
816/// <https://fetch.spec.whatwg.org/#concept-body-consume-body>.
817fn run_package_data_algorithm(
818    cx: &mut js::context::JSContext,
819    bytes: Vec<u8>,
820    body_type: BodyType,
821    mime_type: Vec<u8>,
822) -> Fallible<FetchedData> {
823    let mime = &*mime_type;
824    let realm = CurrentRealm::assert(cx);
825    let global = GlobalScope::from_current_realm(&realm);
826    match body_type {
827        BodyType::Text => run_text_data_algorithm(bytes),
828        BodyType::Json => run_json_data_algorithm(cx, bytes),
829        BodyType::Blob => run_blob_data_algorithm(cx, &global, bytes, mime),
830        BodyType::FormData => run_form_data_algorithm(cx, &global, bytes, mime),
831        BodyType::ArrayBuffer => run_array_buffer_data_algorithm(cx, bytes),
832        BodyType::Bytes => run_bytes_data_algorithm(cx, bytes),
833    }
834}
835
836/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A4>
837fn run_text_data_algorithm(bytes: Vec<u8>) -> Fallible<FetchedData> {
838    // This implements the Encoding standard's "decode UTF-8", which removes the
839    // BOM if present.
840    let no_bom_bytes = if bytes.starts_with(b"\xEF\xBB\xBF") {
841        &bytes[3..]
842    } else {
843        &bytes
844    };
845    Ok(FetchedData::Text(
846        String::from_utf8_lossy(no_bom_bytes).into_owned(),
847    ))
848}
849
850#[expect(unsafe_code)]
851/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A3>
852fn run_json_data_algorithm(
853    cx: &mut js::context::JSContext,
854    bytes: Vec<u8>,
855) -> Fallible<FetchedData> {
856    // The JSON spec allows implementations to either ignore UTF-8 BOM or treat it as an error.
857    // `JS_ParseJSON` treats this as an error, so it is necessary for us to strip it if present.
858    //
859    // https://datatracker.ietf.org/doc/html/rfc8259#section-8.1
860    let json_text = decode_to_utf16_with_bom_removal(&bytes, UTF_8);
861    rooted!(&in(cx) let mut rval = UndefinedValue());
862    unsafe {
863        if !JS_ParseJSON(
864            cx,
865            json_text.as_ptr(),
866            json_text.len() as u32,
867            rval.handle_mut(),
868        ) {
869            rooted!(&in(cx) let mut exception = UndefinedValue());
870            assert!(JS_GetPendingException(cx, exception.handle_mut()));
871            JS_ClearPendingException(cx);
872            return Ok(FetchedData::JSException(RootedTraceableBox::from_box(
873                Heap::boxed(exception.get()),
874            )));
875        }
876        let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(rval.get()));
877        Ok(FetchedData::Json(rooted_heap))
878    }
879}
880
881/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A0>
882fn run_blob_data_algorithm(
883    cx: &mut js::context::JSContext,
884    root: &GlobalScope,
885    bytes: Vec<u8>,
886    mime: &[u8],
887) -> Fallible<FetchedData> {
888    let mime_string = if let Ok(s) = String::from_utf8(mime.to_vec()) {
889        s
890    } else {
891        "".to_string()
892    };
893    let blob = Blob::new(
894        root,
895        BlobImpl::new_from_bytes(bytes, normalize_type_string(&mime_string)),
896        CanGc::from_cx(cx),
897    );
898    Ok(FetchedData::BlobData(blob))
899}
900
901fn extract_name_from_content_disposition(headers: &HeaderMap) -> Option<String> {
902    let cd = headers.get(CONTENT_DISPOSITION)?.to_str().ok()?;
903
904    for part in cd.split(';').map(|s| s.trim()) {
905        if let Some(rest) = part.strip_prefix("name=") {
906            let v = rest.trim();
907            let v = v.strip_prefix('"').unwrap_or(v);
908            let v = v.strip_suffix('"').unwrap_or(v);
909            return Some(v.to_string());
910        }
911    }
912    None
913}
914
915fn extract_filename_from_content_disposition(headers: &HeaderMap) -> Option<String> {
916    let cd = headers.get(CONTENT_DISPOSITION)?.to_str().ok()?;
917    if let Some(index) = cd.find("filename=") {
918        let start = index + "filename=".len();
919        return Some(
920            cd.get(start..)
921                .unwrap_or_default()
922                .trim_matches('"')
923                .to_owned(),
924        );
925    }
926    if let Some(index) = cd.find("filename*=UTF-8''") {
927        let start = index + "filename*=UTF-8''".len();
928        return Some(
929            cd.get(start..)
930                .unwrap_or_default()
931                .trim_matches('"')
932                .to_owned(),
933        );
934    }
935    None
936}
937
938fn content_type_from_headers(headers: &HeaderMap) -> Result<String, Error> {
939    match headers.get(CONTENT_TYPE) {
940        Some(value) => Ok(value
941            .to_str()
942            .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?
943            .to_string()),
944        None => Ok("text/plain".to_string()),
945    }
946}
947
948fn append_form_data_entry_from_part(
949    cx: &mut js::context::JSContext,
950    root: &GlobalScope,
951    formdata: &FormData,
952    headers: &HeaderMap,
953    body: Vec<u8>,
954) -> Fallible<()> {
955    let Some(name) = extract_name_from_content_disposition(headers) else {
956        return Ok(());
957    };
958    // A part whose `Content-Disposition` header contains a `name` parameter whose value is `_charset_` is parsed like any other part. It does not change the encoding.
959    let filename = extract_filename_from_content_disposition(headers);
960    if let Some(filename) = filename {
961        // Each part whose `Content-Disposition` header contains a `filename` parameter must be parsed into an entry whose value is a File object whose contents are the contents of the part.
962        //
963        // The name attribute of the File object must have the value of the `filename` parameter of the part.
964        //
965        // The type attribute of the File object must have the value of the `Content-Type` header of the part if the part has such header, and `text/plain` (the default defined by [RFC7578] section 4.4) otherwise.
966        let content_type = content_type_from_headers(headers)?;
967        let file = File::new(
968            root,
969            BlobImpl::new_from_bytes(body, normalize_type_string(&content_type)),
970            DOMString::from(filename),
971            None,
972            CanGc::from_cx(cx),
973        );
974        let blob = file.upcast::<Blob>();
975        formdata.Append_(USVString(name), blob, None);
976    } else {
977        // Each part whose `Content-Disposition` header does not contain a `filename` parameter must be parsed into an entry whose value is the UTF-8 decoded without BOM content of the part. This is done regardless of the presence or the value of a `Content-Type` header and regardless of the presence or the value of a `charset` parameter.
978
979        let (value, _) = UTF_8.decode_without_bom_handling(&body);
980        formdata.Append(USVString(name), USVString(value.to_string()));
981    }
982    Ok(())
983}
984
985fn append_multipart_nodes(
986    cx: &mut js::context::JSContext,
987    root: &GlobalScope,
988    formdata: &FormData,
989    nodes: Vec<Node>,
990) -> Fallible<()> {
991    for node in nodes {
992        match node {
993            Node::Part(part) => {
994                append_form_data_entry_from_part(cx, root, formdata, &part.headers, part.body)?;
995            },
996            Node::File(file_part) => {
997                let body = fs::read(&file_part.path)
998                    .map_err(|_| Error::Type(c"file part could not be read".to_owned()))?;
999                append_form_data_entry_from_part(cx, root, formdata, &file_part.headers, body)?;
1000            },
1001            Node::Multipart((_, inner)) => {
1002                append_multipart_nodes(cx, root, formdata, inner)?;
1003            },
1004        }
1005    }
1006    Ok(())
1007}
1008
1009/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A2>
1010fn run_form_data_algorithm(
1011    cx: &mut js::context::JSContext,
1012    root: &GlobalScope,
1013    bytes: Vec<u8>,
1014    mime: &[u8],
1015) -> Fallible<FetchedData> {
1016    // The formData() method steps are to return the result of running consume body
1017    // with this and the following steps given a byte sequence bytes:
1018    let mime_str = str::from_utf8(mime).unwrap_or_default();
1019    let mime: Mime = mime_str
1020        .parse()
1021        .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?;
1022
1023    // Let mimeType be the result of get the MIME type with this.
1024    //
1025    // If mimeType is non-null, then switch on mimeType’s essence and run the corresponding steps:
1026    if mime.type_() == mime::MULTIPART && mime.subtype() == mime::FORM_DATA {
1027        // "multipart/form-data"
1028        // Parse bytes, using the value of the `boundary` parameter from mimeType,
1029        // per the rules set forth in Returning Values from Forms: multipart/form-data. [RFC7578]
1030        let mut headers = HeaderMap::new();
1031        headers.insert(
1032            CONTENT_TYPE,
1033            mime_str
1034                .parse()
1035                .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?,
1036        );
1037
1038        if let Some(boundary) = mime.get_param(mime::BOUNDARY) {
1039            let closing_boundary = format!("--{}--", boundary.as_str()).into_bytes();
1040            let trimmed_bytes = bytes.strip_suffix(b"\r\n").unwrap_or(&bytes);
1041            if trimmed_bytes == closing_boundary {
1042                let formdata = FormData::new(None, root, CanGc::from_cx(cx));
1043                return Ok(FetchedData::FormData(formdata));
1044            }
1045        }
1046
1047        let mut cursor = Cursor::new(bytes);
1048        // If that fails for some reason, then throw a TypeError.
1049        let nodes = read_multipart_body(&mut cursor, &headers, false)
1050            .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?;
1051        // The above is a rough approximation of what is needed for `multipart/form-data`,
1052        // a more detailed parsing specification is to be written. Volunteers welcome.
1053
1054        // Return a new FormData object, appending each entry, resulting from the parsing operation, to its entry list.
1055        let formdata = FormData::new(None, root, CanGc::from_cx(cx));
1056
1057        append_multipart_nodes(cx, root, &formdata, nodes)?;
1058
1059        return Ok(FetchedData::FormData(formdata));
1060    }
1061
1062    if mime.type_() == mime::APPLICATION && mime.subtype() == mime::WWW_FORM_URLENCODED {
1063        // "application/x-www-form-urlencoded"
1064        // Let entries be the result of parsing bytes.
1065        //
1066        // Return a new FormData object whose entry list is entries.
1067        let entries = form_urlencoded::parse(&bytes);
1068        let formdata = FormData::new(None, root, CanGc::from_cx(cx));
1069        for (k, e) in entries {
1070            formdata.Append(USVString(k.into_owned()), USVString(e.into_owned()));
1071        }
1072        return Ok(FetchedData::FormData(formdata));
1073    }
1074
1075    // Throw a TypeError.
1076    Err(Error::Type(c"Inappropriate MIME-type for Body".to_owned()))
1077}
1078
1079/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A1>
1080fn run_bytes_data_algorithm(
1081    cx: &mut js::context::JSContext,
1082    bytes: Vec<u8>,
1083) -> Fallible<FetchedData> {
1084    rooted!(&in(cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>());
1085
1086    create_buffer_source::<Uint8>(
1087        cx.into(),
1088        &bytes,
1089        array_buffer_ptr.handle_mut(),
1090        CanGc::from_cx(cx),
1091    )
1092    .map_err(|_| Error::JSFailed)?;
1093
1094    let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(array_buffer_ptr.get()));
1095    Ok(FetchedData::Bytes(rooted_heap))
1096}
1097
1098/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body>
1099pub(crate) fn run_array_buffer_data_algorithm(
1100    cx: &mut js::context::JSContext,
1101    bytes: Vec<u8>,
1102) -> Fallible<FetchedData> {
1103    rooted!(&in(cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>());
1104
1105    create_buffer_source::<ArrayBufferU8>(
1106        cx.into(),
1107        &bytes,
1108        array_buffer_ptr.handle_mut(),
1109        CanGc::from_cx(cx),
1110    )
1111    .map_err(|_| Error::JSFailed)?;
1112
1113    let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(array_buffer_ptr.get()));
1114    Ok(FetchedData::ArrayBuffer(rooted_heap))
1115}
1116
1117#[expect(unsafe_code)]
1118pub(crate) fn decode_to_utf16_with_bom_removal(
1119    bytes: &[u8],
1120    encoding: &'static Encoding,
1121) -> Vec<u16> {
1122    let mut decoder = encoding.new_decoder_with_bom_removal();
1123    let capacity = decoder
1124        .max_utf16_buffer_length(bytes.len())
1125        .expect("Overflow");
1126    let mut utf16 = Vec::with_capacity(capacity);
1127    let extra = unsafe { slice::from_raw_parts_mut(utf16.as_mut_ptr(), capacity) };
1128    let (_, read, written, _) = decoder.decode_to_utf16(bytes, extra, true);
1129    assert_eq!(read, bytes.len());
1130    unsafe { utf16.set_len(written) }
1131    utf16
1132}
1133
1134/// <https://fetch.spec.whatwg.org/#body>
1135pub(crate) trait BodyMixin {
1136    /// <https://fetch.spec.whatwg.org/#dom-body-bodyused>
1137    fn is_body_used(&self) -> bool;
1138    /// <https://fetch.spec.whatwg.org/#body-unusable>
1139    fn is_unusable(&self) -> bool;
1140    /// <https://fetch.spec.whatwg.org/#dom-body-body>
1141    fn body(&self) -> Option<DomRoot<ReadableStream>>;
1142    /// <https://fetch.spec.whatwg.org/#concept-body-mime-type>
1143    fn get_mime_type(&self, cx: &mut js::context::JSContext) -> Vec<u8>;
1144}