From ff319faef3e4d1588d48469dc7455ade76e50afb Mon Sep 17 00:00:00 2001 From: Mirko Alicastro Date: Tue, 26 May 2026 18:37:42 +0200 Subject: [PATCH] Use Locale.ROOT when lowercasing rewrite URIs (#1618) Use Locale.ROOT to apply the lowecase format. For URI scheme and host locale-neutral casing is the semantically the correct choice. Added a unit test that sets the default locale to tr-TR and that would fail without the fix. --- .../pkl/core/http/RequestRewritingClient.java | 5 +++-- .../core/http/RequestRewritingClientTest.kt | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java b/pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java index 7b7d39e2..9e453327 100644 --- a/pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java +++ b/pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java @@ -26,6 +26,7 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; @@ -205,9 +206,9 @@ final class RequestRewritingClient implements HttpClient { private URI normalizeRewrite(URI uri) { try { return new URI( - uri.getScheme().toLowerCase(), + uri.getScheme().toLowerCase(Locale.ROOT), uri.getUserInfo(), - uri.getHost().toLowerCase(), + uri.getHost().toLowerCase(Locale.ROOT), uri.getPort(), uri.getPath(), uri.getQuery(), diff --git a/pkl-core/src/test/kotlin/org/pkl/core/http/RequestRewritingClientTest.kt b/pkl-core/src/test/kotlin/org/pkl/core/http/RequestRewritingClientTest.kt index cf94a6d7..42f4ce63 100644 --- a/pkl-core/src/test/kotlin/org/pkl/core/http/RequestRewritingClientTest.kt +++ b/pkl-core/src/test/kotlin/org/pkl/core/http/RequestRewritingClientTest.kt @@ -21,6 +21,7 @@ import java.net.http.HttpRequest import java.net.http.HttpRequest.BodyPublishers import java.net.http.HttpResponse.BodyHandlers import java.time.Duration +import java.util.Locale import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatList import org.junit.jupiter.api.Test @@ -321,6 +322,24 @@ class RequestRewritingClientTest { .isEqualTo("https://bar.com/bar/baz") } + @Test + fun `rewrites URIs - host with capital I under tr_TR locale`() { + // Under tr_TR, "INDEX.COM".toLowerCase() becomes "ındex.com". + val previous = Locale.getDefault() + Locale.setDefault(Locale.forLanguageTag("tr-TR")) + try { + assertThat( + rewrittenRequest( + "https://INDEX.COM/foo/bar", + mapOf(URI("https://index.com/") to URI("https://bar.com/")), + ) + ) + .isEqualTo("https://bar.com/foo/bar") + } finally { + Locale.setDefault(previous) + } + } + private fun rewrittenRequest(uri: String, rules: Map): String { val captured = RequestCapturingClient() val client = RequestRewritingClient("Pkl", Duration.ofSeconds(42), -1, captured, rules, mapOf())