BodyInit.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/Completion.h>
  8. #include <LibWeb/Fetch/BodyInit.h>
  9. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  10. #include <LibWeb/HTML/FormControlInfrastructure.h>
  11. #include <LibWeb/URL/URLSearchParams.h>
  12. #include <LibWeb/WebIDL/AbstractOperations.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. #include <LibWeb/XHR/FormData.h>
  15. namespace Web::Fetch {
  16. // https://fetch.spec.whatwg.org/#bodyinit-safely-extract
  17. WebIDL::ExceptionOr<Infrastructure::BodyWithType> safely_extract_body(JS::Realm& realm, BodyInitOrReadableBytes const& object)
  18. {
  19. // 1. If object is a ReadableStream object, then:
  20. if (auto const* stream = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
  21. // 1. Assert: object is neither disturbed nor locked.
  22. VERIFY(!((*stream)->is_disturbed() || (*stream)->is_locked()));
  23. }
  24. // 2. Return the result of extracting object.
  25. return extract_body(realm, object);
  26. }
  27. // https://fetch.spec.whatwg.org/#concept-bodyinit-extract
  28. WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInitOrReadableBytes const& object, bool keepalive)
  29. {
  30. auto& vm = realm.vm();
  31. // 1. Let stream be null.
  32. JS::GCPtr<Streams::ReadableStream> stream;
  33. // 2. If object is a ReadableStream object, then set stream to object.
  34. if (auto const* stream_handle = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
  35. stream = const_cast<Streams::ReadableStream*>(stream_handle->cell());
  36. }
  37. // 3. Otherwise, if object is a Blob object, set stream to the result of running object’s get stream.
  38. else if (auto const* blob_handle = object.get_pointer<JS::Handle<FileAPI::Blob>>()) {
  39. // FIXME: "set stream to the result of running object’s get stream"
  40. (void)blob_handle;
  41. stream = MUST_OR_THROW_OOM(realm.heap().allocate<Streams::ReadableStream>(realm, realm));
  42. }
  43. // 4. Otherwise, set stream to a new ReadableStream object, and set up stream.
  44. else {
  45. // FIXME: "set up stream"
  46. stream = MUST_OR_THROW_OOM(realm.heap().allocate<Streams::ReadableStream>(realm, realm));
  47. }
  48. // 5. Assert: stream is a ReadableStream object.
  49. VERIFY(stream);
  50. // FIXME: 6. Let action be null.
  51. // 7. Let source be null.
  52. Infrastructure::Body::SourceType source {};
  53. // 8. Let length be null.
  54. Optional<u64> length {};
  55. // 9. Let type be null.
  56. Optional<ByteBuffer> type {};
  57. // 10. Switch on object.
  58. // FIXME: Still need to support BufferSource
  59. TRY(object.visit(
  60. [&](JS::Handle<FileAPI::Blob> const& blob) -> WebIDL::ExceptionOr<void> {
  61. // Set source to object.
  62. source = blob;
  63. // Set length to object’s size.
  64. length = blob->size();
  65. // If object’s type attribute is not the empty byte sequence, set type to its value.
  66. if (!blob->type().is_empty())
  67. type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(blob->type().bytes()));
  68. return {};
  69. },
  70. [&](ReadonlyBytes bytes) -> WebIDL::ExceptionOr<void> {
  71. // Set source to object.
  72. source = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(bytes));
  73. return {};
  74. },
  75. [&](JS::Handle<JS::Object> const& buffer_source) -> WebIDL::ExceptionOr<void> {
  76. // Set source to a copy of the bytes held by object.
  77. source = TRY_OR_THROW_OOM(vm, WebIDL::get_buffer_source_copy(*buffer_source.cell()));
  78. return {};
  79. },
  80. [&](JS::Handle<XHR::FormData> const& form_data) -> WebIDL::ExceptionOr<void> {
  81. // Set action to this step: run the multipart/form-data encoding algorithm, with object’s entry list and UTF-8.
  82. auto serialized_form_data = TRY_OR_THROW_OOM(vm, HTML::serialize_to_multipart_form_data(form_data->entry_list()));
  83. // Set source to object.
  84. source = serialized_form_data.serialized_data;
  85. // FIXME: Set length to unclear, see html/6424 for improving this.
  86. // Set type to `multipart/form-data; boundary=`, followed by the multipart/form-data boundary string generated by the multipart/form-data encoding algorithm.
  87. type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(TRY_OR_THROW_OOM(vm, String::formatted("multipart/form-data; boundary={}"sv, serialized_form_data.boundary)).bytes()));
  88. return {};
  89. },
  90. [&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> WebIDL::ExceptionOr<void> {
  91. // Set source to the result of running the application/x-www-form-urlencoded serializer with object’s list.
  92. auto search_params_bytes = TRY(url_search_params->to_string()).bytes();
  93. source = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(search_params_bytes));
  94. // Set type to `application/x-www-form-urlencoded;charset=UTF-8`.
  95. type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
  96. return {};
  97. },
  98. [&](String const& scalar_value_string) -> WebIDL::ExceptionOr<void> {
  99. // NOTE: AK::String is always UTF-8.
  100. // Set source to the UTF-8 encoding of object.
  101. source = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(scalar_value_string.bytes()));
  102. // Set type to `text/plain;charset=UTF-8`.
  103. type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
  104. return {};
  105. },
  106. [&](JS::Handle<Streams::ReadableStream> const& stream) -> WebIDL::ExceptionOr<void> {
  107. // If keepalive is true, then throw a TypeError.
  108. if (keepalive)
  109. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot extract body from stream when keepalive is set"sv };
  110. // If object is disturbed or locked, then throw a TypeError.
  111. if (stream->is_disturbed() || stream->is_locked())
  112. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot extract body from disturbed or locked stream"sv };
  113. return {};
  114. }));
  115. // FIXME: 11. If source is a byte sequence, then set action to a step that returns source and length to source’s length.
  116. // For now, do it synchronously.
  117. if (source.has<ByteBuffer>())
  118. length = source.get<ByteBuffer>().size();
  119. // FIXME: 12. If action is non-null, then run these steps in parallel:
  120. // 13. Let body be a body whose stream is stream, source is source, and length is length.
  121. auto body = Infrastructure::Body { JS::make_handle(*stream), move(source), move(length) };
  122. // 14. Return (body, type).
  123. return Infrastructure::BodyWithType { .body = move(body), .type = move(type) };
  124. }
  125. }