2021-05-25 22:13:15 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
2024-03-18 16:22:27 +13:00
|
|
|
* Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
|
2021-05-25 22:13:15 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/StringView.h>
|
2024-08-05 16:03:53 +01:00
|
|
|
#include <LibTextCodec/Encoder.h>
|
2024-03-18 16:22:27 +13:00
|
|
|
#include <LibURL/URL.h>
|
2021-05-25 22:13:15 +02:00
|
|
|
|
2024-03-18 16:22:27 +13:00
|
|
|
namespace URL {
|
2021-05-25 22:13:15 +02:00
|
|
|
|
2021-06-03 12:40:04 +02:00
|
|
|
#define ENUMERATE_STATES \
|
|
|
|
STATE(SchemeStart) \
|
|
|
|
STATE(Scheme) \
|
|
|
|
STATE(NoScheme) \
|
|
|
|
STATE(SpecialRelativeOrAuthority) \
|
|
|
|
STATE(PathOrAuthority) \
|
|
|
|
STATE(Relative) \
|
|
|
|
STATE(RelativeSlash) \
|
|
|
|
STATE(SpecialAuthoritySlashes) \
|
|
|
|
STATE(SpecialAuthorityIgnoreSlashes) \
|
|
|
|
STATE(Authority) \
|
|
|
|
STATE(Host) \
|
|
|
|
STATE(Hostname) \
|
|
|
|
STATE(Port) \
|
|
|
|
STATE(File) \
|
|
|
|
STATE(FileSlash) \
|
|
|
|
STATE(FileHost) \
|
|
|
|
STATE(PathStart) \
|
|
|
|
STATE(Path) \
|
2025-03-05 16:32:23 +13:00
|
|
|
STATE(OpaquePath) \
|
2021-06-03 12:40:04 +02:00
|
|
|
STATE(Query) \
|
|
|
|
STATE(Fragment)
|
|
|
|
|
2024-03-18 16:22:27 +13:00
|
|
|
class Parser {
|
2021-05-25 22:13:15 +02:00
|
|
|
public:
|
|
|
|
enum class State {
|
2021-06-03 12:40:04 +02:00
|
|
|
#define STATE(state) state,
|
|
|
|
ENUMERATE_STATES
|
|
|
|
#undef STATE
|
2021-05-25 22:13:15 +02:00
|
|
|
};
|
|
|
|
|
2021-06-03 12:40:04 +02:00
|
|
|
static char const* state_name(State const& state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
#define STATE(state) \
|
|
|
|
case State::state: \
|
|
|
|
return #state;
|
|
|
|
ENUMERATE_STATES
|
|
|
|
#undef STATE
|
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2023-07-15 14:29:20 +12:00
|
|
|
// https://url.spec.whatwg.org/#concept-basic-url-parser
|
2025-01-10 04:50:34 +13:00
|
|
|
static Optional<URL> basic_parse(StringView input, Optional<URL const&> base_url = {}, URL* url = nullptr, Optional<State> state_override = {}, Optional<StringView> encoding = {});
|
2021-05-25 22:13:15 +02:00
|
|
|
|
2023-06-25 14:11:34 +12:00
|
|
|
// https://url.spec.whatwg.org/#string-percent-encode-after-encoding
|
2024-08-11 00:05:22 +12:00
|
|
|
static String percent_encode_after_encoding(TextCodec::Encoder&, StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus = false);
|
2023-06-25 14:11:34 +12:00
|
|
|
|
2023-09-17 13:15:52 +12:00
|
|
|
// https://url.spec.whatwg.org/#shorten-a-urls-path
|
|
|
|
static void shorten_urls_path(URL&);
|
2025-06-26 14:21:17 +12:00
|
|
|
|
|
|
|
static Optional<Host> parse_host(StringView input, bool is_opaque = false);
|
2021-05-25 22:13:15 +02:00
|
|
|
};
|
|
|
|
|
2021-06-03 12:40:04 +02:00
|
|
|
#undef ENUMERATE_STATES
|
|
|
|
|
2021-05-25 22:13:15 +02:00
|
|
|
}
|