Class: AgileProxy::ProxyHandler

Inherits:
Object
  • Object
show all
Includes:
Handler
Defined in:
lib/agile_proxy/handlers/proxy_handler.rb

Overview

The handler used for proxying the request on to the original server

This handler is responsible for proxying the request through to the original server and passing back its response

It works as a rack end point as usual :-)

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Array

The endpoint called by ‘rack’

Requests the response back from the destination server and passes it back to the client

Parameters:

  • env (Hash)

    The rack environment hash

Returns:

  • (Array)

    The rack response array (status, headers, body)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/agile_proxy/handlers/proxy_handler.rb', line 21

def call(env)
  request = ActionDispatch::Request.new(env)
  method = request.request_method.downcase
  body = request.body.read
  request.body.rewind
  url = request.url
  if handles_request?(request)
    req = EventMachine::HttpRequest.new(request.url,
                                        inactivity_timeout: AgileProxy.config.proxied_request_inactivity_timeout,
                                        connect_timeout: AgileProxy.config.proxied_request_connect_timeout
    )

    req = req.send(method.downcase, build_request_options(request.headers, body))

    if req.error
      return [500, {}, ["Request to #{request.url} failed with error: #{req.error}"]]
    end

    if req.response
      response = process_response(req)

      unless allowed_response_code?(response[:status])
        AgileProxy.log(:warn, "agile-proxy: Received response status code #{response[:status]} for '#{url}'")
      end

      AgileProxy.log(:info, "agile-proxy: PROXY #{request.request_method} succeeded for '#{request.url}'")
      return [response[:status], response[:headers], response[:content]]
    end
  end
  [404, {}, ['Not proxied']]
end