Skip to content

Commit

Permalink
fixes issue lemonlabsuk#509 from original repo (#3)
Browse files Browse the repository at this point in the history
* fixes issue lemonlabsuk#509 from original repo

* formated files
  • Loading branch information
fedorovar authored Apr 23, 2024
1 parent d62c634 commit a3d5ba3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
12 changes: 12 additions & 0 deletions jvm/src/test/scala/io/lemonlabs/uri/ApexDomainTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ class ApexDomainTests extends AnyFlatSpec with Matchers {
Url.parse("http://maps.google.com").apexDomain should equal(Some("google.com"))
}

"DomainName" should "return apex domain for domain name in punycode" in {
DomainName.parse("www.example.xn--6frz82g").apexDomain should equal(Some("example.xn--6frz82g"))
}

"DomainName" should "return apex domain for domain name in unicode" in {
DomainName.parse("www.example.移动").apexDomain should equal(Some("example.移动"))
}

"DomainName" should "return apex domain for fully qualified domain name" in {
DomainName.parse("www.hello.example.com.").apexDomain should equal(Some("example.com."))
}

it should "return itself for apexDomain when it is the apex domain" in {
Url.parse("http://google.com").apexDomain should equal(Some("google.com"))
}
Expand Down
23 changes: 14 additions & 9 deletions shared/src/main/scala/io/lemonlabs/uri/Host.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,21 @@ final case class DomainName(value: String)(implicit val conf: UriConfig = UriCon
with PunycodeSupport {
type Self = DomainName

private def isValidPublicSuffix(suffix: String): Boolean =
if (PublicSuffixes.set.contains(suffix)) { true }
else if (PublicSuffixes.exceptions.contains(suffix)) { false }
else {
val dotIndex = suffix.indexOf('.')
if (dotIndex < 1) { false }
else {
PublicSuffixes.wildcardPrefixes.contains(suffix.substring(dotIndex + 1))
private def isValidPublicSuffix(suffix: String): Boolean = {
Try(toPunycode(suffix).stripSuffix("."))
.map { normalisedSuffix =>
if (PublicSuffixes.set.contains(normalisedSuffix)) { true }
else if (PublicSuffixes.exceptions.contains(normalisedSuffix)) { false }
else {
val dotIndex = normalisedSuffix.indexOf('.')
if (dotIndex < 1) { false }
else {
PublicSuffixes.wildcardPrefixes.contains(normalisedSuffix.substring(dotIndex + 1))
}
}
}
}
.getOrElse(false)
}

def withConfig(config: UriConfig): DomainName =
DomainName(value)(config)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.lemonlabs.uri.inet

object PublicSuffixes {
/*
All suffixes are normalised in punycode
*/
object PublicSuffixes extends PunycodeSupport {
lazy val exceptions: Set[String] = Set(
"www.ck",
"city.kawasaki.jp",
Expand Down Expand Up @@ -33,7 +36,8 @@ object PublicSuffixes {
"sch.uk"
)

lazy val set: Set[String] = publicSuffixes0 ++ publicSuffixes1
lazy val set: Set[String] = (publicSuffixes0 ++ publicSuffixes1).map(toPunycode)

private def publicSuffixes0 =
Set(
"ac",
Expand Down
10 changes: 10 additions & 0 deletions shared/src/test/scala/io/lemonlabs/uri/PublicSuffixTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class PublicSuffixTests extends AnyFlatSpec with Matchers {
uri.publicSuffix should equal(None)
}

it should "handle punycode correctly" in {
val uri = Url.parse("http://www.example.xn--6frz82g")
uri.publicSuffix should equal(Some("xn--6frz82g"))
}

it should "should handle trailing dot correctly" in {
val uri = Url.parse("http://www.example.com.")
uri.publicSuffix should equal(Some("com."))
}

"RelativeUrls" should "not return public suffixes" in {
val uri = RelativeUrl.parse("/blah")
uri.publicSuffix should equal(None)
Expand Down

0 comments on commit a3d5ba3

Please sign in to comment.