dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / fetch / fetching / fetched_data_receiver.lucb
#==============================================================================================
#
# fetched_data_receiver - Fetch::Fetching::FetchedDataReceiver: the network's bytes into a
# response body's stream
#
# DESCRIPTION:
# Port of Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp (lines 1-159) with
# FetchedDataReceiver.h's set_response. The receiver implements the parallel steps of
# HTTP-network fetch's pullAlgorithm: it keeps the first bytes for MIME sniffing until the
# body exists, enqueues each chunk into the stream as a Uint8Array, closes the stream when
# the transmission is done, and finalizes the memory cache's entry (fetching_7 makes it).
#
# Copyright (c) 2024-2026, Tim Flynn <trflynn89@ladybird.org>
# Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp:27 Web::Fetch::Fetching::FetchedDataReceiver::FetchedDataReceiver (private).
func fetch_fetched_data_receiver_construct(this: FetchFetchedDataReceiver*, fetch_params: FetchParams*, stream: StreamsReadableStream*, http_cache: HttpMemoryCache*?):
this.m_fetch_params = fetch_params
this.m_stream = stream
this.m_http_cache = http_cache
## Ported from Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.h:25 Web::Fetch::Fetching::FetchedDataReceiver::set_response.
pub func fetch_fetched_data_receiver_set_response(this: FetchFetchedDataReceiver*, response: FetchResponse*):
this.m_response = response
## Ported from Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp:36 Web::Fetch::Fetching::FetchedDataReceiver::set_body.
pub func fetch_fetched_data_receiver_set_body(this: FetchFetchedDataReceiver*, body: FetchBody*):
this.m_body = body
# Flush any bytes that were buffered before the body was set
if not ak.byte_buffer_is_empty(&this.m_pre_body_sniff_buffer):
fetch_body_append_sniff_bytes(body, ak.byte_buffer_bytes_const(&this.m_pre_body_sniff_buffer))
ak.byte_buffer_clear(&this.m_pre_body_sniff_buffer)
# If the stream already completed before the body was set,
# we missed the set_sniff_bytes_complete() call in handle_network_bytes.
if this.m_network_complete:
fetch_body_set_sniff_bytes_complete(body)
## Ported from Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp:50 Web::Fetch::Fetching::FetchedDataReceiver::visit_edges (override).
pub func fetch_fetched_data_receiver_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (FetchFetchedDataReceiver*)base
gc.cell_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2(visitor, this.m_fetch_params)
gc.cell_visitor_visit_t(visitor, this.m_response)
gc.cell_visitor_visit_t(visitor, this.m_body)
gc.cell_visitor_visit_t_2(visitor, this.m_stream)
## The deferred invocation of handle_network_bytes once the transmission is done, `[this]() { close_stream(); }`.
func fetch_fetched_data_receiver_close_stream_task(context: void*?):
fetch_fetched_data_receiver_close_stream((FetchFetchedDataReceiver*)(context else trap("handle_network_bytes: no receiver")))
## Ported from Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp:61 Web::Fetch::Fetching::FetchedDataReceiver::handle_network_bytes.
# This implements the parallel steps of the pullAlgorithm in HTTP-network-fetch.
# https://fetch.spec.whatwg.org/#ref-for-in-parallel⑤
pub func fetch_fetched_data_receiver_handle_network_bytes(this: FetchFetchedDataReceiver*, bytes: const u8[], state: FetchFetchedDataReceiverNetworkState):
if state == FetchFetchedDataReceiverNetworkState.complete:
assert(bytes.length == 0)
this.m_network_complete = true
# Mark sniff bytes as complete when the stream ends
if let body = this.m_body:
fetch_body_set_sniff_bytes_complete(body)
# 2. Otherwise, if the bytes transmission for response’s message body is done normally and stream is readable,
# then close stream, and abort these in-parallel steps.
platform_event_loop_plugin_deferred_invoke(platform_event_loop_plugin_the(), gc.create_function0(gc.cell_heap(this.cell()), ak.Function0[unit](call = fetch_fetched_data_receiver_close_stream_task, context = this)))
return
if state == FetchFetchedDataReceiverNetworkState.error_:
return
# 1. If one or more bytes have been transmitted from response’s message body, then:
if bytes.length == 0:
return
# 1. Let bytes be the transmitted bytes.
# FIXME: 2. Let codings be the result of extracting header list values given `Content-Encoding` and response’s header list.
# FIXME: 3. Increase response’s body info’s encoded size by bytes’s length.
# FIXME: 4. Set bytes to the result of handling content codings given codings and bytes.
# FIXME: 5. Increase response’s body info’s decoded size by bytes’s length.
# FIXME: 6. If bytes is failure, then terminate fetchParams’s controller.
# Capture bytes for MIME sniffing
if let body = this.m_body:
fetch_body_append_sniff_bytes(body, bytes)
elif ak.byte_buffer_size(&this.m_pre_body_sniff_buffer) < fetch_max_sniff_bytes:
let space_remaining = fetch_max_sniff_bytes - ak.byte_buffer_size(&this.m_pre_body_sniff_buffer)
ak.byte_buffer_append_span_u8(&this.m_pre_body_sniff_buffer, bytes[..<(bytes.length if bytes.length < space_remaining else space_remaining)])
if this.m_http_cache != none:
ak.byte_buffer_append_span_u8(&this.m_cache_buffer, bytes)
# 7. Append bytes to buffer.
fetch_fetched_data_receiver_enqueue_into_stream(this, bytes)
# FIXME: 8. If the size of buffer is larger than an upper limit chosen by the user agent, ask the user agent
# to suspend the ongoing fetch.
## Ported from Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp:113 Web::Fetch::Fetching::FetchedDataReceiver::enqueue_into_stream (private).
# This implements the parallel steps of the pullAlgorithm in HTTP-network-fetch.
# https://fetch.spec.whatwg.org/#ref-for-in-parallel④
func fetch_fetched_data_receiver_enqueue_into_stream(this: FetchFetchedDataReceiver*, bytes: const u8[]):
# FIXME: 1. If the size of buffer is smaller than a lower limit chosen by the user agent and the ongoing fetch
# is suspended, resume the fetch.
if not streams_readable_stream_is_readable(this.m_stream):
return
let realm = streams_readable_stream_realm(this.m_stream)
var execution_context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&execution_context, realm, HtmlTemporaryExecutionContextCallbacksEnabled.yes)
defer html_temporary_execution_context_destruct(&execution_context)
# 1. Pull from bytes buffer into stream.
let byte_buffer = ak.byte_buffer_copy_span_u8(bytes) else trap("MUST: ByteBuffer::copy")
let array_buffer = js_array_buffer_create_byte_buffer(realm, byte_buffer)
let view = js_uint8_array_create(realm, (u32)js_array_buffer_byte_length(array_buffer), array_buffer)
let controller = streams_readable_stream_byte_controller(this.m_stream)
streams_readable_byte_stream_controller_enqueue_2(controller, js_value_from_object((JsObject*)view)) catch:
let completion = bind_exception_to_throw_completion(realm_vm(realm), idl_take_pending_exception())
# 2. If stream is errored, then terminate fetchParams’s controller.
streams_readable_byte_stream_controller_error_2(controller, js_completion_value(&completion))
fetch_controller_terminate(fetch_params_controller(this.m_fetch_params))
recover ()
## Ported from Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp:139 Web::Fetch::Fetching::FetchedDataReceiver::close_stream (private).
func fetch_fetched_data_receiver_close_stream(this: FetchFetchedDataReceiver*):
if let http_cache = this.m_http_cache:
let request = fetch_params_request(this.m_fetch_params)
if streams_readable_stream_is_readable(this.m_stream) and not fetch_params_is_canceled(this.m_fetch_params) and fetch_request_cache_mode(request) != HttpCacheMode.no_store:
if let response = this.m_response:
let method = fetch_request_method(request)
http_memory_cache_finalize_entry(http_cache, fetch_request_current_url(request), ak.byte_string_view(&method), fetch_request_header_list(request), (u32)fetch_response_status(response), fetch_response_header_list(response), this.m_cache_buffer)
this.m_http_cache = none
if not streams_readable_stream_is_readable(this.m_stream):
return
var execution_context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&execution_context, streams_readable_stream_realm(this.m_stream), HtmlTemporaryExecutionContextCallbacksEnabled.yes)
defer html_temporary_execution_context_destruct(&execution_context)
streams_readable_stream_close(this.m_stream)