Represents a domain name ready for extracting its registered domain and TLD.
Methods
Public Class
Public Instance
Included modules
- 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 |
hostname_idn | [R] |
The Unicode representation of the |
Public Class methods
# File lib/httpx/domain_name.rb 55 def new(domain) 56 return domain if domain.is_a?(self) 57 58 super(domain) 59 end
Parses hostname into a DomainName
object. An IP address is also accepted. An IPv6 address may be enclosed in square brackets.
# 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
Normalizes a domain using the Punycode
algorithm as necessary. The result will be a downcased, ASCII-only string.
# 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
# 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
Checks if the server represented by this domain is qualified to send and receive cookies with a domain attribute value of domain. A true value given as the second argument represents cookies without a domain attribute value, in which case only hostname equality is checked.
# File lib/httpx/domain_name.rb 115 def cookie_domain?(domain, host_only = false) 116 # RFC 6265 #5.3 117 # When the user agent "receives a cookie": 118 return self == @domain if host_only 119 120 domain = DomainName.new(domain) 121 122 # RFC 6265 #5.1.3 123 # Do not perform subdomain matching against IP addresses. 124 @hostname == domain.hostname if @ipaddr 125 126 # RFC 6265 #4.1.1 127 # Domain-value must be a subdomain. 128 @domain && self <= domain && domain <= @domain 129 end