Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / fetch / infrastructure / http / bodies.lucb

#==============================================================================================
#
#   bodies - Fetch::Infrastructure::Body: a body's stream, source and length, the resource
#   header bytes for MIME type sniffing, cloning, and reading a body fully or incrementally
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp (lines 20-217) with the
#       inline members of Bodies.h (lines 48-51). A body's stream is a Streams::ReadableStream:
#       before phase 4 the stand-in of DESIGN.md §3.6 (streams/, region p2s), a byte stream read
#       by default readers; Fetch/BodyInit's safely_extract_body of a byte sequence is
#       fetch/body_init.
#
#       Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Creation ================================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:22 Web::Fetch::Infrastructure::to_source_type_internal (internal linkage).
func fetch_to_source_type_internal(source_type: FetchBodySourceType) -> FetchBodySourceTypeInternal:
    match source_type:
        .empty: return .empty
        .byte_buffer(buffer): return .byte_buffer(buffer)
        .file_api_blob(blob): return .file_api_blob(blob else trap("GC::Ref { *blob }: a null blob"))

## `vm.heap().allocate<Body>(...)`: a zeroed Body cell of the VM's heap, garbage collection deferred
## while the caller runs the constructor and undefers.
func fetch_body_allocate(vm: Vm*) -> FetchBody*:
    let heap = vm_heap(vm)
    let body = (FetchBody*)web_heap_allocate_cell(heap, &fetch_body_class)
    gc.heap_defer_gc(heap)
    return body

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:30 Web::Fetch::Infrastructure::Body::create.
pub func fetch_body_create(vm: Vm*, stream: StreamsReadableStream*) -> FetchBody*:
    let body = fetch_body_allocate(vm)
    fetch_body_construct(body, stream)
    gc.heap_undefer_gc(vm_heap(vm))
    return body

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:35 Web::Fetch::Infrastructure::Body::create (with a SourceType).
pub func fetch_body_create_fetch_body_source_type(vm: Vm*, stream: StreamsReadableStream*, source: FetchBodySourceType, length: u64?) -> FetchBody*:
    return fetch_body_create_fetch_body_source_type_interna(vm, stream, fetch_to_source_type_internal(source), length)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:40 Web::Fetch::Infrastructure::Body::create (with a SourceTypeInternal).
pub func fetch_body_create_fetch_body_source_type_interna(vm: Vm*, stream: StreamsReadableStream*, source: FetchBodySourceTypeInternal, length: u64?) -> FetchBody*:
    let body = fetch_body_allocate(vm)
    fetch_body_construct_fetch_body_source_type_interna(body, stream, source, length)
    gc.heap_undefer_gc(vm_heap(vm))
    return body

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:45 Web::Fetch::Infrastructure::Body::Body (private).
func fetch_body_construct(this: FetchBody*, stream: StreamsReadableStream*):
    this.m_stream = stream

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:50 Web::Fetch::Infrastructure::Body::Body (private).
func fetch_body_construct_fetch_body_source_type_interna(this: FetchBody*, stream: StreamsReadableStream*, source: FetchBodySourceTypeInternal, length: u64?):
    this.m_stream = stream
    this.m_source = source
    this.m_length = length

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:57 Web::Fetch::Infrastructure::Body::visit_edges (override).
pub func fetch_body_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (FetchBody*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_stream)
    gc.cell_visitor_visit_t(visitor, this.m_sniff_bytes_callback)
    match this.m_source:
        .file_api_blob(blob): gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)blob)  # FileAPI::Blob: opaque until phase 3
        _: return

# mark: Bodies.h's accessors ====================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h:48 Web::Fetch::Infrastructure::Body::stream.
pub func fetch_body_stream(this: FetchBody*) -> StreamsReadableStream*:
    return this.m_stream

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h:49 Web::Fetch::Infrastructure::Body::set_stream.
pub func fetch_body_set_stream(this: FetchBody*, value: StreamsReadableStream*):
    this.m_stream = value

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h:50 Web::Fetch::Infrastructure::Body::source.
pub func fetch_body_source(this: FetchBody*) -> FetchBodySourceTypeInternal:
    return this.m_source

## `body->source().get_pointer<ByteBuffer>()` (Bodies.h:50 Body::source): the source's byte buffer, if
## the source is one (a copy of the buffer's handle in managed memory; its bytes are shared).
pub func fetch_body_source_byte_buffer(this: FetchBody*) -> const ak.ByteBuffer*?:
    match this.m_source:
        .byte_buffer(buffer):
            let pointer = new ak.ByteBuffer else trap("out of memory")
            *pointer = buffer
            return pointer
        _: return none

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h:51 Web::Fetch::Infrastructure::Body::length.
pub func fetch_body_length(this: FetchBody*) -> u64?:
    return this.m_length

# mark: Sniff bytes =============================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:67 Web::Fetch::Infrastructure::Body::append_sniff_bytes.
pub func fetch_body_append_sniff_bytes(this: FetchBody*, bytes: const u8[]):
    if this.m_sniff_bytes_complete:
        return

    let space_remaining = fetch_max_sniff_bytes - ak.byte_buffer_size(&this.m_sniff_bytes)
    if space_remaining == 0:
        fetch_body_set_sniff_bytes_complete(this)
        return

    let to_append = bytes.length if bytes.length < space_remaining else space_remaining
    ak.byte_buffer_append_void(&this.m_sniff_bytes, (const void*)bytes.data, to_append)

    if ak.byte_buffer_size(&this.m_sniff_bytes) >= fetch_max_sniff_bytes:
        fetch_body_set_sniff_bytes_complete(this)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:85 Web::Fetch::Infrastructure::Body::set_sniff_bytes_complete.
pub func fetch_body_set_sniff_bytes_complete(this: FetchBody*):
    if this.m_sniff_bytes_complete:
        return
    this.m_sniff_bytes_complete = true
    if let callback = this.m_sniff_bytes_callback:
        this.m_sniff_bytes_callback = none
        ak.function1_call(&callback.function, ak.byte_buffer_bytes_const(&this.m_sniff_bytes))

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:96 Web::Fetch::Infrastructure::Body::sniff_bytes_if_available.
pub func fetch_body_sniff_bytes_if_available(this: FetchBody*) -> (const u8[])?:
    match this.m_source:
        # Non-streaming body: source has bytes
        .byte_buffer(buffer):
            let bytes = ak.byte_buffer_bytes_const(&buffer)
            return bytes[0..<(bytes.length if bytes.length < fetch_max_sniff_bytes else fetch_max_sniff_bytes)]
        .file_api_blob(blob):
            let raw = file_api_blob_raw_bytes(blob)
            return raw[0..<(raw.length if raw.length < fetch_max_sniff_bytes else fetch_max_sniff_bytes)]
        .empty:
            # Streaming body: bytes captured during fetch
            if this.m_sniff_bytes_complete:
                return ak.byte_buffer_bytes_const(&this.m_sniff_bytes)

            # Still waiting for bytes
            return none

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:117 Web::Fetch::Infrastructure::Body::wait_for_sniff_bytes.
pub func fetch_body_wait_for_sniff_bytes(this: FetchBody*, on_ready: gc.Function1[const u8[], unit]*):
    if let bytes = fetch_body_sniff_bytes_if_available(this):
        ak.function1_call(&on_ready.function, bytes)
        return

    # Wait for bytes to arrive
    this.m_sniff_bytes_callback = on_ready

# mark: Cloning and reading =====================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:129 Web::Fetch::Infrastructure::Body::clone.
# https://fetch.spec.whatwg.org/#concept-body-clone
pub func fetch_body_clone(this: FetchBody*, realm: Realm*) -> FetchBody*:
    var execution_context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&execution_context, realm, HtmlTemporaryExecutionContextCallbacksEnabled.yes)
    defer html_temporary_execution_context_destruct(&execution_context)

    # To clone a body body, run these steps:
    # 1. Let « out1, out2 » be the result of teeing body’s stream.
    let (out1, out2) = streams_readable_stream_tee(this.m_stream, realm) else trap("FIXME: should propagate errors")

    # 2. Set body’s stream to out1.
    this.m_stream = out1

    # 3. Return a body whose stream is out2 and other members are copied from body.
    # (A copy of the source: C++ copies its ByteBuffer.)
    let source: FetchBodySourceTypeInternal = match this.m_source:
        .byte_buffer(buffer) => .byte_buffer(ak.byte_buffer_clone(&buffer))
        _ => this.m_source
    return fetch_body_create_fetch_body_source_type_interna(realm_vm(realm), out2, source, this.m_length)

## The captures of fully_read's processBody task, `[process_body, bytes = move(bytes)]() mutable`.
struct FetchBodyFullyReadProcessBody:
    var process_body: gc.Function1[ak.ByteBuffer, unit]*
    var bytes: ak.ByteBuffer

## fully_read's processBody task: run processBody given bytes.
func fetch_body_fully_read_process_body(context: void*?):
    let captures = (FetchBodyFullyReadProcessBody*)(context else trap("fully_read: no captures"))
    ak.function1_call(&captures.process_body.function, captures.bytes)

## The captures of fully_read's processBodyError task, `[process_body_error, exception]()`.
struct FetchBodyFullyReadProcessBodyError:
    var process_body_error: gc.Function1[JsValue, unit]*
    var exception: JsValue

## fully_read's processBodyError task: run processBodyError given exception.
func fetch_body_fully_read_process_body_error(context: void*?):
    let captures = (FetchBodyFullyReadProcessBodyError*)(context else trap("fully_read: no captures"))
    ak.function1_call(&captures.process_body_error.function, captures.exception)

## The captures of fully_read's successSteps and errorSteps, `[&realm, process_body(_error), task_destination]`.
struct FetchBodyFullyReadSteps:
    var realm: Realm*
    var process_body: gc.Function1[ak.ByteBuffer, unit]*
    var process_body_error: gc.Function1[JsValue, unit]*
    var task_destination: FetchTaskDestination

## fully_read's successSteps given a byte sequence bytes: queue a fetch task to run processBody given bytes, with
## taskDestination.
func fetch_body_fully_read_success_steps(context: void*?, bytes: ak.ByteBuffer):
    let captures = (FetchBodyFullyReadSteps*)(context else trap("fully_read: no captures"))
    let task = new FetchBodyFullyReadProcessBody(process_body = captures.process_body, bytes = bytes) else trap("out of memory")
    _ = fetch_queue_fetch_task_fetch_task_destination(captures.task_destination, gc.create_function0(realm_heap(captures.realm), ak.Function0[unit](call = fetch_body_fully_read_process_body, context = task)))

## fully_read's errorSteps optionally given an exception exception: queue a fetch task to run processBodyError
## given exception, with taskDestination.
func fetch_body_fully_read_error_steps(context: void*?, exception: JsValue):
    let captures = (FetchBodyFullyReadSteps*)(context else trap("fully_read: no captures"))
    let task = new FetchBodyFullyReadProcessBodyError(process_body_error = captures.process_body_error, exception = exception) else trap("out of memory")
    _ = fetch_queue_fetch_task_fetch_task_destination(captures.task_destination, gc.create_function0(realm_heap(captures.realm), ak.Function0[unit](call = fetch_body_fully_read_process_body_error, context = task)))

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:145 Web::Fetch::Infrastructure::Body::fully_read.
# https://fetch.spec.whatwg.org/#body-fully-read
pub func fetch_body_fully_read(this: FetchBody*, realm: Realm*, process_body: gc.Function1[ak.ByteBuffer, unit]*, process_body_error: gc.Function1[JsValue, unit]*, task_destination: FetchTaskDestination):
    var execution_context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&execution_context, realm, HtmlTemporaryExecutionContextCallbacksEnabled.yes)
    defer html_temporary_execution_context_destruct(&execution_context)

    # 1. If taskDestination is null, then set taskDestination to the result of starting a new parallel queue.
    var destination = task_destination
    if destination == .empty:
        destination = .html_parallel_queue(html_parallel_queue_create())

    # 2. Let successSteps given a byte sequence bytes be to queue a fetch task to run processBody given bytes, with taskDestination.
    # 3. Let errorSteps optionally given an exception exception be to queue a fetch task to run processBodyError given
    #    exception, with taskDestination.
    let steps = new FetchBodyFullyReadSteps(realm = realm, process_body = process_body, process_body_error = process_body_error, task_destination = destination) else trap("out of memory")

    # 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.
    let reader = streams_readable_stream_get_a_reader(this.m_stream) catch:
        let completion = bind_exception_to_throw_completion(realm_vm(realm), idl_take_pending_exception())
        fetch_body_fully_read_error_steps(steps, js_completion_value(&completion))
        return

    # 5. Read all bytes from reader, given successSteps and errorSteps.
    let heap = realm_heap(realm)
    streams_readable_stream_default_reader_read_all_bytes(reader, gc.create_function1(heap, ak.Function1[ak.ByteBuffer, unit](call = fetch_body_fully_read_success_steps, context = steps)), gc.create_function1(heap, ak.Function1[JsValue, unit](call = fetch_body_fully_read_error_steps, context = steps)))

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:183 Web::Fetch::Infrastructure::Body::incrementally_read.
# https://fetch.spec.whatwg.org/#body-incrementally-read
pub func fetch_body_incrementally_read(this: FetchBody*, process_body_chunk: gc.Function1[ak.ByteBuffer, unit]*, process_end_of_body: gc.Function0[unit]*, process_body_error: gc.Function1[JsValue, unit]*, task_destination: FetchTaskDestination):
    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)

    # 1. If taskDestination is null, then set taskDestination to the result of starting a new parallel queue.
    var destination = task_destination
    if destination == .empty:
        destination = .html_parallel_queue(html_parallel_queue_create())

    # 2. Let reader be the result of getting a reader for body’s stream.
    # NOTE: This operation will not throw an exception.
    let reader = streams_readable_stream_get_a_reader(this.m_stream) else trap("MUST: get_a_reader")

    # 3. Perform the incrementally-read loop given reader, taskDestination, processBodyChunk, processEndOfBody, and processBodyError.
    assert(destination != .empty)
    let object = match destination:
        .js_object(object) => object
        _ => trap("VERIFY: task_destination.get<GC::Ref<JS::Object>>()")
    fetch_body_incrementally_read_loop(this, reader, .js_object(object), process_body_chunk, process_end_of_body, process_body_error)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:201 Web::Fetch::Infrastructure::Body::incrementally_read_loop.
# https://fetch.spec.whatwg.org/#incrementally-read-loop
pub func fetch_body_incrementally_read_loop(this: FetchBody*, reader: StreamsReadableStreamDefaultReader*, task_destination: FetchTaskDestination, process_body_chunk: gc.Function1[ak.ByteBuffer, unit]*, process_end_of_body: gc.Function0[unit]*, process_body_error: gc.Function1[JsValue, unit]*):
    let realm = streams_readable_stream_default_reader_realm(reader)
    # 1. Let readRequest be the following read request:
    let read_request = realm_create_fetch_incremental_read_loop_read_request(realm, this, reader, task_destination, process_body_chunk, process_end_of_body, process_body_error)

    # 2. Read a chunk from reader given readRequest.
    streams_readable_stream_default_reader_read_a_chunk(reader, read_request)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp:212 Web::Fetch::Infrastructure::byte_sequence_as_body.
# https://fetch.spec.whatwg.org/#byte-sequence-as-a-body
pub func fetch_byte_sequence_as_body(realm: Realm*, bytes: const u8[]) -> FetchBody*:
    # To get a byte sequence bytes as a body, return the body of the result of safely extracting bytes.
    let body_with_type = fetch_safely_extract_body_bytes(realm, bytes)
    return body_with_type.body