BodyInit.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/Bindings/IDLAbstractOperations.h>
  8. #include <LibWeb/Fetch/BodyInit.h>
  9. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  10. #include <LibWeb/HTML/Window.h>
  11. #include <LibWeb/URL/URLSearchParams.h>
  12. namespace Web::Fetch {
  13. // https://fetch.spec.whatwg.org/#concept-bodyinit-extract
  14. // FIXME: The parameter 'body_init' should be 'typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit'. For now we just let it be 'XMLHttpRequestBodyInit'.
  15. ErrorOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, XMLHttpRequestBodyInit const& body_init)
  16. {
  17. auto& window = verify_cast<HTML::Window>(realm.global_object());
  18. // FIXME: 1. Let stream be object if object is a ReadableStream object. Otherwise, let stream be a new ReadableStream, and set up stream.
  19. auto* stream = realm.heap().allocate<Streams::ReadableStream>(realm, window);
  20. // FIXME: 2. Let action be null.
  21. // 3. Let source be null.
  22. Infrastructure::Body::SourceType source {};
  23. // 4. Let length be null.
  24. Optional<u64> length {};
  25. // 5. Let type be null.
  26. Optional<ByteBuffer> type {};
  27. // 6. Switch on object.
  28. // FIXME: Still need to support BufferSource and FormData
  29. TRY(body_init.visit(
  30. [&](JS::Handle<FileAPI::Blob> const& blob) -> ErrorOr<void> {
  31. // FIXME: Set action to this step: read object.
  32. // Set source to object.
  33. source = blob;
  34. // Set length to object’s size.
  35. length = blob->size();
  36. // If object’s type attribute is not the empty byte sequence, set type to its value.
  37. if (!blob->type().is_empty())
  38. type = blob->type().to_byte_buffer();
  39. return {};
  40. },
  41. [&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
  42. // Set source to a copy of the bytes held by object.
  43. source = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
  44. return {};
  45. },
  46. [&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> ErrorOr<void> {
  47. // Set source to the result of running the application/x-www-form-urlencoded serializer with object’s list.
  48. source = url_search_params->to_string().to_byte_buffer();
  49. // Set type to `application/x-www-form-urlencoded;charset=UTF-8`.
  50. type = TRY(ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
  51. return {};
  52. },
  53. [&](String const& scalar_value_string) -> ErrorOr<void> {
  54. // NOTE: AK::String is always UTF-8.
  55. // Set source to the UTF-8 encoding of object.
  56. source = scalar_value_string.to_byte_buffer();
  57. // Set type to `text/plain;charset=UTF-8`.
  58. type = TRY(ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
  59. return {};
  60. }));
  61. // FIXME: 7. If source is a byte sequence, then set action to a step that returns source and length to source’s length.
  62. // FIXME: 8. If action is non-null, then run these steps in in parallel:
  63. // 9. Let body be a body whose stream is stream, source is source, and length is length.
  64. auto body = Infrastructure::Body { JS::make_handle(stream), move(source), move(length) };
  65. // 10. Return (body, type).
  66. return Infrastructure::BodyWithType { .body = move(body), .type = move(type) };
  67. }
  68. }