Body.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/ArrayBuffer.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/PromiseCapability.h>
  12. #include <LibJS/Runtime/TypedArray.h>
  13. #include <LibTextCodec/Decoder.h>
  14. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  15. #include <LibWeb/Bindings/HostDefined.h>
  16. #include <LibWeb/Bindings/MainThreadVM.h>
  17. #include <LibWeb/DOMURL/URLSearchParams.h>
  18. #include <LibWeb/Fetch/Body.h>
  19. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  20. #include <LibWeb/FileAPI/Blob.h>
  21. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  22. #include <LibWeb/Infra/JSON.h>
  23. #include <LibWeb/MimeSniff/MimeType.h>
  24. #include <LibWeb/Streams/ReadableStream.h>
  25. #include <LibWeb/WebIDL/Promise.h>
  26. #include <LibWeb/XHR/FormData.h>
  27. namespace Web::Fetch {
  28. BodyMixin::~BodyMixin() = default;
  29. // https://fetch.spec.whatwg.org/#body-unusable
  30. bool BodyMixin::is_unusable() const
  31. {
  32. // An object including the Body interface mixin is said to be unusable if its body is non-null and its body’s stream is disturbed or locked.
  33. auto const& body = body_impl();
  34. return body && (body->stream()->is_disturbed() || body->stream()->is_locked());
  35. }
  36. // https://fetch.spec.whatwg.org/#dom-body-body
  37. JS::GCPtr<Streams::ReadableStream> BodyMixin::body() const
  38. {
  39. // The body getter steps are to return null if this’s body is null; otherwise this’s body’s stream.
  40. auto const& body = body_impl();
  41. return body ? body->stream().ptr() : nullptr;
  42. }
  43. // https://fetch.spec.whatwg.org/#dom-body-bodyused
  44. bool BodyMixin::body_used() const
  45. {
  46. // The bodyUsed getter steps are to return true if this’s body is non-null and this’s body’s stream is disturbed; otherwise false.
  47. auto const& body = body_impl();
  48. return body && body->stream()->is_disturbed();
  49. }
  50. // https://fetch.spec.whatwg.org/#dom-body-arraybuffer
  51. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::array_buffer() const
  52. {
  53. auto& vm = Bindings::main_thread_vm();
  54. auto& realm = *vm.current_realm();
  55. // The arrayBuffer() method steps are to return the result of running consume body with this and ArrayBuffer.
  56. return consume_body(realm, *this, PackageDataType::ArrayBuffer);
  57. }
  58. // https://fetch.spec.whatwg.org/#dom-body-blob
  59. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::blob() const
  60. {
  61. auto& vm = Bindings::main_thread_vm();
  62. auto& realm = *vm.current_realm();
  63. // The blob() method steps are to return the result of running consume body with this and Blob.
  64. return consume_body(realm, *this, PackageDataType::Blob);
  65. }
  66. // https://fetch.spec.whatwg.org/#dom-body-bytes
  67. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::bytes() const
  68. {
  69. auto& vm = Bindings::main_thread_vm();
  70. auto& realm = *vm.current_realm();
  71. // The bytes() method steps are to return the result of running consume body with this and Uint8Array.
  72. return consume_body(realm, *this, PackageDataType::Uint8Array);
  73. }
  74. // https://fetch.spec.whatwg.org/#dom-body-formdata
  75. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::form_data() const
  76. {
  77. auto& vm = Bindings::main_thread_vm();
  78. auto& realm = *vm.current_realm();
  79. // The formData() method steps are to return the result of running consume body with this and FormData.
  80. return consume_body(realm, *this, PackageDataType::FormData);
  81. }
  82. // https://fetch.spec.whatwg.org/#dom-body-json
  83. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::json() const
  84. {
  85. auto& vm = Bindings::main_thread_vm();
  86. auto& realm = *vm.current_realm();
  87. // The json() method steps are to return the result of running consume body with this and JSON.
  88. return consume_body(realm, *this, PackageDataType::JSON);
  89. }
  90. // https://fetch.spec.whatwg.org/#dom-body-text
  91. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::text() const
  92. {
  93. auto& vm = Bindings::main_thread_vm();
  94. auto& realm = *vm.current_realm();
  95. // The text() method steps are to return the result of running consume body with this and text.
  96. return consume_body(realm, *this, PackageDataType::Text);
  97. }
  98. // https://fetch.spec.whatwg.org/#concept-body-package-data
  99. WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes, PackageDataType type, Optional<MimeSniff::MimeType> const& mime_type)
  100. {
  101. auto& vm = realm.vm();
  102. switch (type) {
  103. case PackageDataType::ArrayBuffer:
  104. // Return a new ArrayBuffer whose contents are bytes.
  105. return JS::ArrayBuffer::create(realm, move(bytes));
  106. case PackageDataType::Blob: {
  107. // Return a Blob whose contents are bytes and type attribute is mimeType.
  108. // NOTE: If extracting the mime type returns failure, other browsers set it to an empty string - not sure if that's spec'd.
  109. auto mime_type_string = mime_type.has_value() ? MUST(mime_type->serialized()) : String {};
  110. return FileAPI::Blob::create(realm, move(bytes), move(mime_type_string));
  111. }
  112. case PackageDataType::Uint8Array: {
  113. // Return the result of creating a Uint8Array from bytes in this’s relevant realm.
  114. auto bytes_length = bytes.size();
  115. auto array_buffer = JS::ArrayBuffer::create(realm, move(bytes));
  116. return JS::Uint8Array::create(realm, bytes_length, *array_buffer);
  117. }
  118. case PackageDataType::FormData:
  119. // If mimeType’s essence is "multipart/form-data", then:
  120. if (mime_type.has_value() && mime_type->essence() == "multipart/form-data"sv) {
  121. // FIXME: 1. Parse bytes, using the value of the `boundary` parameter from mimeType, per the rules set forth in Returning Values from Forms: multipart/form-data. [RFC7578]
  122. // FIXME: 2. If that fails for some reason, then throw a TypeError.
  123. // FIXME: 3. Return a new FormData object, appending each entry, resulting from the parsing operation, to its entry list.
  124. return JS::js_null();
  125. }
  126. // Otherwise, if mimeType’s essence is "application/x-www-form-urlencoded", then:
  127. else if (mime_type.has_value() && mime_type->essence() == "application/x-www-form-urlencoded"sv) {
  128. // 1. Let entries be the result of parsing bytes.
  129. auto entries = DOMURL::url_decode(StringView { bytes });
  130. // 2. If entries is failure, then throw a TypeError.
  131. // FIXME: Spec bug? It doesn't seem possible to throw an error here.
  132. // 3. Return a new FormData object whose entry list is entries.
  133. return TRY(XHR::FormData::create(realm, entries));
  134. }
  135. // Otherwise, throw a TypeError.
  136. else {
  137. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Mime type must be 'multipart/form-data' or 'application/x-www-form-urlencoded'"sv };
  138. }
  139. case PackageDataType::JSON:
  140. // Return the result of running parse JSON from bytes on bytes.
  141. return Infra::parse_json_bytes_to_javascript_value(realm, bytes);
  142. case PackageDataType::Text: {
  143. // Return the result of running UTF-8 decode on bytes.
  144. auto decoder = TextCodec::decoder_for("UTF-8"sv);
  145. VERIFY(decoder.has_value());
  146. auto utf8_text = MUST(TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, bytes));
  147. return JS::PrimitiveString::create(vm, move(utf8_text));
  148. }
  149. default:
  150. VERIFY_NOT_REACHED();
  151. }
  152. }
  153. // https://fetch.spec.whatwg.org/#concept-body-consume-body
  154. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> consume_body(JS::Realm& realm, BodyMixin const& object, PackageDataType type)
  155. {
  156. // 1. If object is unusable, then return a promise rejected with a TypeError.
  157. if (object.is_unusable()) {
  158. WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::TypeError, "Body is unusable"sv };
  159. return WebIDL::create_rejected_promise_from_exception(realm, move(exception));
  160. }
  161. // 2. Let promise be a new promise.
  162. auto promise = WebIDL::create_promise(realm);
  163. // 3. Let errorSteps given error be to reject promise with error.
  164. // NOTE: `promise` and `realm` is protected by JS::SafeFunction.
  165. auto error_steps = JS::create_heap_function(realm.heap(), [promise, &realm](JS::Value error) {
  166. // AD-HOC: An execution context is required for Promise's reject function.
  167. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  168. WebIDL::reject_promise(realm, promise, error);
  169. });
  170. // 4. Let successSteps given a byte sequence data be to resolve promise with the result of running convertBytesToJSValue
  171. // with data. If that threw an exception, then run errorSteps with that exception.
  172. // NOTE: `promise`, `realm` and `object` is protected by JS::SafeFunction.
  173. // FIXME: Refactor this to the new version of the spec introduced with https://github.com/whatwg/fetch/commit/464326e8eb6a602122c030cd40042480a3c0e265
  174. auto success_steps = JS::create_heap_function(realm.heap(), [promise, &realm, &object, type](ByteBuffer data) {
  175. auto& vm = realm.vm();
  176. // AD-HOC: An execution context is required for Promise's reject function and JSON.parse.
  177. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm) };
  178. auto value_or_error = Bindings::throw_dom_exception_if_needed(vm, [&]() -> WebIDL::ExceptionOr<JS::Value> {
  179. return package_data(realm, data, type, object.mime_type_impl());
  180. });
  181. if (value_or_error.is_error()) {
  182. // We can't call error_steps here without moving it into success_steps, causing a double move when we pause error_steps
  183. // to fully_read, so just reject the promise like error_steps does.
  184. WebIDL::reject_promise(realm, promise, value_or_error.release_error().value().value());
  185. return;
  186. }
  187. WebIDL::resolve_promise(realm, promise, value_or_error.release_value());
  188. });
  189. // 5. If object’s body is null, then run successSteps with an empty byte sequence.
  190. auto const& body = object.body_impl();
  191. if (!body) {
  192. success_steps->function()(ByteBuffer {});
  193. }
  194. // 6. Otherwise, fully read object’s body given successSteps, errorSteps, and object’s relevant global object.
  195. else {
  196. body->fully_read(realm, success_steps, error_steps, JS::NonnullGCPtr { HTML::relevant_global_object(object.as_platform_object()) });
  197. }
  198. // 7. Return promise.
  199. return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise->promise().ptr()) };
  200. }
  201. }