module Wmap::Utils::DomainRoot

Module to validate and retrieve the top or second level domain name from a host-name (FQDN).

Constants

File_ccsld

Internet Domain Architecture Definitions

File_cctld
File_gtld
File_tld

Public Instance Methods

domain_root(host)
Alias for: get_domain_root
get_domain_root(host) click to toggle source

Main function to retrieve the registered domain ('domain root' from the 'registrant' perspective) from a hostname, for example, “www.telegraph.co.uk” -> “telegraph.co.uk”

# File lib/wmap/utils/domain_root.rb, line 22
    def get_domain_root (host)
            puts "Retrieve the root domain for host: #{host}" if @verbose
            if host.strip.nil?
                    puts "Error: empty record found. Please check your input and remove any empty line." if @verbose
                    return nil
            else
                    host=host.downcase.strip
            end
# First order - search country code second level domain list
root_domain = get_domain_root_by_ccsld(host)
if root_domain.nil?
            # Second order - search the country code top level domain list
  root_domain = get_domain_root_by_cctld(host)
  if root_domain.nil?
    # Third order - search top level domain list
    root_domain = get_domain_root_by_tlds(host)
    if root_domain.nil?
      # do nothing - no further search
    else
      return root_domain
    end
  else
    return root_domain
  end
else
  return root_domain
end
puts "#{host} - the top level domain is unknown. Please check out your record #{root_domain} " if @verbose
return nil
    #rescue => ee
    #      puts "Exception on method #{__method__}: #{ee}" if @verbose
    #      return nil
    end
get_domain_root_by_ccsld(host) click to toggle source

get domain root by lookup Country Code Second Level Domain list

# File lib/wmap/utils/domain_root.rb, line 61
def get_domain_root_by_ccsld(host)
  puts "First order search - domain root lookup by Country Code Second Level Domain list ..." if @verbose
  root_domain = nil
  dn = host.split(".")
  # Country code second level domain - loading once
              @ccsld=load_ccsld_from_file(File_ccsld) if @ccsld.nil?
  # search country code second level domain list
  if @ccsld.key?(dn.last)
    @ccsld[dn.last].each do |v|
      if ( v =~ /#{dn[dn.length-2]}/i )
        return dn[dn.length-3] + "." + dn[dn.length-2] + "." + dn.last
      end
    end
  end
  return root_domain
#rescue => ee
      #      puts "Exception on method #{__method__}: #{ee}" if @verbose
      #      return nil
end
get_domain_root_by_cctld(host) click to toggle source

get domain root by lookup Country Code Top Level Domain list

# File lib/wmap/utils/domain_root.rb, line 82
def get_domain_root_by_cctld(host)
  puts "Second order search - domain root lookup by Country Code Top Level Domain list ..." if @verbose
  root_domain = nil
  dn = host.split(".")
  # Country code top-level domain list - loading once
  @cctld=file_2_hash(File_cctld) if @cctld.nil?
  # Generic Top Level Domain List - loading once
  @gtld=file_2_hash(File_gtld) if @gtld.nil?
  # Country code second level domain - loading once
              @ccsld=load_ccsld_from_file(File_ccsld) if @ccsld.nil?
  # search the country code top level domain list
  if @cctld.key?(dn.last)
    # reverse search of general top level domain
    if @gtld.key?(dn[dn.length-2])
      root_domain=dn[dn.length-3] + "." + dn[dn.length-2] + "." + dn.last
    else
      root_domain=dn[dn.length-2] + "." + dn.last
    end
  end
  return root_domain
#rescue => ee
      #      puts "Exception on method #{__method__}: #{ee}" if @verbose
      #      return nil
end
get_domain_root_by_tlds(host) click to toggle source

get domain root by lookup Top Level Domain list

# File lib/wmap/utils/domain_root.rb, line 108
def get_domain_root_by_tlds(host)
  puts "Third order search - domain root lookup by Top Level Domain list ..." if @verbose
  root_domain = nil
  dn = host.split(".")
  # Comnplete Top Level Domain List - loading once
  @tlds=file_2_hash(File_tld) if @tlds.nil?
  # Country code top-level domain list - loading once
  @cctld=file_2_hash(File_cctld) if @cctld.nil?
  cc_found=false
  if @tlds.key?(dn.last)
    if @cctld.key?(dn[dn.length-2])
      cc_found=true
    end
    if cc_found
      root_domain=dn[dn.length-3] + "." + dn[dn.length-2] + "." + dn.last
    else
      root_domain=dn[dn.length-2] + "." + dn.last
    end
  end
  return root_domain
  #rescue => ee
      #    puts "Exception on method #{__method__}: #{ee}" if @verbose
      #    return nil
end
get_root_domain(host)
Alias for: get_domain_root
get_sub_domain(host) click to toggle source

Function to retrieve the sub-domain from a Fully Qualified Domain Name(FQDN), for example, “www.secure.telegraph.co.uk” -> “secure.telegraph.co.uk”

# File lib/wmap/utils/domain_root.rb, line 172
def get_sub_domain (host)
        puts "Retrieve sub-domain from host: #{host}" if @verbose
        subdomain=String.new
        host=host.strip.downcase
        domain=get_domain_root(host)
        record_h=host.split(".")
        record_d=domain.split(".")
        if (record_h.length - record_d.length) >= 2
                subdomain=record_h[record_h.length-record_d.length-1]+"."+domain
                puts "Sub domain found: #{subdomain}" if @verbose
                return subdomain
        else
                return nil
        end
rescue Exception => ee
        puts "Exception on method #{__method__} for #{host}: #{ee}" if @verbose
        return nil
end
Also aliased as: get_subdomain
get_subdomain(host)
Alias for: get_sub_domain
host_2_domain(host)
Alias for: get_domain_root
is_domain?(domain)
Alias for: is_domain_root?
is_domain_root?(domain) click to toggle source

Test a host string to see if it's a valid Internet root domain

# File lib/wmap/utils/domain_root.rb, line 159
def is_domain_root? (domain)
  puts "Validate the domain name is valid: #{domain}" if @verbose
        domain=domain.strip.downcase
        return domain == get_domain_root(domain)
rescue => ee
        puts "Exception on method #{__method__} for #{domain}: #{ee}" if @verbose
        return false
end
Also aliased as: is_root_domain?, is_domain?, is_root?
is_root?(domain)
Alias for: is_domain_root?
is_root_domain?(domain)
Alias for: is_domain_root?
print_ccsld() click to toggle source

Function to print instance variable - Country code second-level domain list

print_cctld() click to toggle source

Function to print instance variable - Country code top-level domain list

print_gtld() click to toggle source

Function to print instance variable - General top level domain list

root_domain(host)
Alias for: get_domain_root

Private Instance Methods

load_ccsld_from_file(file_ccsld) click to toggle source

'setter' to parse and load the known country code second level domain table from the file data structure example: {“uk” =>[“co”,“plc”],“za”=>}

# File lib/wmap/utils/domain_root.rb, line 135
def load_ccsld_from_file (file_ccsld)
        ccsld=Hash.new
        puts "Loading known country code second level domain list from file: #{file_ccsld}" if @verbose
        f=File.open(file_ccsld, 'r:ISO-8859-1:UTF-8')   # transcoded magic bit
        f.each do |line|
                next unless line =~ /^\s+\.\w/
                line=line.chomp.strip.downcase
                entry=line.split(' ')[0].split('.')
                if entry.length > 2
                        key=entry.last
                        ccsld[key] = Array.new if not ccsld.key?(key)
                        val=entry[entry.length-2]
                        #puts "Loading country code second level domain table with - Country code: #{key}, Second level domain: #{val}" if @verbose
                        ccsld[key].push(val) unless key.nil?
                end
        end
        f.close
        # Sort the blocks once in descendant order once for better performance
        return ccsld
rescue => ee
        puts "Exception on method #{__method__}: #{ee}" if @verbose
end