Skip to content

Commit

Permalink
fixes issue lemonlabsuk#468 from original repo (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedorovar authored Apr 23, 2024
1 parent 1b3ea88 commit d62c634
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
15 changes: 10 additions & 5 deletions shared/src/main/scala/io/lemonlabs/uri/config/UriConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit d62c634

Please sign in to comment.