BodyInit.cpp 4.4 KB

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