faraday 0.17.6 → 1.0.0.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.md +1 -1
- data/README.md +18 -358
- data/lib/faraday/adapter/em_http.rb +142 -99
- data/lib/faraday/adapter/em_http_ssl_patch.rb +23 -17
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/excon.rb +97 -57
- data/lib/faraday/adapter/httpclient.rb +61 -39
- data/lib/faraday/adapter/net_http.rb +103 -51
- data/lib/faraday/adapter/net_http_persistent.rb +49 -28
- data/lib/faraday/adapter/patron.rb +54 -35
- data/lib/faraday/adapter/rack.rb +28 -12
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter.rb +36 -22
- data/lib/faraday/adapter_registry.rb +28 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +321 -179
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +94 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
- data/lib/faraday/error.rb +21 -79
- data/lib/faraday/logging/formatter.rb +92 -0
- data/lib/faraday/middleware.rb +4 -28
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +28 -0
- data/lib/faraday/options/request_options.rb +21 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +33 -184
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +66 -55
- data/lib/faraday/request/authorization.rb +42 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +72 -49
- data/lib/faraday/request/retry.rb +197 -171
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +41 -23
- data/lib/faraday/request.rb +68 -38
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +36 -18
- data/lib/faraday/response.rb +22 -15
- data/lib/faraday/upload_io.rb +31 -30
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +28 -245
- data/lib/faraday.rb +93 -174
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- metadata +25 -51
- data/CHANGELOG.md +0 -232
- data/Rakefile +0 -13
- data/lib/faraday/deprecate.rb +0 -109
- data/spec/faraday/deprecate_spec.rb +0 -147
- data/spec/faraday/error_spec.rb +0 -102
- data/spec/faraday/response/raise_error_spec.rb +0 -106
- data/spec/spec_helper.rb +0 -105
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
@@ -1,7 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'uri'
|
2
4
|
|
3
5
|
module Faraday
|
4
6
|
class Adapter
|
7
|
+
# EventMachine Synchrony adapter.
|
5
8
|
class EMSynchrony < Faraday::Adapter
|
6
9
|
include EMHttp::Options
|
7
10
|
|
@@ -13,7 +16,8 @@ module Faraday
|
|
13
16
|
|
14
17
|
self.supports_parallel = true
|
15
18
|
|
16
|
-
|
19
|
+
# @return [ParallelManager]
|
20
|
+
def self.setup_parallel_manager(_options = nil)
|
17
21
|
ParallelManager.new
|
18
22
|
end
|
19
23
|
|
@@ -23,73 +27,110 @@ module Faraday
|
|
23
27
|
|
24
28
|
http_method = env[:method].to_s.downcase.to_sym
|
25
29
|
|
26
|
-
# Queue requests for parallel execution.
|
27
30
|
if env[:parallel_manager]
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
# Queue requests for parallel execution.
|
32
|
+
execute_parallel_request(env, request, http_method)
|
33
|
+
else
|
34
|
+
# Execute single request.
|
35
|
+
execute_single_request(env, request, http_method)
|
36
|
+
end
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
+
@app.call env
|
39
|
+
rescue Errno::ECONNREFUSED
|
40
|
+
raise Faraday::ConnectionFailed, $ERROR_INFO
|
41
|
+
rescue EventMachine::Connectify::CONNECTError => e
|
42
|
+
if e.message.include?('Proxy Authentication Required')
|
43
|
+
raise Faraday::ConnectionFailed,
|
44
|
+
%(407 "Proxy Authentication Required")
|
45
|
+
end
|
38
46
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
raise Faraday::ConnectionFailed, e
|
48
|
+
rescue Errno::ETIMEDOUT => e
|
49
|
+
raise Faraday::TimeoutError, e
|
50
|
+
rescue RuntimeError => e
|
51
|
+
if e.message == 'connection closed by server'
|
52
|
+
raise Faraday::ConnectionFailed, e
|
53
|
+
end
|
54
|
+
|
55
|
+
raise Faraday::TimeoutError, e if e.message.include?('timeout error')
|
56
|
+
|
57
|
+
raise
|
58
|
+
rescue StandardError => e
|
59
|
+
if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
|
60
|
+
raise Faraday::SSLError, e
|
61
|
+
end
|
62
|
+
|
63
|
+
raise
|
64
|
+
end
|
65
|
+
|
66
|
+
def create_request(env)
|
67
|
+
EventMachine::HttpRequest.new(
|
68
|
+
Utils::URI(env[:url].to_s),
|
69
|
+
connection_config(env).merge(@connection_options)
|
70
|
+
)
|
71
|
+
end
|
54
72
|
|
55
|
-
|
73
|
+
private
|
56
74
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
75
|
+
def execute_parallel_request(env, request, http_method)
|
76
|
+
env[:parallel_manager].add(request, http_method,
|
77
|
+
request_config(env)) do |resp|
|
78
|
+
if (req = env[:request]).stream_response?
|
79
|
+
warn "Streaming downloads for #{self.class.name} " \
|
80
|
+
'are not yet implemented.'
|
81
|
+
req.on_data.call(resp.response, resp.response.bytesize)
|
82
|
+
end
|
83
|
+
|
84
|
+
save_response(env, resp.response_header.status,
|
85
|
+
resp.response) do |resp_headers|
|
86
|
+
resp.response_header.each do |name, value|
|
61
87
|
resp_headers[name.to_sym] = value
|
62
88
|
end
|
63
89
|
end
|
90
|
+
|
91
|
+
# Finalize the response object with values from `env`.
|
92
|
+
env[:response].finish(env)
|
64
93
|
end
|
94
|
+
end
|
65
95
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
96
|
+
def execute_single_request(env, request, http_method)
|
97
|
+
block = -> { request.send(http_method, request_config(env)) }
|
98
|
+
client = call_block(block)
|
99
|
+
|
100
|
+
raise client.error if client&.error
|
101
|
+
|
102
|
+
if env[:request].stream_response?
|
103
|
+
warn "Streaming downloads for #{self.class.name} " \
|
104
|
+
'are not yet implemented.'
|
105
|
+
env[:request].on_data.call(
|
106
|
+
client.response,
|
107
|
+
client.response.bytesize
|
108
|
+
)
|
74
109
|
end
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
raise
|
110
|
+
status = client.response_header.status
|
111
|
+
reason = client.response_header.http_reason
|
112
|
+
save_response(env, status, client.response, nil, reason) do |headers|
|
113
|
+
client.response_header.each do |name, value|
|
114
|
+
headers[name.to_sym] = value
|
115
|
+
end
|
82
116
|
end
|
83
|
-
|
84
|
-
|
85
|
-
|
117
|
+
end
|
118
|
+
|
119
|
+
def call_block(block)
|
120
|
+
client = nil
|
121
|
+
|
122
|
+
if EM.reactor_running?
|
123
|
+
client = block.call
|
86
124
|
else
|
87
|
-
|
125
|
+
EM.run do
|
126
|
+
Fiber.new do
|
127
|
+
client = block.call
|
128
|
+
EM.stop
|
129
|
+
end.resume
|
130
|
+
end
|
88
131
|
end
|
89
|
-
end
|
90
132
|
|
91
|
-
|
92
|
-
EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env).merge(@connection_options))
|
133
|
+
client
|
93
134
|
end
|
94
135
|
end
|
95
136
|
end
|
@@ -97,10 +138,13 @@ end
|
|
97
138
|
|
98
139
|
require 'faraday/adapter/em_synchrony/parallel_manager'
|
99
140
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
141
|
+
if Faraday::Adapter::EMSynchrony.loaded?
|
142
|
+
begin
|
143
|
+
require 'openssl'
|
144
|
+
rescue LoadError
|
145
|
+
warn 'Warning: no such file to load -- openssl. ' \
|
146
|
+
'Make sure it is installed if you want HTTPS support'
|
147
|
+
else
|
148
|
+
require 'faraday/adapter/em_http_ssl_patch'
|
149
|
+
end
|
150
|
+
end
|
@@ -1,74 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
4
|
class Adapter
|
5
|
+
# Excon adapter.
|
3
6
|
class Excon < Faraday::Adapter
|
4
7
|
dependency 'excon'
|
5
8
|
|
6
9
|
def call(env)
|
7
10
|
super
|
8
11
|
|
9
|
-
opts =
|
10
|
-
|
11
|
-
opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
|
12
|
-
opts[:ssl_ca_path] = ssl[:ca_path] if ssl[:ca_path]
|
13
|
-
opts[:ssl_ca_file] = ssl[:ca_file] if ssl[:ca_file]
|
14
|
-
opts[:client_cert] = ssl[:client_cert] if ssl[:client_cert]
|
15
|
-
opts[:client_key] = ssl[:client_key] if ssl[:client_key]
|
16
|
-
opts[:certificate] = ssl[:certificate] if ssl[:certificate]
|
17
|
-
opts[:private_key] = ssl[:private_key] if ssl[:private_key]
|
18
|
-
opts[:ssl_version] = ssl[:version] if ssl[:version]
|
19
|
-
opts[:ssl_min_version] = ssl[:min_version] if ssl[:min_version]
|
20
|
-
opts[:ssl_max_version] = ssl[:max_version] if ssl[:max_version]
|
21
|
-
|
22
|
-
# https://github.com/geemus/excon/issues/106
|
23
|
-
# https://github.com/jruby/jruby-ossl/issues/19
|
24
|
-
opts[:nonblock] = false
|
25
|
-
end
|
12
|
+
opts = opts_from_env(env)
|
13
|
+
conn = create_connection(env, opts)
|
26
14
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
if req[:proxy]
|
39
|
-
opts[:proxy] = {
|
40
|
-
:host => req[:proxy][:uri].host,
|
41
|
-
:hostname => req[:proxy][:uri].hostname,
|
42
|
-
:port => req[:proxy][:uri].port,
|
43
|
-
:scheme => req[:proxy][:uri].scheme,
|
44
|
-
:user => req[:proxy][:user],
|
45
|
-
:password => req[:proxy][:password]
|
46
|
-
}
|
47
|
-
end
|
15
|
+
resp = conn.request(method: env[:method].to_s.upcase,
|
16
|
+
headers: env[:request_headers],
|
17
|
+
body: read_body(env))
|
18
|
+
|
19
|
+
req = env[:request]
|
20
|
+
if req&.stream_response?
|
21
|
+
warn "Streaming downloads for #{self.class.name} are not yet " \
|
22
|
+
' implemented.'
|
23
|
+
req.on_data.call(resp.body, resp.body.bytesize)
|
48
24
|
end
|
25
|
+
save_response(env, resp.status.to_i, resp.body, resp.headers,
|
26
|
+
resp.reason_phrase)
|
49
27
|
|
50
|
-
|
28
|
+
@app.call(env)
|
29
|
+
rescue ::Excon::Errors::SocketError => e
|
30
|
+
raise Faraday::TimeoutError, e if e.message =~ /\btimeout\b/
|
51
31
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
save_response(env, resp.status.to_i, resp.body, resp.headers, resp.reason_phrase)
|
58
|
-
|
59
|
-
@app.call env
|
60
|
-
rescue ::Excon::Errors::SocketError => err
|
61
|
-
if err.message =~ /\btimeout\b/
|
62
|
-
raise Faraday::TimeoutError, err
|
63
|
-
elsif err.message =~ /\bcertificate\b/
|
64
|
-
raise Faraday::SSLError, err
|
65
|
-
else
|
66
|
-
raise Faraday::ConnectionFailed, err
|
67
|
-
end
|
68
|
-
rescue ::Excon::Errors::Timeout => err
|
69
|
-
raise Faraday::TimeoutError, err
|
32
|
+
raise Faraday::SSLError, e if e.message =~ /\bcertificate\b/
|
33
|
+
|
34
|
+
raise Faraday::ConnectionFailed, e
|
35
|
+
rescue ::Excon::Errors::Timeout => e
|
36
|
+
raise Faraday::TimeoutError, e
|
70
37
|
end
|
71
38
|
|
39
|
+
# @return [Excon]
|
72
40
|
def create_connection(env, opts)
|
73
41
|
::Excon.new(env[:url].to_s, opts.merge(@connection_options))
|
74
42
|
end
|
@@ -77,6 +45,78 @@ module Faraday
|
|
77
45
|
def read_body(env)
|
78
46
|
env[:body].respond_to?(:read) ? env[:body].read : env[:body]
|
79
47
|
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def opts_from_env(env)
|
52
|
+
opts = {}
|
53
|
+
amend_opts_with_ssl!(opts, env[:ssl]) if needs_ssl_settings?(env)
|
54
|
+
|
55
|
+
if (req = env[:request])
|
56
|
+
amend_opts_with_timeouts!(opts, req)
|
57
|
+
amend_opts_with_proxy_settings!(opts, req)
|
58
|
+
end
|
59
|
+
|
60
|
+
opts
|
61
|
+
end
|
62
|
+
|
63
|
+
def needs_ssl_settings?(env)
|
64
|
+
env[:url].scheme == 'https' && env[:ssl]
|
65
|
+
end
|
66
|
+
|
67
|
+
OPTS_KEYS = [
|
68
|
+
%i[client_cert client_cert],
|
69
|
+
%i[client_key client_key],
|
70
|
+
%i[certificate certificate],
|
71
|
+
%i[private_key private_key],
|
72
|
+
%i[ssl_ca_path ca_path],
|
73
|
+
%i[ssl_ca_file ca_file],
|
74
|
+
%i[ssl_version version],
|
75
|
+
%i[ssl_min_version min_version],
|
76
|
+
%i[ssl_max_version max_version]
|
77
|
+
].freeze
|
78
|
+
|
79
|
+
def amend_opts_with_ssl!(opts, ssl)
|
80
|
+
opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
|
81
|
+
# https://github.com/geemus/excon/issues/106
|
82
|
+
# https://github.com/jruby/jruby-ossl/issues/19
|
83
|
+
opts[:nonblock] = false
|
84
|
+
|
85
|
+
OPTS_KEYS.each do |(key_in_opts, key_in_ssl)|
|
86
|
+
next unless ssl[key_in_ssl]
|
87
|
+
|
88
|
+
opts[key_in_opts] = ssl[key_in_ssl]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def amend_opts_with_timeouts!(opts, req)
|
93
|
+
timeout = req[:timeout]
|
94
|
+
return unless timeout
|
95
|
+
|
96
|
+
opts[:read_timeout] = timeout
|
97
|
+
opts[:connect_timeout] = timeout
|
98
|
+
opts[:write_timeout] = timeout
|
99
|
+
|
100
|
+
open_timeout = req[:open_timeout]
|
101
|
+
return unless open_timeout
|
102
|
+
|
103
|
+
opts[:connect_timeout] = open_timeout
|
104
|
+
end
|
105
|
+
|
106
|
+
def amend_opts_with_proxy_settings!(opts, req)
|
107
|
+
opts[:proxy] = proxy_settings_for_opts(req[:proxy]) if req[:proxy]
|
108
|
+
end
|
109
|
+
|
110
|
+
def proxy_settings_for_opts(proxy)
|
111
|
+
{
|
112
|
+
host: proxy[:uri].host,
|
113
|
+
hostname: proxy[:uri].hostname,
|
114
|
+
port: proxy[:uri].port,
|
115
|
+
scheme: proxy[:uri].scheme,
|
116
|
+
user: proxy[:user],
|
117
|
+
password: proxy[:password]
|
118
|
+
}
|
119
|
+
end
|
80
120
|
end
|
81
121
|
end
|
82
122
|
end
|
@@ -1,8 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
4
|
class Adapter
|
5
|
+
# HTTPClient adapter.
|
3
6
|
class HTTPClient < Faraday::Adapter
|
4
7
|
dependency 'httpclient'
|
5
8
|
|
9
|
+
# @return [HTTPClient]
|
6
10
|
def client
|
7
11
|
@client ||= ::HTTPClient.new
|
8
12
|
end
|
@@ -13,65 +17,76 @@ module Faraday
|
|
13
17
|
# enable compression
|
14
18
|
client.transparent_gzip_decompression = true
|
15
19
|
|
16
|
-
if req = env[:request]
|
17
|
-
if proxy = req[:proxy]
|
20
|
+
if (req = env[:request])
|
21
|
+
if (proxy = req[:proxy])
|
18
22
|
configure_proxy proxy
|
19
23
|
end
|
20
24
|
|
21
|
-
if bind = req[:bind]
|
25
|
+
if (bind = req[:bind])
|
22
26
|
configure_socket bind
|
23
27
|
end
|
24
28
|
|
25
29
|
configure_timeouts req
|
26
30
|
end
|
27
31
|
|
28
|
-
if env[:url].scheme == 'https' && ssl = env[:ssl]
|
32
|
+
if env[:url].scheme == 'https' && (ssl = env[:ssl])
|
29
33
|
configure_ssl ssl
|
30
34
|
end
|
31
35
|
|
32
36
|
configure_client
|
33
37
|
|
34
|
-
# TODO Don't stream yet.
|
38
|
+
# TODO: Don't stream yet.
|
35
39
|
# https://github.com/nahi/httpclient/pull/90
|
36
40
|
env[:body] = env[:body].read if env[:body].respond_to? :read
|
37
41
|
|
38
42
|
resp = client.request env[:method], env[:url],
|
39
|
-
|
40
|
-
|
43
|
+
body: env[:body],
|
44
|
+
header: env[:request_headers]
|
41
45
|
|
46
|
+
if (req = env[:request]).stream_response?
|
47
|
+
warn "Streaming downloads for #{self.class.name} " \
|
48
|
+
'are not yet implemented.'
|
49
|
+
req.on_data.call(resp.body, resp.body.bytesize)
|
50
|
+
end
|
42
51
|
save_response env, resp.status, resp.body, resp.headers, resp.reason
|
43
52
|
|
44
53
|
@app.call env
|
45
54
|
rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
|
46
|
-
raise Faraday::TimeoutError,
|
47
|
-
rescue ::HTTPClient::BadResponseError =>
|
48
|
-
if
|
49
|
-
raise Faraday::ConnectionFailed,
|
50
|
-
|
51
|
-
raise Faraday::ClientError, $!
|
55
|
+
raise Faraday::TimeoutError, $ERROR_INFO
|
56
|
+
rescue ::HTTPClient::BadResponseError => e
|
57
|
+
if e.message.include?('status 407')
|
58
|
+
raise Faraday::ConnectionFailed,
|
59
|
+
%(407 "Proxy Authentication Required ")
|
52
60
|
end
|
53
|
-
|
54
|
-
raise Faraday::
|
55
|
-
rescue
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
raise
|
61
|
+
|
62
|
+
raise Faraday::ClientError, $ERROR_INFO
|
63
|
+
rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, IOError, SocketError
|
64
|
+
raise Faraday::ConnectionFailed, $ERROR_INFO
|
65
|
+
rescue StandardError => e
|
66
|
+
if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
|
67
|
+
raise Faraday::SSLError, e
|
60
68
|
end
|
69
|
+
|
70
|
+
raise
|
61
71
|
end
|
62
72
|
|
73
|
+
# @param bind [Hash]
|
63
74
|
def configure_socket(bind)
|
64
75
|
client.socket_local.host = bind[:host]
|
65
76
|
client.socket_local.port = bind[:port]
|
66
77
|
end
|
67
78
|
|
79
|
+
# Configure proxy URI and any user credentials.
|
80
|
+
#
|
81
|
+
# @param proxy [Hash]
|
68
82
|
def configure_proxy(proxy)
|
69
83
|
client.proxy = proxy[:uri]
|
70
|
-
|
71
|
-
|
72
|
-
|
84
|
+
return unless proxy[:user] && proxy[:password]
|
85
|
+
|
86
|
+
client.set_proxy_auth(proxy[:user], proxy[:password])
|
73
87
|
end
|
74
88
|
|
89
|
+
# @param ssl [Hash]
|
75
90
|
def configure_ssl(ssl)
|
76
91
|
ssl_config = client.ssl_config
|
77
92
|
ssl_config.verify_mode = ssl_verify_mode(ssl)
|
@@ -84,40 +99,47 @@ module Faraday
|
|
84
99
|
ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
85
100
|
end
|
86
101
|
|
102
|
+
# @param req [Hash]
|
87
103
|
def configure_timeouts(req)
|
88
|
-
if req[:timeout]
|
89
|
-
|
90
|
-
|
91
|
-
client.send_timeout = req[:timeout]
|
92
|
-
end
|
104
|
+
configure_timeout(req) if req[:timeout]
|
105
|
+
configure_open_timeout(req) if req[:open_timeout]
|
106
|
+
end
|
93
107
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
108
|
+
def configure_timeout(req)
|
109
|
+
client.connect_timeout = req[:timeout]
|
110
|
+
client.receive_timeout = req[:timeout]
|
111
|
+
client.send_timeout = req[:timeout]
|
112
|
+
end
|
113
|
+
|
114
|
+
def configure_open_timeout(req)
|
115
|
+
client.connect_timeout = req[:open_timeout]
|
116
|
+
client.send_timeout = req[:open_timeout]
|
98
117
|
end
|
99
118
|
|
100
119
|
def configure_client
|
101
|
-
@config_block
|
120
|
+
@config_block&.call(client)
|
102
121
|
end
|
103
122
|
|
123
|
+
# @param ssl [Hash]
|
124
|
+
# @return [OpenSSL::X509::Store]
|
104
125
|
def ssl_cert_store(ssl)
|
105
126
|
return ssl[:cert_store] if ssl[:cert_store]
|
127
|
+
|
106
128
|
# Memoize the cert store so that the same one is passed to
|
107
|
-
# HTTPClient each time, to avoid resyncing SSL
|
129
|
+
# HTTPClient each time, to avoid resyncing SSL sessions when
|
108
130
|
# it's changed
|
109
|
-
@
|
131
|
+
@ssl_cert_store ||= begin
|
110
132
|
# Use the default cert store by default, i.e. system ca certs
|
111
|
-
|
112
|
-
cert_store.set_default_paths
|
113
|
-
cert_store
|
133
|
+
OpenSSL::X509::Store.new.tap(&:set_default_paths)
|
114
134
|
end
|
115
135
|
end
|
116
136
|
|
137
|
+
# @param ssl [Hash]
|
117
138
|
def ssl_verify_mode(ssl)
|
118
139
|
ssl[:verify_mode] || begin
|
119
140
|
if ssl.fetch(:verify, true)
|
120
|
-
OpenSSL::SSL::VERIFY_PEER |
|
141
|
+
OpenSSL::SSL::VERIFY_PEER |
|
142
|
+
OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
|
121
143
|
else
|
122
144
|
OpenSSL::SSL::VERIFY_NONE
|
123
145
|
end
|