Body.cpp 10 KB

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