FetchedDataReceiver.cpp 3.3 KB

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