Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes issue #468 from original repo #5

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
Loading