class HTTPX::DomainName

  1. lib/httpx/domain_name.rb
Superclass: Object

Represents a domain name ready for extracting its registered domain and TLD.

Methods

Public Class

  1. new
  2. new
  3. normalize

Public Instance

  1. <=>
  2. cookie_domain?
  3. domain
  4. hostname
  5. hostname_idn

Included modules

  1. Comparable

Attributes

domain [R]

The least “universally original” domain part of this domain name. For example, “example.co.uk” for “www.sub.example.co.uk”. This may be nil if the hostname does not have one, like when it is an IP address, an effective TLD or higher itself, or of a non-canonical domain.

hostname [R]

The full host name normalized, ASCII-ized and downcased using the Unicode NFC rules and the Punycode algorithm. If initialized with an IP address, the string representation of the IP address suitable for opening a connection to.

hostname_idn [R]

The Unicode representation of the hostname property.

Public Class methods

new(domain)
[show source]
   # File lib/httpx/domain_name.rb
55 def new(domain)
56   return domain if domain.is_a?(self)
57 
58   super(domain)
59 end
new(hostname)

Parses hostname into a DomainName object. An IP address is also accepted. An IPv6 address may be enclosed in square brackets.

[show source]
    # File lib/httpx/domain_name.rb
 75 def initialize(hostname)
 76   hostname = String(hostname)
 77 
 78   raise ArgumentError, "domain name must not start with a dot: #{hostname}" if hostname.start_with?(".")
 79 
 80   begin
 81     @ipaddr = IPAddr.new(hostname)
 82     @hostname = @ipaddr.to_s
 83     return
 84   rescue IPAddr::Error
 85     nil
 86   end
 87 
 88   @hostname = DomainName.normalize(hostname)
 89   tld = if (last_dot = @hostname.rindex("."))
 90     @hostname[(last_dot + 1)..-1]
 91   else
 92     @hostname
 93   end
 94 
 95   # unknown/local TLD
 96   @domain = if last_dot
 97     # fallback - accept cookies down to second level
 98     # cf. http://www.dkim-reputation.org/regdom-libs/
 99     if (penultimate_dot = @hostname.rindex(".", last_dot - 1))
100       @hostname[(penultimate_dot + 1)..-1]
101     else
102       @hostname
103     end
104   else
105     # no domain part - must be a local hostname
106     tld
107   end
108 end
normalize(domain)

Normalizes a domain using the Punycode algorithm as necessary. The result will be a downcased, ASCII-only string.

[show source]
   # File lib/httpx/domain_name.rb
63 def normalize(domain)
64   unless domain.ascii_only?
65     domain = domain.chomp(".").unicode_normalize(:nfc)
66     domain = Punycode.encode_hostname(domain)
67   end
68 
69   domain.downcase
70 end

Public Instance methods

<=>(other)
[show source]
    # File lib/httpx/domain_name.rb
131 def <=>(other)
132   other = DomainName.new(other)
133   othername = other.hostname
134   if othername == @hostname
135     0
136   elsif @hostname.end_with?(othername) && @hostname[-othername.size - 1, 1] == "."
137     # The other is higher
138     -1
139   else
140     # The other is lower
141     1
142   end
143 end