BodyInit.cpp 4.5 KB

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