forked from lostisland/faraday
-
Notifications
You must be signed in to change notification settings - Fork 5
/
typhoeus.rb
64 lines (51 loc) · 2.11 KB
/
typhoeus.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Faraday
class Adapter
class Typhoeus < Faraday::Adapter
self.supports_parallel_requests = true
def self.setup_parallel_manager(options = {})
options.empty? ? ::Typhoeus::Hydra.hydra : ::Typhoeus::Hydra.new(options)
end
dependency 'typhoeus'
def call(env)
super
# TODO: support streaming requests
env[:body] = env[:body].read if env[:body].respond_to? :read
req = ::Typhoeus::Request.new env[:url].to_s,
:method => env[:method],
:body => env[:body],
:headers => env[:request_headers],
:disable_ssl_peer_verification => (env[:ssl] && !env[:ssl].fetch(:verify, true))
if ssl = env[:ssl]
req.ssl_cert = ssl[:client_cert_file] if ssl[:client_cert_file]
req.ssl_key = ssl[:client_key_file] if ssl[:client_key_file]
req.ssl_cacert = ssl[:ca_file] if ssl[:ca_file]
req.ssl_capath = ssl[:ca_path] if ssl[:ca_path]
end
env_req = env[:request]
if proxy = env_req[:proxy]
req.proxy = "#{proxy[:uri].host}:#{proxy[:uri].port}"
if proxy[:username] && proxy[:password]
req.proxy_username = proxy[:username]
req.proxy_password = proxy[:password]
end
end
req.timeout = req.connect_timeout = (env_req[:timeout] * 1000) if env_req[:timeout]
req.connect_timeout = (env_req[:open_timeout] * 1000) if env_req[:open_timeout]
is_parallel = !!env[:parallel_manager]
req.on_complete do |resp|
save_response(env, resp.code, resp.body) do |response_headers|
response_headers.parse resp.headers
end
# in async mode, :response is initialized at this point
env[:response].finish(env) if is_parallel
end
hydra = env[:parallel_manager] || self.class.setup_parallel_manager
hydra.queue req
hydra.run unless is_parallel
@app.call env
rescue Errno::ECONNREFUSED
raise Error::ConnectionFailed, $!
end
end
end
end