1use 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
57pub(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 let Some(stream) = original_body_stream.get() else {
66 return Ok(());
67 };
68
69 let branches = stream.tee(cx, true)?;
71 let out1 = &*branches[0];
72 let out2 = &*branches[1];
73
74 original_body_stream.set(Some(out1));
77 cloned_body_stream.set(Some(out2));
78
79 Ok(())
80}
81
82#[derive(Clone, PartialEq)]
85pub(crate) enum BodySource {
86 Null,
88 Object,
92}
93
94enum StopReading {
96 Error,
98 Done,
100}
101
102#[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 pub(crate) fn reset_in_memory_done(&mut self) {
140 self.in_memory_done = false;
141 }
142
143 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 BodyChunkRequest::Done => {
164 body_handler.stop_reading(StopReading::Done);
165 },
166 BodyChunkRequest::Error => {
169 body_handler.stop_reading(StopReading::Error);
170 },
171 }
172 }),
173 );
174 }
175
176 fn transmit_source(&mut self) {
183 if self.in_memory_done {
184 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 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 fn start_reading(&mut self, sender: IpcSender<BodyChunkResponse>) {
209 self.bytes_sender = Some(sender);
210
211 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 let rooted_stream = stream.root();
218
219 rooted_stream.acquire_default_reader(cx)
223 .expect("Couldn't acquire a reader for the body stream.");
224
225 }));
227 }
228 }
229
230 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 fn transmit_body_chunk(&mut self) {
252 if self.in_memory_done {
253 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 if let Some(bytes) = self.in_memory.clone() {
267 let _ = bytes_sender.send(BodyChunkResponse::Chunk(bytes));
268 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 let promise = rooted_stream.read_a_chunk(cx);
281
282 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#[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 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 let _ = self.control_sender.send(BodyChunkRequest::Done);
331 return self.stream.stop_reading(cx);
332 },
333 };
334
335 if is_done {
336 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 let _ = self.control_sender.send(BodyChunkRequest::Error);
347 return self.stream.stop_reading(cx);
348 },
349 };
350
351 let _ = self
355 .bytes_sender
356 .send(BodyChunkResponse::Chunk(GenericSharedMemory::from_vec(
357 chunk,
358 )));
359 }
360}
361
362#[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 fn callback(&self, cx: &mut CurrentRealm, _v: HandleValue) {
379 let _ = self.control_sender.send(BodyChunkRequest::Error);
381 self.stream.stop_reading(cx);
382 }
383}
384
385pub(crate) struct ExtractedBody {
387 pub(crate) stream: DomRoot<ReadableStream>,
389 pub(crate) source: BodySource,
391 pub(crate) total_bytes: Option<usize>,
393 pub(crate) content_type: Option<DOMString>,
395}
396
397impl ExtractedBody {
398 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 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 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 BodyChunkRequest::Done => {
464 body_handler.stop_reading(StopReading::Done);
465 },
466 BodyChunkRequest::Error => {
469 body_handler.stop_reading(StopReading::Error);
470 },
471 }
472 }),
473 );
474
475 let request_body = RequestBody::new(chunk_request_sender, net_source, total_bytes);
478
479 (request_body, stream)
481 }
482
483 pub(crate) fn in_memory(&self) -> bool {
485 self.stream.in_memory()
486 }
487}
488
489pub(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
499fn stream_from_body_init_bytes(
501 cx: &mut js::context::JSContext,
502 global: &GlobalScope,
503 bytes: Vec<u8>,
504) -> Fallible<DomRoot<ReadableStream>> {
505 ReadableStream::new_from_bytes_with_byte_reading_support(cx, global, bytes)
510}
511
512impl Extractable for BodyInit {
513 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 keep_alive {
550 return Err(Error::Type(
551 c"The body's stream is for a keepalive request".to_owned(),
552 ));
553 }
554 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 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
702pub(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 let mut realm = enter_auto_realm(cx, &*global);
716 let cx: &mut _ = &mut realm.current_realm();
717
718 let promise = Promise::new_in_realm(cx);
721
722 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 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 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 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 error_promise = promise.clone();
778
779 let mime_type = object.get_mime_type(cx);
783 let success_promise = promise.clone();
784
785 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
808fn 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
835fn 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
857fn run_text_data_algorithm(bytes: Vec<u8>) -> Fallible<FetchedData> {
859 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)]
872fn run_json_data_algorithm(
874 cx: &mut js::context::JSContext,
875 bytes: Vec<u8>,
876) -> Fallible<FetchedData> {
877 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
902fn 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 let filename = extract_filename_from_content_disposition(headers);
981 if let Some(filename) = filename {
982 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 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
1030fn run_form_data_algorithm(
1032 cx: &mut js::context::JSContext,
1033 root: &GlobalScope,
1034 bytes: Vec<u8>,
1035 mime: &[u8],
1036) -> Fallible<FetchedData> {
1037 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 if mime.type_() == mime::MULTIPART && mime.subtype() == mime::FORM_DATA {
1048 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 let nodes = read_multipart_body(&mut cursor, &headers, false)
1071 .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?;
1072 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 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 Err(Error::Type(c"Inappropriate MIME-type for Body".to_owned()))
1098}
1099
1100fn 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
1114pub(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
1145pub(crate) trait BodyMixin {
1147 fn is_body_used(&self) -> bool;
1149 fn is_unusable(&self) -> bool;
1151 fn body(&self) -> Option<DomRoot<ReadableStream>>;
1153 fn get_mime_type(&self, cx: &mut js::context::JSContext) -> Vec<u8>;
1155}
1156
1157pub(crate) fn body_text_stream<T: BodyMixin + DomObject>(
1159 cx: &mut js::context::JSContext,
1160 object: &T,
1161) -> Fallible<DomRoot<ReadableStream>> {
1162 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 let Some(stream) = object.body() else {
1171 return ReadableStream::new_empty(cx, &object.global());
1174 };
1175
1176 let decoder =
1179 TextDecoderStream::new_with_proto(cx, &object.global(), None, UTF_8, false, false)?;
1180
1181 Ok(pipe_through(&stream, cx, &object.global(), &decoder))
1183}