Body.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/ArrayBuffer.h>
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/PromiseCapability.h>
  9. #include <LibWeb/Bindings/MainThreadVM.h>
  10. #include <LibWeb/Fetch/Body.h>
  11. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  12. #include <LibWeb/FileAPI/Blob.h>
  13. #include <LibWeb/Infra/JSON.h>
  14. #include <LibWeb/MimeSniff/MimeType.h>
  15. #include <LibWeb/Streams/ReadableStream.h>
  16. #include <LibWeb/WebIDL/Promise.h>
  17. namespace Web::Fetch {
  18. BodyMixin::~BodyMixin() = default;
  19. // https://fetch.spec.whatwg.org/#body-unusable
  20. bool BodyMixin::is_unusable() const
  21. {
  22. // 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.
  23. auto const& body = body_impl();
  24. return body.has_value() && (body->stream()->is_disturbed() || body->stream()->is_locked());
  25. }
  26. // https://fetch.spec.whatwg.org/#dom-body-body
  27. JS::GCPtr<Streams::ReadableStream> BodyMixin::body() const
  28. {
  29. // The body getter steps are to return null if this’s body is null; otherwise this’s body’s stream.
  30. auto const& body = body_impl();
  31. return body.has_value() ? body->stream().ptr() : nullptr;
  32. }
  33. // https://fetch.spec.whatwg.org/#dom-body-bodyused
  34. bool BodyMixin::body_used() const
  35. {
  36. // 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.
  37. auto const& body = body_impl();
  38. return body.has_value() && body->stream()->is_disturbed();
  39. }
  40. // https://fetch.spec.whatwg.org/#dom-body-arraybuffer
  41. JS::NonnullGCPtr<JS::Promise> BodyMixin::array_buffer() const
  42. {
  43. auto& vm = Bindings::main_thread_vm();
  44. auto& realm = *vm.current_realm();
  45. // The arrayBuffer() method steps are to return the result of running consume body with this and ArrayBuffer.
  46. return consume_body(realm, *this, PackageDataType::ArrayBuffer);
  47. }
  48. // https://fetch.spec.whatwg.org/#dom-body-blob
  49. JS::NonnullGCPtr<JS::Promise> BodyMixin::blob() const
  50. {
  51. auto& vm = Bindings::main_thread_vm();
  52. auto& realm = *vm.current_realm();
  53. // The blob() method steps are to return the result of running consume body with this and Blob.
  54. return consume_body(realm, *this, PackageDataType::Blob);
  55. }
  56. // https://fetch.spec.whatwg.org/#dom-body-formdata
  57. JS::NonnullGCPtr<JS::Promise> BodyMixin::form_data() const
  58. {
  59. auto& vm = Bindings::main_thread_vm();
  60. auto& realm = *vm.current_realm();
  61. // The formData() method steps are to return the result of running consume body with this and FormData.
  62. return consume_body(realm, *this, PackageDataType::FormData);
  63. }
  64. // https://fetch.spec.whatwg.org/#dom-body-json
  65. JS::NonnullGCPtr<JS::Promise> BodyMixin::json() const
  66. {
  67. auto& vm = Bindings::main_thread_vm();
  68. auto& realm = *vm.current_realm();
  69. // The json() method steps are to return the result of running consume body with this and JSON.
  70. return consume_body(realm, *this, PackageDataType::JSON);
  71. }
  72. // https://fetch.spec.whatwg.org/#dom-body-text
  73. JS::NonnullGCPtr<JS::Promise> BodyMixin::text() const
  74. {
  75. auto& vm = Bindings::main_thread_vm();
  76. auto& realm = *vm.current_realm();
  77. // The text() method steps are to return the result of running consume body with this and text.
  78. return consume_body(realm, *this, PackageDataType::Text);
  79. }
  80. // https://fetch.spec.whatwg.org/#concept-body-package-data
  81. WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes, PackageDataType type, Optional<MimeSniff::MimeType> const& mime_type)
  82. {
  83. auto& vm = realm.vm();
  84. switch (type) {
  85. case PackageDataType::ArrayBuffer:
  86. // Return a new ArrayBuffer whose contents are bytes.
  87. return JS::ArrayBuffer::create(realm, move(bytes));
  88. case PackageDataType::Blob: {
  89. // Return a Blob whose contents are bytes and type attribute is mimeType.
  90. // NOTE: If extracting the mime type returns failure, other browsers set it to an empty string - not sure if that's spec'd.
  91. auto mime_type_string = mime_type.has_value() ? mime_type->serialized() : String::empty();
  92. return FileAPI::Blob::create(realm, move(bytes), move(mime_type_string));
  93. }
  94. case PackageDataType::FormData:
  95. // If mimeType’s essence is "multipart/form-data", then:
  96. if (mime_type.has_value() && mime_type->essence() == "multipart/form-data"sv) {
  97. // 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]
  98. // FIXME: 2. If that fails for some reason, then throw a TypeError.
  99. // FIXME: 3. Return a new FormData object, appending each entry, resulting from the parsing operation, to its entry list.
  100. return JS::js_null();
  101. }
  102. // Otherwise, if mimeType’s essence is "application/x-www-form-urlencoded", then:
  103. else if (mime_type.has_value() && mime_type->essence() == "application/x-www-form-urlencoded"sv) {
  104. // FIXME: 1. Let entries be the result of parsing bytes.
  105. // FIXME: 2. If entries is failure, then throw a TypeError.
  106. // FIXME: 3. Return a new FormData object whose entry list is entries.
  107. return JS::js_null();
  108. }
  109. // Otherwise, throw a TypeError.
  110. else {
  111. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Mime type must be 'multipart/form-data' or 'application/x-www-form-urlencoded'"sv };
  112. }
  113. case PackageDataType::JSON:
  114. // Return the result of running parse JSON from bytes on bytes.
  115. return Infra::parse_json_bytes_to_javascript_value(vm, bytes);
  116. case PackageDataType::Text:
  117. // Return the result of running UTF-8 decode on bytes.
  118. return JS::js_string(vm, String::copy(bytes));
  119. default:
  120. VERIFY_NOT_REACHED();
  121. }
  122. }
  123. // https://fetch.spec.whatwg.org/#concept-body-consume-body
  124. JS::NonnullGCPtr<JS::Promise> consume_body(JS::Realm& realm, BodyMixin const& object, PackageDataType type)
  125. {
  126. auto& vm = realm.vm();
  127. // 1. If object is unusable, then return a promise rejected with a TypeError.
  128. if (object.is_unusable()) {
  129. auto promise_capability = WebIDL::create_rejected_promise(realm, JS::TypeError::create(realm, "Body is unusable"sv));
  130. return verify_cast<JS::Promise>(*promise_capability->promise().ptr());
  131. }
  132. // 2. Let promise be a promise resolved with an empty byte sequence.
  133. auto promise = WebIDL::create_resolved_promise(realm, JS::js_string(vm, String::empty()));
  134. // 3. If object’s body is non-null, then set promise to the result of fully reading body as promise given object’s body.
  135. auto const& body = object.body_impl();
  136. if (body.has_value())
  137. promise = body->fully_read_as_promise();
  138. // 4. Let steps be to return the result of package data with the first argument given, type, and object’s MIME type.
  139. auto steps = [&realm, &object, type](JS::Value value) -> WebIDL::ExceptionOr<JS::Value> {
  140. VERIFY(value.is_string());
  141. auto bytes = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(value.as_string().string().bytes()));
  142. return package_data(realm, move(bytes), type, object.mime_type_impl());
  143. };
  144. // 5. Return the result of upon fulfillment of promise given steps.
  145. return WebIDL::upon_fulfillment(promise, move(steps));
  146. }
  147. }