LibTextCodec+LibURL: Implement utf-8 and euc-jp encoders

Implements the corresponding encoders, selects the appropriate one when
encoding URL search params. If an encoder for the given encoding could
not be found, fallback to utf-8.
This commit is contained in:
BenJilks 2024-08-05 16:03:53 +01:00 committed by Tim Ledbetter
commit 72d0e3284b
Notes: github-actions[bot] 2024-08-08 16:51:38 +00:00
11 changed files with 260 additions and 22 deletions

View file

@ -9,6 +9,7 @@
#include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <LibTextCodec/Decoder.h>
#include <LibTextCodec/Encoder.h>
#include <LibURL/Parser.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
@ -47,6 +48,12 @@ ErrorOr<String> url_encode(Vector<QueryParam> const& tuples, StringView encoding
// 1. Set encoding to the result of getting an output encoding from encoding.
encoding = TextCodec::get_output_encoding(encoding);
auto encoder = TextCodec::encoder_for(encoding);
if (!encoder.has_value()) {
// NOTE: Fallback to default utf-8 encoder.
encoder = TextCodec::encoder_for("utf-8"sv);
}
// 2. Let output be the empty string.
StringBuilder output;
@ -55,12 +62,10 @@ ErrorOr<String> url_encode(Vector<QueryParam> const& tuples, StringView encoding
// 1. Assert: tuples name and tuples value are scalar value strings.
// 2. Let name be the result of running percent-encode after encoding with encoding, tuples name, the application/x-www-form-urlencoded percent-encode set, and true.
// FIXME: URL::Parser does not currently implement encoding.
auto name = TRY(URL::Parser::percent_encode_after_encoding(tuple.name, URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded, true));
auto name = TRY(URL::Parser::percent_encode_after_encoding(*encoder, tuple.name, URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded, true));
// 3. Let value be the result of running percent-encode after encoding with encoding, tuples value, the application/x-www-form-urlencoded percent-encode set, and true.
// FIXME: URL::Parser does not currently implement encoding.
auto value = TRY(URL::Parser::percent_encode_after_encoding(tuple.value, URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded, true));
auto value = TRY(URL::Parser::percent_encode_after_encoding(*encoder, tuple.value, URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded, true));
// 4. If output is not the empty string, then append U+0026 (&) to output.
if (!output.is_empty())