BodyInit.cpp 7.0 KB

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