1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
# -*- coding: utf-8 -*-
#
# domain_name.rb - Domain Name manipulation library for Ruby
#
# Copyright (C) 2011-2017 Akinori MUSHA, All rights reserved.
#
require 'domain_name/version'
require 'domain_name/punycode'
require 'domain_name/etld_data'
require 'ipaddr'
# Represents a domain name ready for extracting its registered domain
# and TLD.
class DomainName
# 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.
attr_reader :hostname
# The Unicode representation of the #hostname property.
#
# :attr_reader: hostname_idn
# 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.
attr_reader :domain
# The Unicode representation of the #domain property.
#
# :attr_reader: domain_idn
# The TLD part of this domain name. For example, if the hostname is
# "www.sub.example.co.uk", the TLD part is "uk". This property is
# nil only if +ipaddr?+ is true. This may be nil if the hostname
# does not have one, like when it is an IP address or of a
# non-canonical domain.
attr_reader :tld
# The Unicode representation of the #tld property.
#
# :attr_reader: tld_idn
# Returns an IPAddr object if this is an IP address.
attr_reader :ipaddr
# Returns true if this is an IP address, such as "192.168.0.1" and
# "[::1]".
def ipaddr?
@ipaddr ? true : false
end
# Returns a host name representation suitable for use in the host
# name part of a URI. A host name, an IPv4 address, or a IPv6
# address enclosed in square brackets.
attr_reader :uri_host
# Returns true if this domain name has a canonical TLD.
def canonical_tld?
@canonical_tld_p
end
# Returns true if this domain name has a canonical registered
# domain.
def canonical?
@canonical_tld_p && (@domain ? true : false)
end
DOT = '.'.freeze # :nodoc:
# Parses _hostname_ into a DomainName object. An IP address is also
# accepted. An IPv6 address may be enclosed in square brackets.
def initialize(hostname)
hostname.is_a?(String) or
(hostname.respond_to?(:to_str) && (hostname = hostname.to_str).is_a?(String)) or
raise TypeError, "#{hostname.class} is not a String"
if hostname.start_with?(DOT)
raise ArgumentError, "domain name must not start with a dot: #{hostname}"
end
case hostname
when /\A([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\z/
@ipaddr = IPAddr.new($1)
@uri_host = @hostname = @ipaddr.to_s
@domain = @tld = nil
return
when /\A([0-9A-Fa-f:]*:[0-9A-Fa-f:]*:[0-9A-Fa-f:]*)\z/,
/\A\[([0-9A-Fa-f:]*:[0-9A-Fa-f:]*:[0-9A-Fa-f:]*)\]\z/
@ipaddr = IPAddr.new($1)
@hostname = @ipaddr.to_s
@uri_host = "[#{@hostname}]"
@domain = @tld = nil
return
end
@ipaddr = nil
@hostname = DomainName.normalize(hostname)
@uri_host = @hostname
if last_dot = @hostname.rindex(DOT)
@tld = @hostname[(last_dot + 1)..-1]
else
@tld = @hostname
end
etld_data = DomainName.etld_data
if @canonical_tld_p = etld_data.key?(@tld)
subdomain = domain = nil
parent = @hostname
loop {
case etld_data[parent]
when 0
@domain = domain
return
when -1
@domain = subdomain
return
when 1
@domain = parent
return
end
subdomain = domain
domain = parent
pos = @hostname.index(DOT, -domain.length) or break
parent = @hostname[(pos + 1)..-1]
}
else
# unknown/local TLD
if last_dot
# fallback - accept cookies down to second level
# cf. http://www.dkim-reputation.org/regdom-libs/
if penultimate_dot = @hostname.rindex(DOT, last_dot - 1)
@domain = @hostname[(penultimate_dot + 1)..-1]
else
@domain = @hostname
end
else
# no domain part - must be a local hostname
@domain = @tld
end
end
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.
def cookie_domain?(domain, host_only = false)
# RFC 6265 #5.3
# When the user agent "receives a cookie":
return self == domain if host_only
domain = DomainName.new(domain) unless DomainName === domain
if ipaddr?
# RFC 6265 #5.1.3
# Do not perform subdomain matching against IP addresses.
@hostname == domain.hostname
else
# RFC 6265 #4.1.1
# Domain-value must be a subdomain.
@domain && self <= domain && domain <= @domain ? true : false
end
end
# Returns the superdomain of this domain name.
def superdomain
return nil if ipaddr?
pos = @hostname.index(DOT) or return nil
self.class.new(@hostname[(pos + 1)..-1])
end
def ==(other)
other = DomainName.new(other) unless DomainName === other
other.hostname == @hostname
end
def <=>(other)
other = DomainName.new(other) unless DomainName === other
othername = other.hostname
if othername == @hostname
0
elsif @hostname.end_with?(othername) && @hostname[-othername.size - 1, 1] == DOT
# The other is higher
-1
elsif othername.end_with?(@hostname) && othername[-@hostname.size - 1, 1] == DOT
# The other is lower
1
else
nil
end
end
def <(other)
case self <=> other
when -1
true
when nil
nil
else
false
end
end
def >(other)
case self <=> other
when 1
true
when nil
nil
else
false
end
end
def <=(other)
case self <=> other
when -1, 0
true
when nil
nil
else
false
end
end
def >=(other)
case self <=> other
when 1, 0
true
when nil
nil
else
false
end
end
def to_s
@hostname
end
alias to_str to_s
def hostname_idn
@hostname_idn ||=
if @ipaddr
@hostname
else
DomainName::Punycode.decode_hostname(@hostname)
end
end
alias idn hostname_idn
def domain_idn
@domain_idn ||=
if @ipaddr
@domain
else
DomainName::Punycode.decode_hostname(@domain)
end
end
def tld_idn
@tld_idn ||=
if @ipaddr
@tld
else
DomainName::Punycode.decode_hostname(@tld)
end
end
def inspect
str = '#<%s:%s' % [self.class.name, @hostname]
if @ipaddr
str << ' (ipaddr)'
else
str << ' domain=' << @domain if @domain
str << ' tld=' << @tld if @tld
end
str << '>'
end
class << self
# Normalizes a _domain_ using the Punycode algorithm as necessary.
# Input must be strictly ASCII-only or unicode.
# The result will be a downcased, ASCII-only string.
def normalize(domain)
chomped = domain.chomp(DOT)
normalized = chomped.ascii_only? ? chomped : chomped.unicode_normalize(:nfc)
DomainName::Punycode.encode_hostname(normalized).downcase
end
end
end
# Short hand for DomainName.new().
def DomainName(hostname)
DomainName.new(hostname)
end
|