Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

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

#==============================================================================================
#
#   requests_1 - Fetch::Infrastructure::Request: creation, URLs, subresource and navigation
#   requests, redirect-taint, serializing the origin, cloning, the range and origin headers
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp (lines 21-374) with
#       Requests.h's non-standard pending-response members (lines 310-325). The accessors of
#       Requests.h and the destination, mode and initiator-type strings are in requests_2.
#
#       Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2023, networkException <networkexception@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

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

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:23 Web::Fetch::Infrastructure::Request::create.
pub func fetch_request_create(vm: Vm*) -> FetchRequest*:
    let heap = vm_heap(vm)
    let request = (FetchRequest*)web_heap_allocate_cell(heap, &fetch_request_class)
    gc.heap_defer_gc(heap)
    fetch_request_construct(request, http_header_list_create())
    gc.heap_undefer_gc(heap)
    return request

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:28 Web::Fetch::Infrastructure::Request::Request (private),
## after the default member initializers (those the generator could not translate are written here).
func fetch_request_construct(this: FetchRequest*, header_list: HttpHeaderList*):
    fetch_request_init_fields(this)
    this.m_method = ak.byte_string_from_str("GET")
    this.m_traversable_for_user_prompts = .fetch_request_traversable_for_user_prompts(FetchRequestTraversableForUserPrompts.client)
    this.m_origin = .fetch_request_origin(FetchRequestOrigin.client)
    this.m_policy_container = .fetch_request_policy_container(FetchRequestPolicyContainer.client)
    this.m_referrer = .fetch_request_referrer(FetchRequestReferrer.client)
    this.m_header_list = header_list
    this.m_request_time = ak.unix_date_time_now()

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:34 Web::Fetch::Infrastructure::Request::visit_edges (override).
pub func fetch_request_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (FetchRequest*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_client)
    match this.m_body:
        .fetch_body(body): gc.cell_visitor_visit_t_2(visitor, body)
        _: _ = 0
    gc.cell_visitor_visit_t(visitor, this.m_reserved_client)
    match this.m_traversable_for_user_prompts:
        .html_environment_settings_object(value): gc.cell_visitor_visit_t(visitor, value)
        .html_traversable_navigable(value): gc.cell_visitor_visit_t(visitor, value)
        _: _ = 0
    gc.cell_visitor_visit_vector(visitor, &this.m_pending_responses)
    match this.m_policy_container:
        .html_policy_container(policy_container): gc.cell_visitor_visit_t_2(visitor, policy_container)
        _: _ = 0

# mark: URLs ====================================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:53 Web::Fetch::Infrastructure::Request::url.
# https://fetch.spec.whatwg.org/#concept-request-url
pub func fetch_request_url(this: FetchRequest*) -> web_url.Url:
    # A request has an associated URL (a URL).
    # NOTE: Implementations are encouraged to make this a pointer to the first URL in request’s URL list. It is provided as a distinct field solely for the convenience of other standards hooking into Fetch.
    assert(not ak.vector_is_empty(&this.m_url_list))
    return *ak.vector_first(&this.m_url_list)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:62 Web::Fetch::Infrastructure::Request::url (const).
# https://fetch.spec.whatwg.org/#concept-request-url
pub func fetch_request_url_const(this: FetchRequest*) -> web_url.Url:
    return fetch_request_url(this)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:68 Web::Fetch::Infrastructure::Request::current_url.
# https://fetch.spec.whatwg.org/#concept-request-current-url
pub func fetch_request_current_url(this: FetchRequest*) -> web_url.Url:
    # A request has an associated current URL. It is a pointer to the last URL in request’s URL list.
    assert(not ak.vector_is_empty(&this.m_url_list))
    return *ak.vector_last(&this.m_url_list)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:76 Web::Fetch::Infrastructure::Request::current_url (const).
# https://fetch.spec.whatwg.org/#concept-request-current-url
pub func fetch_request_current_url_const(this: FetchRequest*) -> web_url.Url:
    return fetch_request_current_url(this)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:81 Web::Fetch::Infrastructure::Request::set_url.
pub func fetch_request_set_url(this: FetchRequest*, url: web_url.Url):
    # Sometimes setting the URL and URL list are done as two distinct steps in the spec,
    # but since we know the URL is always the URL list's first item and doesn't change later
    # on, we can combine them.
    if not ak.vector_is_empty(&this.m_url_list):
        ak.vector_clear(&this.m_url_list)
    ak.vector_append_t(&this.m_url_list, url)

# mark: Destinations ============================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:92 Web::Fetch::Infrastructure::Request::destination_is_script_like.
# https://fetch.spec.whatwg.org/#request-destination-script-like
pub func fetch_request_destination_is_script_like(this: FetchRequest*) -> bool:
    if let destination = this.m_destination:
        return fetch_destination_is_script_like(destination)
    return false

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:98 Web::Fetch::Infrastructure::Request::is_subresource_request.
# https://fetch.spec.whatwg.org/#subresource-request
pub func fetch_request_is_subresource_request(this: FetchRequest*) -> bool:
    # A subresource request is a request whose destination is "audio", "audioworklet", "font", "image", "json", "manifest", "paintworklet", "script", "style", "track", "video", "xslt", or the empty string.
    let subresource_request_destinations: FetchRequestDestination[12] = [
        FetchRequestDestination.audio,
        FetchRequestDestination.audio_worklet,
        FetchRequestDestination.font,
        FetchRequestDestination.image,
        FetchRequestDestination.json,
        FetchRequestDestination.manifest,
        FetchRequestDestination.paint_worklet,
        FetchRequestDestination.script,
        FetchRequestDestination.style,
        FetchRequestDestination.track,
        FetchRequestDestination.video,
        FetchRequestDestination.xslt,
    ]
    for destination in subresource_request_destinations:
        if this.m_destination == destination:
            return true
    return this.m_destination == none

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:121 Web::Fetch::Infrastructure::Request::is_non_subresource_request.
# https://fetch.spec.whatwg.org/#non-subresource-request
pub func fetch_request_is_non_subresource_request(this: FetchRequest*) -> bool:
    # A non-subresource request is a request whose destination is "document", "embed", "frame", "iframe", "object", "report", "serviceworker", "sharedworker", or "worker".
    let non_subresource_request_destinations: FetchRequestDestination[9] = [
        FetchRequestDestination.document,
        FetchRequestDestination.embed,
        FetchRequestDestination.frame,
        FetchRequestDestination.i_frame,
        FetchRequestDestination.object,
        FetchRequestDestination.report,
        FetchRequestDestination.service_worker,
        FetchRequestDestination.shared_worker,
        FetchRequestDestination.worker,
    ]
    for destination in non_subresource_request_destinations:
        if this.m_destination == destination:
            return true
    return false

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:141 Web::Fetch::Infrastructure::Request::is_navigation_request.
# https://fetch.spec.whatwg.org/#navigation-request
pub func fetch_request_is_navigation_request(this: FetchRequest*) -> bool:
    # A navigation request is a request whose destination is "document", "embed", "frame", "iframe", or "object".
    let navigation_request_destinations: FetchRequestDestination[5] = [
        FetchRequestDestination.document,
        FetchRequestDestination.embed,
        FetchRequestDestination.frame,
        FetchRequestDestination.i_frame,
        FetchRequestDestination.object,
    ]
    for destination in navigation_request_destinations:
        if this.m_destination == destination:
            return true
    return false

# mark: Redirect-taint and origins ==============================================================

## `m_origin.get_pointer<URL::Origin>()`: the request's origin when it is an origin rather than "client".
func fetch_request_origin_if_origin(this: FetchRequest*) -> web_url.Origin?:
    match this.m_origin:
        .origin(origin): return origin
        .fetch_request_origin(_): return none

## `if (auto const* origin = m_origin.get_pointer<Origin>()) VERIFY(*origin != Origin::Client);`
func fetch_request_assert_origin_is_not_client(this: FetchRequest*):
    match this.m_origin:
        .fetch_request_origin(origin): assert(origin != FetchRequestOrigin.client)
        .origin(_): return

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:157 Web::Fetch::Infrastructure::Request::redirect_taint.
# https://fetch.spec.whatwg.org/#concept-request-tainted-origin
pub func fetch_request_redirect_taint(this: FetchRequest*) -> FetchRedirectTaint:
    # 1. Assert: request’s origin is not "client".
    fetch_request_assert_origin_is_not_client(this)

    # 2. Let lastURL be null.
    var last_url: web_url.Url? = none

    # 3. Let taint be "same-origin".
    var taint = FetchRedirectTaint.same_origin

    # 4. For each url of request’s URL list:
    for url in &ak.vector_span(&this.m_url_list):
        # 1. If lastURL is null, then set lastURL to url and continue.
        if last_url == none:
            last_url = *url
            continue
        let last = last_url else trap("unreachable: checked above")

        # 2. If url’s origin is not same site with lastURL’s origin and request’s origin is not same site with
        #    lastURL’s origin, then return "cross-site".
        let request_origin = fetch_request_origin_if_origin(this)
        let url_origin = web_url.url_origin(url)
        let last_url_origin = web_url.url_origin(&last)
        if not web_url.origin_is_same_site(&url_origin, last_url_origin):
            let request_origin_is_same_site = match request_origin:
                .some(origin) => web_url.origin_is_same_site(&origin, last_url_origin)
                .none => false
            if not request_origin_is_same_site:
                return FetchRedirectTaint.cross_site

        # 3. If url’s origin is not same origin with lastURL’s origin and request’s origin is not same origin with
        #    lastURL’s origin, then set taint to "same-site".
        if not web_url.origin_is_same_origin(&url_origin, last_url_origin):
            let request_origin_is_same_origin = match request_origin:
                .some(origin) => web_url.origin_is_same_origin(&origin, last_url_origin)
                .none => false
            if not request_origin_is_same_origin:
                taint = FetchRedirectTaint.same_site

        # 4. Set lastURL to url.
        last_url = *url

    # 5. Return taint.
    return taint

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:201 Web::Fetch::Infrastructure::Request::serialize_origin.
# https://fetch.spec.whatwg.org/#serializing-a-request-origin
pub func fetch_request_serialize_origin(this: FetchRequest*) -> ak.String:
    # 1. Assert: request’s origin is not "client".
    fetch_request_assert_origin_is_not_client(this)

    # 2. If request’s redirect-taint is not "same-origin", then return "null".
    if fetch_request_redirect_taint(this) != FetchRedirectTaint.same_origin:
        return ak.string_from_str("null")

    # 3. Return request’s origin, serialized.
    let origin = fetch_request_origin_if_origin(this) else trap("VERIFY: m_origin.has<URL::Origin>()")
    return web_url.origin_serialize(&origin)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:216 Web::Fetch::Infrastructure::Request::byte_serialize_origin.
# https://fetch.spec.whatwg.org/#byte-serializing-a-request-origin
pub func fetch_request_byte_serialize_origin(this: FetchRequest*) -> ak.ByteString:
    # Byte-serializing a request origin, given a request request, is to return the result of serializing a request
    # origin with request, isomorphic encoded.
    let serialized = fetch_request_serialize_origin(this)
    return text_codec.isomorphic_encode(ak.string_bytes_as_string_view(&serialized))

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

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:224 Web::Fetch::Infrastructure::Request::clone.
# https://fetch.spec.whatwg.org/#concept-request-clone
pub func fetch_request_clone(this: FetchRequest*, realm: Realm*) -> FetchRequest*:
    # To clone a request request, run these steps:
    let vm = realm_vm(realm)

    # 1. Let newRequest be a copy of request, except for its body.
    let new_request = fetch_request_create(vm)
    fetch_request_set_method(new_request, this.m_method)
    fetch_request_set_local_urls_only(new_request, this.m_local_urls_only)
    for header in &ak.vector_span(&this.m_header_list.m_headers):
        http_header_list_append(fetch_request_header_list(new_request), *header)
    fetch_request_set_unsafe_request(new_request, this.m_unsafe_request)
    fetch_request_set_client(new_request, this.m_client)
    fetch_request_set_reserved_client(new_request, this.m_reserved_client)
    fetch_request_set_replaces_client_id(new_request, this.m_replaces_client_id)
    fetch_request_set_traversable_for_user_prompts(new_request, this.m_traversable_for_user_prompts)
    fetch_request_set_keepalive(new_request, this.m_keepalive)
    fetch_request_set_initiator_type(new_request, this.m_initiator_type)
    fetch_request_set_service_workers_mode(new_request, this.m_service_workers_mode)
    fetch_request_set_initiator(new_request, this.m_initiator)
    fetch_request_set_destination(new_request, this.m_destination)
    fetch_request_set_priority(new_request, this.m_priority)
    fetch_request_set_origin(new_request, this.m_origin)
    fetch_request_set_policy_container(new_request, this.m_policy_container)
    fetch_request_set_referrer(new_request, this.m_referrer)
    fetch_request_set_referrer_policy(new_request, this.m_referrer_policy)
    fetch_request_set_mode(new_request, this.m_mode)
    fetch_request_set_use_cors_preflight(new_request, this.m_use_cors_preflight)
    fetch_request_set_credentials_mode(new_request, this.m_credentials_mode)
    fetch_request_set_use_url_credentials(new_request, this.m_use_url_credentials)
    fetch_request_set_cache_mode(new_request, this.m_cache_mode)
    fetch_request_set_redirect_mode(new_request, this.m_redirect_mode)
    fetch_request_set_integrity_metadata(new_request, this.m_integrity_metadata)
    fetch_request_set_cryptographic_nonce_metadata(new_request, this.m_cryptographic_nonce_metadata)
    fetch_request_set_parser_metadata(new_request, this.m_parser_metadata)
    fetch_request_set_reload_navigation(new_request, this.m_reload_navigation)
    fetch_request_set_history_navigation(new_request, this.m_history_navigation)
    fetch_request_set_user_activation(new_request, this.m_user_activation)
    fetch_request_set_render_blocking(new_request, this.m_render_blocking)
    fetch_request_set_url_list(new_request, ak.vector_clone(&this.m_url_list))
    fetch_request_set_redirect_count(new_request, this.m_redirect_count)
    fetch_request_set_response_tainting(new_request, this.m_response_tainting)
    fetch_request_set_prevent_no_cache_cache_control_header_modification(new_request, this.m_prevent_no_cache_cache_control_header_modification)
    fetch_request_set_done(new_request, this.m_done)
    fetch_request_set_timing_allow_failed(new_request, this.m_timing_allow_failed)

    # 2. If request’s body is non-null, set newRequest’s body to the result of cloning request’s body.
    match this.m_body:
        .fetch_body(body): fetch_request_set_body(new_request, .fetch_body(fetch_body_clone(body, realm)))
        _: _ = 0

    # 3. Return newRequest.
    return new_request

# mark: Headers =================================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:279 Web::Fetch::Infrastructure::Request::add_range_header.
# https://fetch.spec.whatwg.org/#concept-request-add-range-header
pub func fetch_request_add_range_header(this: FetchRequest*, first: u64, last: u64?):
    # To add a range header to a request request, with an integer first, and an optional integer last, run these steps:

    # 1. Assert: last is not given, or first is less than or equal to last.
    if let last_value = last:
        assert(first <= last_value)

    # 2. Let rangeValue be `bytes=`.
    # 3. Serialize and isomorphic encode first, and append the result to rangeValue.
    # 4. Append 0x2D (-) to rangeValue.
    # 5. If last is given, then serialize and isomorphic encode it, and append the result to rangeValue.
    var builder = ak.string_builder_new()
    ak.string_builder_append(&builder, "bytes=")
    ak.format_u64(&builder, first)
    ak.string_builder_append(&builder, "-")
    if let last_value = last:
        ak.format_u64(&builder, last_value)
    let range_value = ak.string_builder_to_byte_string(&builder)

    # 6. Append (`Range`, rangeValue) to request’s header list.
    http_header_list_append(this.m_header_list, HttpHeader(name = ak.byte_string_from_str("Range"), value = range_value))

## `origin.scheme() == "https"sv` of a tuple origin.
func fetch_request_origin_scheme_is_https(origin: const web_url.Origin*) -> bool:
    let scheme = web_url.origin_scheme(origin) else return false
    return ak.string_eq_string_view(&scheme, ak.sv("https"))

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:299 Web::Fetch::Infrastructure::Request::add_origin_header.
# https://fetch.spec.whatwg.org/#append-a-request-origin-header
pub func fetch_request_add_origin_header(this: FetchRequest*):
    # 1. Let serializedOrigin be the result of byte-serializing a request origin with request.
    var serialized_origin = fetch_request_byte_serialize_origin(this)

    # 2. If request’s response tainting is "cors" or request’s mode is "websocket", then append (`Origin`, serializedOrigin) to request’s header list.
    if this.m_response_tainting == FetchRequestResponseTainting.cors or this.m_mode == FetchRequestMode.web_socket:
        http_header_list_append(this.m_header_list, HttpHeader(name = ak.byte_string_from_str("Origin"), value = serialized_origin))
    # 3. Otherwise, if request’s method is neither `GET` nor `HEAD`, then:
    elif not (ak.byte_string_eq_string_view(&this.m_method, ak.sv("GET")) or ak.byte_string_eq_string_view(&this.m_method, ak.sv("HEAD"))):
        # 1. If request’s mode is not "cors", then switch on request’s referrer policy:
        if this.m_mode != FetchRequestMode.cors:
            match this.m_referrer_policy:
                # -> "no-referrer"
                .no_referrer:
                    # Set serializedOrigin to `null`.
                    serialized_origin = ak.byte_string_from_str("null")
                # -> "no-referrer-when-downgrade"
                # -> "strict-origin"
                # -> "strict-origin-when-cross-origin"
                .no_referrer_when_downgrade, .strict_origin, .strict_origin_when_cross_origin:
                    # If request’s origin is a tuple origin, its scheme is "https", and request’s current URL’s scheme is
                    # not "https", then set serializedOrigin to `null`.
                    if let origin = fetch_request_origin_if_origin(this):
                        if not web_url.origin_is_opaque(&origin) and fetch_request_origin_scheme_is_https(&origin):
                            let current_url = fetch_request_current_url(this)
                            let current_url_scheme = web_url.url_scheme(&current_url)
                            if not ak.string_eq_string_view(&current_url_scheme, ak.sv("https")):
                                serialized_origin = ak.byte_string_from_str("null")
                # -> "same-origin"
                .same_origin:
                    # If request’s origin is not same origin with request’s current URL’s origin, then set serializedOrigin
                    # to `null`.
                    if let origin = fetch_request_origin_if_origin(this):
                        let current_url = fetch_request_current_url(this)
                        if not web_url.origin_is_same_origin(&origin, web_url.url_origin(&current_url)):
                            serialized_origin = ak.byte_string_from_str("null")
                # -> Otherwise
                _:
                    # Do nothing.
                    _ = 0

        # 2. Append (`Origin`, serializedOrigin) to request’s header list.
        http_header_list_append(this.m_header_list, HttpHeader(name = ak.byte_string_from_str("Origin"), value = serialized_origin))

# mark: Cross-Origin-Embedder-Policy ============================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp:349 Web::Fetch::Infrastructure::Request::cross_origin_embedder_policy_allows_credentials.
# https://fetch.spec.whatwg.org/#cross-origin-embedder-policy-allows-credentials
pub func fetch_request_cross_origin_embedder_policy_allows_credentials(this: FetchRequest*) -> bool:
    # 1. Assert: request’s origin is not "client".
    fetch_request_assert_origin_is_not_client(this)

    # 2. If request’s mode is not "no-cors", then return true.
    if this.m_mode != FetchRequestMode.no_cors:
        return true

    # 3. If request’s client is null, then return true.
    if this.m_client == none:
        return true

    # 4. If request’s client’s policy container’s embedder policy’s value is not "credentialless", then return true.
    match this.m_policy_container:
        .html_policy_container(policy_container):
            if policy_container.embedder_policy.value != HtmlEmbedderPolicyValue.credentialless:
                return true
        _: _ = 0

    # 5. If request’s origin is same origin with request’s current URL’s origin and request’s redirect-taint is not
    #    "same-origin", then return true.
    # 6. Return false.
    let request_origin = fetch_request_origin_if_origin(this) else return false

    let current_url = fetch_request_current_url(this)
    return web_url.origin_is_same_origin(&request_origin, web_url.url_origin(&current_url)) and fetch_request_redirect_taint(this) != FetchRedirectTaint.same_origin

# mark: Pending responses (non-standard) ========================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h:314 Web::Fetch::Infrastructure::Request::add_pending_response.
pub func fetch_request_add_pending_response(this: FetchRequest*, pending_response: FetchPendingResponse*):
    for existing in ak.vector_span(&this.m_pending_responses):
        assert(existing != pending_response)
    ak.vector_append_t(&this.m_pending_responses, pending_response)

## The lambda of Request::remove_pending_response: `gc_ptr == pending_response`.
func fetch_request_is_pending_response(context: void*?, gc_ptr: const (FetchPendingResponse*)*) -> bool:
    return (void*)*gc_ptr == context

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h:320 Web::Fetch::Infrastructure::Request::remove_pending_response.
pub func fetch_request_remove_pending_response(this: FetchRequest*, pending_response: FetchPendingResponse*):
    _ = ak.vector_remove_first_matching(&this.m_pending_responses, ak.Function1[const (FetchPendingResponse*)*, bool](call = fetch_request_is_pending_response, context = pending_response))

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h:325 Web::Fetch::Infrastructure::Request::request_time.
pub func fetch_request_request_time(this: FetchRequest*) -> ak.UnixDateTime:
    return this.m_request_time