Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Checking if URI is HTTPS?

Tags:

uri

ruby

https

I would like to check if the URI will need SSL authentication:

url = URI.parse("http://www.google.com")

# [some code]

if url.instance_of? URI::HTTPS
   http.use_ssl=true
   http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

However, those few lines throw the following error..

/usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?): HTTPS (URI::InvalidURIError)
    from /usr/lib/ruby/1.8/uri/common.rb:485:in `parse'
    from /usr/lib/ruby/1.8/uri/common.rb:608:in `URI'
    from links.rb:18

Why is it happening?

like image 654
Marco Avatar asked Feb 06 '10 10:02

Marco


1 Answers

>> uri = URI.parse("http://www.google.com")
=> #<URI::HTTP:0x1014ca458 URL:http://www.google.com>
>> uri.scheme
=> "http"
>> uri = URI.parse("https://mail.google.com")
=> #<URI::HTTPS:0x1014c2e60 URL:https://mail.google.com>
>> uri.scheme
=> "https"

So you could check uri's scheme against simple "https" string.

like image 87
Eimantas Avatar answered Oct 11 '22 23:10

Eimantas