faraday 0.17.6 → 1.0.0.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.md +1 -1
- data/README.md +18 -358
- data/lib/faraday/adapter/em_http.rb +142 -99
- data/lib/faraday/adapter/em_http_ssl_patch.rb +23 -17
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/excon.rb +97 -57
- data/lib/faraday/adapter/httpclient.rb +61 -39
- data/lib/faraday/adapter/net_http.rb +103 -51
- data/lib/faraday/adapter/net_http_persistent.rb +49 -28
- data/lib/faraday/adapter/patron.rb +54 -35
- data/lib/faraday/adapter/rack.rb +28 -12
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter.rb +36 -22
- data/lib/faraday/adapter_registry.rb +28 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +321 -179
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +94 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
- data/lib/faraday/error.rb +21 -79
- data/lib/faraday/logging/formatter.rb +92 -0
- data/lib/faraday/middleware.rb +4 -28
- 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 +28 -0
- data/lib/faraday/options/request_options.rb +21 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +33 -184
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +66 -55
- data/lib/faraday/request/authorization.rb +42 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +72 -49
- data/lib/faraday/request/retry.rb +197 -171
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +41 -23
- data/lib/faraday/request.rb +68 -38
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +36 -18
- data/lib/faraday/response.rb +22 -15
- data/lib/faraday/upload_io.rb +31 -30
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +28 -245
- data/lib/faraday.rb +93 -174
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- metadata +25 -51
- data/CHANGELOG.md +0 -232
- data/Rakefile +0 -13
- data/lib/faraday/deprecate.rb +0 -109
- data/spec/faraday/deprecate_spec.rb +0 -147
- data/spec/faraday/error_spec.rb +0 -102
- data/spec/faraday/response/raise_error_spec.rb +0 -106
- data/spec/spec_helper.rb +0 -105
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
@@ -1,15 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
begin
|
2
4
|
require 'net/https'
|
3
5
|
rescue LoadError
|
4
|
-
warn
|
6
|
+
warn 'Warning: no such file to load -- net/https. ' \
|
7
|
+
'Make sure openssl is installed if you want ssl support'
|
5
8
|
require 'net/http'
|
6
9
|
end
|
7
10
|
require 'zlib'
|
8
11
|
|
9
12
|
module Faraday
|
10
13
|
class Adapter
|
14
|
+
# Net::HTTP adapter.
|
11
15
|
class NetHttp < Faraday::Adapter
|
12
|
-
|
16
|
+
exceptions = [
|
13
17
|
IOError,
|
14
18
|
Errno::EADDRNOTAVAIL,
|
15
19
|
Errno::ECONNABORTED,
|
@@ -23,34 +27,40 @@ module Faraday
|
|
23
27
|
Net::HTTPHeaderSyntaxError,
|
24
28
|
Net::ProtocolError,
|
25
29
|
SocketError,
|
26
|
-
Zlib::GzipFile::Error
|
30
|
+
Zlib::GzipFile::Error
|
27
31
|
]
|
28
32
|
|
29
|
-
|
30
|
-
|
33
|
+
exceptions << OpenSSL::SSL::SSLError if defined?(OpenSSL)
|
34
|
+
exceptions << Net::OpenTimeout if defined?(Net::OpenTimeout)
|
35
|
+
|
36
|
+
NET_HTTP_EXCEPTIONS = exceptions.freeze
|
31
37
|
|
32
38
|
def initialize(app = nil, opts = {}, &block)
|
33
|
-
@
|
39
|
+
@ssl_cert_store = nil
|
34
40
|
super(app, opts, &block)
|
35
41
|
end
|
36
42
|
|
37
43
|
def call(env)
|
38
44
|
super
|
39
45
|
with_net_http_connection(env) do |http|
|
40
|
-
|
46
|
+
if (env[:url].scheme == 'https') && env[:ssl]
|
47
|
+
configure_ssl(http, env[:ssl])
|
48
|
+
end
|
41
49
|
configure_request(http, env[:request])
|
42
50
|
|
43
51
|
begin
|
44
52
|
http_response = perform_request(http, env)
|
45
|
-
rescue *NET_HTTP_EXCEPTIONS =>
|
46
|
-
if defined?(OpenSSL) && OpenSSL::SSL::SSLError
|
47
|
-
raise Faraday::SSLError,
|
48
|
-
else
|
49
|
-
raise Faraday::ConnectionFailed, err
|
53
|
+
rescue *NET_HTTP_EXCEPTIONS => e
|
54
|
+
if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
|
55
|
+
raise Faraday::SSLError, e
|
50
56
|
end
|
57
|
+
|
58
|
+
raise Faraday::ConnectionFailed, e
|
51
59
|
end
|
52
60
|
|
53
|
-
save_response(env, http_response.code.to_i,
|
61
|
+
save_response(env, http_response.code.to_i,
|
62
|
+
http_response.body || '', nil,
|
63
|
+
http_response.message) do |response_headers|
|
54
64
|
http_response.each_header do |key, value|
|
55
65
|
response_headers[key] = value
|
56
66
|
end
|
@@ -58,19 +68,19 @@ module Faraday
|
|
58
68
|
end
|
59
69
|
|
60
70
|
@app.call env
|
61
|
-
rescue Timeout::Error, Errno::ETIMEDOUT =>
|
62
|
-
raise Faraday::TimeoutError,
|
71
|
+
rescue Timeout::Error, Errno::ETIMEDOUT => e
|
72
|
+
raise Faraday::TimeoutError, e
|
63
73
|
end
|
64
74
|
|
65
75
|
private
|
66
76
|
|
67
77
|
def create_request(env)
|
68
78
|
request = Net::HTTPGenericRequest.new \
|
69
|
-
env[:method].to_s.upcase,
|
70
|
-
!!env[:body],
|
71
|
-
:
|
72
|
-
env[:url].request_uri,
|
73
|
-
env[:request_headers]
|
79
|
+
env[:method].to_s.upcase, # request method
|
80
|
+
!!env[:body], # is there request body
|
81
|
+
env[:method] != :head, # is there response body
|
82
|
+
env[:url].request_uri, # request uri path
|
83
|
+
env[:request_headers] # request headers
|
74
84
|
|
75
85
|
if env[:body].respond_to?(:read)
|
76
86
|
request.body_stream = env[:body]
|
@@ -81,9 +91,44 @@ module Faraday
|
|
81
91
|
end
|
82
92
|
|
83
93
|
def perform_request(http, env)
|
84
|
-
if
|
94
|
+
if env[:request].stream_response?
|
95
|
+
size = 0
|
96
|
+
yielded = false
|
97
|
+
http_response = request_with_wrapped_block(http, env) do |chunk|
|
98
|
+
if chunk.bytesize.positive? || size.positive?
|
99
|
+
yielded = true
|
100
|
+
size += chunk.bytesize
|
101
|
+
env[:request].on_data.call(chunk, size)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
env[:request].on_data.call('', 0) unless yielded
|
105
|
+
# Net::HTTP returns something,
|
106
|
+
# but it's not meaningful according to the docs.
|
107
|
+
http_response.body = nil
|
108
|
+
http_response
|
109
|
+
else
|
110
|
+
request_with_wrapped_block(http, env)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def request_with_wrapped_block(http, env, &block)
|
115
|
+
if (env[:method] == :get) && !env[:body]
|
85
116
|
# prefer `get` to `request` because the former handles gzip (ruby 1.9)
|
86
|
-
http
|
117
|
+
request_via_get_method(http, env, &block)
|
118
|
+
else
|
119
|
+
request_via_request_method(http, env, &block)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def request_via_get_method(http, env, &block)
|
124
|
+
http.get env[:url].request_uri, env[:request_headers], &block
|
125
|
+
end
|
126
|
+
|
127
|
+
def request_via_request_method(http, env, &block)
|
128
|
+
if block_given?
|
129
|
+
http.request create_request(env) do |response|
|
130
|
+
response.read_body(&block)
|
131
|
+
end
|
87
132
|
else
|
88
133
|
http.request create_request(env)
|
89
134
|
end
|
@@ -94,49 +139,56 @@ module Faraday
|
|
94
139
|
end
|
95
140
|
|
96
141
|
def net_http_connection(env)
|
97
|
-
if proxy = env[:request][:proxy]
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
142
|
+
klass = if (proxy = env[:request][:proxy])
|
143
|
+
Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port,
|
144
|
+
proxy[:user], proxy[:password])
|
145
|
+
else
|
146
|
+
Net::HTTP
|
147
|
+
end
|
148
|
+
port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
|
149
|
+
klass.new(env[:url].hostname, port)
|
102
150
|
end
|
103
151
|
|
104
152
|
def configure_ssl(http, ssl)
|
105
|
-
http.use_ssl
|
106
|
-
http.verify_mode
|
107
|
-
http.cert_store
|
108
|
-
|
109
|
-
http.cert
|
110
|
-
http.key
|
111
|
-
http.ca_file
|
112
|
-
http.ca_path
|
153
|
+
http.use_ssl = true
|
154
|
+
http.verify_mode = ssl_verify_mode(ssl)
|
155
|
+
http.cert_store = ssl_cert_store(ssl)
|
156
|
+
|
157
|
+
http.cert = ssl[:client_cert] if ssl[:client_cert]
|
158
|
+
http.key = ssl[:client_key] if ssl[:client_key]
|
159
|
+
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
|
160
|
+
http.ca_path = ssl[:ca_path] if ssl[:ca_path]
|
113
161
|
http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
114
|
-
http.ssl_version
|
115
|
-
http.min_version
|
116
|
-
http.max_version
|
162
|
+
http.ssl_version = ssl[:version] if ssl[:version]
|
163
|
+
http.min_version = ssl[:min_version] if ssl[:min_version]
|
164
|
+
http.max_version = ssl[:max_version] if ssl[:max_version]
|
117
165
|
end
|
118
166
|
|
119
167
|
def configure_request(http, req)
|
120
168
|
if req[:timeout]
|
121
|
-
http.read_timeout
|
122
|
-
http.open_timeout
|
123
|
-
|
169
|
+
http.read_timeout = req[:timeout]
|
170
|
+
http.open_timeout = req[:timeout]
|
171
|
+
if http.respond_to?(:write_timeout=)
|
172
|
+
http.write_timeout = req[:timeout]
|
173
|
+
end
|
174
|
+
end
|
175
|
+
http.open_timeout = req[:open_timeout] if req[:open_timeout]
|
176
|
+
if req[:write_timeout] && http.respond_to?(:write_timeout=)
|
177
|
+
http.write_timeout = req[:write_timeout]
|
124
178
|
end
|
125
|
-
|
126
|
-
http.
|
127
|
-
# Only set if Net::Http supports it, since Ruby 2.5.
|
128
|
-
http.max_retries = 0 if http.respond_to?(:max_retries=)
|
179
|
+
# Only set if Net::Http supports it, since Ruby 2.5.
|
180
|
+
http.max_retries = 0 if http.respond_to?(:max_retries=)
|
129
181
|
|
130
|
-
@config_block
|
182
|
+
@config_block&.call(http)
|
131
183
|
end
|
132
184
|
|
133
185
|
def ssl_cert_store(ssl)
|
134
186
|
return ssl[:cert_store] if ssl[:cert_store]
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
187
|
+
|
188
|
+
@ssl_cert_store ||= begin
|
189
|
+
# Use the default cert store by default, i.e. system ca certs
|
190
|
+
OpenSSL::X509::Store.new.tap(&:set_default_paths)
|
191
|
+
end
|
140
192
|
end
|
141
193
|
|
142
194
|
def ssl_verify_mode(ssl)
|
@@ -1,5 +1,8 @@
|
|
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
|
|
@@ -7,61 +10,79 @@ module Faraday
|
|
7
10
|
|
8
11
|
def net_http_connection(env)
|
9
12
|
@cached_connection ||=
|
10
|
-
if Net::HTTP::Persistent.instance_method(:initialize)
|
11
|
-
|
12
|
-
options
|
13
|
-
|
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
21
|
Net::HTTP::Persistent.new('Faraday')
|
16
22
|
end
|
17
23
|
|
18
24
|
proxy_uri = proxy_uri(env)
|
19
|
-
|
25
|
+
if @cached_connection.proxy_uri != proxy_uri
|
26
|
+
@cached_connection.proxy = proxy_uri
|
27
|
+
end
|
20
28
|
@cached_connection
|
21
29
|
end
|
22
30
|
|
23
31
|
def proxy_uri(env)
|
24
32
|
proxy_uri = nil
|
25
33
|
if (proxy = env[:request][:proxy])
|
26
|
-
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
|
27
39
|
proxy_uri.user = proxy_uri.password = nil
|
28
|
-
# awful patch for net-http-persistent 2.8
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
33
48
|
end
|
34
49
|
proxy_uri
|
35
50
|
end
|
36
51
|
|
37
52
|
def perform_request(http, env)
|
38
53
|
http.request env[:url], create_request(env)
|
39
|
-
rescue Errno::ETIMEDOUT =>
|
40
|
-
raise Faraday::TimeoutError,
|
41
|
-
rescue Net::HTTP::Persistent::Error =>
|
42
|
-
if
|
43
|
-
|
44
|
-
|
45
|
-
raise Faraday::ConnectionFailed,
|
46
|
-
else
|
47
|
-
raise
|
54
|
+
rescue Errno::ETIMEDOUT => 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
|
48
61
|
end
|
62
|
+
|
63
|
+
raise
|
49
64
|
end
|
50
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
|
+
|
51
75
|
def configure_ssl(http, ssl)
|
52
76
|
http_set(http, :verify_mode, ssl_verify_mode(ssl))
|
53
|
-
http_set(http, :cert_store,
|
77
|
+
http_set(http, :cert_store, ssl_cert_store(ssl))
|
54
78
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
http_set(http, :ssl_version, ssl[:version]) if ssl[:version]
|
79
|
+
SSL_CONFIGURATIONS
|
80
|
+
.select { |_, key| ssl[key] }
|
81
|
+
.each { |target, key| http_set(http, target, ssl[key]) }
|
59
82
|
end
|
60
83
|
|
61
84
|
def http_set(http, attr, value)
|
62
|
-
if http.send(attr) != value
|
63
|
-
http.send("#{attr}=", value)
|
64
|
-
end
|
85
|
+
http.send("#{attr}=", value) if http.send(attr) != value
|
65
86
|
end
|
66
87
|
end
|
67
88
|
end
|
@@ -1,5 +1,8 @@
|
|
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,46 +12,60 @@ module Faraday
|
|
9
12
|
env[:body] = env[:body].read if env[:body].respond_to? :read
|
10
13
|
|
11
14
|
session = ::Patron::Session.new
|
12
|
-
@config_block
|
13
|
-
|
15
|
+
@config_block&.call(session)
|
16
|
+
if (env[:url].scheme == 'https') && env[:ssl]
|
17
|
+
configure_ssl(session, env[:ssl])
|
18
|
+
end
|
14
19
|
|
15
|
-
if req = env[:request]
|
16
|
-
|
17
|
-
|
20
|
+
if (req = env[:request])
|
21
|
+
if req[:timeout]
|
22
|
+
session.timeout = session.connect_timeout = req[:timeout]
|
23
|
+
end
|
24
|
+
session.connect_timeout = req[:open_timeout] if req[:open_timeout]
|
18
25
|
|
19
|
-
if proxy = req[:proxy]
|
26
|
+
if (proxy = req[:proxy])
|
20
27
|
proxy_uri = proxy[:uri].dup
|
21
|
-
proxy_uri.user = proxy[:user] &&
|
22
|
-
|
28
|
+
proxy_uri.user = proxy[:user] &&
|
29
|
+
Utils.escape(proxy[:user]).gsub('+', '%20')
|
30
|
+
proxy_uri.password = proxy[:password] &&
|
31
|
+
Utils.escape(proxy[:password]).gsub('+', '%20')
|
23
32
|
session.proxy = proxy_uri.to_s
|
24
33
|
end
|
25
34
|
end
|
26
35
|
|
27
36
|
response = begin
|
28
37
|
data = env[:body] ? env[:body].to_s : nil
|
29
|
-
session.request(env[:method], env[:url].to_s,
|
30
|
-
|
31
|
-
|
38
|
+
session.request(env[:method], env[:url].to_s,
|
39
|
+
env[:request_headers], data: data)
|
40
|
+
rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
|
41
|
+
raise Faraday::ConnectionFailed, $ERROR_INFO
|
32
42
|
end
|
33
43
|
|
44
|
+
if (req = env[:request]).stream_response?
|
45
|
+
warn "Streaming downloads for #{self.class.name} " \
|
46
|
+
'are not yet implemented.'
|
47
|
+
req.on_data.call(response.body, response.body.bytesize)
|
48
|
+
end
|
34
49
|
# Remove the "HTTP/1.1 200", leaving just the reason phrase
|
35
50
|
reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
|
36
51
|
|
37
|
-
save_response(env, response.status, response.body,
|
52
|
+
save_response(env, response.status, response.body,
|
53
|
+
response.headers, reason_phrase)
|
38
54
|
|
39
55
|
@app.call env
|
40
|
-
rescue ::Patron::TimeoutError =>
|
41
|
-
if connection_timed_out_message?(
|
42
|
-
raise Faraday::ConnectionFailed,
|
43
|
-
else
|
44
|
-
raise Faraday::TimeoutError, err
|
56
|
+
rescue ::Patron::TimeoutError => e
|
57
|
+
if connection_timed_out_message?(e.message)
|
58
|
+
raise Faraday::ConnectionFailed, e
|
45
59
|
end
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
raise Faraday::ConnectionFailed,
|
60
|
+
|
61
|
+
raise Faraday::TimeoutError, e
|
62
|
+
rescue ::Patron::Error => e
|
63
|
+
if e.message.include?('code 407')
|
64
|
+
raise Faraday::ConnectionFailed,
|
65
|
+
%(407 "Proxy Authentication Required ")
|
51
66
|
end
|
67
|
+
|
68
|
+
raise Faraday::ConnectionFailed, e
|
52
69
|
end
|
53
70
|
|
54
71
|
if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
|
@@ -60,8 +77,8 @@ module Faraday
|
|
60
77
|
actions << :options unless actions.include? :options
|
61
78
|
else
|
62
79
|
# Patron 0.4.20 and up
|
63
|
-
actions <<
|
64
|
-
actions <<
|
80
|
+
actions << 'PATCH' unless actions.include? 'PATCH'
|
81
|
+
actions << 'OPTIONS' unless actions.include? 'OPTIONS'
|
65
82
|
end
|
66
83
|
end
|
67
84
|
end
|
@@ -76,20 +93,22 @@ module Faraday
|
|
76
93
|
|
77
94
|
private
|
78
95
|
|
79
|
-
CURL_TIMEOUT_MESSAGES = [
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
96
|
+
CURL_TIMEOUT_MESSAGES = [
|
97
|
+
'Connection time-out',
|
98
|
+
'Connection timed out',
|
99
|
+
'Timed out before name resolve',
|
100
|
+
'server connect has timed out',
|
101
|
+
'Resolving timed out',
|
102
|
+
'name lookup timed out',
|
103
|
+
'timed out before SSL',
|
104
|
+
'connect() timed out'
|
105
|
+
].freeze
|
88
106
|
|
89
107
|
def connection_timed_out_message?(message)
|
90
|
-
CURL_TIMEOUT_MESSAGES.any?
|
108
|
+
CURL_TIMEOUT_MESSAGES.any? do |curl_message|
|
109
|
+
message.include?(curl_message)
|
110
|
+
end
|
91
111
|
end
|
92
|
-
|
93
112
|
end
|
94
113
|
end
|
95
114
|
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,46 @@ 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
40
|
timeout = env[:request][:timeout] || env[:request][:open_timeout]
|
43
41
|
response = if timeout
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
Timer.timeout(timeout, Faraday::TimeoutError) do
|
43
|
+
execute_request(env, rack_env)
|
44
|
+
end
|
45
|
+
else
|
46
|
+
execute_request(env, rack_env)
|
47
|
+
end
|
48
|
+
|
49
|
+
if (req = env[:request]).stream_response?
|
50
|
+
warn "Streaming downloads for #{self.class.name} " \
|
51
|
+
'are not yet implemented.'
|
52
|
+
req.on_data.call(response.body, response.body.bytesize)
|
47
53
|
end
|
48
54
|
|
49
55
|
save_response(env, response.status, response.body, response.headers)
|
50
56
|
@app.call env
|
51
57
|
end
|
52
58
|
|
59
|
+
private
|
60
|
+
|
53
61
|
def execute_request(env, rack_env)
|
54
62
|
@session.request(env[:url].to_s, rack_env)
|
55
63
|
end
|
64
|
+
|
65
|
+
def build_rack_env(env)
|
66
|
+
{
|
67
|
+
method: env[:method],
|
68
|
+
input: env[:body].respond_to?(:read) ? env[:body].read : env[:body],
|
69
|
+
'rack.url_scheme' => env[:url].scheme
|
70
|
+
}
|
71
|
+
end
|
56
72
|
end
|
57
73
|
end
|
58
74
|
end
|