]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/aviator/feature/faraday/adapter/excon.rb
add aimonb/aviator to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / aviator / feature / faraday / adapter / excon.rb
1 module Faraday
2   class Adapter
3     class Excon < Faraday::Adapter
4       dependency 'excon'
5
6       def initialize(app, connection_options = {})
7         @connection_options = connection_options
8         super(app)
9       end
10
11       def call(env)
12         super
13
14         opts = {}
15         if env[:url].scheme == 'https' && ssl = env[:ssl]
16           opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
17           opts[:ssl_ca_path] = ssl[:ca_path] if ssl[:ca_path]
18           opts[:ssl_ca_file] = ssl[:ca_file] if ssl[:ca_file]
19           opts[:client_cert] = ssl[:client_cert] if ssl[:client_cert]
20           opts[:client_key]  = ssl[:client_key]  if ssl[:client_key]
21           opts[:certificate] = ssl[:certificate] if ssl[:certificate]
22           opts[:private_key] = ssl[:private_key] if ssl[:private_key]
23
24           # https://github.com/geemus/excon/issues/106
25           # https://github.com/jruby/jruby-ossl/issues/19
26           opts[:nonblock] = false
27         end
28
29         if ( req = env[:request] )
30           if req[:timeout]
31             opts[:read_timeout]      = req[:timeout]
32             opts[:connect_timeout]   = req[:timeout]
33             opts[:write_timeout]     = req[:timeout]
34           end
35
36           if req[:open_timeout]
37             opts[:connect_timeout]   = req[:open_timeout]
38             opts[:write_timeout]     = req[:open_timeout]
39           end
40
41           if req[:proxy]
42             opts[:proxy] = {
43               :host     => req[:proxy][:uri].host,
44               :port     => req[:proxy][:uri].port,
45               :scheme   => req[:proxy][:uri].scheme,
46               :user     => req[:proxy][:user],
47               :password => req[:proxy][:password]
48             }
49           end
50         end
51
52         conn = ::Excon.new(env[:url].to_s, opts.merge(@connection_options))
53
54         resp = conn.request \
55           :method  => env[:method].to_s.upcase,
56           :headers => env[:request_headers],
57           :body    => read_body(env)
58
59         save_response(env, resp.status.to_i, resp.body, resp.headers)
60
61         @app.call env
62       rescue ::Excon::Errors::SocketError => err
63         if err.message =~ /\btimeout\b/
64           raise Error::TimeoutError, err
65         elsif err.message =~ /\bcertificate\b/
66           raise Faraday::SSLError, err
67         else
68           raise Error::ConnectionFailed, err
69         end
70       rescue ::Excon::Errors::Timeout => err
71         raise Error::TimeoutError, err
72       end
73
74       # TODO: support streaming requests
75       def read_body(env)
76         env[:body].respond_to?(:read) ? env[:body].read : env[:body]
77       end
78     end
79   end
80 end