Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / is_domain_name.rb
1 #
2 # is_domain_name.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:is_domain_name, :type => :rvalue, :doc => <<-DOC
6     @summary
7       **Deprecated:** Returns true if the string passed to this function is
8       a syntactically correct domain name.
9
10     @return [Boolean]
11       Returns `true` or `false`
12
13     > **Note:* **Deprecated** Will be removed in a future version of stdlib. See
14     [`validate_legacy`](#validate_legacy).
15     DOC
16              ) do |arguments|
17
18     if arguments.size != 1
19       raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments given #{arguments.size} for 1")
20     end
21
22     # Only allow string types
23     return false unless arguments[0].is_a?(String)
24
25     domain = arguments[0].dup
26
27     # Limits (rfc1035, 3.1)
28     domain_max_length = 255
29     label_min_length = 1
30     label_max_length = 63
31
32     # Allow ".", it is the top level domain
33     return true if domain == '.'
34
35     # Remove the final dot, if present.
36     domain.chomp!('.')
37
38     # Check the whole domain
39     return false if domain.empty?
40     return false if domain.length > domain_max_length
41
42     # The top level domain must be alphabetic if there are multiple labels.
43     # See rfc1123, 2.1
44     return false if domain.include?('.') && !%r{\.[A-Za-z]+$}.match(domain)
45
46     # Check each label in the domain
47     labels = domain.split('.')
48     vlabels = labels.each do |label|
49       break if label.length < label_min_length
50       break if label.length > label_max_length
51       break if label[-1..-1] == '-'
52       break if label[0..0] == '-'
53       break unless %r{^[a-z\d-]+$}i =~ label
54     end
55     return vlabels == labels
56   end
57 end
58
59 # vim: set ts=2 sw=2 et :