Skip to content

Commit

Permalink
optimize rule provider
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 15, 2024
1 parent 4092d0a commit 4f487d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
57 changes: 36 additions & 21 deletions src/Nager.PublicSuffix.TestConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Nager.PublicSuffix;
using Nager.PublicSuffix.RuleProviders;
using Nager.PublicSuffix.RuleProviders.CacheProviders;

using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
});
#region CachedHttpRuleProvider

var logger = loggerFactory.CreateLogger("Program");
var ruleProviderLogger = loggerFactory.CreateLogger<CachedHttpRuleProvider>();
//using var loggerFactory = LoggerFactory.Create(builder =>
//{
// builder.AddConsole();
//});

IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new List<KeyValuePair<string, string?>>
{
new("Nager:PublicSuffix:DataUrl", "https://publicsuffix.org/list/public_suffix_list1.dat")
})
.Build();
//var ruleProviderLogger = loggerFactory.CreateLogger<CachedHttpRuleProvider>();

var httpClient = new HttpClient();
var cacheProvider = new LocalFileSystemCacheProvider();
//IConfiguration configuration = new ConfigurationBuilder()
// .AddInMemoryCollection(new List<KeyValuePair<string, string?>>
// {
// new("Nager:PublicSuffix:DataUrl", "https://publicsuffix.org/list/public_suffix_list1.dat")
// })
// .Build();

var ruleProvider = new CachedHttpRuleProvider(ruleProviderLogger, configuration, cacheProvider, httpClient);
await ruleProvider.BuildAsync(ignoreCache: true);
//var httpClient = new HttpClient();
//var cacheProvider = new Nager.PublicSuffix.RuleProviders.CacheProviders.LocalFileSystemCacheProvider();

//var ruleProvider = new CachedHttpRuleProvider(ruleProviderLogger, configuration, cacheProvider, httpClient);

#endregion

#region

var ruleProvider = new SimpleHttpRuleProvider();

#endregion

#region LocalFileRuleProvider

//var ruleProvider = new LocalFileRuleProvider("public_suffix_list.dat");

#endregion

await ruleProvider.BuildAsync(ignoreCache: true);

var domainParser = new DomainParser(ruleProvider);
var domainInfo = domainParser.Parse("www.google.com");

if (domainInfo != null)
{
logger.LogInformation($"TLD:{domainInfo.TopLevelDomain}");
logger.LogInformation($"FQDN:{domainInfo.FullyQualifiedDomainName}");
logger.LogInformation($"RegistrableDomain:{domainInfo.RegistrableDomain}");
Console.WriteLine("------------------------------------------------");
Console.WriteLine($"{"TLD:", 20} {domainInfo.TopLevelDomain}");
Console.WriteLine($"{"FQDN:", 20} { domainInfo.FullyQualifiedDomainName}");
Console.WriteLine($"{"RegistrableDomain:", 20} {domainInfo.RegistrableDomain}");
Console.WriteLine($"{"Subdomain:",20} {domainInfo.Subdomain}");
Console.WriteLine("------------------------------------------------");
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SimpleHttpRuleProvider : BaseRuleProvider, IDisposable
/// <param name="httpClient"></param>
/// <param name="logger"></param>
public SimpleHttpRuleProvider(
IConfiguration configuration,
IConfiguration? configuration = null,
HttpClient? httpClient = null,
ILogger<SimpleHttpRuleProvider>? logger = null)
{
Expand All @@ -39,7 +39,7 @@ public SimpleHttpRuleProvider(
this._disposeHttpClient = httpClient == null;
this._httpClient = httpClient ?? new HttpClient();

var url = configuration["Nager:PublicSuffix:DataUrl"];
var url = configuration != null ? configuration["Nager:PublicSuffix:DataUrl"] : string.Empty;
if (string.IsNullOrEmpty(url))
{
url = "https://publicsuffix.org/list/public_suffix_list.dat";
Expand Down

0 comments on commit 4f487d9

Please sign in to comment.