faraday 0.17.5 → 2.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 -8
- data/LICENSE.md +1 -1
- data/README.md +24 -364
- data/Rakefile +1 -7
- data/examples/client_spec.rb +97 -0
- data/examples/client_test.rb +118 -0
- data/lib/faraday/adapter/test.rb +127 -72
- data/lib/faraday/adapter.rb +69 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/connection.rb +309 -232
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +183 -0
- data/lib/faraday/error.rb +31 -42
- data/lib/faraday/logging/formatter.rb +106 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +18 -25
- data/lib/faraday/middleware_registry.rb +83 -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 +38 -193
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +91 -76
- data/lib/faraday/request/authorization.rb +37 -29
- data/lib/faraday/request/instrumentation.rb +47 -27
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +48 -24
- data/lib/faraday/request.rb +64 -44
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +57 -18
- data/lib/faraday/response.rb +25 -32
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +47 -251
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +108 -198
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/connection_spec.rb +787 -0
- data/spec/faraday/error_spec.rb +12 -54
- data/spec/faraday/middleware_registry_spec.rb +31 -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 +150 -0
- data/spec/faraday/rack_builder_spec.rb +317 -0
- data/spec/faraday/request/authorization_spec.rb +83 -0
- data/spec/faraday/request/instrumentation_spec.rb +74 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +93 -0
- data/spec/faraday/request_spec.rb +109 -0
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/raise_error_spec.rb +81 -15
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +117 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +63 -36
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +96 -0
- data/spec/support/shared_examples/adapter.rb +104 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +249 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- metadata +74 -63
- 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 -82
- data/lib/faraday/adapter/httpclient.rb +0 -128
- data/lib/faraday/adapter/net_http.rb +0 -153
- data/lib/faraday/adapter/net_http_persistent.rb +0 -68
- data/lib/faraday/adapter/patron.rb +0 -95
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/adapter/typhoeus.rb +0 -12
- data/lib/faraday/autoload.rb +0 -84
- data/lib/faraday/deprecate.rb +0 -109
- data/lib/faraday/request/basic_authentication.rb +0 -13
- data/lib/faraday/request/multipart.rb +0 -68
- data/lib/faraday/request/retry.rb +0 -213
- data/lib/faraday/request/token_authentication.rb +0 -15
- data/lib/faraday/upload_io.rb +0 -67
- data/spec/faraday/deprecate_spec.rb +0 -147
- 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,82 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Adapter
|
3
|
-
class Excon < Faraday::Adapter
|
4
|
-
dependency 'excon'
|
5
|
-
|
6
|
-
def call(env)
|
7
|
-
super
|
8
|
-
|
9
|
-
opts = {}
|
10
|
-
if env[:url].scheme == 'https' && ssl = env[:ssl]
|
11
|
-
opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
|
12
|
-
opts[:ssl_ca_path] = ssl[:ca_path] if ssl[:ca_path]
|
13
|
-
opts[:ssl_ca_file] = ssl[:ca_file] if ssl[:ca_file]
|
14
|
-
opts[:client_cert] = ssl[:client_cert] if ssl[:client_cert]
|
15
|
-
opts[:client_key] = ssl[:client_key] if ssl[:client_key]
|
16
|
-
opts[:certificate] = ssl[:certificate] if ssl[:certificate]
|
17
|
-
opts[:private_key] = ssl[:private_key] if ssl[:private_key]
|
18
|
-
opts[:ssl_version] = ssl[:version] if ssl[:version]
|
19
|
-
opts[:ssl_min_version] = ssl[:min_version] if ssl[:min_version]
|
20
|
-
opts[:ssl_max_version] = ssl[:max_version] if ssl[:max_version]
|
21
|
-
|
22
|
-
# https://github.com/geemus/excon/issues/106
|
23
|
-
# https://github.com/jruby/jruby-ossl/issues/19
|
24
|
-
opts[:nonblock] = false
|
25
|
-
end
|
26
|
-
|
27
|
-
if ( req = env[:request] )
|
28
|
-
if req[:timeout]
|
29
|
-
opts[:read_timeout] = req[:timeout]
|
30
|
-
opts[:connect_timeout] = req[:timeout]
|
31
|
-
opts[:write_timeout] = req[:timeout]
|
32
|
-
end
|
33
|
-
|
34
|
-
if req[:open_timeout]
|
35
|
-
opts[:connect_timeout] = req[:open_timeout]
|
36
|
-
end
|
37
|
-
|
38
|
-
if req[:proxy]
|
39
|
-
opts[:proxy] = {
|
40
|
-
:host => req[:proxy][:uri].host,
|
41
|
-
:hostname => req[:proxy][:uri].hostname,
|
42
|
-
:port => req[:proxy][:uri].port,
|
43
|
-
:scheme => req[:proxy][:uri].scheme,
|
44
|
-
:user => req[:proxy][:user],
|
45
|
-
:password => req[:proxy][:password]
|
46
|
-
}
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
conn = create_connection(env, opts)
|
51
|
-
|
52
|
-
resp = conn.request \
|
53
|
-
:method => env[:method].to_s.upcase,
|
54
|
-
:headers => env[:request_headers],
|
55
|
-
:body => read_body(env)
|
56
|
-
|
57
|
-
save_response(env, resp.status.to_i, resp.body, resp.headers, resp.reason_phrase)
|
58
|
-
|
59
|
-
@app.call env
|
60
|
-
rescue ::Excon::Errors::SocketError => err
|
61
|
-
if err.message =~ /\btimeout\b/
|
62
|
-
raise Faraday::TimeoutError, err
|
63
|
-
elsif err.message =~ /\bcertificate\b/
|
64
|
-
raise Faraday::SSLError, err
|
65
|
-
else
|
66
|
-
raise Faraday::ConnectionFailed, err
|
67
|
-
end
|
68
|
-
rescue ::Excon::Errors::Timeout => err
|
69
|
-
raise Faraday::TimeoutError, err
|
70
|
-
end
|
71
|
-
|
72
|
-
def create_connection(env, opts)
|
73
|
-
::Excon.new(env[:url].to_s, opts.merge(@connection_options))
|
74
|
-
end
|
75
|
-
|
76
|
-
# TODO: support streaming requests
|
77
|
-
def read_body(env)
|
78
|
-
env[:body].respond_to?(:read) ? env[:body].read : env[:body]
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
@@ -1,128 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Adapter
|
3
|
-
class HTTPClient < Faraday::Adapter
|
4
|
-
dependency 'httpclient'
|
5
|
-
|
6
|
-
def client
|
7
|
-
@client ||= ::HTTPClient.new
|
8
|
-
end
|
9
|
-
|
10
|
-
def call(env)
|
11
|
-
super
|
12
|
-
|
13
|
-
# enable compression
|
14
|
-
client.transparent_gzip_decompression = true
|
15
|
-
|
16
|
-
if req = env[:request]
|
17
|
-
if proxy = req[:proxy]
|
18
|
-
configure_proxy proxy
|
19
|
-
end
|
20
|
-
|
21
|
-
if bind = req[:bind]
|
22
|
-
configure_socket bind
|
23
|
-
end
|
24
|
-
|
25
|
-
configure_timeouts req
|
26
|
-
end
|
27
|
-
|
28
|
-
if env[:url].scheme == 'https' && ssl = env[:ssl]
|
29
|
-
configure_ssl ssl
|
30
|
-
end
|
31
|
-
|
32
|
-
configure_client
|
33
|
-
|
34
|
-
# TODO Don't stream yet.
|
35
|
-
# https://github.com/nahi/httpclient/pull/90
|
36
|
-
env[:body] = env[:body].read if env[:body].respond_to? :read
|
37
|
-
|
38
|
-
resp = client.request env[:method], env[:url],
|
39
|
-
:body => env[:body],
|
40
|
-
:header => env[:request_headers]
|
41
|
-
|
42
|
-
save_response env, resp.status, resp.body, resp.headers, resp.reason
|
43
|
-
|
44
|
-
@app.call env
|
45
|
-
rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
|
46
|
-
raise Faraday::TimeoutError, $!
|
47
|
-
rescue ::HTTPClient::BadResponseError => err
|
48
|
-
if err.message.include?('status 407')
|
49
|
-
raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
|
50
|
-
else
|
51
|
-
raise Faraday::ClientError, $!
|
52
|
-
end
|
53
|
-
rescue Errno::ECONNREFUSED, IOError, SocketError
|
54
|
-
raise Faraday::ConnectionFailed, $!
|
55
|
-
rescue => err
|
56
|
-
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
|
57
|
-
raise Faraday::SSLError, err
|
58
|
-
else
|
59
|
-
raise
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def configure_socket(bind)
|
64
|
-
client.socket_local.host = bind[:host]
|
65
|
-
client.socket_local.port = bind[:port]
|
66
|
-
end
|
67
|
-
|
68
|
-
def configure_proxy(proxy)
|
69
|
-
client.proxy = proxy[:uri]
|
70
|
-
if proxy[:user] && proxy[:password]
|
71
|
-
client.set_proxy_auth proxy[:user], proxy[:password]
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def configure_ssl(ssl)
|
76
|
-
ssl_config = client.ssl_config
|
77
|
-
ssl_config.verify_mode = ssl_verify_mode(ssl)
|
78
|
-
ssl_config.cert_store = ssl_cert_store(ssl)
|
79
|
-
|
80
|
-
ssl_config.add_trust_ca ssl[:ca_file] if ssl[:ca_file]
|
81
|
-
ssl_config.add_trust_ca ssl[:ca_path] if ssl[:ca_path]
|
82
|
-
ssl_config.client_cert = ssl[:client_cert] if ssl[:client_cert]
|
83
|
-
ssl_config.client_key = ssl[:client_key] if ssl[:client_key]
|
84
|
-
ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
85
|
-
end
|
86
|
-
|
87
|
-
def configure_timeouts(req)
|
88
|
-
if req[:timeout]
|
89
|
-
client.connect_timeout = req[:timeout]
|
90
|
-
client.receive_timeout = req[:timeout]
|
91
|
-
client.send_timeout = req[:timeout]
|
92
|
-
end
|
93
|
-
|
94
|
-
if req[:open_timeout]
|
95
|
-
client.connect_timeout = req[:open_timeout]
|
96
|
-
client.send_timeout = req[:open_timeout]
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
def configure_client
|
101
|
-
@config_block.call(client) if @config_block
|
102
|
-
end
|
103
|
-
|
104
|
-
def ssl_cert_store(ssl)
|
105
|
-
return ssl[:cert_store] if ssl[:cert_store]
|
106
|
-
# Memoize the cert store so that the same one is passed to
|
107
|
-
# HTTPClient each time, to avoid resyncing SSL sesions when
|
108
|
-
# it's changed
|
109
|
-
@cert_store ||= begin
|
110
|
-
# Use the default cert store by default, i.e. system ca certs
|
111
|
-
cert_store = OpenSSL::X509::Store.new
|
112
|
-
cert_store.set_default_paths
|
113
|
-
cert_store
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def ssl_verify_mode(ssl)
|
118
|
-
ssl[:verify_mode] || begin
|
119
|
-
if ssl.fetch(:verify, true)
|
120
|
-
OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
|
121
|
-
else
|
122
|
-
OpenSSL::SSL::VERIFY_NONE
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
@@ -1,153 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'net/https'
|
3
|
-
rescue LoadError
|
4
|
-
warn "Warning: no such file to load -- net/https. Make sure openssl is installed if you want ssl support"
|
5
|
-
require 'net/http'
|
6
|
-
end
|
7
|
-
require 'zlib'
|
8
|
-
|
9
|
-
module Faraday
|
10
|
-
class Adapter
|
11
|
-
class NetHttp < Faraday::Adapter
|
12
|
-
NET_HTTP_EXCEPTIONS = [
|
13
|
-
IOError,
|
14
|
-
Errno::EADDRNOTAVAIL,
|
15
|
-
Errno::ECONNABORTED,
|
16
|
-
Errno::ECONNREFUSED,
|
17
|
-
Errno::ECONNRESET,
|
18
|
-
Errno::EHOSTUNREACH,
|
19
|
-
Errno::EINVAL,
|
20
|
-
Errno::ENETUNREACH,
|
21
|
-
Errno::EPIPE,
|
22
|
-
Net::HTTPBadResponse,
|
23
|
-
Net::HTTPHeaderSyntaxError,
|
24
|
-
Net::ProtocolError,
|
25
|
-
SocketError,
|
26
|
-
Zlib::GzipFile::Error,
|
27
|
-
]
|
28
|
-
|
29
|
-
NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)
|
30
|
-
NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout)
|
31
|
-
|
32
|
-
def initialize(app = nil, opts = {}, &block)
|
33
|
-
@cert_store = nil
|
34
|
-
super(app, opts, &block)
|
35
|
-
end
|
36
|
-
|
37
|
-
def call(env)
|
38
|
-
super
|
39
|
-
with_net_http_connection(env) do |http|
|
40
|
-
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
|
41
|
-
configure_request(http, env[:request])
|
42
|
-
|
43
|
-
begin
|
44
|
-
http_response = perform_request(http, env)
|
45
|
-
rescue *NET_HTTP_EXCEPTIONS => err
|
46
|
-
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
|
47
|
-
raise Faraday::SSLError, err
|
48
|
-
else
|
49
|
-
raise Faraday::ConnectionFailed, err
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
|
54
|
-
http_response.each_header do |key, value|
|
55
|
-
response_headers[key] = value
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
@app.call env
|
61
|
-
rescue Timeout::Error, Errno::ETIMEDOUT => err
|
62
|
-
raise Faraday::TimeoutError, err
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
|
67
|
-
def create_request(env)
|
68
|
-
request = Net::HTTPGenericRequest.new \
|
69
|
-
env[:method].to_s.upcase, # request method
|
70
|
-
!!env[:body], # is there request body
|
71
|
-
:head != env[:method], # is there response body
|
72
|
-
env[:url].request_uri, # request uri path
|
73
|
-
env[:request_headers] # request headers
|
74
|
-
|
75
|
-
if env[:body].respond_to?(:read)
|
76
|
-
request.body_stream = env[:body]
|
77
|
-
else
|
78
|
-
request.body = env[:body]
|
79
|
-
end
|
80
|
-
request
|
81
|
-
end
|
82
|
-
|
83
|
-
def perform_request(http, env)
|
84
|
-
if :get == env[:method] and !env[:body]
|
85
|
-
# prefer `get` to `request` because the former handles gzip (ruby 1.9)
|
86
|
-
http.get env[:url].request_uri, env[:request_headers]
|
87
|
-
else
|
88
|
-
http.request create_request(env)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def with_net_http_connection(env)
|
93
|
-
yield net_http_connection(env)
|
94
|
-
end
|
95
|
-
|
96
|
-
def net_http_connection(env)
|
97
|
-
if proxy = env[:request][:proxy]
|
98
|
-
Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port, proxy[:user], proxy[:password])
|
99
|
-
else
|
100
|
-
Net::HTTP
|
101
|
-
end.new(env[:url].hostname, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80))
|
102
|
-
end
|
103
|
-
|
104
|
-
def configure_ssl(http, ssl)
|
105
|
-
http.use_ssl = true
|
106
|
-
http.verify_mode = ssl_verify_mode(ssl)
|
107
|
-
http.cert_store = ssl_cert_store(ssl)
|
108
|
-
|
109
|
-
http.cert = ssl[:client_cert] if ssl[:client_cert]
|
110
|
-
http.key = ssl[:client_key] if ssl[:client_key]
|
111
|
-
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
|
112
|
-
http.ca_path = ssl[:ca_path] if ssl[:ca_path]
|
113
|
-
http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
114
|
-
http.ssl_version = ssl[:version] if ssl[:version]
|
115
|
-
http.min_version = ssl[:min_version] if ssl[:min_version]
|
116
|
-
http.max_version = ssl[:max_version] if ssl[:max_version]
|
117
|
-
end
|
118
|
-
|
119
|
-
def configure_request(http, req)
|
120
|
-
if req[:timeout]
|
121
|
-
http.read_timeout = req[:timeout]
|
122
|
-
http.open_timeout = req[:timeout]
|
123
|
-
http.write_timeout = req[:timeout] if http.respond_to?(:write_timeout=)
|
124
|
-
end
|
125
|
-
http.open_timeout = req[:open_timeout] if req[:open_timeout]
|
126
|
-
http.write_timeout = req[:write_timeout] if req[:write_timeout] && http.respond_to?(:write_timeout=)
|
127
|
-
# Only set if Net::Http supports it, since Ruby 2.5.
|
128
|
-
http.max_retries = 0 if http.respond_to?(:max_retries=)
|
129
|
-
|
130
|
-
@config_block.call(http) if @config_block
|
131
|
-
end
|
132
|
-
|
133
|
-
def ssl_cert_store(ssl)
|
134
|
-
return ssl[:cert_store] if ssl[:cert_store]
|
135
|
-
return @cert_store if @cert_store
|
136
|
-
# Use the default cert store by default, i.e. system ca certs
|
137
|
-
@cert_store = OpenSSL::X509::Store.new
|
138
|
-
@cert_store.set_default_paths
|
139
|
-
@cert_store
|
140
|
-
end
|
141
|
-
|
142
|
-
def ssl_verify_mode(ssl)
|
143
|
-
ssl[:verify_mode] || begin
|
144
|
-
if ssl.fetch(:verify, true)
|
145
|
-
OpenSSL::SSL::VERIFY_PEER
|
146
|
-
else
|
147
|
-
OpenSSL::SSL::VERIFY_NONE
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Adapter
|
3
|
-
class NetHttpPersistent < NetHttp
|
4
|
-
dependency 'net/http/persistent'
|
5
|
-
|
6
|
-
private
|
7
|
-
|
8
|
-
def net_http_connection(env)
|
9
|
-
@cached_connection ||=
|
10
|
-
if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
|
11
|
-
options = {name: 'Faraday'}
|
12
|
-
options[:pool_size] = @connection_options[:pool_size] if @connection_options.key?(:pool_size)
|
13
|
-
Net::HTTP::Persistent.new(**options)
|
14
|
-
else
|
15
|
-
Net::HTTP::Persistent.new('Faraday')
|
16
|
-
end
|
17
|
-
|
18
|
-
proxy_uri = proxy_uri(env)
|
19
|
-
@cached_connection.proxy = proxy_uri if @cached_connection.proxy_uri != proxy_uri
|
20
|
-
@cached_connection
|
21
|
-
end
|
22
|
-
|
23
|
-
def proxy_uri(env)
|
24
|
-
proxy_uri = nil
|
25
|
-
if (proxy = env[:request][:proxy])
|
26
|
-
proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
|
27
|
-
proxy_uri.user = proxy_uri.password = nil
|
28
|
-
# awful patch for net-http-persistent 2.8 not unescaping user/password
|
29
|
-
(class << proxy_uri; self; end).class_eval do
|
30
|
-
define_method(:user) { proxy[:user] }
|
31
|
-
define_method(:password) { proxy[:password] }
|
32
|
-
end if proxy[:user]
|
33
|
-
end
|
34
|
-
proxy_uri
|
35
|
-
end
|
36
|
-
|
37
|
-
def perform_request(http, env)
|
38
|
-
http.request env[:url], create_request(env)
|
39
|
-
rescue Errno::ETIMEDOUT => error
|
40
|
-
raise Faraday::TimeoutError, error
|
41
|
-
rescue Net::HTTP::Persistent::Error => error
|
42
|
-
if error.message.include? 'Timeout'
|
43
|
-
raise Faraday::TimeoutError, error
|
44
|
-
elsif error.message.include? 'connection refused'
|
45
|
-
raise Faraday::ConnectionFailed, error
|
46
|
-
else
|
47
|
-
raise
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def configure_ssl(http, ssl)
|
52
|
-
http_set(http, :verify_mode, ssl_verify_mode(ssl))
|
53
|
-
http_set(http, :cert_store, ssl_cert_store(ssl))
|
54
|
-
|
55
|
-
http_set(http, :certificate, ssl[:client_cert]) if ssl[:client_cert]
|
56
|
-
http_set(http, :private_key, ssl[:client_key]) if ssl[:client_key]
|
57
|
-
http_set(http, :ca_file, ssl[:ca_file]) if ssl[:ca_file]
|
58
|
-
http_set(http, :ssl_version, ssl[:version]) if ssl[:version]
|
59
|
-
end
|
60
|
-
|
61
|
-
def http_set(http, attr, value)
|
62
|
-
if http.send(attr) != value
|
63
|
-
http.send("#{attr}=", value)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
@@ -1,95 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Adapter
|
3
|
-
class Patron < Faraday::Adapter
|
4
|
-
dependency 'patron'
|
5
|
-
|
6
|
-
def call(env)
|
7
|
-
super
|
8
|
-
# TODO: support streaming requests
|
9
|
-
env[:body] = env[:body].read if env[:body].respond_to? :read
|
10
|
-
|
11
|
-
session = ::Patron::Session.new
|
12
|
-
@config_block.call(session) if @config_block
|
13
|
-
configure_ssl(session, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
|
14
|
-
|
15
|
-
if req = env[:request]
|
16
|
-
session.timeout = session.connect_timeout = req[:timeout] if req[:timeout]
|
17
|
-
session.connect_timeout = req[:open_timeout] if req[:open_timeout]
|
18
|
-
|
19
|
-
if proxy = req[:proxy]
|
20
|
-
proxy_uri = proxy[:uri].dup
|
21
|
-
proxy_uri.user = proxy[:user] && Utils.escape(proxy[:user]).gsub('+', '%20')
|
22
|
-
proxy_uri.password = proxy[:password] && Utils.escape(proxy[:password]).gsub('+', '%20')
|
23
|
-
session.proxy = proxy_uri.to_s
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
response = begin
|
28
|
-
data = env[:body] ? env[:body].to_s : nil
|
29
|
-
session.request(env[:method], env[:url].to_s, env[:request_headers], :data => data)
|
30
|
-
rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
|
31
|
-
raise Faraday::ConnectionFailed, $!
|
32
|
-
end
|
33
|
-
|
34
|
-
# Remove the "HTTP/1.1 200", leaving just the reason phrase
|
35
|
-
reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
|
36
|
-
|
37
|
-
save_response(env, response.status, response.body, response.headers, reason_phrase)
|
38
|
-
|
39
|
-
@app.call env
|
40
|
-
rescue ::Patron::TimeoutError => err
|
41
|
-
if connection_timed_out_message?(err.message)
|
42
|
-
raise Faraday::ConnectionFailed, err
|
43
|
-
else
|
44
|
-
raise Faraday::TimeoutError, err
|
45
|
-
end
|
46
|
-
rescue ::Patron::Error => err
|
47
|
-
if err.message.include?("code 407")
|
48
|
-
raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
|
49
|
-
else
|
50
|
-
raise Faraday::ConnectionFailed, err
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
|
55
|
-
# HAX: helps but doesn't work completely
|
56
|
-
# https://github.com/toland/patron/issues/34
|
57
|
-
::Patron::Request::VALID_ACTIONS.tap do |actions|
|
58
|
-
if actions[0].is_a?(Symbol)
|
59
|
-
actions << :patch unless actions.include? :patch
|
60
|
-
actions << :options unless actions.include? :options
|
61
|
-
else
|
62
|
-
# Patron 0.4.20 and up
|
63
|
-
actions << "PATCH" unless actions.include? "PATCH"
|
64
|
-
actions << "OPTIONS" unless actions.include? "OPTIONS"
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def configure_ssl(session, ssl)
|
70
|
-
if ssl.fetch(:verify, true)
|
71
|
-
session.cacert = ssl[:ca_file]
|
72
|
-
else
|
73
|
-
session.insecure = true
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
78
|
-
|
79
|
-
CURL_TIMEOUT_MESSAGES = [ "Connection time-out",
|
80
|
-
"Connection timed out",
|
81
|
-
"Timed out before name resolve",
|
82
|
-
"server connect has timed out",
|
83
|
-
"Resolving timed out",
|
84
|
-
"name lookup timed out",
|
85
|
-
"timed out before SSL",
|
86
|
-
"connect() timed out"
|
87
|
-
].freeze
|
88
|
-
|
89
|
-
def connection_timed_out_message?(message)
|
90
|
-
CURL_TIMEOUT_MESSAGES.any? { |curl_message| message.include?(curl_message) }
|
91
|
-
end
|
92
|
-
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
data/lib/faraday/adapter/rack.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Adapter
|
3
|
-
# Sends requests to a Rack app.
|
4
|
-
#
|
5
|
-
# Examples
|
6
|
-
#
|
7
|
-
# class MyRackApp
|
8
|
-
# def call(env)
|
9
|
-
# [200, {'Content-Type' => 'text/html'}, ["hello world"]]
|
10
|
-
# end
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# Faraday.new do |conn|
|
14
|
-
# conn.adapter :rack, MyRackApp.new
|
15
|
-
# end
|
16
|
-
class Rack < Faraday::Adapter
|
17
|
-
dependency 'rack/test'
|
18
|
-
|
19
|
-
# not prefixed with "HTTP_"
|
20
|
-
SPECIAL_HEADERS = %w[ CONTENT_LENGTH CONTENT_TYPE ]
|
21
|
-
|
22
|
-
def initialize(faraday_app, rack_app)
|
23
|
-
super(faraday_app)
|
24
|
-
mock_session = ::Rack::MockSession.new(rack_app)
|
25
|
-
@session = ::Rack::Test::Session.new(mock_session)
|
26
|
-
end
|
27
|
-
|
28
|
-
def call(env)
|
29
|
-
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
|
-
}
|
35
|
-
|
36
|
-
env[:request_headers].each do |name, value|
|
37
|
-
name = name.upcase.tr('-', '_')
|
38
|
-
name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
|
39
|
-
rack_env[name] = value
|
40
|
-
end if env[:request_headers]
|
41
|
-
|
42
|
-
timeout = env[:request][:timeout] || env[:request][:open_timeout]
|
43
|
-
response = if timeout
|
44
|
-
Timer.timeout(timeout, Faraday::TimeoutError) { execute_request(env, rack_env) }
|
45
|
-
else
|
46
|
-
execute_request(env, rack_env)
|
47
|
-
end
|
48
|
-
|
49
|
-
save_response(env, response.status, response.body, response.headers)
|
50
|
-
@app.call env
|
51
|
-
end
|
52
|
-
|
53
|
-
def execute_request(env, rack_env)
|
54
|
-
@session.request(env[:url].to_s, rack_env)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Adapter
|
3
|
-
# This class is just a stub, the real adapter is in https://github.com/philsturgeon/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
|
4
|
-
class Typhoeus < Faraday::Adapter
|
5
|
-
# Needs to define this method in order to support Typhoeus <= 1.3.0
|
6
|
-
def call; end
|
7
|
-
|
8
|
-
dependency 'typhoeus'
|
9
|
-
dependency 'typhoeus/adapters/faraday'
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
data/lib/faraday/autoload.rb
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
# Internal: Adds the ability for other modules to manage autoloadable
|
3
|
-
# constants.
|
4
|
-
module AutoloadHelper
|
5
|
-
# Internal: Registers the constants to be auto loaded.
|
6
|
-
#
|
7
|
-
# prefix - The String require prefix. If the path is inside Faraday, then
|
8
|
-
# it will be prefixed with the root path of this loaded Faraday
|
9
|
-
# version.
|
10
|
-
# options - Hash of Symbol => String library names.
|
11
|
-
#
|
12
|
-
# Examples.
|
13
|
-
#
|
14
|
-
# Faraday.autoload_all 'faraday/foo',
|
15
|
-
# :Bar => 'bar'
|
16
|
-
#
|
17
|
-
# # requires faraday/foo/bar to load Faraday::Bar.
|
18
|
-
# Faraday::Bar
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# Returns nothing.
|
22
|
-
def autoload_all(prefix, options)
|
23
|
-
if prefix =~ /^faraday(\/|$)/i
|
24
|
-
prefix = File.join(Faraday.root_path, prefix)
|
25
|
-
end
|
26
|
-
options.each do |const_name, path|
|
27
|
-
autoload const_name, File.join(prefix, path)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
# Internal: Loads each autoloaded constant. If thread safety is a concern,
|
32
|
-
# wrap this in a Mutex.
|
33
|
-
#
|
34
|
-
# Returns nothing.
|
35
|
-
def load_autoloaded_constants
|
36
|
-
constants.each do |const|
|
37
|
-
const_get(const) if autoload?(const)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# Internal: Filters the module's contents with those that have been already
|
42
|
-
# autoloaded.
|
43
|
-
#
|
44
|
-
# Returns an Array of Class/Module objects.
|
45
|
-
def all_loaded_constants
|
46
|
-
constants.map { |c| const_get(c) }.
|
47
|
-
select { |a| a.respond_to?(:loaded?) && a.loaded? }
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
class Adapter
|
52
|
-
extend AutoloadHelper
|
53
|
-
autoload_all 'faraday/adapter',
|
54
|
-
:NetHttp => 'net_http',
|
55
|
-
:NetHttpPersistent => 'net_http_persistent',
|
56
|
-
:EMSynchrony => 'em_synchrony',
|
57
|
-
:EMHttp => 'em_http',
|
58
|
-
:Typhoeus => 'typhoeus',
|
59
|
-
:Patron => 'patron',
|
60
|
-
:Excon => 'excon',
|
61
|
-
:Test => 'test',
|
62
|
-
:Rack => 'rack',
|
63
|
-
:HTTPClient => 'httpclient'
|
64
|
-
end
|
65
|
-
|
66
|
-
class Request
|
67
|
-
extend AutoloadHelper
|
68
|
-
autoload_all 'faraday/request',
|
69
|
-
:UrlEncoded => 'url_encoded',
|
70
|
-
:Multipart => 'multipart',
|
71
|
-
:Retry => 'retry',
|
72
|
-
:Authorization => 'authorization',
|
73
|
-
:BasicAuthentication => 'basic_authentication',
|
74
|
-
:TokenAuthentication => 'token_authentication',
|
75
|
-
:Instrumentation => 'instrumentation'
|
76
|
-
end
|
77
|
-
|
78
|
-
class Response
|
79
|
-
extend AutoloadHelper
|
80
|
-
autoload_all 'faraday/response',
|
81
|
-
:RaiseError => 'raise_error',
|
82
|
-
:Logger => 'logger'
|
83
|
-
end
|
84
|
-
end
|