Module: Mongo::Address::Validator Private

Included in:
Srv::Result, URI
Defined in:
build/ruby-driver-master/lib/mongo/address/validator.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

Instance Method Summary collapse

Instance Method Details

#validate_address_str!(address_str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Takes an address string in ipv4/ipv6/hostname/socket path format and validates its format.

Since:

  • 2.0.0



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
# File 'build/ruby-driver-master/lib/mongo/address/validator.rb', line 27

def validate_address_str!(address_str)
  case address_str
  when /\A\[[\d:]+\](?::(\d+))?\z/
    # ipv6 with optional port
    if port_str = $1
      validate_port_str!(port_str)
    end
  when /\A\//, /\.sock\z/
    # Unix socket path.
    # Spec requires us to validate that the path has no unescaped
    # slashes, but if this were to be the case, parsing would have
    # already failed elsewhere because the URI would've been split in
    # a weird place.
    # The spec also allows relative socket paths and requires that
    # socket paths end in ".sock". We accept all paths but special case
    # the .sock extension to avoid relative paths falling into the
    # host:port case below.
  when /[\/\[\]]/
    # Not a host:port nor an ipv4 address with optional port.
    # Possibly botched ipv6 address with e.g. port delimiter present and
    # port missing, or extra junk before or after.
    raise Error::InvalidAddress,
      "Invalid hostname: #{address_str}"
  when /:.*:/m
    raise Error::InvalidAddress,
      "Multiple port delimiters are not allowed: #{address_str}"
  else
    # host:port or ipv4 address with optional port number
    host, port = address_str.split(':')
    if host.empty?
      raise Error::InvalidAddress, "Host is empty: #{address_str}"
    end

    validate_hostname!(host)

    if port && port.empty?
      raise Error::InvalidAddress, "Port is empty: #{address_str}"
    end

    validate_port_str!(port)
  end
end