Skip to content

Commit

Permalink
optimize parsing logic, not fire exception for a TLD
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 13, 2024
1 parent 2686ecf commit 6c8db36
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
20 changes: 9 additions & 11 deletions src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void Parse_ValidDomainWithSubdomain2()
}

[TestMethod]
[ExpectedException(typeof(ParseException))]
public void Parse_OnlyTopLevelDomain1()
{
var rules = new List<TldRule>
Expand All @@ -101,11 +100,12 @@ public void Parse_OnlyTopLevelDomain1()

var domainParser = this.GetDomainParser(rules);

domainParser.Parse("co.uk");
var domainInfo = domainParser.Parse("co.uk");
Assert.AreEqual("co.uk", domainInfo.TopLevelDomain);
Assert.AreEqual(null, domainInfo.RegistrableDomain);
}

[TestMethod]
[ExpectedException(typeof(ParseException))]
public void Parse_OnlyTopLevelDomain2()
{
var rules = new List<TldRule>
Expand All @@ -115,11 +115,13 @@ public void Parse_OnlyTopLevelDomain2()

var domainParser = this.GetDomainParser(rules);

domainParser.Parse("com");
var domainInfo = domainParser.Parse("com");
Assert.AreEqual("com", domainInfo.TopLevelDomain);
Assert.AreEqual(null, domainInfo.RegistrableDomain);
}

[TestMethod]
public void CheckDomainNameForUnlistedTld()
public void CheckDomainNameForNotListedTld()
{
var rules = new List<TldRule>
{
Expand All @@ -129,13 +131,9 @@ public void CheckDomainNameForUnlistedTld()

var domainParser = this.GetDomainParser(rules);

var domainName = domainParser.Parse("unlisted.domain.example");
var domainInfo = domainParser.Parse("unlisted.domain.example");

Assert.AreEqual("domain", domainName.Domain);
Assert.AreEqual("example", domainName.TopLevelDomain);
Assert.AreEqual("domain.example", domainName.RegistrableDomain);
Assert.AreEqual("unlisted", domainName.Subdomain);
Assert.AreEqual("*", domainName.TopLevelDomainRule.Name);
Assert.IsNull(domainInfo);
}

[TestMethod]
Expand Down
15 changes: 7 additions & 8 deletions src/Nager.PublicSuffix.UnitTest/NormalizationVariationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,19 @@ public void UnderscoreBasedVariationsInvalid()
}

[TestMethod]
[ExpectedException(typeof(ParseException))]
public void SingleWordTest()
{
this.PerformParsingCheck("singleword", null, null);
}

[TestMethod]
[ExpectedException(typeof(ParseException))]
//[ExpectedException(typeof(ParseException))]
public void SimpleNumberTest()
{
// Uri object transforms 12344 to 48.57 because 12345 = (48 * 256) + (57 * 1)
// This creates two parts separated by a dot which allows the rule-matching to handle it like a domain name
// Whereas the IdnMapper does not do this so the rule-matching fails because the 'domain name' is just a single word with no dot.
this.PerformParsingCheck("12345", "48.57", null);
this.PerformParsingCheck("12345", null, null);
}

[TestMethod]
Expand All @@ -86,14 +85,14 @@ public void DashBasedVariationsValid()
// For example "sub.-example.com" is valid but "double.sub.-example.com" is not.

this.PerformParsingCheck("-example.com", "-example.com", "-example.com");
this.PerformParsingCheck("example.-com", "example.-com", "example.-com");
this.PerformParsingCheck("example.com-", "example.com-", "example.com-");
this.PerformParsingCheck("example.-com-", "example.-com-", "example.-com-");
this.PerformParsingCheck("example.-com", null, null);
this.PerformParsingCheck("example.com-", null, null);
this.PerformParsingCheck("example.-com-", null, null);

this.PerformParsingCheck("sub.-example.com", "-example.com", "-example.com");
this.PerformParsingCheck("sub.example.com-", "example.com-", "example.com-");
this.PerformParsingCheck("sub.example.com-", null, null);

this.PerformParsingCheck("double.sub.example.com-", "example.com-", "example.com-");
this.PerformParsingCheck("double.sub.example.com-", null, null);
}

[TestMethod]
Expand Down
23 changes: 12 additions & 11 deletions src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,30 @@ protected void CheckPublicSuffix(string domain, string expected)
[DataRow(".example")]
[DataRow(".example.com")]
[DataRow(".example.example")]
[DataRow("example")]
[DataRow("mm")]
[DataRow("c.mm")]
[DataRow("c.kobe.jp")]
[DataRow("ck")]
[DataRow("test.ck")]
[ExpectedException(typeof(ParseException))]
public void ParseInvalidDomainCheck(string domain)
{
this.CheckPublicSuffix(domain, null);
}

[DataTestMethod]
[DataRow("example")]
public void ParseNotUsedTopLevelDomain_(string domain)
{
this.CheckPublicSuffix(domain, null);
}

[TestMethod]
public void ComprehensiveCheck()
{
// Mixed case.
this.CheckPublicSuffix("example.COM", "example.com");
this.CheckPublicSuffix("WwW.example.COM", "example.com");

// Unlisted TLD.
this.CheckPublicSuffix("example.example", "example.example");
this.CheckPublicSuffix("b.example.example", "example.example");
this.CheckPublicSuffix("a.b.example.example", "example.example");
// Not listed TLD.
this.CheckPublicSuffix("example.example", null);
this.CheckPublicSuffix("b.example.example", null);
this.CheckPublicSuffix("a.b.example.example", null);

// Listed, but non-Internet, TLD.
//this.CheckPublicSuffix("local", null);
Expand Down Expand Up @@ -140,7 +141,7 @@ public void ComprehensiveCheck()
[DataRow("ec2-34-206-8-177.compute-1.amazonaws.com")]
[DataRow("s3-website.us-east-2.amazonaws.com")]
[DataRow("test.stg.dev")]
[ExpectedExceptionWithMessage(typeof(ParseException), "Domain is a TLD according publicsuffix")]
//[ExpectedExceptionWithMessage(typeof(ParseException), "Domain is a TLD according publicsuffix")]
public void TldCheck(string domain)
{
this.CheckPublicSuffix(domain, null);
Expand Down
5 changes: 5 additions & 0 deletions src/Nager.PublicSuffix.WebApi/Nager.PublicSuffix.WebApi.http
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ GET {{Nager.PublicSuffix.WebApi_HostAddress}}/DomainInfo/mail.test.de/
Accept: application/json

###

GET {{Nager.PublicSuffix.WebApi_HostAddress}}/DomainInfo/s3-1.amazonaws.com/
Accept: application/json

###
11 changes: 11 additions & 0 deletions src/Nager.PublicSuffix/DomainInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public DomainInfo()
{
}

/// <summary>
/// Domain Info
/// </summary>
/// <param name="tldRule"></param>
public DomainInfo(
TldRule tldRule)
{
this.TopLevelDomainRule = tldRule;
this.TopLevelDomain = tldRule.Name;
}

/// <summary>
/// Domain Info
/// </summary>
Expand Down
18 changes: 15 additions & 3 deletions src/Nager.PublicSuffix/DomainParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ public bool IsValidDomain(string domain)
return false;
}

return !domainName.TopLevelDomainRule.Equals(this._rootTldRule);
var domainDataStructure = this._ruleProvider.GetDomainDataStructure();

return !domainName.TopLevelDomainRule.Equals(domainDataStructure.TldRule);
}
catch (ParseException)
{
Expand Down Expand Up @@ -133,22 +135,32 @@ private DomainInfo GetDomainFromParts(string domain, List<string> parts)

if (winningRule.Type == TldRuleType.Wildcard)
{
if (winningRule == domainDataStructure.TldRule)
{
return null;
}

if (tld.EndsWith(winningRule.Name.Substring(1)))
{
throw new ParseException("Domain is a TLD according publicsuffix", winningRule);
return new DomainInfo(winningRule);
}
}
else
{
if (tld.Equals(winningRule.Name))
{
throw new ParseException("Domain is a TLD according publicsuffix", winningRule);
return new DomainInfo(winningRule);
}
}

throw new ParseException($"Unknown domain {domain}");
}

if (winningRule == domainDataStructure.TldRule)
{
return null;
}

return new DomainInfo(domain, winningRule);
}

Expand Down

0 comments on commit 6c8db36

Please sign in to comment.