ruby / uri

URI is a module providing classes to handle Uniform Resource Identifiers
https://ruby.github.io/uri/
Other
81 stars 43 forks source link

ruby/uri does not parse MongoDB connection strings #10

Closed UnixGuru closed 3 years ago

UnixGuru commented 4 years ago

I was playing around with the idea of using the URI class to build a MongoDB connection string, so I thought I'd start with seeing if it could parse one. Using the example from https://docs.mongodb.com/manual/reference/connection-string/ (see the ReplicaSet tab), I did this (using ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]):

require "uri"
# This example throws:  URI::InvalidURIError, bad URI(is not URI?): ....
begin
  u = URI("mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl")
rescue Exception => e
   puts "#{e.class}, #{e.message}"
else
 puts u.inspect
end

However, if you drop the :27017 port numbers from all, it will parse. Also, if you limit this to one host with/without a port, it parses just ducky.

jeremyevans commented 3 years ago

This isn't a bug, that's not a valid URI. See https://tools.ietf.org/html/rfc3986#appendix-A (port cannot contain , character).