Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

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

#==============================================================================================
#
#   mime - extracting a MIME type from a header list, and the legacy encoding extraction
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Infrastructure/HTTP/MIME.cpp (lines 14-91) with MIME.h.
#       legacy_extract_an_encoding was ported whole for region r21's encoding sniffing (it was
#       a closure stub until region p2b).
#
#       Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
#       Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/MIME.cpp:16 Web::Fetch::Infrastructure::extract_mime_type.
# https://fetch.spec.whatwg.org/#concept-header-extract-mime-type
pub func fetch_extract_mime_type(headers: const HttpHeaderList*) -> MimeType?:
    # 1. Let charset be null.
    var charset: ak.String? = none

    # 2. Let essence be null.
    var essence: ak.String? = none

    # 3. Let mimeType be null.
    var mime_type: MimeType? = none

    # 4. Let values be the result of getting, decoding, and splitting `Content-Type` from headers.
    let values = http_header_list_get_decode_and_split(headers, ak.sv("Content-Type"))

    # 5. If values is null, then return failure.
    var actual_values = values else return none

    # 6. For each value of values:
    for value in &ak.vector_span(&actual_values):
        # 1. Let temporaryMimeType be the result of parsing value.
        let temporary_mime_type = mime_type_parse(ak.string_bytes_as_string_view(value))

        # 2. If temporaryMimeType is failure or its essence is "*/*", then continue.
        var current = temporary_mime_type else continue
        let current_essence = mime_type_essence(&current)
        if ak.string_eq_string_view(&current_essence, ak.sv("*/*")):
            continue

        # 3. Set mimeType to temporaryMimeType.
        # (`current` is mimeType from here on: C++ mutates the copy it stored.)

        # 4. If mimeType’s essence is not essence, then:
        let essence_differs = match essence:
            .some(previous_essence) => not ak.string_equals(&current_essence, &previous_essence)
            .none => true
        if essence_differs:
            # 1. Set charset to null.
            charset = none

            # 2. If mimeType’s parameters["charset"] exists, then set charset to mimeType’s parameters["charset"].
            if let it = ak.ordered_hash_map_get_k_const(mime_type_parameters(&current), ak.string_from_str("charset")):
                charset = *it

            # 3. Set essence to mimeType’s essence.
            essence = current_essence
        # 5. Otherwise, if mimeType’s parameters["charset"] does not exist, and charset is non-null, set mimeType’s parameters["charset"] to charset.
        elif not ak.ordered_hash_map_contains_k(mime_type_parameters(&current), ak.string_from_str("charset")) and charset != none:
            mime_type_set_parameter(&current, ak.string_from_str("charset"), charset else trap("unreachable: checked above"))
            charset = none  # charset.release_value()

        mime_type = current

    # 7. If mimeType is null, then return failure.
    # 8. Return mimeType.
    return mime_type

## Ported from Libraries/LibWeb/Fetch/Infrastructure/HTTP/MIME.cpp:71 Web::Fetch::Infrastructure::legacy_extract_an_encoding.
# https://fetch.spec.whatwg.org/#legacy-extract-an-encoding
pub func fetch_legacy_extract_an_encoding(mime_type: MimeType?, fallback_encoding: ak.StringView) -> ak.StringView:
    # 1. If mimeType is failure, then return fallbackEncoding.
    let actual_mime_type = mime_type else return fallback_encoding

    # 2. If mimeType["charset"] does not exist, then return fallbackEncoding.
    let charset = ak.ordered_hash_map_get_k_const(mime_type_parameters(&actual_mime_type), ak.string_from_str("charset")) else return fallback_encoding

    # 3. Let tentativeEncoding be the result of getting an encoding from mimeType["charset"].
    let tentative_encoding = text_codec.get_standardized_encoding(ak.string_bytes_as_string_view(charset))

    # 4. If tentativeEncoding is failure, then return fallbackEncoding.
    # 5. Return tentativeEncoding.
    return tentative_encoding else fallback_encoding