FetchedDataReceiver.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/HeapFunction.h>
  7. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  8. #include <LibWeb/Bindings/HostDefined.h>
  9. #include <LibWeb/Fetch/Fetching/FetchedDataReceiver.h>
  10. #include <LibWeb/Fetch/Infrastructure/FetchParams.h>
  11. #include <LibWeb/Fetch/Infrastructure/Task.h>
  12. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  13. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  14. #include <LibWeb/Streams/AbstractOperations.h>
  15. #include <LibWeb/WebIDL/Promise.h>
  16. namespace Web::Fetch::Fetching {
  17. JS_DEFINE_ALLOCATOR(FetchedDataReceiver);
  18. FetchedDataReceiver::FetchedDataReceiver(JS::NonnullGCPtr<Infrastructure::FetchParams const> fetch_params, JS::NonnullGCPtr<Streams::ReadableStream> stream)
  19. : m_fetch_params(fetch_params)
  20. , m_stream(stream)
  21. {
  22. }
  23. FetchedDataReceiver::~FetchedDataReceiver() = default;
  24. void FetchedDataReceiver::visit_edges(Visitor& visitor)
  25. {
  26. Base::visit_edges(visitor);
  27. visitor.visit(m_fetch_params);
  28. visitor.visit(m_stream);
  29. visitor.visit(m_pending_promise);
  30. }
  31. void FetchedDataReceiver::set_pending_promise(JS::NonnullGCPtr<WebIDL::Promise> promise)
  32. {
  33. auto had_pending_promise = m_pending_promise != nullptr;
  34. m_pending_promise = promise;
  35. if (!had_pending_promise && !m_buffer.is_empty()) {
  36. on_data_received(m_buffer);
  37. m_buffer.clear();
  38. }
  39. }
  40. // This implements the parallel steps of the pullAlgorithm in HTTP-network-fetch.
  41. // https://fetch.spec.whatwg.org/#ref-for-in-parallel④
  42. void FetchedDataReceiver::on_data_received(ReadonlyBytes bytes)
  43. {
  44. // FIXME: 1. If the size of buffer is smaller than a lower limit chosen by the user agent and the ongoing fetch
  45. // is suspended, resume the fetch.
  46. // FIXME: 2. Wait until buffer is not empty.
  47. // If the remote end sends data immediately after we receive headers, we will often get that data here before the
  48. // stream tasks have all been queued internally. Just hold onto that data.
  49. if (!m_pending_promise) {
  50. m_buffer.append(bytes);
  51. return;
  52. }
  53. // 3. Queue a fetch task to run the following steps, with fetchParams’s task destination.
  54. Infrastructure::queue_fetch_task(
  55. m_fetch_params->controller(),
  56. m_fetch_params->task_destination().get<JS::NonnullGCPtr<JS::Object>>(),
  57. JS::create_heap_function(heap(), [this, bytes = MUST(ByteBuffer::copy(bytes))]() mutable {
  58. HTML::TemporaryExecutionContext execution_context { m_stream->realm(), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  59. // 1. Pull from bytes buffer into stream.
  60. if (auto result = Streams::readable_stream_pull_from_bytes(m_stream, move(bytes)); result.is_error()) {
  61. auto throw_completion = Bindings::dom_exception_to_throw_completion(m_stream->vm(), result.release_error());
  62. dbgln("FetchedDataReceiver: Stream error pulling bytes");
  63. HTML::report_exception(throw_completion, m_stream->realm());
  64. return;
  65. }
  66. // 2. If stream is errored, then terminate fetchParams’s controller.
  67. if (m_stream->is_errored())
  68. m_fetch_params->controller()->terminate();
  69. // 3. Resolve promise with undefined.
  70. WebIDL::resolve_promise(m_stream->realm(), *m_pending_promise, JS::js_undefined());
  71. }));
  72. }
  73. }