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::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
54pub(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 let Some(stream) = original_body_stream.get() else {
63 return Ok(());
64 };
65
66 let branches = stream.tee(cx, true)?;
68 let out1 = &*branches[0];
69 let out2 = &*branches[1];
70
71 original_body_stream.set(Some(out1));
74 cloned_body_stream.set(Some(out2));
75
76 Ok(())
77}
78
79#[derive(Clone, PartialEq)]
82pub(crate) enum BodySource {
83 Null,
85 Object,
89}
90
91enum StopReading {
93 Error,
95 Done,
97}
98
99#[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 pub(crate) fn reset_in_memory_done(&mut self) {
137 self.in_memory_done = false;
138 }
139
140 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 BodyChunkRequest::Done => {
161 body_handler.stop_reading(StopReading::Done);
162 },
163 BodyChunkRequest::Error => {
166 body_handler.stop_reading(StopReading::Error);
167 },
168 }
169 }),
170 );
171 }
172
173 fn transmit_source(&mut self) {
180 if self.in_memory_done {
181 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 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 fn start_reading(&mut self, sender: IpcSender<BodyChunkResponse>) {
206 self.bytes_sender = Some(sender);
207
208 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 let rooted_stream = stream.root();
215
216 rooted_stream.acquire_default_reader(CanGc::from_cx(cx))
220 .expect("Couldn't acquire a reader for the body stream.");
221
222 }));
224 }
225 }
226
227 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 fn transmit_body_chunk(&mut self) {
249 if self.in_memory_done {
250 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 if let Some(bytes) = self.in_memory.clone() {
264 let _ = bytes_sender.send(BodyChunkResponse::Chunk(bytes));
265 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 let promise = rooted_stream.read_a_chunk(cx);
278
279 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#[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 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 let _ = self.control_sender.send(BodyChunkRequest::Done);
328 return self.stream.stop_reading(cx);
329 },
330 };
331
332 if is_done {
333 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 let _ = self.control_sender.send(BodyChunkRequest::Error);
344 return self.stream.stop_reading(cx);
345 },
346 };
347
348 let _ = self
352 .bytes_sender
353 .send(BodyChunkResponse::Chunk(GenericSharedMemory::from_vec(
354 chunk,
355 )));
356 }
357}
358
359#[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 fn callback(&self, cx: &mut CurrentRealm, _v: HandleValue) {
376 let _ = self.control_sender.send(BodyChunkRequest::Error);
378 self.stream.stop_reading(cx);
379 }
380}
381
382pub(crate) struct ExtractedBody {
384 pub(crate) stream: DomRoot<ReadableStream>,
386 pub(crate) source: BodySource,
388 pub(crate) total_bytes: Option<usize>,
390 pub(crate) content_type: Option<DOMString>,
392}
393
394impl ExtractedBody {
395 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 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 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 BodyChunkRequest::Done => {
460 body_handler.stop_reading(StopReading::Done);
461 },
462 BodyChunkRequest::Error => {
465 body_handler.stop_reading(StopReading::Error);
466 },
467 }
468 }),
469 );
470
471 let request_body = RequestBody::new(chunk_request_sender, net_source, total_bytes);
474
475 (request_body, stream)
477 }
478
479 pub(crate) fn in_memory(&self) -> bool {
481 self.stream.in_memory()
482 }
483}
484
485pub(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 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 keep_alive {
533 return Err(Error::Type(
534 c"The body's stream is for a keepalive request".to_owned(),
535 ));
536 }
537 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 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
685pub(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 let mut realm = enter_auto_realm(cx, &*global);
699 let cx: &mut _ = &mut realm.current_realm();
700
701 let promise = Promise::new_in_realm(cx);
704
705 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 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 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 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 error_promise = promise.clone();
761
762 let mime_type = object.get_mime_type(cx);
766 let success_promise = promise.clone();
767
768 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
791fn 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
820fn 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
842fn run_text_data_algorithm(bytes: Vec<u8>) -> Fallible<FetchedData> {
844 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)]
857fn run_json_data_algorithm(
859 cx: &mut js::context::JSContext,
860 bytes: Vec<u8>,
861) -> Fallible<FetchedData> {
862 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
887fn 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 let filename = extract_filename_from_content_disposition(headers);
966 if let Some(filename) = filename {
967 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 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
1015fn run_form_data_algorithm(
1017 cx: &mut js::context::JSContext,
1018 root: &GlobalScope,
1019 bytes: Vec<u8>,
1020 mime: &[u8],
1021) -> Fallible<FetchedData> {
1022 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 if mime.type_() == mime::MULTIPART && mime.subtype() == mime::FORM_DATA {
1033 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 let nodes = read_multipart_body(&mut cursor, &headers, false)
1056 .map_err(|_| Error::Type(c"Inappropriate MIME-type for Body".to_owned()))?;
1057 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 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 Err(Error::Type(c"Inappropriate MIME-type for Body".to_owned()))
1083}
1084
1085fn 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
1104pub(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
1140pub(crate) trait BodyMixin {
1142 fn is_body_used(&self) -> bool;
1144 fn is_unusable(&self) -> bool;
1146 fn body(&self) -> Option<DomRoot<ReadableStream>>;
1148 fn get_mime_type(&self, cx: &mut js::context::JSContext) -> Vec<u8>;
1150}