Bodies.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/PromiseCapability.h>
  7. #include <LibWeb/Bindings/MainThreadVM.h>
  8. #include <LibWeb/Fetch/BodyInit.h>
  9. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  10. #include <LibWeb/Fetch/Infrastructure/Task.h>
  11. #include <LibWeb/WebIDL/Promise.h>
  12. namespace Web::Fetch::Infrastructure {
  13. JS::NonnullGCPtr<Body> Body::create(JS::VM& vm, JS::NonnullGCPtr<Streams::ReadableStream> stream)
  14. {
  15. return vm.heap().allocate_without_realm<Body>(stream);
  16. }
  17. JS::NonnullGCPtr<Body> Body::create(JS::VM& vm, JS::NonnullGCPtr<Streams::ReadableStream> stream, SourceType source, Optional<u64> length)
  18. {
  19. return vm.heap().allocate_without_realm<Body>(stream, source, length);
  20. }
  21. Body::Body(JS::NonnullGCPtr<Streams::ReadableStream> stream)
  22. : m_stream(move(stream))
  23. {
  24. }
  25. Body::Body(JS::NonnullGCPtr<Streams::ReadableStream> stream, SourceType source, Optional<u64> length)
  26. : m_stream(move(stream))
  27. , m_source(move(source))
  28. , m_length(move(length))
  29. {
  30. }
  31. void Body::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_stream);
  35. }
  36. // https://fetch.spec.whatwg.org/#concept-body-clone
  37. JS::NonnullGCPtr<Body> Body::clone(JS::Realm& realm) const
  38. {
  39. // To clone a body body, run these steps:
  40. // FIXME: 1. Let « out1, out2 » be the result of teeing body’s stream.
  41. // FIXME: 2. Set body’s stream to out1.
  42. auto out2 = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
  43. // 3. Return a body whose stream is out2 and other members are copied from body.
  44. return Body::create(realm.vm(), out2, m_source, m_length);
  45. }
  46. // https://fetch.spec.whatwg.org/#body-fully-read
  47. WebIDL::ExceptionOr<void> Body::fully_read(JS::Realm& realm, Web::Fetch::Infrastructure::Body::ProcessBodyCallback process_body, Web::Fetch::Infrastructure::Body::ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const
  48. {
  49. auto& vm = realm.vm();
  50. // FIXME: 1. If taskDestination is null, then set taskDestination to the result of starting a new parallel queue.
  51. // FIXME: Handle 'parallel queue' task destination
  52. VERIFY(!task_destination.has<Empty>());
  53. auto task_destination_object = task_destination.get<JS::NonnullGCPtr<JS::Object>>();
  54. // 2. Let successSteps given a byte sequence bytes be to queue a fetch task to run processBody given bytes, with taskDestination.
  55. auto success_steps = [process_body = move(process_body), task_destination_object = JS::make_handle(task_destination_object)](ByteBuffer const& bytes) mutable -> ErrorOr<void> {
  56. // Make a copy of the bytes, as the source of the bytes may disappear between the time the task is queued and executed.
  57. auto bytes_copy = TRY(ByteBuffer::copy(bytes));
  58. queue_fetch_task(*task_destination_object, [process_body = move(process_body), bytes_copy = move(bytes_copy)]() {
  59. process_body(move(bytes_copy));
  60. });
  61. return {};
  62. };
  63. // 3. Let errorSteps optionally given an exception exception be to queue a fetch task to run processBodyError given exception, with taskDestination.
  64. auto error_steps = [process_body_error = move(process_body_error), task_destination_object = JS::make_handle(task_destination_object)](JS::GCPtr<WebIDL::DOMException> exception) mutable {
  65. queue_fetch_task(*task_destination_object, [process_body_error = move(process_body_error), exception = JS::make_handle(exception)]() {
  66. process_body_error(*exception);
  67. });
  68. };
  69. // 4. Let reader be the result of getting a reader for body’s stream. If that threw an exception, then run errorSteps with that exception and return.
  70. // 5. Read all bytes from reader, given successSteps and errorSteps.
  71. // FIXME: Implement the streams spec - this is completely made up for now :^)
  72. if (auto const* byte_buffer = m_source.get_pointer<ByteBuffer>()) {
  73. TRY_OR_THROW_OOM(vm, success_steps(*byte_buffer));
  74. } else if (auto const* blob_handle = m_source.get_pointer<JS::Handle<FileAPI::Blob>>()) {
  75. auto byte_buffer = TRY_OR_THROW_OOM(vm, ByteBuffer::copy((*blob_handle)->bytes()));
  76. TRY_OR_THROW_OOM(vm, success_steps(move(byte_buffer)));
  77. } else {
  78. // Empty, Blob, FormData
  79. error_steps(WebIDL::DOMException::create(realm, "DOMException"_fly_string, "Reading from Blob, FormData or null source is not yet implemented"_fly_string));
  80. }
  81. return {};
  82. }
  83. // https://fetch.spec.whatwg.org/#byte-sequence-as-a-body
  84. WebIDL::ExceptionOr<JS::NonnullGCPtr<Body>> byte_sequence_as_body(JS::Realm& realm, ReadonlyBytes bytes)
  85. {
  86. // To get a byte sequence bytes as a body, return the body of the result of safely extracting bytes.
  87. auto [body, _] = TRY(safely_extract_body(realm, bytes));
  88. return body;
  89. }
  90. }