Ruby: Checking if URI is HTTPS?

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

url = URI.parse("")

# [some code]

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

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?


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

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

as shown in the previous answer, HTTP and HTTPS are different classes. in particular, HTTPS is a subclass of the HTTP class. thus you could check with instance_of?.

http  = URI.parse ""
https = URI.parse ""

http.instance_of?  URI::HTTPS  #=> false
https.instance_of? URI::HTTPS  #=> true

but if this hierarchy ever gets changed, then your code could break, thus the above answer might be more future-proof.

Need Your Help

Request Parameter Losing Plus Sign

java javascript html url escaping

I am editing a search form and trying to protect against special characters in the database. In the JSP search form, a (multiselect) dropdown allows users to select descriptions that will be used i...

WCF REST Webinvoke not found

.net wcf rest .net-4.0 webinvoke

Following is the code i am using in my test app: