Coverage Report

Created: 2025-09-05 06:52

/src/serenity/Userland/Libraries/LibURL/Parser.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
3
 * Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#pragma once
9
10
#include <AK/Optional.h>
11
#include <AK/StringView.h>
12
#include <LibTextCodec/Encoder.h>
13
#include <LibURL/URL.h>
14
15
namespace URL {
16
17
#define ENUMERATE_STATES                 \
18
    STATE(SchemeStart)                   \
19
    STATE(Scheme)                        \
20
    STATE(NoScheme)                      \
21
    STATE(SpecialRelativeOrAuthority)    \
22
    STATE(PathOrAuthority)               \
23
    STATE(Relative)                      \
24
    STATE(RelativeSlash)                 \
25
    STATE(SpecialAuthoritySlashes)       \
26
    STATE(SpecialAuthorityIgnoreSlashes) \
27
    STATE(Authority)                     \
28
    STATE(Host)                          \
29
    STATE(Hostname)                      \
30
    STATE(Port)                          \
31
    STATE(File)                          \
32
    STATE(FileSlash)                     \
33
    STATE(FileHost)                      \
34
    STATE(PathStart)                     \
35
    STATE(Path)                          \
36
    STATE(CannotBeABaseUrlPath)          \
37
    STATE(Query)                         \
38
    STATE(Fragment)
39
40
class Parser {
41
public:
42
    enum class State {
43
#define STATE(state) state,
44
        ENUMERATE_STATES
45
#undef STATE
46
    };
47
48
    static char const* state_name(State const& state)
49
0
    {
50
0
        switch (state) {
51
0
#define STATE(state)   \
52
0
    case State::state: \
53
0
        return #state;
54
0
            ENUMERATE_STATES
55
0
#undef STATE
56
0
        }
57
0
        VERIFY_NOT_REACHED();
58
0
    }
59
60
    // https://url.spec.whatwg.org/#concept-basic-url-parser
61
    static URL basic_parse(StringView input, Optional<URL> const& base_url = {}, URL* url = nullptr, Optional<State> state_override = {}, Optional<StringView> encoding = {});
62
63
    // https://url.spec.whatwg.org/#string-percent-encode-after-encoding
64
    static String percent_encode_after_encoding(TextCodec::Encoder&, StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus = false);
65
66
    // https://url.spec.whatwg.org/#concept-host-serializer
67
    static ErrorOr<String> serialize_host(Host const&);
68
69
    // https://url.spec.whatwg.org/#shorten-a-urls-path
70
    static void shorten_urls_path(URL&);
71
};
72
73
#undef ENUMERATE_STATES
74
75
}