]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/aviator/lib/puppet/feature/faraday/adapter/httpclient.rb
add aimonb/aviator to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / aviator / lib / puppet / feature / faraday / adapter / httpclient.rb
1 module Faraday
2   class Adapter
3     class HTTPClient < Faraday::Adapter
4       dependency 'httpclient'
5
6       def client
7         @client ||= ::HTTPClient.new
8       end
9
10       def call(env)
11         super
12
13         if req = env[:request]
14           if proxy = req[:proxy]
15             configure_proxy proxy
16           end
17
18           if bind = req[:bind]
19             configure_socket bind
20           end
21
22           configure_timeouts req
23         end
24
25         if env[:url].scheme == 'https' && ssl = env[:ssl]
26           configure_ssl ssl
27         end
28
29         # TODO Don't stream yet.
30         # https://github.com/nahi/httpclient/pull/90
31         env[:body] = env[:body].read if env[:body].respond_to? :read
32
33         resp = client.request env[:method], env[:url],
34           :body   => env[:body],
35           :header => env[:request_headers]
36
37         save_response env, resp.status, resp.body, resp.headers
38
39         @app.call env
40       rescue ::HTTPClient::TimeoutError
41         raise Faraday::Error::TimeoutError, $!
42       rescue ::HTTPClient::BadResponseError => err
43         if err.message.include?('status 407')
44           raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
45         else
46           raise Faraday::Error::ClientError, $!
47         end
48       rescue Errno::ECONNREFUSED, EOFError
49         raise Faraday::Error::ConnectionFailed, $!
50       rescue => err
51         if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
52           raise Faraday::SSLError, err
53         else
54           raise
55         end
56       end
57
58       def configure_socket(bind)
59         client.socket_local.host = bind[:host]
60         client.socket_local.port = bind[:port]
61       end
62
63       def configure_proxy(proxy)
64         client.proxy = proxy[:uri]
65         if proxy[:user] && proxy[:password]
66           client.set_proxy_auth proxy[:user], proxy[:password]
67         end
68       end
69
70       def configure_ssl(ssl)
71         ssl_config = client.ssl_config
72
73         ssl_config.add_trust_ca ssl[:ca_file]        if ssl[:ca_file]
74         ssl_config.add_trust_ca ssl[:ca_path]        if ssl[:ca_path]
75         ssl_config.cert_store   = ssl[:cert_store]   if ssl[:cert_store]
76         ssl_config.client_cert  = ssl[:client_cert]  if ssl[:client_cert]
77         ssl_config.client_key   = ssl[:client_key]   if ssl[:client_key]
78         ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
79         ssl_config.verify_mode  = ssl_verify_mode(ssl)
80       end
81
82       def configure_timeouts(req)
83         if req[:timeout]
84           client.connect_timeout   = req[:timeout]
85           client.receive_timeout   = req[:timeout]
86           client.send_timeout      = req[:timeout]
87         end
88
89         if req[:open_timeout]
90           client.connect_timeout   = req[:open_timeout]
91           client.send_timeout      = req[:open_timeout]
92         end
93       end
94
95       def ssl_verify_mode(ssl)
96         ssl[:verify_mode] || begin
97           if ssl.fetch(:verify, true)
98             OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
99           else
100             OpenSSL::SSL::VERIFY_NONE
101           end
102         end
103       end
104     end
105   end
106 end