3 class HTTPClient < Faraday::Adapter
4 dependency 'httpclient'
7 @client ||= ::HTTPClient.new
13 if req = env[:request]
14 if proxy = req[:proxy]
22 configure_timeouts req
25 if env[:url].scheme == 'https' && ssl = env[:ssl]
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
33 resp = client.request env[:method], env[:url],
35 :header => env[:request_headers]
37 save_response env, resp.status, resp.body, resp.headers
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 "}
46 raise Faraday::Error::ClientError, $!
48 rescue Errno::ECONNREFUSED, EOFError
49 raise Faraday::Error::ConnectionFailed, $!
51 if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
52 raise Faraday::SSLError, err
58 def configure_socket(bind)
59 client.socket_local.host = bind[:host]
60 client.socket_local.port = bind[:port]
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]
70 def configure_ssl(ssl)
71 ssl_config = client.ssl_config
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)
82 def configure_timeouts(req)
84 client.connect_timeout = req[:timeout]
85 client.receive_timeout = req[:timeout]
86 client.send_timeout = req[:timeout]
90 client.connect_timeout = req[:open_timeout]
91 client.send_timeout = req[:open_timeout]
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
100 OpenSSL::SSL::VERIFY_NONE