/
uri_parser.rb
409 lines (358 loc) · 14.7 KB
/
uri_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Copyright (C) 2009-2013 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'cgi'
require 'uri'
module Mongo
class URIParser
AUTH_REGEX = /((.+)@)?/
HOST_REGEX = /([-.\w]+)|(\[[^\]]+\])/
PORT_REGEX = /(?::(\w+))?/
UNIX_SOCK_REGEX = /([\S]+.sock)/
NODE_REGEX = /((#{HOST_REGEX}#{PORT_REGEX},?)+|#{UNIX_SOCK_REGEX}{1})/
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][?options]]"
SPEC_ATTRS = [:nodes, :auths]
READ_PREFERENCES = {
'primary' => :primary,
'primarypreferred' => :primary_preferred,
'secondary' => :secondary,
'secondarypreferred' => :secondary_preferred,
'nearest' => :nearest
}
OPT_ATTRS = [
:authmechanism,
:authmechanismproperties,
:authsource,
:connect,
:connecttimeoutms,
:fsync,
:journal,
:pool_size,
:readpreference,
:readpreferencetags,
:replicaset,
:safe,
:slaveok,
:sockettimeoutms,
:ssl,
:w,
:wtimeout,
:wtimeoutms
]
OPT_VALID = {
:authmechanism => lambda { |arg| Mongo::Authentication.validate_mechanism(arg) },
:authmechanismproperties => lambda { |arg| arg.length > 0 },
:authsource => lambda { |arg| arg.length > 0 },
:connect => lambda { |arg| [ 'direct', 'replicaset', 'true', 'false', true, false ].include?(arg) },
:connecttimeoutms => lambda { |arg| arg =~ /^\d+$/ },
:fsync => lambda { |arg| ['true', 'false'].include?(arg) },
:journal => lambda { |arg| ['true', 'false'].include?(arg) },
:pool_size => lambda { |arg| arg.to_i > 0 },
:readpreference => lambda { |arg| READ_PREFERENCES.keys.include?(arg) },
:readpreferencetags => lambda { |arg| arg.none? { |tags| tags.scan(/(\w+:\w+),?/).empty? } },
:replicaset => lambda { |arg| arg.length > 0 },
:safe => lambda { |arg| ['true', 'false'].include?(arg) },
:slaveok => lambda { |arg| ['true', 'false'].include?(arg) },
:sockettimeoutms => lambda { |arg| arg =~ /^\d+$/ },
:ssl => lambda { |arg| ['true', 'false'].include?(arg) },
:w => lambda { |arg| arg =~ /^\w+$/ },
:wtimeout => lambda { |arg| arg =~ /^\d+$/ },
:wtimeoutms => lambda { |arg| arg =~ /^\d+$/ }
}
OPT_ERR = {
:authmechanism => Mongo::Authentication::MECHANISM_ERROR,
:authmechanismproperties => "must meet the format requirements of the authentication mechanism's properties",
:authsource => "must be a string containing the name of the database being used for authentication",
:connect => "must be 'direct', 'replicaset', 'true', or 'false'",
:connecttimeoutms => "must be an integer specifying milliseconds",
:fsync => "must be 'true' or 'false'",
:journal => "must be 'true' or 'false'",
:pool_size => "must be an integer greater than zero",
:readpreference => "must be one of #{READ_PREFERENCES.keys.map(&:inspect).join(",")}",
:readpreferencetags => "must be a comma-separated list of one or more key:value pairs",
:replicaset => "must be a string containing the name of the replica set to connect to",
:safe => "must be 'true' or 'false'",
:slaveok => "must be 'true' or 'false'",
:settimeoutms => "must be an integer specifying milliseconds",
:ssl => "must be 'true' or 'false'",
:w => "must be an integer indicating number of nodes to replicate to or a string " +
"specifying that replication is required to the majority or nodes with a " +
"particilar getLastErrorMode.",
:wtimeout => "must be an integer specifying milliseconds",
:wtimeoutms => "must be an integer specifying milliseconds"
}
OPT_CONV = {
:authmechanism => lambda { |arg| arg.upcase },
:authmechanismproperties => lambda { |arg| arg },
:authsource => lambda { |arg| arg },
:connect => lambda { |arg| arg == 'false' ? false : arg }, # convert 'false' to FalseClass
:connecttimeoutms => lambda { |arg| arg.to_f / 1000 }, # stored as seconds
:fsync => lambda { |arg| arg == 'true' ? true : false },
:journal => lambda { |arg| arg == 'true' ? true : false },
:pool_size => lambda { |arg| arg.to_i },
:readpreference => lambda { |arg| READ_PREFERENCES[arg] },
:readpreferencetags => lambda { |arg| arg.map do |tags|
tags.scan(/(\w+:\w+),?/).reduce({}) do |tags, pair|
key, value = pair.first.split(":")
tags.merge!(key => value)
end
end },
:replicaset => lambda { |arg| arg },
:safe => lambda { |arg| arg == 'true' ? true : false },
:slaveok => lambda { |arg| arg == 'true' ? true : false },
:sockettimeoutms => lambda { |arg| arg.to_f / 1000 }, # stored as seconds
:ssl => lambda { |arg| arg == 'true' ? true : false },
:w => lambda { |arg| Mongo::Support.is_i?(arg) ? arg.to_i : arg.to_sym },
:wtimeout => lambda { |arg| arg.to_i },
:wtimeoutms => lambda { |arg| arg.to_i }
}
OPT_CASE_SENSITIVE = [ :authsource,
:authmechanismproperties,
:replicaset,
:w
]
OPT_NOT_STRING = [ :readpreferencetags ]
attr_reader :auths,
:authmechanism,
:authmechanismproperties,
:authsource,
:connect,
:connecttimeoutms,
:db_name,
:fsync,
:journal,
:nodes,
:pool_size,
:readpreference,
:readpreferencetags,
:replicaset,
:safe,
:slaveok,
:sockettimeoutms,
:ssl,
:w,
:wtimeout,
:wtimeoutms
# Parse a MongoDB URI. This method is used by MongoClient.from_uri.
# Returns an array of nodes and an array of db authorizations, if applicable.
#
# @note Passwords can contain any character except for ','
#
# @param [String] uri The MongoDB URI string.
def initialize(uri)
if uri.start_with?('mongodb://')
uri = uri[10..-1]
else
raise MongoArgumentError, "MongoDB URI must match this spec: #{MONGODB_URI_SPEC}"
end
hosts, opts = uri.split('?')
parse_options(opts)
parse_hosts(hosts)
validate_connect
end
# Create a Mongo::MongoClient or a Mongo::MongoReplicaSetClient based on the URI.
#
# @note Don't confuse this with attribute getter method #connect.
#
# @return [MongoClient,MongoReplicaSetClient]
def connection(extra_opts={}, legacy = false, sharded = false)
opts = connection_options.merge!(extra_opts)
if(legacy)
if replicaset?
ReplSetConnection.new(node_strings, opts)
else
Connection.new(host, port, opts)
end
else
if sharded
MongoShardedClient.new(node_strings, opts)
elsif replicaset?
MongoReplicaSetClient.new(node_strings, opts)
else
MongoClient.new(host, port, opts)
end
end
end
# Whether this represents a replica set.
# @return [true,false]
def replicaset?
replicaset.is_a?(String) || nodes.length > 1
end
# Whether to immediately connect to the MongoDB node[s]. Defaults to true.
# @return [true, false]
def connect?
connect != false
end
# Whether this represents a direct connection.
#
# @note Specifying :connect => 'direct' has no effect... other than to raise an exception if other variables suggest a replicaset.
#
# @return [true,false]
def direct?
!replicaset?
end
# For direct connections, the host of the (only) node.
# @return [String]
def host
nodes[0][0]
end
# For direct connections, the port of the (only) node.
# @return [Integer]
def port
nodes[0][1].to_i
end
# Options that can be passed to MongoClient.new or MongoReplicaSetClient.new
# @return [Hash]
def connection_options
opts = {}
if @wtimeout
warn "Using wtimeout in a URI is deprecated, please use wtimeoutMS. It will be removed in v2.0."
opts[:wtimeout] = @wtimeout
end
opts[:wtimeout] = @wtimeoutms if @wtimeoutms
opts[:w] = 1 if @safe
opts[:w] = @w if @w
opts[:j] = @journal if @journal
opts[:fsync] = @fsync if @fsync
opts[:connect_timeout] = @connecttimeoutms if @connecttimeoutms
opts[:op_timeout] = @sockettimeoutms if @sockettimeoutms
opts[:pool_size] = @pool_size if @pool_size
opts[:read] = @readpreference if @readpreference
opts[:tag_sets] = @readpreferencetags if @readpreferencetags
if @slaveok && !@readpreference
unless replicaset?
opts[:slave_ok] = true
else
opts[:read] = :secondary_preferred
end
end
if replicaset.is_a?(String)
opts[:name] = replicaset
end
opts[:db_name] = @db_name if @db_name
opts[:auths] = @auths if @auths
opts[:ssl] = @ssl if @ssl
opts[:connect] = connect?
opts
end
def node_strings
nodes.map { |node| node.join(':') }
end
private
def parse_hosts(uri_without_protocol)
@nodes = []
@auths = Set.new
unless matches = MONGODB_URI_MATCHER.match(uri_without_protocol)
raise MongoArgumentError,
"MongoDB URI must match this spec: #{MONGODB_URI_SPEC}"
end
user_info = matches[2].split(':') if matches[2]
host_info = matches[3].split(',')
@db_name = matches[9]
if host_info.first.end_with?('.sock')
@nodes << [ host_info.first ]
else
host_info.each do |host|
if host[0,1] == '['
host, port = host.split(']:') << MongoClient::DEFAULT_PORT
host = host.end_with?(']') ? host[1...-1] : host[1..-1]
else
host, port = host.split(':') << MongoClient::DEFAULT_PORT
end
unless port.to_s =~ /^\d+$/
raise MongoArgumentError,
"Invalid port #{port}; port must be specified as digits."
end
@nodes << [host, port.to_i]
end
end
if @nodes.empty?
raise MongoArgumentError,
"No nodes specified. Please ensure that you've provided at " +
"least one node."
end
# no user info to parse, exit here
return unless user_info
# check for url encoding for username and password
username, password = user_info
if user_info.size > 2 ||
(username && username.include?('@')) ||
(password && password.include?('@'))
raise MongoArgumentError,
"The characters ':' and '@' in a username or password " +
"must be escaped (RFC 2396)."
end
# if username exists, proceed adding to auth set
unless username.nil? || username.empty?
auth = Authentication.validate_credentials({
:db_name => @db_name,
:username => URI.unescape(username),
:password => password ? URI.unescape(password) : nil,
:source => @authsource,
:mechanism => @authmechanism,
:extra => @authmechanismproperties || {}
})
@auths << auth
end
end
# This method uses the lambdas defined in OPT_VALID and OPT_CONV to validate
# and convert the given options.
def parse_options(string_opts)
# initialize instance variables for available options
OPT_VALID.keys.each { |k| instance_variable_set("@#{k}", nil) }
string_opts ||= ''
return if string_opts.empty?
if string_opts.include?(';') and string_opts.include?('&')
raise MongoArgumentError, 'must not mix URL separators ; and &'
end
opts = CGI.parse(string_opts).inject({}) do |memo, (key, value)|
key_sym = key.downcase.to_sym
if OPT_NOT_STRING.include?(key_sym)
memo[key_sym] = value
else
value = value.first
memo[key_sym] = OPT_CASE_SENSITIVE.include?(key_sym) ? value.strip : value.strip.downcase
end
memo
end
opts.each do |key, value|
if !OPT_ATTRS.include?(key)
raise MongoArgumentError, "Invalid Mongo URI option #{key}"
end
if OPT_VALID[key].call(value)
instance_variable_set("@#{key}", OPT_CONV[key].call(value))
else
raise MongoArgumentError, "Invalid value #{value.inspect} for #{key}: #{OPT_ERR[key]}"
end
end
if @authmechanismproperties
@authmechanismproperties = @authmechanismproperties.split(',').reduce({}) do |prop, pair|
key, value = pair.split(':')
if ["true", "false"].include?(value.downcase)
value = value.downcase == "true"
end
prop[key.downcase.to_sym] = value
prop
end
end
end
def validate_connect
if replicaset? and @connect == 'direct'
# Make sure the user doesn't specify something contradictory
raise MongoArgumentError, "connect=direct conflicts with setting a replicaset name"
end
end
end
end