Skip to content

Truly independent web browser

20.6 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Debug.h>
#include <AK/StringView.h>
#include <AK/Time.h>
#include <LibGC/Function.h>
#include <LibGC/Heap.h>
#include <LibJS/Runtime/Value.h>
#include <LibTextCodec/Decoder.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/ResponseView.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/HTMLLinkElement.h>
#include <LibWeb/HTML/Parser/HTMLEncodingDetection.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/HTML/Parser/IncrementalDocumentParser.h>

namespace Web::HTML {

GC_DEFINE_ALLOCATOR(IncrementalDocumentParser);

static Utf16String utf16_string_from_standardized_encoding_label(StringView label)
{
    return Utf16String::from_ascii_without_validation(label.bytes());
}

GC::Ref<IncrementalDocumentParser> IncrementalDocumentParser::create(GC::Ref<DOM::Document> document, Fetch::Infrastructure::ResponseView const& response, GC::Ptr<DOM::Document> container_document)
{
    return GC::Heap::the().allocate<IncrementalDocumentParser>(document, response, container_document);
}

IncrementalDocumentParser::IncrementalDocumentParser(GC::Ref<DOM::Document> document, Fetch::Infrastructure::ResponseView const& response, GC::Ptr<DOM::Document> container_document)
    : m_document(document)
    , m_container_document(container_document)
    , m_body(response.body())
    , m_url(response.url().value())
    , m_mime_type(Fetch::Infrastructure::extract_mime_type(response.header_list()))
    , m_response_header_list(response.header_list())
{
}

void IncrementalDocumentParser::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_document);
    visitor.visit(m_container_document);
    visitor.visit(m_body);
    visitor.visit(m_parser);
}

void IncrementalDocumentParser::start()
{
    // https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-html
    // The user agent may wait for more bytes of the resource to be available while determining the
    // encoding. Body::wait_for_sniff_bytes waits until its sniff-byte threshold is available, or
    // until the stream closes.
    //
    // FIXME: The spec allows starting the parse after 500 ms or 1024 bytes, whichever comes first.
    // We only honor the byte threshold.
    auto parser = GC::Ref { *this };
    m_body->wait_for_sniff_bytes(GC::create_function(GC::Heap::the(), GC::captures(parser), [](auto& parser, Web::Fetch::Infrastructure::Body::SniffBytes sniff_bytes) {
        // A failed response initializes the parser too.
        parser->initialize_parser(sniff_bytes.bytes);
    }));
}

void IncrementalDocumentParser::set_allow_declarative_shadow_roots(HTMLParser::AllowDeclarativeShadowRoots allow_declarative_shadow_roots)
{
    m_allow_declarative_shadow_roots = allow_declarative_shadow_roots;
    if (m_parser)
        m_parser->set_allow_declarative_shadow_roots(allow_declarative_shadow_roots);
}

void IncrementalDocumentParser::initialize_parser(ReadonlyBytes sniff_bytes)
{
    if (m_parser)
        return;

    m_document->set_url(m_url);

    // The parser exists while the prescan runs, so that aborting the document aborts it. Its encoding confidence is
    // set along with its encoding.
    m_parser = HTMLParser::create_with_open_input_stream(m_document, EncodingConfidence::Tentative);
    m_parser->set_change_encoding_callback(GC::create_function(GC::Heap::the(), GC::captures(*this), [](IncrementalDocumentParser& self, StringView encoding) {
        auto result = self.change_encoding(encoding);
        if (result.is_error()) {
            self.release_encoding_change_buffers();
            return false;
        }
        return result.release_value();
    }));
    m_parser->set_parsing_complete_callback(GC::create_function(GC::Heap::the(), GC::captures(*this), [](IncrementalDocumentParser& self) {
        self.release_encoding_change_buffers();
    }));
    m_parser->set_ready_for_more_input_callback(GC::create_function(GC::Heap::the(), GC::captures(*this), [](IncrementalDocumentParser& self) {
        self.feed_parser();
    }));
    m_parser->set_allow_declarative_shadow_roots(m_allow_declarative_shadow_roots);

    // https://html.spec.whatwg.org/multipage/parsing.html#parsing-with-a-known-character-encoding
    // https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding
    if (m_document->has_encoding()) {
        set_encoding(m_document->encoding()->to_byte_string(), EncodingConfidence::Certain);
    } else if (auto result = sniff_encoding_before_prescan(sniff_bytes, m_mime_type); result.has_value()) {
        set_encoding(result->encoding, result->confidence);
    } else {
        // The prescan can need bytes past the ones for sniffing, as far as the end of the head, so it reads them as
        // they arrive, and the parser gets them once it's done.
        m_sniff_bytes = MUST(ByteBuffer::copy(sniff_bytes));
        m_prescan = make<EncodingDeclarationPrescan>(m_document->is_scripting_enabled());
    }

    start_incremental_read();
}

void IncrementalDocumentParser::continue_prescan(bool input_complete)
{
    auto new_bytes = m_input_bytes.span().slice(m_prescanned_byte_count);
    m_prescanned_byte_count = m_input_bytes.size();

    auto result = m_prescan->scan(new_bytes, input_complete);
    if (result == EncodingDeclarationPrescan::Result::NeedMoreInput)
        return;

    auto [encoding, confidence] = result == EncodingDeclarationPrescan::Result::Found
        ? EncodingSniffingResult { ByteString { m_prescan->encoding() }, EncodingConfidence::Tentative }
        : sniff_encoding_after_prescan(m_document, m_sniff_bytes, m_container_document);
    m_prescan = nullptr;
    m_sniff_bytes = {};
    set_encoding(encoding, confidence);
}

void IncrementalDocumentParser::set_encoding(StringView encoding, EncodingConfidence encoding_confidence)
{
    auto standardized_encoding = TextCodec::get_standardized_encoding(encoding);
    VERIFY(standardized_encoding.has_value());
    dbgln_if(HTML_PARSER_DEBUG, "The incremental HTML parser selected encoding '{}'", standardized_encoding.value());

    m_decoder = make<TextCodec::StreamingDecoder>(standardized_encoding.value(), TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement);

    // https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding
    // The document's character encoding must immediately be set to the value returned from this
    // algorithm, at the same time as the user agent uses the returned value to select the decoder
    // to use for the input byte stream.
    m_document->set_encoding(utf16_string_from_standardized_encoding_label(standardized_encoding.value()));
    m_parser->set_encoding_confidence({}, encoding_confidence);
}

void IncrementalDocumentParser::start_incremental_read()
{
    auto parser = GC::Ref { *this };
    m_body->incrementally_read(
        m_document->relevant_settings_object().realm(),
        GC::create_function(GC::Heap::the(), GC::captures(parser), [](auto& parser, ByteBuffer bytes) {
            parser->process_body_chunk(move(bytes));
        }),
        GC::create_function(GC::Heap::the(), GC::captures(parser), [](auto& parser) {
            parser->process_end_of_body();
        }),
        GC::create_function(GC::Heap::the(), GC::captures(parser), [](auto& parser, JS::Value error) {
            parser->process_body_error(error);
        }),
        GC::Ref { m_document->relevant_settings_object().realm().global_object() });
}

bool IncrementalDocumentParser::should_continue() const
{
    // NOTE: document.open() replaces m_document->parser() without aborting the old parser, so we have to stop feeding
    //       bytes once we're no longer the document's active parser.
    return m_parser && !m_parser->aborted() && m_document->parser() == m_parser;
}

ReadonlyBytes IncrementalDocumentParser::decoded_input_bytes() const
{
    return m_input_bytes.span().slice(0, m_decoded_byte_count);
}

ReadonlyBytes IncrementalDocumentParser::undecoded_input_bytes() const
{
    return m_input_bytes.span().slice(m_decoded_byte_count);
}

// pump() runs the tokenizer to the end of its input, so the only reasons decoded characters can be left unconsumed
// are the parser waiting for scripts to be allowed to run and the parser being paused on a parser-blocking script.
bool IncrementalDocumentParser::tokenizer_will_consume_input_immediately() const
{
    return m_document->ready_to_run_scripts() && !m_parser->is_paused();
}

void IncrementalDocumentParser::append_decoded(Utf16View decoded)
{
    m_source.append(decoded);
    m_parser->tokenizer().append_to_input_stream(decoded);
}

void IncrementalDocumentParser::process_body_chunk(ByteBuffer bytes)
{
    if (!should_continue()) {
        discard_input_bytes();
        return;
    }

    // https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-html
    // Each task that the networking task source places on the task queue while fetching runs must
    // fill the parser's input byte stream with the fetched bytes and cause the HTML parser to
    // perform the appropriate processing of the input stream.
    if (m_input_bytes.is_empty())
        m_input_bytes = move(bytes);
    else
        m_input_bytes.append(bytes);

    if (m_prescan)
        continue_prescan(false);
    if (!m_has_queued_task_to_feed_parser)
        feed_parser();
    process_link_headers_after_first_task();
}

void IncrementalDocumentParser::process_link_headers_after_first_task()
{
    if (!m_response_header_list)
        return;

    // https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-html
    // The first task that the networking task source places on the task queue while fetching runs must process link
    // headers given document, navigationParams's response, and "media", after the task has been processed by the HTML
    // parser.
    auto response_header_list = m_response_header_list.release_nonnull();
    HTMLLinkElement::process_link_headers(m_document, response_header_list, m_url, HTMLLinkElement::LinkHeaderPhase::Media);
}

static size_t encoding_independent_prefix_length(ReadonlyBytes bytes)
{
    size_t length = 0;
    while (length < bytes.size()) {
        // Non-ASCII bytes may have different Unicode interpretations in different encodings. ESC is also
        // encoding-sensitive because it can change the state of an ISO-2022-JP decoder, affecting how
        // subsequent ASCII-range bytes are interpreted.
        auto byte = bytes[length];
        if (!is_ascii(byte) || byte == 0x1B)
            break;

        ++length;
    }
    return length;
}

void IncrementalDocumentParser::queue_task_to_feed_parser()
{
    // NB: A parser that was blocked may ask for more input while this task is queued, and that task goes on from there.
    if (m_has_queued_task_to_feed_parser)
        return;
    ++m_times_yielded_to_event_loop;
    m_has_queued_task_to_feed_parser = true;
    queue_global_task(Task::Source::Networking, m_document->relevant_settings_object().realm().global_object(), GC::create_function(GC::Heap::the(), GC::captures(GC::Ref { *this }), [](auto& parser) {
        parser->m_has_queued_task_to_feed_parser = false;
        parser->feed_parser();
    }));
}

void IncrementalDocumentParser::feed_parser()
{
    // The parser gets no bytes until the prescan has found their encoding.
    if (m_prescan)
        return;

    // NB: A document that arrives faster than we can parse it would otherwise keep the event loop for as long as that
    //     takes. The first slices are short, so that the first rendering update has little to lay out. After those,
    //     longer slices spare us laying out a document over and over while it is still growing.
    static constexpr size_t maximum_bytes_to_process_at_once = 16 * KiB;
    static constexpr auto first_slices_time_budget = AK::Duration::from_milliseconds(10);
    static constexpr auto later_slices_time_budget = AK::Duration::from_milliseconds(500);
    static constexpr u32 number_of_first_slices = 6;
    auto const deadline = MonotonicTime::now() + (m_times_yielded_to_event_loop < number_of_first_slices ? first_slices_time_budget : later_slices_time_budget);

    while (!undecoded_input_bytes().is_empty()) {
        if (!should_continue()) {
            discard_input_bytes();
            return;
        }

        auto undecoded_bytes = undecoded_input_bytes();
        auto length_to_decode = undecoded_bytes.size();

        if (m_parser->encoding_confidence() == EncodingConfidence::Tentative) {
            auto current_encoding = TextCodec::get_standardized_encoding(m_document->encoding().value());
            VERIFY(current_encoding.has_value());

            // Every byte of a UTF-16 stream is encoding-sensitive, and step 1 of the change-the-encoding algorithm
            // refuses to move away from UTF-16BE/LE anyway, so there is no declaration left to hold bytes for.
            if (!current_encoding->is_one_of_ignoring_ascii_case("UTF-16BE"sv, "UTF-16LE"sv)) {
                // Only the bytes this round can decode need checking, so a large body isn't checked again from the
                // start of what's left each round.
                auto prefix_length = encoding_independent_prefix_length(undecoded_bytes.trim(maximum_bytes_to_process_at_once));

                // Step 5 of the change-the-encoding algorithm only swaps the decoder for bytes that read the same
                // in both encodings, so converting an encoding-sensitive byte commits any declaration behind it to
                // the step 6 restart we do not implement. Holding them until the tokenizer catches up rules one out.
                if (prefix_length == 0 && !tokenizer_will_consume_input_immediately()) {
                    // Nothing is decoded this round, so pump() cannot arm the wake-up that brings us back. A parser
                    // paused on a script has the resume hook; one that cannot run scripts yet needs this.
                    if (!m_document->ready_to_run_scripts())
                        register_deferred_start();
                    return;
                }

                if (prefix_length > 0)
                    length_to_decode = prefix_length;
            }
        }

        decode_and_process(min(length_to_decode, maximum_bytes_to_process_at_once));

        if (!undecoded_input_bytes().is_empty() && MonotonicTime::now() >= deadline) {
            queue_task_to_feed_parser();
            return;
        }
    }

    if (!should_continue() || m_parser->encoding_confidence() != EncodingConfidence::Tentative)
        release_encoding_change_buffers();

    if (m_body_is_exhausted)
        finish_body();
}

void IncrementalDocumentParser::process_end_of_body()
{
    // The body's stream lives in the realm of the document that started this navigation, and this parser stays attached
    // to the new document until its load event. So, drop the body as soon as it's exhausted — rather than keeping the
    // previous document alive until the new one has finished loading. Blink, WebKit, and Gecko all do this too.
    m_body = nullptr;
    m_body_is_exhausted = true;
    if (m_parser)
        m_parser->did_exhaust_streaming_body({});

    if (!should_continue()) {
        discard_input_bytes();
        return;
    }

    if (m_prescan)
        continue_prescan(true);

    // Bytes held back for a still-possible encoding declaration are fed first; the implied EOF character waits for
    // them in finish_body().
    if (!m_has_queued_task_to_feed_parser)
        feed_parser();
    process_link_headers_after_first_task();
}

void IncrementalDocumentParser::finish_body()
{
    if (m_processed_implied_eof)
        return;
    if (!should_continue())
        return;

    VERIFY(m_body_is_exhausted);
    VERIFY(undecoded_input_bytes().is_empty());

    auto decoded = m_decoder->decode({}, TextCodec::Flush::Yes).release_value_but_fixme_should_propagate_errors();
    append_decoded(decoded);

    // https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-html
    // When no more bytes are available, have the parser process the implied EOF character.
    m_document->set_source(m_source.to_string());
    m_processed_implied_eof = true;
    m_parser->tokenizer().close_input_stream();
    pump();

    if (!should_continue())
        release_encoding_change_buffers();
}

ErrorOr<bool> IncrementalDocumentParser::change_encoding(StringView new_encoding)
{
    // If all bytes converted so far have the same interpretation in the new encoding, retain the replacement
    // streaming decoder after feeding it those bytes. Feeding the prefix both performs the required comparison and
    // restores any decoder state needed to continue at the current byte position.
    auto decoder = make<TextCodec::StreamingDecoder>(new_encoding, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement);

    Utf16StringBuilder builder;
    builder.append(TRY(decoder->decode(decoded_input_bytes())));
    if (m_processed_implied_eof)
        builder.append(TRY(decoder->decode({}, TextCodec::Flush::Yes)));
    auto redecoded_source = builder.to_string();

    auto document_source = m_processed_implied_eof ? m_document->source() : Utf16String {};
    auto current_source = m_processed_implied_eof ? document_source.utf16_view() : m_source.view();
    if (!redecoded_source.starts_with(current_source)
        || (m_processed_implied_eof && redecoded_source.length_in_code_units() != current_source.length_in_code_units())) {
        // FIXME: A mismatch requires restarting the navigation with the new encoding, as described by step 6 of the
        //        change-the-encoding algorithm.
        release_encoding_change_buffers();
        return false;
    }

    auto additionally_decoded_source = redecoded_source.utf16_view().substring_view(current_source.length_in_code_units());
    if (!additionally_decoded_source.is_empty()) {
        VERIFY(!m_processed_implied_eof);
        append_decoded(additionally_decoded_source);
    }

    m_decoder = move(decoder);
    release_encoding_change_buffers();
    return true;
}

void IncrementalDocumentParser::decode_and_process(size_t length)
{
    auto undecoded_bytes = undecoded_input_bytes();
    VERIFY(length <= undecoded_bytes.size());

    auto decoded = m_decoder->decode(undecoded_bytes.trim(length)).release_value_but_fixme_should_propagate_errors();

    // Count the bytes as converted before running the parser: a script it runs can deliver another body chunk, and
    // feed_parser() would otherwise convert these a second time.
    m_decoded_byte_count += length;

    append_decoded(decoded);
    pump();
}

void IncrementalDocumentParser::release_encoding_change_buffers()
{
    // Only bytes the decoder has yet to convert are still needed; the rest were kept solely to compare decoders.
    auto undecoded_bytes = undecoded_input_bytes();
    auto undecoded_size = undecoded_bytes.size();
    undecoded_bytes.copy_to(m_input_bytes.span());
    m_input_bytes.resize(undecoded_size);
    m_decoded_byte_count = 0;
}

void IncrementalDocumentParser::discard_input_bytes()
{
    m_input_bytes.clear();
    m_decoded_byte_count = 0;
}

void IncrementalDocumentParser::process_body_error(JS::Value)
{
    m_body = nullptr;

    // The bytes received are all the prescan gets.
    if (m_prescan && should_continue())
        continue_prescan(true);

    // Bytes withheld while the tokenizer was behind are still part of the document. This ends the parse, so no
    // declaration can still change how they decode: feed them to the tokenizer with the encoding as it stands.
    if (should_continue() && !undecoded_input_bytes().is_empty())
        decode_and_process(undecoded_input_bytes().size());

    discard_input_bytes();
    dbgln("FIXME: Load html page with an error if incremental read of body failed.");
    HTMLParser::the_end(m_document, m_parser);
}

void IncrementalDocumentParser::register_deferred_start()
{
    if (m_document->has_deferred_parser_start())
        return;

    auto parser = GC::Ref { *this };
    m_document->set_deferred_parser_start(GC::create_function(GC::Heap::the(), GC::captures(parser), [](auto& parser) {
        parser->pump();

        // The tokenizer can consume input again, so hand over any bytes withheld while it could not.
        parser->feed_parser();
    }));
}

void IncrementalDocumentParser::pump()
{
    if (!should_continue())
        return;

    if (!m_document->ready_to_run_scripts()) {
        register_deferred_start();
        return;
    }

    if (m_parser->stopped())
        return;

    if (m_parser->tokenizer().is_input_stream_closed()) {
        m_parser->run_until_completion();
        return;
    }

    if (m_parser->is_paused())
        return;

    m_parser->run();
}

}