BodyInit.cpp 5.5 KB

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