diff options
author | adamw <adam@warski.org> | 2017-11-27 11:36:45 +0100 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-11-27 11:36:45 +0100 |
commit | 54722c73e472178f96416fbff8929d9a699d3e03 (patch) | |
tree | 3d812a94d4a909270f9a6335d0966f536cab549c /core | |
parent | 01b1d2b9dc062ab004f096ffb6fcb175f28d95aa (diff) | |
download | sttp-54722c73e472178f96416fbff8929d9a699d3e03.tar.gz sttp-54722c73e472178f96416fbff8929d9a699d3e03.tar.bz2 sttp-54722c73e472178f96416fbff8929d9a699d3e03.zip |
Properly encoding ipv6 addresses
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/Uri.scala | 10 | ||||
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/UriTests.scala | 19 |
2 files changed, 26 insertions, 3 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/Uri.scala b/core/src/main/scala/com/softwaremill/sttp/Uri.scala index eebd450..dac16b8 100644 --- a/core/src/main/scala/com/softwaremill/sttp/Uri.scala +++ b/core/src/main/scala/com/softwaremill/sttp/Uri.scala @@ -117,7 +117,7 @@ case class Uri(scheme: String, val schemeS = encode(Rfc3986.Scheme)(scheme) val userInfoS = userInfo.fold("")(encodeUserInfo(_) + "@") - val hostS = encode(Rfc3986.Host)(java.net.IDN.toASCII(host)) + val hostS = encodeHost val portS = port.fold("")(":" + _) val pathPrefixS = if (path.isEmpty) "" else "/" val pathS = path.map(encode(Rfc3986.PathSegment)).mkString("/") @@ -162,6 +162,14 @@ case class Uri(scheme: String, val QueryNoStandardDelims: Set[Char] = Query -- Set('&', '=') } + private val IpV6Pattern = "[0-9a-fA-F:]+".r + + private def encodeHost: String = + host match { + case IpV6Pattern() => s"[$host]" + case _ => encode(Rfc3986.Host)(java.net.IDN.toASCII(host)) + } + /** * @param spaceAsPlus In the query, space is encoded as a `+`. In other * contexts, it should be %-encoded as `%20`. diff --git a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala index 3c54fd2..23fe3d5 100644 --- a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala @@ -50,8 +50,7 @@ class UriTests extends FunSuite with Matchers { "http://example.com#f:g/h%20i", Uri("http", None, "example.com", None, List("key=value"), Nil, None) -> "http://example.com/key=value", - Uri("http", "www.mikołak.net") -> - "http://www.xn--mikoak-6db.net" + Uri("2001:db8::ff00:42:8329", 8080) -> "http://[2001:db8::ff00:42:8329]:8080" ) for { @@ -112,6 +111,22 @@ class UriTests extends FunSuite with Matchers { } } + val hostTestData = List( + "www.mikołak.net" -> "http://www.xn--mikoak-6db.net", + "192.168.1.0" -> "http://192.168.1.0", + "::1" -> "http://[::1]", + "2001:db8::ff00:42:8329" -> "http://[2001:db8::ff00:42:8329]", + "2001:0db8:0000:0000:0000:ff00:0042:8329" -> "http://[2001:0db8:0000:0000:0000:ff00:0042:8329]" + ) + + for { + (host, expected) <- hostTestData + } { + test(s"host $host should serialize to $expected") { + Uri(host).toString should be(s"$expected") + } + } + test("should convert from java URI") { val uriAsString = "https://sub.example.com:8080/a/b/xyz?p1=v1&p2=v2#f" Uri(URI.create(uriAsString)).toString should be(uriAsString) |