Represents a domain name ready for extracting its registered domain and TLD.
punycode.rb - PunyCode encoder for the Domain Name library
Copyright (C) 2011, 2012 Akinori MUSHA, All rights reserved.
Ported from puny.c, a part of VeriSign XCode (encode/decode) IDN Library.
Copyright (C) 2000-2002 Verisign Inc., All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3) Neither the name of the VeriSign Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software is licensed under the BSD open source license. For more information visit www.opensource.org.
Authors:
John Colosi (VeriSign) Srikanth Veeramachaneni (VeriSign) Nagesh Chigurupati (Verisign) Praveen Srinivasan(Verisign)
The least “universally original” domain part of this domain name. For example, “example.co.uk” for “www.sub.example.co.uk”.
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.
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.
# File lib/domain_name/etld_data.rb, line 4301 def self.etld_data ETLD_DATA end
Parses hostname into a DomainName object. An IP address is also accepted. An IPv6 address may be enclosed in square brackets.
# File lib/domain_name.rb, line 59 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 if domain return when -1 @domain = subdomain if 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
Normalizes a domain using the Punycode algorithm as necessary. The result will be a downcased, ASCII-only string.
# File lib/domain_name.rb, line 226 def normalize(domain) DomainName::Punycode.encode_hostname(domain.chomp(DOT).to_nfc).downcase end
# File lib/domain_name.rb, line 162 def <(other) case self <=> other when -1 true when nil nil else false end end
# File lib/domain_name.rb, line 184 def <=(other) case self <=> other when -1, 0 true when nil nil else false end end
# File lib/domain_name.rb, line 146 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
# File lib/domain_name.rb, line 141 def ==(other) other = DomainName.new(other) unless DomainName === other other.hostname == @hostname end
# File lib/domain_name.rb, line 173 def >(other) case self <=> other when 1 true when nil nil else false end end
# File lib/domain_name.rb, line 195 def >=(other) case self <=> other when 1, 0 true when nil nil else false end end
Returns true if this domain name has a canonical registered domain.
# File lib/domain_name.rb, line 51 def canonical? @canonical_tld_p && (@domain ? true : false) end
Returns true if this domain name has a canonical TLD.
# File lib/domain_name.rb, line 45 def canonical_tld? @canonical_tld_p end
# File lib/domain_name.rb, line 212 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
Returns true if this is an IP address, such as “192.168.0.1” and “[::1]”.
# File lib/domain_name.rb, line 35 def ipaddr? @ipaddr ? true : false end
# File lib/domain_name.rb, line 206 def to_s @hostname end
Generated with the Darkfish Rdoc Generator 2.