Parent

Namespace

Files

DomainName

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)

Constants

DOT
ETLD_DATA

Attributes

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.

domain[R]

The least “universally original” domain part of this domain name. For example, “example.co.uk“ for “www.sub.example.co.uk“.

tld[R]

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.

ipaddr[R]

Returns an IPAddr object if this is an IP address.

uri_host[R]

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.

Public Class Methods

etld_data() click to toggle source
      # File lib/domain_name/etld_data.rb, line 4301
4301:   def self.etld_data
4302:     ETLD_DATA
4303:   end
new(hostname) click to toggle source

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 60
 60:   def initialize(hostname)
 61:     hostname.is_a?(String) or
 62:       (hostname.respond_to?(:to_str) && (hostname = hostname.to_str).is_a?(String)) or
 63:       raise TypeError, "#{hostname.class} is not a String"
 64:     if hostname.start_with?(DOT)
 65:       raise ArgumentError, "domain name must not start with a dot: #{hostname}"
 66:     end
 67:     case hostname
 68:     when /\A([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\z/
 69:       @ipaddr = IPAddr.new($1)
 70:       @uri_host = @hostname = @ipaddr.to_s
 71:       @domain = @tld = nil
 72:       return
 73:     when /\A([0-9A-Fa-f:]*:[0-9A-Fa-f:]*:[0-9A-Fa-f:]*)\z/,
 74:       /\A\[([0-9A-Fa-f:]*:[0-9A-Fa-f:]*:[0-9A-Fa-f:]*)\]\z/
 75:       @ipaddr = IPAddr.new($1)
 76:       @hostname = @ipaddr.to_s
 77:       @uri_host = "[#{@hostname}]"
 78:       @domain = @tld = nil
 79:       return
 80:     end
 81:     @ipaddr = nil
 82:     @hostname = DomainName.normalize(hostname)
 83:     @uri_host = @hostname
 84:     if last_dot = @hostname.rindex(DOT)
 85:       @tld = @hostname[(last_dot + 1)..1]
 86:     else
 87:       @tld = @hostname
 88:     end
 89:     etld_data = DomainName.etld_data
 90:     if @canonical_tld_p = etld_data.key?(@tld)
 91:       subdomain = domain = nil
 92:       parent = @hostname
 93:       loop {
 94:         case etld_data[parent]
 95:         when 0
 96:           @domain = domain if domain
 97:           return
 98:         when 1
 99:           @domain = subdomain if subdomain
100:           return
101:         when 1
102:           @domain = parent
103:           return
104:         end
105:         subdomain = domain
106:         domain = parent
107:         pos = @hostname.index(DOT, -domain.length) or break
108:         parent = @hostname[(pos + 1)..1]
109:       }
110:     else
111:       # unknown/local TLD
112:       if last_dot
113:         # fallback - accept cookies down to second level
114:         # cf. http://www.dkim-reputation.org/regdom-libs/
115:         if penultimate_dot = @hostname.rindex(DOT, last_dot - 1)
116:           @domain = @hostname[(penultimate_dot + 1)..1]
117:         else
118:           @domain = @hostname
119:         end
120:       else
121:         # no domain part - must be a local hostname
122:         @domain = @tld
123:       end
124:     end
125:   end
normalize(domain) click to toggle source

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

     # File lib/domain_name.rb, line 227
227:     def normalize(domain)
228:       DomainName::Punycode.encode_hostname(domain.chomp(DOT).to_nfc).downcase
229:     end

Public Instance Methods

<(other) click to toggle source
     # File lib/domain_name.rb, line 163
163:   def <(other)
164:     case self <=> other
165:     when 1
166:       true
167:     when nil
168:       nil
169:     else
170:       false
171:     end
172:   end
<=(other) click to toggle source
     # File lib/domain_name.rb, line 185
185:   def <=(other)
186:     case self <=> other
187:     when 1, 0
188:       true
189:     when nil
190:       nil
191:     else
192:       false
193:     end
194:   end
<=>(other) click to toggle source
     # File lib/domain_name.rb, line 147
147:   def <=>(other)
148:     other = DomainName.new(other) unless DomainName === other
149:     othername = other.hostname
150:     if othername == @hostname
151:       0
152:     elsif @hostname.end_with?(othername) && @hostname[-othername.size - 1, 1] == DOT
153:       # The other is higher
154:       1
155:     elsif othername.end_with?(@hostname) && othername[-@hostname.size - 1, 1] == DOT
156:       # The other is lower
157:       1
158:     else
159:       nil
160:     end
161:   end
==(other) click to toggle source
     # File lib/domain_name.rb, line 142
142:   def ==(other)
143:     other = DomainName.new(other) unless DomainName === other
144:     other.hostname == @hostname
145:   end
>(other) click to toggle source
     # File lib/domain_name.rb, line 174
174:   def >(other)
175:     case self <=> other
176:     when 1
177:       true
178:     when nil
179:       nil
180:     else
181:       false
182:     end
183:   end
>=(other) click to toggle source
     # File lib/domain_name.rb, line 196
196:   def >=(other)
197:     case self <=> other
198:     when 1, 0
199:       true
200:     when nil
201:       nil
202:     else
203:       false
204:     end
205:   end
canonical?() click to toggle source

Returns true if this domain name has a canonical registered domain.

    # File lib/domain_name.rb, line 52
52:   def canonical?
53:     @canonical_tld_p && (@domain ? true : false)
54:   end
canonical_tld?() click to toggle source

Returns true if this domain name has a canonical TLD.

    # File lib/domain_name.rb, line 46
46:   def canonical_tld?
47:     @canonical_tld_p
48:   end
inspect() click to toggle source
     # File lib/domain_name.rb, line 213
213:   def inspect
214:     str = '#<%s:%s' % [self.class.name, @hostname]
215:     if @ipaddr
216:       str << ' (ipaddr)'
217:     else
218:       str << ' domain=' << @domain if @domain
219:       str << ' tld=' << @tld if @tld
220:     end
221:     str << '>'
222:   end
ipaddr?() click to toggle source

Returns true if this is an IP address, such as “192.168.0.1“ and “[::1]”.

    # File lib/domain_name.rb, line 36
36:   def ipaddr?
37:     @ipaddr ? true : false
38:   end
to_s() click to toggle source
     # File lib/domain_name.rb, line 207
207:   def to_s
208:     @hostname
209:   end
Also aliased as: to_str
to_str() click to toggle source
Alias for: to_s

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.