faraday 1.0.0 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +299 -1
  3. data/LICENSE.md +1 -1
  4. data/README.md +35 -23
  5. data/Rakefile +6 -1
  6. data/examples/client_spec.rb +68 -14
  7. data/examples/client_test.rb +80 -15
  8. data/lib/faraday/adapter/test.rb +117 -52
  9. data/lib/faraday/adapter.rb +7 -21
  10. data/lib/faraday/adapter_registry.rb +3 -1
  11. data/lib/faraday/connection.rb +75 -133
  12. data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
  13. data/lib/faraday/encoders/nested_params_encoder.rb +20 -8
  14. data/lib/faraday/error.rb +39 -6
  15. data/lib/faraday/logging/formatter.rb +28 -15
  16. data/lib/faraday/methods.rb +6 -0
  17. data/lib/faraday/middleware.rb +59 -5
  18. data/lib/faraday/middleware_registry.rb +17 -63
  19. data/lib/faraday/options/connection_options.rb +7 -6
  20. data/lib/faraday/options/env.rb +85 -62
  21. data/lib/faraday/options/proxy_options.rb +11 -3
  22. data/lib/faraday/options/request_options.rb +7 -6
  23. data/lib/faraday/options/ssl_options.rb +56 -45
  24. data/lib/faraday/options.rb +11 -14
  25. data/lib/faraday/rack_builder.rb +35 -32
  26. data/lib/faraday/request/authorization.rb +37 -36
  27. data/lib/faraday/request/instrumentation.rb +5 -1
  28. data/lib/faraday/request/json.rb +70 -0
  29. data/lib/faraday/request/url_encoded.rb +8 -2
  30. data/lib/faraday/request.rb +22 -29
  31. data/lib/faraday/response/json.rb +74 -0
  32. data/lib/faraday/response/logger.rb +8 -4
  33. data/lib/faraday/response/raise_error.rb +43 -3
  34. data/lib/faraday/response.rb +10 -23
  35. data/lib/faraday/utils/headers.rb +17 -6
  36. data/lib/faraday/utils.rb +22 -10
  37. data/lib/faraday/version.rb +5 -0
  38. data/lib/faraday.rb +49 -58
  39. data/spec/faraday/adapter/test_spec.rb +442 -0
  40. data/spec/faraday/connection_spec.rb +207 -90
  41. data/spec/faraday/error_spec.rb +45 -5
  42. data/spec/faraday/middleware_registry_spec.rb +31 -0
  43. data/spec/faraday/middleware_spec.rb +193 -6
  44. data/spec/faraday/options/env_spec.rb +8 -2
  45. data/spec/faraday/options/options_spec.rb +1 -1
  46. data/spec/faraday/options/proxy_options_spec.rb +15 -0
  47. data/spec/faraday/params_encoders/flat_spec.rb +8 -0
  48. data/spec/faraday/params_encoders/nested_spec.rb +18 -1
  49. data/spec/faraday/rack_builder_spec.rb +171 -50
  50. data/spec/faraday/request/authorization_spec.rb +54 -24
  51. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  52. data/spec/faraday/request/json_spec.rb +199 -0
  53. data/spec/faraday/request/url_encoded_spec.rb +25 -2
  54. data/spec/faraday/request_spec.rb +11 -10
  55. data/spec/faraday/response/json_spec.rb +206 -0
  56. data/spec/faraday/response/logger_spec.rb +38 -0
  57. data/spec/faraday/response/raise_error_spec.rb +149 -0
  58. data/spec/faraday/response_spec.rb +3 -1
  59. data/spec/faraday/utils/headers_spec.rb +31 -4
  60. data/spec/faraday/utils_spec.rb +63 -1
  61. data/spec/faraday_spec.rb +10 -4
  62. data/spec/spec_helper.rb +6 -5
  63. data/spec/support/fake_safe_buffer.rb +1 -1
  64. data/spec/support/faraday_middleware_subclasses.rb +18 -0
  65. data/spec/support/helper_methods.rb +0 -37
  66. data/spec/support/shared_examples/adapter.rb +4 -3
  67. data/spec/support/shared_examples/request_method.rb +60 -31
  68. metadata +34 -44
  69. data/UPGRADING.md +0 -55
  70. data/lib/faraday/adapter/em_http.rb +0 -285
  71. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  72. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  73. data/lib/faraday/adapter/em_synchrony.rb +0 -150
  74. data/lib/faraday/adapter/excon.rb +0 -124
  75. data/lib/faraday/adapter/httpclient.rb +0 -151
  76. data/lib/faraday/adapter/net_http.rb +0 -209
  77. data/lib/faraday/adapter/net_http_persistent.rb +0 -91
  78. data/lib/faraday/adapter/patron.rb +0 -132
  79. data/lib/faraday/adapter/rack.rb +0 -75
  80. data/lib/faraday/adapter/typhoeus.rb +0 -15
  81. data/lib/faraday/autoload.rb +0 -95
  82. data/lib/faraday/dependency_loader.rb +0 -37
  83. data/lib/faraday/file_part.rb +0 -128
  84. data/lib/faraday/param_part.rb +0 -53
  85. data/lib/faraday/request/basic_authentication.rb +0 -20
  86. data/lib/faraday/request/multipart.rb +0 -99
  87. data/lib/faraday/request/retry.rb +0 -239
  88. data/lib/faraday/request/token_authentication.rb +0 -20
  89. data/spec/faraday/adapter/em_http_spec.rb +0 -47
  90. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
  91. data/spec/faraday/adapter/excon_spec.rb +0 -49
  92. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  93. data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
  94. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  95. data/spec/faraday/adapter/patron_spec.rb +0 -18
  96. data/spec/faraday/adapter/rack_spec.rb +0 -8
  97. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  98. data/spec/faraday/composite_read_io_spec.rb +0 -80
  99. data/spec/faraday/request/multipart_spec.rb +0 -274
  100. data/spec/faraday/request/retry_spec.rb +0 -242
  101. data/spec/faraday/response/middleware_spec.rb +0 -52
  102. data/spec/support/webmock_rack_app.rb +0 -68
@@ -1,285 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- # EventMachine adapter. This adapter is useful for either asynchronous
6
- # requests when in an EM reactor loop, or for making parallel requests in
7
- # synchronous code.
8
- class EMHttp < Faraday::Adapter
9
- # Options is a module containing helpers to convert the Faraday env object
10
- # into options hashes for EMHTTP method calls.
11
- module Options
12
- # @return [Hash]
13
- def connection_config(env)
14
- options = {}
15
- configure_proxy(options, env)
16
- configure_timeout(options, env)
17
- configure_socket(options, env)
18
- configure_ssl(options, env)
19
- options
20
- end
21
-
22
- def request_config(env)
23
- options = {
24
- body: read_body(env),
25
- head: env[:request_headers]
26
- # keepalive: true,
27
- # file: 'path/to/file', # stream data off disk
28
- }
29
- configure_compression(options, env)
30
- options
31
- end
32
-
33
- def read_body(env)
34
- body = env[:body]
35
- body.respond_to?(:read) ? body.read : body
36
- end
37
-
38
- # Reads out proxy settings from env into options
39
- def configure_proxy(options, env)
40
- proxy = request_options(env)[:proxy]
41
- return unless proxy
42
-
43
- options[:proxy] = {
44
- host: proxy[:uri].host,
45
- port: proxy[:uri].port,
46
- authorization: [proxy[:user], proxy[:password]]
47
- }
48
- end
49
-
50
- # Reads out host and port settings from env into options
51
- def configure_socket(options, env)
52
- bind = request_options(env)[:bind]
53
- return unless bind
54
-
55
- options[:bind] = {
56
- host: bind[:host],
57
- port: bind[:port]
58
- }
59
- end
60
-
61
- # Reads out SSL certificate settings from env into options
62
- def configure_ssl(options, env)
63
- return unless env[:url].scheme == 'https' && env[:ssl]
64
-
65
- options[:ssl] = {
66
- cert_chain_file: env[:ssl][:ca_file],
67
- verify_peer: env[:ssl].fetch(:verify, true)
68
- }
69
- end
70
-
71
- # Reads out timeout settings from env into options
72
- def configure_timeout(options, env)
73
- req = request_options(env)
74
- options[:inactivity_timeout] = request_timeout(:read, req)
75
- options[:connect_timeout] = request_timeout(:open, req)
76
- end
77
-
78
- # Reads out compression header settings from env into options
79
- def configure_compression(options, env)
80
- return unless (env[:method] == :get) &&
81
- !options[:head].key?('accept-encoding')
82
-
83
- options[:head]['accept-encoding'] = 'gzip, compressed'
84
- end
85
-
86
- def request_options(env)
87
- env[:request]
88
- end
89
- end
90
-
91
- include Options
92
-
93
- dependency 'em-http'
94
-
95
- self.supports_parallel = true
96
-
97
- # @return [Manager]
98
- def self.setup_parallel_manager(_options = nil)
99
- Manager.new
100
- end
101
-
102
- def call(env)
103
- super
104
- perform_request env
105
- @app.call env
106
- end
107
-
108
- def perform_request(env)
109
- if parallel?(env)
110
- manager = env[:parallel_manager]
111
- manager.add do
112
- perform_single_request(env)
113
- .callback { env[:response].finish(env) }
114
- end
115
- elsif EventMachine.reactor_running?
116
- # EM is running: instruct upstream that this is an async request
117
- env[:parallel_manager] = true
118
- perform_single_request(env)
119
- .callback { env[:response].finish(env) }
120
- .errback do
121
- # TODO: no way to communicate the error in async mode
122
- raise NotImplementedError
123
- end
124
- else
125
- error = nil
126
- # start EM, block until request is completed
127
- EventMachine.run do
128
- perform_single_request(env)
129
- .callback { EventMachine.stop }
130
- .errback do |client|
131
- error = error_message(client)
132
- EventMachine.stop
133
- end
134
- end
135
- raise_error(error) if error
136
- end
137
- rescue EventMachine::Connectify::CONNECTError => e
138
- if e.message.include?('Proxy Authentication Required')
139
- raise Faraday::ConnectionFailed,
140
- %(407 "Proxy Authentication Required ")
141
- end
142
-
143
- raise Faraday::ConnectionFailed, e
144
- rescue StandardError => e
145
- if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
146
- raise Faraday::SSLError, e
147
- end
148
-
149
- raise
150
- end
151
-
152
- # TODO: reuse the connection to support pipelining
153
- def perform_single_request(env)
154
- req = create_request(env)
155
- req = req.setup_request(env[:method], request_config(env))
156
- req.callback do |client|
157
- if env[:request].stream_response?
158
- warn "Streaming downloads for #{self.class.name} " \
159
- 'are not yet implemented.'
160
- env[:request].on_data.call(
161
- client.response,
162
- client.response.bytesize
163
- )
164
- end
165
- status = client.response_header.status
166
- reason = client.response_header.http_reason
167
- save_response(env, status, client.response, nil, reason) do |headers|
168
- client.response_header.each do |name, value|
169
- headers[name.to_sym] = value
170
- end
171
- end
172
- end
173
- end
174
-
175
- def create_request(env)
176
- EventMachine::HttpRequest.new(
177
- env[:url], connection_config(env).merge(@connection_options)
178
- )
179
- end
180
-
181
- def error_message(client)
182
- client.error || 'request failed'
183
- end
184
-
185
- def raise_error(msg)
186
- error_class = Faraday::ClientError
187
- if timeout_message?(msg)
188
- error_class = Faraday::TimeoutError
189
- msg = 'request timed out'
190
- elsif msg == Errno::ECONNREFUSED
191
- error_class = Faraday::ConnectionFailed
192
- msg = 'connection refused'
193
- elsif msg == 'connection closed by server'
194
- error_class = Faraday::ConnectionFailed
195
- end
196
- raise error_class, msg
197
- end
198
-
199
- def timeout_message?(msg)
200
- msg == Errno::ETIMEDOUT ||
201
- (msg.is_a?(String) && msg.include?('timeout error'))
202
- end
203
-
204
- # @return [Boolean]
205
- def parallel?(env)
206
- !!env[:parallel_manager]
207
- end
208
-
209
- # This parallel manager is designed to start an EventMachine loop
210
- # and block until all registered requests have been completed.
211
- class Manager
212
- # @see reset
213
- def initialize
214
- reset
215
- end
216
-
217
- # Re-initializes instance variables
218
- def reset
219
- @registered_procs = []
220
- @num_registered = 0
221
- @num_succeeded = 0
222
- @errors = []
223
- @running = false
224
- end
225
-
226
- # @return [Boolean]
227
- def running?
228
- @running
229
- end
230
-
231
- def add(&block)
232
- if running?
233
- perform_request { yield }
234
- else
235
- @registered_procs << block
236
- end
237
- @num_registered += 1
238
- end
239
-
240
- def run
241
- if @num_registered.positive?
242
- @running = true
243
- EventMachine.run do
244
- @registered_procs.each do |proc|
245
- perform_request(&proc)
246
- end
247
- end
248
- unless @errors.empty?
249
- raise Faraday::ClientError, @errors.first || 'connection failed'
250
- end
251
- end
252
- ensure
253
- reset
254
- end
255
-
256
- def perform_request
257
- client = yield
258
- client.callback do
259
- @num_succeeded += 1
260
- check_finished
261
- end
262
- client.errback do
263
- @errors << client.error
264
- check_finished
265
- end
266
- end
267
-
268
- def check_finished
269
- EventMachine.stop if @num_succeeded + @errors.size == @num_registered
270
- end
271
- end
272
- end
273
- end
274
- end
275
-
276
- if Faraday::Adapter::EMHttp.loaded?
277
- begin
278
- require 'openssl'
279
- rescue LoadError
280
- warn 'Warning: no such file to load -- openssl. ' \
281
- 'Make sure it is installed if you want HTTPS support'
282
- else
283
- require 'faraday/adapter/em_http_ssl_patch'
284
- end
285
- end
@@ -1,62 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'openssl'
4
- require 'em-http'
5
-
6
- # EventMachine patch to make SSL work.
7
- module EmHttpSslPatch
8
- def ssl_verify_peer(cert_string)
9
- begin
10
- @last_seen_cert = OpenSSL::X509::Certificate.new(cert_string)
11
- rescue OpenSSL::X509::CertificateError
12
- return false
13
- end
14
-
15
- unless certificate_store.verify(@last_seen_cert)
16
- raise OpenSSL::SSL::SSLError,
17
- %(unable to verify the server certificate for "#{host}")
18
- end
19
-
20
- begin
21
- certificate_store.add_cert(@last_seen_cert)
22
- rescue OpenSSL::X509::StoreError => e
23
- raise e unless e.message == 'cert already in hash table'
24
- end
25
- true
26
- end
27
-
28
- def ssl_handshake_completed
29
- return true unless verify_peer?
30
-
31
- unless verified_cert_identity?
32
- raise OpenSSL::SSL::SSLError,
33
- %(host "#{host}" does not match the server certificate)
34
- end
35
-
36
- true
37
- end
38
-
39
- def verify_peer?
40
- parent.connopts.tls[:verify_peer]
41
- end
42
-
43
- def verified_cert_identity?
44
- OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
45
- end
46
-
47
- def host
48
- parent.uri.host
49
- end
50
-
51
- def certificate_store
52
- @certificate_store ||= begin
53
- store = OpenSSL::X509::Store.new
54
- store.set_default_paths
55
- ca_file = parent.connopts.tls[:cert_chain_file]
56
- store.add_file(ca_file) if ca_file
57
- store
58
- end
59
- end
60
- end
61
-
62
- EventMachine::HttpStubConnection.include(EmHttpSslPatch)
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- class EMSynchrony < Faraday::Adapter
6
- # A parallel manager for EMSynchrony.
7
- class ParallelManager
8
- # Add requests to queue.
9
- #
10
- # @param request [EM::HttpRequest]
11
- # @param method [Symbol, String] HTTP method
12
- # @param args [Array] the rest of the positional arguments
13
- def add(request, method, *args, &block)
14
- queue << {
15
- request: request,
16
- method: method,
17
- args: args,
18
- block: block
19
- }
20
- end
21
-
22
- # Run all requests on queue with `EM::Synchrony::Multi`, wrapping
23
- # it in a reactor and fiber if needed.
24
- def run
25
- result = nil
26
- if !EM.reactor_running?
27
- EM.run do
28
- Fiber.new do
29
- result = perform
30
- EM.stop
31
- end.resume
32
- end
33
- else
34
- result = perform
35
- end
36
- result
37
- end
38
-
39
- private
40
-
41
- # The request queue.
42
- def queue
43
- @queue ||= []
44
- end
45
-
46
- # Main `EM::Synchrony::Multi` performer.
47
- def perform
48
- multi = ::EM::Synchrony::Multi.new
49
-
50
- queue.each do |item|
51
- method = "a#{item[:method]}".to_sym
52
-
53
- req = item[:request].send(method, *item[:args])
54
- req.callback(&item[:block])
55
-
56
- req_name = "req_#{multi.requests.size}".to_sym
57
- multi.add(req_name, req)
58
- end
59
-
60
- # Clear the queue, so parallel manager objects can be reused.
61
- @queue = []
62
-
63
- # Block fiber until all requests have returned.
64
- multi.perform
65
- end
66
- end
67
- end
68
- end
69
- end
@@ -1,150 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'uri'
4
-
5
- module Faraday
6
- class Adapter
7
- # EventMachine Synchrony adapter.
8
- class EMSynchrony < Faraday::Adapter
9
- include EMHttp::Options
10
-
11
- dependency do
12
- require 'em-synchrony/em-http'
13
- require 'em-synchrony/em-multi'
14
- require 'fiber'
15
- end
16
-
17
- self.supports_parallel = true
18
-
19
- # @return [ParallelManager]
20
- def self.setup_parallel_manager(_options = nil)
21
- ParallelManager.new
22
- end
23
-
24
- def call(env)
25
- super
26
- request = create_request(env)
27
-
28
- http_method = env[:method].to_s.downcase.to_sym
29
-
30
- if env[:parallel_manager]
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
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
46
-
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
72
-
73
- private
74
-
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|
87
- resp_headers[name.to_sym] = value
88
- end
89
- end
90
-
91
- # Finalize the response object with values from `env`.
92
- env[:response].finish(env)
93
- end
94
- end
95
-
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
- )
109
- end
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
116
- end
117
- end
118
-
119
- def call_block(block)
120
- client = nil
121
-
122
- if EM.reactor_running?
123
- client = block.call
124
- else
125
- EM.run do
126
- Fiber.new do
127
- client = block.call
128
- EM.stop
129
- end.resume
130
- end
131
- end
132
-
133
- client
134
- end
135
- end
136
- end
137
- end
138
-
139
- require 'faraday/adapter/em_synchrony/parallel_manager'
140
-
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,124 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- # Excon adapter.
6
- class Excon < Faraday::Adapter
7
- dependency 'excon'
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
-
14
- def call(env)
15
- super
16
-
17
- req_opts = {
18
- method: env[:method].to_s.upcase,
19
- headers: env[:request_headers],
20
- body: read_body(env)
21
- }
22
-
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)
28
- end
29
- end
30
-
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)
34
-
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)
60
- end
61
-
62
- opts
63
- end
64
-
65
- def needs_ssl_settings?(env)
66
- env[:url].scheme == 'https' && env[:ssl]
67
- end
68
-
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
80
-
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]
91
- end
92
- end
93
-
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
106
- end
107
-
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
- }
121
- end
122
- end
123
- end
124
- end