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