Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / mime_sniff / resource_3.lucb

#==============================================================================================
#
#   resource_3 - MimeSniff::Resource: the MIME type sniffing algorithm and the
#                context-specific sniffing algorithms
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/MimeSniff/Resource.cpp (lines 669-987) with the inline
#       accessors of Resource.h (lines 41-42). A Resource is a value; its resource header is
#       a ByteBuffer inside it, so resource_header() is a view into the struct. Where the C++
#       copies the supplied MimeType into the computed one, mime_type_copy copies it.
#
#       Copyright (c) 2023-2024, Kemal Zebari <kemalzebra@gmail.com>.
#       Copyright (c) 2025, Ben Eidson <b.e.eidson@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

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

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:342 Web::MimeSniff::Resource::create.
pub func mime_resource_create(data: const u8[], configuration: MimeSniffingConfiguration = MimeSniffingConfiguration()) -> MimeResource:
    # NOTE: Non-standard but for cases where pattern matching fails, let's fall back to the safest MIME type.
    let default_computed_mime_type = mime_type_create(ak.string_from_str("application"), ak.string_from_str("octet-stream"))
    var resource: MimeResource
    mime_resource_construct(&resource, data, configuration.no_sniff, default_computed_mime_type)

    mime_resource_supplied_mime_type_detection_algorithm(&resource, configuration.scheme, configuration.supplied_type)
    mime_resource_context_specific_sniffing_algorithm(&resource, configuration.sniffing_context)

    return resource

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:354 Web::MimeSniff::Resource::sniff.
pub func mime_resource_sniff(data: const u8[], configuration: MimeSniffingConfiguration = MimeSniffingConfiguration()) -> MimeType:
    let resource = mime_resource_create(data, configuration)
    return resource.m_computed_mime_type

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:360 Web::MimeSniff::Resource::Resource.
pub func mime_resource_construct(this: MimeResource*, data: const u8[], no_sniff: bool, default_computed_mime_type: MimeType):
    this.m_no_sniff = no_sniff
    this.m_computed_mime_type = default_computed_mime_type
    mime_resource_read_the_resource_header(this, data)

## Ported from Libraries/LibWeb/MimeSniff/Resource.h:41 Web::MimeSniff::Resource::computed_mime_type.
pub func mime_resource_computed_mime_type(this: const MimeResource*) -> const MimeType*:
    return &this.m_computed_mime_type

## Ported from Libraries/LibWeb/MimeSniff/Resource.h:42 Web::MimeSniff::Resource::resource_header.
pub func mime_resource_resource_header(this: const MimeResource*) -> const u8[]:
    return ak.byte_buffer_bytes_const(&this.m_resource_header)

# mark: The supplied MIME type and the resource header ==========================================

## Resource::supplied_mime_type_detection_algorithm's `apache_bug_mime_types`.
# NOTE: The spec expects a space between the semicolon and the start of the charset parameter. However, we will lose this
#       space because MimeType::parse() ignores any spaces found there.
let mime_resource_apache_bug_mime_types: str[4] = [
    "text/plain",
    "text/plain;charset=ISO-8859-1",
    "text/plain;charset=iso-8859-1",
    "text/plain;charset=UTF-8",
]

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:371 Web::MimeSniff::Resource::supplied_mime_type_detection_algorithm.
# https://mimesniff.spec.whatwg.org/#supplied-mime-type-detection-algorithm
# NOTE: Parameters are non-standard.
pub func mime_resource_supplied_mime_type_detection_algorithm(this: MimeResource*, scheme: ak.StringView, supplied_type: MimeType?):
    # 1. Let supplied-type be null.
    # 2. If the resource is retrieved via HTTP, execute the following steps:
    #        1. If one or more Content-Type headers are associated with the resource, execute the following steps:
    #               1. Set supplied-type to the value of the last Content-Type header associated with the resource.
    #               2. Set the check-for-apache-bug flag if supplied-type is exactly equal to one of the values in the following table:
    # NOTE: Non-standard but this algorithm expects the caller to handle step 2.1.1.
    if let type_ = supplied_type:
        if fetch_is_http_or_https_scheme(scheme):
            let serialized_supplied_type = mime_type_serialized(&type_)
            for apache_bug_mime_type in mime_resource_apache_bug_mime_types:
                if ak.string_eq_string_view(&serialized_supplied_type, ak.sv(apache_bug_mime_type)):
                    this.m_check_for_apache_bug_flag = true
                    break

    # 3. If the resource is retrieved directly from the file system, set supplied-type
    #    to the MIME type provided by the file system.
    # 4. If the resource is retrieved via another protocol (such as FTP), set
    #    supplied-type to the MIME type as determined by that protocol, if any.
    # 5. If supplied-type is not a MIME type, the supplied MIME type is undefined.
    #    Abort these steps.
    # 6. The supplied MIME type is supplied-type.
    # NOTE: The expectation is for the caller to handle these spec steps.
    this.m_supplied_mime_type = supplied_type

## Resource::read_the_resource_header's MAX_SNIFF_SIZE.
let mime_resource_max_sniff_size: usize = 1445

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:412 Web::MimeSniff::Resource::read_the_resource_header.
# https://mimesniff.spec.whatwg.org/#read-the-resource-header
pub func mime_resource_read_the_resource_header(this: MimeResource*, data: const u8[]):
    # 1. Let buffer be a byte sequence.
    var buffer: ak.ByteBuffer

    # 2. Read bytes of the resource into buffer until one of the following conditions is met:
    #      - the end of the resource is reached.
    #      - the number of bytes in buffer is greater than or equal to 1445.
    #      - a reasonable amount of time has elapsed, as determined by the user agent.
    # FIXME: The spec expects us to be reading from a stream. Reimplement this spec step once
    #        we have greater support for streaming in areas that calls on this API.
    let size = data.length if data.length < mime_resource_max_sniff_size else mime_resource_max_sniff_size
    ak.byte_buffer_append_span_u8(&buffer, data[0..<size])

    # 3. The resource header is buffer.
    this.m_resource_header = buffer

# mark: Sniffing ================================================================================

## `m_computed_mime_type = m_supplied_mime_type.value()`.
func mime_resource_compute_the_supplied_mime_type(this: MimeResource*):
    let supplied = this.m_supplied_mime_type else trap("VERIFY: the supplied MIME type")
    this.m_computed_mime_type = mime_type_copy(&supplied)

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:431 Web::MimeSniff::Resource::mime_type_sniffing_algorithm.
# https://mimesniff.spec.whatwg.org/#mime-type-sniffing-algorithm
pub func mime_resource_mime_type_sniffing_algorithm(this: MimeResource*):
    # 1. If the supplied MIME type is an XML MIME type or HTML MIME type, the computed MIME type is the supplied MIME type.
    #    Abort these steps.
    if let supplied = this.m_supplied_mime_type:
        if mime_type_is_xml(&supplied) or mime_type_is_html(&supplied):
            mime_resource_compute_the_supplied_mime_type(this)
            return

    # 2. If the supplied MIME type is undefined or if the supplied MIME type’s essence
    #    is "unknown/unknown", "application/unknown", or "*/*", execute the rules for
    #    identifying an unknown MIME type with the sniff-scriptable flag equal to the
    #    inverse of the no-sniff flag and abort these steps.
    var supplied_is_unknown = true
    if let supplied = this.m_supplied_mime_type:
        let essence = mime_type_essence(&supplied)
        supplied_is_unknown = (ak.string_eq_string_view(&essence, ak.sv("unknown/unknown")) or ak.string_eq_string_view(&essence, ak.sv("application/unknown")) or ak.string_eq_string_view(&essence, ak.sv("*/*")))
    if supplied_is_unknown:
        this.m_computed_mime_type = rules_for_identifying_an_unknown_mime_type(this, not this.m_no_sniff)
        return

    # 3. If the no-sniff flag is set, the computed MIME type is the supplied MIME type.
    #    Abort these steps.
    if this.m_no_sniff:
        mime_resource_compute_the_supplied_mime_type(this)
        return

    # 4. If the check-for-apache-bug flag is set, execute the rules for distinguishing
    #    if a resource is text or binary and abort these steps.
    if this.m_check_for_apache_bug_flag:
        mime_resource_rules_for_distinguishing_if_a_resource_is_text_or_binary(this)
        return

    # FIXME: 5. If the supplied MIME type is an image MIME type supported by the user agent, let matched-type be
    #    the result of executing the image type pattern matching algorithm with the resource header as
    #    the byte sequence to be matched.
    let matched_type: MimeType? = none

    # 6. If matched-type is not undefined, the computed MIME type is matched-type.
    #    Abort these steps.
    if let type_ = matched_type:
        this.m_computed_mime_type = type_
        return

    # FIXME: 7. If the supplied MIME type is an audio or video MIME type supported by the user agent, let matched-type be
    #    the result of executing the audio or video type pattern matching algorithm with the resource header as
    #    the byte sequence to be matched.

    # 8. If matched-type is not undefined, the computed MIME type is matched-type.
    #    Abort these steps.
    if let type_ = matched_type:
        this.m_computed_mime_type = type_
        return

    # 9. The computed MIME type is the supplied MIME type.
    mime_resource_compute_the_supplied_mime_type(this)

    return

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:493 Web::MimeSniff::Resource::rules_for_distinguishing_if_a_resource_is_text_or_binary.
# https://mimesniff.spec.whatwg.org/#sniffing-a-mislabeled-binary-resource
pub func mime_resource_rules_for_distinguishing_if_a_resource_is_text_or_binary(this: MimeResource*):
    # 1. Let length be the number of bytes in the resource header.
    let length = ak.byte_buffer_size(&this.m_resource_header)

    # 2. If length is greater than or equal to 2 and the first 2 bytes of the
    #    resource header are equal to 0xFE 0xFF (UTF-16BE BOM) or 0xFF 0xFE (UTF-16LE BOM), the computed MIME type is "text/plain".
    #    Abort these steps.
    let resource_header_span = ak.byte_buffer_span_const(&this.m_resource_header)
    let utf_16_be_bom = b"\xFE\xFF"
    let utf_16_le_bom = b"\xFF\xFE"
    if (length >= 2 and (mime_bytes_start_with(resource_header_span, utf_16_be_bom) or mime_bytes_start_with(resource_header_span, utf_16_le_bom))):
        this.m_computed_mime_type = mime_type_create(ak.string_from_str("text"), ak.string_from_str("plain"))
        return

    # 3. If length is greater than or equal to 3 and the first 3 bytes of the resource header are equal to 0xEF 0xBB 0xBF (UTF-8 BOM),
    #    the computed MIME type is "text/plain".
    #    Abort these steps.
    let utf_8_bom = b"\xEF\xBB\xBF"
    if length >= 3 and mime_bytes_start_with(resource_header_span, utf_8_bom):
        this.m_computed_mime_type = mime_type_create(ak.string_from_str("text"), ak.string_from_str("plain"))
        return

    # 4. If the resource header contains no binary data bytes, the computed MIME type is "text/plain".
    #    Abort these steps.
    if not mime_bytes_contain_binary_data_byte(mime_resource_resource_header(this)):
        this.m_computed_mime_type = mime_type_create(ak.string_from_str("text"), ak.string_from_str("plain"))
        return

    # 5. The computed MIME type is "application/octet-stream".
    # NOTE: This is the default MIME type of the computed MIME type.
    return

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:533 Web::MimeSniff::Resource::context_specific_sniffing_algorithm.
# https://mimesniff.spec.whatwg.org/#context-specific-sniffing-algorithm
pub func mime_resource_context_specific_sniffing_algorithm(this: MimeResource*, sniffing_context: MimeSniffingContext):
    # A context-specific sniffing algorithm determines the computed MIME type of a resource only if
    # the resource is a MIME type relevant to a particular context.
    if sniffing_context == MimeSniffingContext.none_ or sniffing_context == MimeSniffingContext.browsing:
        # https://mimesniff.spec.whatwg.org/#sniffing-in-a-browsing-context
        # Use the MIME type sniffing algorithm.
        mime_resource_mime_type_sniffing_algorithm(this)
        return

    # NOTE: Non-standard but if the client expects us to not sniff, we shouldn't be doing any
    #       context-specific sniffing if we don't have to.
    if this.m_no_sniff and mime_resource_has_supplied_mime_type(this):
        mime_resource_compute_the_supplied_mime_type(this)
        return

    if sniffing_context == MimeSniffingContext.image:
        mime_resource_rules_for_sniffing_images_specifically(this)
        return
    if sniffing_context == MimeSniffingContext.audio_or_video:
        mime_resource_rules_for_sniffing_audio_or_video_specifically(this)
        return
    if sniffing_context == MimeSniffingContext.font:
        mime_resource_rules_for_sniffing_fonts_specifically(this)
        return
    if sniffing_context == MimeSniffingContext.text_or_binary:
        mime_resource_rules_for_distinguishing_if_a_resource_is_text_or_binary(this)
        return

    return

## `m_supplied_mime_type.has_value()`.
func mime_resource_has_supplied_mime_type(this: const MimeResource*) -> bool:
    return match this.m_supplied_mime_type:
        .some(_) => true
        .none => false

## `m_supplied_mime_type.has_value() && m_supplied_mime_type->is_xml()`.
func mime_resource_supplied_mime_type_is_xml(this: const MimeResource*) -> bool:
    if let supplied = this.m_supplied_mime_type:
        return mime_type_is_xml(&supplied)
    return false

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:563 Web::MimeSniff::Resource::rules_for_sniffing_images_specifically.
# https://mimesniff.spec.whatwg.org/#sniffing-in-an-image-context
pub func mime_resource_rules_for_sniffing_images_specifically(this: MimeResource*):
    # 1. If the supplied MIME type is an XML MIME type, the computed MIME type is the supplied MIME type.
    #    Abort these steps.
    # NOTE: Non-standard but due to the mime type detection algorithm we need this sanity check.
    if mime_resource_supplied_mime_type_is_xml(this):
        mime_resource_compute_the_supplied_mime_type(this)
        return

    # 2. Let image-type-matched be the result of executing the image type pattern matching algorithm with
    #    the resource header as the byte sequence to be matched.
    let image_type_matched = match_an_image_type_pattern(mime_resource_resource_header(this))

    # 3. If image-type-matched is not undefined, the computed MIME type is image-type-matched.
    #    Abort these steps.
    if let type_ = image_type_matched:
        this.m_computed_mime_type = type_
        return

    # 4. The computed MIME type is the supplied MIME type.
    # NOTE: Non-standard but due to the mime type detection algorithm we need this sanity check.
    if mime_resource_has_supplied_mime_type(this):
        mime_resource_compute_the_supplied_mime_type(this)

    # NOTE: Non-standard but if the supplied mime type is undefined, we use computed mime type's default value.
    return

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:595 Web::MimeSniff::Resource::rules_for_sniffing_audio_or_video_specifically.
# https://mimesniff.spec.whatwg.org/#sniffing-in-an-audio-or-video-context
pub func mime_resource_rules_for_sniffing_audio_or_video_specifically(this: MimeResource*):
    # 1. If the supplied MIME type is an XML MIME type, the computed MIME type is the supplied MIME type.
    #    Abort these steps.
    # NOTE: Non-standard but due to the mime type detection algorithm we need this sanity check.
    if mime_resource_supplied_mime_type_is_xml(this):
        mime_resource_compute_the_supplied_mime_type(this)
        return

    # 2. Let audio-or-video-type-matched be the result of executing the audio or video type pattern matching
    #    algorithm with the resource header as the byte sequence to be matched.
    let audio_or_video_type_matched = match_an_audio_or_video_type_pattern(mime_resource_resource_header(this))

    # 3. If audio-or-video-type-matched is not undefined, the computed MIME type is audio-or-video-type-matched.
    #    Abort these steps.
    if let type_ = audio_or_video_type_matched:
        this.m_computed_mime_type = type_
        return

    # 4. The computed MIME type is the supplied MIME type.
    # NOTE: Non-standard but due to the mime type detection algorithm we need this sanity check.
    if mime_resource_has_supplied_mime_type(this):
        mime_resource_compute_the_supplied_mime_type(this)

    # NOTE: Non-standard but if the supplied mime type is undefined, we use computed mime type's default value.
    return

## Ported from Libraries/LibWeb/MimeSniff/Resource.cpp:627 Web::MimeSniff::Resource::rules_for_sniffing_fonts_specifically.
# https://mimesniff.spec.whatwg.org/#sniffing-in-a-font-context
pub func mime_resource_rules_for_sniffing_fonts_specifically(this: MimeResource*):
    # 1. If the supplied MIME type is an XML MIME type, the computed MIME type is the supplied MIME type.
    #    Abort these steps.
    # NOTE: Non-standard but due to the mime type detection algorithm we need this sanity check.
    if mime_resource_supplied_mime_type_is_xml(this):
        mime_resource_compute_the_supplied_mime_type(this)
        return

    # 2. Let font-type-matched be the result of executing the font type pattern matching algorithm with the
    #    resource header as the byte sequence to be matched.
    let font_type_matched = match_a_font_type_pattern(mime_resource_resource_header(this))

    # 3. If font-type-matched is not undefined, the computed MIME type is font-type-matched.
    #    Abort these steps.
    if let type_ = font_type_matched:
        this.m_computed_mime_type = type_
        return

    # 4. The computed MIME type is the supplied MIME type.
    # NOTE: Non-standard but due to the mime type detection algorithm we need this sanity check.
    if mime_resource_has_supplied_mime_type(this):
        mime_resource_compute_the_supplied_mime_type(this)

    # NOTE: Non-standard but if the supplied mime type is undefined, we use computed mime type's default value.
    return