faraday 0.14.0 → 0.17.6

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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +232 -0
  3. data/README.md +21 -7
  4. data/Rakefile +13 -0
  5. data/lib/faraday/adapter/em_http.rb +9 -9
  6. data/lib/faraday/adapter/em_synchrony.rb +5 -5
  7. data/lib/faraday/adapter/excon.rb +6 -3
  8. data/lib/faraday/adapter/httpclient.rb +4 -4
  9. data/lib/faraday/adapter/net_http.rb +25 -7
  10. data/lib/faraday/adapter/net_http_persistent.rb +33 -19
  11. data/lib/faraday/adapter/patron.rb +7 -12
  12. data/lib/faraday/adapter/rack.rb +1 -1
  13. data/lib/faraday/adapter.rb +2 -0
  14. data/lib/faraday/deprecate.rb +109 -0
  15. data/lib/faraday/error.rb +129 -34
  16. data/lib/faraday/options.rb +6 -5
  17. data/lib/faraday/parameters.rb +2 -1
  18. data/lib/faraday/rack_builder.rb +2 -2
  19. data/lib/faraday/request/retry.rb +65 -16
  20. data/lib/faraday/request.rb +22 -0
  21. data/lib/faraday/response/logger.rb +3 -3
  22. data/lib/faraday/response/raise_error.rb +7 -3
  23. data/lib/faraday/response.rb +3 -3
  24. data/lib/faraday/upload_io.rb +16 -6
  25. data/lib/faraday.rb +2 -3
  26. data/spec/faraday/deprecate_spec.rb +147 -0
  27. data/spec/faraday/error_spec.rb +102 -0
  28. data/spec/faraday/response/raise_error_spec.rb +106 -0
  29. data/spec/spec_helper.rb +105 -0
  30. data/test/adapters/default_test.rb +14 -0
  31. data/test/adapters/em_http_test.rb +30 -0
  32. data/test/adapters/em_synchrony_test.rb +32 -0
  33. data/test/adapters/excon_test.rb +30 -0
  34. data/test/adapters/httpclient_test.rb +34 -0
  35. data/test/adapters/integration.rb +263 -0
  36. data/test/adapters/logger_test.rb +136 -0
  37. data/test/adapters/net_http_persistent_test.rb +114 -0
  38. data/test/adapters/net_http_test.rb +79 -0
  39. data/test/adapters/patron_test.rb +40 -0
  40. data/test/adapters/rack_test.rb +38 -0
  41. data/test/adapters/test_middleware_test.rb +157 -0
  42. data/test/adapters/typhoeus_test.rb +38 -0
  43. data/test/authentication_middleware_test.rb +65 -0
  44. data/test/composite_read_io_test.rb +109 -0
  45. data/test/connection_test.rb +738 -0
  46. data/test/env_test.rb +268 -0
  47. data/test/helper.rb +75 -0
  48. data/test/live_server.rb +67 -0
  49. data/test/middleware/instrumentation_test.rb +88 -0
  50. data/test/middleware/retry_test.rb +282 -0
  51. data/test/middleware_stack_test.rb +260 -0
  52. data/test/multibyte.txt +1 -0
  53. data/test/options_test.rb +333 -0
  54. data/test/parameters_test.rb +157 -0
  55. data/test/request_middleware_test.rb +126 -0
  56. data/test/response_middleware_test.rb +72 -0
  57. data/test/strawberry.rb +2 -0
  58. data/test/utils_test.rb +98 -0
  59. metadata +48 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff30e260051828899307875d73f5ccb1fdcdb4bb40fba3df707e2d6966675cb1
4
- data.tar.gz: 4bc5b5df30090ae639a937d180f0cc2d7b66a163dca438be8049eef0a5b2a483
3
+ metadata.gz: 56d15adebd24ca50e38ac12d74f518927ee757abe32ce2693aac8f6b3b2d5db6
4
+ data.tar.gz: 72247b6844e16989a3c186a95e226b8254da0dff82854922242add3535707a9f
5
5
  SHA512:
6
- metadata.gz: 6edafc07bcf5c647f82ab5e50aa8cd7c319e5198559bd7350394fd3fb918e96f1a450fa4eb175ee3b7db9e03880cc2534c3b52b3a213cdc852fe094953603e13
7
- data.tar.gz: 9d2951f42b5e8ae17505bc92493ee8ef2814412de9ade880adf20c081a02e6437da7174a064ca68f37483793e48efdf67aeb145e07ca3a90c71ac80014926070
6
+ metadata.gz: '08df648fee5c02b6fd54d70615ee97ccc978d5a16471cbfc4790150189de39799e60bd67d45236645c79785062dc14aef36a0c516e847c7a43615e7147381aad'
7
+ data.tar.gz: de528b3a2c4a552f68cfb95814729018106146c5bc0861abfcbd45d4202036c9e7347f40dfe1703a27a939e1960e2019bae9293039a1550031a41124c27dfd7c
data/CHANGELOG.md ADDED
@@ -0,0 +1,232 @@
1
+ # Faraday Changelog
2
+
3
+ ## v0.17.3
4
+
5
+ Fixes:
6
+
7
+ * Reverts changes in error classes hierarchy. #1092 (@iMacTia)
8
+ * Fix Ruby 1.9 syntax errors and improve Error class testing #1094 (@BanzaiMan,
9
+ @mrexox, @technoweenie)
10
+
11
+ Misc:
12
+
13
+ * Stops using `&Proc.new` for block forwarding. #1083 (@olleolleolle)
14
+ * Update CI to test against ruby 2.0-2.7 #1087, #1099 (@iMacTia, @olleolleolle,
15
+ @technoweenie)
16
+ * require FARADAY_DEPRECATE=warn to show Faraday v1.0 deprecation warnings
17
+ #1098 (@technoweenie)
18
+
19
+ ## v0.17.1
20
+
21
+ Final release before Faraday v1.0, with important fixes for Ruby 2.7.
22
+
23
+ Fixes:
24
+
25
+ * RaiseError response middleware raises exception if HTTP client returns a nil
26
+ status. #1042 (@jonnyom, @BobbyMcWho)
27
+
28
+ Misc:
29
+
30
+ * Fix Ruby 2.7 warnings (#1009)
31
+ * Add `Faraday::Deprecate` to warn about upcoming v1.0 changes. (#1054, #1059,
32
+ #1076, #1077)
33
+ * Add release notes up to current in CHANGELOG.md (#1066)
34
+ * Port minimal rspec suite from main branch to run backported tests. (#1058)
35
+
36
+ ## v0.17.0
37
+
38
+ This release is the same as v0.15.4. It was pushed to cover up releases
39
+ v0.16.0-v0.16.2.
40
+
41
+ ## v0.15.4
42
+
43
+ * Expose `pool_size` as a option for the NetHttpPersistent adapter (#834)
44
+
45
+ ## v0.15.3
46
+
47
+ * Make Faraday::Request serialisable with Marshal. (#803)
48
+ * Add DEFAULT_EXCEPTIONS constant to Request::Retry (#814)
49
+ * Add support for Ruby 2.6 Net::HTTP write_timeout (#824)
50
+
51
+ ## v0.15.2
52
+
53
+ * Prevents `Net::HTTP` adapters to retry request internally by setting `max_retries` to 0 if available (Ruby 2.5+). (#799)
54
+ * Fixes `NestedParamsEncoder` handling of empty array values (#801)
55
+
56
+ ## v0.15.1
57
+
58
+ * NetHttpPersistent adapter better reuse of SSL connections (#793)
59
+ * Refactor: inline cached_connection (#797)
60
+ * Logger middleware: use $stdout instead of STDOUT (#794)
61
+ * Fix: do not memoize/reuse Patron session (#796)
62
+
63
+ Also in this release:
64
+
65
+ * Allow setting min/max ssl version for Net::HTTP (#792)
66
+ * Allow setting min/max ssl version for Excon (#795)
67
+
68
+ ## v0.15.0
69
+
70
+ Features:
71
+
72
+ * Added retry block option to retry middleware. (#770)
73
+ * Retry middleware improvements (honour Retry-After header, retry statuses) (#773)
74
+ * Improve response logger middleware output (#784)
75
+
76
+ Fixes:
77
+
78
+ * Remove unused class error (#767)
79
+ * Fix minor typo in README (#760)
80
+ * Reuse persistent connections when using net-http-persistent (#778)
81
+ * Fix Retry middleware documentation (#781)
82
+ * Returns the http response when giving up on retrying by status (#783)
83
+
84
+ ## v0.14.0
85
+
86
+ Features:
87
+
88
+ * Allow overriding env proxy (#754)
89
+ * Remove legacy Typhoeus adapter (#715)
90
+ * External Typhoeus Adapter Compatibility (#748)
91
+ * Warn about missing adapter when making a request (#743)
92
+ * Faraday::Adapter::Test stubs now support entire urls (with host) (#741)
93
+
94
+ Fixes:
95
+
96
+ * If proxy is manually provided, this takes priority over `find_proxy` (#724)
97
+ * Fixes the behaviour for Excon's open_timeout (not setting write_timeout anymore) (#731)
98
+ * Handle all connection timeout messages in Patron (#687)
99
+
100
+ ## v0.13.1
101
+
102
+ * Fixes an incompatibility with Addressable::URI being used as uri_parser
103
+
104
+ ## v0.13.0
105
+
106
+ Features:
107
+
108
+ * Dynamically reloads the proxy when performing a request on an absolute domain (#701)
109
+ * Adapter support for Net::HTTP::Persistent v3.0.0 (#619)
110
+
111
+ Fixes:
112
+
113
+ * Prefer #hostname over #host. (#714)
114
+ * Fixes an edge-case issue with response headers parsing (missing HTTP header) (#719)
115
+
116
+ ## v0.12.2
117
+
118
+ * Parse headers from aggregated proxy requests/responses (#681)
119
+ * Guard against invalid middleware configuration with warning (#685)
120
+ * Do not use :insecure option by default in Patron (#691)
121
+ * Fixes an issue with HTTPClient not raising a `Faraday::ConnectionFailed` (#702)
122
+ * Fixes YAML serialization/deserialization for `Faraday::Utils::Headers` (#690)
123
+ * Fixes an issue with Options having a nil value (#694)
124
+ * Fixes an issue with Faraday.default_connection not using Faraday.default_connection_options (#698)
125
+ * Fixes an issue with Options.merge! and Faraday instrumentation middleware (#710)
126
+
127
+ ## v0.12.1
128
+
129
+ * Fix an issue with Patron tests failing on jruby
130
+ * Fix an issue with new `rewind_files` feature that was causing an exception when the body was not an Hash
131
+ * Expose wrapped_exception in all client errors
132
+ * Add Authentication Section to the ReadMe
133
+
134
+ ## v0.12.0.1
135
+
136
+ * Hotfix release to address an issue with TravisCI deploy on Rubygems
137
+
138
+ ## v0.12.0
139
+
140
+ Features:
141
+
142
+ * Proxy feature now relies on Ruby `URI::Generic#find_proxy` and can use `no_proxy` ENV variable (not compatible with ruby < 2.0)
143
+ * Adds support for `context` request option to pass arbitrary information to middlewares
144
+
145
+ Fixes:
146
+
147
+ * Fix an issue with options that was causing new options to override defaults ones unexpectedly
148
+ * Rewind `UploadIO`s on retry to fix a compatibility issue
149
+ * Make multipart boundary unique
150
+ * Improvements in `README.md`
151
+
152
+ ## v0.11.0
153
+
154
+ Features:
155
+
156
+ * Add `filter` method to Logger middleware
157
+ * Add support for Ruby2.4 and Minitest 6
158
+ * Introduce block syntax to customise the adapter
159
+
160
+ Fixes:
161
+
162
+ * Fix an issue that was allowing to override `default_connection_options` from a connection instance
163
+ * Fix a bug that was causing newline escape characters ("\n") to be used when building the Authorization header
164
+
165
+ ## v0.10.1
166
+
167
+ - Fix an issue with HTTPClient adapter that was causing the SSL to be reset on every request
168
+ - Rescue `IOError` instead of specific subclass
169
+ - `Faraday::Utils::Headers` can now be successfully serialised in YAML
170
+ - Handle `default_connection_options` set with hash
171
+
172
+ ## v0.10.0
173
+
174
+ Breaking changes:
175
+ - Drop support for Ruby 1.8
176
+
177
+ Features:
178
+ - Include wrapped exception/reponse in ClientErrors
179
+ - Add `response.reason_phrase`
180
+ - Provide option to selectively skip logging request/response headers
181
+ - Add regex support for pattern matching in `test` adapter
182
+
183
+ Fixes:
184
+ - Add `Faraday.respond_to?` to find methods managed by `method_missing`
185
+ - em-http: `request.host` instead of `connection.host` should be taken for SSL validations
186
+ - Allow `default_connection_options` to be merged when options are passed as url parameter
187
+ - Improve splitting key-value pairs in raw HTTP headers
188
+
189
+ ## v0.9.2
190
+
191
+ Adapters:
192
+ - Enable gzip compression for httpclient
193
+ - Fixes default certificate store for httpclient not having default paths.
194
+ - Make excon adapter compatible with 0.44 excon version
195
+ - Add compatibility with Patron 0.4.20
196
+ - Determine default port numbers in Net::HTTP adapters (Addressable compatibility)
197
+ - em-http: wrap "connection closed by server" as ConnectionFailed type
198
+ - Wrap Errno::ETIMEDOUT in Faraday::Error::TimeoutError
199
+
200
+ Utils:
201
+ - Add Rack-compatible support for parsing `a[][b]=c` nested queries
202
+ - Encode nil values in queries different than empty strings. Before: `a=`; now: `a`.
203
+ - Have `Faraday::Utils::Headers#replace` clear internal key cache
204
+ - Dup the internal key cache when a Headers hash is copied
205
+
206
+ Env and middleware:
207
+ - Ensure `env` stored on middleware response has reference to the response
208
+ - Ensure that Response properties are initialized during `on_complete` (VCR compatibility)
209
+ - Copy request options in Faraday::Connection#dup
210
+ - Env custom members should be copied by Env.from(env)
211
+ - Honour per-request `request.options.params_encoder`
212
+ - Fix `interval_randomness` data type for Retry middleware
213
+ - Add maximum interval option for Retry middleware
214
+
215
+ ## v0.9.1
216
+
217
+ * Refactor Net:HTTP adapter so that with_net_http_connection can be overridden to allow pooled connections. (@Ben-M)
218
+ * Add configurable methods that bypass `retry_if` in the Retry request middleware. (@mike-bourgeous)
219
+
220
+ ## v0.9.0
221
+
222
+ * Add HTTPClient adapter (@hakanensari)
223
+ * Improve Retry handler (@mislav)
224
+ * Remove autoloading by default (@technoweenie)
225
+ * Improve internal docs (@technoweenie, @mislav)
226
+ * Respect user/password in http proxy string (@mislav)
227
+ * Adapter options are structs. Reinforces consistent options across adapters
228
+ (@technoweenie)
229
+ * Stop stripping trailing / off base URLs in a Faraday::Connection. (@technoweenie)
230
+ * Add a configurable URI parser. (@technoweenie)
231
+ * Remove need to manually autoload when using the authorization header helpers on `Faraday::Connection`. (@technoweenie)
232
+ * `Faraday::Adapter::Test` respects the `Faraday::RequestOptions#params_encoder` option. (@technoweenie)
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Faraday
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
4
- [![Build Status](https://travis-ci.org/lostisland/faraday.svg)](https://travis-ci.org/lostisland/faraday)
4
+ [![Build Status](https://travis-ci.org/lostisland/faraday.svg?branch=master)](https://travis-ci.org/lostisland/faraday)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/lostisland/faraday/badge.svg?branch=master)](https://coveralls.io/github/lostisland/faraday?branch=master)
6
6
  [![Code Climate](https://codeclimate.com/github/lostisland/faraday/badges/gpa.svg)](https://codeclimate.com/github/lostisland/faraday)
7
7
  [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
@@ -31,6 +31,17 @@ Rack::Test, and a Test adapter for stubbing requests by hand.
31
31
 
32
32
  Available at [rubydoc.info](http://www.rubydoc.info/gems/faraday).
33
33
 
34
+ ## Faraday 1.0
35
+
36
+ Faraday 1.0 will ship soon! Faraday 0.17 will be the last 0.x release, except for serious bugs or security issues.
37
+ Faraday 1.0 is a major release, but it will be mostly backwards-compatible with 0.x, so we strongly encourage you
38
+ to use it on your next projects and/or consider to upgrade your existing ones.
39
+ Upgrading is simple:
40
+
41
+ * Checkout the new code from [`master`](https://github.com/lostisland/faraday) and the new [Documentation Website](https://lostisland.github.io/faraday/).
42
+ * If you're upgrading an existing project, check the [UPGRADING](https://github.com/lostisland/faraday/blob/master/UPGRADING.md) file.
43
+ * You can also enable upgrade warnings on apps with Faraday 0.x if you run them with `FARADAY_DEPRECATE=warn` or `FARADAY_DEPRECATE=1` in ENV.
44
+
34
45
  ## Usage
35
46
 
36
47
  ### Basic Use
@@ -54,7 +65,7 @@ Since the default middleware stack uses url\_encoded middleware and default adap
54
65
  ```ruby
55
66
  conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
56
67
  faraday.request :url_encoded # form-encode POST params
57
- faraday.response :logger # log requests to STDOUT
68
+ faraday.response :logger # log requests to $stdout
58
69
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
59
70
  end
60
71
 
@@ -69,7 +80,7 @@ conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday|
69
80
  end
70
81
  ```
71
82
 
72
- Once you have the connection object, use it to make HTTP requests. You can pass paramters to it in a few different ways:
83
+ Once you have the connection object, use it to make HTTP requests. You can pass parameters to it in a few different ways:
73
84
 
74
85
  ```ruby
75
86
  ## GET ##
@@ -79,7 +90,7 @@ response.body
79
90
 
80
91
  conn.get '/nigiri', { :name => 'Maguro' } # GET http://sushi.com/nigiri?name=Maguro
81
92
 
82
- conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
93
+ conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
83
94
  req.url '/search', :page => 2
84
95
  req.params['limit'] = 100
85
96
  end
@@ -118,7 +129,7 @@ conn.get do |req|
118
129
  req.options.context = {
119
130
  foo: 'foo',
120
131
  bar: 'bar'
121
- }
132
+ }
122
133
  end
123
134
  ```
124
135
 
@@ -189,6 +200,9 @@ Faraday.new(...) do |conn|
189
200
  # POST/PUT params encoders:
190
201
  conn.request :multipart
191
202
  conn.request :url_encoded
203
+
204
+ # add custom middleware
205
+ conn.use MyMiddleware
192
206
 
193
207
  # Last middleware must be the adapter:
194
208
  conn.adapter :net_http
@@ -274,7 +288,7 @@ end
274
288
  ### NetHttpPersistent
275
289
  ```ruby
276
290
  conn = Faraday.new(...) do |f|
277
- f.adapter :net_http_persistent do |http| # yields Net::HTTP::Persistent
291
+ f.adapter :net_http_persistent, pool_size: 5 do |http| # yields Net::HTTP::Persistent
278
292
  http.idle_timeout = 100
279
293
  http.retry_change_requests = true
280
294
  end
@@ -360,7 +374,7 @@ of a major release, support for that Ruby version may be dropped.
360
374
  ## Contribute
361
375
 
362
376
  Do you want to contribute to Faraday?
363
- Open the issues page and check for the `any volunteer?` label!
377
+ Open the issues page and check for the `help wanted` label!
364
378
  But before you start coding, please read our [Contributing Guide](https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md)
365
379
 
366
380
  ## Copyright
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake/testtask'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :test
9
+
10
+ desc "Run all tests"
11
+ task :test => :spec do
12
+ exec 'script/test'
13
+ end
@@ -124,9 +124,9 @@ module Faraday
124
124
  end
125
125
  rescue EventMachine::Connectify::CONNECTError => err
126
126
  if err.message.include?("Proxy Authentication Required")
127
- raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
127
+ raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
128
128
  else
129
- raise Error::ConnectionFailed, err
129
+ raise Faraday::ConnectionFailed, err
130
130
  end
131
131
  rescue => err
132
132
  if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
@@ -159,15 +159,15 @@ module Faraday
159
159
  end
160
160
 
161
161
  def raise_error(msg)
162
- errklass = Faraday::Error::ClientError
162
+ errklass = Faraday::ClientError
163
163
  if msg == Errno::ETIMEDOUT
164
- errklass = Faraday::Error::TimeoutError
164
+ errklass = Faraday::TimeoutError
165
165
  msg = "request timed out"
166
166
  elsif msg == Errno::ECONNREFUSED
167
- errklass = Faraday::Error::ConnectionFailed
167
+ errklass = Faraday::ConnectionFailed
168
168
  msg = "connection refused"
169
169
  elsif msg == "connection closed by server"
170
- errklass = Faraday::Error::ConnectionFailed
170
+ errklass = Faraday::ConnectionFailed
171
171
  end
172
172
  raise errklass, msg
173
173
  end
@@ -193,11 +193,11 @@ module Faraday
193
193
 
194
194
  def running?() @running end
195
195
 
196
- def add
196
+ def add(&block)
197
197
  if running?
198
198
  perform_request { yield }
199
199
  else
200
- @registered_procs << Proc.new
200
+ @registered_procs << block
201
201
  end
202
202
  @num_registered += 1
203
203
  end
@@ -211,7 +211,7 @@ module Faraday
211
211
  end
212
212
  end
213
213
  if @errors.size > 0
214
- raise Faraday::Error::ClientError, @errors.first || "connection failed"
214
+ raise Faraday::ClientError, @errors.first || "connection failed"
215
215
  end
216
216
  end
217
217
  ensure
@@ -65,18 +65,18 @@ module Faraday
65
65
 
66
66
  @app.call env
67
67
  rescue Errno::ECONNREFUSED
68
- raise Error::ConnectionFailed, $!
68
+ raise Faraday::ConnectionFailed, $!
69
69
  rescue EventMachine::Connectify::CONNECTError => err
70
70
  if err.message.include?("Proxy Authentication Required")
71
- raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
71
+ raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
72
72
  else
73
- raise Error::ConnectionFailed, err
73
+ raise Faraday::ConnectionFailed, err
74
74
  end
75
75
  rescue Errno::ETIMEDOUT => err
76
- raise Error::TimeoutError, err
76
+ raise Faraday::TimeoutError, err
77
77
  rescue RuntimeError => err
78
78
  if err.message == "connection closed by server"
79
- raise Error::ConnectionFailed, err
79
+ raise Faraday::ConnectionFailed, err
80
80
  else
81
81
  raise
82
82
  end
@@ -15,6 +15,9 @@ module Faraday
15
15
  opts[:client_key] = ssl[:client_key] if ssl[:client_key]
16
16
  opts[:certificate] = ssl[:certificate] if ssl[:certificate]
17
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]
18
21
 
19
22
  # https://github.com/geemus/excon/issues/106
20
23
  # https://github.com/jruby/jruby-ossl/issues/19
@@ -56,14 +59,14 @@ module Faraday
56
59
  @app.call env
57
60
  rescue ::Excon::Errors::SocketError => err
58
61
  if err.message =~ /\btimeout\b/
59
- raise Error::TimeoutError, err
62
+ raise Faraday::TimeoutError, err
60
63
  elsif err.message =~ /\bcertificate\b/
61
64
  raise Faraday::SSLError, err
62
65
  else
63
- raise Error::ConnectionFailed, err
66
+ raise Faraday::ConnectionFailed, err
64
67
  end
65
68
  rescue ::Excon::Errors::Timeout => err
66
- raise Error::TimeoutError, err
69
+ raise Faraday::TimeoutError, err
67
70
  end
68
71
 
69
72
  def create_connection(env, opts)
@@ -43,15 +43,15 @@ module Faraday
43
43
 
44
44
  @app.call env
45
45
  rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
46
- raise Faraday::Error::TimeoutError, $!
46
+ raise Faraday::TimeoutError, $!
47
47
  rescue ::HTTPClient::BadResponseError => err
48
48
  if err.message.include?('status 407')
49
- raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
49
+ raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
50
50
  else
51
- raise Faraday::Error::ClientError, $!
51
+ raise Faraday::ClientError, $!
52
52
  end
53
53
  rescue Errno::ECONNREFUSED, IOError, SocketError
54
- raise Faraday::Error::ConnectionFailed, $!
54
+ raise Faraday::ConnectionFailed, $!
55
55
  rescue => err
56
56
  if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
57
57
  raise Faraday::SSLError, err
@@ -11,6 +11,7 @@ module Faraday
11
11
  class NetHttp < Faraday::Adapter
12
12
  NET_HTTP_EXCEPTIONS = [
13
13
  IOError,
14
+ Errno::EADDRNOTAVAIL,
14
15
  Errno::ECONNABORTED,
15
16
  Errno::ECONNREFUSED,
16
17
  Errno::ECONNRESET,
@@ -28,6 +29,11 @@ module Faraday
28
29
  NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)
29
30
  NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout)
30
31
 
32
+ def initialize(app = nil, opts = {}, &block)
33
+ @cert_store = nil
34
+ super(app, opts, &block)
35
+ end
36
+
31
37
  def call(env)
32
38
  super
33
39
  with_net_http_connection(env) do |http|
@@ -40,7 +46,7 @@ module Faraday
40
46
  if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
41
47
  raise Faraday::SSLError, err
42
48
  else
43
- raise Error::ConnectionFailed, err
49
+ raise Faraday::ConnectionFailed, err
44
50
  end
45
51
  end
46
52
 
@@ -53,9 +59,11 @@ module Faraday
53
59
 
54
60
  @app.call env
55
61
  rescue Timeout::Error, Errno::ETIMEDOUT => err
56
- raise Faraday::Error::TimeoutError, err
62
+ raise Faraday::TimeoutError, err
57
63
  end
58
64
 
65
+ private
66
+
59
67
  def create_request(env)
60
68
  request = Net::HTTPGenericRequest.new \
61
69
  env[:method].to_s.upcase, # request method
@@ -104,21 +112,31 @@ module Faraday
104
112
  http.ca_path = ssl[:ca_path] if ssl[:ca_path]
105
113
  http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
106
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]
107
117
  end
108
118
 
109
119
  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]
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=)
112
129
 
113
130
  @config_block.call(http) if @config_block
114
131
  end
115
132
 
116
133
  def ssl_cert_store(ssl)
117
134
  return ssl[:cert_store] if ssl[:cert_store]
135
+ return @cert_store if @cert_store
118
136
  # 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
137
+ @cert_store = OpenSSL::X509::Store.new
138
+ @cert_store.set_default_paths
139
+ @cert_store
122
140
  end
123
141
 
124
142
  def ssl_verify_mode(ssl)
@@ -1,13 +1,26 @@
1
- # Rely on autoloading instead of explicit require; helps avoid the "already
2
- # initialized constant" warning on Ruby 1.8.7 when NetHttp is refereced below.
3
- # require 'faraday/adapter/net_http'
4
-
5
1
  module Faraday
6
2
  class Adapter
7
3
  class NetHttpPersistent < NetHttp
8
4
  dependency 'net/http/persistent'
9
5
 
6
+ private
7
+
10
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)
11
24
  proxy_uri = nil
12
25
  if (proxy = env[:request][:proxy])
13
26
  proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
@@ -18,36 +31,37 @@ module Faraday
18
31
  define_method(:password) { proxy[:password] }
19
32
  end if proxy[:user]
20
33
  end
21
-
22
- if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
23
- Net::HTTP::Persistent.new(name: 'Faraday', proxy: proxy_uri)
24
- else
25
- Net::HTTP::Persistent.new('Faraday', proxy_uri)
26
- end
34
+ proxy_uri
27
35
  end
28
36
 
29
37
  def perform_request(http, env)
30
38
  http.request env[:url], create_request(env)
31
39
  rescue Errno::ETIMEDOUT => error
32
- raise Faraday::Error::TimeoutError, error
40
+ raise Faraday::TimeoutError, error
33
41
  rescue Net::HTTP::Persistent::Error => error
34
42
  if error.message.include? 'Timeout'
35
- raise Faraday::Error::TimeoutError, error
43
+ raise Faraday::TimeoutError, error
36
44
  elsif error.message.include? 'connection refused'
37
- raise Faraday::Error::ConnectionFailed, error
45
+ raise Faraday::ConnectionFailed, error
38
46
  else
39
47
  raise
40
48
  end
41
49
  end
42
50
 
43
51
  def configure_ssl(http, ssl)
44
- http.verify_mode = ssl_verify_mode(ssl)
45
- http.cert_store = ssl_cert_store(ssl)
52
+ http_set(http, :verify_mode, ssl_verify_mode(ssl))
53
+ http_set(http, :cert_store, ssl_cert_store(ssl))
46
54
 
47
- http.certificate = ssl[:client_cert] if ssl[:client_cert]
48
- http.private_key = ssl[:client_key] if ssl[:client_key]
49
- http.ca_file = ssl[:ca_file] if ssl[:ca_file]
50
- http.ssl_version = ssl[:version] if ssl[:version]
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
51
65
  end
52
66
  end
53
67
  end