Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby OpenURI FTP not working when username has spaces

Tags:

ruby

ftp

open-uri

I am using Ruby 1.9.3 and am running the following command:

open("ftp://user name:[email protected]/1234/1234.txt.gz")

which returns:

URI::InvalidURIError: bad URI(is not URI?)

Encoding the user name (replacing spaces with %20) does not work either:

Net::FTPPermError: 530 Invalid userid/password

The URI works fine in all browsers and FTP clients tested - just not when using OpenURI. Also, using Net::FTP (which is wrapped by OpenURI) works fine as well:

require 'net/ftp'
ftp = Net::FTP.new
ftp.connect("datafeeds.domain.com", 21)
ftp.login("user name", "password")
ftp.getbinaryfile("/1234/1234.txt.gz")

Any idea why the OpenURI method does not work, while the Net::FTP method does? Thanks.

like image 569
modulaaron Avatar asked Oct 04 '22 16:10

modulaaron


2 Answers

By definition in the specification, URL user names only allow these characters:

user                   alphanum2 [ user ]
[...]
alphanum2              alpha | digit | - | _ | . | +  

Browsers are notorious for ignoring the specifications so saying they support it isn't a good proof. They shouldn't per the spec.

If cURL supports them, then use the Curb gem and see if that'll let you use them.

like image 89
the Tin Man Avatar answered Oct 12 '22 10:10

the Tin Man


According to this StackOverflow answer, you should be able to just escape the special characters in your username and password. You could do something like:

login = URI.escape('user name') + ':' + URI.escape('password')
open("ftp://#{login}@datafeeds.domain.com/1234/1234.txt.gz")
like image 36
Jacob Brown Avatar answered Oct 12 '22 12:10

Jacob Brown