faraday 0.11.0 → 1.4.3
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 +5 -5
- data/CHANGELOG.md +380 -0
- data/LICENSE.md +1 -1
- data/README.md +25 -229
- data/Rakefile +7 -0
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday/adapter/httpclient.rb +83 -59
- data/lib/faraday/adapter/patron.rb +92 -36
- data/lib/faraday/adapter/rack.rb +30 -13
- data/lib/faraday/adapter/test.rb +103 -62
- data/lib/faraday/adapter/typhoeus.rb +7 -115
- data/lib/faraday/adapter.rb +77 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +42 -36
- data/lib/faraday/connection.rb +351 -167
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
- data/lib/faraday/error.rb +127 -38
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +19 -25
- 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 +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +58 -207
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -196
- data/lib/faraday/rack_builder.rb +84 -48
- 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 +88 -45
- data/lib/faraday/request/retry.rb +211 -126
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/request.rb +94 -32
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +49 -14
- data/lib/faraday/response.rb +27 -23
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +38 -238
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +124 -187
- 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_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/test_spec.rb +260 -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 +736 -0
- data/spec/faraday/error_spec.rb +60 -0
- data/spec/faraday/middleware_spec.rb +52 -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 +44 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +142 -0
- data/spec/faraday/rack_builder_spec.rb +345 -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 +302 -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 +120 -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 +169 -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 +105 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +262 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +164 -16
- data/lib/faraday/adapter/em_http.rb +0 -243
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -106
- data/lib/faraday/adapter/excon.rb +0 -80
- data/lib/faraday/adapter/net_http.rb +0 -135
- data/lib/faraday/adapter/net_http_persistent.rb +0 -50
- data/lib/faraday/upload_io.rb +0 -67
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Requires Ruby with rspec and faraday gems.
|
4
|
+
# rspec client_spec.rb
|
5
|
+
|
6
|
+
require 'faraday'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
# Example API client
|
10
|
+
class Client
|
11
|
+
def initialize(conn)
|
12
|
+
@conn = conn
|
13
|
+
end
|
14
|
+
|
15
|
+
def sushi(jname)
|
16
|
+
res = @conn.get("/#{jname}")
|
17
|
+
data = JSON.parse(res.body)
|
18
|
+
data['name']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
RSpec.describe Client do
|
23
|
+
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
|
24
|
+
let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }
|
25
|
+
let(:client) { Client.new(conn) }
|
26
|
+
|
27
|
+
it 'parses name' do
|
28
|
+
stubs.get('/ebi') do |env|
|
29
|
+
# optional: you can inspect the Faraday::Env
|
30
|
+
expect(env.url.path).to eq('/ebi')
|
31
|
+
[
|
32
|
+
200,
|
33
|
+
{ 'Content-Type': 'application/javascript' },
|
34
|
+
'{"name": "shrimp"}'
|
35
|
+
]
|
36
|
+
end
|
37
|
+
|
38
|
+
# uncomment to trigger stubs.verify_stubbed_calls failure
|
39
|
+
# stubs.get('/unused') { [404, {}, ''] }
|
40
|
+
|
41
|
+
expect(client.sushi('ebi')).to eq('shrimp')
|
42
|
+
stubs.verify_stubbed_calls
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'handles 404' do
|
46
|
+
stubs.get('/ebi') do
|
47
|
+
[
|
48
|
+
404,
|
49
|
+
{ 'Content-Type': 'application/javascript' },
|
50
|
+
'{}'
|
51
|
+
]
|
52
|
+
end
|
53
|
+
expect(client.sushi('ebi')).to be_nil
|
54
|
+
stubs.verify_stubbed_calls
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'handles exception' do
|
58
|
+
stubs.get('/ebi') do
|
59
|
+
raise Faraday::ConnectionFailed, nil
|
60
|
+
end
|
61
|
+
|
62
|
+
expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
|
63
|
+
stubs.verify_stubbed_calls
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Requires Ruby with test-unit and faraday gems.
|
4
|
+
# ruby client_test.rb
|
5
|
+
|
6
|
+
require 'faraday'
|
7
|
+
require 'json'
|
8
|
+
require 'test/unit'
|
9
|
+
|
10
|
+
# Example API client
|
11
|
+
class Client
|
12
|
+
def initialize(conn)
|
13
|
+
@conn = conn
|
14
|
+
end
|
15
|
+
|
16
|
+
def sushi(jname)
|
17
|
+
res = @conn.get("/#{jname}")
|
18
|
+
data = JSON.parse(res.body)
|
19
|
+
data['name']
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Example API client test
|
24
|
+
class ClientTest < Test::Unit::TestCase
|
25
|
+
def test_sushi_name
|
26
|
+
stubs = Faraday::Adapter::Test::Stubs.new
|
27
|
+
stubs.get('/ebi') do |env|
|
28
|
+
# optional: you can inspect the Faraday::Env
|
29
|
+
assert_equal '/ebi', env.url.path
|
30
|
+
[
|
31
|
+
200,
|
32
|
+
{ 'Content-Type': 'application/javascript' },
|
33
|
+
'{"name": "shrimp"}'
|
34
|
+
]
|
35
|
+
end
|
36
|
+
|
37
|
+
# uncomment to trigger stubs.verify_stubbed_calls failure
|
38
|
+
# stubs.get('/unused') { [404, {}, ''] }
|
39
|
+
|
40
|
+
cli = client(stubs)
|
41
|
+
assert_equal 'shrimp', cli.sushi('ebi')
|
42
|
+
stubs.verify_stubbed_calls
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_sushi_404
|
46
|
+
stubs = Faraday::Adapter::Test::Stubs.new
|
47
|
+
stubs.get('/ebi') do
|
48
|
+
[
|
49
|
+
404,
|
50
|
+
{ 'Content-Type': 'application/javascript' },
|
51
|
+
'{}'
|
52
|
+
]
|
53
|
+
end
|
54
|
+
|
55
|
+
cli = client(stubs)
|
56
|
+
assert_nil cli.sushi('ebi')
|
57
|
+
stubs.verify_stubbed_calls
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_sushi_exception
|
61
|
+
stubs = Faraday::Adapter::Test::Stubs.new
|
62
|
+
stubs.get('/ebi') do
|
63
|
+
raise Faraday::ConnectionFailed, nil
|
64
|
+
end
|
65
|
+
|
66
|
+
cli = client(stubs)
|
67
|
+
assert_raise Faraday::ConnectionFailed do
|
68
|
+
cli.sushi('ebi')
|
69
|
+
end
|
70
|
+
stubs.verify_stubbed_calls
|
71
|
+
end
|
72
|
+
|
73
|
+
def client(stubs)
|
74
|
+
conn = Faraday.new do |builder|
|
75
|
+
builder.adapter :test, stubs
|
76
|
+
end
|
77
|
+
Client.new(conn)
|
78
|
+
end
|
79
|
+
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
|
@@ -1,53 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
4
|
class Adapter
|
5
|
+
# Patron adapter.
|
3
6
|
class Patron < Faraday::Adapter
|
4
7
|
dependency 'patron'
|
5
8
|
|
9
|
+
def build_connection(env)
|
10
|
+
session = ::Patron::Session.new
|
11
|
+
@config_block&.call(session)
|
12
|
+
if (env[:url].scheme == 'https') && env[:ssl]
|
13
|
+
configure_ssl(session, env[:ssl])
|
14
|
+
end
|
15
|
+
|
16
|
+
if (req = env[:request])
|
17
|
+
configure_timeouts(session, req)
|
18
|
+
configure_proxy(session, req[:proxy])
|
19
|
+
end
|
20
|
+
|
21
|
+
session
|
22
|
+
end
|
23
|
+
|
6
24
|
def call(env)
|
7
25
|
super
|
8
|
-
|
9
26
|
# TODO: support streaming requests
|
10
27
|
env[:body] = env[:body].read if env[:body].respond_to? :read
|
11
28
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
proxy_uri = proxy[:uri].dup
|
20
|
-
proxy_uri.user = proxy[:user] && Utils.escape(proxy[:user]).gsub('+', '%20')
|
21
|
-
proxy_uri.password = proxy[:password] && Utils.escape(proxy[:password]).gsub('+', '%20')
|
22
|
-
session.proxy = proxy_uri.to_s
|
29
|
+
response = connection(env) do |session|
|
30
|
+
begin
|
31
|
+
data = env[:body] ? env[:body].to_s : nil
|
32
|
+
session.request(env[:method], env[:url].to_s,
|
33
|
+
env[:request_headers], data: data)
|
34
|
+
rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
|
35
|
+
raise Faraday::ConnectionFailed, $ERROR_INFO
|
23
36
|
end
|
24
37
|
end
|
25
38
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
raise Error::ConnectionFailed, $!
|
39
|
+
if (req = env[:request]).stream_response?
|
40
|
+
warn "Streaming downloads for #{self.class.name} " \
|
41
|
+
'are not yet implemented.'
|
42
|
+
req.on_data.call(response.body, response.body.bytesize)
|
31
43
|
end
|
32
|
-
|
33
44
|
# Remove the "HTTP/1.1 200", leaving just the reason phrase
|
34
45
|
reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
|
35
46
|
|
36
|
-
save_response(env, response.status, response.body,
|
47
|
+
save_response(env, response.status, response.body,
|
48
|
+
response.headers, reason_phrase)
|
37
49
|
|
38
50
|
@app.call env
|
39
|
-
rescue ::Patron::TimeoutError =>
|
40
|
-
if
|
41
|
-
raise Faraday::
|
42
|
-
else
|
43
|
-
raise Faraday::Error::TimeoutError, err
|
51
|
+
rescue ::Patron::TimeoutError => e
|
52
|
+
if connection_timed_out_message?(e.message)
|
53
|
+
raise Faraday::ConnectionFailed, e
|
44
54
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
raise
|
55
|
+
|
56
|
+
raise Faraday::TimeoutError, e
|
57
|
+
rescue ::Patron::Error => e
|
58
|
+
if e.message.include?('code 407')
|
59
|
+
raise Faraday::ConnectionFailed,
|
60
|
+
%(407 "Proxy Authentication Required ")
|
50
61
|
end
|
62
|
+
|
63
|
+
raise Faraday::ConnectionFailed, e
|
51
64
|
end
|
52
65
|
|
53
66
|
if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
|
@@ -59,17 +72,60 @@ module Faraday
|
|
59
72
|
actions << :options unless actions.include? :options
|
60
73
|
else
|
61
74
|
# Patron 0.4.20 and up
|
62
|
-
actions <<
|
63
|
-
actions <<
|
75
|
+
actions << 'PATCH' unless actions.include? 'PATCH'
|
76
|
+
actions << 'OPTIONS' unless actions.include? 'OPTIONS'
|
64
77
|
end
|
65
78
|
end
|
66
79
|
end
|
67
80
|
|
68
|
-
def
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
81
|
+
def configure_ssl(session, ssl)
|
82
|
+
if ssl.fetch(:verify, true)
|
83
|
+
session.cacert = ssl[:ca_file]
|
84
|
+
else
|
85
|
+
session.insecure = true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def configure_timeouts(session, req)
|
90
|
+
return unless req
|
91
|
+
|
92
|
+
if (sec = request_timeout(:read, req))
|
93
|
+
session.timeout = sec
|
94
|
+
end
|
95
|
+
|
96
|
+
return unless (sec = request_timeout(:open, req))
|
97
|
+
|
98
|
+
session.connect_timeout = sec
|
99
|
+
end
|
100
|
+
|
101
|
+
def configure_proxy(session, proxy)
|
102
|
+
return unless proxy
|
103
|
+
|
104
|
+
proxy_uri = proxy[:uri].dup
|
105
|
+
proxy_uri.user = proxy[:user] &&
|
106
|
+
Utils.escape(proxy[:user]).gsub('+', '%20')
|
107
|
+
proxy_uri.password = proxy[:password] &&
|
108
|
+
Utils.escape(proxy[:password]).gsub('+', '%20')
|
109
|
+
session.proxy = proxy_uri.to_s
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
CURL_TIMEOUT_MESSAGES = [
|
115
|
+
'Connection time-out',
|
116
|
+
'Connection timed out',
|
117
|
+
'Timed out before name resolve',
|
118
|
+
'server connect has timed out',
|
119
|
+
'Resolving timed out',
|
120
|
+
'name lookup timed out',
|
121
|
+
'timed out before SSL',
|
122
|
+
'connect() timed out'
|
123
|
+
].freeze
|
124
|
+
|
125
|
+
def connection_timed_out_message?(message)
|
126
|
+
CURL_TIMEOUT_MESSAGES.any? do |curl_message|
|
127
|
+
message.include?(curl_message)
|
128
|
+
end
|
73
129
|
end
|
74
130
|
end
|
75
131
|
end
|
data/lib/faraday/adapter/rack.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
4
|
class Adapter
|
3
5
|
# Sends requests to a Rack app.
|
4
6
|
#
|
5
|
-
#
|
7
|
+
# @example
|
6
8
|
#
|
7
9
|
# class MyRackApp
|
8
10
|
# def call(env)
|
@@ -17,7 +19,7 @@ module Faraday
|
|
17
19
|
dependency 'rack/test'
|
18
20
|
|
19
21
|
# not prefixed with "HTTP_"
|
20
|
-
SPECIAL_HEADERS = %w[
|
22
|
+
SPECIAL_HEADERS = %w[CONTENT_LENGTH CONTENT_TYPE].freeze
|
21
23
|
|
22
24
|
def initialize(faraday_app, rack_app)
|
23
25
|
super(faraday_app)
|
@@ -27,32 +29,47 @@ module Faraday
|
|
27
29
|
|
28
30
|
def call(env)
|
29
31
|
super
|
30
|
-
rack_env =
|
31
|
-
:method => env[:method],
|
32
|
-
:input => env[:body].respond_to?(:read) ? env[:body].read : env[:body],
|
33
|
-
'rack.url_scheme' => env[:url].scheme
|
34
|
-
}
|
32
|
+
rack_env = build_rack_env(env)
|
35
33
|
|
36
|
-
env[:request_headers]
|
34
|
+
env[:request_headers]&.each do |name, value|
|
37
35
|
name = name.upcase.tr('-', '_')
|
38
36
|
name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
|
39
37
|
rack_env[name] = value
|
40
|
-
end
|
38
|
+
end
|
41
39
|
|
42
|
-
timeout
|
40
|
+
timeout = request_timeout(:open, env[:request])
|
41
|
+
timeout ||= request_timeout(:read, env[:request])
|
43
42
|
response = if timeout
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
Timer.timeout(timeout, Faraday::TimeoutError) do
|
44
|
+
execute_request(env, rack_env)
|
45
|
+
end
|
46
|
+
else
|
47
|
+
execute_request(env, rack_env)
|
48
|
+
end
|
49
|
+
|
50
|
+
if (req = env[:request]).stream_response?
|
51
|
+
warn "Streaming downloads for #{self.class.name} " \
|
52
|
+
'are not yet implemented.'
|
53
|
+
req.on_data.call(response.body, response.body.bytesize)
|
47
54
|
end
|
48
55
|
|
49
56
|
save_response(env, response.status, response.body, response.headers)
|
50
57
|
@app.call env
|
51
58
|
end
|
52
59
|
|
60
|
+
private
|
61
|
+
|
53
62
|
def execute_request(env, rack_env)
|
54
63
|
@session.request(env[:url].to_s, rack_env)
|
55
64
|
end
|
65
|
+
|
66
|
+
def build_rack_env(env)
|
67
|
+
{
|
68
|
+
method: env[:method],
|
69
|
+
input: env[:body].respond_to?(:read) ? env[:body].read : env[:body],
|
70
|
+
'rack.url_scheme' => env[:url].scheme
|
71
|
+
}
|
72
|
+
end
|
56
73
|
end
|
57
74
|
end
|
58
75
|
end
|