forked from lostisland/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
excon.rb
90 lines (77 loc) · 2.91 KB
/
excon.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true
module Faraday
class Adapter
# Excon adapter.
class Excon < Faraday::Adapter
dependency 'excon'
def call(env)
super
opts = {}
if env[:url].scheme == 'https' && (ssl = env[:ssl])
opts[:ssl_verify_peer] = !ssl[:verify]
opts[:ssl_ca_path] = ssl[:ca_path] if ssl[:ca_path]
opts[:ssl_ca_file] = ssl[:ca_file] if ssl[:ca_file]
opts[:client_cert] = ssl[:client_cert] if ssl[:client_cert]
opts[:client_key] = ssl[:client_key] if ssl[:client_key]
opts[:certificate] = ssl[:certificate] if ssl[:certificate]
opts[:private_key] = ssl[:private_key] if ssl[:private_key]
opts[:ssl_version] = ssl[:version] if ssl[:version]
opts[:ssl_min_version] = ssl[:min_version] if ssl[:min_version]
opts[:ssl_max_version] = ssl[:max_version] if ssl[:max_version]
# https://github.com/geemus/excon/issues/106
# https://github.com/jruby/jruby-ossl/issues/19
opts[:nonblock] = false
end
if (req = env[:request])
if req[:timeout]
opts[:read_timeout] = req[:timeout]
opts[:connect_timeout] = req[:timeout]
opts[:write_timeout] = req[:timeout]
end
if req[:open_timeout]
opts[:connect_timeout] = req[:open_timeout]
end
if req[:proxy]
opts[:proxy] = {
host: req[:proxy][:uri].host,
hostname: req[:proxy][:uri].hostname,
port: req[:proxy][:uri].port,
scheme: req[:proxy][:uri].scheme,
user: req[:proxy][:user],
password: req[:proxy][:password]
}
end
end
conn = create_connection(env, opts)
resp = conn.request \
method: env[:method].to_s.upcase,
headers: env[:request_headers],
body: read_body(env)
if req.stream_response?
warn "Streaming downloads for #{self.class.name} are not yet implemented."
req.on_data.call(resp.body, resp.body.bytesize)
end
save_response(env, resp.status.to_i, resp.body, resp.headers, resp.reason_phrase)
@app.call env
rescue ::Excon::Errors::SocketError => err
if err.message =~ /\btimeout\b/
raise Faraday::TimeoutError, err
elsif err.message =~ /\bcertificate\b/
raise Faraday::SSLError, err
else
raise Faraday::ConnectionFailed, err
end
rescue ::Excon::Errors::Timeout => err
raise Faraday::TimeoutError, err
end
# @return [Excon]
def create_connection(env, opts)
::Excon.new(env[:url].to_s, opts.merge(@connection_options))
end
# TODO: support streaming requests
def read_body(env)
env[:body].respond_to?(:read) ? env[:body].read : env[:body]
end
end
end
end