Proxying HTTPS traffic

I’m trying to use Net::HTTP::Proxy to proxy HTTPS traffic and I’ve got
the following code:

proxy = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port)

http = proxy.start(uri.host)
if uri.scheme == ‘https’
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
resp = http.get(path)
end

But this complains about the session already being started:

use_ssl value changed, but session already started

How do I get access to the http object to set these values without
starting the proxy?

To answer my own question, you have to pass the use_ssl as an optional
parameter when starting the proxy. This works:

require ‘net/http’

proxy_uri = URI(‘http://localhost:8080’)
uri = URI(‘https://www.example.org/’)

http = Net::HTTP.new(uri.path)

if uri.scheme == ‘https’
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

proxy = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port)
http = proxy.start(uri.host, :use_ssl => true, :verify_mode =>
OpenSSL::SSL::VERIFY_NONE)

resp = http.get(uri.path)

puts resp.code
puts resp.body

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs