Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Blocking HTTP.new.start and dynamic HTTP(S)

Tags:

ruby

net-http

I need an application to block an HTTP request so I had to add a couple of lines of code, the only piece I couldn't figure out was the statement if uri.scheme == 'https'; http.use_ssl = true is there a way I can set http/https in the current statement:

Net::HTTP.new(uri.host, uri.port).start do |http|

  # Causes and IOError... 
  if uri.scheme == 'https'
    http.use_ssl = true
  end

  request = Net::HTTP::Get.new(uri.request_uri)
  http.request(request)
end

Added: IOError: use_ssl value changed, but session already started

like image 200
Jordon Bedwell Avatar asked Oct 17 '11 20:10

Jordon Bedwell


1 Answers

Per the documentation, you cannot call use_ssl= after starting the session (i.e. after start). You have to set it before, e.g.:

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

http.start do |h|
  h.request Net::HTTP::Get.new(uri.request_uri)
end
like image 104
Jordan Running Avatar answered Sep 18 '22 13:09

Jordan Running