faraday 0.15.4 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +1 -1
  3. data/README.md +18 -344
  4. data/lib/faraday.rb +93 -175
  5. data/lib/faraday/adapter.rb +36 -22
  6. data/lib/faraday/adapter/em_http.rb +142 -99
  7. data/lib/faraday/adapter/em_http_ssl_patch.rb +23 -17
  8. data/lib/faraday/adapter/em_synchrony.rb +104 -60
  9. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  10. data/lib/faraday/adapter/excon.rb +100 -55
  11. data/lib/faraday/adapter/httpclient.rb +61 -39
  12. data/lib/faraday/adapter/net_http.rb +104 -51
  13. data/lib/faraday/adapter/net_http_persistent.rb +48 -27
  14. data/lib/faraday/adapter/patron.rb +54 -35
  15. data/lib/faraday/adapter/rack.rb +28 -12
  16. data/lib/faraday/adapter/test.rb +86 -53
  17. data/lib/faraday/adapter/typhoeus.rb +4 -1
  18. data/lib/faraday/adapter_registry.rb +28 -0
  19. data/lib/faraday/autoload.rb +47 -36
  20. data/lib/faraday/connection.rb +321 -179
  21. data/lib/faraday/dependency_loader.rb +37 -0
  22. data/lib/faraday/encoders/flat_params_encoder.rb +94 -0
  23. data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
  24. data/lib/faraday/error.rb +67 -33
  25. data/lib/faraday/file_part.rb +128 -0
  26. data/lib/faraday/logging/formatter.rb +92 -0
  27. data/lib/faraday/middleware.rb +4 -28
  28. data/lib/faraday/middleware_registry.rb +129 -0
  29. data/lib/faraday/options.rb +35 -186
  30. data/lib/faraday/options/connection_options.rb +22 -0
  31. data/lib/faraday/options/env.rb +181 -0
  32. data/lib/faraday/options/proxy_options.rb +28 -0
  33. data/lib/faraday/options/request_options.rb +21 -0
  34. data/lib/faraday/options/ssl_options.rb +59 -0
  35. data/lib/faraday/param_part.rb +53 -0
  36. data/lib/faraday/parameters.rb +4 -197
  37. data/lib/faraday/rack_builder.rb +67 -56
  38. data/lib/faraday/request.rb +68 -36
  39. data/lib/faraday/request/authorization.rb +42 -30
  40. data/lib/faraday/request/basic_authentication.rb +14 -7
  41. data/lib/faraday/request/instrumentation.rb +45 -27
  42. data/lib/faraday/request/multipart.rb +79 -48
  43. data/lib/faraday/request/retry.rb +198 -169
  44. data/lib/faraday/request/token_authentication.rb +15 -10
  45. data/lib/faraday/request/url_encoded.rb +41 -23
  46. data/lib/faraday/response.rb +23 -16
  47. data/lib/faraday/response/logger.rb +22 -69
  48. data/lib/faraday/response/raise_error.rb +36 -14
  49. data/lib/faraday/utils.rb +28 -245
  50. data/lib/faraday/utils/headers.rb +139 -0
  51. data/lib/faraday/utils/params_hash.rb +61 -0
  52. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  53. metadata +21 -5
  54. data/lib/faraday/upload_io.rb +0 -67
@@ -1,43 +1,49 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'openssl'
2
4
  require 'em-http'
3
5
 
6
+ # EventMachine patch to make SSL work.
4
7
  module EmHttpSslPatch
5
8
  def ssl_verify_peer(cert_string)
6
- cert = nil
7
9
  begin
8
- cert = OpenSSL::X509::Certificate.new(cert_string)
10
+ @last_seen_cert = OpenSSL::X509::Certificate.new(cert_string)
9
11
  rescue OpenSSL::X509::CertificateError
10
12
  return false
11
13
  end
12
14
 
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}"))
15
+ unless certificate_store.verify(@last_seen_cert)
16
+ raise OpenSSL::SSL::SSLError,
17
+ %(unable to verify the server certificate for "#{host}")
18
+ end
19
+
20
+ begin
21
+ certificate_store.add_cert(@last_seen_cert)
22
+ rescue OpenSSL::X509::StoreError => e
23
+ raise e unless e.message == 'cert already in hash table'
24
24
  end
25
+ true
25
26
  end
26
27
 
27
28
  def ssl_handshake_completed
28
29
  return true unless verify_peer?
29
30
 
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
31
+ unless verified_cert_identity?
32
+ raise OpenSSL::SSL::SSLError,
33
+ %(host "#{host}" does not match the server certificate)
34
34
  end
35
+
36
+ true
35
37
  end
36
38
 
37
39
  def verify_peer?
38
40
  parent.connopts.tls[:verify_peer]
39
41
  end
40
42
 
43
+ def verified_cert_identity?
44
+ OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
45
+ end
46
+
41
47
  def host
42
48
  parent.uri.host
43
49
  end
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'uri'
2
4
 
3
5
  module Faraday
4
6
  class Adapter
7
+ # EventMachine Synchrony adapter.
5
8
  class EMSynchrony < Faraday::Adapter
6
9
  include EMHttp::Options
7
10
 
@@ -13,7 +16,8 @@ module Faraday
13
16
 
14
17
  self.supports_parallel = true
15
18
 
16
- def self.setup_parallel_manager(options = {})
19
+ # @return [ParallelManager]
20
+ def self.setup_parallel_manager(_options = nil)
17
21
  ParallelManager.new
18
22
  end
19
23
 
@@ -23,73 +27,110 @@ module Faraday
23
27
 
24
28
  http_method = env[:method].to_s.downcase.to_sym
25
29
 
26
- # Queue requests for parallel execution.
27
30
  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
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
34
37
 
35
- # Finalize the response object with values from `env`.
36
- env[:response].finish(env)
37
- end
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
38
46
 
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
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
54
72
 
55
- raise client.error if client.error
73
+ private
56
74
 
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|
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|
61
87
  resp_headers[name.to_sym] = value
62
88
  end
63
89
  end
90
+
91
+ # Finalize the response object with values from `env`.
92
+ env[:response].finish(env)
64
93
  end
94
+ end
65
95
 
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
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
+ )
74
109
  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
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
82
116
  end
83
- rescue => err
84
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
85
- raise Faraday::SSLError, err
117
+ end
118
+
119
+ def call_block(block)
120
+ client = nil
121
+
122
+ if EM.reactor_running?
123
+ client = block.call
86
124
  else
87
- raise
125
+ EM.run do
126
+ Fiber.new do
127
+ client = block.call
128
+ EM.stop
129
+ end.resume
130
+ end
88
131
  end
89
- end
90
132
 
91
- def create_request(env)
92
- EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env).merge(@connection_options))
133
+ client
93
134
  end
94
135
  end
95
136
  end
@@ -97,10 +138,13 @@ end
97
138
 
98
139
  require 'faraday/adapter/em_synchrony/parallel_manager'
99
140
 
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?
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,16 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
4
  class Adapter
3
5
  class EMSynchrony < Faraday::Adapter
6
+ # A parallel manager for EMSynchrony.
4
7
  class ParallelManager
5
-
6
- # Add requests to queue. The `request` argument should be a
7
- # `EM::HttpRequest` object.
8
+ # Add requests to queue.
9
+ #
10
+ # @param request [EM::HttpRequest]
11
+ # @param method [Symbol, String] HTTP method
12
+ # @param args [Array] the rest of the positional arguments
8
13
  def add(request, method, *args, &block)
9
14
  queue << {
10
- :request => request,
11
- :method => method,
12
- :args => args,
13
- :block => block
15
+ request: request,
16
+ method: method,
17
+ args: args,
18
+ block: block
14
19
  }
15
20
  end
16
21
 
@@ -19,19 +24,18 @@ module Faraday
19
24
  def run
20
25
  result = nil
21
26
  if !EM.reactor_running?
22
- EM.run {
27
+ EM.run do
23
28
  Fiber.new do
24
29
  result = perform
25
30
  EM.stop
26
31
  end.resume
27
- }
32
+ end
28
33
  else
29
34
  result = perform
30
35
  end
31
36
  result
32
37
  end
33
38
 
34
-
35
39
  private
36
40
 
37
41
  # The request queue.
@@ -59,8 +63,7 @@ module Faraday
59
63
  # Block fiber until all requests have returned.
60
64
  multi.perform
61
65
  end
62
-
63
- end # ParallelManager
64
- end # EMSynchrony
65
- end # Adapter
66
- end # Faraday
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,74 +1,47 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
4
  class Adapter
5
+ # Excon adapter.
3
6
  class Excon < Faraday::Adapter
4
7
  dependency 'excon'
5
8
 
6
9
  def call(env)
7
10
  super
8
11
 
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
12
+ opts = opts_from_env(env)
13
+ conn = create_connection(env, opts)
33
14
 
34
- if req[:open_timeout]
35
- opts[:connect_timeout] = req[:open_timeout]
36
- end
15
+ req_opts = {
16
+ method: env[:method].to_s.upcase,
17
+ headers: env[:request_headers],
18
+ body: read_body(env)
19
+ }
37
20
 
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
- }
21
+ req = env[:request]
22
+ if req&.stream_response?
23
+ total = 0
24
+ req_opts[:response_block] = lambda do |chunk, _remain, _total|
25
+ req.on_data.call(chunk, total += chunk.size)
47
26
  end
48
27
  end
49
28
 
50
- conn = create_connection(env, opts)
29
+ resp = conn.request(req_opts)
30
+ save_response(env, resp.status.to_i, resp.body, resp.headers,
31
+ resp.reason_phrase)
51
32
 
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 Error::TimeoutError, err
63
- elsif err.message =~ /\bcertificate\b/
64
- raise Faraday::SSLError, err
65
- else
66
- raise Error::ConnectionFailed, err
67
- end
68
- rescue ::Excon::Errors::Timeout => err
69
- raise Error::TimeoutError, err
33
+ @app.call(env)
34
+ rescue ::Excon::Errors::SocketError => e
35
+ raise Faraday::TimeoutError, e if e.message =~ /\btimeout\b/
36
+
37
+ raise Faraday::SSLError, e if e.message =~ /\bcertificate\b/
38
+
39
+ raise Faraday::ConnectionFailed, e
40
+ rescue ::Excon::Errors::Timeout => e
41
+ raise Faraday::TimeoutError, e
70
42
  end
71
43
 
44
+ # @return [Excon]
72
45
  def create_connection(env, opts)
73
46
  ::Excon.new(env[:url].to_s, opts.merge(@connection_options))
74
47
  end
@@ -77,6 +50,78 @@ module Faraday
77
50
  def read_body(env)
78
51
  env[:body].respond_to?(:read) ? env[:body].read : env[:body]
79
52
  end
53
+
54
+ private
55
+
56
+ def opts_from_env(env)
57
+ opts = {}
58
+ amend_opts_with_ssl!(opts, env[:ssl]) if needs_ssl_settings?(env)
59
+
60
+ if (req = env[:request])
61
+ amend_opts_with_timeouts!(opts, req)
62
+ amend_opts_with_proxy_settings!(opts, req)
63
+ end
64
+
65
+ opts
66
+ end
67
+
68
+ def needs_ssl_settings?(env)
69
+ env[:url].scheme == 'https' && env[:ssl]
70
+ end
71
+
72
+ OPTS_KEYS = [
73
+ %i[client_cert client_cert],
74
+ %i[client_key client_key],
75
+ %i[certificate certificate],
76
+ %i[private_key private_key],
77
+ %i[ssl_ca_path ca_path],
78
+ %i[ssl_ca_file ca_file],
79
+ %i[ssl_version version],
80
+ %i[ssl_min_version min_version],
81
+ %i[ssl_max_version max_version]
82
+ ].freeze
83
+
84
+ def amend_opts_with_ssl!(opts, ssl)
85
+ opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
86
+ # https://github.com/geemus/excon/issues/106
87
+ # https://github.com/jruby/jruby-ossl/issues/19
88
+ opts[:nonblock] = false
89
+
90
+ OPTS_KEYS.each do |(key_in_opts, key_in_ssl)|
91
+ next unless ssl[key_in_ssl]
92
+
93
+ opts[key_in_opts] = ssl[key_in_ssl]
94
+ end
95
+ end
96
+
97
+ def amend_opts_with_timeouts!(opts, req)
98
+ timeout = req[:timeout]
99
+ return unless timeout
100
+
101
+ opts[:read_timeout] = timeout
102
+ opts[:connect_timeout] = timeout
103
+ opts[:write_timeout] = timeout
104
+
105
+ open_timeout = req[:open_timeout]
106
+ return unless open_timeout
107
+
108
+ opts[:connect_timeout] = open_timeout
109
+ end
110
+
111
+ def amend_opts_with_proxy_settings!(opts, req)
112
+ opts[:proxy] = proxy_settings_for_opts(req[:proxy]) if req[:proxy]
113
+ end
114
+
115
+ def proxy_settings_for_opts(proxy)
116
+ {
117
+ host: proxy[:uri].host,
118
+ hostname: proxy[:uri].hostname,
119
+ port: proxy[:uri].port,
120
+ scheme: proxy[:uri].scheme,
121
+ user: proxy[:user],
122
+ password: proxy[:password]
123
+ }
124
+ end
80
125
  end
81
126
  end
82
127
  end