diff --git a/lib/scala/downloader/src/main/scala/org/enso/downloader/http/URIBuilder.scala b/lib/scala/downloader/src/main/scala/org/enso/downloader/http/URIBuilder.scala index 7ac3f159ec8..ff653e30e4d 100644 --- a/lib/scala/downloader/src/main/scala/org/enso/downloader/http/URIBuilder.scala +++ b/lib/scala/downloader/src/main/scala/org/enso/downloader/http/URIBuilder.scala @@ -1,6 +1,7 @@ package org.enso.downloader.http -import java.net.URI +import java.net.{URI, URLEncoder} +import java.nio.charset.StandardCharsets /** A simple immutable builder for URIs based on URLs. * @@ -25,13 +26,15 @@ case class URIBuilder private (uri: URI) { * The query is appended at the end. */ def addQuery(key: String, value: String): URIBuilder = { - val scheme = uri.getScheme - val authority = uri.getAuthority - val path = uri.getPath - val query = if (uri.getQuery == null) "" else uri.getQuery + "&" - val fragment = uri.getFragment - val newQuery = query + key + "=" + value - val newUri = new URI(scheme, authority, path, newQuery, fragment) + val scheme = uri.getScheme + val authority = uri.getAuthority + val path = uri.getPath + val query = if (uri.getQuery == null) "" else uri.getQuery + "&" + val fragment = uri.getFragment + val encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8) + val encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8) + val newQuery = query + encodedKey + "=" + encodedValue + val newUri = new URI(scheme, authority, path, newQuery, fragment) copy(newUri) } diff --git a/lib/scala/downloader/src/test/scala/org/enso/downloader/http/URIBuilderSpec.scala b/lib/scala/downloader/src/test/scala/org/enso/downloader/http/URIBuilderSpec.scala index 49b7c3a2d59..d62ba527421 100644 --- a/lib/scala/downloader/src/test/scala/org/enso/downloader/http/URIBuilderSpec.scala +++ b/lib/scala/downloader/src/test/scala/org/enso/downloader/http/URIBuilderSpec.scala @@ -16,6 +16,12 @@ class URIBuilderSpec extends AnyWordSpec with Matchers { val uri = bldr.addQuery("foo", "bar").addQuery("baz", "qux").build() uri.toString mustEqual "http://google.com?foo=bar&baz=qux" } + + "Handle non-standard symbols in queries" in { + val bldr = URIBuilder.fromUri("http://google.com") + val uri = bldr.addQuery("foo", "bar baz").addQuery("baz", "qux").build() + uri.toString mustEqual "http://google.com?foo=bar+baz&baz=qux" + } } }