Example #1
0
        /// <summary>
        /// Domain Info
        /// </summary>
        /// <param name="domain"></param>
        /// <param name="tldRule"></param>
        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 #2
0
        private void AddRule(TldRule tldRule)
        {
            var structure  = this._domainDataStructure;
            var domainPart = string.Empty;

            var parts = tldRule.Name.Split('.').Reverse().ToList();

            for (var i = 0; i < parts.Count; i++)
            {
                domainPart = parts[i];

                if (parts.Count - 1 > i)
                {
                    //Check if domain exists
                    if (!structure.Nested.ContainsKey(domainPart))
                    {
                        structure.Nested.Add(domainPart, new DomainDataStructure(domainPart));
                    }

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

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

                structure.Nested.Add(domainPart, new DomainDataStructure(domainPart, tldRule));
            }
        }
Example #3
0
        public List <TldRule> ParseRules(string[] lines)
        {
            var items = new List <TldRule>();

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

                //Ignore comments
                if (line.StartsWith("//"))
                {
                    continue;
                }

                //LL: Detect whitespace in middle of line

                var tldRule = new TldRule(line.Trim());

                items.Add(tldRule);
            }

            return(items);
        }
        public DomainName(Uri uri, TldRule tldRule, string unresolvedUrl)
        {
            if (uri != null && string.IsNullOrEmpty(uri.Host))
            {
                return;
            }

            if (tldRule == null)
            {
                return;
            }

            var domainParts       = uri.Host.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 (uri.Host.Equals(tld))
            {
                return;
            }

            this.TLDRule           = tldRule;
            this.Hostname          = uri.Host;
            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;

            this.OriginalUrl   = uri?.OriginalString;
            this.UnresolvedUrl = unresolvedUrl;
        }
Example #5
0
        private DomainName GetDomainFromParts(string domain, List <string> parts)
        {
            if (parts == null || parts.Count == 0 || parts.Any(x => x.Equals(string.Empty)))
            {
                return(null);
            }

            var structure = this._domainDataStructure;
            var matches   = new List <TldRule>();

            this.FindMatches(parts, structure, matches);

            //Sort so exceptions are first, then by biggest label count (with wildcards at bottom)
            var sortedMatches = matches.OrderByDescending(x => x.Type == TldRuleType.WildcardException ? 1 : 0)
                                .ThenByDescending(x => x.LabelCount)
                                .ThenByDescending(x => x.Name);

            var winningRule = sortedMatches.FirstOrDefault();

            if (winningRule == null)
            {
                winningRule = new TldRule("*");
            }

            //Domain is TLD
            if (parts.Count == winningRule.LabelCount)
            {
                return(null);
            }

            var domainName = new DomainName(domain, winningRule);

            return(domainName);
        }
        public DomainName Get(string domain)
        {
            if (string.IsNullOrEmpty(domain))
            {
                return(null);
            }

            //We use Uri methods to normalize host (So Punycode is converted to UTF-8
            Uri uri;

            if (!Uri.TryCreate(string.Concat("https://", domain), UriKind.RelativeOrAbsolute, out uri))
            {
                return(null);
            }

            var normalizedDomain = uri.Host;
            var normalizedHost   = uri.GetComponents(UriComponents.NormalizedHost, UriFormat.UriEscaped); //Normalize punycode

            var parts = normalizedHost
                        .Split('.')
                        .Reverse()
                        .ToList();

            if (parts.Count == 0 || parts.Any(x => x.Equals(string.Empty)))
            {
                return(null);
            }

            var structure = this._domainDataStructure;
            var matches   = new List <TldRule>();

            this.FindMatches(parts, structure, matches);

            //Sort so exceptions are first, then by biggest label count (with wildcards at bottom)
            var sortedMatches = matches.OrderByDescending(x => x.Type == TldRuleType.WildcardException ? 1 : 0)
                                .ThenByDescending(x => x.LabelCount)
                                .ThenByDescending(x => x.Name);

            var winningRule = sortedMatches.FirstOrDefault();

            if (winningRule == null)
            {
                winningRule = new TldRule("*");
            }

            //Domain is TLD
            if (parts.Count == winningRule.LabelCount)
            {
                return(null);
            }

            var domainName = new DomainName(normalizedDomain, winningRule);

            return(domainName);
        }
Example #7
0
        public IEnumerable <TldRule> ParseRules(IEnumerable <string> lines)
        {
            var items    = new List <TldRule>();
            var division = TldRuleDivision.Unknown;

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


                //Ignore comments (and set Division)
                if (line.StartsWith("//"))
                {
                    //Detect Division
                    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);
        }
        public DomainName Get(Uri uri)
        {
            var normalizedDomain = uri.Host;
            var normalizedHost   = uri.GetComponents(UriComponents.NormalizedHost, UriFormat.UriEscaped); //Normalize punycode

            var parts = normalizedHost
                        .Split('.')
                        .Reverse()
                        .ToList();

            if (parts.Count == 0 || parts.Any(x => x.Equals(string.Empty)))
            {
                return(null);
            }

            var structure = this._domainDataStructure;
            var matches   = new List <TldRule>();

            this.FindMatches(parts, structure, matches);

            //Sort so exceptions are first, then by biggest label count (with wildcards at bottom)
            var sortedMatches = matches.OrderByDescending(x => x.Type == TldRuleType.WildcardException ? 1 : 0)
                                .ThenByDescending(x => x.LabelCount)
                                .ThenByDescending(x => x.Name);

            var winningRule = sortedMatches.FirstOrDefault();

            if (winningRule == null)
            {
                winningRule = new TldRule("*");
            }

            //Domain is TLD
            if (parts.Count == winningRule.LabelCount)
            {
                return(null);
            }

            var domainName = new DomainName(normalizedDomain, winningRule);

            return(domainName);
        }
Example #9
0
 /// <summary>
 /// Creates a new <see cref="DomainDataStructure"/> for <paramref name="domain"/>.
 /// </summary>
 /// <param name="domain">The Domain.</param>
 /// <param name="tldRule">The type of TLD domain.</param>
 public DomainDataStructure(string domain, TldRule tldRule)
 {
     this.Domain  = domain;
     this.TldRule = tldRule;
     this.Nested  = new Dictionary <string, DomainDataStructure>();
 }
 public DomainName(Uri uri, TldRule tldRule) : this(uri, tldRule, string.Empty)
 {
 }
 /// <summary>
 /// Parse Exception
 /// </summary>
 /// <param name="errorMessage"></param>
 /// <param name="winningRule"></param>
 public ParseException(string errorMessage, TldRule winningRule = null)
 {
     this.ErrorMessage = errorMessage;
     this.WinningRule  = winningRule;
 }