Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / fetch / infrastructure / url.lucb

#==============================================================================================
#
#   url - Fetch's URL scheme predicates
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Infrastructure/URL.h (lines 23-50) and URL.cpp
#       (lines 18-112): the HTTP(S) scheme test MIME sniffing needs (MimeSniff/Resource.cpp),
#       local URLs and fetch schemes, which navigation needs (region r28: policy containers in
#       history, attempting to create a non-fetch scheme document), and the data: URL
#       processor (region p2a). DataURL is FetchDataUrl (types_url).
#
#       Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2023, Karol Kosek <krkk@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Web::Fetch::Infrastructure::LOCAL_SCHEMES (Fetch/Infrastructure/URL.h:23).
pub let fetch_local_schemes: str[3] = ["about", "blob", "data"]

## Web::Fetch::Infrastructure::HTTP_SCHEMES (Fetch/Infrastructure/URL.h:29).
pub let fetch_http_schemes: str[2] = ["http", "https"]

## Web::Fetch::Infrastructure::FETCH_SCHEMES (Fetch/Infrastructure/URL.h:35).
pub let fetch_fetch_schemes: str[7] = [
    "about",
    "blob",
    "data",
    "file",
    "http",
    "https",
    # AD-HOC: Internal fetch schemes:
    "resource",
]

## Ported from Libraries/LibWeb/Fetch/Infrastructure/URL.cpp:18 Web::Fetch::Infrastructure::is_local_url.
# https://fetch.spec.whatwg.org/#is-local
pub func fetch_is_local_url(url: web_url.Url) -> bool:
    # A URL is local if its scheme is a local scheme.
    let scheme = web_url.url_scheme(&url)
    for local_scheme in fetch_local_schemes:
        if ak.string_eq_string_view(&scheme, ak.sv(local_scheme)):
            return true
    return false

## Ported from Libraries/LibWeb/Fetch/Infrastructure/URL.cpp:26 Web::Fetch::Infrastructure::is_fetch_scheme.
# https://fetch.spec.whatwg.org/#fetch-scheme
pub func fetch_is_fetch_scheme(scheme: ak.StringView) -> bool:
    # A fetch scheme is "about", "blob", "data", "file", or an HTTP(S) scheme.
    for fetch_scheme in fetch_fetch_schemes:
        if ak.string_view_equals(&scheme, fetch_scheme):
            return true
    return false

## Ported from Libraries/LibWeb/Fetch/Infrastructure/URL.cpp:33 Web::Fetch::Infrastructure::is_http_or_https_scheme.
# https://fetch.spec.whatwg.org/#http-scheme
pub func fetch_is_http_or_https_scheme(scheme: ak.StringView) -> bool:
    # An HTTP(S) scheme is "http" or "https".
    for http_scheme in fetch_http_schemes:
        if ak.string_view_equals(&scheme, http_scheme):
            return true
    return false

## Ported from Libraries/LibWeb/Fetch/Infrastructure/URL.cpp:40 Web::Fetch::Infrastructure::process_data_url.
# https://fetch.spec.whatwg.org/#data-url-processor
pub func fetch_process_data_url(data_url: web_url.Url) -> FetchDataUrl!:
    # 1. Assert: dataURL’s scheme is "data".
    let scheme = web_url.url_scheme(&data_url)
    assert(ak.string_eq_string_view(&scheme, ak.sv("data")))

    # 2. Let input be the result of running the URL serializer on dataURL with exclude fragment set to true.
    let input_serialized = web_url.url_serialize(&data_url, web_url.ExcludeFragment.yes)
    var input = ak.string_bytes_as_string_view(&input_serialized)

    # 3. Remove the leading "data:" from input.
    input = ak.string_view_substring_view(&input, "data:".length)

    # 4. Let position point at the start of input.

    # 5. Let mimeType be the result of collecting a sequence of code points that are not equal to U+002C (,), given position.
    var position = ak.string_view_find_u8(&input, ',')
    var mime_type = ak.string_view_substring_view_usize(&input, 0, position else ak.string_view_length(&input))

    # 6. Strip leading and trailing ASCII whitespace from mimeType.
    mime_type = ak.string_view_trim_whitespace(&mime_type, ak.TrimMode.both)

    # 7. If position is past the end of input, then return failure.
    if position == none:
        error(ak.ak_error, "Missing a comma character")

    # 8. Advance position by 1.
    position = (position else trap("unreachable: checked above")) + 1

    # 9. Let encodedBody be the remainder of input.
    let encoded_body = ak.string_view_substring_view(&input, position else trap("unreachable: set above"))

    # 10. Let body be the percent-decoding of encodedBody.
    let percent_decoded = web_url.percent_decode(encoded_body)
    var body = ak.byte_string_to_byte_buffer(&percent_decoded)

    # 11. If mimeType ends with U+003B (;), followed by zero or more U+0020 SPACE, followed by an ASCII case-insensitive match for "base64", then:
    if ak.string_view_ends_with_string_view(&mime_type, ak.sv("base64"), ak.CaseSensitivity.case_insensitive):
        var trimmed_substring_view = ak.string_view_substring_view_usize(&mime_type, 0, ak.string_view_length(&mime_type) - 6)
        trimmed_substring_view = ak.string_view_trim(&trimmed_substring_view, ak.sv(" "), ak.TrimMode.right)
        if ak.string_view_ends_with_u8(&trimmed_substring_view, ';'):
            # 1. Let stringBody be the isomorphic decode of body.
            let string_body = text_codec.isomorphic_decode(ak.string_view_from_bytes(ak.byte_buffer_bytes_const(&body)))

            # 2. Set body to the forgiving-base64 decode of stringBody.
            # 3. If body is failure, then return failure.
            body = try ak.decode_base64(ak.string_bytes_as_string_view(&string_body))

            # 4. Remove the last 6 code points from mimeType.
            # 5. Remove trailing U+0020 SPACE code points from mimeType, if any.
            # 6. Remove the last U+003B (;) from mimeType.
            mime_type = ak.string_view_substring_view_usize(&trimmed_substring_view, 0, ak.string_view_length(&trimmed_substring_view) - 1)

    # 12. If mimeType starts with ";", then prepend "text/plain" to mimeType.
    var builder = ak.string_builder_new()
    if ak.string_view_starts_with_u8(&mime_type, ';'):
        ak.string_builder_append_string_view(&builder, ak.sv("text/plain"))
        ak.string_builder_append_string_view(&builder, mime_type)
        mime_type = ak.string_builder_string_view(&builder)

    # 13. Let mimeTypeRecord be the result of parsing mimeType.
    var mime_type_record = mime_type_parse(mime_type)

    # 14. If mimeTypeRecord is failure, then set mimeTypeRecord to text/plain;charset=US-ASCII.
    if mime_type_record == none:
        var record = mime_type_create(ak.string_from_str("text"), ak.string_from_str("plain"))
        mime_type_set_parameter(&record, ak.string_from_str("charset"), ak.string_from_str("US-ASCII"))
        mime_type_record = record

    # 15. Return a new data: URL struct whose MIME type is mimeTypeRecord and body is body.
    return FetchDataUrl(mime_type = mime_type_record else trap("unreachable: set above"), body = body)