From c715f73b330aab3aa7811e2d029c8d5c6feafdb6 Mon Sep 17 00:00:00 2001 From: Felix Hanau Date: Thu, 20 Jun 2024 10:46:13 -0400 Subject: [PATCH] Support case-insensitive encoding in TextDecoder API (Fixes #2300) --- src/workerd/api/encoding.c++ | 4 +++- src/workerd/api/tests/encoding-test.js | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/workerd/api/encoding.c++ b/src/workerd/api/encoding.c++ index a68c61330f0..d7b71cadfe0 100644 --- a/src/workerd/api/encoding.c++ +++ b/src/workerd/api/encoding.c++ @@ -3,6 +3,7 @@ // https://opensource.org/licenses/Apache-2.0 #include "encoding.h" +#include "util.h" #include #include #include @@ -256,6 +257,7 @@ kj::StringPtr getEncodingId(Encoding encoding) { } Encoding getEncodingForLabel(kj::StringPtr label) { + kj::String labelInsensitive = toLower(label); const auto trim = [](kj::StringPtr label) { const auto isAsciiWhitespace = [](auto c) { return c == 0x09 /* tab */ || @@ -271,7 +273,7 @@ Encoding getEncodingForLabel(kj::StringPtr label) { return label.slice(start, end).asChars(); }; - auto trimmed = trim(label); + auto trimmed = trim(labelInsensitive); #define V(label, key) if (trimmed == label##_kj) return Encoding::key; EW_ENCODING_LABELS(V) #undef V diff --git a/src/workerd/api/tests/encoding-test.js b/src/workerd/api/tests/encoding-test.js index 3bd21e39768..691b26ab528 100644 --- a/src/workerd/api/tests/encoding-test.js +++ b/src/workerd/api/tests/encoding-test.js @@ -577,6 +577,9 @@ export const allTheDecoders = { ["utf-16", "utf-16le"], ["utf-16le", "utf-16le"], ["x-user-defined", undefined], + // Test that match is case-insensitive + ["UTF-8", "utf-8"], + ["UtF-8", "utf-8"], ].forEach((pair) => { const [label, key] = pair; if (key === undefined) {