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/Buffers.h>
  14. #include <LibWeb/WebIDL/ExceptionOr.h>
  15. #include <LibWeb/XHR/FormData.h>
  16. namespace Web::Fetch {
  17. // https://fetch.spec.whatwg.org/#bodyinit-safely-extract
  18. WebIDL::ExceptionOr<Infrastructure::BodyWithType> safely_extract_body(JS::Realm& realm, BodyInitOrReadableBytes const& object)
  19. {
  20. // 1. If object is a ReadableStream object, then:
  21. if (auto const* stream = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
  22. // 1. Assert: object is neither disturbed nor locked.
  23. VERIFY(!((*stream)->is_disturbed() || (*stream)->is_locked()));
  24. }
  25. // 2. Return the result of extracting object.
  26. return extract_body(realm, object);
  27. }
  28. // https://fetch.spec.whatwg.org/#concept-bodyinit-extract
  29. WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInitOrReadableBytes const& object, bool keepalive)
  30. {
  31. auto& vm = realm.vm();
  32. // 1. Let stream be null.
  33. JS::GCPtr<Streams::ReadableStream> stream;
  34. // 2. If object is a ReadableStream object, then set stream to object.
  35. if (auto const* stream_handle = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
  36. stream = const_cast<Streams::ReadableStream*>(stream_handle->cell());
  37. }
  38. // 3. Otherwise, if object is a Blob object, set stream to the result of running object’s get stream.
  39. else if (auto const* blob_handle = object.get_pointer<JS::Handle<FileAPI::Blob>>()) {
  40. // FIXME: "set stream to the result of running object’s get stream"
  41. (void)blob_handle;
  42. stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
  43. }
  44. // 4. Otherwise, set stream to a new ReadableStream object, and set up stream.
  45. else {
  46. // FIXME: "set up stream"
  47. stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
  48. }
  49. // 5. Assert: stream is a ReadableStream object.
  50. VERIFY(stream);
  51. // FIXME: 6. Let action be null.
  52. // 7. Let source be null.
  53. Infrastructure::Body::SourceType source {};
  54. // 8. Let length be null.
  55. Optional<u64> length {};
  56. // 9. Let type be null.
  57. Optional<ByteBuffer> type {};
  58. // 10. Switch on object.
  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<WebIDL::BufferSource> 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->raw_object()));
  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::create(vm, *stream, move(source), move(length));
  122. // 14. Return (body, type).
  123. return Infrastructure::BodyWithType { .body = move(body), .type = move(type) };
  124. }
  125. }