Skip to content

Commit c0a4ae6

Browse files
committed
Merge pull request #103 from seamusabshere/ruby_18_url_decoding
Since `URI.decode_www_form` is Ruby 1.9-only, use `CGI.parse` instead
2 parents 4195ce3 + d88e6d7 commit c0a4ae6

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib/mongo/util/uri_parser.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# limitations under the License.
1717
# ++
1818

19+
require 'cgi'
20+
1921
module Mongo
2022
class URIParser
2123

@@ -260,8 +262,12 @@ def parse_options(string_opts, extra_opts={})
260262

261263
return if string_opts.empty? && extra_opts.empty?
262264

263-
opts = string_opts.split(/&|;/).inject({}) do |memo, kv|
264-
key, value = kv.split('=')
265+
if string_opts.include?(';') and string_opts.include?('&')
266+
raise MongoArgumentError, "must not mix URL separators ; and &"
267+
end
268+
269+
opts = CGI.parse(string_opts).inject({}) do |memo, (key, value)|
270+
value = value.first
265271
memo[key.downcase.to_sym] = value.strip.downcase
266272
memo
267273
end

test/uri_test.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,19 @@ def test_opts_with_amp_separator
7979
assert parser.safe
8080
end
8181

82-
#def test_opts_made_invalid_by_mixed_separators
83-
# assert_raise_error ArgumentError, "invalid data of application/x-www-form-urlencoded (replicaset=foo;bar&slaveok=true&safe=true)" do
84-
# Mongo::URIParser.new('mongodb://localhost:27018?replicaset=foo;bar&slaveok=true&safe=true')
85-
# end
86-
#end
82+
def test_opts_with_uri_encoded_stuff
83+
parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=%64%69%72%65%63%74&slaveok=%74%72%75%65&safe=true')
84+
assert_equal 'direct', parser.connect
85+
assert parser.direct?
86+
assert parser.slaveok
87+
assert parser.safe
88+
end
89+
90+
def test_opts_made_invalid_by_mixed_separators
91+
assert_raise_error MongoArgumentError, "must not mix URL separators ; and &" do
92+
Mongo::URIParser.new('mongodb://localhost:27018?replicaset=foo;bar&slaveok=true&safe=true')
93+
end
94+
end
8795

8896
def test_opts_safe
8997
parser = Mongo::URIParser.new('mongodb://localhost:27018?safe=true;w=2;journal=true;fsync=true;wtimeoutMS=200')

0 commit comments

Comments
 (0)