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