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::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(cx, &global, promise_handler.take().map(|h| Box::new(h) as Box<_>), rejection_handler.take().map(|h| Box::new(h) as Box<_>));
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 is_done = match get_read_promise_done(cx, &v) {
323            Ok(is_done) => is_done,
324            Err(_) => {
325                // Step 5.5, the "otherwise" steps.
326                // TODO: terminate fetch.
327                let _ = self.control_sender.send(BodyChunkRequest::Done);
328                return self.stream.stop_reading(cx);
329            },
330        };
331
332        if is_done {
333            // Step 5.3, the "done" steps.
334            // TODO: queue a fetch task on request to process request end-of-body.
335            let _ = self.control_sender.send(BodyChunkRequest::Done);
336            return self.stream.stop_reading(cx);
337        }
338
339        let chunk = match get_read_promise_bytes(cx, &v) {
340            Ok(chunk) => chunk,
341            Err(_) => {
342                // Step 5.5, the "otherwise" steps.
343                let _ = self.control_sender.send(BodyChunkRequest::Error);
344                return self.stream.stop_reading(cx);
345            },
346        };
347
348        // Step 5.1 and 5.2, transmit chunk.
349        // Send the chunk to the body transmitter in net::http_loader::obtain_response.
350        // TODO: queue a fetch task on request to process request body for request.
351        let _ = self
352            .bytes_sender
353            .send(BodyChunkResponse::Chunk(GenericSharedMemory::from_vec(
354                chunk,
355            )));
356    }
357}
358
359/// The handler of read promises rejection of body streams used in
360/// <https://fetch.spec.whatwg.org/#concept-request-transmit-body>.
361#[derive(Clone, JSTraceable, MallocSizeOf)]
362#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
363struct TransmitBodyPromiseRejectionHandler {
364    #[no_trace]
365    bytes_sender: IpcSender<BodyChunkResponse>,
366    stream: Dom<ReadableStream>,
367    #[no_trace]
368    control_sender: IpcSender<BodyChunkRequest>,
369}
370
371impl js::gc::Rootable for TransmitBodyPromiseRejectionHandler {}
372
373impl Callback for TransmitBodyPromiseRejectionHandler {
374    /// <https://fetch.spec.whatwg.org/#concept-request-transmit-body>
375    fn callback(&self, cx: &mut CurrentRealm, _v: HandleValue) {
376        // Step 5.4, the "rejection" steps.
377        let _ = self.control_sender.send(BodyChunkRequest::Error);
378        self.stream.stop_reading(cx);
379    }
380}
381
382/// <https://fetch.spec.whatwg.org/#body-with-type>
383pub(crate) struct ExtractedBody {
384    /// <https://fetch.spec.whatwg.org/#concept-body-stream>
385    pub(crate) stream: DomRoot<ReadableStream>,
386    /// <https://fetch.spec.whatwg.org/#concept-body-source>
387    pub(crate) source: BodySource,
388    /// <https://fetch.spec.whatwg.org/#concept-body-total-bytes>
389    pub(crate) total_bytes: Option<usize>,
390    /// <https://fetch.spec.whatwg.org/#body-with-type-type>
391    pub(crate) content_type: Option<DOMString>,
392}
393
394impl ExtractedBody {
395    /// Build a request body from the extracted body,
396    /// to be sent over IPC to net to use with `concept-request-transmit-body`,
397    /// see <https://fetch.spec.whatwg.org/#concept-request-transmit-body>.
398    ///
399    /// Also returning the corresponding readable stream,
400    /// to be stored on the request in script,
401    /// and potentially used as part of `consume_body`,
402    /// see <https://fetch.spec.whatwg.org/#concept-body-consume-body>
403    ///
404    /// Transmitting a body over fetch, and consuming it in script,
405    /// are mutually exclusive operations, since each will lock the stream to a reader.
406    pub(crate) fn into_net_request_body(self) -> (RequestBody, DomRoot<ReadableStream>) {
407        let ExtractedBody {
408            stream,
409            total_bytes,
410            content_type: _,
411            source,
412        } = self;
413
414        // First, setup some infra to be used to transmit body
415        //  from `components::script` to `components::net`.
416        let (chunk_request_sender, chunk_request_receiver) = ipc::channel().unwrap();
417
418        let trusted_stream = Trusted::new(&*stream);
419
420        let global = stream.global();
421        let task_source = global.task_manager().networking_task_source();
422
423        // In case of the data being in-memory, send everything in one chunk, by-passing SM.
424        // Empty extracted bodies are always representable as an in-memory empty payload.
425        let in_memory = stream.get_in_memory_bytes().or_else(|| {
426            if total_bytes == Some(0) {
427                Some(GenericSharedMemory::from_bytes(&[]))
428            } else {
429                None
430            }
431        });
432
433        let net_source = match source {
434            BodySource::Null => NetBodySource::Null,
435            _ => NetBodySource::Object,
436        };
437
438        let mut body_handler = TransmitBodyConnectHandler::new(
439            trusted_stream,
440            task_source.into(),
441            chunk_request_sender.clone(),
442            in_memory,
443            source,
444        );
445
446        ROUTER.add_typed_route(
447            chunk_request_receiver,
448            Box::new(move |message| {
449                match message.unwrap() {
450                    BodyChunkRequest::Connect(sender) => {
451                        body_handler.start_reading(sender);
452                    },
453                    BodyChunkRequest::Extract(receiver) => {
454                        body_handler.re_extract(receiver);
455                    },
456                    BodyChunkRequest::Chunk => body_handler.transmit_body_chunk(),
457                    // Note: this is actually sent from this process
458                    // by the TransmitBodyPromiseHandler when reading stops.
459                    BodyChunkRequest::Done => {
460                        body_handler.stop_reading(StopReading::Done);
461                    },
462                    // Note: this is actually sent from this process
463                    // by the TransmitBodyPromiseHandler when the stream errors.
464                    BodyChunkRequest::Error => {
465                        body_handler.stop_reading(StopReading::Error);
466                    },
467                }
468            }),
469        );
470
471        // Return `components::net` view into this request body,
472        // which can be used by `net` to transmit it over the network.
473        let request_body = RequestBody::new(chunk_request_sender, net_source, total_bytes);
474
475        // Also return the stream for this body, which can be used by script to consume it.
476        (request_body, stream)
477    }
478
479    /// Is the data of the stream of this extracted body available in memory?
480    pub(crate) fn in_memory(&self) -> bool {
481        self.stream.in_memory()
482    }
483}
484
485/// <https://fetch.spec.whatwg.org/#concept-bodyinit-extract>
486pub(crate) trait Extractable {
487    fn extract(
488        &self,
489        cx: &mut js::context::JSContext,
490        global: &GlobalScope,
491        keep_alive: bool,
492    ) -> Fallible<ExtractedBody>;
493}
494
495impl Extractable for BodyInit {
496    /// <https://fetch.spec.whatwg.org/#concept-bodyinit-extract>
497    fn extract(
498        &self,
499        cx: &mut js::context::JSContext,
500        global: &GlobalScope,
501        keep_alive: bool,
502    ) -> Fallible<ExtractedBody> {
503        match self {
504            BodyInit::String(s) => s.extract(cx, global, keep_alive),
505            BodyInit::URLSearchParams(usp) => usp.extract(cx, global, keep_alive),
506            BodyInit::Blob(b) => b.extract(cx, global, keep_alive),
507            BodyInit::FormData(formdata) => formdata.extract(cx, global, keep_alive),
508            BodyInit::ArrayBuffer(typedarray) => {
509                let bytes = typedarray.to_vec();
510                let total_bytes = bytes.len();
511                let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
512                Ok(ExtractedBody {
513                    stream,
514                    total_bytes: Some(total_bytes),
515                    content_type: None,
516                    source: BodySource::Object,
517                })
518            },
519            BodyInit::ArrayBufferView(typedarray) => {
520                let bytes = typedarray.to_vec();
521                let total_bytes = bytes.len();
522                let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
523                Ok(ExtractedBody {
524                    stream,
525                    total_bytes: Some(total_bytes),
526                    content_type: None,
527                    source: BodySource::Object,
528                })
529            },
530            BodyInit::ReadableStream(stream) => {
531                // If keepalive is true, then throw a TypeError.
532                if keep_alive {
533                    return Err(Error::Type(
534                        c"The body's stream is for a keepalive request".to_owned(),
535                    ));
536                }
537                // If object is disturbed or locked, then throw a TypeError.
538                if stream.is_locked() || stream.is_disturbed() {
539                    return Err(Error::Type(
540                        c"The body's stream is disturbed or locked".to_owned(),
541                    ));
542                }
543
544                Ok(ExtractedBody {
545                    stream: stream.clone(),
546                    total_bytes: None,
547                    content_type: None,
548                    source: BodySource::Null,
549                })
550            },
551        }
552    }
553}
554
555impl Extractable for Vec<u8> {
556    fn extract(
557        &self,
558        cx: &mut js::context::JSContext,
559        global: &GlobalScope,
560        _keep_alive: bool,
561    ) -> Fallible<ExtractedBody> {
562        let bytes = self.clone();
563        let total_bytes = self.len();
564        let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
565        Ok(ExtractedBody {
566            stream,
567            total_bytes: Some(total_bytes),
568            content_type: None,
569            // A vec is used only in `submit_entity_body`.
570            source: BodySource::Object,
571        })
572    }
573}
574
575impl Extractable for Blob {
576    fn extract(
577        &self,
578        cx: &mut js::context::JSContext,
579        _global: &GlobalScope,
580        _keep_alive: bool,
581    ) -> Fallible<ExtractedBody> {
582        let blob_type = self.Type();
583        let content_type = if blob_type.is_empty() {
584            None
585        } else {
586            Some(blob_type)
587        };
588        let total_bytes = self.Size() as usize;
589        let stream = self.get_stream(cx)?;
590        Ok(ExtractedBody {
591            stream,
592            total_bytes: Some(total_bytes),
593            content_type,
594            source: BodySource::Object,
595        })
596    }
597}
598
599impl Extractable for DOMString {
600    fn extract(
601        &self,
602        cx: &mut js::context::JSContext,
603        global: &GlobalScope,
604        _keep_alive: bool,
605    ) -> Fallible<ExtractedBody> {
606        let bytes = self.as_bytes().to_owned();
607        let total_bytes = bytes.len();
608        let content_type = Some(DOMString::from("text/plain;charset=UTF-8"));
609        let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
610        Ok(ExtractedBody {
611            stream,
612            total_bytes: Some(total_bytes),
613            content_type,
614            source: BodySource::Object,
615        })
616    }
617}
618
619impl Extractable for FormData {
620    fn extract(
621        &self,
622        cx: &mut js::context::JSContext,
623        global: &GlobalScope,
624        _keep_alive: bool,
625    ) -> Fallible<ExtractedBody> {
626        let boundary = generate_boundary();
627        let bytes = encode_multipart_form_data(&mut self.datums(), boundary.clone(), UTF_8);
628        let total_bytes = bytes.len();
629        let content_type = Some(DOMString::from(format!(
630            "multipart/form-data; boundary={}",
631            boundary
632        )));
633        let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
634        Ok(ExtractedBody {
635            stream,
636            total_bytes: Some(total_bytes),
637            content_type,
638            source: BodySource::Object,
639        })
640    }
641}
642
643impl Extractable for URLSearchParams {
644    fn extract(
645        &self,
646        cx: &mut js::context::JSContext,
647        global: &GlobalScope,
648        _keep_alive: bool,
649    ) -> Fallible<ExtractedBody> {
650        let bytes = self.serialize_utf8().into_bytes();
651        let total_bytes = bytes.len();
652        let content_type = Some(DOMString::from(
653            "application/x-www-form-urlencoded;charset=UTF-8",
654        ));
655        let stream = ReadableStream::new_from_bytes(cx, global, bytes)?;
656        Ok(ExtractedBody {
657            stream,
658            total_bytes: Some(total_bytes),
659            content_type,
660            source: BodySource::Object,
661        })
662    }
663}
664
665#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
666pub(crate) enum BodyType {
667    Blob,
668    Bytes,
669    FormData,
670    Json,
671    Text,
672    ArrayBuffer,
673}
674
675pub(crate) enum FetchedData {
676    Text(String),
677    Json(RootedTraceableBox<Heap<JSValue>>),
678    BlobData(DomRoot<Blob>),
679    Bytes(RootedTraceableBox<Heap<*mut JSObject>>),
680    FormData(DomRoot<FormData>),
681    ArrayBuffer(RootedTraceableBox<Heap<*mut JSObject>>),
682    JSException(RootedTraceableBox<Heap<JSVal>>),
683}
684
685/// <https://fetch.spec.whatwg.org/#concept-body-consume-body>
686/// <https://fetch.spec.whatwg.org/#body-fully-read>
687/// A combination of parts of both algorithms,
688/// `body-fully-read` can be fully implemented, and separated, later,
689/// see #36049.
690pub(crate) fn consume_body<T: BodyMixin + DomObject>(
691    cx: &mut js::context::JSContext,
692    object: &T,
693    body_type: BodyType,
694) -> Rc<Promise> {
695    let global = object.global();
696
697    // Enter the realm of the object whose body is being consumed.
698    let mut realm = enter_auto_realm(cx, &*global);
699    let cx: &mut _ = &mut realm.current_realm();
700
701    // Let promise be a new promise.
702    // Note: re-ordered so we can return the promise below.
703    let promise = Promise::new_in_realm(cx);
704
705    // If object is unusable, then return a promise rejected with a TypeError.
706    if object.is_unusable() {
707        promise.reject_error_with_cx(
708            cx,
709            Error::Type(c"The body's stream is disturbed or locked".to_owned()),
710        );
711        return promise;
712    }
713
714    let stream = match object.body() {
715        Some(stream) => stream,
716        None => {
717            // If object’s body is null, then run successSteps with an empty byte sequence.
718            let mime_type = object.get_mime_type(cx);
719            resolve_result_promise(cx, body_type, &promise, mime_type, Vec::with_capacity(0));
720            return promise;
721        },
722    };
723
724    // <https://fetch.spec.whatwg.org/#concept-body-consume-body>
725    // Otherwise, fully read object’s body given successSteps, errorSteps, and object’s relevant global object.
726    //
727    // <https://fetch.spec.whatwg.org/#body-fully-read>
728    // Let reader be the result of getting a reader for body’s stream.
729    // Read all bytes from reader, given successSteps and errorSteps.
730    //
731    // <https://streams.spec.whatwg.org/#readable-stream-default-reader-read>
732    // Set stream.[[disturbed]] to true.
733    // Otherwise, if stream.[[state]] is "errored", perform readRequest’s error steps given stream.[[storedError]].
734    //
735    // If the body stream is already errored (for example, the fetch was aborted after the Response exists),
736    // the normal fully read path would reject with [[storedError]] but would also mark the stream disturbed.
737    // Once the stream is disturbed, later calls reject with TypeError ("disturbed or locked") instead of the
738    // original AbortError. This early return rejects with the same [[storedError]] without disturbing the
739    // stream, so repeated calls (for example, calling text() twice) keep rejecting with AbortError.
740    if stream.is_errored() {
741        rooted!(&in(cx) let mut stored_error = UndefinedValue());
742        stream.get_stored_error(stored_error.handle_mut());
743        promise.reject_with_cx(cx, stored_error.handle());
744        return promise;
745    }
746
747    // Note: from `fully_read`.
748    // Let reader be the result of getting a reader for body’s stream.
749    // If that threw an exception,
750    // then run errorSteps with that exception and return.
751    let reader = match stream.acquire_default_reader(CanGc::from_cx(cx)) {
752        Ok(r) => r,
753        Err(e) => {
754            promise.reject_error_with_cx(cx, e);
755            return promise;
756        },
757    };
758
759    // Let errorSteps given error be to reject promise with error.
760    let error_promise = promise.clone();
761
762    // Let successSteps given a byte sequence data be to resolve promise
763    // with the result of running convertBytesToJSValue with data.
764    // If that threw an exception, then run errorSteps with that exception.
765    let mime_type = object.get_mime_type(cx);
766    let success_promise = promise.clone();
767
768    // Read all bytes from reader, given successSteps and errorSteps.
769    // Note: spec uses an intermediary concept of `fully_read`,
770    // which seems useful when invoking fetch from other places.
771    // TODO: #36049
772    reader.read_all_bytes(
773        cx,
774        Rc::new(move |cx, bytes: &[u8]| {
775            resolve_result_promise(
776                cx,
777                body_type,
778                &success_promise,
779                mime_type.clone(),
780                bytes.to_vec(),
781            );
782        }),
783        Rc::new(move |cx, v| {
784            error_promise.reject_with_cx(cx, v);
785        }),
786    );
787
788    promise
789}
790
791/// The success steps of
792/// <https://fetch.spec.whatwg.org/#concept-body-consume-body>.
793fn resolve_result_promise(
794    cx: &mut js::context::JSContext,
795    body_type: BodyType,
796    promise: &Promise,
797    mime_type: Vec<u8>,
798    body: Vec<u8>,
799) {
800    let pkg_data_results = run_package_data_algorithm(cx, body, body_type, mime_type);
801
802    match pkg_data_results {
803        Ok(results) => {
804            match results {
805                FetchedData::Text(s) => promise.resolve_native_with_cx(cx, &USVString(s)),
806                FetchedData::Json(j) => promise.resolve_native_with_cx(cx, &j),
807                FetchedData::BlobData(b) => promise.resolve_native_with_cx(cx, &b),
808                FetchedData::FormData(f) => promise.resolve_native_with_cx(cx, &f),
809                FetchedData::Bytes(b) => promise.resolve_native_with_cx(cx, &b),
810                FetchedData::ArrayBuffer(a) => promise.resolve_native_with_cx(cx, &a),
811                FetchedData::JSException(e) => {
812                    promise.reject_native(&e.handle(), CanGc::from_cx(cx))
813                },
814            };
815        },
816        Err(err) => promise.reject_error_with_cx(cx, err),
817    }
818}
819
820/// The algorithm that takes a byte sequence
821/// and returns a JavaScript value or throws an exception of
822/// <https://fetch.spec.whatwg.org/#concept-body-consume-body>.
823fn run_package_data_algorithm(
824    cx: &mut js::context::JSContext,
825    bytes: Vec<u8>,
826    body_type: BodyType,
827    mime_type: Vec<u8>,
828) -> Fallible<FetchedData> {
829    let mime = &*mime_type;
830    let realm = CurrentRealm::assert(cx);
831    let global = GlobalScope::from_current_realm(&realm);
832    match body_type {
833        BodyType::Text => run_text_data_algorithm(bytes),
834        BodyType::Json => run_json_data_algorithm(cx, bytes),
835        BodyType::Blob => run_blob_data_algorithm(cx, &global, bytes, mime),
836        BodyType::FormData => run_form_data_algorithm(cx, &global, bytes, mime),
837        BodyType::ArrayBuffer => run_array_buffer_data_algorithm(cx, bytes),
838        BodyType::Bytes => run_bytes_data_algorithm(cx, bytes),
839    }
840}
841
842/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A4>
843fn run_text_data_algorithm(bytes: Vec<u8>) -> Fallible<FetchedData> {
844    // This implements the Encoding standard's "decode UTF-8", which removes the
845    // BOM if present.
846    let no_bom_bytes = if bytes.starts_with(b"\xEF\xBB\xBF") {
847        &bytes[3..]
848    } else {
849        &bytes
850    };
851    Ok(FetchedData::Text(
852        String::from_utf8_lossy(no_bom_bytes).into_owned(),
853    ))
854}
855
856#[expect(unsafe_code)]
857/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A3>
858fn run_json_data_algorithm(
859    cx: &mut js::context::JSContext,
860    bytes: Vec<u8>,
861) -> Fallible<FetchedData> {
862    // The JSON spec allows implementations to either ignore UTF-8 BOM or treat it as an error.
863    // `JS_ParseJSON` treats this as an error, so it is necessary for us to strip it if present.
864    //
865    // https://datatracker.ietf.org/doc/html/rfc8259#section-8.1
866    let json_text = decode_to_utf16_with_bom_removal(&bytes, UTF_8);
867    rooted!(&in(cx) let mut rval = UndefinedValue());
868    unsafe {
869        if !JS_ParseJSON(
870            cx,
871            json_text.as_ptr(),
872            json_text.len() as u32,
873            rval.handle_mut(),
874        ) {
875            rooted!(&in(cx) let mut exception = UndefinedValue());
876            assert!(JS_GetPendingException(cx, exception.handle_mut()));
877            JS_ClearPendingException(cx);
878            return Ok(FetchedData::JSException(RootedTraceableBox::from_box(
879                Heap::boxed(exception.get()),
880            )));
881        }
882        let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(rval.get()));
883        Ok(FetchedData::Json(rooted_heap))
884    }
885}
886
887/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A0>
888fn run_blob_data_algorithm(
889    cx: &mut js::context::JSContext,
890    root: &GlobalScope,
891    bytes: Vec<u8>,
892    mime: &[u8],
893) -> Fallible<FetchedData> {
894    let mime_string = if let Ok(s) = String::from_utf8(mime.to_vec()) {
895        s
896    } else {
897        "".to_string()
898    };
899    let blob = Blob::new(
900        cx,
901        root,
902        BlobImpl::new_from_bytes(bytes, normalize_type_string(&mime_string)),
903    );
904    Ok(FetchedData::BlobData(blob))
905}
906
907fn extract_name_from_content_disposition(headers: &HeaderMap) -> Option<String> {
908    let cd = headers.get(CONTENT_DISPOSITION)?.to_str().ok()?;
909
910    for part in cd.split(';').map(|s| s.trim()) {
911        if let Some(rest) = part.strip_prefix("name=") {
912            let v = rest.trim();
913            let v = v.strip_prefix('"').unwrap_or(v);
914            let v = v.strip_suffix('"').unwrap_or(v);
915            return Some(v.to_string());
916        }
917    }
918    None
919}
920
921fn extract_filename_from_content_disposition(headers: &HeaderMap) -> Option<String> {
922    let cd = headers.get(CONTENT_DISPOSITION)?.to_str().ok()?;
923    if let Some(index) = cd.find("filename=") {
924        let start = index + "filename=".len();
925        return Some(
926            cd.get(start..)
927                .unwrap_or_default()
928                .trim_matches('"')
929                .to_owned(),
930        );
931    }
932    if let Some(index) = cd.find("filename*=UTF-8''") {
933        let start = index + "filename*=UTF-8''".len();
934        return Some(
935            cd.get(start..)
936                .unwrap_or_default()
937                .trim_matches('"')
938                .to_owned(),
939        );
940    }
941    None
942}
943
944fn content_type_from_headers(headers: &HeaderMap) -> Result<String, Error> {
945    match headers.get(CONTENT_TYPE) {
946        Some(value) => Ok(value
947            .to_str()
948            .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?
949            .to_string()),
950        None => Ok("text/plain".to_string()),
951    }
952}
953
954fn append_form_data_entry_from_part(
955    cx: &mut js::context::JSContext,
956    root: &GlobalScope,
957    formdata: &FormData,
958    headers: &HeaderMap,
959    body: Vec<u8>,
960) -> Fallible<()> {
961    let Some(name) = extract_name_from_content_disposition(headers) else {
962        return Ok(());
963    };
964    // 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.
965    let filename = extract_filename_from_content_disposition(headers);
966    if let Some(filename) = filename {
967        // 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.
968        //
969        // The name attribute of the File object must have the value of the `filename` parameter of the part.
970        //
971        // 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.
972        let content_type = content_type_from_headers(headers)?;
973        let file = File::new(
974            root,
975            BlobImpl::new_from_bytes(body, normalize_type_string(&content_type)),
976            DOMString::from(filename),
977            None,
978            CanGc::from_cx(cx),
979        );
980        let blob = file.upcast::<Blob>();
981        formdata.Append_(USVString(name), blob, None);
982    } else {
983        // 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.
984
985        let (value, _) = UTF_8.decode_without_bom_handling(&body);
986        formdata.Append(USVString(name), USVString(value.to_string()));
987    }
988    Ok(())
989}
990
991fn append_multipart_nodes(
992    cx: &mut js::context::JSContext,
993    root: &GlobalScope,
994    formdata: &FormData,
995    nodes: Vec<Node>,
996) -> Fallible<()> {
997    for node in nodes {
998        match node {
999            Node::Part(part) => {
1000                append_form_data_entry_from_part(cx, root, formdata, &part.headers, part.body)?;
1001            },
1002            Node::File(file_part) => {
1003                let body = fs::read(&file_part.path)
1004                    .map_err(|_| Error::Type(c"file part could not be read".to_owned()))?;
1005                append_form_data_entry_from_part(cx, root, formdata, &file_part.headers, body)?;
1006            },
1007            Node::Multipart((_, inner)) => {
1008                append_multipart_nodes(cx, root, formdata, inner)?;
1009            },
1010        }
1011    }
1012    Ok(())
1013}
1014
1015/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A2>
1016fn run_form_data_algorithm(
1017    cx: &mut js::context::JSContext,
1018    root: &GlobalScope,
1019    bytes: Vec<u8>,
1020    mime: &[u8],
1021) -> Fallible<FetchedData> {
1022    // The formData() method steps are to return the result of running consume body
1023    // with this and the following steps given a byte sequence bytes:
1024    let mime_str = str::from_utf8(mime).unwrap_or_default();
1025    let mime: Mime = mime_str
1026        .parse()
1027        .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?;
1028
1029    // Let mimeType be the result of get the MIME type with this.
1030    //
1031    // If mimeType is non-null, then switch on mimeType’s essence and run the corresponding steps:
1032    if mime.type_() == mime::MULTIPART && mime.subtype() == mime::FORM_DATA {
1033        // "multipart/form-data"
1034        // Parse bytes, using the value of the `boundary` parameter from mimeType,
1035        // per the rules set forth in Returning Values from Forms: multipart/form-data. [RFC7578]
1036        let mut headers = HeaderMap::new();
1037        headers.insert(
1038            CONTENT_TYPE,
1039            mime_str
1040                .parse()
1041                .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?,
1042        );
1043
1044        if let Some(boundary) = mime.get_param(mime::BOUNDARY) {
1045            let closing_boundary = format!("--{}--", boundary.as_str()).into_bytes();
1046            let trimmed_bytes = bytes.strip_suffix(b"\r\n").unwrap_or(&bytes);
1047            if trimmed_bytes == closing_boundary {
1048                let formdata = FormData::new(None, root, CanGc::from_cx(cx));
1049                return Ok(FetchedData::FormData(formdata));
1050            }
1051        }
1052
1053        let mut cursor = Cursor::new(bytes);
1054        // If that fails for some reason, then throw a TypeError.
1055        let nodes = read_multipart_body(&mut cursor, &headers, false)
1056            .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?;
1057        // The above is a rough approximation of what is needed for `multipart/form-data`,
1058        // a more detailed parsing specification is to be written. Volunteers welcome.
1059
1060        // Return a new FormData object, appending each entry, resulting from the parsing operation, to its entry list.
1061        let formdata = FormData::new(None, root, CanGc::from_cx(cx));
1062
1063        append_multipart_nodes(cx, root, &formdata, nodes)?;
1064
1065        return Ok(FetchedData::FormData(formdata));
1066    }
1067
1068    if mime.type_() == mime::APPLICATION && mime.subtype() == mime::WWW_FORM_URLENCODED {
1069        // "application/x-www-form-urlencoded"
1070        // Let entries be the result of parsing bytes.
1071        //
1072        // Return a new FormData object whose entry list is entries.
1073        let entries = form_urlencoded::parse(&bytes);
1074        let formdata = FormData::new(None, root, CanGc::from_cx(cx));
1075        for (k, e) in entries {
1076            formdata.Append(USVString(k.into_owned()), USVString(e.into_owned()));
1077        }
1078        return Ok(FetchedData::FormData(formdata));
1079    }
1080
1081    // Throw a TypeError.
1082    Err(Error::Type(c"Inappropriate MIME-type for Body".to_owned()))
1083}
1084
1085/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body%E2%91%A1>
1086fn run_bytes_data_algorithm(
1087    cx: &mut js::context::JSContext,
1088    bytes: Vec<u8>,
1089) -> Fallible<FetchedData> {
1090    rooted!(&in(cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>());
1091
1092    create_buffer_source::<Uint8>(
1093        cx.into(),
1094        &bytes,
1095        array_buffer_ptr.handle_mut(),
1096        CanGc::from_cx(cx),
1097    )
1098    .map_err(|_| Error::JSFailed)?;
1099
1100    let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(array_buffer_ptr.get()));
1101    Ok(FetchedData::Bytes(rooted_heap))
1102}
1103
1104/// <https://fetch.spec.whatwg.org/#ref-for-concept-body-consume-body>
1105pub(crate) fn run_array_buffer_data_algorithm(
1106    cx: &mut js::context::JSContext,
1107    bytes: Vec<u8>,
1108) -> Fallible<FetchedData> {
1109    rooted!(&in(cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>());
1110
1111    create_buffer_source::<ArrayBufferU8>(
1112        cx.into(),
1113        &bytes,
1114        array_buffer_ptr.handle_mut(),
1115        CanGc::from_cx(cx),
1116    )
1117    .map_err(|_| Error::JSFailed)?;
1118
1119    let rooted_heap = RootedTraceableBox::from_box(Heap::boxed(array_buffer_ptr.get()));
1120    Ok(FetchedData::ArrayBuffer(rooted_heap))
1121}
1122
1123#[expect(unsafe_code)]
1124pub(crate) fn decode_to_utf16_with_bom_removal(
1125    bytes: &[u8],
1126    encoding: &'static Encoding,
1127) -> Vec<u16> {
1128    let mut decoder = encoding.new_decoder_with_bom_removal();
1129    let capacity = decoder
1130        .max_utf16_buffer_length(bytes.len())
1131        .expect("Overflow");
1132    let mut utf16 = Vec::with_capacity(capacity);
1133    let extra = unsafe { slice::from_raw_parts_mut(utf16.as_mut_ptr(), capacity) };
1134    let (_, read, written, _) = decoder.decode_to_utf16(bytes, extra, true);
1135    assert_eq!(read, bytes.len());
1136    unsafe { utf16.set_len(written) }
1137    utf16
1138}
1139
1140/// <https://fetch.spec.whatwg.org/#body>
1141pub(crate) trait BodyMixin {
1142    /// <https://fetch.spec.whatwg.org/#dom-body-bodyused>
1143    fn is_body_used(&self) -> bool;
1144    /// <https://fetch.spec.whatwg.org/#body-unusable>
1145    fn is_unusable(&self) -> bool;
1146    /// <https://fetch.spec.whatwg.org/#dom-body-body>
1147    fn body(&self) -> Option<DomRoot<ReadableStream>>;
1148    /// <https://fetch.spec.whatwg.org/#concept-body-mime-type>
1149    fn get_mime_type(&self, cx: &mut js::context::JSContext) -> Vec<u8>;
1150}