/// <summary>
        /// Add <paramref name="tldRule"/> to <paramref name="structure"/>.
        /// </summary>
        /// <param name="structure">The structure to appened the rule.</param>
        /// <param name="tldRule">The rule to append.</param>
        public static void AddRule(this DomainDataStructure structure, TldRule tldRule)
        {
            var parts = tldRule.Name.Split('.').Reverse().ToList();

            for (var i = 0; i < parts.Count; i++)
            {
                var domainPart = parts[i];
                if (parts.Count - 1 > i)
                {
                    if (!structure.Nested.ContainsKey(domainPart))
                    {
                        structure.Nested.Add(domainPart, new DomainDataStructure(domainPart));
                    }

                    structure = structure.Nested[domainPart];
                    continue;
                }

                if (structure.Nested.ContainsKey(domainPart))
                {
                    structure.Nested[domainPart].TldRule = tldRule;
                    continue;
                }

                structure.Nested.Add(domainPart, new DomainDataStructure(domainPart, tldRule));
            }
        }
Example #2
0
        public DomainInfo(string domain, TldRule tldRule)
        {
            if (string.IsNullOrEmpty(domain))
            {
                return;
            }

            if (tldRule == null)
            {
                return;
            }

            var domainParts       = domain.Split('.').Reverse().ToList();
            var ruleParts         = tldRule.Name.Split('.').Skip(tldRule.Type == TldRuleType.WildcardException ? 1 : 0).Reverse().ToList();
            var tld               = string.Join(".", domainParts.Take(ruleParts.Count).Reverse());
            var registrableDomain = string.Join(".", domainParts.Take(ruleParts.Count + 1).Reverse());

            if (domain.Equals(tld))
            {
                return;
            }

            this.TldRule           = tldRule;
            this.Hostname          = domain;
            this.Tld               = tld;
            this.RegistrableDomain = registrableDomain;

            this.Domain = domainParts.Skip(ruleParts.Count).FirstOrDefault();
            var subDomain = string.Join(".", domainParts.Skip(ruleParts.Count + 1).Reverse());

            this.SubDomain = string.IsNullOrEmpty(subDomain) ? null : subDomain;
        }
Example #3
0
        public IEnumerable <TldRule> ParseRules(IEnumerable <string> lines)
        {
            var items    = new List <TldRule>();
            var division = TldRuleDivision.Unknown;

            foreach (var line in lines)
            {
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                if (line.StartsWith("//"))
                {
                    if (line.StartsWith("// ===BEGIN ICANN DOMAINS==="))
                    {
                        division = TldRuleDivision.ICANN;
                    }
                    else if (line.StartsWith("// ===END ICANN DOMAINS==="))
                    {
                        division = TldRuleDivision.Unknown;
                    }
                    else if (line.StartsWith("// ===BEGIN PRIVATE DOMAINS==="))
                    {
                        division = TldRuleDivision.Private;
                    }
                    else if (line.StartsWith("// ===END PRIVATE DOMAINS==="))
                    {
                        division = TldRuleDivision.Unknown;
                    }

                    continue;
                }

                var tldRule = new TldRule(line.Trim(), division);
                items.Add(tldRule);
            }

            return(items);
        }
Example #4
0
 public DomainDataStructure(string domain, TldRule tldRule)
 {
     this.Domain  = domain;
     this.TldRule = tldRule;
     this.Nested  = new Dictionary <string, DomainDataStructure>();
 }
Example #5
0
 public ParseException(string errorMessage, TldRule winningRule = null)
 {
     this.ErrorMessage = errorMessage;
     this.WinningRule  = winningRule;
 }