Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / fetch / infrastructure / http / responses_1.lucb

#==============================================================================================
#
#   responses_1 - Fetch::Infrastructure::Response: creation, network errors, the virtual
#   accessors of Responses.h, the response's URL and location URL, cloning, unsafe responses
#   and CORS-same-origin
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp (lines 22-222) with
#       the members of Response in Responses.h (lines 62-145). Region r28 ported create, the
#       constructor, visit_edges, url and six accessors for the responses phase 1 makes;
#       region p2b the rest. Response's virtual accessors (FilteredResponse forwards them to
#       its internal response) are dispatchers over FetchResponseVTable, with the bodies here.
#
#       Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## `dbgln_if(WEB_FETCH_DEBUG, ...)` (AK/Debug.h; off by default).
let fetch_web_fetch_debug: bool = false

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

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:28 Web::Fetch::Infrastructure::Response::create.
pub func fetch_infrastructure_response_create(vm: Vm*) -> FetchResponse*:
    let heap = vm_heap(vm)
    let response = (FetchResponse*)web_heap_allocate_cell(heap, &fetch_response_class)
    gc.heap_defer_gc(heap)
    fetch_response_construct(response, http_header_list_create())
    gc.heap_undefer_gc(heap)
    return response

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:33 Web::Fetch::Infrastructure::Response::Response
## (protected), after the default member initializers.
func fetch_response_construct(this: FetchResponse*, header_list: HttpHeaderList*):
    fetch_response_init_fields(this)
    # (The default-constructed strings and BodyInfo's default member initializers are the zeroed cell's.)
    this.m_header_list = header_list
    this.m_monotonic_response_time = ak.monotonic_time_now()

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:39 Web::Fetch::Infrastructure::Response::visit_edges (override).
pub func fetch_response_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (FetchResponse*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_body)

# mark: Network errors ==========================================================================

# https://fetch.spec.whatwg.org/#ref-for-concept-network-error%E2%91%A3
# A network error is a response whose status is always 0, status message is always
# the empty byte sequence, header list is always empty, and body is always null.

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:49 Web::Fetch::Infrastructure::Response::aborted_network_error.
pub func fetch_response_aborted_network_error(vm: Vm*) -> FetchResponse*:
    let response = fetch_response_network_error(vm, ak.string_from_str("Fetch has been aborted"))
    fetch_response_set_aborted(response, true)
    return response

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:56 Web::Fetch::Infrastructure::Response::network_error.
pub func fetch_response_network_error(vm: Vm*, message: ak.String) -> FetchResponse*:
    if fetch_web_fetch_debug:
        ak.dbgln(f"Fetch: Creating network error response with message: {ak.string_view_text(ak.string_bytes_as_string_view(&message))}")
    let response = fetch_infrastructure_response_create(vm)
    fetch_response_set_status(response, 0)
    fetch_response_set_type(response, FetchResponseType.error_)
    assert(fetch_response_body(response) == none)
    response.m_network_error_message = message
    return response

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:68 Web::Fetch::Infrastructure::Response::appropriate_network_error.
# https://fetch.spec.whatwg.org/#appropriate-network-error
pub func fetch_response_appropriate_network_error(vm: Vm*, fetch_params: FetchParams*) -> FetchResponse*:
    # 1. Assert: fetchParams is canceled.
    assert(fetch_params_is_canceled(fetch_params))

    # 2. Return an aborted network error if fetchParams is aborted; otherwise return a network error.
    if fetch_params_is_aborted(fetch_params):
        return fetch_response_aborted_network_error(vm)
    return fetch_response_network_error(vm, ak.string_from_str("Fetch has been terminated"))

# mark: The virtual accessors of Responses.h ===================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:68 Web::Fetch::Infrastructure::Response::type (virtual).
pub func fetch_response_type_impl(this: FetchResponse*) -> FetchResponseType:
    return this.m_type

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:69 Web::Fetch::Infrastructure::Response::set_type.
pub func fetch_response_set_type(this: FetchResponse*, type_: FetchResponseType):
    this.m_type = type_

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:71 Web::Fetch::Infrastructure::Response::aborted (virtual).
pub func fetch_response_aborted_impl(this: FetchResponse*) -> bool:
    return this.m_aborted

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:72 Web::Fetch::Infrastructure::Response::set_aborted (virtual).
pub func fetch_response_set_aborted_impl(this: FetchResponse*, aborted: bool):
    this.m_aborted = aborted

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:74 Web::Fetch::Infrastructure::Response::url_list (virtual, const).
pub func fetch_response_url_list_const_impl(this: FetchResponse*) -> const ak.Vector[web_url.Url]*:
    return &this.m_url_list

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:75 Web::Fetch::Infrastructure::Response::url_list (virtual).
pub func fetch_response_url_list_impl(this: FetchResponse*) -> ak.Vector[web_url.Url]*:
    return &this.m_url_list

## Virtual: Web::Fetch::Infrastructure::Response::url_list.
pub func fetch_response_url_list(this: FetchResponse*) -> ak.Vector[web_url.Url]*:
    return ((const FetchResponseVTable*)((gc.Cell*)this).vtable).url_list(this)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:76 Web::Fetch::Infrastructure::Response::set_url_list (virtual).
pub func fetch_response_set_url_list_impl(this: FetchResponse*, url_list: ak.Vector[web_url.Url]):
    this.m_url_list = url_list

## Virtual: Web::Fetch::Infrastructure::Response::set_url_list.
pub func fetch_response_set_url_list(this: FetchResponse*, url_list: ak.Vector[web_url.Url]):
    ((const FetchResponseVTable*)((gc.Cell*)this).vtable).set_url_list(this, url_list)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:78 Web::Fetch::Infrastructure::Response::status (virtual).
pub func fetch_response_status_impl(this: FetchResponse*) -> u16:
    return this.m_status

## Virtual: Web::Fetch::Infrastructure::Response::status.
pub func fetch_response_status(this: FetchResponse*) -> u16:
    return ((const FetchResponseVTable*)((gc.Cell*)this).vtable).status(this)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:79 Web::Fetch::Infrastructure::Response::set_status (virtual).
pub func fetch_response_set_status_impl(this: FetchResponse*, status: u16):
    this.m_status = status

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:81 Web::Fetch::Infrastructure::Response::status_message (virtual).
pub func fetch_response_status_message_impl(this: FetchResponse*) -> ak.ByteString:
    return this.m_status_message

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:82 Web::Fetch::Infrastructure::Response::set_status_message (virtual).
pub func fetch_response_set_status_message_impl(this: FetchResponse*, status_message: ak.ByteString):
    this.m_status_message = status_message

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:84 Web::Fetch::Infrastructure::Response::header_list (virtual).
pub func fetch_response_header_list_impl(this: FetchResponse*) -> HttpHeaderList*:
    return this.m_header_list

## Virtual: Web::Fetch::Infrastructure::Response::header_list.
pub func fetch_response_header_list(this: FetchResponse*) -> HttpHeaderList*:
    return ((const FetchResponseVTable*)((gc.Cell*)this).vtable).header_list(this)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:85 Web::Fetch::Infrastructure::Response::set_header_list (virtual).
pub func fetch_response_set_header_list_impl(this: FetchResponse*, header_list: HttpHeaderList*):
    this.m_header_list = header_list

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:87 Web::Fetch::Infrastructure::Response::body (virtual).
pub func fetch_response_body_impl(this: FetchResponse*) -> FetchBody*?:
    return this.m_body

## Virtual: Web::Fetch::Infrastructure::Response::body.
pub func fetch_response_body(this: FetchResponse*) -> FetchBody*?:
    return ((const FetchResponseVTable*)((gc.Cell*)this).vtable).body(this)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:88 Web::Fetch::Infrastructure::Response::set_body (virtual).
pub func fetch_response_set_body_impl(this: FetchResponse*, body: FetchBody*?):
    this.m_body = body

## Virtual: Web::Fetch::Infrastructure::Response::set_body.
pub func fetch_response_set_body(this: FetchResponse*, body: FetchBody*?):
    ((const FetchResponseVTable*)((gc.Cell*)this).vtable).set_body(this, body)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:90 Web::Fetch::Infrastructure::Response::cache_state (virtual).
pub func fetch_response_cache_state_impl(this: FetchResponse*) -> FetchResponseCacheState?:
    return this.m_cache_state

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:91 Web::Fetch::Infrastructure::Response::set_cache_state (virtual).
pub func fetch_response_set_cache_state_impl(this: FetchResponse*, cache_state: FetchResponseCacheState?):
    this.m_cache_state = cache_state

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:93 Web::Fetch::Infrastructure::Response::cors_exposed_header_name_list (virtual).
pub func fetch_response_cors_exposed_header_name_list_impl(this: FetchResponse*) -> const ak.Vector[ak.ByteString]*:
    return &this.m_cors_exposed_header_name_list

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:94 Web::Fetch::Infrastructure::Response::set_cors_exposed_header_name_list (virtual).
pub func fetch_response_set_cors_exposed_header_name_list_impl(this: FetchResponse*, cors_exposed_header_name_list: ak.Vector[ak.ByteString]):
    this.m_cors_exposed_header_name_list = cors_exposed_header_name_list

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:96 Web::Fetch::Infrastructure::Response::range_requested (virtual).
pub func fetch_response_range_requested_impl(this: FetchResponse*) -> bool:
    return this.m_range_requested

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:97 Web::Fetch::Infrastructure::Response::set_range_requested (virtual).
pub func fetch_response_set_range_requested_impl(this: FetchResponse*, range_requested: bool):
    this.m_range_requested = range_requested

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:99 Web::Fetch::Infrastructure::Response::request_includes_credentials (virtual).
pub func fetch_response_request_includes_credentials_impl(this: FetchResponse*) -> bool:
    return this.m_request_includes_credentials

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:100 Web::Fetch::Infrastructure::Response::set_request_includes_credentials (virtual).
pub func fetch_response_set_request_includes_credentials_impl(this: FetchResponse*, request_includes_credentials: bool):
    this.m_request_includes_credentials = request_includes_credentials

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:102 Web::Fetch::Infrastructure::Response::timing_allow_passed (virtual).
pub func fetch_response_timing_allow_passed_impl(this: FetchResponse*) -> bool:
    return this.m_timing_allow_passed

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:103 Web::Fetch::Infrastructure::Response::set_timing_allow_passed (virtual).
pub func fetch_response_set_timing_allow_passed_impl(this: FetchResponse*, timing_allow_passed: bool):
    this.m_timing_allow_passed = timing_allow_passed

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:105 Web::Fetch::Infrastructure::Response::body_info (virtual).
pub func fetch_response_body_info_impl(this: FetchResponse*) -> const FetchResponseBodyInfo*:
    return &this.m_body_info

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:106 Web::Fetch::Infrastructure::Response::set_body_info (virtual).
pub func fetch_response_set_body_info_impl(this: FetchResponse*, body_info: FetchResponseBodyInfo):
    this.m_body_info = body_info

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:58 Web::Fetch::Infrastructure::Response::BodyInfo::operator==
## (defaulted: every member equal).
pub func fetch_response_body_info_eq(this: const FetchResponseBodyInfo*, other: const FetchResponseBodyInfo*) -> bool:
    return this.encoded_size == other.encoded_size and this.decoded_size == other.decoded_size and ak.string_equals(&this.content_type, &other.content_type)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:108 Web::Fetch::Infrastructure::Response::redirect_taint.
pub func fetch_response_redirect_taint(this: FetchResponse*) -> FetchRedirectTaint:
    return this.m_redirect_taint

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:109 Web::Fetch::Infrastructure::Response::set_redirect_taint.
pub func fetch_response_set_redirect_taint(this: FetchResponse*, redirect_taint: FetchRedirectTaint):
    this.m_redirect_taint = redirect_taint

# mark: Network errors, URLs ====================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:80 Web::Fetch::Infrastructure::Response::is_aborted_network_error.
# https://fetch.spec.whatwg.org/#concept-aborted-network-error
pub func fetch_response_is_aborted_network_error(this: FetchResponse*) -> bool:
    # A response whose type is "error" and aborted flag is set is known as an aborted network error.
    # NOTE: We have to use the virtual getter here to not bypass filtered responses.
    return fetch_response_type(this) == FetchResponseType.error_ and fetch_response_aborted(this)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:88 Web::Fetch::Infrastructure::Response::is_network_error.
# https://fetch.spec.whatwg.org/#concept-network-error
pub func fetch_response_is_network_error(this: FetchResponse*) -> bool:
    # A network error is a response whose type is "error", status is 0, status message is the empty byte sequence,
    # header list is « », body is null, and body info is a new response body info.
    # NOTE: We have to use the virtual getter here to not bypass filtered responses.
    if fetch_response_type(this) != FetchResponseType.error_:
        return false
    if fetch_response_status(this) != 0:
        return false
    let status_message = fetch_response_status_message(this)
    if not ak.byte_string_is_empty(&status_message):
        return false
    if not http_header_list_is_empty(fetch_response_header_list(this)):
        return false
    if fetch_response_body(this) != none:
        return false
    let new_body_info = FetchResponseBodyInfo(encoded_size = 0, decoded_size = 0, content_type = ak.string_new())
    if not fetch_response_body_info_eq(fetch_response_body_info(this), &new_body_info):
        return false
    return true

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:109 Web::Fetch::Infrastructure::Response::url.
# https://fetch.spec.whatwg.org/#concept-response-url
pub func fetch_response_url(this: FetchResponse*) -> web_url.Url?:
    # A response has an associated URL. It is a pointer to the last URL in response’s URL list and null if response’s URL list is empty.
    # NOTE: We have to use the virtual getter here to not bypass filtered responses.
    let url_list = fetch_response_url_list_const(this)
    if ak.vector_is_empty(url_list):
        return none
    return *ak.vector_last_const(url_list)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:119 Web::Fetch::Infrastructure::Response::location_url.
# https://fetch.spec.whatwg.org/#concept-response-location-url
pub func fetch_response_location_url(this: FetchResponse*, request_fragment: ak.String?) -> web_url.Url?!:
    # The location URL of a response response, given null or an ASCII string requestFragment, is the value returned by the following steps. They return null, failure, or a URL.

    # 1. If response’s status is not a redirect status, then return null.
    # NOTE: We have to use the virtual getter here to not bypass filtered responses.
    if not fetch_is_redirect_status(fetch_response_status(this)):
        return none

    # 2. Let location be the result of extracting header list values given `Location` and response’s header list.
    let location_values_or_failure = http_header_list_extract_header_list_values(this.m_header_list, ak.sv("Location"))
    var location_values: ak.Vector[ak.ByteString]
    match location_values_or_failure:
        .vector_byte_string(values): location_values = values
        _: return none

    if ak.vector_size(&location_values) != 1:
        return none

    # 3. If location is a header value, then set location to the result of parsing location with response’s URL.
    var location = domurl_parse(ak.byte_string_view(ak.vector_first(&location_values)), fetch_response_url(this)) else error(ak.ak_error, "Invalid 'Location' header URL")

    # 4. If location is a URL whose fragment is null, then set location’s fragment to requestFragment.
    if web_url.url_fragment(&location) == none:
        web_url.url_set_fragment(&location, request_fragment)

    # 5. Return location.
    return location

# mark: Cloning =================================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:149 Web::Fetch::Infrastructure::Response::clone.
# https://fetch.spec.whatwg.org/#concept-response-clone
pub func fetch_response_clone(this: FetchResponse*, realm: Realm*) -> FetchResponse*:
    # To clone a response response, run these steps:
    let vm = realm_vm(realm)

    # 1. If response is a filtered response, then return a new identical filtered response whose internal response is a clone of response’s internal response.
    if let filtered = as_if_fetch_filtered_response(this):
        let internal_response = fetch_response_clone(filtered.m_internal_response, realm)
        if is_fetch_basic_filtered_response(this):
            return (FetchResponse*)fetch_basic_filtered_response_create(vm, internal_response)
        if is_fetch_cors_filtered_response(this):
            return (FetchResponse*)fetch_cors_filtered_response_create(vm, internal_response)
        if is_fetch_opaque_filtered_response(this):
            return (FetchResponse*)fetch_opaque_filtered_response_create(vm, internal_response)
        if is_fetch_opaque_redirect_filtered_response(this):
            return (FetchResponse*)fetch_opaque_redirect_filtered_response_create(vm, internal_response)
        trap("VERIFY_NOT_REACHED")

    # 2. Let newResponse be a copy of response, except for its body.
    let new_response = fetch_infrastructure_response_create(vm)
    fetch_response_set_type(new_response, this.m_type)
    fetch_response_set_aborted(new_response, this.m_aborted)
    fetch_response_set_url_list(new_response, ak.vector_clone(&this.m_url_list))
    fetch_response_set_status(new_response, this.m_status)
    fetch_response_set_status_message(new_response, this.m_status_message)
    for header in &ak.vector_span(&this.m_header_list.m_headers):
        http_header_list_append(fetch_response_header_list(new_response), *header)
    fetch_response_set_cache_state(new_response, this.m_cache_state)
    fetch_response_set_cors_exposed_header_name_list(new_response, ak.vector_clone(&this.m_cors_exposed_header_name_list))
    fetch_response_set_range_requested(new_response, this.m_range_requested)
    fetch_response_set_request_includes_credentials(new_response, this.m_request_includes_credentials)
    fetch_response_set_timing_allow_passed(new_response, this.m_timing_allow_passed)
    fetch_response_set_body_info(new_response, this.m_body_info)
    # FIXME: service worker timing info

    # 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body.
    if let body = this.m_body:
        fetch_response_set_body(new_response, fetch_body_clone(body, realm))

    # 4. Return newResponse.
    return new_response

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:194 Web::Fetch::Infrastructure::Response::unsafe_response.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#unsafe-response
pub func fetch_response_unsafe_response(this: FetchResponse*) -> FetchResponse*:
    # A response's unsafe response is its internal response if it has one, and the response itself otherwise.
    if let filtered = as_if_fetch_filtered_response(this):
        return filtered.m_internal_response

    return this

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:204 Web::Fetch::Infrastructure::Response::is_cors_same_origin.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-same-origin
pub func fetch_response_is_cors_same_origin(this: FetchResponse*) -> bool:
    # A response whose type is "basic", "cors", or "default" is CORS-same-origin. [FETCH]
    match fetch_response_type(this):
        .basic, .cors, .default: return true
        _: return false

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp:218 Web::Fetch::Infrastructure::Response::is_cors_cross_origin.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-cross-origin
pub func fetch_response_is_cors_cross_origin(this: FetchResponse*) -> bool:
    # A response whose type is "opaque" or "opaqueredirect" is CORS-cross-origin.
    let type_ = fetch_response_type(this)
    return type_ == FetchResponseType.opaque or type_ == FetchResponseType.opaque_redirect

# mark: Responses.h's other members =============================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:125 Web::Fetch::Infrastructure::Response::network_error_message.
pub func fetch_response_network_error_message(this: FetchResponse*) -> ak.String?:
    return this.m_network_error_message

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:126 Web::Fetch::Infrastructure::Response::monotonic_response_time.
pub func fetch_response_monotonic_response_time(this: FetchResponse*) -> ak.MonotonicTime:
    return this.m_monotonic_response_time

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:200 Web::Fetch::Infrastructure::Response::method.
pub func fetch_response_method(this: FetchResponse*) -> ak.ByteString:
    return this.m_method

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h:201 Web::Fetch::Infrastructure::Response::set_method.
pub func fetch_response_set_method(this: FetchResponse*, method: ak.ByteString):
    this.m_method = method