faraday 0.12.0 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +380 -0
- data/LICENSE.md +1 -1
- data/README.md +21 -308
- data/Rakefile +7 -0
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday/adapter/httpclient.rb +83 -59
- data/lib/faraday/adapter/patron.rb +92 -36
- data/lib/faraday/adapter/rack.rb +30 -13
- data/lib/faraday/adapter/test.rb +103 -62
- data/lib/faraday/adapter/typhoeus.rb +7 -115
- data/lib/faraday/adapter.rb +77 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +42 -36
- data/lib/faraday/connection.rb +345 -179
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
- data/lib/faraday/error.rb +127 -38
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +19 -25
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +39 -193
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -196
- data/lib/faraday/rack_builder.rb +84 -48
- data/lib/faraday/request/authorization.rb +44 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +86 -48
- data/lib/faraday/request/retry.rb +209 -133
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/request.rb +94 -32
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +49 -14
- data/lib/faraday/response.rb +27 -23
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +38 -238
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +124 -187
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +47 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/test_spec.rb +260 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +736 -0
- data/spec/faraday/error_spec.rb +60 -0
- data/spec/faraday/middleware_spec.rb +52 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +44 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +142 -0
- data/spec/faraday/rack_builder_spec.rb +345 -0
- data/spec/faraday/request/authorization_spec.rb +88 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/multipart_spec.rb +302 -0
- data/spec/faraday/request/retry_spec.rb +242 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +120 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +169 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +132 -0
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +133 -0
- data/spec/support/shared_examples/adapter.rb +105 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +262 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +164 -16
- data/lib/faraday/adapter/em_http.rb +0 -243
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -106
- data/lib/faraday/adapter/excon.rb +0 -80
- data/lib/faraday/adapter/net_http.rb +0 -135
- data/lib/faraday/adapter/net_http_persistent.rb +0 -50
- data/lib/faraday/upload_io.rb +0 -67
@@ -1,243 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Adapter
|
3
|
-
# EventMachine adapter is useful for either asynchronous requests
|
4
|
-
# when in EM reactor loop or for making parallel requests in
|
5
|
-
# synchronous code.
|
6
|
-
class EMHttp < Faraday::Adapter
|
7
|
-
module Options
|
8
|
-
def connection_config(env)
|
9
|
-
options = {}
|
10
|
-
configure_proxy(options, env)
|
11
|
-
configure_timeout(options, env)
|
12
|
-
configure_socket(options, env)
|
13
|
-
configure_ssl(options, env)
|
14
|
-
options
|
15
|
-
end
|
16
|
-
|
17
|
-
def request_config(env)
|
18
|
-
options = {
|
19
|
-
:body => read_body(env),
|
20
|
-
:head => env[:request_headers],
|
21
|
-
# :keepalive => true,
|
22
|
-
# :file => 'path/to/file', # stream data off disk
|
23
|
-
}
|
24
|
-
configure_compression(options, env)
|
25
|
-
options
|
26
|
-
end
|
27
|
-
|
28
|
-
def read_body(env)
|
29
|
-
body = env[:body]
|
30
|
-
body.respond_to?(:read) ? body.read : body
|
31
|
-
end
|
32
|
-
|
33
|
-
def configure_proxy(options, env)
|
34
|
-
if proxy = request_options(env)[:proxy]
|
35
|
-
options[:proxy] = {
|
36
|
-
:host => proxy[:uri].host,
|
37
|
-
:port => proxy[:uri].port,
|
38
|
-
:authorization => [proxy[:user], proxy[:password]]
|
39
|
-
}
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def configure_socket(options, env)
|
44
|
-
if bind = request_options(env)[:bind]
|
45
|
-
options[:bind] = {
|
46
|
-
:host => bind[:host],
|
47
|
-
:port => bind[:port]
|
48
|
-
}
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def configure_ssl(options, env)
|
53
|
-
if env[:url].scheme == 'https' && env[:ssl]
|
54
|
-
options[:ssl] = {
|
55
|
-
:cert_chain_file => env[:ssl][:ca_file],
|
56
|
-
:verify_peer => env[:ssl].fetch(:verify, true)
|
57
|
-
}
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def configure_timeout(options, env)
|
62
|
-
timeout, open_timeout = request_options(env).values_at(:timeout, :open_timeout)
|
63
|
-
options[:connect_timeout] = options[:inactivity_timeout] = timeout
|
64
|
-
options[:connect_timeout] = open_timeout if open_timeout
|
65
|
-
end
|
66
|
-
|
67
|
-
def configure_compression(options, env)
|
68
|
-
if env[:method] == :get and not options[:head].key? 'accept-encoding'
|
69
|
-
options[:head]['accept-encoding'] = 'gzip, compressed'
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def request_options(env)
|
74
|
-
env[:request]
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
include Options
|
79
|
-
|
80
|
-
dependency 'em-http'
|
81
|
-
|
82
|
-
self.supports_parallel = true
|
83
|
-
|
84
|
-
def self.setup_parallel_manager(options = nil)
|
85
|
-
Manager.new
|
86
|
-
end
|
87
|
-
|
88
|
-
def call(env)
|
89
|
-
super
|
90
|
-
perform_request env
|
91
|
-
@app.call env
|
92
|
-
end
|
93
|
-
|
94
|
-
def perform_request(env)
|
95
|
-
if parallel?(env)
|
96
|
-
manager = env[:parallel_manager]
|
97
|
-
manager.add {
|
98
|
-
perform_single_request(env).
|
99
|
-
callback { env[:response].finish(env) }
|
100
|
-
}
|
101
|
-
else
|
102
|
-
unless EventMachine.reactor_running?
|
103
|
-
error = nil
|
104
|
-
# start EM, block until request is completed
|
105
|
-
EventMachine.run do
|
106
|
-
perform_single_request(env).
|
107
|
-
callback { EventMachine.stop }.
|
108
|
-
errback { |client|
|
109
|
-
error = error_message(client)
|
110
|
-
EventMachine.stop
|
111
|
-
}
|
112
|
-
end
|
113
|
-
raise_error(error) if error
|
114
|
-
else
|
115
|
-
# EM is running: instruct upstream that this is an async request
|
116
|
-
env[:parallel_manager] = true
|
117
|
-
perform_single_request(env).
|
118
|
-
callback { env[:response].finish(env) }.
|
119
|
-
errback {
|
120
|
-
# TODO: no way to communicate the error in async mode
|
121
|
-
raise NotImplementedError
|
122
|
-
}
|
123
|
-
end
|
124
|
-
end
|
125
|
-
rescue EventMachine::Connectify::CONNECTError => err
|
126
|
-
if err.message.include?("Proxy Authentication Required")
|
127
|
-
raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
|
128
|
-
else
|
129
|
-
raise Error::ConnectionFailed, err
|
130
|
-
end
|
131
|
-
rescue => err
|
132
|
-
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
|
133
|
-
raise Faraday::SSLError, err
|
134
|
-
else
|
135
|
-
raise
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
# TODO: reuse the connection to support pipelining
|
140
|
-
def perform_single_request(env)
|
141
|
-
req = create_request(env)
|
142
|
-
req.setup_request(env[:method], request_config(env)).callback { |client|
|
143
|
-
status = client.response_header.status
|
144
|
-
reason = client.response_header.http_reason
|
145
|
-
save_response(env, status, client.response, nil, reason) do |resp_headers|
|
146
|
-
client.response_header.each do |name, value|
|
147
|
-
resp_headers[name.to_sym] = value
|
148
|
-
end
|
149
|
-
end
|
150
|
-
}
|
151
|
-
end
|
152
|
-
|
153
|
-
def create_request(env)
|
154
|
-
EventMachine::HttpRequest.new(env[:url], connection_config(env).merge(@connection_options))
|
155
|
-
end
|
156
|
-
|
157
|
-
def error_message(client)
|
158
|
-
client.error or "request failed"
|
159
|
-
end
|
160
|
-
|
161
|
-
def raise_error(msg)
|
162
|
-
errklass = Faraday::Error::ClientError
|
163
|
-
if msg == Errno::ETIMEDOUT
|
164
|
-
errklass = Faraday::Error::TimeoutError
|
165
|
-
msg = "request timed out"
|
166
|
-
elsif msg == Errno::ECONNREFUSED
|
167
|
-
errklass = Faraday::Error::ConnectionFailed
|
168
|
-
msg = "connection refused"
|
169
|
-
elsif msg == "connection closed by server"
|
170
|
-
errklass = Faraday::Error::ConnectionFailed
|
171
|
-
end
|
172
|
-
raise errklass, msg
|
173
|
-
end
|
174
|
-
|
175
|
-
def parallel?(env)
|
176
|
-
!!env[:parallel_manager]
|
177
|
-
end
|
178
|
-
|
179
|
-
# The parallel manager is designed to start an EventMachine loop
|
180
|
-
# and block until all registered requests have been completed.
|
181
|
-
class Manager
|
182
|
-
def initialize
|
183
|
-
reset
|
184
|
-
end
|
185
|
-
|
186
|
-
def reset
|
187
|
-
@registered_procs = []
|
188
|
-
@num_registered = 0
|
189
|
-
@num_succeeded = 0
|
190
|
-
@errors = []
|
191
|
-
@running = false
|
192
|
-
end
|
193
|
-
|
194
|
-
def running?() @running end
|
195
|
-
|
196
|
-
def add
|
197
|
-
if running?
|
198
|
-
perform_request { yield }
|
199
|
-
else
|
200
|
-
@registered_procs << Proc.new
|
201
|
-
end
|
202
|
-
@num_registered += 1
|
203
|
-
end
|
204
|
-
|
205
|
-
def run
|
206
|
-
if @num_registered > 0
|
207
|
-
@running = true
|
208
|
-
EventMachine.run do
|
209
|
-
@registered_procs.each do |proc|
|
210
|
-
perform_request(&proc)
|
211
|
-
end
|
212
|
-
end
|
213
|
-
if @errors.size > 0
|
214
|
-
raise Faraday::Error::ClientError, @errors.first || "connection failed"
|
215
|
-
end
|
216
|
-
end
|
217
|
-
ensure
|
218
|
-
reset
|
219
|
-
end
|
220
|
-
|
221
|
-
def perform_request
|
222
|
-
client = yield
|
223
|
-
client.callback { @num_succeeded += 1; check_finished }
|
224
|
-
client.errback { @errors << client.error; check_finished }
|
225
|
-
end
|
226
|
-
|
227
|
-
def check_finished
|
228
|
-
if @num_succeeded + @errors.size == @num_registered
|
229
|
-
EventMachine.stop
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
233
|
-
end
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
begin
|
238
|
-
require 'openssl'
|
239
|
-
rescue LoadError
|
240
|
-
warn "Warning: no such file to load -- openssl. Make sure it is installed if you want HTTPS support"
|
241
|
-
else
|
242
|
-
require 'faraday/adapter/em_http_ssl_patch'
|
243
|
-
end if Faraday::Adapter::EMHttp.loaded?
|
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'openssl'
|
2
|
-
require 'em-http'
|
3
|
-
|
4
|
-
module EmHttpSslPatch
|
5
|
-
def ssl_verify_peer(cert_string)
|
6
|
-
cert = nil
|
7
|
-
begin
|
8
|
-
cert = OpenSSL::X509::Certificate.new(cert_string)
|
9
|
-
rescue OpenSSL::X509::CertificateError
|
10
|
-
return false
|
11
|
-
end
|
12
|
-
|
13
|
-
@last_seen_cert = cert
|
14
|
-
|
15
|
-
if certificate_store.verify(@last_seen_cert)
|
16
|
-
begin
|
17
|
-
certificate_store.add_cert(@last_seen_cert)
|
18
|
-
rescue OpenSSL::X509::StoreError => e
|
19
|
-
raise e unless e.message == 'cert already in hash table'
|
20
|
-
end
|
21
|
-
true
|
22
|
-
else
|
23
|
-
raise OpenSSL::SSL::SSLError.new(%(unable to verify the server certificate for "#{host}"))
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def ssl_handshake_completed
|
28
|
-
return true unless verify_peer?
|
29
|
-
|
30
|
-
unless OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
|
31
|
-
raise OpenSSL::SSL::SSLError.new(%(host "#{host}" does not match the server certificate))
|
32
|
-
else
|
33
|
-
true
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def verify_peer?
|
38
|
-
parent.connopts.tls[:verify_peer]
|
39
|
-
end
|
40
|
-
|
41
|
-
def host
|
42
|
-
parent.uri.host
|
43
|
-
end
|
44
|
-
|
45
|
-
def certificate_store
|
46
|
-
@certificate_store ||= begin
|
47
|
-
store = OpenSSL::X509::Store.new
|
48
|
-
store.set_default_paths
|
49
|
-
ca_file = parent.connopts.tls[:cert_chain_file]
|
50
|
-
store.add_file(ca_file) if ca_file
|
51
|
-
store
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
EventMachine::HttpStubConnection.send(:include, EmHttpSslPatch)
|
@@ -1,66 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Adapter
|
3
|
-
class EMSynchrony < Faraday::Adapter
|
4
|
-
class ParallelManager
|
5
|
-
|
6
|
-
# Add requests to queue. The `request` argument should be a
|
7
|
-
# `EM::HttpRequest` object.
|
8
|
-
def add(request, method, *args, &block)
|
9
|
-
queue << {
|
10
|
-
:request => request,
|
11
|
-
:method => method,
|
12
|
-
:args => args,
|
13
|
-
:block => block
|
14
|
-
}
|
15
|
-
end
|
16
|
-
|
17
|
-
# Run all requests on queue with `EM::Synchrony::Multi`, wrapping
|
18
|
-
# it in a reactor and fiber if needed.
|
19
|
-
def run
|
20
|
-
result = nil
|
21
|
-
if !EM.reactor_running?
|
22
|
-
EM.run {
|
23
|
-
Fiber.new do
|
24
|
-
result = perform
|
25
|
-
EM.stop
|
26
|
-
end.resume
|
27
|
-
}
|
28
|
-
else
|
29
|
-
result = perform
|
30
|
-
end
|
31
|
-
result
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
# The request queue.
|
38
|
-
def queue
|
39
|
-
@queue ||= []
|
40
|
-
end
|
41
|
-
|
42
|
-
# Main `EM::Synchrony::Multi` performer.
|
43
|
-
def perform
|
44
|
-
multi = ::EM::Synchrony::Multi.new
|
45
|
-
|
46
|
-
queue.each do |item|
|
47
|
-
method = "a#{item[:method]}".to_sym
|
48
|
-
|
49
|
-
req = item[:request].send(method, *item[:args])
|
50
|
-
req.callback(&item[:block])
|
51
|
-
|
52
|
-
req_name = "req_#{multi.requests.size}".to_sym
|
53
|
-
multi.add(req_name, req)
|
54
|
-
end
|
55
|
-
|
56
|
-
# Clear the queue, so parallel manager objects can be reused.
|
57
|
-
@queue = []
|
58
|
-
|
59
|
-
# Block fiber until all requests have returned.
|
60
|
-
multi.perform
|
61
|
-
end
|
62
|
-
|
63
|
-
end # ParallelManager
|
64
|
-
end # EMSynchrony
|
65
|
-
end # Adapter
|
66
|
-
end # Faraday
|
@@ -1,106 +0,0 @@
|
|
1
|
-
require 'uri'
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
class Adapter
|
5
|
-
class EMSynchrony < Faraday::Adapter
|
6
|
-
include EMHttp::Options
|
7
|
-
|
8
|
-
dependency do
|
9
|
-
require 'em-synchrony/em-http'
|
10
|
-
require 'em-synchrony/em-multi'
|
11
|
-
require 'fiber'
|
12
|
-
end
|
13
|
-
|
14
|
-
self.supports_parallel = true
|
15
|
-
|
16
|
-
def self.setup_parallel_manager(options = {})
|
17
|
-
ParallelManager.new
|
18
|
-
end
|
19
|
-
|
20
|
-
def call(env)
|
21
|
-
super
|
22
|
-
request = create_request(env)
|
23
|
-
|
24
|
-
http_method = env[:method].to_s.downcase.to_sym
|
25
|
-
|
26
|
-
# Queue requests for parallel execution.
|
27
|
-
if env[:parallel_manager]
|
28
|
-
env[:parallel_manager].add(request, http_method, request_config(env)) do |resp|
|
29
|
-
save_response(env, resp.response_header.status, resp.response) do |resp_headers|
|
30
|
-
resp.response_header.each do |name, value|
|
31
|
-
resp_headers[name.to_sym] = value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
# Finalize the response object with values from `env`.
|
36
|
-
env[:response].finish(env)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Execute single request.
|
40
|
-
else
|
41
|
-
client = nil
|
42
|
-
block = lambda { request.send(http_method, request_config(env)) }
|
43
|
-
|
44
|
-
if !EM.reactor_running?
|
45
|
-
EM.run do
|
46
|
-
Fiber.new {
|
47
|
-
client = block.call
|
48
|
-
EM.stop
|
49
|
-
}.resume
|
50
|
-
end
|
51
|
-
else
|
52
|
-
client = block.call
|
53
|
-
end
|
54
|
-
|
55
|
-
raise client.error if client.error
|
56
|
-
|
57
|
-
status = client.response_header.status
|
58
|
-
reason = client.response_header.http_reason
|
59
|
-
save_response(env, status, client.response, nil, reason) do |resp_headers|
|
60
|
-
client.response_header.each do |name, value|
|
61
|
-
resp_headers[name.to_sym] = value
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
@app.call env
|
67
|
-
rescue Errno::ECONNREFUSED
|
68
|
-
raise Error::ConnectionFailed, $!
|
69
|
-
rescue EventMachine::Connectify::CONNECTError => err
|
70
|
-
if err.message.include?("Proxy Authentication Required")
|
71
|
-
raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
|
72
|
-
else
|
73
|
-
raise Error::ConnectionFailed, err
|
74
|
-
end
|
75
|
-
rescue Errno::ETIMEDOUT => err
|
76
|
-
raise Error::TimeoutError, err
|
77
|
-
rescue RuntimeError => err
|
78
|
-
if err.message == "connection closed by server"
|
79
|
-
raise Error::ConnectionFailed, err
|
80
|
-
else
|
81
|
-
raise
|
82
|
-
end
|
83
|
-
rescue => err
|
84
|
-
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
|
85
|
-
raise Faraday::SSLError, err
|
86
|
-
else
|
87
|
-
raise
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def create_request(env)
|
92
|
-
EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env).merge(@connection_options))
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
require 'faraday/adapter/em_synchrony/parallel_manager'
|
99
|
-
|
100
|
-
begin
|
101
|
-
require 'openssl'
|
102
|
-
rescue LoadError
|
103
|
-
warn "Warning: no such file to load -- openssl. Make sure it is installed if you want HTTPS support"
|
104
|
-
else
|
105
|
-
require 'faraday/adapter/em_http_ssl_patch'
|
106
|
-
end if Faraday::Adapter::EMSynchrony.loaded?
|
@@ -1,80 +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
|
-
|
19
|
-
# https://github.com/geemus/excon/issues/106
|
20
|
-
# https://github.com/jruby/jruby-ossl/issues/19
|
21
|
-
opts[:nonblock] = false
|
22
|
-
end
|
23
|
-
|
24
|
-
if ( req = env[:request] )
|
25
|
-
if req[:timeout]
|
26
|
-
opts[:read_timeout] = req[:timeout]
|
27
|
-
opts[:connect_timeout] = req[:timeout]
|
28
|
-
opts[:write_timeout] = req[:timeout]
|
29
|
-
end
|
30
|
-
|
31
|
-
if req[:open_timeout]
|
32
|
-
opts[:connect_timeout] = req[:open_timeout]
|
33
|
-
opts[:write_timeout] = req[:open_timeout]
|
34
|
-
end
|
35
|
-
|
36
|
-
if req[:proxy]
|
37
|
-
opts[:proxy] = {
|
38
|
-
:host => req[:proxy][:uri].host,
|
39
|
-
:hostname => req[:proxy][:uri].hostname,
|
40
|
-
:port => req[:proxy][:uri].port,
|
41
|
-
:scheme => req[:proxy][:uri].scheme,
|
42
|
-
:user => req[:proxy][:user],
|
43
|
-
:password => req[:proxy][:password]
|
44
|
-
}
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
conn = create_connection(env, opts)
|
49
|
-
|
50
|
-
resp = conn.request \
|
51
|
-
:method => env[:method].to_s.upcase,
|
52
|
-
:headers => env[:request_headers],
|
53
|
-
:body => read_body(env)
|
54
|
-
|
55
|
-
save_response(env, resp.status.to_i, resp.body, resp.headers, resp.reason_phrase)
|
56
|
-
|
57
|
-
@app.call env
|
58
|
-
rescue ::Excon::Errors::SocketError => err
|
59
|
-
if err.message =~ /\btimeout\b/
|
60
|
-
raise Error::TimeoutError, err
|
61
|
-
elsif err.message =~ /\bcertificate\b/
|
62
|
-
raise Faraday::SSLError, err
|
63
|
-
else
|
64
|
-
raise Error::ConnectionFailed, err
|
65
|
-
end
|
66
|
-
rescue ::Excon::Errors::Timeout => err
|
67
|
-
raise Error::TimeoutError, err
|
68
|
-
end
|
69
|
-
|
70
|
-
def create_connection(env, opts)
|
71
|
-
::Excon.new(env[:url].to_s, opts.merge(@connection_options))
|
72
|
-
end
|
73
|
-
|
74
|
-
# TODO: support streaming requests
|
75
|
-
def read_body(env)
|
76
|
-
env[:body].respond_to?(:read) ? env[:body].read : env[:body]
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
@@ -1,135 +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::ECONNABORTED,
|
15
|
-
Errno::ECONNREFUSED,
|
16
|
-
Errno::ECONNRESET,
|
17
|
-
Errno::EHOSTUNREACH,
|
18
|
-
Errno::EINVAL,
|
19
|
-
Errno::ENETUNREACH,
|
20
|
-
Errno::EPIPE,
|
21
|
-
Net::HTTPBadResponse,
|
22
|
-
Net::HTTPHeaderSyntaxError,
|
23
|
-
Net::ProtocolError,
|
24
|
-
SocketError,
|
25
|
-
Zlib::GzipFile::Error,
|
26
|
-
]
|
27
|
-
|
28
|
-
NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)
|
29
|
-
NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout)
|
30
|
-
|
31
|
-
def call(env)
|
32
|
-
super
|
33
|
-
with_net_http_connection(env) do |http|
|
34
|
-
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
|
35
|
-
configure_request(http, env[:request])
|
36
|
-
|
37
|
-
begin
|
38
|
-
http_response = perform_request(http, env)
|
39
|
-
rescue *NET_HTTP_EXCEPTIONS => err
|
40
|
-
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
|
41
|
-
raise Faraday::SSLError, err
|
42
|
-
else
|
43
|
-
raise Error::ConnectionFailed, err
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
|
48
|
-
http_response.each_header do |key, value|
|
49
|
-
response_headers[key] = value
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
@app.call env
|
55
|
-
rescue Timeout::Error, Errno::ETIMEDOUT => err
|
56
|
-
raise Faraday::Error::TimeoutError, err
|
57
|
-
end
|
58
|
-
|
59
|
-
def create_request(env)
|
60
|
-
request = Net::HTTPGenericRequest.new \
|
61
|
-
env[:method].to_s.upcase, # request method
|
62
|
-
!!env[:body], # is there request body
|
63
|
-
:head != env[:method], # is there response body
|
64
|
-
env[:url].request_uri, # request uri path
|
65
|
-
env[:request_headers] # request headers
|
66
|
-
|
67
|
-
if env[:body].respond_to?(:read)
|
68
|
-
request.body_stream = env[:body]
|
69
|
-
else
|
70
|
-
request.body = env[:body]
|
71
|
-
end
|
72
|
-
request
|
73
|
-
end
|
74
|
-
|
75
|
-
def perform_request(http, env)
|
76
|
-
if :get == env[:method] and !env[:body]
|
77
|
-
# prefer `get` to `request` because the former handles gzip (ruby 1.9)
|
78
|
-
http.get env[:url].request_uri, env[:request_headers]
|
79
|
-
else
|
80
|
-
http.request create_request(env)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def with_net_http_connection(env)
|
85
|
-
yield net_http_connection(env)
|
86
|
-
end
|
87
|
-
|
88
|
-
def net_http_connection(env)
|
89
|
-
if proxy = env[:request][:proxy]
|
90
|
-
Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
|
91
|
-
else
|
92
|
-
Net::HTTP
|
93
|
-
end.new(env[:url].host, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80))
|
94
|
-
end
|
95
|
-
|
96
|
-
def configure_ssl(http, ssl)
|
97
|
-
http.use_ssl = true
|
98
|
-
http.verify_mode = ssl_verify_mode(ssl)
|
99
|
-
http.cert_store = ssl_cert_store(ssl)
|
100
|
-
|
101
|
-
http.cert = ssl[:client_cert] if ssl[:client_cert]
|
102
|
-
http.key = ssl[:client_key] if ssl[:client_key]
|
103
|
-
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
|
104
|
-
http.ca_path = ssl[:ca_path] if ssl[:ca_path]
|
105
|
-
http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
106
|
-
http.ssl_version = ssl[:version] if ssl[:version]
|
107
|
-
end
|
108
|
-
|
109
|
-
def configure_request(http, req)
|
110
|
-
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
|
111
|
-
http.open_timeout = req[:open_timeout] if req[:open_timeout]
|
112
|
-
|
113
|
-
@config_block.call(http) if @config_block
|
114
|
-
end
|
115
|
-
|
116
|
-
def ssl_cert_store(ssl)
|
117
|
-
return ssl[:cert_store] if ssl[:cert_store]
|
118
|
-
# Use the default cert store by default, i.e. system ca certs
|
119
|
-
cert_store = OpenSSL::X509::Store.new
|
120
|
-
cert_store.set_default_paths
|
121
|
-
cert_store
|
122
|
-
end
|
123
|
-
|
124
|
-
def ssl_verify_mode(ssl)
|
125
|
-
ssl[:verify_mode] || begin
|
126
|
-
if ssl.fetch(:verify, true)
|
127
|
-
OpenSSL::SSL::VERIFY_PEER
|
128
|
-
else
|
129
|
-
OpenSSL::SSL::VERIFY_NONE
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|