2021-05-25 17:13:15 -03:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
2026-01-23 13:56:01 -03:00
|
|
|
|
* Copyright (c) 2023-2026, Shannon Booth <shannon@serenityos.org>
|
2021-05-25 17:13:15 -03:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
|
|
#include <AK/StringUtils.h>
|
|
|
|
|
|
#include <AK/Utf8View.h>
|
2024-08-05 12:03:53 -03:00
|
|
|
|
#include <LibTextCodec/Encoder.h>
|
2024-03-18 00:22:27 -03:00
|
|
|
|
#include <LibURL/Parser.h>
|
2026-03-14 14:05:06 -03:00
|
|
|
|
#include <LibURL/RustIntegration.h>
|
2021-05-25 17:13:15 -03:00
|
|
|
|
|
2024-03-18 00:22:27 -03:00
|
|
|
|
namespace URL {
|
2021-05-25 17:13:15 -03:00
|
|
|
|
|
|
|
|
|
|
// https://url.spec.whatwg.org/#concept-host-parser
|
2025-06-25 23:21:17 -03:00
|
|
|
|
Optional<Host> Parser::parse_host(StringView input, bool is_opaque)
|
2021-05-25 17:13:15 -03:00
|
|
|
|
{
|
2026-04-05 11:55:02 -03:00
|
|
|
|
return RustIntegration::parse_host(input, is_opaque);
|
2023-09-16 22:15:52 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-09 19:48:15 -03:00
|
|
|
|
// https://url.spec.whatwg.org/#string-percent-encode-after-encoding
|
2024-08-10 09:05:22 -03:00
|
|
|
|
String Parser::percent_encode_after_encoding(TextCodec::Encoder& encoder, StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus)
|
2022-04-09 19:48:15 -03:00
|
|
|
|
{
|
2024-08-05 12:03:53 -03:00
|
|
|
|
// 1. Let encodeOutput be an empty I/O queue.
|
2022-04-09 19:48:15 -03:00
|
|
|
|
StringBuilder output;
|
|
|
|
|
|
|
2024-08-06 07:06:05 -03:00
|
|
|
|
// 2. Set potentialError to the result of running encode or fail with inputQueue, encoder, and encodeOutput.
|
2024-08-10 09:05:22 -03:00
|
|
|
|
MUST(encoder.process(
|
2024-08-06 07:06:05 -03:00
|
|
|
|
Utf8View(input),
|
|
|
|
|
|
|
|
|
|
|
|
// 3. For each byte of encodeOutput converted to a byte sequence:
|
|
|
|
|
|
[&](u8 byte) -> ErrorOr<void> {
|
|
|
|
|
|
// 1. If spaceAsPlus is true and byte is 0x20 (SP), then append U+002B (+) to output and continue.
|
|
|
|
|
|
if (space_as_plus && byte == ' ') {
|
|
|
|
|
|
output.append('+');
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
2022-04-09 19:48:15 -03:00
|
|
|
|
|
2024-08-06 07:06:05 -03:00
|
|
|
|
// 2. Let isomorph be a code point whose value is byte’s value.
|
|
|
|
|
|
u32 isomorph = byte;
|
2022-04-09 19:48:15 -03:00
|
|
|
|
|
2024-08-06 07:06:05 -03:00
|
|
|
|
// 3. Assert: percentEncodeSet includes all non-ASCII code points.
|
2022-04-09 19:48:15 -03:00
|
|
|
|
|
2024-08-06 07:06:05 -03:00
|
|
|
|
// 4. If isomorphic is not in percentEncodeSet, then append isomorph to output.
|
|
|
|
|
|
if (!code_point_is_in_percent_encode_set(isomorph, percent_encode_set)) {
|
|
|
|
|
|
output.append_code_point(isomorph);
|
|
|
|
|
|
}
|
2022-04-09 19:48:15 -03:00
|
|
|
|
|
2024-08-06 07:06:05 -03:00
|
|
|
|
// 5. Otherwise, percent-encode byte and append the result to output.
|
|
|
|
|
|
else {
|
|
|
|
|
|
output.appendff("%{:02X}", byte);
|
|
|
|
|
|
}
|
2024-08-05 12:03:53 -03:00
|
|
|
|
|
2024-08-06 07:06:05 -03:00
|
|
|
|
return {};
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 4. If potentialError is non-null, then append "%26%23", followed by the shortest sequence of ASCII digits
|
|
|
|
|
|
// representing potentialError in base ten, followed by "%3B", to output.
|
|
|
|
|
|
[&](u32 error) -> ErrorOr<void> {
|
|
|
|
|
|
output.appendff("%26%23{}%3B", error);
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}));
|
2022-04-09 19:48:15 -03:00
|
|
|
|
|
|
|
|
|
|
// 6. Return output.
|
2024-08-10 09:05:22 -03:00
|
|
|
|
return MUST(output.to_string());
|
2022-04-09 19:48:15 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-09 12:50:34 -03:00
|
|
|
|
Optional<URL> Parser::basic_parse(StringView raw_input, Optional<URL const&> base_url, URL* url, Optional<State> state_override, Optional<StringView> encoding)
|
2021-05-25 17:13:15 -03:00
|
|
|
|
{
|
2026-03-14 14:05:06 -03:00
|
|
|
|
return RustIntegration::parse_basic_url(raw_input, base_url, url, state_override, encoding);
|
2021-05-25 17:13:15 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|