faraday 1.0.0 → 2.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) 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 +3 -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 +6 -20
  10. data/lib/faraday/adapter_registry.rb +3 -1
  11. data/lib/faraday/connection.rb +73 -132
  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 +37 -8
  15. data/lib/faraday/logging/formatter.rb +28 -15
  16. data/lib/faraday/methods.rb +6 -0
  17. data/lib/faraday/middleware.rb +17 -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 +73 -0
  32. data/lib/faraday/response/logger.rb +8 -4
  33. data/lib/faraday/response/raise_error.rb +41 -3
  34. data/lib/faraday/response.rb +10 -23
  35. data/lib/faraday/utils/headers.rb +9 -4
  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 +50 -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 +16 -0
  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 +189 -0
  56. data/spec/faraday/response/logger_spec.rb +38 -0
  57. data/spec/faraday/response/raise_error_spec.rb +105 -0
  58. data/spec/faraday/response_spec.rb +3 -1
  59. data/spec/faraday/utils/headers_spec.rb +22 -4
  60. data/spec/faraday/utils_spec.rb +63 -1
  61. data/spec/faraday_spec.rb +8 -4
  62. data/spec/spec_helper.rb +6 -5
  63. data/spec/support/fake_safe_buffer.rb +1 -1
  64. data/spec/support/helper_methods.rb +0 -37
  65. data/spec/support/shared_examples/adapter.rb +4 -3
  66. data/spec/support/shared_examples/request_method.rb +60 -31
  67. metadata +19 -44
  68. data/UPGRADING.md +0 -55
  69. data/lib/faraday/adapter/em_http.rb +0 -285
  70. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  71. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  72. data/lib/faraday/adapter/em_synchrony.rb +0 -150
  73. data/lib/faraday/adapter/excon.rb +0 -124
  74. data/lib/faraday/adapter/httpclient.rb +0 -151
  75. data/lib/faraday/adapter/net_http.rb +0 -209
  76. data/lib/faraday/adapter/net_http_persistent.rb +0 -91
  77. data/lib/faraday/adapter/patron.rb +0 -132
  78. data/lib/faraday/adapter/rack.rb +0 -75
  79. data/lib/faraday/adapter/typhoeus.rb +0 -15
  80. data/lib/faraday/autoload.rb +0 -95
  81. data/lib/faraday/dependency_loader.rb +0 -37
  82. data/lib/faraday/file_part.rb +0 -128
  83. data/lib/faraday/param_part.rb +0 -53
  84. data/lib/faraday/request/basic_authentication.rb +0 -20
  85. data/lib/faraday/request/multipart.rb +0 -99
  86. data/lib/faraday/request/retry.rb +0 -239
  87. data/lib/faraday/request/token_authentication.rb +0 -20
  88. data/spec/faraday/adapter/em_http_spec.rb +0 -47
  89. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
  90. data/spec/faraday/adapter/excon_spec.rb +0 -49
  91. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  92. data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
  93. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  94. data/spec/faraday/adapter/patron_spec.rb +0 -18
  95. data/spec/faraday/adapter/rack_spec.rb +0 -8
  96. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  97. data/spec/faraday/composite_read_io_spec.rb +0 -80
  98. data/spec/faraday/request/multipart_spec.rb +0 -274
  99. data/spec/faraday/request/retry_spec.rb +0 -242
  100. data/spec/faraday/response/middleware_spec.rb +0 -52
  101. data/spec/support/webmock_rack_app.rb +0 -68
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@technoweenie"
@@ -10,28 +10,28 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-01-01 00:00:00.000000000 Z
13
+ date: 2024-01-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: multipart-post
16
+ name: faraday-net_http
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: '1.2'
21
+ version: '2.0'
22
22
  - - "<"
23
23
  - !ruby/object:Gem::Version
24
- version: '3'
24
+ version: '3.2'
25
25
  type: :runtime
26
26
  prerelease: false
27
27
  version_requirements: !ruby/object:Gem::Requirement
28
28
  requirements:
29
29
  - - ">="
30
30
  - !ruby/object:Gem::Version
31
- version: '1.2'
31
+ version: '2.0'
32
32
  - - "<"
33
33
  - !ruby/object:Gem::Version
34
- version: '3'
34
+ version: '3.2'
35
35
  description:
36
36
  email: technoweenie@gmail.com
37
37
  executables: []
@@ -42,32 +42,18 @@ files:
42
42
  - LICENSE.md
43
43
  - README.md
44
44
  - Rakefile
45
- - UPGRADING.md
46
45
  - examples/client_spec.rb
47
46
  - examples/client_test.rb
48
47
  - lib/faraday.rb
49
48
  - lib/faraday/adapter.rb
50
- - lib/faraday/adapter/em_http.rb
51
- - lib/faraday/adapter/em_http_ssl_patch.rb
52
- - lib/faraday/adapter/em_synchrony.rb
53
- - lib/faraday/adapter/em_synchrony/parallel_manager.rb
54
- - lib/faraday/adapter/excon.rb
55
- - lib/faraday/adapter/httpclient.rb
56
- - lib/faraday/adapter/net_http.rb
57
- - lib/faraday/adapter/net_http_persistent.rb
58
- - lib/faraday/adapter/patron.rb
59
- - lib/faraday/adapter/rack.rb
60
49
  - lib/faraday/adapter/test.rb
61
- - lib/faraday/adapter/typhoeus.rb
62
50
  - lib/faraday/adapter_registry.rb
63
- - lib/faraday/autoload.rb
64
51
  - lib/faraday/connection.rb
65
- - lib/faraday/dependency_loader.rb
66
52
  - lib/faraday/encoders/flat_params_encoder.rb
67
53
  - lib/faraday/encoders/nested_params_encoder.rb
68
54
  - lib/faraday/error.rb
69
- - lib/faraday/file_part.rb
70
55
  - lib/faraday/logging/formatter.rb
56
+ - lib/faraday/methods.rb
71
57
  - lib/faraday/middleware.rb
72
58
  - lib/faraday/middleware_registry.rb
73
59
  - lib/faraday/options.rb
@@ -76,38 +62,28 @@ files:
76
62
  - lib/faraday/options/proxy_options.rb
77
63
  - lib/faraday/options/request_options.rb
78
64
  - lib/faraday/options/ssl_options.rb
79
- - lib/faraday/param_part.rb
80
65
  - lib/faraday/parameters.rb
81
66
  - lib/faraday/rack_builder.rb
82
67
  - lib/faraday/request.rb
83
68
  - lib/faraday/request/authorization.rb
84
- - lib/faraday/request/basic_authentication.rb
85
69
  - lib/faraday/request/instrumentation.rb
86
- - lib/faraday/request/multipart.rb
87
- - lib/faraday/request/retry.rb
88
- - lib/faraday/request/token_authentication.rb
70
+ - lib/faraday/request/json.rb
89
71
  - lib/faraday/request/url_encoded.rb
90
72
  - lib/faraday/response.rb
73
+ - lib/faraday/response/json.rb
91
74
  - lib/faraday/response/logger.rb
92
75
  - lib/faraday/response/raise_error.rb
93
76
  - lib/faraday/utils.rb
94
77
  - lib/faraday/utils/headers.rb
95
78
  - lib/faraday/utils/params_hash.rb
79
+ - lib/faraday/version.rb
96
80
  - spec/external_adapters/faraday_specs_setup.rb
97
- - spec/faraday/adapter/em_http_spec.rb
98
- - spec/faraday/adapter/em_synchrony_spec.rb
99
- - spec/faraday/adapter/excon_spec.rb
100
- - spec/faraday/adapter/httpclient_spec.rb
101
- - spec/faraday/adapter/net_http_persistent_spec.rb
102
- - spec/faraday/adapter/net_http_spec.rb
103
- - spec/faraday/adapter/patron_spec.rb
104
- - spec/faraday/adapter/rack_spec.rb
105
- - spec/faraday/adapter/typhoeus_spec.rb
81
+ - spec/faraday/adapter/test_spec.rb
106
82
  - spec/faraday/adapter_registry_spec.rb
107
83
  - spec/faraday/adapter_spec.rb
108
- - spec/faraday/composite_read_io_spec.rb
109
84
  - spec/faraday/connection_spec.rb
110
85
  - spec/faraday/error_spec.rb
86
+ - spec/faraday/middleware_registry_spec.rb
111
87
  - spec/faraday/middleware_spec.rb
112
88
  - spec/faraday/options/env_spec.rb
113
89
  - spec/faraday/options/options_spec.rb
@@ -118,12 +94,11 @@ files:
118
94
  - spec/faraday/rack_builder_spec.rb
119
95
  - spec/faraday/request/authorization_spec.rb
120
96
  - spec/faraday/request/instrumentation_spec.rb
121
- - spec/faraday/request/multipart_spec.rb
122
- - spec/faraday/request/retry_spec.rb
97
+ - spec/faraday/request/json_spec.rb
123
98
  - spec/faraday/request/url_encoded_spec.rb
124
99
  - spec/faraday/request_spec.rb
100
+ - spec/faraday/response/json_spec.rb
125
101
  - spec/faraday/response/logger_spec.rb
126
- - spec/faraday/response/middleware_spec.rb
127
102
  - spec/faraday/response/raise_error_spec.rb
128
103
  - spec/faraday/response_spec.rb
129
104
  - spec/faraday/utils/headers_spec.rb
@@ -137,31 +112,31 @@ files:
137
112
  - spec/support/shared_examples/params_encoder.rb
138
113
  - spec/support/shared_examples/request_method.rb
139
114
  - spec/support/streaming_response_checker.rb
140
- - spec/support/webmock_rack_app.rb
141
115
  homepage: https://lostisland.github.io/faraday
142
116
  licenses:
143
117
  - MIT
144
118
  metadata:
145
119
  homepage_uri: https://lostisland.github.io/faraday
146
- changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.0.0
120
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.9.0
147
121
  source_code_uri: https://github.com/lostisland/faraday
148
122
  bug_tracker_uri: https://github.com/lostisland/faraday/issues
149
123
  post_install_message:
150
124
  rdoc_options: []
151
125
  require_paths:
152
126
  - lib
127
+ - spec/external_adapters
153
128
  required_ruby_version: !ruby/object:Gem::Requirement
154
129
  requirements:
155
130
  - - ">="
156
131
  - !ruby/object:Gem::Version
157
- version: '2.3'
132
+ version: '3.0'
158
133
  required_rubygems_version: !ruby/object:Gem::Requirement
159
134
  requirements:
160
135
  - - ">="
161
136
  - !ruby/object:Gem::Version
162
137
  version: '0'
163
138
  requirements: []
164
- rubygems_version: 3.1.2
139
+ rubygems_version: 3.5.3
165
140
  signing_key:
166
141
  specification_version: 4
167
142
  summary: HTTP/REST API client library.
data/UPGRADING.md DELETED
@@ -1,55 +0,0 @@
1
- ## Faraday 1.0
2
-
3
- ### Errors
4
- * Removes sub-class constants definition from `Faraday::Error`. A sub-class (e.g. `ClientError`) was previously accessible
5
- either through the `Faraday` module (e.g. `Faraday::ClientError`) or through the `Faraday::Error` class (e.g. `Faraday::Error::ClientError`).
6
- The latter is no longer available and the former should be used instead, so check your `rescue`s.
7
- * Introduces a new `Faraday::ServerError` (5xx status codes) alongside the existing `Faraday::ClientError` (4xx status codes).
8
- Please note `Faraday::ClientError` was previously used for both.
9
- * Introduces new Errors that describe the most common REST status codes:
10
- * Faraday::BadRequestError (400)
11
- * Faraday::UnauthorizedError (401)
12
- * Faraday::ForbiddenError (403)
13
- * Faraday::ProxyAuthError (407). Please note this raised a `Faraday::ConnectionFailed` before.
14
- * Faraday::ConflictError (409)
15
- * Faraday::UnprocessableEntityError (422)
16
- * The following error classes have changed the hierarchy to better mirror their real-world usage and semantic meaning:
17
- * TimeoutError < ServerError (was < ClientError)
18
- * ConnectionFailed < Error (was < ClientError)
19
- * SSLError < Error (was < ClientError)
20
- * ParsingError < Error (was < ClientError)
21
- * RetriableResponse < Error (was < ClientError)
22
-
23
- ### Custom adapters
24
- If you have written a custom adapter, please be aware that `env.body` is now an alias to the two new properties `request_body` and `response_body`.
25
- This should work without you noticing if your adapter inherits from `Faraday::Adapter` and calls `save_response`, but if it doesn't, then please ensure you set the `status` BEFORE the `body` while processing the response.
26
-
27
- ### Others
28
- * Dropped support for jruby and Rubinius.
29
- * Officially supports Ruby 2.4+
30
- * In order to specify the adapter you now MUST use the `#adapter` method on the connection builder. If you don't do so and your adapter inherits from `Faraday::Adapter` then Faraday will raise an exception. Otherwise, Faraday will automatically push the default adapter at the end of the stack causing your request to be executed twice.
31
- ```ruby
32
- class OfficialAdapter < Faraday::Adapter
33
- ...
34
- end
35
-
36
- class MyAdapter
37
- ...
38
- end
39
-
40
- # This will raise an exception
41
- conn = Faraday.new(...) do |f|
42
- f.use OfficialAdapter
43
- end
44
-
45
- # This will cause Faraday inserting the default adapter at the end of the stack
46
- conn = Faraday.new(...) do |f|
47
- f.use MyAdapter
48
- end
49
-
50
- # You MUST use `adapter` method
51
- conn = Faraday.new(...) do |f|
52
- f.adapter AnyAdapter
53
- end
54
- ```
55
-
@@ -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