diff --git a/lib/mongo/util/uri_parser.rb b/lib/mongo/util/uri_parser.rb index b2af56f..51a1602 100644 --- a/lib/mongo/util/uri_parser.rb +++ b/lib/mongo/util/uri_parser.rb @@ -20,8 +20,20 @@ module Mongo class URIParser DEFAULT_PORT = 27017 - MONGODB_URI_MATCHER = /(([-.\w:]+):([^@,]+)@)?((?:(?:[-.\w]+)(?::(?:[\w]+))?,?)+)(\/([-\w]+))?/ + + USER_REGEX = /(?[-.\w:]+)/ + PASS_REGEX = /(?[^@,]+)/ + AUTH_REGEX = /(?#{USER_REGEX}:#{PASS_REGEX}@)?/ + + HOST_REGEX = /(?[-.\w]+)/ + PORT_REGEX = /(?::(?\w+))?/ + NODE_REGEX = /(?(#{HOST_REGEX}#{PORT_REGEX},?)+)/ + + PATH_REGEX = /(?:\/(?[-\w]+))?/ + + MONGODB_URI_MATCHER = /#{AUTH_REGEX}#{NODE_REGEX}#{PATH_REGEX}/ MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]" + SPEC_ATTRS = [:nodes, :auths] OPT_ATTRS = [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync] @@ -118,10 +130,10 @@ module Mongo raise MongoArgumentError, "MongoDB URI must match this spec: #{MONGODB_URI_SPEC}" end - uname = matches[2] - pwd = matches[3] - hosturis = matches[4].split(',') - db = matches[6] + uname = matches['username'] + pwd = matches['password'] + hosturis = matches['nodes'].split(',') + db = matches['path'] hosturis.each do |hosturi| # If port is present, use it, otherwise use default port