2021-05-25 20:13:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
2024-03-18 03:22:27 +00:00
|
|
|
* Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
|
2021-05-25 20:13:15 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/StringView.h>
|
2024-08-05 15:03:53 +00:00
|
|
|
#include <LibTextCodec/Encoder.h>
|
2024-03-18 03:22:27 +00:00
|
|
|
#include <LibURL/URL.h>
|
2021-05-25 20:13:15 +00:00
|
|
|
|
2024-03-18 03:22:27 +00:00
|
|
|
namespace URL {
|
2021-05-25 20:13:15 +00:00
|
|
|
|
2021-06-03 10:40:04 +00: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) \
|
|
|
|
STATE(CannotBeABaseUrlPath) \
|
|
|
|
STATE(Query) \
|
|
|
|
STATE(Fragment)
|
|
|
|
|
2024-03-18 03:22:27 +00:00
|
|
|
class Parser {
|
2021-05-25 20:13:15 +00:00
|
|
|
public:
|
|
|
|
enum class State {
|
2021-06-03 10:40:04 +00:00
|
|
|
#define STATE(state) state,
|
|
|
|
ENUMERATE_STATES
|
|
|
|
#undef STATE
|
2021-05-25 20:13:15 +00:00
|
|
|
};
|
|
|
|
|
2021-06-03 10:40:04 +00: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 02:29:20 +00:00
|
|
|
// https://url.spec.whatwg.org/#concept-basic-url-parser
|
2024-08-13 07:18:50 +00:00
|
|
|
static URL basic_parse(StringView input, Optional<URL> const& base_url = {}, URL* url = nullptr, Optional<State> state_override = {}, Optional<StringView> encoding = {});
|
2021-05-25 20:13:15 +00:00
|
|
|
|
2023-06-25 02:11:34 +00:00
|
|
|
// https://url.spec.whatwg.org/#string-percent-encode-after-encoding
|
2024-08-10 12:05:22 +00:00
|
|
|
static String percent_encode_after_encoding(TextCodec::Encoder&, StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus = false);
|
2023-06-25 02:11:34 +00:00
|
|
|
|
2023-07-26 09:05:35 +00:00
|
|
|
// https://url.spec.whatwg.org/#concept-host-serializer
|
2024-03-18 03:22:27 +00:00
|
|
|
static ErrorOr<String> serialize_host(Host const&);
|
2023-09-17 01:15:52 +00:00
|
|
|
|
|
|
|
// https://url.spec.whatwg.org/#shorten-a-urls-path
|
|
|
|
static void shorten_urls_path(URL&);
|
2021-05-25 20:13:15 +00:00
|
|
|
};
|
|
|
|
|
2021-06-03 10:40:04 +00:00
|
|
|
#undef ENUMERATE_STATES
|
|
|
|
|
2021-05-25 20:13:15 +00:00
|
|
|
}
|