Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / fetch / fetching / fetching_3.lucb

#==============================================================================================
#
#   fetching_3 - Fetch::Fetching: determining a request's environment and fetch response
#   handover
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Fetching/Fetching.cpp (lines 727-930): determine the
#       environment, and fetch response handover ("fetch finale"): the report timing steps,
#       processResponse, processResponseEndOfBody (through an identity TransformStream when
#       the response has a body), and processResponseConsumeBody. Each C++ lambda is a
#       capture struct with a static function (DESIGN.md §2.7); the lambdas that share
#       `[&vm, &response, &fetch_params, timing_info]` share FetchFetchingFetchResponseHandover.
#
#       Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
#       Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
#       Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
#       Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
#       Copyright (c) 2025, Kenneth Myhra <kennethmyhra@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Fetch/Fetching/Fetching.cpp:728 Web::Fetch::Fetching::determine_the_environment.
# https://fetch.spec.whatwg.org/#request-determine-the-environment
func fetch_fetching_determine_the_environment(request: FetchRequest*) -> HtmlEnvironment*?:
    # 1. If request’s reserved client is non-null, then return request’s reserved client.
    if let reserved_client = fetch_request_reserved_client(request):
        return reserved_client

    # 2. If request’s client is non-null, then return request’s client.
    if let client = fetch_request_client(request):
        return client.html_environment()

    # 3. Return null.
    return none

# mark: The report timing steps =================================================================

## The captures that fetch response handover's lambdas share, `[&vm, &response, &fetch_params, timing_info]`
## (setup_report_timing_steps, processResponseEndOfBody and its task, processBodyError).
struct FetchFetchingFetchResponseHandover:
    var realm: Realm*
    var fetch_params: FetchParams*
    var response: FetchResponse*
    var timing_info: FetchTimingInfo*

## The captures of the report timing steps, `[&vm, &response, &fetch_params, timing_info, unsafe_end_time](JS::Object&
## global) mutable`.
struct FetchFetchingFetchResponseHandoverReportTiming:
    var vm: Vm*
    var fetch_params: FetchParams*
    var response: FetchResponse*
    var timing_info: FetchTimingInfo*
    var unsafe_end_time: f64

    ## fetchParams’s controller’s report timing steps given a global object global.
    static func call(context: void*?, global: JsObject*):
        let captures = (FetchFetchingFetchResponseHandoverReportTiming*)(context else trap("fetch_response_handover: no captures"))
        let request = fetch_params_request(captures.fetch_params)
        let response = captures.response

        # 1. If fetchParams’s request’s URL’s scheme is not an HTTP(S) scheme, then return.
        let url = fetch_request_url(request)
        let scheme = web_url.url_scheme(&url)
        if not fetch_is_http_or_https_scheme(ak.string_bytes_as_string_view(&scheme)):
            return

        # 2. Set timingInfo’s end time to the relative high resolution time given unsafeEndTime and global.
        # Spec Issue: Using relative time here is incorrect, as end time is converted to relative time by Resource Timing,
        #             causing it to take a relative time of an already relative time, effectively make it always a negative
        #             value approximately the value of the time origin.
        fetch_timing_info_set_end_time(captures.timing_info, captures.unsafe_end_time)

        # 3. Let cacheState be response’s cache state.
        var cache_state = fetch_response_cache_state(response)

        # 4. Let bodyInfo be response’s body info.
        var body_info = *fetch_response_body_info(response)

        # 5. If response’s timing allow passed flag is not set, then set timingInfo to the result of creating an
        #    opaque timing info for timingInfo, set bodyInfo to a new response body info, and set cacheState to
        #    the empty string.
        # NOTE: This covers the case of response being a network error.
        if not fetch_response_timing_allow_passed(response):
            captures.timing_info = fetch_create_opaque_timing_info(captures.vm, captures.timing_info)
            body_info = FetchResponseBodyInfo(content_type = ak.string_new())
            cache_state = none

        # 6. Let responseStatus be 0.
        var response_status: u16 = 0

        # 7. If fetchParams’s request’s mode is not "navigate" or response’s redirect taint is "same-origin":
        if fetch_request_mode(request) != FetchRequestMode.navigate or fetch_response_redirect_taint(response) == FetchRedirectTaint.same_origin:
            # 1. Set responseStatus to response’s status.
            response_status = fetch_response_status(response)

            # 2. Let mimeType be the result of extracting a MIME type from response’s header list.
            let mime_type = fetch_extract_mime_type(fetch_response_header_list(response))

            # 3. If mimeType is non-null, then set bodyInfo’s content type to the result of minimizing a supported MIME type given mimeType.
            if let value = mime_type:
                body_info.content_type = mime_minimise_a_supported_mime_type(&value)

        # 8. If fetchParams’s request’s initiator type is not null, then mark resource timing given timingInfo,
        #    request’s URL, request’s initiator type, global, cacheState, bodyInfo, and responseStatus.
        if let initiator_type = fetch_request_initiator_type(request):
            resource_timing_performance_resource_timing_mark_resource_timing(captures.timing_info, web_url.url_to_string(&url), fetch_initiator_type_to_string(initiator_type), global, cache_state, body_info, response_status)

## Steps 1 to 3 of processResponseEndOfBody, `setup_report_timing_steps`.
# AD-HOC: We extract steps 1-3 of processResponseEndOfBody into a separate lambda so we can also call it from
#         the error path. The fetch spec only runs processResponseEndOfBody on successful body read (via the
#         transform stream's flush algorithm). However, processResponseConsumeBody is called for both success
#         and failure, and specs like HTML's preload algorithm expect to be able to call reportTiming from
#         within processResponseConsumeBody. So we ensure report_timing_steps is set on error too, which allows
#         reportTiming to work without asserting, and still produces useful timing data for failed fetches.
func fetch_fetching_fetch_response_handover_setup_report_timing_steps(handover: FetchFetchingFetchResponseHandover*):
    let fetch_params = handover.fetch_params

    # 1. Let unsafeEndTime be the unsafe shared current time.
    let unsafe_end_time = high_resolution_time_unsafe_shared_current_time()

    # 2. If fetchParams’s request’s destination is "document", then set fetchParams’s controller’s full timing
    #    info to fetchParams’s timing info.
    if fetch_request_destination(fetch_params_request(fetch_params)) == FetchRequestDestination.document:
        fetch_controller_set_full_timing_info(fetch_params_controller(fetch_params), fetch_params_timing_info(fetch_params))

    # 3. Set fetchParams’s controller’s report timing steps to the following steps given a global object global:
    let report_timing = new FetchFetchingFetchResponseHandoverReportTiming(vm = realm_vm(handover.realm), fetch_params = fetch_params, response = handover.response, timing_info = handover.timing_info, unsafe_end_time = unsafe_end_time) else trap("out of memory")
    fetch_controller_set_report_timing_steps(fetch_params_controller(fetch_params), ak.Function1[JsObject*, unit](call = FetchFetchingFetchResponseHandoverReportTiming.call, context = report_timing))

# mark: processResponseEndOfBody ================================================================

## processResponseEndOfBody: the report timing steps, then queue a fetch task to run
## processResponseEndOfBodyTask with fetchParams’s task destination.
func fetch_fetching_fetch_response_handover_process_response_end_of_body(handover: FetchFetchingFetchResponseHandover*):
    # 1-3. (See setup_report_timing_steps above)
    fetch_fetching_fetch_response_handover_setup_report_timing_steps(handover)

    # 4. Let processResponseEndOfBodyTask be the following steps:
    let process_response_end_of_body_task = gc.create_function0(realm_heap(handover.realm), ak.Function0[unit](call = fetch_fetching_fetch_response_handover_process_response_end_of_body_task, context = handover))

    # 5. Queue a fetch task to run processResponseEndOfBodyTask with fetchParams’s task destination.
    let fetch_params = handover.fetch_params
    _ = fetch_queue_fetch_task_fetch_controller(fetch_params_controller(fetch_params), fetch_params_task_destination_const(fetch_params), process_response_end_of_body_task)

## processResponseEndOfBodyTask, `[&fetch_params, &response]`.
func fetch_fetching_fetch_response_handover_process_response_end_of_body_task(context: void*?):
    let handover = (FetchFetchingFetchResponseHandover*)(context else trap("fetch_response_handover: no captures"))
    let fetch_params = handover.fetch_params
    let request = fetch_params_request(fetch_params)

    # 1. Set fetchParams’s request’s done flag.
    fetch_request_set_done(request, true)

    # 2. If fetchParams’s process response end-of-body is non-null, then run fetchParams’s process response
    #    end-of-body given response.
    let process_response_end_of_body = fetch_algorithms_process_response_end_of_body(fetch_params_algorithms(fetch_params))
    if ak.function1_is_set(&process_response_end_of_body):
        ak.function1_call(&process_response_end_of_body, handover.response)

    # 3. If fetchParams’s request’s initiator type is non-null and fetchParams’s request’s client’s global
    #    object is fetchParams’s task destination, then run fetchParams’s controller’s report timing steps
    #    given fetchParams’s request’s client’s global object.
    let client = fetch_request_client(request)
    let task_destination_global_object: JsObject*? = match fetch_params_task_destination_const(fetch_params):
        .js_object(object) => object
        _ => none
    if let client_object = client:
        if let destination_global_object = task_destination_global_object:
            let global = html_environment_settings_object_global_object(client_object)
            if fetch_request_initiator_type(request) != none and global == destination_global_object:
                fetch_controller_report_timing(fetch_params_controller(fetch_params), global)

# mark: The identity TransformStream ============================================================

## The captures of identityTransformAlgorithm, `[&realm, transform_stream](JS::Value chunk) -> GC::Ref<WebIDL::Promise>`.
struct FetchFetchingFetchResponseHandoverIdentityTransform:
    var realm: Realm*
    var transform_stream: StreamsTransformStream*

    ## Given chunk, enqueues chunk in transformStream.
    static func call(context: void*?, chunk: JsValue) -> JsPromiseCapability*:
        let captures = (FetchFetchingFetchResponseHandoverIdentityTransform*)(context else trap("fetch_response_handover: no captures"))
        streams_transform_stream_default_controller_enqueue(streams_transform_stream_controller(captures.transform_stream), chunk) else trap("MUST: transform_stream_default_controller_enqueue")
        return idl_create_resolved_promise(captures.realm, js_undefined())

## The flush algorithm, `[&realm, process_response_end_of_body]() -> GC::Ref<WebIDL::Promise>`.
func fetch_fetching_fetch_response_handover_flush_algorithm(context: void*?) -> JsPromiseCapability*:
    let handover = (FetchFetchingFetchResponseHandover*)(context else trap("fetch_response_handover: no captures"))
    fetch_fetching_fetch_response_handover_process_response_end_of_body(handover)
    return idl_create_resolved_promise(handover.realm, js_undefined())

# mark: processResponse and processResponseConsumeBody ==========================================

## The task that runs fetchParams’s process response given response, `[&fetch_params, &response]()`.
func fetch_fetching_fetch_response_handover_process_response(context: void*?):
    let handover = (FetchFetchingFetchResponseHandover*)(context else trap("fetch_response_handover: no captures"))
    let process_response = fetch_algorithms_process_response(fetch_params_algorithms(handover.fetch_params))
    ak.function1_call(&process_response, handover.response)

## processBody given nullOrBytes, `[&fetch_params, &response](ByteBuffer bytes)`: run fetchParams’s process response
## consume body given response and nullOrBytes.
func fetch_fetching_fetch_response_handover_process_body(context: void*?, bytes: ak.ByteBuffer):
    let handover = (FetchFetchingFetchResponseHandover*)(context else trap("fetch_response_handover: no captures"))
    let process_response_consume_body = fetch_algorithms_process_response_consume_body(fetch_params_algorithms(handover.fetch_params))
    ak.function2_call(&process_response_consume_body, handover.response, .byte_buffer(bytes))

## processBodyError, `[&fetch_params, &response, setup_report_timing_steps](JS::Value)`: run fetchParams’s process
## response consume body given response and failure.
func fetch_fetching_fetch_response_handover_process_body_error(context: void*?, value: JsValue):
    let handover = (FetchFetchingFetchResponseHandover*)(context else trap("fetch_response_handover: no captures"))
    # AD-HOC: See comment on setup_report_timing_steps above.
    fetch_fetching_fetch_response_handover_setup_report_timing_steps(handover)
    let process_response_consume_body = fetch_algorithms_process_response_consume_body(fetch_params_algorithms(handover.fetch_params))
    ak.function2_call(&process_response_consume_body, handover.response, .fetch_algorithms_consume_body_failure_tag(FetchAlgorithmsConsumeBodyFailureTag()))

## The task that runs processBody given null, `[&fetch_params, &response]()`.
func fetch_fetching_fetch_response_handover_process_null_body(context: void*?):
    let handover = (FetchFetchingFetchResponseHandover*)(context else trap("fetch_response_handover: no captures"))
    # NOTE: We have to provide `fully_read` a callback which accepts a ByteBuffer. Since that is not
    #       nullable, we just invoke `process_response_consume_body` with a null value manually here.
    let process_response_consume_body = fetch_algorithms_process_response_consume_body(fetch_params_algorithms(handover.fetch_params))
    ak.function2_call(&process_response_consume_body, handover.response, .empty)

# mark: Fetch response handover =================================================================

## Ported from Libraries/LibWeb/Fetch/Fetching/Fetching.cpp:743 Web::Fetch::Fetching::fetch_response_handover.
# https://fetch.spec.whatwg.org/#fetch-finale
pub func fetch_fetching_fetch_response_handover(realm: Realm*, fetch_params: FetchParams*, response: FetchResponse*):
    if fetch_web_fetch_debug:
        ak.dbgln(f"Fetch: Running 'fetch response handover' with: fetch_params @ {(void*)fetch_params}, response @ {(void*)response}")

    let vm = realm_vm(realm)

    # 1. Let timingInfo be fetchParams’s timing info.
    let timing_info = fetch_params_timing_info(fetch_params)

    # 2. If response is not a network error and fetchParams’s request’s client is a secure context, then set
    #    timingInfo’s server-timing headers to the result of getting, decoding, and splitting `Server-Timing` from
    #    response’s header list.
    #    The user agent may decide to expose `Server-Timing` headers to non-secure contexts requests as well.
    if not fetch_response_is_network_error(response):
        if let client = fetch_request_client(fetch_params_request(fetch_params)):
            if html_is_secure_context(client.html_environment()):
                if let server_timing_headers = http_header_list_get_decode_and_split(fetch_response_header_list(response), ak.sv("Server-Timing")):
                    fetch_timing_info_set_server_timing_headers(timing_info, server_timing_headers)

    # (The captures of setup_report_timing_steps, processResponseEndOfBody and processBodyError.)
    let handover = new FetchFetchingFetchResponseHandover(realm = realm, fetch_params = fetch_params, response = response, timing_info = timing_info) else trap("out of memory")

    # 3. Let processResponseEndOfBody be the following steps: (fetch_fetching_fetch_response_handover_process_response_end_of_body)

    # 4. If fetchParams’s process response is non-null, then queue a fetch task to run fetchParams’s process response
    #    given response, with fetchParams’s task destination.
    let process_response = fetch_algorithms_process_response(fetch_params_algorithms(fetch_params))
    if ak.function1_is_set(&process_response):
        _ = fetch_queue_fetch_task_fetch_controller(fetch_params_controller(fetch_params), fetch_params_task_destination_const(fetch_params), gc.create_function0(vm_heap(vm), ak.Function0[unit](call = fetch_fetching_fetch_response_handover_process_response, context = handover)))

    # 5. Let internalResponse be response, if response is a network error; otherwise response’s internal response.
    let internal_response = response if fetch_response_is_network_error(response) else fetch_response_unsafe_response(response)

    # 6. If internalResponse’s body is null, then run processResponseEndOfBody.
    if fetch_response_body(internal_response) == none:
        fetch_fetching_fetch_response_handover_process_response_end_of_body(handover)
    # 7. Otherwise:
    else:
        var execution_context: HtmlTemporaryExecutionContext = ---
        html_temporary_execution_context_construct_realm(&execution_context, realm, HtmlTemporaryExecutionContextCallbacksEnabled.yes)
        defer html_temporary_execution_context_destruct(&execution_context)

        # 1. Let transformStream be a new TransformStream.
        let transform_stream = realm_create_streams_transform_stream(realm, realm)

        # 2. Let identityTransformAlgorithm be an algorithm which, given chunk, enqueues chunk in transformStream.
        let identity_captures = new FetchFetchingFetchResponseHandoverIdentityTransform(realm = realm, transform_stream = transform_stream) else trap("out of memory")
        let identity_transform_algorithm = gc.create_function1(realm_heap(realm), ak.Function1[JsValue, JsPromiseCapability*](call = FetchFetchingFetchResponseHandoverIdentityTransform.call, context = identity_captures))

        # 3. Set up transformStream with transformAlgorithm set to identityTransformAlgorithm and flushAlgorithm set
        #    to processResponseEndOfBody.
        let flush_algorithm = gc.create_function0(realm_heap(realm), ak.Function0[JsPromiseCapability*](call = fetch_fetching_fetch_response_handover_flush_algorithm, context = handover))
        streams_transform_stream_set_up(transform_stream, identity_transform_algorithm, flush_algorithm)

        # 4. Set internalResponse’s body’s stream to the result of internalResponse’s body’s stream piped through transformStream.
        let internal_body = fetch_response_body(internal_response) else trap("VERIFY: internalResponse's body")
        fetch_body_set_stream(internal_body, streams_readable_stream_piped_through(fetch_body_stream(internal_body), transform_stream))

    # 8. If fetchParams’s process response consume body is non-null, then:
    let process_response_consume_body = fetch_algorithms_process_response_consume_body(fetch_params_algorithms(fetch_params))
    if ak.function2_is_set(&process_response_consume_body):
        # 1. Let processBody given nullOrBytes be this step: run fetchParams’s process response consume body given
        #    response and nullOrBytes.
        let process_body = gc.create_function1(vm_heap(vm), ak.Function1[ak.ByteBuffer, unit](call = fetch_fetching_fetch_response_handover_process_body, context = handover))

        # 2. Let processBodyError be this step: run fetchParams’s process response consume body given response and
        #    failure.
        let process_body_error = gc.create_function1(vm_heap(vm), ak.Function1[JsValue, unit](call = fetch_fetching_fetch_response_handover_process_body_error, context = handover))

        # 3. If internalResponse's body is null, then queue a fetch task to run processBody given null, with
        #    fetchParams’s task destination.
        if fetch_response_body(internal_response) == none:
            _ = fetch_queue_fetch_task_fetch_controller(fetch_params_controller(fetch_params), fetch_params_task_destination_const(fetch_params), gc.create_function0(vm_heap(vm), ak.Function0[unit](call = fetch_fetching_fetch_response_handover_process_null_body, context = handover)))
        # 4. Otherwise, fully read internalResponse body given processBody, processBodyError, and fetchParams’s task
        #    destination.
        else:
            let internal_body = fetch_response_body(internal_response) else trap("VERIFY: internalResponse's body")
            fetch_body_fully_read(internal_body, realm, process_body, process_body_error, fetch_params_task_destination_const(fetch_params))