faraday 0.15.0 → 1.3.0
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 +350 -0
- data/LICENSE.md +1 -1
- data/README.md +18 -345
- data/Rakefile +7 -0
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday.rb +118 -190
- data/lib/faraday/adapter.rb +82 -22
- data/lib/faraday/adapter/em_http.rb +150 -104
- data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
- data/lib/faraday/adapter/em_synchrony.rb +110 -63
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/excon.rb +98 -53
- data/lib/faraday/adapter/httpclient.rb +83 -59
- data/lib/faraday/adapter/net_http_persistent.rb +57 -29
- data/lib/faraday/adapter/patron.rb +80 -48
- data/lib/faraday/adapter/rack.rb +30 -13
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +46 -36
- data/lib/faraday/connection.rb +312 -182
- 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 +123 -37
- 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.rb +39 -193
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +28 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -196
- data/lib/faraday/rack_builder.rb +77 -65
- data/lib/faraday/request.rb +94 -32
- 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 +86 -48
- data/lib/faraday/request/retry.rb +198 -170
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/response.rb +27 -23
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +49 -14
- data/lib/faraday/utils.rb +38 -247
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/version.rb +5 -0
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +47 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/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 +691 -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 +37 -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 +109 -10
- data/lib/faraday/adapter/net_http.rb +0 -137
- data/lib/faraday/upload_io.rb +0 -67
@@ -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,62 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
4
|
class Adapter
|
5
|
+
# Net::HTTP::Persistent adapter.
|
3
6
|
class NetHttpPersistent < NetHttp
|
4
7
|
dependency 'net/http/persistent'
|
5
8
|
|
6
9
|
private
|
7
10
|
|
8
11
|
def net_http_connection(env)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
@cached_connection ||=
|
13
|
+
if Net::HTTP::Persistent.instance_method(:initialize)
|
14
|
+
.parameters.first == %i[key name]
|
15
|
+
options = { name: 'Faraday' }
|
16
|
+
if @connection_options.key?(:pool_size)
|
17
|
+
options[:pool_size] = @connection_options[:pool_size]
|
18
|
+
end
|
19
|
+
Net::HTTP::Persistent.new(**options)
|
14
20
|
else
|
15
|
-
Net::HTTP::Persistent.new('Faraday'
|
21
|
+
Net::HTTP::Persistent.new('Faraday')
|
16
22
|
end
|
23
|
+
|
24
|
+
proxy_uri = proxy_uri(env)
|
25
|
+
if @cached_connection.proxy_uri != proxy_uri
|
26
|
+
@cached_connection.proxy = proxy_uri
|
17
27
|
end
|
28
|
+
@cached_connection
|
18
29
|
end
|
19
30
|
|
20
31
|
def proxy_uri(env)
|
21
32
|
proxy_uri = nil
|
22
33
|
if (proxy = env[:request][:proxy])
|
23
|
-
proxy_uri =
|
34
|
+
proxy_uri = if proxy[:uri].is_a?(::URI::HTTP)
|
35
|
+
proxy[:uri].dup
|
36
|
+
else
|
37
|
+
::URI.parse(proxy[:uri].to_s)
|
38
|
+
end
|
24
39
|
proxy_uri.user = proxy_uri.password = nil
|
25
|
-
# awful patch for net-http-persistent 2.8
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
40
|
+
# awful patch for net-http-persistent 2.8
|
41
|
+
# not unescaping user/password
|
42
|
+
if proxy[:user]
|
43
|
+
(class << proxy_uri; self; end).class_eval do
|
44
|
+
define_method(:user) { proxy[:user] }
|
45
|
+
define_method(:password) { proxy[:password] }
|
46
|
+
end
|
47
|
+
end
|
30
48
|
end
|
31
49
|
proxy_uri
|
32
50
|
end
|
33
51
|
|
34
52
|
def perform_request(http, env)
|
35
53
|
http.request env[:url], create_request(env)
|
36
|
-
rescue Errno::ETIMEDOUT =>
|
37
|
-
raise Faraday::
|
38
|
-
rescue Net::HTTP::Persistent::Error =>
|
39
|
-
if
|
40
|
-
|
41
|
-
|
42
|
-
raise Faraday::
|
43
|
-
else
|
44
|
-
raise
|
54
|
+
rescue Errno::ETIMEDOUT, Net::OpenTimeout => e
|
55
|
+
raise Faraday::TimeoutError, e
|
56
|
+
rescue Net::HTTP::Persistent::Error => e
|
57
|
+
raise Faraday::TimeoutError, e if e.message.include? 'Timeout'
|
58
|
+
|
59
|
+
if e.message.include? 'connection refused'
|
60
|
+
raise Faraday::ConnectionFailed, e
|
45
61
|
end
|
62
|
+
|
63
|
+
raise
|
46
64
|
end
|
47
65
|
|
66
|
+
SSL_CONFIGURATIONS = {
|
67
|
+
certificate: :client_cert,
|
68
|
+
private_key: :client_key,
|
69
|
+
ca_file: :ca_file,
|
70
|
+
ssl_version: :version,
|
71
|
+
min_version: :min_version,
|
72
|
+
max_version: :max_version
|
73
|
+
}.freeze
|
74
|
+
|
48
75
|
def configure_ssl(http, ssl)
|
49
|
-
|
50
|
-
|
76
|
+
return unless ssl
|
77
|
+
|
78
|
+
http_set(http, :verify_mode, ssl_verify_mode(ssl))
|
79
|
+
http_set(http, :cert_store, ssl_cert_store(ssl))
|
51
80
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
http.ssl_version = ssl[:version] if ssl[:version]
|
81
|
+
SSL_CONFIGURATIONS
|
82
|
+
.select { |_, key| ssl[key] }
|
83
|
+
.each { |target, key| http_set(http, target, ssl[key]) }
|
56
84
|
end
|
57
85
|
|
58
|
-
def
|
59
|
-
(
|
86
|
+
def http_set(http, attr, value)
|
87
|
+
http.send("#{attr}=", value) if http.send(attr) != value
|
60
88
|
end
|
61
89
|
end
|
62
90
|
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
26
|
# TODO: support streaming requests
|
9
27
|
env[:body] = env[:body].read if env[:body].respond_to? :read
|
10
28
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
if proxy = req[:proxy]
|
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 connection_timed_out_message?(
|
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,18 +72,12 @@ 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 create_session
|
69
|
-
session = ::Patron::Session.new
|
70
|
-
@config_block.call(session) if @config_block
|
71
|
-
session
|
72
|
-
end
|
73
|
-
|
74
81
|
def configure_ssl(session, ssl)
|
75
82
|
if ssl.fetch(:verify, true)
|
76
83
|
session.cacert = ssl[:ca_file]
|
@@ -79,22 +86,47 @@ module Faraday
|
|
79
86
|
end
|
80
87
|
end
|
81
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
|
+
|
82
112
|
private
|
83
113
|
|
84
|
-
CURL_TIMEOUT_MESSAGES = [
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
93
124
|
|
94
125
|
def connection_timed_out_message?(message)
|
95
|
-
CURL_TIMEOUT_MESSAGES.any?
|
126
|
+
CURL_TIMEOUT_MESSAGES.any? do |curl_message|
|
127
|
+
message.include?(curl_message)
|
128
|
+
end
|
96
129
|
end
|
97
|
-
|
98
130
|
end
|
99
131
|
end
|
100
132
|
end
|