Class: Goliath::Rack::ReverseProxy

Inherits:
Object
  • Object
show all
Includes:
AsyncMiddleware
Defined in:
lib/goliath/rack/reverse_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ ReverseProxy

Returns a new instance of ReverseProxy.



8
9
10
11
# File 'lib/goliath/rack/reverse_proxy.rb', line 8

def initialize(app, options)
  @app = app
  @connection = EM::HttpRequest.new(options[:base_url])
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
# File 'lib/goliath/rack/reverse_proxy.rb', line 13

def call(env)
  connection = @connection.dup
  super(env, connection)
end

#post_process(env, status, headers, body, connection) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/goliath/rack/reverse_proxy.rb', line 18

def post_process(env, status, headers, body, connection)
  method = env['REQUEST_METHOD'].downcase.to_sym

  options = {:head => request_headers(env, headers), :path => env['REQUEST_URI']}
  options[:body] = env['params'] if [:put, :post, :patch].include? method

  http = connection.send(method, options)

  [http.response_header.status, http.response_header.raw, [http.response]]
end

#request_headers(env, headers) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/goliath/rack/reverse_proxy.rb', line 29

def request_headers(env, headers)
  env.each do |key, value|
    headers[$1.gsub('_', '-').downcase] = value if key =~ /HTTP_(.*)/
  end
  headers['X-Forwarded-Host'] = env['HTTP_HOST']
  headers['X-Forwarded-User'] = env['REMOTE_USER'] if env['REMOTE_USER']
  headers
end