BodyInit.cpp 4.3 KB

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