faraday 0.15.4 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +276 -0
- data/LICENSE.md +1 -1
- data/README.md +18 -344
- data/Rakefile +7 -0
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday/adapter/em_http.rb +144 -101
- data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
- 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 +98 -56
- data/lib/faraday/adapter/httpclient.rb +83 -59
- data/lib/faraday/adapter/net_http.rb +130 -63
- data/lib/faraday/adapter/net_http_persistent.rb +51 -28
- data/lib/faraday/adapter/patron.rb +80 -43
- data/lib/faraday/adapter/rack.rb +30 -13
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter.rb +82 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +312 -182
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +98 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
- data/lib/faraday/error.rb +103 -37
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/middleware.rb +12 -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 +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +35 -186
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +67 -56
- data/lib/faraday/request/authorization.rb +44 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +79 -48
- data/lib/faraday/request/retry.rb +198 -169
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/request.rb +68 -36
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +38 -14
- data/lib/faraday/response.rb +27 -17
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +36 -245
- data/lib/faraday.rb +94 -176
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +47 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +691 -0
- data/spec/faraday/error_spec.rb +45 -0
- data/spec/faraday/middleware_spec.rb +26 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +37 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +34 -0
- data/spec/faraday/params_encoders/nested_spec.rb +134 -0
- data/spec/faraday/rack_builder_spec.rb +196 -0
- data/spec/faraday/request/authorization_spec.rb +88 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/multipart_spec.rb +274 -0
- data/spec/faraday/request/retry_spec.rb +242 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +109 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +106 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +132 -0
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +133 -0
- data/spec/support/shared_examples/adapter.rb +104 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +234 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +78 -9
- data/lib/faraday/upload_io.rb +0 -67
@@ -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,81 +1,123 @@
|
|
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
|
|
9
|
+
def build_connection(env)
|
10
|
+
opts = opts_from_env(env)
|
11
|
+
::Excon.new(env[:url].to_s, opts.merge(@connection_options))
|
12
|
+
end
|
13
|
+
|
6
14
|
def call(env)
|
7
15
|
super
|
8
16
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
17
|
+
req_opts = {
|
18
|
+
method: env[:method].to_s.upcase,
|
19
|
+
headers: env[:request_headers],
|
20
|
+
body: read_body(env)
|
21
|
+
}
|
26
22
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
23
|
+
req = env[:request]
|
24
|
+
if req&.stream_response?
|
25
|
+
total = 0
|
26
|
+
req_opts[:response_block] = lambda do |chunk, _remain, _total|
|
27
|
+
req.on_data.call(chunk, total += chunk.size)
|
32
28
|
end
|
29
|
+
end
|
33
30
|
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
resp = connection(env) { |http| http.request(req_opts) }
|
32
|
+
save_response(env, resp.status.to_i, resp.body, resp.headers,
|
33
|
+
resp.reason_phrase)
|
37
34
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
35
|
+
@app.call(env)
|
36
|
+
rescue ::Excon::Errors::SocketError => e
|
37
|
+
raise Faraday::TimeoutError, e if e.message =~ /\btimeout\b/
|
38
|
+
|
39
|
+
raise Faraday::SSLError, e if e.message =~ /\bcertificate\b/
|
40
|
+
|
41
|
+
raise Faraday::ConnectionFailed, e
|
42
|
+
rescue ::Excon::Errors::Timeout => e
|
43
|
+
raise Faraday::TimeoutError, e
|
44
|
+
end
|
45
|
+
|
46
|
+
# TODO: support streaming requests
|
47
|
+
def read_body(env)
|
48
|
+
env[:body].respond_to?(:read) ? env[:body].read : env[:body]
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def opts_from_env(env)
|
54
|
+
opts = {}
|
55
|
+
amend_opts_with_ssl!(opts, env[:ssl]) if needs_ssl_settings?(env)
|
56
|
+
|
57
|
+
if (req = env[:request])
|
58
|
+
amend_opts_with_timeouts!(opts, req)
|
59
|
+
amend_opts_with_proxy_settings!(opts, req)
|
48
60
|
end
|
49
61
|
|
50
|
-
|
62
|
+
opts
|
63
|
+
end
|
51
64
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
:body => read_body(env)
|
65
|
+
def needs_ssl_settings?(env)
|
66
|
+
env[:url].scheme == 'https' && env[:ssl]
|
67
|
+
end
|
56
68
|
|
57
|
-
|
69
|
+
OPTS_KEYS = [
|
70
|
+
%i[client_cert client_cert],
|
71
|
+
%i[client_key client_key],
|
72
|
+
%i[certificate certificate],
|
73
|
+
%i[private_key private_key],
|
74
|
+
%i[ssl_ca_path ca_path],
|
75
|
+
%i[ssl_ca_file ca_file],
|
76
|
+
%i[ssl_version version],
|
77
|
+
%i[ssl_min_version min_version],
|
78
|
+
%i[ssl_max_version max_version]
|
79
|
+
].freeze
|
58
80
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
81
|
+
def amend_opts_with_ssl!(opts, ssl)
|
82
|
+
opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
|
83
|
+
# https://github.com/geemus/excon/issues/106
|
84
|
+
# https://github.com/jruby/jruby-ossl/issues/19
|
85
|
+
opts[:nonblock] = false
|
86
|
+
|
87
|
+
OPTS_KEYS.each do |(key_in_opts, key_in_ssl)|
|
88
|
+
next unless ssl[key_in_ssl]
|
89
|
+
|
90
|
+
opts[key_in_opts] = ssl[key_in_ssl]
|
67
91
|
end
|
68
|
-
rescue ::Excon::Errors::Timeout => err
|
69
|
-
raise Error::TimeoutError, err
|
70
92
|
end
|
71
93
|
|
72
|
-
def
|
73
|
-
|
94
|
+
def amend_opts_with_timeouts!(opts, req)
|
95
|
+
if (sec = request_timeout(:read, req))
|
96
|
+
opts[:read_timeout] = sec
|
97
|
+
end
|
98
|
+
|
99
|
+
if (sec = request_timeout(:write, req))
|
100
|
+
opts[:write_timeout] = sec
|
101
|
+
end
|
102
|
+
|
103
|
+
return unless (sec = request_timeout(:open, req))
|
104
|
+
|
105
|
+
opts[:connect_timeout] = sec
|
74
106
|
end
|
75
107
|
|
76
|
-
|
77
|
-
|
78
|
-
|
108
|
+
def amend_opts_with_proxy_settings!(opts, req)
|
109
|
+
opts[:proxy] = proxy_settings_for_opts(req[:proxy]) if req[:proxy]
|
110
|
+
end
|
111
|
+
|
112
|
+
def proxy_settings_for_opts(proxy)
|
113
|
+
{
|
114
|
+
host: proxy[:uri].host,
|
115
|
+
hostname: proxy[:uri].hostname,
|
116
|
+
port: proxy[:uri].port,
|
117
|
+
scheme: proxy[:uri].scheme,
|
118
|
+
user: proxy[:user],
|
119
|
+
password: proxy[:password]
|
120
|
+
}
|
79
121
|
end
|
80
122
|
end
|
81
123
|
end
|
@@ -1,78 +1,97 @@
|
|
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
|
|
6
|
-
def
|
7
|
-
@client ||= ::HTTPClient.new
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
super
|
12
|
-
|
13
|
-
# enable compression
|
14
|
-
client.transparent_gzip_decompression = true
|
9
|
+
def build_connection(env)
|
10
|
+
@client ||= ::HTTPClient.new.tap do |cli|
|
11
|
+
# enable compression
|
12
|
+
cli.transparent_gzip_decompression = true
|
13
|
+
end
|
15
14
|
|
16
|
-
if req = env[:request]
|
17
|
-
if proxy = req[:proxy]
|
18
|
-
configure_proxy proxy
|
15
|
+
if (req = env[:request])
|
16
|
+
if (proxy = req[:proxy])
|
17
|
+
configure_proxy @client, proxy
|
19
18
|
end
|
20
19
|
|
21
|
-
if bind = req[:bind]
|
22
|
-
configure_socket bind
|
20
|
+
if (bind = req[:bind])
|
21
|
+
configure_socket @client, bind
|
23
22
|
end
|
24
23
|
|
25
|
-
configure_timeouts req
|
24
|
+
configure_timeouts @client, req
|
26
25
|
end
|
27
26
|
|
28
|
-
if env[:url].scheme == 'https' && ssl = env[:ssl]
|
29
|
-
configure_ssl ssl
|
27
|
+
if env[:url].scheme == 'https' && (ssl = env[:ssl])
|
28
|
+
configure_ssl @client, ssl
|
30
29
|
end
|
31
30
|
|
32
|
-
configure_client
|
31
|
+
configure_client @client
|
32
|
+
|
33
|
+
@client
|
34
|
+
end
|
33
35
|
|
34
|
-
|
36
|
+
def call(env)
|
37
|
+
super
|
38
|
+
|
39
|
+
# TODO: Don't stream yet.
|
35
40
|
# https://github.com/nahi/httpclient/pull/90
|
36
41
|
env[:body] = env[:body].read if env[:body].respond_to? :read
|
37
42
|
|
38
|
-
|
39
|
-
:
|
40
|
-
|
43
|
+
connection(env) do |http|
|
44
|
+
resp = http.request env[:method], env[:url],
|
45
|
+
body: env[:body],
|
46
|
+
header: env[:request_headers]
|
41
47
|
|
42
|
-
|
48
|
+
if (req = env[:request]).stream_response?
|
49
|
+
warn "Streaming downloads for #{self.class.name} " \
|
50
|
+
'are not yet implemented.'
|
51
|
+
req.on_data.call(resp.body, resp.body.bytesize)
|
52
|
+
end
|
53
|
+
save_response env, resp.status, resp.body, resp.headers, resp.reason
|
43
54
|
|
44
|
-
|
55
|
+
@app.call env
|
56
|
+
end
|
45
57
|
rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
|
46
|
-
raise Faraday::
|
47
|
-
rescue ::HTTPClient::BadResponseError =>
|
48
|
-
if
|
49
|
-
raise Faraday::
|
50
|
-
|
51
|
-
raise Faraday::Error::ClientError, $!
|
58
|
+
raise Faraday::TimeoutError, $ERROR_INFO
|
59
|
+
rescue ::HTTPClient::BadResponseError => e
|
60
|
+
if e.message.include?('status 407')
|
61
|
+
raise Faraday::ConnectionFailed,
|
62
|
+
%(407 "Proxy Authentication Required ")
|
52
63
|
end
|
53
|
-
|
54
|
-
raise Faraday::
|
55
|
-
rescue
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
64
|
+
|
65
|
+
raise Faraday::ClientError, $ERROR_INFO
|
66
|
+
rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, IOError, SocketError
|
67
|
+
raise Faraday::ConnectionFailed, $ERROR_INFO
|
68
|
+
rescue StandardError => e
|
69
|
+
if defined?(::OpenSSL::SSL::SSLError) && \
|
70
|
+
e.is_a?(::OpenSSL::SSL::SSLError)
|
71
|
+
raise Faraday::SSLError, e
|
60
72
|
end
|
73
|
+
|
74
|
+
raise
|
61
75
|
end
|
62
76
|
|
63
|
-
|
77
|
+
# @param bind [Hash]
|
78
|
+
def configure_socket(client, bind)
|
64
79
|
client.socket_local.host = bind[:host]
|
65
80
|
client.socket_local.port = bind[:port]
|
66
81
|
end
|
67
82
|
|
68
|
-
|
83
|
+
# Configure proxy URI and any user credentials.
|
84
|
+
#
|
85
|
+
# @param proxy [Hash]
|
86
|
+
def configure_proxy(client, proxy)
|
69
87
|
client.proxy = proxy[:uri]
|
70
|
-
|
71
|
-
|
72
|
-
|
88
|
+
return unless proxy[:user] && proxy[:password]
|
89
|
+
|
90
|
+
client.set_proxy_auth(proxy[:user], proxy[:password])
|
73
91
|
end
|
74
92
|
|
75
|
-
|
93
|
+
# @param ssl [Hash]
|
94
|
+
def configure_ssl(client, ssl)
|
76
95
|
ssl_config = client.ssl_config
|
77
96
|
ssl_config.verify_mode = ssl_verify_mode(ssl)
|
78
97
|
ssl_config.cert_store = ssl_cert_store(ssl)
|
@@ -84,40 +103,45 @@ module Faraday
|
|
84
103
|
ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
85
104
|
end
|
86
105
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
client.
|
91
|
-
client.send_timeout = req[:timeout]
|
106
|
+
# @param req [Hash]
|
107
|
+
def configure_timeouts(client, req)
|
108
|
+
if (sec = request_timeout(:open, req))
|
109
|
+
client.connect_timeout = sec
|
92
110
|
end
|
93
111
|
|
94
|
-
if req
|
95
|
-
client.
|
96
|
-
client.send_timeout = req[:open_timeout]
|
112
|
+
if (sec = request_timeout(:write, req))
|
113
|
+
client.send_timeout = sec
|
97
114
|
end
|
115
|
+
|
116
|
+
return unless (sec = request_timeout(:read, req))
|
117
|
+
|
118
|
+
client.receive_timeout = sec
|
98
119
|
end
|
99
120
|
|
100
|
-
def configure_client
|
101
|
-
@config_block
|
121
|
+
def configure_client(client)
|
122
|
+
@config_block&.call(client)
|
102
123
|
end
|
103
124
|
|
125
|
+
# @param ssl [Hash]
|
126
|
+
# @return [OpenSSL::X509::Store]
|
104
127
|
def ssl_cert_store(ssl)
|
105
128
|
return ssl[:cert_store] if ssl[:cert_store]
|
129
|
+
|
106
130
|
# Memoize the cert store so that the same one is passed to
|
107
|
-
# HTTPClient each time, to avoid resyncing SSL
|
131
|
+
# HTTPClient each time, to avoid resyncing SSL sessions when
|
108
132
|
# it's changed
|
109
|
-
@
|
133
|
+
@ssl_cert_store ||= begin
|
110
134
|
# Use the default cert store by default, i.e. system ca certs
|
111
|
-
|
112
|
-
cert_store.set_default_paths
|
113
|
-
cert_store
|
135
|
+
OpenSSL::X509::Store.new.tap(&:set_default_paths)
|
114
136
|
end
|
115
137
|
end
|
116
138
|
|
139
|
+
# @param ssl [Hash]
|
117
140
|
def ssl_verify_mode(ssl)
|
118
141
|
ssl[:verify_mode] || begin
|
119
142
|
if ssl.fetch(:verify, true)
|
120
|
-
OpenSSL::SSL::VERIFY_PEER |
|
143
|
+
OpenSSL::SSL::VERIFY_PEER |
|
144
|
+
OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
|
121
145
|
else
|
122
146
|
OpenSSL::SSL::VERIFY_NONE
|
123
147
|
end
|