Class: Billy::ProxyHandler

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

Instance Method Summary collapse

Methods included from Handler

#reset

Instance Method Details

#handle_request(method, url, headers, body) ⇒ Object



13
14
15
16
17
18
19
20
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
# File 'lib/billy/handlers/proxy_handler.rb', line 13

def handle_request(method, url, headers, body)
  if handles_request?(method, url, headers, body)
    req = EventMachine::HttpRequest.new(url, {
        :inactivity_timeout => Billy.config.proxied_request_inactivity_timeout,
        :connect_timeout => Billy.config.proxied_request_connect_timeout})

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

    if req.error
      return { :error => "Request to #{url} failed with error: #{req.error}" }
    end

    if req.response
      response = process_response(req)

      unless allowed_response_code?(response[:status])
        if Billy.config.non_successful_error_level == :error
          return { :error => "Request failed due to response status #{response[:status]} for '#{url}' which was not allowed." }
        else
          Billy.log(:warn, "puffing-billy: Received response status code #{response[:status]} for '#{url}'")
        end
      end

      if cacheable?(url, response[:headers], response[:status])
        Billy::Cache.instance.store(method.downcase, url, headers, body, response[:headers], response[:status], response[:content])
      end

      Billy.log(:info, "puffing-billy: PROXY #{method} succeeded for '#{url}'")
      return response
    end
  end
  nil
end

#handles_request?(method, url, headers, body) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/billy/handlers/proxy_handler.rb', line 9

def handles_request?(method, url, headers, body)
  !disabled_request?(url)
end