faraday 1.0.0 → 2.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +299 -1
- data/LICENSE.md +1 -1
- data/README.md +35 -23
- data/Rakefile +3 -1
- data/examples/client_spec.rb +68 -14
- data/examples/client_test.rb +80 -15
- data/lib/faraday/adapter/test.rb +117 -52
- data/lib/faraday/adapter.rb +6 -20
- data/lib/faraday/adapter_registry.rb +3 -1
- data/lib/faraday/connection.rb +73 -132
- data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
- data/lib/faraday/encoders/nested_params_encoder.rb +20 -8
- data/lib/faraday/error.rb +37 -8
- data/lib/faraday/logging/formatter.rb +28 -15
- 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/connection_options.rb +7 -6
- data/lib/faraday/options/env.rb +85 -62
- data/lib/faraday/options/proxy_options.rb +11 -3
- data/lib/faraday/options/request_options.rb +7 -6
- data/lib/faraday/options/ssl_options.rb +56 -45
- data/lib/faraday/options.rb +11 -14
- data/lib/faraday/rack_builder.rb +35 -32
- data/lib/faraday/request/authorization.rb +37 -36
- data/lib/faraday/request/instrumentation.rb +5 -1
- data/lib/faraday/request/json.rb +70 -0
- data/lib/faraday/request/url_encoded.rb +8 -2
- data/lib/faraday/request.rb +22 -29
- data/lib/faraday/response/json.rb +73 -0
- data/lib/faraday/response/logger.rb +8 -4
- data/lib/faraday/response/raise_error.rb +41 -3
- data/lib/faraday/response.rb +10 -23
- data/lib/faraday/utils/headers.rb +9 -4
- data/lib/faraday/utils.rb +22 -10
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +49 -58
- data/spec/faraday/adapter/test_spec.rb +442 -0
- data/spec/faraday/connection_spec.rb +207 -90
- data/spec/faraday/error_spec.rb +45 -5
- 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/options_spec.rb +1 -1
- data/spec/faraday/options/proxy_options_spec.rb +15 -0
- data/spec/faraday/params_encoders/flat_spec.rb +8 -0
- data/spec/faraday/params_encoders/nested_spec.rb +16 -0
- data/spec/faraday/rack_builder_spec.rb +171 -50
- 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 +199 -0
- data/spec/faraday/request/url_encoded_spec.rb +25 -2
- data/spec/faraday/request_spec.rb +11 -10
- data/spec/faraday/response/json_spec.rb +189 -0
- data/spec/faraday/response/logger_spec.rb +38 -0
- data/spec/faraday/response/raise_error_spec.rb +105 -0
- 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/faraday_spec.rb +8 -4
- data/spec/spec_helper.rb +6 -5
- 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 +60 -31
- metadata +19 -44
- data/UPGRADING.md +0 -55
- data/lib/faraday/adapter/em_http.rb +0 -285
- 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 -151
- data/lib/faraday/adapter/net_http.rb +0 -209
- 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 -37
- 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 -99
- 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 -274
- data/spec/faraday/request/retry_spec.rb +0 -242
- data/spec/faraday/response/middleware_spec.rb +0 -52
- data/spec/support/webmock_rack_app.rb +0 -68
@@ -1,150 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'uri'
|
4
|
-
|
5
|
-
module Faraday
|
6
|
-
class Adapter
|
7
|
-
# EventMachine Synchrony adapter.
|
8
|
-
class EMSynchrony < Faraday::Adapter
|
9
|
-
include EMHttp::Options
|
10
|
-
|
11
|
-
dependency do
|
12
|
-
require 'em-synchrony/em-http'
|
13
|
-
require 'em-synchrony/em-multi'
|
14
|
-
require 'fiber'
|
15
|
-
end
|
16
|
-
|
17
|
-
self.supports_parallel = true
|
18
|
-
|
19
|
-
# @return [ParallelManager]
|
20
|
-
def self.setup_parallel_manager(_options = nil)
|
21
|
-
ParallelManager.new
|
22
|
-
end
|
23
|
-
|
24
|
-
def call(env)
|
25
|
-
super
|
26
|
-
request = create_request(env)
|
27
|
-
|
28
|
-
http_method = env[:method].to_s.downcase.to_sym
|
29
|
-
|
30
|
-
if env[:parallel_manager]
|
31
|
-
# Queue requests for parallel execution.
|
32
|
-
execute_parallel_request(env, request, http_method)
|
33
|
-
else
|
34
|
-
# Execute single request.
|
35
|
-
execute_single_request(env, request, http_method)
|
36
|
-
end
|
37
|
-
|
38
|
-
@app.call env
|
39
|
-
rescue Errno::ECONNREFUSED
|
40
|
-
raise Faraday::ConnectionFailed, $ERROR_INFO
|
41
|
-
rescue EventMachine::Connectify::CONNECTError => e
|
42
|
-
if e.message.include?('Proxy Authentication Required')
|
43
|
-
raise Faraday::ConnectionFailed,
|
44
|
-
%(407 "Proxy Authentication Required")
|
45
|
-
end
|
46
|
-
|
47
|
-
raise Faraday::ConnectionFailed, e
|
48
|
-
rescue Errno::ETIMEDOUT => e
|
49
|
-
raise Faraday::TimeoutError, e
|
50
|
-
rescue RuntimeError => e
|
51
|
-
if e.message == 'connection closed by server'
|
52
|
-
raise Faraday::ConnectionFailed, e
|
53
|
-
end
|
54
|
-
|
55
|
-
raise Faraday::TimeoutError, e if e.message.include?('timeout error')
|
56
|
-
|
57
|
-
raise
|
58
|
-
rescue StandardError => e
|
59
|
-
if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
|
60
|
-
raise Faraday::SSLError, e
|
61
|
-
end
|
62
|
-
|
63
|
-
raise
|
64
|
-
end
|
65
|
-
|
66
|
-
def create_request(env)
|
67
|
-
EventMachine::HttpRequest.new(
|
68
|
-
Utils::URI(env[:url].to_s),
|
69
|
-
connection_config(env).merge(@connection_options)
|
70
|
-
)
|
71
|
-
end
|
72
|
-
|
73
|
-
private
|
74
|
-
|
75
|
-
def execute_parallel_request(env, request, http_method)
|
76
|
-
env[:parallel_manager].add(request, http_method,
|
77
|
-
request_config(env)) do |resp|
|
78
|
-
if (req = env[:request]).stream_response?
|
79
|
-
warn "Streaming downloads for #{self.class.name} " \
|
80
|
-
'are not yet implemented.'
|
81
|
-
req.on_data.call(resp.response, resp.response.bytesize)
|
82
|
-
end
|
83
|
-
|
84
|
-
save_response(env, resp.response_header.status,
|
85
|
-
resp.response) do |resp_headers|
|
86
|
-
resp.response_header.each do |name, value|
|
87
|
-
resp_headers[name.to_sym] = value
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
# Finalize the response object with values from `env`.
|
92
|
-
env[:response].finish(env)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def execute_single_request(env, request, http_method)
|
97
|
-
block = -> { request.send(http_method, request_config(env)) }
|
98
|
-
client = call_block(block)
|
99
|
-
|
100
|
-
raise client.error if client&.error
|
101
|
-
|
102
|
-
if env[:request].stream_response?
|
103
|
-
warn "Streaming downloads for #{self.class.name} " \
|
104
|
-
'are not yet implemented.'
|
105
|
-
env[:request].on_data.call(
|
106
|
-
client.response,
|
107
|
-
client.response.bytesize
|
108
|
-
)
|
109
|
-
end
|
110
|
-
status = client.response_header.status
|
111
|
-
reason = client.response_header.http_reason
|
112
|
-
save_response(env, status, client.response, nil, reason) do |headers|
|
113
|
-
client.response_header.each do |name, value|
|
114
|
-
headers[name.to_sym] = value
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def call_block(block)
|
120
|
-
client = nil
|
121
|
-
|
122
|
-
if EM.reactor_running?
|
123
|
-
client = block.call
|
124
|
-
else
|
125
|
-
EM.run do
|
126
|
-
Fiber.new do
|
127
|
-
client = block.call
|
128
|
-
EM.stop
|
129
|
-
end.resume
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
client
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
require 'faraday/adapter/em_synchrony/parallel_manager'
|
140
|
-
|
141
|
-
if Faraday::Adapter::EMSynchrony.loaded?
|
142
|
-
begin
|
143
|
-
require 'openssl'
|
144
|
-
rescue LoadError
|
145
|
-
warn 'Warning: no such file to load -- openssl. ' \
|
146
|
-
'Make sure it is installed if you want HTTPS support'
|
147
|
-
else
|
148
|
-
require 'faraday/adapter/em_http_ssl_patch'
|
149
|
-
end
|
150
|
-
end
|
@@ -1,124 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
class Adapter
|
5
|
-
# Excon adapter.
|
6
|
-
class Excon < Faraday::Adapter
|
7
|
-
dependency 'excon'
|
8
|
-
|
9
|
-
def build_connection(env)
|
10
|
-
opts = opts_from_env(env)
|
11
|
-
::Excon.new(env[:url].to_s, opts.merge(@connection_options))
|
12
|
-
end
|
13
|
-
|
14
|
-
def call(env)
|
15
|
-
super
|
16
|
-
|
17
|
-
req_opts = {
|
18
|
-
method: env[:method].to_s.upcase,
|
19
|
-
headers: env[:request_headers],
|
20
|
-
body: read_body(env)
|
21
|
-
}
|
22
|
-
|
23
|
-
req = env[:request]
|
24
|
-
if req&.stream_response?
|
25
|
-
total = 0
|
26
|
-
req_opts[:response_block] = lambda do |chunk, _remain, _total|
|
27
|
-
req.on_data.call(chunk, total += chunk.size)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
resp = connection(env) { |http| http.request(req_opts) }
|
32
|
-
save_response(env, resp.status.to_i, resp.body, resp.headers,
|
33
|
-
resp.reason_phrase)
|
34
|
-
|
35
|
-
@app.call(env)
|
36
|
-
rescue ::Excon::Errors::SocketError => e
|
37
|
-
raise Faraday::TimeoutError, e if e.message =~ /\btimeout\b/
|
38
|
-
|
39
|
-
raise Faraday::SSLError, e if e.message =~ /\bcertificate\b/
|
40
|
-
|
41
|
-
raise Faraday::ConnectionFailed, e
|
42
|
-
rescue ::Excon::Errors::Timeout => e
|
43
|
-
raise Faraday::TimeoutError, e
|
44
|
-
end
|
45
|
-
|
46
|
-
# TODO: support streaming requests
|
47
|
-
def read_body(env)
|
48
|
-
env[:body].respond_to?(:read) ? env[:body].read : env[:body]
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def opts_from_env(env)
|
54
|
-
opts = {}
|
55
|
-
amend_opts_with_ssl!(opts, env[:ssl]) if needs_ssl_settings?(env)
|
56
|
-
|
57
|
-
if (req = env[:request])
|
58
|
-
amend_opts_with_timeouts!(opts, req)
|
59
|
-
amend_opts_with_proxy_settings!(opts, req)
|
60
|
-
end
|
61
|
-
|
62
|
-
opts
|
63
|
-
end
|
64
|
-
|
65
|
-
def needs_ssl_settings?(env)
|
66
|
-
env[:url].scheme == 'https' && env[:ssl]
|
67
|
-
end
|
68
|
-
|
69
|
-
OPTS_KEYS = [
|
70
|
-
%i[client_cert client_cert],
|
71
|
-
%i[client_key client_key],
|
72
|
-
%i[certificate certificate],
|
73
|
-
%i[private_key private_key],
|
74
|
-
%i[ssl_ca_path ca_path],
|
75
|
-
%i[ssl_ca_file ca_file],
|
76
|
-
%i[ssl_version version],
|
77
|
-
%i[ssl_min_version min_version],
|
78
|
-
%i[ssl_max_version max_version]
|
79
|
-
].freeze
|
80
|
-
|
81
|
-
def amend_opts_with_ssl!(opts, ssl)
|
82
|
-
opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
|
83
|
-
# https://github.com/geemus/excon/issues/106
|
84
|
-
# https://github.com/jruby/jruby-ossl/issues/19
|
85
|
-
opts[:nonblock] = false
|
86
|
-
|
87
|
-
OPTS_KEYS.each do |(key_in_opts, key_in_ssl)|
|
88
|
-
next unless ssl[key_in_ssl]
|
89
|
-
|
90
|
-
opts[key_in_opts] = ssl[key_in_ssl]
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
def amend_opts_with_timeouts!(opts, req)
|
95
|
-
if (sec = request_timeout(:read, req))
|
96
|
-
opts[:read_timeout] = sec
|
97
|
-
end
|
98
|
-
|
99
|
-
if (sec = request_timeout(:write, req))
|
100
|
-
opts[:write_timeout] = sec
|
101
|
-
end
|
102
|
-
|
103
|
-
return unless (sec = request_timeout(:open, req))
|
104
|
-
|
105
|
-
opts[:connect_timeout] = sec
|
106
|
-
end
|
107
|
-
|
108
|
-
def amend_opts_with_proxy_settings!(opts, req)
|
109
|
-
opts[:proxy] = proxy_settings_for_opts(req[:proxy]) if req[:proxy]
|
110
|
-
end
|
111
|
-
|
112
|
-
def proxy_settings_for_opts(proxy)
|
113
|
-
{
|
114
|
-
host: proxy[:uri].host,
|
115
|
-
hostname: proxy[:uri].hostname,
|
116
|
-
port: proxy[:uri].port,
|
117
|
-
scheme: proxy[:uri].scheme,
|
118
|
-
user: proxy[:user],
|
119
|
-
password: proxy[:password]
|
120
|
-
}
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
@@ -1,151 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
class Adapter
|
5
|
-
# HTTPClient adapter.
|
6
|
-
class HTTPClient < Faraday::Adapter
|
7
|
-
dependency 'httpclient'
|
8
|
-
|
9
|
-
def build_connection(env)
|
10
|
-
@client ||= ::HTTPClient.new.tap do |cli|
|
11
|
-
# enable compression
|
12
|
-
cli.transparent_gzip_decompression = true
|
13
|
-
end
|
14
|
-
|
15
|
-
if (req = env[:request])
|
16
|
-
if (proxy = req[:proxy])
|
17
|
-
configure_proxy @client, proxy
|
18
|
-
end
|
19
|
-
|
20
|
-
if (bind = req[:bind])
|
21
|
-
configure_socket @client, bind
|
22
|
-
end
|
23
|
-
|
24
|
-
configure_timeouts @client, req
|
25
|
-
end
|
26
|
-
|
27
|
-
if env[:url].scheme == 'https' && (ssl = env[:ssl])
|
28
|
-
configure_ssl @client, ssl
|
29
|
-
end
|
30
|
-
|
31
|
-
configure_client @client
|
32
|
-
|
33
|
-
@client
|
34
|
-
end
|
35
|
-
|
36
|
-
def call(env)
|
37
|
-
super
|
38
|
-
|
39
|
-
# TODO: Don't stream yet.
|
40
|
-
# https://github.com/nahi/httpclient/pull/90
|
41
|
-
env[:body] = env[:body].read if env[:body].respond_to? :read
|
42
|
-
|
43
|
-
connection(env) do |http|
|
44
|
-
resp = http.request env[:method], env[:url],
|
45
|
-
body: env[:body],
|
46
|
-
header: env[:request_headers]
|
47
|
-
|
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
|
54
|
-
|
55
|
-
@app.call env
|
56
|
-
end
|
57
|
-
rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
|
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 ")
|
63
|
-
end
|
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) && e.is_a?(OpenSSL::SSL::SSLError)
|
70
|
-
raise Faraday::SSLError, e
|
71
|
-
end
|
72
|
-
|
73
|
-
raise
|
74
|
-
end
|
75
|
-
|
76
|
-
# @param bind [Hash]
|
77
|
-
def configure_socket(client, bind)
|
78
|
-
client.socket_local.host = bind[:host]
|
79
|
-
client.socket_local.port = bind[:port]
|
80
|
-
end
|
81
|
-
|
82
|
-
# Configure proxy URI and any user credentials.
|
83
|
-
#
|
84
|
-
# @param proxy [Hash]
|
85
|
-
def configure_proxy(client, proxy)
|
86
|
-
client.proxy = proxy[:uri]
|
87
|
-
return unless proxy[:user] && proxy[:password]
|
88
|
-
|
89
|
-
client.set_proxy_auth(proxy[:user], proxy[:password])
|
90
|
-
end
|
91
|
-
|
92
|
-
# @param ssl [Hash]
|
93
|
-
def configure_ssl(client, ssl)
|
94
|
-
ssl_config = client.ssl_config
|
95
|
-
ssl_config.verify_mode = ssl_verify_mode(ssl)
|
96
|
-
ssl_config.cert_store = ssl_cert_store(ssl)
|
97
|
-
|
98
|
-
ssl_config.add_trust_ca ssl[:ca_file] if ssl[:ca_file]
|
99
|
-
ssl_config.add_trust_ca ssl[:ca_path] if ssl[:ca_path]
|
100
|
-
ssl_config.client_cert = ssl[:client_cert] if ssl[:client_cert]
|
101
|
-
ssl_config.client_key = ssl[:client_key] if ssl[:client_key]
|
102
|
-
ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
103
|
-
end
|
104
|
-
|
105
|
-
# @param req [Hash]
|
106
|
-
def configure_timeouts(client, req)
|
107
|
-
if (sec = request_timeout(:open, req))
|
108
|
-
client.connect_timeout = sec
|
109
|
-
end
|
110
|
-
|
111
|
-
if (sec = request_timeout(:write, req))
|
112
|
-
client.send_timeout = sec
|
113
|
-
end
|
114
|
-
|
115
|
-
return unless (sec = request_timeout(:read, req))
|
116
|
-
|
117
|
-
client.receive_timeout = sec
|
118
|
-
end
|
119
|
-
|
120
|
-
def configure_client(client)
|
121
|
-
@config_block&.call(client)
|
122
|
-
end
|
123
|
-
|
124
|
-
# @param ssl [Hash]
|
125
|
-
# @return [OpenSSL::X509::Store]
|
126
|
-
def ssl_cert_store(ssl)
|
127
|
-
return ssl[:cert_store] if ssl[:cert_store]
|
128
|
-
|
129
|
-
# Memoize the cert store so that the same one is passed to
|
130
|
-
# HTTPClient each time, to avoid resyncing SSL sessions when
|
131
|
-
# it's changed
|
132
|
-
@ssl_cert_store ||= begin
|
133
|
-
# Use the default cert store by default, i.e. system ca certs
|
134
|
-
OpenSSL::X509::Store.new.tap(&:set_default_paths)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
# @param ssl [Hash]
|
139
|
-
def ssl_verify_mode(ssl)
|
140
|
-
ssl[:verify_mode] || begin
|
141
|
-
if ssl.fetch(:verify, true)
|
142
|
-
OpenSSL::SSL::VERIFY_PEER |
|
143
|
-
OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
|
144
|
-
else
|
145
|
-
OpenSSL::SSL::VERIFY_NONE
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
@@ -1,209 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
begin
|
4
|
-
require 'net/https'
|
5
|
-
rescue LoadError
|
6
|
-
warn 'Warning: no such file to load -- net/https. ' \
|
7
|
-
'Make sure openssl is installed if you want ssl support'
|
8
|
-
require 'net/http'
|
9
|
-
end
|
10
|
-
require 'zlib'
|
11
|
-
|
12
|
-
module Faraday
|
13
|
-
class Adapter
|
14
|
-
# Net::HTTP adapter.
|
15
|
-
class NetHttp < Faraday::Adapter
|
16
|
-
exceptions = [
|
17
|
-
IOError,
|
18
|
-
Errno::EADDRNOTAVAIL,
|
19
|
-
Errno::ECONNABORTED,
|
20
|
-
Errno::ECONNREFUSED,
|
21
|
-
Errno::ECONNRESET,
|
22
|
-
Errno::EHOSTUNREACH,
|
23
|
-
Errno::EINVAL,
|
24
|
-
Errno::ENETUNREACH,
|
25
|
-
Errno::EPIPE,
|
26
|
-
Net::HTTPBadResponse,
|
27
|
-
Net::HTTPHeaderSyntaxError,
|
28
|
-
Net::ProtocolError,
|
29
|
-
SocketError,
|
30
|
-
Zlib::GzipFile::Error
|
31
|
-
]
|
32
|
-
|
33
|
-
exceptions << OpenSSL::SSL::SSLError if defined?(OpenSSL)
|
34
|
-
exceptions << Net::OpenTimeout if defined?(Net::OpenTimeout)
|
35
|
-
|
36
|
-
NET_HTTP_EXCEPTIONS = exceptions.freeze
|
37
|
-
|
38
|
-
def initialize(app = nil, opts = {}, &block)
|
39
|
-
@ssl_cert_store = nil
|
40
|
-
super(app, opts, &block)
|
41
|
-
end
|
42
|
-
|
43
|
-
def build_connection(env)
|
44
|
-
net_http_connection(env).tap do |http|
|
45
|
-
if http.respond_to?(:use_ssl=)
|
46
|
-
http.use_ssl = env[:url].scheme == 'https'
|
47
|
-
end
|
48
|
-
configure_ssl(http, env[:ssl])
|
49
|
-
configure_request(http, env[:request])
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def net_http_connection(env)
|
54
|
-
klass = if (proxy = env[:request][:proxy])
|
55
|
-
Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port,
|
56
|
-
proxy[:user], proxy[:password])
|
57
|
-
else
|
58
|
-
Net::HTTP
|
59
|
-
end
|
60
|
-
port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
|
61
|
-
klass.new(env[:url].hostname, port)
|
62
|
-
end
|
63
|
-
|
64
|
-
def call(env)
|
65
|
-
super
|
66
|
-
http_response = connection(env) do |http|
|
67
|
-
begin
|
68
|
-
perform_request(http, env)
|
69
|
-
rescue *NET_HTTP_EXCEPTIONS => e
|
70
|
-
if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
|
71
|
-
raise Faraday::SSLError, e
|
72
|
-
end
|
73
|
-
|
74
|
-
raise Faraday::ConnectionFailed, e
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
save_response(env, http_response.code.to_i,
|
79
|
-
http_response.body || +'', nil,
|
80
|
-
http_response.message) do |response_headers|
|
81
|
-
http_response.each_header do |key, value|
|
82
|
-
response_headers[key] = value
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
@app.call env
|
87
|
-
rescue Timeout::Error, Errno::ETIMEDOUT => e
|
88
|
-
raise Faraday::TimeoutError, e
|
89
|
-
end
|
90
|
-
|
91
|
-
private
|
92
|
-
|
93
|
-
def create_request(env)
|
94
|
-
request = Net::HTTPGenericRequest.new \
|
95
|
-
env[:method].to_s.upcase, # request method
|
96
|
-
!!env[:body], # is there request body
|
97
|
-
env[:method] != :head, # is there response body
|
98
|
-
env[:url].request_uri, # request uri path
|
99
|
-
env[:request_headers] # request headers
|
100
|
-
|
101
|
-
if env[:body].respond_to?(:read)
|
102
|
-
request.body_stream = env[:body]
|
103
|
-
else
|
104
|
-
request.body = env[:body]
|
105
|
-
end
|
106
|
-
request
|
107
|
-
end
|
108
|
-
|
109
|
-
def perform_request(http, env)
|
110
|
-
if env[:request].stream_response?
|
111
|
-
size = 0
|
112
|
-
yielded = false
|
113
|
-
http_response = request_with_wrapped_block(http, env) do |chunk|
|
114
|
-
if chunk.bytesize.positive? || size.positive?
|
115
|
-
yielded = true
|
116
|
-
size += chunk.bytesize
|
117
|
-
env[:request].on_data.call(chunk, size)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
env[:request].on_data.call(+'', 0) unless yielded
|
121
|
-
# Net::HTTP returns something,
|
122
|
-
# but it's not meaningful according to the docs.
|
123
|
-
http_response.body = nil
|
124
|
-
http_response
|
125
|
-
else
|
126
|
-
request_with_wrapped_block(http, env)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def request_with_wrapped_block(http, env, &block)
|
131
|
-
if (env[:method] == :get) && !env[:body]
|
132
|
-
# prefer `get` to `request` because the former handles gzip (ruby 1.9)
|
133
|
-
request_via_get_method(http, env, &block)
|
134
|
-
else
|
135
|
-
request_via_request_method(http, env, &block)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
def request_via_get_method(http, env, &block)
|
140
|
-
http.get env[:url].request_uri, env[:request_headers], &block
|
141
|
-
end
|
142
|
-
|
143
|
-
def request_via_request_method(http, env, &block)
|
144
|
-
if block_given?
|
145
|
-
http.request create_request(env) do |response|
|
146
|
-
response.read_body(&block)
|
147
|
-
end
|
148
|
-
else
|
149
|
-
http.request create_request(env)
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
def configure_ssl(http, ssl)
|
154
|
-
return unless ssl
|
155
|
-
|
156
|
-
http.verify_mode = ssl_verify_mode(ssl)
|
157
|
-
http.cert_store = ssl_cert_store(ssl)
|
158
|
-
|
159
|
-
http.cert = ssl[:client_cert] if ssl[:client_cert]
|
160
|
-
http.key = ssl[:client_key] if ssl[:client_key]
|
161
|
-
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
|
162
|
-
http.ca_path = ssl[:ca_path] if ssl[:ca_path]
|
163
|
-
http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
164
|
-
http.ssl_version = ssl[:version] if ssl[:version]
|
165
|
-
http.min_version = ssl[:min_version] if ssl[:min_version]
|
166
|
-
http.max_version = ssl[:max_version] if ssl[:max_version]
|
167
|
-
end
|
168
|
-
|
169
|
-
def configure_request(http, req)
|
170
|
-
if (sec = request_timeout(:read, req))
|
171
|
-
http.read_timeout = sec
|
172
|
-
end
|
173
|
-
|
174
|
-
if (sec = http.respond_to?(:write_timeout=) &&
|
175
|
-
request_timeout(:write, req))
|
176
|
-
http.write_timeout = sec
|
177
|
-
end
|
178
|
-
|
179
|
-
if (sec = request_timeout(:open, req))
|
180
|
-
http.open_timeout = sec
|
181
|
-
end
|
182
|
-
|
183
|
-
# Only set if Net::Http supports it, since Ruby 2.5.
|
184
|
-
http.max_retries = 0 if http.respond_to?(:max_retries=)
|
185
|
-
|
186
|
-
@config_block&.call(http)
|
187
|
-
end
|
188
|
-
|
189
|
-
def ssl_cert_store(ssl)
|
190
|
-
return ssl[:cert_store] if ssl[:cert_store]
|
191
|
-
|
192
|
-
@ssl_cert_store ||= begin
|
193
|
-
# Use the default cert store by default, i.e. system ca certs
|
194
|
-
OpenSSL::X509::Store.new.tap(&:set_default_paths)
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
def ssl_verify_mode(ssl)
|
199
|
-
ssl[:verify_mode] || begin
|
200
|
-
if ssl.fetch(:verify, true)
|
201
|
-
OpenSSL::SSL::VERIFY_PEER
|
202
|
-
else
|
203
|
-
OpenSSL::SSL::VERIFY_NONE
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
209
|
-
end
|