Skip to content

Commit

Permalink
Merge pull request mongodb#83 from TylerBrock/361-uri-parser
Browse files Browse the repository at this point in the history
RUBY-361 Clean up MongoDB URI parser.
  • Loading branch information
banker committed Jan 19, 2012
2 parents ebd31d0 + 6f80a48 commit 881c73a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/mongo/util/uri_parser.rb
Expand Up @@ -20,8 +20,20 @@ module Mongo
class URIParser

DEFAULT_PORT = 27017
MONGODB_URI_MATCHER = /(([-.\w:]+):([^@,]+)@)?((?:(?:[-.\w]+)(?::(?:[\w]+))?,?)+)(\/([-\w]+))?/

USER_REGEX = /(?<username>[-.\w:]+)/
PASS_REGEX = /(?<password>[^@,]+)/
AUTH_REGEX = /(?<auth>#{USER_REGEX}:#{PASS_REGEX}@)?/

HOST_REGEX = /(?<host>[-.\w]+)/
PORT_REGEX = /(?::(?<port>\w+))?/
NODE_REGEX = /(?<nodes>(#{HOST_REGEX}#{PORT_REGEX},?)+)/

PATH_REGEX = /(?:\/(?<path>[-\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]

Expand Down Expand Up @@ -118,10 +130,10 @@ def parse_hosts(uri_without_proto)
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
Expand Down

0 comments on commit 881c73a

Please sign in to comment.