Class: Explorer::Proxy
- Inherits:
-
Object
- Object
- Explorer::Proxy
- Defined in:
- lib/explorer/proxy.rb
Instance Method Summary collapse
- #handle(reel_request) ⇒ Object
-
#initialize(host, port) ⇒ Proxy
constructor
A new instance of Proxy.
Constructor Details
#initialize(host, port) ⇒ Proxy
Returns a new instance of Proxy.
5 6 7 8 |
# File 'lib/explorer/proxy.rb', line 5 def initialize(host, port) @host = host @port = port end |
Instance Method Details
#handle(reel_request) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/explorer/proxy.rb', line 10 def handle(reel_request) Net::HTTP.new(@host, @port).start do |http| http.request(net_request(reel_request)) do |net_response| # Map the headers to be friendly to reel (no arrays in values) headers = Hash[net_response.to_hash.map{|k,v| [k,v.join(", ")]}] # Delete the connection header as otherwise there will be 2 headers.delete 'connection' if headers['transfer-encoding'] == 'chunked' reel_request.respond net_response.code.to_i, headers net_response.read_body do |chunk| reel_request << chunk end reel_request.finish_response else reel_request.respond net_response.code.to_i, headers, net_response.read_body end end end rescue Errno::ECONNREFUSED reel_request.respond 504, 'Could not connect to upstream server' end |