faraday 0.9.1 → 0.15.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.
Files changed (73) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE.md +1 -1
  3. data/README.md +189 -28
  4. data/lib/faraday/adapter/em_http.rb +8 -2
  5. data/lib/faraday/adapter/em_http_ssl_patch.rb +1 -1
  6. data/lib/faraday/adapter/em_synchrony.rb +16 -2
  7. data/lib/faraday/adapter/excon.rb +7 -8
  8. data/lib/faraday/adapter/httpclient.rb +27 -5
  9. data/lib/faraday/adapter/net_http.rb +16 -9
  10. data/lib/faraday/adapter/net_http_persistent.rb +24 -9
  11. data/lib/faraday/adapter/patron.rb +40 -12
  12. data/lib/faraday/adapter/test.rb +79 -28
  13. data/lib/faraday/adapter/typhoeus.rb +4 -115
  14. data/lib/faraday/adapter.rb +10 -1
  15. data/lib/faraday/autoload.rb +1 -1
  16. data/lib/faraday/connection.rb +72 -20
  17. data/lib/faraday/error.rb +18 -5
  18. data/lib/faraday/options.rb +40 -18
  19. data/lib/faraday/parameters.rb +54 -38
  20. data/lib/faraday/rack_builder.rb +27 -2
  21. data/lib/faraday/request/authorization.rb +1 -2
  22. data/lib/faraday/request/multipart.rb +7 -2
  23. data/lib/faraday/request/retry.rb +80 -17
  24. data/lib/faraday/request.rb +2 -0
  25. data/lib/faraday/response/logger.rb +28 -7
  26. data/lib/faraday/response.rb +6 -2
  27. data/lib/faraday/utils.rb +32 -3
  28. data/lib/faraday.rb +14 -34
  29. metadata +7 -93
  30. data/.document +0 -6
  31. data/CHANGELOG.md +0 -20
  32. data/CONTRIBUTING.md +0 -36
  33. data/Gemfile +0 -25
  34. data/Rakefile +0 -71
  35. data/faraday.gemspec +0 -34
  36. data/script/cached-bundle +0 -46
  37. data/script/console +0 -7
  38. data/script/generate_certs +0 -42
  39. data/script/package +0 -7
  40. data/script/proxy-server +0 -42
  41. data/script/release +0 -17
  42. data/script/s3-put +0 -71
  43. data/script/server +0 -36
  44. data/script/test +0 -172
  45. data/test/adapters/default_test.rb +0 -14
  46. data/test/adapters/em_http_test.rb +0 -20
  47. data/test/adapters/em_synchrony_test.rb +0 -20
  48. data/test/adapters/excon_test.rb +0 -20
  49. data/test/adapters/httpclient_test.rb +0 -21
  50. data/test/adapters/integration.rb +0 -254
  51. data/test/adapters/logger_test.rb +0 -82
  52. data/test/adapters/net_http_persistent_test.rb +0 -20
  53. data/test/adapters/net_http_test.rb +0 -14
  54. data/test/adapters/patron_test.rb +0 -20
  55. data/test/adapters/rack_test.rb +0 -31
  56. data/test/adapters/test_middleware_test.rb +0 -114
  57. data/test/adapters/typhoeus_test.rb +0 -28
  58. data/test/authentication_middleware_test.rb +0 -65
  59. data/test/composite_read_io_test.rb +0 -111
  60. data/test/connection_test.rb +0 -522
  61. data/test/env_test.rb +0 -218
  62. data/test/helper.rb +0 -81
  63. data/test/live_server.rb +0 -67
  64. data/test/middleware/instrumentation_test.rb +0 -88
  65. data/test/middleware/retry_test.rb +0 -177
  66. data/test/middleware_stack_test.rb +0 -173
  67. data/test/multibyte.txt +0 -1
  68. data/test/options_test.rb +0 -252
  69. data/test/parameters_test.rb +0 -64
  70. data/test/request_middleware_test.rb +0 -142
  71. data/test/response_middleware_test.rb +0 -72
  72. data/test/strawberry.rb +0 -2
  73. data/test/utils_test.rb +0 -58
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 1c31dbb021fc6a43b4f0b102a9a3ccf52a72303a
4
- data.tar.gz: 54da868027777adb5a21145bd6433b0154af2297
2
+ SHA256:
3
+ metadata.gz: 3737507d92b48e4b98e891c5026210048ab70b2b1ee17ea20993b8ffc65eafb5
4
+ data.tar.gz: caaaa7f7ad1bdda848ac7bc70d4d1b9ba9d06bc85912a61b1ee4082086629a26
5
5
  SHA512:
6
- metadata.gz: 22c1c286687ca596bd1d6b7d9a6a3749731f6d8962a5bbbe7e1e39cf22e364b92010ae44446cf1804ba10567a2e6409cc2be537375d9f47ace9bca909196dcd3
7
- data.tar.gz: 7e8116cdeea918b8e7de661ec6d49d839960e26f136903e53db62e8cc353f89b3ac053d5efe6401995129418ae4dfce8fda9c8a471093223dca08002615d10e1
6
+ metadata.gz: 231b96efdd5b0399f204edaeba22d01661b9d301aff6cad7d097c27c1e95f0a0a8adc9184e88f089862fa9db9a833e50a3eb7bfb96034a2df7f1bdd93661f3fb
7
+ data.tar.gz: f19fca5f59454b7b620160c3e58339e0dff8049be29bda8e8f1a494455dac6c62a1c6cbe4b4e141e999ea3df25cbcdf3ef09d65e93d71a882c318c75c3070dec
data/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2013 Rick Olson, Zack Hobson
1
+ Copyright (c) 2009-2017 Rick Olson, Zack Hobson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,23 +1,56 @@
1
1
  # Faraday
2
2
 
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)
5
+ [![Coverage Status](https://coveralls.io/repos/github/lostisland/faraday/badge.svg?branch=master)](https://coveralls.io/github/lostisland/faraday?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/lostisland/faraday/badges/gpa.svg)](https://codeclimate.com/github/lostisland/faraday)
7
+ [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
8
+
9
+
3
10
  Faraday is an HTTP client lib that provides a common interface over many
4
11
  adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
5
12
  processing the request/response cycle.
6
13
 
7
- Faraday supports these adapters:
14
+ Faraday supports these adapters out of the box:
8
15
 
9
- * Net::HTTP
16
+ * [Net::HTTP][net_http] _(default)_
17
+ * [Net::HTTP::Persistent][persistent]
10
18
  * [Excon][]
11
- * [Typhoeus][]
12
19
  * [Patron][]
13
20
  * [EventMachine][]
14
21
  * [HTTPClient][]
15
22
 
23
+ Adapters are slowly being moved into their own gems, or bundled with HTTP clients:
24
+
25
+ * [Typhoeus][]
26
+
16
27
  It also includes a Rack adapter for hitting loaded Rack applications through
17
28
  Rack::Test, and a Test adapter for stubbing requests by hand.
18
29
 
30
+ ## API documentation
31
+
32
+ Available at [rubydoc.info](http://www.rubydoc.info/gems/faraday).
33
+
19
34
  ## Usage
20
35
 
36
+ ### Basic Use
37
+
38
+ ```ruby
39
+ response = Faraday.get 'http://sushi.com/nigiri/sake.json'
40
+ ```
41
+ A simple `get` request can be performed by using the syntax described above. This works if you don't need to set up anything; you can roll with just the default middleware
42
+ stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)).
43
+
44
+ A more flexible way to use Faraday is to start with a Connection object. If you want to keep the same defaults, you can use this syntax:
45
+
46
+ ```ruby
47
+ conn = Faraday.new(:url => 'http://www.example.com')
48
+ response = conn.get '/users' # GET http://www.example.com/users'
49
+ ```
50
+
51
+ Connections can also take an options hash as a parameter or be configured by using a block. Checkout the section called [Advanced middleware usage](#advanced-middleware-usage) for more details about how to use this block for configurations.
52
+ Since the default middleware stack uses url\_encoded middleware and default adapter, use them on building your own middleware stack.
53
+
21
54
  ```ruby
22
55
  conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
23
56
  faraday.request :url_encoded # form-encode POST params
@@ -25,6 +58,20 @@ conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
25
58
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
26
59
  end
27
60
 
61
+ # Filter sensitive information from logs with a regex matcher
62
+
63
+ conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday|
64
+ faraday.request :url_encoded # form-encode POST params
65
+ faraday.response :logger do | logger |
66
+ logger.filter(/(api_key=)(\w+)/,'\1[REMOVED]')
67
+ end
68
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
69
+ end
70
+ ```
71
+
72
+ Once you have the connection object, use it to make HTTP requests. You can pass parameters to it in a few different ways:
73
+
74
+ ```ruby
28
75
  ## GET ##
29
76
 
30
77
  response = conn.get '/nigiri/sake.json' # GET http://sushi.com/nigiri/sake.json
@@ -32,7 +79,7 @@ response.body
32
79
 
33
80
  conn.get '/nigiri', { :name => 'Maguro' } # GET http://sushi.com/nigiri?name=Maguro
34
81
 
35
- conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
82
+ conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
36
83
  req.url '/search', :page => 2
37
84
  req.params['limit'] = 100
38
85
  end
@@ -40,7 +87,11 @@ end
40
87
  ## POST ##
41
88
 
42
89
  conn.post '/nigiri', { :name => 'Maguro' } # POST "name=maguro" to http://sushi.com/nigiri
90
+ ```
91
+
92
+ Some configuration options can be adjusted per request:
43
93
 
94
+ ```ruby
44
95
  # post payload as JSON instead of "www-form-urlencoded" encoding:
45
96
  conn.post do |req|
46
97
  req.url '/nigiri'
@@ -57,11 +108,74 @@ conn.get do |req|
57
108
  end
58
109
  ```
59
110
 
60
- If you don't need to set up anything, you can roll with just the default middleware
61
- stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)):
111
+ And you can inject arbitrary data into the request using the `context` option:
62
112
 
63
113
  ```ruby
64
- response = Faraday.get 'http://sushi.com/nigiri/sake.json'
114
+ # Anything you inject using context option will be available in the env on all middlewares
115
+
116
+ conn.get do |req|
117
+ req.url '/search'
118
+ req.options.context = {
119
+ foo: 'foo',
120
+ bar: 'bar'
121
+ }
122
+ end
123
+ ```
124
+
125
+ ### Changing how parameters are serialized
126
+
127
+ Sometimes you need to send the same URL parameter multiple times with different
128
+ values. This requires manually setting the parameter encoder and can be done on
129
+ either per-connection or per-request basis.
130
+
131
+ ```ruby
132
+ # per-connection setting
133
+ conn = Faraday.new :request => { :params_encoder => Faraday::FlatParamsEncoder }
134
+
135
+ conn.get do |req|
136
+ # per-request setting:
137
+ # req.options.params_encoder = my_encoder
138
+ req.params['roll'] = ['california', 'philadelphia']
139
+ end
140
+ # GET 'http://sushi.com?roll=california&roll=philadelphia'
141
+ ```
142
+
143
+ The value of Faraday `params_encoder` can be any object that responds to:
144
+
145
+ * `encode(hash) #=> String`
146
+ * `decode(string) #=> Hash`
147
+
148
+ The encoder will affect both how query strings are processed and how POST bodies
149
+ get serialized. The default encoder is Faraday::NestedParamsEncoder.
150
+
151
+ ## Authentication
152
+
153
+ Basic and Token authentication are handled by Faraday::Request::BasicAuthentication and Faraday::Request::TokenAuthentication respectively. These can be added as middleware manually or through the helper methods.
154
+
155
+ ```ruby
156
+ Faraday.new(...) do |conn|
157
+ conn.basic_auth('username', 'password')
158
+ end
159
+
160
+ Faraday.new(...) do |conn|
161
+ conn.token_auth('authentication-token')
162
+ end
163
+ ```
164
+
165
+ ## Proxy
166
+
167
+ Faraday will try to automatically infer the proxy settings from your system using `URI#find_proxy`.
168
+ This will retrieve them from environment variables such as http_proxy, ftp_proxy, no_proxy, etc.
169
+ If for any reason you want to disable this behaviour, you can do so by setting the global varibale `ignore_env_proxy`:
170
+
171
+ ```ruby
172
+ Faraday.ignore_env_proxy = true
173
+ ```
174
+
175
+ You can also specify a custom proxy when initializing the connection
176
+
177
+ ```ruby
178
+ Faraday.new('http://www.example.com', :proxy => 'http://proxy.com')
65
179
  ```
66
180
 
67
181
  ## Advanced middleware usage
@@ -76,6 +190,7 @@ Faraday.new(...) do |conn|
76
190
  conn.request :multipart
77
191
  conn.request :url_encoded
78
192
 
193
+ # Last middleware must be the adapter:
79
194
  conn.adapter :net_http
80
195
  end
81
196
  ```
@@ -138,6 +253,53 @@ later, response. Some keys are:
138
253
  :response_headers
139
254
  ```
140
255
 
256
+ ## Ad-hoc adapters customization
257
+
258
+ Faraday is intended to be a generic interface between your code and the adapter. However, sometimes you need to access a feature specific to one of the adapters that is not covered in Faraday's interface.
259
+
260
+ When that happens, you can pass a block when specifying the adapter to customize it. The block parameter will change based on the adapter you're using. See below for some examples.
261
+
262
+ ### NetHttp
263
+ ```ruby
264
+ conn = Faraday.new(...) do |f|
265
+ f.adapter :net_http do |http| # yields Net::HTTP
266
+ http.idle_timeout = 100
267
+ http.verify_callback = lambda do | preverify_ok, cert_store |
268
+ # do something here...
269
+ end
270
+ end
271
+ end
272
+ ```
273
+
274
+ ### NetHttpPersistent
275
+ ```ruby
276
+ conn = Faraday.new(...) do |f|
277
+ f.adapter :net_http_persistent do |http| # yields Net::HTTP::Persistent
278
+ http.idle_timeout = 100
279
+ http.retry_change_requests = true
280
+ end
281
+ end
282
+ ```
283
+
284
+ ### Patron
285
+ ```ruby
286
+ conn = Faraday.new(...) do |f|
287
+ f.adapter :patron do |session| # yields Patron::Session
288
+ session.max_redirects = 10
289
+ end
290
+ end
291
+ ```
292
+
293
+ ### HTTPClient
294
+ ```ruby
295
+ conn = Faraday.new(...) do |f|
296
+ f.adapter :httpclient do |client| # yields HTTPClient
297
+ client.keep_alive_timeout = 20
298
+ client.ssl_config.timeout = 25
299
+ end
300
+ end
301
+ ```
302
+
141
303
  ## Using Faraday for testing
142
304
 
143
305
  ```ruby
@@ -173,23 +335,14 @@ resp = test.get '/else' #=> raises "no such stub" error
173
335
  stubs.verify_stubbed_calls
174
336
  ```
175
337
 
176
- ## TODO
177
-
178
- * support streaming requests/responses
179
- * better stubbing API
180
-
181
338
  ## Supported Ruby versions
182
339
 
183
340
  This library aims to support and is [tested against][travis] the following Ruby
184
341
  implementations:
185
342
 
186
- * MRI 1.8.7
187
- * MRI 1.9.2
188
- * MRI 1.9.3
189
- * MRI 2.0.0
190
- * MRI 2.1.0
191
- * [JRuby][]
192
- * [Rubinius][]
343
+ * Ruby 1.9.3+
344
+ * [JRuby][] 1.7+
345
+ * [Rubinius][] 2+
193
346
 
194
347
  If something doesn't work on one of these Ruby versions, it's a bug.
195
348
 
@@ -204,17 +357,25 @@ implementation, you will be responsible for providing patches in a timely
204
357
  fashion. If critical issues for a particular implementation exist at the time
205
358
  of a major release, support for that Ruby version may be dropped.
206
359
 
360
+ ## Contribute
361
+
362
+ Do you want to contribute to Faraday?
363
+ Open the issues page and check for the `any volunteer?` label!
364
+ But before you start coding, please read our [Contributing Guide](https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md)
365
+
207
366
  ## Copyright
208
367
 
209
- Copyright (c) 2009-2013 [Rick Olson](mailto:technoweenie@gmail.com), Zack Hobson.
368
+ Copyright (c) 2009-2017 [Rick Olson](mailto:technoweenie@gmail.com), Zack Hobson.
210
369
  See [LICENSE][] for details.
211
370
 
212
- [travis]: http://travis-ci.org/lostisland/faraday
213
- [excon]: https://github.com/geemus/excon#readme
214
- [typhoeus]: https://github.com/typhoeus/typhoeus#readme
215
- [patron]: http://toland.github.com/patron/
371
+ [net_http]: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/Net/HTTP.html
372
+ [persistent]: https://github.com/drbrain/net-http-persistent
373
+ [travis]: https://travis-ci.org/lostisland/faraday
374
+ [excon]: https://github.com/excon/excon#readme
375
+ [patron]: http://toland.github.io/patron/
216
376
  [eventmachine]: https://github.com/igrigorik/em-http-request#readme
217
- [httpclient]: https://github.com/nahi/httpclient
218
- [jruby]: http://jruby.org/
219
- [rubinius]: http://rubini.us/
220
- [license]: LICENSE.md
377
+ [httpclient]: https://github.com/nahi/httpclient
378
+ [typhoeus]: https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
379
+ [jruby]: http://jruby.org/
380
+ [rubinius]: http://rubini.us/
381
+ [license]: LICENSE.md
@@ -138,9 +138,11 @@ module Faraday
138
138
 
139
139
  # TODO: reuse the connection to support pipelining
140
140
  def perform_single_request(env)
141
- req = EventMachine::HttpRequest.new(env[:url], connection_config(env))
141
+ req = create_request(env)
142
142
  req.setup_request(env[:method], request_config(env)).callback { |client|
143
- save_response(env, client.response_header.status, client.response) do |resp_headers|
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|
144
146
  client.response_header.each do |name, value|
145
147
  resp_headers[name.to_sym] = value
146
148
  end
@@ -148,6 +150,10 @@ module Faraday
148
150
  }
149
151
  end
150
152
 
153
+ def create_request(env)
154
+ EventMachine::HttpRequest.new(env[:url], connection_config(env).merge(@connection_options))
155
+ end
156
+
151
157
  def error_message(client)
152
158
  client.error or "request failed"
153
159
  end
@@ -39,7 +39,7 @@ module EmHttpSslPatch
39
39
  end
40
40
 
41
41
  def host
42
- parent.connopts.host
42
+ parent.uri.host
43
43
  end
44
44
 
45
45
  def certificate_store
@@ -19,7 +19,7 @@ module Faraday
19
19
 
20
20
  def call(env)
21
21
  super
22
- request = EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env))
22
+ request = create_request(env)
23
23
 
24
24
  http_method = env[:method].to_s.downcase.to_sym
25
25
 
@@ -54,7 +54,9 @@ module Faraday
54
54
 
55
55
  raise client.error if client.error
56
56
 
57
- save_response(env, client.response_header.status, client.response) do |resp_headers|
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|
58
60
  client.response_header.each do |name, value|
59
61
  resp_headers[name.to_sym] = value
60
62
  end
@@ -70,6 +72,14 @@ module Faraday
70
72
  else
71
73
  raise Error::ConnectionFailed, err
72
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
73
83
  rescue => err
74
84
  if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
75
85
  raise Faraday::SSLError, err
@@ -77,6 +87,10 @@ module Faraday
77
87
  raise
78
88
  end
79
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
80
94
  end
81
95
  end
82
96
  end
@@ -3,11 +3,6 @@ module Faraday
3
3
  class Excon < Faraday::Adapter
4
4
  dependency 'excon'
5
5
 
6
- def initialize(app, connection_options = {})
7
- @connection_options = connection_options
8
- super(app)
9
- end
10
-
11
6
  def call(env)
12
7
  super
13
8
 
@@ -35,12 +30,12 @@ module Faraday
35
30
 
36
31
  if req[:open_timeout]
37
32
  opts[:connect_timeout] = req[:open_timeout]
38
- opts[:write_timeout] = req[:open_timeout]
39
33
  end
40
34
 
41
35
  if req[:proxy]
42
36
  opts[:proxy] = {
43
37
  :host => req[:proxy][:uri].host,
38
+ :hostname => req[:proxy][:uri].hostname,
44
39
  :port => req[:proxy][:uri].port,
45
40
  :scheme => req[:proxy][:uri].scheme,
46
41
  :user => req[:proxy][:user],
@@ -49,14 +44,14 @@ module Faraday
49
44
  end
50
45
  end
51
46
 
52
- conn = ::Excon.new(env[:url].to_s, opts.merge(@connection_options))
47
+ conn = create_connection(env, opts)
53
48
 
54
49
  resp = conn.request \
55
50
  :method => env[:method].to_s.upcase,
56
51
  :headers => env[:request_headers],
57
52
  :body => read_body(env)
58
53
 
59
- save_response(env, resp.status.to_i, resp.body, resp.headers)
54
+ save_response(env, resp.status.to_i, resp.body, resp.headers, resp.reason_phrase)
60
55
 
61
56
  @app.call env
62
57
  rescue ::Excon::Errors::SocketError => err
@@ -71,6 +66,10 @@ module Faraday
71
66
  raise Error::TimeoutError, err
72
67
  end
73
68
 
69
+ def create_connection(env, opts)
70
+ ::Excon.new(env[:url].to_s, opts.merge(@connection_options))
71
+ end
72
+
74
73
  # TODO: support streaming requests
75
74
  def read_body(env)
76
75
  env[:body].respond_to?(:read) ? env[:body].read : env[:body]
@@ -10,6 +10,9 @@ module Faraday
10
10
  def call(env)
11
11
  super
12
12
 
13
+ # enable compression
14
+ client.transparent_gzip_decompression = true
15
+
13
16
  if req = env[:request]
14
17
  if proxy = req[:proxy]
15
18
  configure_proxy proxy
@@ -26,6 +29,8 @@ module Faraday
26
29
  configure_ssl ssl
27
30
  end
28
31
 
32
+ configure_client
33
+
29
34
  # TODO Don't stream yet.
30
35
  # https://github.com/nahi/httpclient/pull/90
31
36
  env[:body] = env[:body].read if env[:body].respond_to? :read
@@ -34,10 +39,10 @@ module Faraday
34
39
  :body => env[:body],
35
40
  :header => env[:request_headers]
36
41
 
37
- save_response env, resp.status, resp.body, resp.headers
42
+ save_response env, resp.status, resp.body, resp.headers, resp.reason
38
43
 
39
44
  @app.call env
40
- rescue ::HTTPClient::TimeoutError
45
+ rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
41
46
  raise Faraday::Error::TimeoutError, $!
42
47
  rescue ::HTTPClient::BadResponseError => err
43
48
  if err.message.include?('status 407')
@@ -45,7 +50,7 @@ module Faraday
45
50
  else
46
51
  raise Faraday::Error::ClientError, $!
47
52
  end
48
- rescue Errno::ECONNREFUSED, EOFError
53
+ rescue Errno::ECONNREFUSED, IOError, SocketError
49
54
  raise Faraday::Error::ConnectionFailed, $!
50
55
  rescue => err
51
56
  if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
@@ -69,14 +74,14 @@ module Faraday
69
74
 
70
75
  def configure_ssl(ssl)
71
76
  ssl_config = client.ssl_config
77
+ ssl_config.verify_mode = ssl_verify_mode(ssl)
78
+ ssl_config.cert_store = ssl_cert_store(ssl)
72
79
 
73
80
  ssl_config.add_trust_ca ssl[:ca_file] if ssl[:ca_file]
74
81
  ssl_config.add_trust_ca ssl[:ca_path] if ssl[:ca_path]
75
- ssl_config.cert_store = ssl[:cert_store] if ssl[:cert_store]
76
82
  ssl_config.client_cert = ssl[:client_cert] if ssl[:client_cert]
77
83
  ssl_config.client_key = ssl[:client_key] if ssl[:client_key]
78
84
  ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
79
- ssl_config.verify_mode = ssl_verify_mode(ssl)
80
85
  end
81
86
 
82
87
  def configure_timeouts(req)
@@ -92,6 +97,23 @@ module Faraday
92
97
  end
93
98
  end
94
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
+
95
117
  def ssl_verify_mode(ssl)
96
118
  ssl[:verify_mode] || begin
97
119
  if ssl.fetch(:verify, true)
@@ -10,13 +10,14 @@ module Faraday
10
10
  class Adapter
11
11
  class NetHttp < Faraday::Adapter
12
12
  NET_HTTP_EXCEPTIONS = [
13
- EOFError,
13
+ IOError,
14
14
  Errno::ECONNABORTED,
15
15
  Errno::ECONNREFUSED,
16
16
  Errno::ECONNRESET,
17
17
  Errno::EHOSTUNREACH,
18
18
  Errno::EINVAL,
19
19
  Errno::ENETUNREACH,
20
+ Errno::EPIPE,
20
21
  Net::HTTPBadResponse,
21
22
  Net::HTTPHeaderSyntaxError,
22
23
  Net::ProtocolError,
@@ -31,10 +32,7 @@ module Faraday
31
32
  super
32
33
  with_net_http_connection(env) do |http|
33
34
  configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
34
-
35
- req = env[:request]
36
- http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
37
- http.open_timeout = req[:open_timeout] if req[:open_timeout]
35
+ configure_request(http, env[:request])
38
36
 
39
37
  begin
40
38
  http_response = perform_request(http, env)
@@ -46,7 +44,7 @@ module Faraday
46
44
  end
47
45
  end
48
46
 
49
- save_response(env, http_response.code.to_i, http_response.body || '') do |response_headers|
47
+ save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
50
48
  http_response.each_header do |key, value|
51
49
  response_headers[key] = value
52
50
  end
@@ -54,10 +52,12 @@ module Faraday
54
52
  end
55
53
 
56
54
  @app.call env
57
- rescue Timeout::Error => err
55
+ rescue Timeout::Error, Errno::ETIMEDOUT => err
58
56
  raise Faraday::Error::TimeoutError, err
59
57
  end
60
58
 
59
+ private
60
+
61
61
  def create_request(env)
62
62
  request = Net::HTTPGenericRequest.new \
63
63
  env[:method].to_s.upcase, # request method
@@ -89,10 +89,10 @@ module Faraday
89
89
 
90
90
  def net_http_connection(env)
91
91
  if proxy = env[:request][:proxy]
92
- Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
92
+ Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port, proxy[:user], proxy[:password])
93
93
  else
94
94
  Net::HTTP
95
- end.new(env[:url].host, env[:url].port)
95
+ end.new(env[:url].hostname, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80))
96
96
  end
97
97
 
98
98
  def configure_ssl(http, ssl)
@@ -108,6 +108,13 @@ module Faraday
108
108
  http.ssl_version = ssl[:version] if ssl[:version]
109
109
  end
110
110
 
111
+ def configure_request(http, req)
112
+ http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
113
+ http.open_timeout = req[:open_timeout] if req[:open_timeout]
114
+
115
+ @config_block.call(http) if @config_block
116
+ end
117
+
111
118
  def ssl_cert_store(ssl)
112
119
  return ssl[:cert_store] if ssl[:cert_store]
113
120
  # Use the default cert store by default, i.e. system ca certs
@@ -1,15 +1,25 @@
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
- # Experimental adapter for net-http-persistent
8
3
  class NetHttpPersistent < NetHttp
9
4
  dependency 'net/http/persistent'
10
5
 
11
- def with_net_http_connection(env)
12
- if proxy = env[:request][:proxy]
6
+ private
7
+
8
+ def net_http_connection(env)
9
+ proxy_uri = proxy_uri(env)
10
+
11
+ cached_connection env[:url], proxy_uri do
12
+ if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
13
+ Net::HTTP::Persistent.new(name: 'Faraday', proxy: proxy_uri)
14
+ else
15
+ Net::HTTP::Persistent.new('Faraday', proxy_uri)
16
+ end
17
+ end
18
+ end
19
+
20
+ def proxy_uri(env)
21
+ proxy_uri = nil
22
+ if (proxy = env[:request][:proxy])
13
23
  proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
14
24
  proxy_uri.user = proxy_uri.password = nil
15
25
  # awful patch for net-http-persistent 2.8 not unescaping user/password
@@ -18,12 +28,13 @@ module Faraday
18
28
  define_method(:password) { proxy[:password] }
19
29
  end if proxy[:user]
20
30
  end
21
-
22
- yield Net::HTTP::Persistent.new 'Faraday', proxy_uri
31
+ proxy_uri
23
32
  end
24
33
 
25
34
  def perform_request(http, env)
26
35
  http.request env[:url], create_request(env)
36
+ rescue Errno::ETIMEDOUT => error
37
+ raise Faraday::Error::TimeoutError, error
27
38
  rescue Net::HTTP::Persistent::Error => error
28
39
  if error.message.include? 'Timeout'
29
40
  raise Faraday::Error::TimeoutError, error
@@ -43,6 +54,10 @@ module Faraday
43
54
  http.ca_file = ssl[:ca_file] if ssl[:ca_file]
44
55
  http.ssl_version = ssl[:version] if ssl[:version]
45
56
  end
57
+
58
+ def cached_connection(url, proxy_uri)
59
+ (@cached_connection ||= {})[[url.scheme, url.host, url.port, proxy_uri]] ||= yield
60
+ end
46
61
  end
47
62
  end
48
63
  end