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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/billy/handlers/proxy_handler.rb', line 14
def handle_request(method, url, , body)
if handles_request?(method, url, , body)
opts = { inactivity_timeout: Billy.config.proxied_request_inactivity_timeout,
connect_timeout: Billy.config.proxied_request_connect_timeout }
if url =~ /^https/
opts.merge!({tls: {verify_peer: Billy.config.verify_peer}})
end
if Billy.config.proxied_request_host && !bypass_internal_proxy?(url)
opts.merge!({ proxy: { host: Billy.config.proxied_request_host,
port: Billy.config.proxied_request_port }} )
end
req = EventMachine::HttpRequest.new(url, opts)
req = req.send(method.downcase, build_request_options(url, , 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: "#{method} 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])
cache_scope = Billy::Cache.instance.scope
cache_key = Billy::Cache.instance.key(method.downcase, url, body)
Billy::Cache.instance.store(
cache_key,
cache_scope,
method.downcase,
url,
,
body,
response[:headers],
response[:status],
response[:content]
)
end
Billy.log(:info, "puffing-billy: PROXY #{method} succeeded for '#{url}'")
return response
end
end
nil
end
|