Esempio n. 1
0
        public async Task WebTldRuleProviderTest()
        {
            var tldRuleProvider = new WebTldRuleProvider();
            var rules           = await tldRuleProvider.BuildAsync();

            Assert.IsNotNull(rules);
        }
Esempio n. 2
0
        public async Task RuleDivisionExample()
        {
            var provider = new WebTldRuleProvider();
            var rules    = await provider.BuildAsync();

            //ICANN and Private rules
            var domainParser = new DomainParser(rules);
            var domainInfo   = await domainParser.ParseAsync("sub.test.co.uk");

            Assert.Equal("test.co.uk", domainInfo.RegistrableDomain);
            domainInfo = await domainParser.ParseAsync("www.myblog.blogspot.co.uk");

            Assert.Equal("myblog.blogspot.co.uk", domainInfo.RegistrableDomain);

            //ICANN only rules
            var icannRules = rules.Where(x => x.Division == TldRuleDivision.ICANN);

            domainParser = new DomainParser(icannRules);
            domainInfo   = await domainParser.ParseAsync("sub.test.co.uk");

            Assert.Equal("test.co.uk", domainInfo.RegistrableDomain);
            domainInfo = await domainParser.ParseAsync("www.myblog.blogspot.co.uk");

            Assert.Equal("blogspot.co.uk", domainInfo.RegistrableDomain); //Now TLD returned
        }
        public async Task CheckCachedRules()
        {
            string tmpFile = Path.Combine(Path.GetTempPath(), "UnitTestPublicSuffixList.dat");

            if (File.Exists(tmpFile))
            {
                File.Delete(tmpFile);
            }

            ITldRuleProvider provider = new WebTldRuleProvider(fileName: tmpFile);
            var rules = await provider.BuildAsync();

            Assert.True(File.Exists(tmpFile));
            Assert.NotNull(rules);
            var ruleList = rules.ToList();

            Assert.True(ruleList.Count > 100); //Expecting lots of rules

            //Spot checks (If test fails here, verify that rules still exist on:
            //https://publicsuffix.org/list/public_suffix_list.dat
            var spotChecks = new string[] { "com", "*.bd", "blogspot.com" };
            var lookup     = ruleList.ToDictionary(x => x.Name, x => x.Name);

            Assert.True(spotChecks.All(x => lookup.ContainsKey(x)));

            //Verify cache
            var fileDateBefore = File.GetLastWriteTimeUtc(tmpFile);
            var cachedRules    = await provider.BuildAsync();

            var fileDateAfter = File.GetLastWriteTimeUtc(tmpFile);

            Assert.Equal(ruleList.Count, cachedRules.Count());
            Assert.Equal(fileDateBefore, fileDateAfter);
            Assert.True(cachedRules.All(x => lookup.ContainsKey(x.Name)));

            //Cleanup
            if (File.Exists(tmpFile))
            {
                File.Delete(tmpFile);
            }
        }
Esempio n. 4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(svcs =>
            {
                var provider  = new WebTldRuleProvider();
                var rules     = provider.BuildAsync().GetAwaiter().GetResult();
                var structure = new DomainDataStructure("*", new TldRule("*"));
                structure.AddRules(rules);
                return(structure);
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }
Esempio n. 5
0
        public static void LoadFromWeb()
        {
            var webTldRuleProvider = new WebTldRuleProvider(cacheProvider: new FileCacheProvider(cacheTimeToLive: new TimeSpan(10, 0, 0)));

            var domainParser = new DomainParser(webTldRuleProvider);

            for (var i = 0; i < 1000; i++)
            {
                var isValid = webTldRuleProvider.CacheProvider.IsCacheValid();
                if (!isValid)
                {
                    webTldRuleProvider.BuildAsync().GetAwaiter(); //Reload data
                }

                var domainInfo = domainParser.Get($"sub{i}.test.co.uk");

                Console.WriteLine("RegistrableDomain:{0}", domainInfo.RegistrableDomain);
                Console.WriteLine("SubDomain:{0}", domainInfo.SubDomain);
            }
        }
Esempio n. 6
0
        public async Task CachedProviderExample()
        {
            //Cache Public Suffix data to file, refresh after TTL expires
            //All params are optional (in which cases it uses same values as below as defaults)
            var cachedFile       = Path.Combine(Path.GetTempPath(), "public_suffix_list.dat");
            var plublicSuffixUrl = @"https://publicsuffix.org/list/public_suffix_list.dat";
            var timeToLive       = TimeSpan.FromDays(1);

            var provider = new WebTldRuleProvider(cachedFile, plublicSuffixUrl, timeToLive);
            var rules    = await provider.BuildAsync();

            Assert.NotEmpty(rules);
            Assert.False(provider.MustRefresh());

            //Lets manually force refresh of cache
            File.Delete(cachedFile);
            Assert.True(provider.MustRefresh());
            await provider.Refresh();

            Assert.False(provider.MustRefresh());
        }