From 3d468314bed539e118995c2721566fbdb1043160 Mon Sep 17 00:00:00 2001 From: fedorovar Date: Tue, 23 Apr 2024 16:56:39 +0200 Subject: [PATCH] fixes issue #468 from original repo --- .../scala/io/lemonlabs/uri/config/UriConfig.scala | 15 ++++++++++----- .../lemonlabs/uri/decoding/PercentDecoder.scala | 9 ++++----- .../io/lemonlabs/uri/GithubIssuesTests.scala | 9 +++++++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/shared/src/main/scala/io/lemonlabs/uri/config/UriConfig.scala b/shared/src/main/scala/io/lemonlabs/uri/config/UriConfig.scala index b83eae6b..434fc2d9 100644 --- a/shared/src/main/scala/io/lemonlabs/uri/config/UriConfig.scala +++ b/shared/src/main/scala/io/lemonlabs/uri/config/UriConfig.scala @@ -97,18 +97,23 @@ object UriConfig { decoder: UriDecoder = PercentDecoder, charset: String = "UTF-8", renderQuery: RenderQuery = RenderQuery.default - ): UriConfig = + ): UriConfig = { + val updatedDecoder = decoder match { + case d @ PercentDecoder(_, _) => d.copy(charset = charset) + case other => other + } UriConfig( encoder, encoder, encoder, encoder, - decoder, - decoder, - decoder, - decoder, + updatedDecoder, + updatedDecoder, + updatedDecoder, + updatedDecoder, charset, renderQuery, defaultPorts ) + } } diff --git a/shared/src/main/scala/io/lemonlabs/uri/decoding/PercentDecoder.scala b/shared/src/main/scala/io/lemonlabs/uri/decoding/PercentDecoder.scala index a6cefbd8..18fb2871 100644 --- a/shared/src/main/scala/io/lemonlabs/uri/decoding/PercentDecoder.scala +++ b/shared/src/main/scala/io/lemonlabs/uri/decoding/PercentDecoder.scala @@ -3,17 +3,16 @@ package io.lemonlabs.uri.decoding import scala.annotation.tailrec import scala.collection.mutable -object PercentDecoder extends PercentDecoder(ignoreInvalidPercentEncoding = false) { +object PercentDecoder extends PercentDecoder(ignoreInvalidPercentEncoding = false, charset = "UTF-8") { protected val errorMessage = "It looks like this URL isn't Percent Encoded. Ideally you should Percent Encode the relevant parts " + "of your URL before passing to scala-uri. Alternatively, you can use a UriConfig with either " + "PercentDecoder(ignoreInvalidPercentEncoding=true) or NoopDecoder to suppress this Exception" - protected val cs = "UTF-8" protected val percentByte = '%'.toByte } -case class PercentDecoder(ignoreInvalidPercentEncoding: Boolean) extends UriDecoder { +case class PercentDecoder(ignoreInvalidPercentEncoding: Boolean, charset: String = "UTF-8") extends UriDecoder { import io.lemonlabs.uri.decoding.PercentDecoder._ def decodeBytes(s: String, charset: String): Array[Byte] = { @@ -43,12 +42,12 @@ case class PercentDecoder(ignoreInvalidPercentEncoding: Boolean) extends UriDeco throw new UriDecodeException(s"Encountered '%' followed by a non hex number '$hex'. $errorMessage") } case ch :: xs => - go(xs, result ++= ch.toString.getBytes(cs)) + go(xs, result ++= ch.toString.getBytes(charset)) } go(s.toCharArray.toList, new mutable.ArrayBuilder.ofByte) } def decode(s: String): String = - new String(decodeBytes(s, cs), cs) + new String(decodeBytes(s, charset), charset) } diff --git a/shared/src/test/scala/io/lemonlabs/uri/GithubIssuesTests.scala b/shared/src/test/scala/io/lemonlabs/uri/GithubIssuesTests.scala index 5e4b8dc1..b0d9d1d0 100644 --- a/shared/src/test/scala/io/lemonlabs/uri/GithubIssuesTests.scala +++ b/shared/src/test/scala/io/lemonlabs/uri/GithubIssuesTests.scala @@ -123,4 +123,13 @@ class GithubIssuesTests extends AnyFlatSpec with Matchers with OptionValues { "Github Issue #429" should "not allow forward slashes in host" in { Url.parseOption("https://\\") should equal(None) } + + "Github Issue #468" should "use configured charset for query params" in { + val urlPercentage = RelativeUrl.parse("/path?param=r%F3n")(UriConfig(charset = "ISO-8859-1")) + val urlRaw = RelativeUrl.parse("/path?param=rón")(UriConfig(charset = "ISO-8859-1")) + urlPercentage.toStringRaw should be("/path?param=rón") + urlRaw.toStringRaw should be("/path?param=rón") + urlPercentage.toString should be("/path?param=r%F3n") + urlRaw.toString should be("/path?param=r%F3n") + } }