Class: Rodent::Goliath::Middleware

Inherits:
Object
  • Object
show all
Includes:
Goliath::Rack::AsyncMiddleware
Defined in:
lib/rodent/goliath/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, header_proxy_name = 'Rodent-Proxy') ⇒ Middleware

Returns a new instance of Middleware


12
13
14
15
# File 'lib/rodent/goliath/middleware.rb', line 12

def initialize(app, header_proxy_name = 'Rodent-Proxy')
  @header_proxy_name = header_proxy_name
  super(app)
end

Instance Method Details

#hook_into_callback_chain(env, *args) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rodent/goliath/middleware.rb', line 17

def hook_into_callback_chain(env, *args)
  async_callback = env['async.callback']

  downstream_callback = proc do |status, headers, response|
    result_response = [status, headers, response]
    if proxy_type = headers.delete(@header_proxy_name)
      body = response.respond_to?(:body) ? response.body.join : response
      result_response = safely(env) { proxy_request(env, proxy_type, body, headers) }
    end
    async_callback.call(result_response) unless result_response == ::Goliath::Connection::AsyncResponse
  end

  env['async.callback'] = downstream_callback
end