diff --git a/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithIdnMappingNormalization.cs b/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithIdnMappingNormalization.cs index 8dd9332..746bb64 100644 --- a/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithIdnMappingNormalization.cs +++ b/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithIdnMappingNormalization.cs @@ -1,6 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Nager.PublicSuffix.DomainNormalizers; using Nager.PublicSuffix.Models; +using Nager.PublicSuffix.RuleProviders; using System.Collections.Generic; namespace Nager.PublicSuffix.UnitTest.DemoRules @@ -10,8 +11,10 @@ public class DomainParserTestWithIdnMappingNormalization : DomainParserTest { protected override IDomainParser GetDomainParser(List rules) { + var ruleProvider = new StaticRuleProvider(rules); var domainNormalizer = new IdnMappingDomainNormalizer(); - return new DomainParser(rules, domainNormalizer); + + return new DomainParser(ruleProvider, domainNormalizer); } } } diff --git a/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithInitializedStructure.cs b/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithInitializedStructure.cs deleted file mode 100644 index 1ce2e12..0000000 --- a/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithInitializedStructure.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Nager.PublicSuffix.Extensions; -using Nager.PublicSuffix.Models; -using System.Collections.Generic; - -namespace Nager.PublicSuffix.UnitTest.DemoRules -{ - [TestClass] - public class DomainParserTestWithInitializedStructure : DomainParserTest - { - protected override IDomainParser GetDomainParser(List rules) - { - var structure = new DomainDataStructure("*", new TldRule("*")); - structure.AddRules(rules); - - return new DomainParser(structure); - } - } -} diff --git a/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithUriNormalization.cs b/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithUriNormalization.cs index 6c2c730..37066af 100644 --- a/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithUriNormalization.cs +++ b/src/Nager.PublicSuffix.UnitTest/DemoRules/DomainParserTestWithUriNormalization.cs @@ -1,6 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Nager.PublicSuffix.DomainNormalizers; using Nager.PublicSuffix.Models; +using Nager.PublicSuffix.RuleProviders; using System.Collections.Generic; namespace Nager.PublicSuffix.UnitTest.DemoRules @@ -10,9 +11,10 @@ public class DomainParserTestWithUriNormalization : DomainParserTest { protected override IDomainParser GetDomainParser(List rules) { + var ruleProvider = new StaticRuleProvider(rules); var domainNormalizer = new UriDomainNormalizer(); - return new DomainParser(rules, domainNormalizer); + return new DomainParser(ruleProvider, domainNormalizer); } } } diff --git a/src/Nager.PublicSuffix.UnitTest/Nager.PublicSuffix.UnitTest.csproj b/src/Nager.PublicSuffix.UnitTest/Nager.PublicSuffix.UnitTest.csproj index 38ccac0..96432b3 100644 --- a/src/Nager.PublicSuffix.UnitTest/Nager.PublicSuffix.UnitTest.csproj +++ b/src/Nager.PublicSuffix.UnitTest/Nager.PublicSuffix.UnitTest.csproj @@ -7,6 +7,7 @@ + diff --git a/src/Nager.PublicSuffix.UnitTest/NormalizationVariationTests.cs b/src/Nager.PublicSuffix.UnitTest/NormalizationVariationTests.cs index 68bafdc..2a2967b 100644 --- a/src/Nager.PublicSuffix.UnitTest/NormalizationVariationTests.cs +++ b/src/Nager.PublicSuffix.UnitTest/NormalizationVariationTests.cs @@ -2,6 +2,7 @@ using Nager.PublicSuffix.DomainNormalizers; using Nager.PublicSuffix.Exceptions; using Nager.PublicSuffix.Models; +using Nager.PublicSuffix.RuleProviders; using System.Collections.Generic; namespace Nager.PublicSuffix.UnitTest @@ -27,8 +28,10 @@ public void Initialize() new TldRule("com") }; - this._parserUsingUriNormalization = new DomainParser(rules, new UriDomainNormalizer()); - this._parserUsingIdnNormalization = new DomainParser(rules, new IdnMappingDomainNormalizer()); + var ruleProvider = new StaticRuleProvider(rules); + + this._parserUsingUriNormalization = new DomainParser(ruleProvider, new UriDomainNormalizer()); + this._parserUsingIdnNormalization = new DomainParser(ruleProvider, new IdnMappingDomainNormalizer()); } [TestMethod] diff --git a/src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTestsWithIdnMappingNormalization.cs b/src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTestsWithIdnMappingNormalization.cs index f2cf7c9..7db3c21 100644 --- a/src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTestsWithIdnMappingNormalization.cs +++ b/src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTestsWithIdnMappingNormalization.cs @@ -10,7 +10,7 @@ public class PublicSuffixTestsWithIdnMappingNormalization : PublicSuffixTest [TestInitialize()] public void Initialize() { - var domainParser = new DomainParser(new FileTldRuleProvider("public_suffix_list.dat"), new IdnMappingDomainNormalizer()); + var domainParser = new DomainParser(new LocalFileRuleProvider("public_suffix_list.dat"), new IdnMappingDomainNormalizer()); this._domainParser = domainParser; } } diff --git a/src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTestsWithUriNormalization.cs b/src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTestsWithUriNormalization.cs index 46a0ad1..08ae5d1 100644 --- a/src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTestsWithUriNormalization.cs +++ b/src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTestsWithUriNormalization.cs @@ -10,7 +10,7 @@ public class PublicSuffixTestsWithUriNormalization : PublicSuffixTest [TestInitialize()] public void Initialize() { - var domainParser = new DomainParser(new FileTldRuleProvider("public_suffix_list.dat"), new UriDomainNormalizer()); + var domainParser = new DomainParser(new LocalFileRuleProvider("public_suffix_list.dat"), new UriDomainNormalizer()); this._domainParser = domainParser; } } diff --git a/src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs b/src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs index 7561c94..0caed63 100644 --- a/src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs +++ b/src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs @@ -1,6 +1,8 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.Extensions.Configuration; +using Microsoft.VisualStudio.TestTools.UnitTesting; using Nager.PublicSuffix.RuleProviders; using System.Linq; +using System.Net.Http; using System.Threading.Tasks; namespace Nager.PublicSuffix.UnitTest @@ -11,18 +13,24 @@ public class RuleProviderTest [TestMethod] public async Task WebTldRuleProviderTest() { - var webRuleProvider = new WebRuleProvider(); - var rules = await webRuleProvider.BuildAsync(); - Assert.IsNotNull(rules); + var builder = new ConfigurationBuilder(); + using var httpClient = new HttpClient(); + + var configuration = builder.Build(); + + var webRuleProvider = new WebRuleProvider(configuration, httpClient); + var domainDataStructure = await webRuleProvider.BuildAsync(); + Assert.IsNotNull(domainDataStructure); } [TestMethod] public async Task FileTldRuleProviderTest() { var localFileRuleProvider = new LocalFileRuleProvider("public_suffix_list.dat"); - var rules = await localFileRuleProvider.BuildAsync(); - Assert.AreEqual(9609, rules.Count()); - Assert.IsNotNull(rules); + var domainDataStructure = await localFileRuleProvider.BuildAsync(); + + Assert.IsNotNull(domainDataStructure); + Assert.AreEqual(1460, domainDataStructure.Nested.Count); } } } diff --git a/src/Nager.PublicSuffix/DomainNormalizers/IDomainNormalizer.cs b/src/Nager.PublicSuffix/DomainNormalizers/IDomainNormalizer.cs index 53f586a..89f35ff 100644 --- a/src/Nager.PublicSuffix/DomainNormalizers/IDomainNormalizer.cs +++ b/src/Nager.PublicSuffix/DomainNormalizers/IDomainNormalizer.cs @@ -2,6 +2,9 @@ namespace Nager.PublicSuffix.DomainNormalizers { + /// + /// Interface DomainNormalizer + /// public interface IDomainNormalizer { List PartlyNormalizeDomainAndExtractFullyNormalizedParts(string domain, out string partlyNormalizedDomain); diff --git a/src/Nager.PublicSuffix/DomainNormalizers/IdnMappingDomainNormalizer.cs b/src/Nager.PublicSuffix/DomainNormalizers/IdnMappingDomainNormalizer.cs index eb97389..d700878 100644 --- a/src/Nager.PublicSuffix/DomainNormalizers/IdnMappingDomainNormalizer.cs +++ b/src/Nager.PublicSuffix/DomainNormalizers/IdnMappingDomainNormalizer.cs @@ -6,7 +6,7 @@ namespace Nager.PublicSuffix.DomainNormalizers { /// - /// IdnMappingNormalizer + /// IdnMapping DomainNormalizer /// public class IdnMappingDomainNormalizer : IDomainNormalizer { diff --git a/src/Nager.PublicSuffix/DomainNormalizers/UriDomainNormalizer.cs b/src/Nager.PublicSuffix/DomainNormalizers/UriDomainNormalizer.cs index 9ebd9e2..1c79b91 100644 --- a/src/Nager.PublicSuffix/DomainNormalizers/UriDomainNormalizer.cs +++ b/src/Nager.PublicSuffix/DomainNormalizers/UriDomainNormalizer.cs @@ -5,6 +5,9 @@ namespace Nager.PublicSuffix.DomainNormalizers { + /// + /// Uri DomainNormalizer + /// public class UriDomainNormalizer : IDomainNormalizer { public List PartlyNormalizeDomainAndExtractFullyNormalizedParts(string domain, out string partlyNormalizedDomain) diff --git a/src/Nager.PublicSuffix/DomainParser.cs b/src/Nager.PublicSuffix/DomainParser.cs index ffbb285..934d802 100644 --- a/src/Nager.PublicSuffix/DomainParser.cs +++ b/src/Nager.PublicSuffix/DomainParser.cs @@ -1,6 +1,5 @@ using Nager.PublicSuffix.DomainNormalizers; using Nager.PublicSuffix.Exceptions; -using Nager.PublicSuffix.Extensions; using Nager.PublicSuffix.Models; using Nager.PublicSuffix.RuleProviders; using System; @@ -14,47 +13,22 @@ namespace Nager.PublicSuffix /// public class DomainParser : IDomainParser { - private DomainDataStructure _domainDataStructure; + private readonly DomainDataStructure _domainDataStructure; private readonly IDomainNormalizer _domainNormalizer; private readonly TldRule _rootTldRule = new TldRule("*"); - /// - /// Creates and initializes a DomainParser - /// - /// The list of rules. - /// An . - public DomainParser(IEnumerable rules, IDomainNormalizer domainNormalizer = null) - : this(domainNormalizer) - { - if (rules == null) - { - throw new ArgumentNullException(nameof(rules)); - } - - this.AddRules(rules); - } - /// /// Creates and initializes a DomainParser /// /// A rule provider from interface . /// An . - public DomainParser(IRuleProvider ruleProvider, IDomainNormalizer domainNormalizer = null) + public DomainParser( + IRuleProvider ruleProvider, + IDomainNormalizer domainNormalizer = default) : this(domainNormalizer) { - var rules = ruleProvider.BuildAsync().GetAwaiter().GetResult(); - this.AddRules(rules); - } - - /// - /// Creates a DomainParser based on an already initialzed tree. - /// - /// An already initialized tree. - /// An . - public DomainParser(DomainDataStructure initializedDataStructure, IDomainNormalizer domainNormalizer = null) - : this(domainNormalizer) - { - this._domainDataStructure = initializedDataStructure; + var domainDataStructure = ruleProvider.BuildAsync().GetAwaiter().GetResult(); + this._domainDataStructure = domainDataStructure; } private DomainParser(IDomainNormalizer domainNormalizer) @@ -62,7 +36,7 @@ private DomainParser(IDomainNormalizer domainNormalizer) this._domainNormalizer = domainNormalizer ?? new UriDomainNormalizer(); } - /// + /// public DomainInfo Parse(Uri domain) { var partlyNormalizedDomain = domain.Host; @@ -76,14 +50,14 @@ public DomainInfo Parse(Uri domain) return this.GetDomainFromParts(partlyNormalizedDomain, parts); } - /// + /// public DomainInfo Parse(string domain) { var parts = this._domainNormalizer.PartlyNormalizeDomainAndExtractFullyNormalizedParts(domain, out string partlyNormalizedDomain); return this.GetDomainFromParts(partlyNormalizedDomain, parts); } - /// + /// public bool IsValidDomain(string domain) { if (string.IsNullOrEmpty(domain)) @@ -129,13 +103,6 @@ public bool IsValidDomain(string domain) } } - private void AddRules(IEnumerable tldRules) - { - this._domainDataStructure = this._domainDataStructure ?? new DomainDataStructure("*", this._rootTldRule); - - this._domainDataStructure.AddRules(tldRules); - } - private DomainInfo GetDomainFromParts(string domain, List parts) { if (parts == null || parts.Count == 0 || parts.Any(x => x.Equals(string.Empty))) diff --git a/src/Nager.PublicSuffix/Models/DomainDataStructure.cs b/src/Nager.PublicSuffix/Models/DomainDataStructure.cs index 010d554..5d8562a 100644 --- a/src/Nager.PublicSuffix/Models/DomainDataStructure.cs +++ b/src/Nager.PublicSuffix/Models/DomainDataStructure.cs @@ -1,9 +1,10 @@ using System.Collections.Generic; +using System.Linq; namespace Nager.PublicSuffix.Models { /// - /// Represents a tree of TLD domains + /// Represents a tree of Top Level Domains (TLD) /// public class DomainDataStructure { diff --git a/src/Nager.PublicSuffix/Nager.PublicSuffix.csproj b/src/Nager.PublicSuffix/Nager.PublicSuffix.csproj index ed8bd94..13196fe 100644 --- a/src/Nager.PublicSuffix/Nager.PublicSuffix.csproj +++ b/src/Nager.PublicSuffix/Nager.PublicSuffix.csproj @@ -33,5 +33,9 @@ + + + + diff --git a/src/Nager.PublicSuffix/RuleProviders/IRuleProvider.cs b/src/Nager.PublicSuffix/RuleProviders/IRuleProvider.cs index 299aaad..fb324b7 100644 --- a/src/Nager.PublicSuffix/RuleProviders/IRuleProvider.cs +++ b/src/Nager.PublicSuffix/RuleProviders/IRuleProvider.cs @@ -1,5 +1,4 @@ using Nager.PublicSuffix.Models; -using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -15,6 +14,6 @@ public interface IRuleProvider /// /// /// Returns the TldRules - Task> BuildAsync(CancellationToken cancellationToken = default); + Task BuildAsync(CancellationToken cancellationToken = default); } } diff --git a/src/Nager.PublicSuffix/RuleProviders/LocalFileRuleProvider.cs b/src/Nager.PublicSuffix/RuleProviders/LocalFileRuleProvider.cs index 5505e2c..2578121 100644 --- a/src/Nager.PublicSuffix/RuleProviders/LocalFileRuleProvider.cs +++ b/src/Nager.PublicSuffix/RuleProviders/LocalFileRuleProvider.cs @@ -1,6 +1,6 @@ -using Nager.PublicSuffix.Models; +using Nager.PublicSuffix.Extensions; +using Nager.PublicSuffix.Models; using Nager.PublicSuffix.RuleParsers; -using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -23,15 +23,19 @@ public LocalFileRuleProvider(string filePath) this._filePath = filePath; } - /// - public async Task> BuildAsync( + /// + public async Task BuildAsync( CancellationToken cancellationToken = default) { var ruleData = await this.LoadFromFile().ConfigureAwait(false); var ruleParser = new TldRuleParser(); var rules = ruleParser.ParseRules(ruleData); - return rules; + + var domainDataStructure = new DomainDataStructure("*", new TldRule("*")); + domainDataStructure.AddRules(rules); + + return domainDataStructure; } private async Task LoadFromFile() diff --git a/src/Nager.PublicSuffix/RuleProviders/StaticRuleProvider.cs b/src/Nager.PublicSuffix/RuleProviders/StaticRuleProvider.cs new file mode 100644 index 0000000..29e6b40 --- /dev/null +++ b/src/Nager.PublicSuffix/RuleProviders/StaticRuleProvider.cs @@ -0,0 +1,44 @@ +using Nager.PublicSuffix.Extensions; +using Nager.PublicSuffix.Models; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace Nager.PublicSuffix.RuleProviders +{ + /// + /// StaticRuleProvider + /// + public class StaticRuleProvider : IRuleProvider + { + private readonly DomainDataStructure _domainDataStructure; + + /// + /// StaticRuleProvider + /// + /// + public StaticRuleProvider(DomainDataStructure domainDataStructure) + { + this._domainDataStructure = domainDataStructure; + } + + /// + /// StaticRuleProvider + /// + /// + public StaticRuleProvider(IEnumerable tldRules) + { + var domainDataStructure = new DomainDataStructure("*", new TldRule("*")); + domainDataStructure.AddRules(tldRules); + + this._domainDataStructure = domainDataStructure; + } + + /// + public Task BuildAsync( + CancellationToken cancellationToken = default) + { + return Task.FromResult(this._domainDataStructure); + } + } +} diff --git a/src/Nager.PublicSuffix/RuleProviders/WebRuleProvider.cs b/src/Nager.PublicSuffix/RuleProviders/WebRuleProvider.cs index aab97e1..dbbfeb9 100644 --- a/src/Nager.PublicSuffix/RuleProviders/WebRuleProvider.cs +++ b/src/Nager.PublicSuffix/RuleProviders/WebRuleProvider.cs @@ -1,9 +1,9 @@ using Microsoft.Extensions.Configuration; using Nager.PublicSuffix.CacheProviders; using Nager.PublicSuffix.Exceptions; +using Nager.PublicSuffix.Extensions; using Nager.PublicSuffix.Models; using Nager.PublicSuffix.RuleParsers; -using System.Collections.Generic; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -56,8 +56,8 @@ public WebRuleProvider( this._cacheProvider = cacheProvider; } - /// - public async Task> BuildAsync( + /// + public async Task BuildAsync( CancellationToken cancellationToken = default) { var ruleParser = new TldRuleParser(); @@ -74,7 +74,11 @@ public async Task> BuildAsync( } var rules = ruleParser.ParseRules(ruleData); - return rules; + + var domainDataStructure = new DomainDataStructure("*", new TldRule("*")); + domainDataStructure.AddRules(rules); + + return domainDataStructure; } ///