BodyInit.cpp 5.9 KB

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