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