faraday 1.1.0 → 2.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +299 -1
- data/LICENSE.md +1 -1
- data/README.md +18 -17
- data/examples/client_spec.rb +67 -13
- data/examples/client_test.rb +80 -15
- data/lib/faraday/adapter/test.rb +117 -52
- data/lib/faraday/adapter.rb +5 -20
- data/lib/faraday/connection.rb +64 -118
- data/lib/faraday/encoders/nested_params_encoder.rb +13 -6
- data/lib/faraday/error.rb +15 -8
- data/lib/faraday/logging/formatter.rb +19 -2
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +17 -5
- data/lib/faraday/middleware_registry.rb +17 -63
- data/lib/faraday/options/env.rb +31 -7
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/options/ssl_options.rb +11 -1
- data/lib/faraday/options.rb +3 -3
- data/lib/faraday/rack_builder.rb +23 -20
- data/lib/faraday/request/authorization.rb +37 -38
- data/lib/faraday/request/instrumentation.rb +2 -0
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +5 -1
- data/lib/faraday/request.rb +12 -32
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +8 -4
- data/lib/faraday/response/raise_error.rb +9 -1
- data/lib/faraday/response.rb +10 -26
- data/lib/faraday/utils/headers.rb +7 -2
- data/lib/faraday/utils.rb +10 -5
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +49 -58
- data/spec/faraday/adapter/test_spec.rb +182 -0
- data/spec/faraday/connection_spec.rb +207 -90
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/middleware_registry_spec.rb +31 -0
- data/spec/faraday/middleware_spec.rb +50 -6
- data/spec/faraday/options/env_spec.rb +8 -2
- data/spec/faraday/options/proxy_options_spec.rb +7 -0
- data/spec/faraday/params_encoders/nested_spec.rb +8 -0
- data/spec/faraday/rack_builder_spec.rb +26 -54
- data/spec/faraday/request/authorization_spec.rb +54 -24
- data/spec/faraday/request/instrumentation_spec.rb +5 -7
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +12 -2
- data/spec/faraday/request_spec.rb +5 -15
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/logger_spec.rb +28 -0
- data/spec/faraday/response/raise_error_spec.rb +37 -4
- data/spec/faraday/response_spec.rb +3 -1
- data/spec/faraday/utils/headers_spec.rb +22 -4
- data/spec/faraday/utils_spec.rb +63 -1
- data/spec/support/fake_safe_buffer.rb +1 -1
- data/spec/support/helper_methods.rb +0 -37
- data/spec/support/shared_examples/adapter.rb +4 -3
- data/spec/support/shared_examples/request_method.rb +58 -29
- metadata +19 -45
- data/lib/faraday/adapter/em_http.rb +0 -286
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
- data/lib/faraday/adapter/em_synchrony.rb +0 -150
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/httpclient.rb +0 -152
- data/lib/faraday/adapter/net_http.rb +0 -219
- data/lib/faraday/adapter/net_http_persistent.rb +0 -91
- data/lib/faraday/adapter/patron.rb +0 -132
- data/lib/faraday/adapter/rack.rb +0 -75
- data/lib/faraday/adapter/typhoeus.rb +0 -15
- data/lib/faraday/autoload.rb +0 -95
- data/lib/faraday/dependency_loader.rb +0 -39
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/basic_authentication.rb +0 -20
- data/lib/faraday/request/multipart.rb +0 -106
- data/lib/faraday/request/retry.rb +0 -239
- data/lib/faraday/request/token_authentication.rb +0 -20
- data/spec/faraday/adapter/em_http_spec.rb +0 -47
- data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
- data/spec/faraday/adapter/excon_spec.rb +0 -49
- data/spec/faraday/adapter/httpclient_spec.rb +0 -73
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
- data/spec/faraday/adapter/net_http_spec.rb +0 -64
- data/spec/faraday/adapter/patron_spec.rb +0 -18
- data/spec/faraday/adapter/rack_spec.rb +0 -8
- data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
- data/spec/faraday/composite_read_io_spec.rb +0 -80
- data/spec/faraday/request/multipart_spec.rb +0 -302
- data/spec/faraday/request/retry_spec.rb +0 -242
- data/spec/faraday/response/middleware_spec.rb +0 -68
- data/spec/support/webmock_rack_app.rb +0 -68
@@ -1,5 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
shared_examples 'proxy examples' do
|
4
|
+
it 'handles requests with proxy' do
|
5
|
+
res = conn.public_send(http_method, '/')
|
6
|
+
|
7
|
+
expect(res.status).to eq(200)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'handles proxy failures' do
|
11
|
+
request_stub.to_return(status: 407)
|
12
|
+
|
13
|
+
expect { conn.public_send(http_method, '/') }.to raise_error(Faraday::ProxyAuthError)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
3
17
|
shared_examples 'a request method' do |http_method|
|
4
18
|
let(:query_or_body) { method_with_body?(http_method) ? :body : :query }
|
5
19
|
let(:response) { conn.public_send(http_method, '/') }
|
@@ -65,7 +79,7 @@ shared_examples 'a request method' do |http_method|
|
|
65
79
|
|
66
80
|
on_feature :request_body_on_query_methods do
|
67
81
|
it 'sends request body' do
|
68
|
-
request_stub.with(
|
82
|
+
request_stub.with({ body: 'test' })
|
69
83
|
res = if query_or_body == :body
|
70
84
|
conn.public_send(http_method, '/', 'test')
|
71
85
|
else
|
@@ -79,7 +93,7 @@ shared_examples 'a request method' do |http_method|
|
|
79
93
|
|
80
94
|
it 'sends url encoded parameters' do
|
81
95
|
payload = { name: 'zack' }
|
82
|
-
request_stub.with(
|
96
|
+
request_stub.with({ query_or_body => payload })
|
83
97
|
res = conn.public_send(http_method, '/', payload)
|
84
98
|
if query_or_body == :query
|
85
99
|
expect(res.env.request_body).to be_nil
|
@@ -90,7 +104,7 @@ shared_examples 'a request method' do |http_method|
|
|
90
104
|
|
91
105
|
it 'sends url encoded nested parameters' do
|
92
106
|
payload = { name: { first: 'zack' } }
|
93
|
-
request_stub.with(
|
107
|
+
request_stub.with({ query_or_body => payload })
|
94
108
|
conn.public_send(http_method, '/', payload)
|
95
109
|
end
|
96
110
|
|
@@ -112,19 +126,6 @@ shared_examples 'a request method' do |http_method|
|
|
112
126
|
expect { conn.public_send(http_method, '/') }.to raise_error(exc)
|
113
127
|
end
|
114
128
|
|
115
|
-
# Can't send files on get, head and delete methods
|
116
|
-
if method_with_body?(http_method)
|
117
|
-
it 'sends files' do
|
118
|
-
payload = { uploaded_file: multipart_file }
|
119
|
-
request_stub.with(headers: { 'Content-Type' => %r{\Amultipart/form-data} }) do |request|
|
120
|
-
# WebMock does not support matching body for multipart/form-data requests yet :(
|
121
|
-
# https://github.com/bblimke/webmock/issues/623
|
122
|
-
request.body.include?('RubyMultipartPost')
|
123
|
-
end
|
124
|
-
conn.public_send(http_method, '/', payload)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
129
|
on_feature :reason_phrase_parse do
|
129
130
|
it 'parses the reason phrase' do
|
130
131
|
request_stub.to_return(status: [200, 'OK'])
|
@@ -152,12 +153,19 @@ shared_examples 'a request method' do |http_method|
|
|
152
153
|
let(:streamed) { [] }
|
153
154
|
|
154
155
|
context 'when response is empty' do
|
155
|
-
it do
|
156
|
+
it 'handles streaming' do
|
157
|
+
env = nil
|
156
158
|
conn.public_send(http_method, '/') do |req|
|
157
|
-
req.options.on_data = proc
|
159
|
+
req.options.on_data = proc do |chunk, size, block_env|
|
160
|
+
streamed << [chunk, size]
|
161
|
+
env ||= block_env
|
162
|
+
end
|
158
163
|
end
|
159
164
|
|
160
165
|
expect(streamed).to eq([['', 0]])
|
166
|
+
# TODO: enable this after updating all existing adapters to the new streaming API
|
167
|
+
# expect(env).to be_a(Faraday::Env)
|
168
|
+
# expect(env.status).to eq(200)
|
161
169
|
end
|
162
170
|
end
|
163
171
|
|
@@ -165,12 +173,19 @@ shared_examples 'a request method' do |http_method|
|
|
165
173
|
before { request_stub.to_return(body: big_string) }
|
166
174
|
|
167
175
|
it 'handles streaming' do
|
176
|
+
env = nil
|
168
177
|
response = conn.public_send(http_method, '/') do |req|
|
169
|
-
req.options.on_data = proc
|
178
|
+
req.options.on_data = proc do |chunk, size, block_env|
|
179
|
+
streamed << [chunk, size]
|
180
|
+
env ||= block_env
|
181
|
+
end
|
170
182
|
end
|
171
183
|
|
172
184
|
expect(response.body).to eq('')
|
173
185
|
check_streaming_response(streamed, chunk_size: 16 * 1024)
|
186
|
+
# TODO: enable this after updating all existing adapters to the new streaming API
|
187
|
+
# expect(env).to be_a(Faraday::Env)
|
188
|
+
# expect(env.status).to eq(200)
|
174
189
|
end
|
175
190
|
end
|
176
191
|
end
|
@@ -185,11 +200,11 @@ shared_examples 'a request method' do |http_method|
|
|
185
200
|
@payload2 = { b: '2' }
|
186
201
|
|
187
202
|
request_stub
|
188
|
-
.with(
|
203
|
+
.with({ query_or_body => @payload1 })
|
189
204
|
.to_return(body: @payload1.to_json)
|
190
205
|
|
191
206
|
stub_request(http_method, remote)
|
192
|
-
.with(
|
207
|
+
.with({ query_or_body => @payload2 })
|
193
208
|
.to_return(body: @payload2.to_json)
|
194
209
|
|
195
210
|
conn.in_parallel do
|
@@ -218,17 +233,31 @@ shared_examples 'a request method' do |http_method|
|
|
218
233
|
end
|
219
234
|
end
|
220
235
|
|
221
|
-
|
222
|
-
|
236
|
+
context 'when a proxy is provided as option' do
|
237
|
+
before do
|
238
|
+
conn_options[:proxy] = 'http://env-proxy.com:80'
|
239
|
+
end
|
223
240
|
|
224
|
-
|
225
|
-
expect(res.status).to eq(200)
|
241
|
+
include_examples 'proxy examples'
|
226
242
|
end
|
227
243
|
|
228
|
-
|
229
|
-
|
230
|
-
request_stub.to_return(status: 407)
|
244
|
+
context 'when http_proxy env variable is set' do
|
245
|
+
let(:proxy_url) { 'http://env-proxy.com:80' }
|
231
246
|
|
232
|
-
|
247
|
+
around do |example|
|
248
|
+
with_env 'http_proxy' => proxy_url do
|
249
|
+
example.run
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
include_examples 'proxy examples'
|
254
|
+
|
255
|
+
context 'when the env proxy is ignored' do
|
256
|
+
around do |example|
|
257
|
+
with_env_proxy_disabled(&example)
|
258
|
+
end
|
259
|
+
|
260
|
+
include_examples 'proxy examples'
|
261
|
+
end
|
233
262
|
end
|
234
263
|
end
|
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:
|
4
|
+
version: 2.7.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "@technoweenie"
|
@@ -10,42 +10,42 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2023-01-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
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: '
|
21
|
+
version: '2.0'
|
22
22
|
- - "<"
|
23
23
|
- !ruby/object:Gem::Version
|
24
|
-
version: '3'
|
24
|
+
version: '3.1'
|
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: '
|
31
|
+
version: '2.0'
|
32
32
|
- - "<"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '3'
|
34
|
+
version: '3.1'
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: ruby2_keywords
|
37
37
|
requirement: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
41
|
+
version: 0.0.4
|
42
42
|
type: :runtime
|
43
43
|
prerelease: false
|
44
44
|
version_requirements: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
46
|
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version:
|
48
|
+
version: 0.0.4
|
49
49
|
description:
|
50
50
|
email: technoweenie@gmail.com
|
51
51
|
executables: []
|
@@ -60,27 +60,14 @@ files:
|
|
60
60
|
- examples/client_test.rb
|
61
61
|
- lib/faraday.rb
|
62
62
|
- lib/faraday/adapter.rb
|
63
|
-
- lib/faraday/adapter/em_http.rb
|
64
|
-
- lib/faraday/adapter/em_http_ssl_patch.rb
|
65
|
-
- lib/faraday/adapter/em_synchrony.rb
|
66
|
-
- lib/faraday/adapter/em_synchrony/parallel_manager.rb
|
67
|
-
- lib/faraday/adapter/excon.rb
|
68
|
-
- lib/faraday/adapter/httpclient.rb
|
69
|
-
- lib/faraday/adapter/net_http.rb
|
70
|
-
- lib/faraday/adapter/net_http_persistent.rb
|
71
|
-
- lib/faraday/adapter/patron.rb
|
72
|
-
- lib/faraday/adapter/rack.rb
|
73
63
|
- lib/faraday/adapter/test.rb
|
74
|
-
- lib/faraday/adapter/typhoeus.rb
|
75
64
|
- lib/faraday/adapter_registry.rb
|
76
|
-
- lib/faraday/autoload.rb
|
77
65
|
- lib/faraday/connection.rb
|
78
|
-
- lib/faraday/dependency_loader.rb
|
79
66
|
- lib/faraday/encoders/flat_params_encoder.rb
|
80
67
|
- lib/faraday/encoders/nested_params_encoder.rb
|
81
68
|
- lib/faraday/error.rb
|
82
|
-
- lib/faraday/file_part.rb
|
83
69
|
- lib/faraday/logging/formatter.rb
|
70
|
+
- lib/faraday/methods.rb
|
84
71
|
- lib/faraday/middleware.rb
|
85
72
|
- lib/faraday/middleware_registry.rb
|
86
73
|
- lib/faraday/options.rb
|
@@ -89,39 +76,28 @@ files:
|
|
89
76
|
- lib/faraday/options/proxy_options.rb
|
90
77
|
- lib/faraday/options/request_options.rb
|
91
78
|
- lib/faraday/options/ssl_options.rb
|
92
|
-
- lib/faraday/param_part.rb
|
93
79
|
- lib/faraday/parameters.rb
|
94
80
|
- lib/faraday/rack_builder.rb
|
95
81
|
- lib/faraday/request.rb
|
96
82
|
- lib/faraday/request/authorization.rb
|
97
|
-
- lib/faraday/request/basic_authentication.rb
|
98
83
|
- lib/faraday/request/instrumentation.rb
|
99
|
-
- lib/faraday/request/
|
100
|
-
- lib/faraday/request/retry.rb
|
101
|
-
- lib/faraday/request/token_authentication.rb
|
84
|
+
- lib/faraday/request/json.rb
|
102
85
|
- lib/faraday/request/url_encoded.rb
|
103
86
|
- lib/faraday/response.rb
|
87
|
+
- lib/faraday/response/json.rb
|
104
88
|
- lib/faraday/response/logger.rb
|
105
89
|
- lib/faraday/response/raise_error.rb
|
106
90
|
- lib/faraday/utils.rb
|
107
91
|
- lib/faraday/utils/headers.rb
|
108
92
|
- lib/faraday/utils/params_hash.rb
|
93
|
+
- lib/faraday/version.rb
|
109
94
|
- spec/external_adapters/faraday_specs_setup.rb
|
110
|
-
- spec/faraday/adapter/em_http_spec.rb
|
111
|
-
- spec/faraday/adapter/em_synchrony_spec.rb
|
112
|
-
- spec/faraday/adapter/excon_spec.rb
|
113
|
-
- spec/faraday/adapter/httpclient_spec.rb
|
114
|
-
- spec/faraday/adapter/net_http_persistent_spec.rb
|
115
|
-
- spec/faraday/adapter/net_http_spec.rb
|
116
|
-
- spec/faraday/adapter/patron_spec.rb
|
117
|
-
- spec/faraday/adapter/rack_spec.rb
|
118
95
|
- spec/faraday/adapter/test_spec.rb
|
119
|
-
- spec/faraday/adapter/typhoeus_spec.rb
|
120
96
|
- spec/faraday/adapter_registry_spec.rb
|
121
97
|
- spec/faraday/adapter_spec.rb
|
122
|
-
- spec/faraday/composite_read_io_spec.rb
|
123
98
|
- spec/faraday/connection_spec.rb
|
124
99
|
- spec/faraday/error_spec.rb
|
100
|
+
- spec/faraday/middleware_registry_spec.rb
|
125
101
|
- spec/faraday/middleware_spec.rb
|
126
102
|
- spec/faraday/options/env_spec.rb
|
127
103
|
- spec/faraday/options/options_spec.rb
|
@@ -132,12 +108,11 @@ files:
|
|
132
108
|
- spec/faraday/rack_builder_spec.rb
|
133
109
|
- spec/faraday/request/authorization_spec.rb
|
134
110
|
- spec/faraday/request/instrumentation_spec.rb
|
135
|
-
- spec/faraday/request/
|
136
|
-
- spec/faraday/request/retry_spec.rb
|
111
|
+
- spec/faraday/request/json_spec.rb
|
137
112
|
- spec/faraday/request/url_encoded_spec.rb
|
138
113
|
- spec/faraday/request_spec.rb
|
114
|
+
- spec/faraday/response/json_spec.rb
|
139
115
|
- spec/faraday/response/logger_spec.rb
|
140
|
-
- spec/faraday/response/middleware_spec.rb
|
141
116
|
- spec/faraday/response/raise_error_spec.rb
|
142
117
|
- spec/faraday/response_spec.rb
|
143
118
|
- spec/faraday/utils/headers_spec.rb
|
@@ -151,13 +126,12 @@ files:
|
|
151
126
|
- spec/support/shared_examples/params_encoder.rb
|
152
127
|
- spec/support/shared_examples/request_method.rb
|
153
128
|
- spec/support/streaming_response_checker.rb
|
154
|
-
- spec/support/webmock_rack_app.rb
|
155
129
|
homepage: https://lostisland.github.io/faraday
|
156
130
|
licenses:
|
157
131
|
- MIT
|
158
132
|
metadata:
|
159
133
|
homepage_uri: https://lostisland.github.io/faraday
|
160
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/
|
134
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.7.4
|
161
135
|
source_code_uri: https://github.com/lostisland/faraday
|
162
136
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
163
137
|
post_install_message:
|
@@ -169,14 +143,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
169
143
|
requirements:
|
170
144
|
- - ">="
|
171
145
|
- !ruby/object:Gem::Version
|
172
|
-
version: '2.
|
146
|
+
version: '2.6'
|
173
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
148
|
requirements:
|
175
149
|
- - ">="
|
176
150
|
- !ruby/object:Gem::Version
|
177
151
|
version: '0'
|
178
152
|
requirements: []
|
179
|
-
rubygems_version: 3.
|
153
|
+
rubygems_version: 3.1.6
|
180
154
|
signing_key:
|
181
155
|
specification_version: 4
|
182
156
|
summary: HTTP/REST API client library.
|
@@ -1,286 +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::SSL::SSLError) && \
|
146
|
-
e.is_a?(::OpenSSL::SSL::SSLError)
|
147
|
-
raise Faraday::SSLError, e
|
148
|
-
end
|
149
|
-
|
150
|
-
raise
|
151
|
-
end
|
152
|
-
|
153
|
-
# TODO: reuse the connection to support pipelining
|
154
|
-
def perform_single_request(env)
|
155
|
-
req = create_request(env)
|
156
|
-
req = req.setup_request(env[:method], request_config(env))
|
157
|
-
req.callback do |client|
|
158
|
-
if env[:request].stream_response?
|
159
|
-
warn "Streaming downloads for #{self.class.name} " \
|
160
|
-
'are not yet implemented.'
|
161
|
-
env[:request].on_data.call(
|
162
|
-
client.response,
|
163
|
-
client.response.bytesize
|
164
|
-
)
|
165
|
-
end
|
166
|
-
status = client.response_header.status
|
167
|
-
reason = client.response_header.http_reason
|
168
|
-
save_response(env, status, client.response, nil, reason) do |headers|
|
169
|
-
client.response_header.each do |name, value|
|
170
|
-
headers[name.to_sym] = value
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
def create_request(env)
|
177
|
-
EventMachine::HttpRequest.new(
|
178
|
-
env[:url], connection_config(env).merge(@connection_options)
|
179
|
-
)
|
180
|
-
end
|
181
|
-
|
182
|
-
def error_message(client)
|
183
|
-
client.error || 'request failed'
|
184
|
-
end
|
185
|
-
|
186
|
-
def raise_error(msg)
|
187
|
-
error_class = Faraday::ClientError
|
188
|
-
if timeout_message?(msg)
|
189
|
-
error_class = Faraday::TimeoutError
|
190
|
-
msg = 'request timed out'
|
191
|
-
elsif msg == Errno::ECONNREFUSED
|
192
|
-
error_class = Faraday::ConnectionFailed
|
193
|
-
msg = 'connection refused'
|
194
|
-
elsif msg == 'connection closed by server'
|
195
|
-
error_class = Faraday::ConnectionFailed
|
196
|
-
end
|
197
|
-
raise error_class, msg
|
198
|
-
end
|
199
|
-
|
200
|
-
def timeout_message?(msg)
|
201
|
-
msg == Errno::ETIMEDOUT ||
|
202
|
-
(msg.is_a?(String) && msg.include?('timeout error'))
|
203
|
-
end
|
204
|
-
|
205
|
-
# @return [Boolean]
|
206
|
-
def parallel?(env)
|
207
|
-
!!env[:parallel_manager]
|
208
|
-
end
|
209
|
-
|
210
|
-
# This parallel manager is designed to start an EventMachine loop
|
211
|
-
# and block until all registered requests have been completed.
|
212
|
-
class Manager
|
213
|
-
# @see reset
|
214
|
-
def initialize
|
215
|
-
reset
|
216
|
-
end
|
217
|
-
|
218
|
-
# Re-initializes instance variables
|
219
|
-
def reset
|
220
|
-
@registered_procs = []
|
221
|
-
@num_registered = 0
|
222
|
-
@num_succeeded = 0
|
223
|
-
@errors = []
|
224
|
-
@running = false
|
225
|
-
end
|
226
|
-
|
227
|
-
# @return [Boolean]
|
228
|
-
def running?
|
229
|
-
@running
|
230
|
-
end
|
231
|
-
|
232
|
-
def add(&block)
|
233
|
-
if running?
|
234
|
-
perform_request(&block)
|
235
|
-
else
|
236
|
-
@registered_procs << block
|
237
|
-
end
|
238
|
-
@num_registered += 1
|
239
|
-
end
|
240
|
-
|
241
|
-
def run
|
242
|
-
if @num_registered.positive?
|
243
|
-
@running = true
|
244
|
-
EventMachine.run do
|
245
|
-
@registered_procs.each do |proc|
|
246
|
-
perform_request(&proc)
|
247
|
-
end
|
248
|
-
end
|
249
|
-
unless @errors.empty?
|
250
|
-
raise Faraday::ClientError, @errors.first || 'connection failed'
|
251
|
-
end
|
252
|
-
end
|
253
|
-
ensure
|
254
|
-
reset
|
255
|
-
end
|
256
|
-
|
257
|
-
def perform_request
|
258
|
-
client = yield
|
259
|
-
client.callback do
|
260
|
-
@num_succeeded += 1
|
261
|
-
check_finished
|
262
|
-
end
|
263
|
-
client.errback do
|
264
|
-
@errors << client.error
|
265
|
-
check_finished
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
def check_finished
|
270
|
-
EventMachine.stop if @num_succeeded + @errors.size == @num_registered
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
if Faraday::Adapter::EMHttp.loaded?
|
278
|
-
begin
|
279
|
-
require 'openssl'
|
280
|
-
rescue LoadError
|
281
|
-
warn 'Warning: no such file to load -- openssl. ' \
|
282
|
-
'Make sure it is installed if you want HTTPS support'
|
283
|
-
else
|
284
|
-
require 'faraday/adapter/em_http_ssl_patch'
|
285
|
-
end
|
286
|
-
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)
|