net-http-persistent-retry 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/History.txt +360 -0
- data/lib/net/http/persistent.rb +1051 -0
- data/lib/net/http/persistent/connection.rb +40 -0
- data/lib/net/http/persistent/pool.rb +50 -0
- data/lib/net/http/persistent/timed_stack_multi.rb +69 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3a4a9d41d4a3ce59a999b09c99d8516e6ff9b7786cb27774e4e8e42a17952b72
|
4
|
+
data.tar.gz: 5f67f7458415d6d391910b0bbb661febec53d13255f254e0346c14adc4fd1844
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f2be19c4b667c65b4ab0d72720a6e2c26e212a50e3fa31685a2d2692fa4c836bc0d36882927729003a9d78f7ab453e2c7e7f2774ac11cc91fccc8ca3d6fa1210
|
7
|
+
data.tar.gz: 74e8f28e7bc219c0a1f20b651e953b3c5a2bfac7d34165df857f6e51e47b57496969a675cff86c544784929f7c5e5fd9f1020483e0eab56fa8743d8e76118b4c
|
data/History.txt
ADDED
@@ -0,0 +1,360 @@
|
|
1
|
+
=== Next
|
2
|
+
|
3
|
+
Breaking changes:
|
4
|
+
|
5
|
+
* Requests are no longer retried by net-http-persistent.
|
6
|
+
|
7
|
+
=== 3.0
|
8
|
+
|
9
|
+
Breaking changes:
|
10
|
+
|
11
|
+
* No longer supports ruby 2.0 and earlier
|
12
|
+
* Net::HTTP::Persistent::new now uses keyword arguments for +name+ and
|
13
|
+
+proxy+.
|
14
|
+
* Removed #max_age, use #expired?
|
15
|
+
|
16
|
+
New features:
|
17
|
+
|
18
|
+
* Uses connection_pool to manage all connections for a Net::HTTP::Persistent
|
19
|
+
instance.
|
20
|
+
|
21
|
+
Bug fixes:
|
22
|
+
|
23
|
+
* Add missing SSL options ca_path, ciphers, ssl_timeout, verify_depth.
|
24
|
+
Issue #63 by Johnneylee Jack Rollins.
|
25
|
+
|
26
|
+
=== 2.9.4 / 2014-02-10
|
27
|
+
|
28
|
+
* Bug fixes
|
29
|
+
* Improve proxy escaping from 2.9.2. Pull request #59 by Mislav Marohnić.
|
30
|
+
|
31
|
+
=== 2.9.3 / 2014-02-06
|
32
|
+
|
33
|
+
* Bug fixes
|
34
|
+
* Fix breakage in 2.9.2 for users without proxies. Pull request #56 by
|
35
|
+
Yoshihiro TAKAHARA (merged), #57 by ChuckLin, #58 by Kenny Meyer.
|
36
|
+
|
37
|
+
=== 2.9.2 / 2014-02-05
|
38
|
+
|
39
|
+
* Bug fixes
|
40
|
+
* Special characters in proxy passwords are now handled correctly. Issue
|
41
|
+
#48 by Mislav Marohnić. Pull request #54 by Juha Kajava
|
42
|
+
|
43
|
+
=== 2.9.1 / 2014-01-22
|
44
|
+
|
45
|
+
* Bug fixes
|
46
|
+
* Added license to gemspec. Issue #47 by Benjamin Fleischer
|
47
|
+
* Set Net::HTTP#keep_alive_timeout when supported by ruby. Pull request #53
|
48
|
+
by Dylan Thacker-Smith.
|
49
|
+
* The backtrace is preserved for errors in #reset to help with debugging.
|
50
|
+
Issue #41 by Andrew Cholakian.
|
51
|
+
|
52
|
+
=== 2.9 / 2013-07-24
|
53
|
+
|
54
|
+
* Minor enhancement
|
55
|
+
* Added Net::HTTP::Persistent#max_requests to avoid ECONNRESET for a server
|
56
|
+
that allows a limited number of requests on a connection. Pull request
|
57
|
+
#42 by James Tucker.
|
58
|
+
* Request failures are now raised with the backtrace of the original
|
59
|
+
exception. This gives better insight into the reason for the failure.
|
60
|
+
See #41 by Andrew Cholakian.
|
61
|
+
* OpenSSL is no longer required. If OpenSSL is not available an exception
|
62
|
+
will be raised when attempting to access HTTPS resources. Feature request
|
63
|
+
by André Arko
|
64
|
+
|
65
|
+
* Bug fixes
|
66
|
+
* Explain the proper way of sending parameters depending upon the request
|
67
|
+
method. Issue #35 by André Arko.
|
68
|
+
* Handle Errno::ETIMEDOUT by retrying the request. Issue #36 by André Arko.
|
69
|
+
* Requests retried by ruby 2.x are no longer retried by net-http-persistent.
|
70
|
+
* Finish the connection if an otherwise unhandled exception happens during a
|
71
|
+
request. Bug #46 by Mark Oude Veldhuis.
|
72
|
+
* Net::HTTP::Persistent::detect_idle_timeout now assumes a StandardError
|
73
|
+
indicates the idle timeout has been found. Bug #43 by James Tucker.
|
74
|
+
|
75
|
+
=== 2.8 / 2012-10-17
|
76
|
+
|
77
|
+
* Minor enhancements
|
78
|
+
* Added Net::HTTP::Persistent::detect_idle_timeout which can be used to
|
79
|
+
determine the idle timeout for a host.
|
80
|
+
* The read timeout may now be updated for every request. Issue #33 by
|
81
|
+
Mislav Marohnić
|
82
|
+
* Added NO_PROXY support. Pull Request #31 by Laurence Rowe.
|
83
|
+
* Added #cert and #key aliases for Net::HTTP compatibility. Pull request
|
84
|
+
#26 by dlee.
|
85
|
+
* The artifice gem now disables SSL session reuse to prevent breakage of
|
86
|
+
testing frameworks. Pull Request #29 by Christopher Cooke.
|
87
|
+
* Disabled Net::HTTP::Persistent::SSLReuse on Ruby 2+. This feature is now
|
88
|
+
built-in to Net::HTTP.
|
89
|
+
* Bug fixes
|
90
|
+
* Socket options are set again following connection reset. Pull request #28
|
91
|
+
by cmaion.
|
92
|
+
* #shutdown now works even if no connections were made. Pull Request #24 by
|
93
|
+
James Tucker.
|
94
|
+
* Updated test RSA key size to 1024 bits. Bug #25 by Gunnar Wolf.
|
95
|
+
* The correct host:port are shown in the exception when a proxy connection
|
96
|
+
fails. Bug #30 by glebtv.
|
97
|
+
|
98
|
+
=== 2.7 / 2012-06-06
|
99
|
+
|
100
|
+
* Minor enhancement
|
101
|
+
* Added JRuby compatibility by default for HTTPS connections. (JRuby lacks
|
102
|
+
OpenSSL::SSL::Session.)
|
103
|
+
|
104
|
+
=== 2.6 / 2012-03-26
|
105
|
+
|
106
|
+
* Minor enhancement
|
107
|
+
* Net::HTTP::Persistent#idle_timeout may be set to nil to disable expiration
|
108
|
+
of connections. Pull Request #21 by Aaron Stone
|
109
|
+
|
110
|
+
=== 2.5.2 / 2012-02-13
|
111
|
+
|
112
|
+
* Bug fix
|
113
|
+
* Fix variable shadowing warning.
|
114
|
+
|
115
|
+
=== 2.5.1 / 2012-02-10
|
116
|
+
|
117
|
+
* Bug fix
|
118
|
+
* Reusing SSL connections with HTTP proxies now works. Issue #15 by Paul
|
119
|
+
Ingham and mcrmfc
|
120
|
+
|
121
|
+
=== 2.5 / 2012-02-07
|
122
|
+
|
123
|
+
* Minor enhancements
|
124
|
+
* The proxy may be changed at any time.
|
125
|
+
* The allowed SSL version may now be set via #ssl_version.
|
126
|
+
Issue #16 by astera
|
127
|
+
* Added Net::HTTP::Persistent#override_headers which allows overriding
|
128
|
+
* Net::HTTP default headers like User-Agent. See
|
129
|
+
Net::HTTP::Persistent@Headers for details. Issue #17 by andkerosine
|
130
|
+
|
131
|
+
* Bug fixes
|
132
|
+
* The ruby 1.8 speed monkeypatch now handles EAGAIN for windows users.
|
133
|
+
Issue #12 by Alwyn Schoeman
|
134
|
+
* Fixed POST example in README. Submitted by injekt.
|
135
|
+
* Fixed various bugs in the shutdown of connections especially cross-thread
|
136
|
+
(which you shouldn't be doing anyways).
|
137
|
+
|
138
|
+
=== 2.4.1 / 2012-02-03
|
139
|
+
|
140
|
+
* Bug fixes
|
141
|
+
* When FakeWeb or WebMock are loaded SSL sessions will not be reused to
|
142
|
+
prevent breakage of testing frameworks. Issue #13 by Matt Brictson, pull
|
143
|
+
request #14 by Zachary Scott
|
144
|
+
* SSL connections are reset when the SSL parameters change.
|
145
|
+
Mechanize issue #194 by dsisnero
|
146
|
+
* Last-use times are now cleaned up in #shutdown.
|
147
|
+
|
148
|
+
=== 2.4 / 2012-01-31
|
149
|
+
|
150
|
+
* Minor enhancement
|
151
|
+
* net-http-persistent now complains if OpenSSL::SSL::VERIFY_PEER is equal to
|
152
|
+
OpenSSL::SSL::VERIFY_NONE. If you have a platform that is broken this way
|
153
|
+
you must define the constant:
|
154
|
+
|
155
|
+
I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG = nil
|
156
|
+
|
157
|
+
at the top level of your application to disable the warning.
|
158
|
+
|
159
|
+
* Bug fix
|
160
|
+
* Fix persisting SSL sessions through HTTP proxies. Mechanize issue #178 by
|
161
|
+
Robert Poor, net-http-persistent issues #10, #11.
|
162
|
+
|
163
|
+
=== 2.3.2 / 2011-12-21
|
164
|
+
|
165
|
+
* Bug fix
|
166
|
+
* Finish connections that were closed by Net::HTTP so they can be restarted.
|
167
|
+
|
168
|
+
=== 2.3.1 / 2011-10-26
|
169
|
+
|
170
|
+
* Bug fix
|
171
|
+
* If a request object already contains a Connection header it will no longer
|
172
|
+
be overridden. This allows keep-alive connections to be disabled on a
|
173
|
+
per-request basis.
|
174
|
+
|
175
|
+
=== 2.3 / 2011-10-25
|
176
|
+
|
177
|
+
* Minor Enhancement
|
178
|
+
* The time since last use for a connection is now recorded in error
|
179
|
+
messages for the connection.
|
180
|
+
|
181
|
+
=== 2.2 / 2011-10-24
|
182
|
+
|
183
|
+
* Minor Enhancements
|
184
|
+
* Added timeouts for idle connections which are set through #idle_timeout.
|
185
|
+
The default timeout is 5 seconds. Reducing the idle timeout is preferred
|
186
|
+
over setting #retry_change_requests to true if you wish to avoid the "too
|
187
|
+
many connection resets" error when POSTing data.
|
188
|
+
* Documented tunables and settings in one place in Net::HTTP::Persistent
|
189
|
+
|
190
|
+
=== 2.1 / 2011-09-19
|
191
|
+
|
192
|
+
* Minor Enhancement
|
193
|
+
* For HTTPS connections, SSL sessions are now reused avoiding the extra
|
194
|
+
round trips and computations of extra SSL handshakes. If you have
|
195
|
+
problems with SSL session reuse it can be disabled by
|
196
|
+
Net::HTTP::Persistent#reuse_ssl_sessions
|
197
|
+
|
198
|
+
* Bug Fixes
|
199
|
+
* The default certificate store is now used even if #verify_mode was not
|
200
|
+
set. Issue #7, Pull Request #8 by Matthew M. Boedicker
|
201
|
+
|
202
|
+
=== 2.0 / 2011-08-26
|
203
|
+
|
204
|
+
* Incompatibility
|
205
|
+
* Net::HTTP::Persistent#verify_mode now defaults to
|
206
|
+
OpenSSL::SSL::VERIFY_PEER. This may cause HTTPS request failures if your
|
207
|
+
default certificate store lacks the correct certificates.
|
208
|
+
|
209
|
+
=== 1.9 / 2011-08-26
|
210
|
+
|
211
|
+
* Minor Enhancement
|
212
|
+
* Added Net::HTTP::Persistent#cert_store to set an SSL certificate store
|
213
|
+
which defaults to the OpenSSL default certificate store.
|
214
|
+
|
215
|
+
HTTPS server certificates will be validated when this option is combined
|
216
|
+
with setting Net::HTTP::Persistent#verify_mode to
|
217
|
+
OpenSSL::SSL::VERIFY_PEER.
|
218
|
+
|
219
|
+
=== 1.8.1 / 2011-08-08
|
220
|
+
|
221
|
+
* Bug Fix
|
222
|
+
* Requests with OpenSSL errors are retried now. Pull Request #5 by James
|
223
|
+
Tucker.
|
224
|
+
|
225
|
+
=== 1.8 / 2011-06-27
|
226
|
+
|
227
|
+
* Minor Enhancement
|
228
|
+
* Added Net::HTTP::Persistent#retry_change_requests which allows POST and
|
229
|
+
other non-idempotent requests to be retried automatically. Take care when
|
230
|
+
enabling this option to ensure the server will handle multiple POSTs with
|
231
|
+
the same data in a sane manner.
|
232
|
+
|
233
|
+
=== 1.7 / 2011-04-17
|
234
|
+
|
235
|
+
* Minor Enhancement
|
236
|
+
* Added Net::HTTP::Persistent#pipeline which integrates with
|
237
|
+
net-http-pipeline when it is present.
|
238
|
+
* Bug Fix
|
239
|
+
* Perform a case-insensitive check of the URI scheme for HTTPS URIs
|
240
|
+
|
241
|
+
=== 1.6.1 / 2011-03-08
|
242
|
+
|
243
|
+
* Bug Fix
|
244
|
+
* Net::HTTP::Persistent#request now handles Errno::EINVAL as a connection
|
245
|
+
reset and will be retried for idempotent requests. Reported by Aaron
|
246
|
+
Qian.
|
247
|
+
|
248
|
+
=== 1.6 / 2011-03-01
|
249
|
+
|
250
|
+
* Minor Enhancement
|
251
|
+
* Added Net::HTTP::Persistent#socket_options to set multiple socket options
|
252
|
+
at socket startup.
|
253
|
+
|
254
|
+
=== 1.5.2 / 2011-02-24
|
255
|
+
|
256
|
+
* Bug Fix
|
257
|
+
* Only set TCP_NODELAY if the connection has an @socket. Allows
|
258
|
+
net-http-persistent to be used with fake_web. Reported by Sathish
|
259
|
+
Pasupunuri.
|
260
|
+
|
261
|
+
=== 1.5.1 / 2011-02-10
|
262
|
+
|
263
|
+
* Bug fix
|
264
|
+
* Only set TCP_NODELAY at connection start. Reported by Brian Henderson.
|
265
|
+
|
266
|
+
=== 1.5 / 2011-01-25
|
267
|
+
|
268
|
+
* Minor Enhancements
|
269
|
+
* Set TCP_NODELAY on created socket if possible. (This will only help for
|
270
|
+
requests that send bodies.)
|
271
|
+
|
272
|
+
=== 1.4.1 / 2010-10-13
|
273
|
+
|
274
|
+
* Bug Fixes
|
275
|
+
* Don't finish the connection when we're retrying, reset it. Patch by James
|
276
|
+
Tucker.
|
277
|
+
|
278
|
+
=== 1.4 / 2010-09-29
|
279
|
+
|
280
|
+
* Minor Enhancements
|
281
|
+
* Added the very dangerous #shutdown_in_all_threads. IT IS DANGEROUS!.
|
282
|
+
Patch by Torsten Schönebaum.
|
283
|
+
|
284
|
+
=== 1.3.1 / 2010-09-13
|
285
|
+
|
286
|
+
* Bug Fixes
|
287
|
+
* #connection_for no longer tries to ssl-enable an existing connection.
|
288
|
+
Patch by Joseph West.
|
289
|
+
|
290
|
+
=== 1.3 / 2010-09-08
|
291
|
+
|
292
|
+
* Minor Enhancements
|
293
|
+
* HTTP versions are now recorded. This information is not currently used.
|
294
|
+
|
295
|
+
* Bug Fixes
|
296
|
+
* #shutdown no longer fails when an unstarted HTTP connection is shut down.
|
297
|
+
|
298
|
+
=== 1.2.5 / 2010-07-27
|
299
|
+
|
300
|
+
* Bug Fixes
|
301
|
+
* Fix duplicated test name. Noted by Peter Higgins.
|
302
|
+
* #shutdown now works even when no connections were made.
|
303
|
+
|
304
|
+
=== 1.2.4 / 2010-07-26
|
305
|
+
|
306
|
+
* Bug Fixes
|
307
|
+
* Actually have #request only finish a connection. Somehow this got
|
308
|
+
missed.
|
309
|
+
|
310
|
+
=== 1.2.3 / 2010-06-29
|
311
|
+
|
312
|
+
* Bug Fixes
|
313
|
+
* Fix example code (pointed out by Alex Stahl)
|
314
|
+
|
315
|
+
=== 1.2.2 / 2010-06-22
|
316
|
+
|
317
|
+
* Bug Fixes
|
318
|
+
* #request only finishes a connection instead of restarting it. This helps
|
319
|
+
prevents errors on non-idempotent HTTP requests after errors.
|
320
|
+
* #connection_for handles EHOSTDOWN like #reset
|
321
|
+
|
322
|
+
=== 1.2.1 / 2010-05-25
|
323
|
+
|
324
|
+
* Bug Fixes
|
325
|
+
* Don't alter Net::BufferedIO#rbuf_fill on 1.9+
|
326
|
+
|
327
|
+
=== 1.2 / 2010-05-20
|
328
|
+
|
329
|
+
* Minor Enhancements
|
330
|
+
* Net::HTTP#read_timeout is now supported
|
331
|
+
* Net::HTTP#open_timeout is now supported
|
332
|
+
* Net::HTTP::Persistent#request now supports a block like Net::HTTP#request
|
333
|
+
|
334
|
+
=== 1.1 / 2010-05-18
|
335
|
+
|
336
|
+
* Minor Enhancements
|
337
|
+
* Proxy support, see Net::HTTP::Persistent::new,
|
338
|
+
Net::HTTP::Persistent#proxy_from_env
|
339
|
+
* Added +name+ parameter to Net::HTTP::Persistent::new for separation of
|
340
|
+
connection pools.
|
341
|
+
* Added Net::HTTP::Persistent#shutdown so you can clean up after yourself
|
342
|
+
* Net::HTTP::Persistent now suppresses "peer certificate won't be verified
|
343
|
+
in this SSL session" warning.
|
344
|
+
|
345
|
+
* Bug Fixes
|
346
|
+
* Net::HTTP::Persistent retries requests in accordance with RFC 2616.
|
347
|
+
|
348
|
+
=== 1.0.1 / 2010-05-05
|
349
|
+
|
350
|
+
* Minor Enhancements
|
351
|
+
* Added #debug_output
|
352
|
+
* Now uses Hoe minitest plugin
|
353
|
+
* Bug Fixes
|
354
|
+
* Tests pass on 1.9
|
355
|
+
|
356
|
+
=== 1.0.0 / 2010-05-04
|
357
|
+
|
358
|
+
* Major Enhancements
|
359
|
+
* Birthday!
|
360
|
+
|
@@ -0,0 +1,1051 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'cgi' # for escaping
|
4
|
+
require 'connection_pool'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'net/http/pipeline'
|
8
|
+
rescue LoadError
|
9
|
+
end
|
10
|
+
|
11
|
+
autoload :OpenSSL, 'openssl'
|
12
|
+
|
13
|
+
##
|
14
|
+
# Persistent connections for Net::HTTP
|
15
|
+
#
|
16
|
+
# Net::HTTP::Persistent maintains persistent connections across all the
|
17
|
+
# servers you wish to talk to. For each host:port you communicate with a
|
18
|
+
# single persistent connection is created.
|
19
|
+
#
|
20
|
+
# Multiple Net::HTTP::Persistent objects will share the same set of
|
21
|
+
# connections.
|
22
|
+
#
|
23
|
+
# For each thread you start a new connection will be created. A
|
24
|
+
# Net::HTTP::Persistent connection will not be shared across threads.
|
25
|
+
#
|
26
|
+
# You can shut down the HTTP connections when done by calling #shutdown. You
|
27
|
+
# should name your Net::HTTP::Persistent object if you intend to call this
|
28
|
+
# method.
|
29
|
+
#
|
30
|
+
# Example:
|
31
|
+
#
|
32
|
+
# require 'net/http/persistent'
|
33
|
+
#
|
34
|
+
# uri = URI 'http://example.com/awesome/web/service'
|
35
|
+
#
|
36
|
+
# http = Net::HTTP::Persistent.new name: 'my_app_name'
|
37
|
+
#
|
38
|
+
# # perform a GET
|
39
|
+
# response = http.request uri
|
40
|
+
#
|
41
|
+
# # or
|
42
|
+
#
|
43
|
+
# get = Net::HTTP::Get.new uri.request_uri
|
44
|
+
# response = http.request get
|
45
|
+
#
|
46
|
+
# # create a POST
|
47
|
+
# post_uri = uri + 'create'
|
48
|
+
# post = Net::HTTP::Post.new post_uri.path
|
49
|
+
# post.set_form_data 'some' => 'cool data'
|
50
|
+
#
|
51
|
+
# # perform the POST, the URI is always required
|
52
|
+
# response http.request post_uri, post
|
53
|
+
#
|
54
|
+
# Note that for GET, HEAD and other requests that do not have a body you want
|
55
|
+
# to use URI#request_uri not URI#path. The request_uri contains the query
|
56
|
+
# params which are sent in the body for other requests.
|
57
|
+
#
|
58
|
+
# == SSL
|
59
|
+
#
|
60
|
+
# SSL connections are automatically created depending upon the scheme of the
|
61
|
+
# URI. SSL connections are automatically verified against the default
|
62
|
+
# certificate store for your computer. You can override this by changing
|
63
|
+
# verify_mode or by specifying an alternate cert_store.
|
64
|
+
#
|
65
|
+
# Here are the SSL settings, see the individual methods for documentation:
|
66
|
+
#
|
67
|
+
# #certificate :: This client's certificate
|
68
|
+
# #ca_file :: The certificate-authorities
|
69
|
+
# #ca_path :: Directory with certificate-authorities
|
70
|
+
# #cert_store :: An SSL certificate store
|
71
|
+
# #ciphers :: List of SSl ciphers allowed
|
72
|
+
# #private_key :: The client's SSL private key
|
73
|
+
# #reuse_ssl_sessions :: Reuse a previously opened SSL session for a new
|
74
|
+
# connection
|
75
|
+
# #ssl_timeout :: SSL session lifetime
|
76
|
+
# #ssl_version :: Which specific SSL version to use
|
77
|
+
# #verify_callback :: For server certificate verification
|
78
|
+
# #verify_depth :: Depth of certificate verification
|
79
|
+
# #verify_mode :: How connections should be verified
|
80
|
+
#
|
81
|
+
# == Proxies
|
82
|
+
#
|
83
|
+
# A proxy can be set through #proxy= or at initialization time by providing a
|
84
|
+
# second argument to ::new. The proxy may be the URI of the proxy server or
|
85
|
+
# <code>:ENV</code> which will consult environment variables.
|
86
|
+
#
|
87
|
+
# See #proxy= and #proxy_from_env for details.
|
88
|
+
#
|
89
|
+
# == Headers
|
90
|
+
#
|
91
|
+
# Headers may be specified for use in every request. #headers are appended to
|
92
|
+
# any headers on the request. #override_headers replace existing headers on
|
93
|
+
# the request.
|
94
|
+
#
|
95
|
+
# The difference between the two can be seen in setting the User-Agent. Using
|
96
|
+
# <code>http.headers['User-Agent'] = 'MyUserAgent'</code> will send "Ruby,
|
97
|
+
# MyUserAgent" while <code>http.override_headers['User-Agent'] =
|
98
|
+
# 'MyUserAgent'</code> will send "MyUserAgent".
|
99
|
+
#
|
100
|
+
# == Tuning
|
101
|
+
#
|
102
|
+
# === Segregation
|
103
|
+
#
|
104
|
+
# By providing an application name to ::new you can separate your connections
|
105
|
+
# from the connections of other applications.
|
106
|
+
#
|
107
|
+
# === Idle Timeout
|
108
|
+
#
|
109
|
+
# If a connection hasn't been used for this number of seconds it will automatically be
|
110
|
+
# reset upon the next use to avoid attempting to send to a closed connection.
|
111
|
+
# The default value is 5 seconds. nil means no timeout. Set through #idle_timeout.
|
112
|
+
#
|
113
|
+
# Reducing this value may help avoid the "too many connection resets" error
|
114
|
+
# when sending non-idempotent requests while increasing this value will cause
|
115
|
+
# fewer round-trips.
|
116
|
+
#
|
117
|
+
# === Read Timeout
|
118
|
+
#
|
119
|
+
# The amount of time allowed between reading two chunks from the socket. Set
|
120
|
+
# through #read_timeout
|
121
|
+
#
|
122
|
+
# === Max Requests
|
123
|
+
#
|
124
|
+
# The number of requests that should be made before opening a new connection.
|
125
|
+
# Typically many keep-alive capable servers tune this to 100 or less, so the
|
126
|
+
# 101st request will fail with ECONNRESET. If unset (default), this value has no
|
127
|
+
# effect, if set, connections will be reset on the request after max_requests.
|
128
|
+
#
|
129
|
+
# === Open Timeout
|
130
|
+
#
|
131
|
+
# The amount of time to wait for a connection to be opened. Set through
|
132
|
+
# #open_timeout.
|
133
|
+
#
|
134
|
+
# === Socket Options
|
135
|
+
#
|
136
|
+
# Socket options may be set on newly-created connections. See #socket_options
|
137
|
+
# for details.
|
138
|
+
#
|
139
|
+
# === Connection Termination
|
140
|
+
#
|
141
|
+
# If you are done using the Net::HTTP::Persistent instance you may shut down
|
142
|
+
# all the connections in the current thread with #shutdown. This is not
|
143
|
+
# recommended for normal use, it should only be used when it will be several
|
144
|
+
# minutes before you make another HTTP request.
|
145
|
+
#
|
146
|
+
# If you are using multiple threads, call #shutdown in each thread when the
|
147
|
+
# thread is done making requests. If you don't call shutdown, that's OK.
|
148
|
+
# Ruby will automatically garbage collect and shutdown your HTTP connections
|
149
|
+
# when the thread terminates.
|
150
|
+
|
151
|
+
class Net::HTTP::Persistent
|
152
|
+
|
153
|
+
##
|
154
|
+
# The beginning of Time
|
155
|
+
|
156
|
+
EPOCH = Time.at 0 # :nodoc:
|
157
|
+
|
158
|
+
##
|
159
|
+
# Is OpenSSL available? This test works with autoload
|
160
|
+
|
161
|
+
HAVE_OPENSSL = defined? OpenSSL::SSL # :nodoc:
|
162
|
+
|
163
|
+
##
|
164
|
+
# The default connection pool size is 1/4 the allowed open files.
|
165
|
+
|
166
|
+
DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4
|
167
|
+
|
168
|
+
##
|
169
|
+
# The version of Net::HTTP::Persistent you are using
|
170
|
+
|
171
|
+
VERSION = '3.0.0'
|
172
|
+
|
173
|
+
##
|
174
|
+
# Error class for errors raised by Net::HTTP::Persistent. Various
|
175
|
+
# SystemCallErrors are re-raised with a human-readable message under this
|
176
|
+
# class.
|
177
|
+
|
178
|
+
class Error < StandardError; end
|
179
|
+
|
180
|
+
##
|
181
|
+
# Use this method to detect the idle timeout of the host at +uri+. The
|
182
|
+
# value returned can be used to configure #idle_timeout. +max+ controls the
|
183
|
+
# maximum idle timeout to detect.
|
184
|
+
#
|
185
|
+
# After
|
186
|
+
#
|
187
|
+
# Idle timeout detection is performed by creating a connection then
|
188
|
+
# performing a HEAD request in a loop until the connection terminates
|
189
|
+
# waiting one additional second per loop.
|
190
|
+
#
|
191
|
+
# NOTE: This may not work on ruby > 1.9.
|
192
|
+
|
193
|
+
def self.detect_idle_timeout uri, max = 10
|
194
|
+
uri = URI uri unless URI::Generic === uri
|
195
|
+
uri += '/'
|
196
|
+
|
197
|
+
req = Net::HTTP::Head.new uri.request_uri
|
198
|
+
|
199
|
+
http = new 'net-http-persistent detect_idle_timeout'
|
200
|
+
|
201
|
+
http.connection_for uri do |connection|
|
202
|
+
sleep_time = 0
|
203
|
+
|
204
|
+
http = connection.http
|
205
|
+
|
206
|
+
loop do
|
207
|
+
response = http.request req
|
208
|
+
|
209
|
+
$stderr.puts "HEAD #{uri} => #{response.code}" if $DEBUG
|
210
|
+
|
211
|
+
unless Net::HTTPOK === response then
|
212
|
+
raise Error, "bad response code #{response.code} detecting idle timeout"
|
213
|
+
end
|
214
|
+
|
215
|
+
break if sleep_time >= max
|
216
|
+
|
217
|
+
sleep_time += 1
|
218
|
+
|
219
|
+
$stderr.puts "sleeping #{sleep_time}" if $DEBUG
|
220
|
+
sleep sleep_time
|
221
|
+
end
|
222
|
+
end
|
223
|
+
rescue
|
224
|
+
# ignore StandardErrors, we've probably found the idle timeout.
|
225
|
+
ensure
|
226
|
+
return sleep_time unless $!
|
227
|
+
end
|
228
|
+
|
229
|
+
##
|
230
|
+
# This client's OpenSSL::X509::Certificate
|
231
|
+
|
232
|
+
attr_reader :certificate
|
233
|
+
|
234
|
+
##
|
235
|
+
# For Net::HTTP parity
|
236
|
+
|
237
|
+
alias cert certificate
|
238
|
+
|
239
|
+
##
|
240
|
+
# An SSL certificate authority. Setting this will set verify_mode to
|
241
|
+
# VERIFY_PEER.
|
242
|
+
|
243
|
+
attr_reader :ca_file
|
244
|
+
|
245
|
+
##
|
246
|
+
# A directory of SSL certificates to be used as certificate authorities.
|
247
|
+
# Setting this will set verify_mode to VERIFY_PEER.
|
248
|
+
|
249
|
+
attr_reader :ca_path
|
250
|
+
|
251
|
+
##
|
252
|
+
# An SSL certificate store. Setting this will override the default
|
253
|
+
# certificate store. See verify_mode for more information.
|
254
|
+
|
255
|
+
attr_reader :cert_store
|
256
|
+
|
257
|
+
##
|
258
|
+
# The ciphers allowed for SSL connections
|
259
|
+
|
260
|
+
attr_reader :ciphers
|
261
|
+
|
262
|
+
##
|
263
|
+
# Sends debug_output to this IO via Net::HTTP#set_debug_output.
|
264
|
+
#
|
265
|
+
# Never use this method in production code, it causes a serious security
|
266
|
+
# hole.
|
267
|
+
|
268
|
+
attr_accessor :debug_output
|
269
|
+
|
270
|
+
##
|
271
|
+
# Current connection generation
|
272
|
+
|
273
|
+
attr_reader :generation # :nodoc:
|
274
|
+
|
275
|
+
##
|
276
|
+
# Headers that are added to every request using Net::HTTP#add_field
|
277
|
+
|
278
|
+
attr_reader :headers
|
279
|
+
|
280
|
+
##
|
281
|
+
# Maps host:port to an HTTP version. This allows us to enable version
|
282
|
+
# specific features.
|
283
|
+
|
284
|
+
attr_reader :http_versions
|
285
|
+
|
286
|
+
##
|
287
|
+
# Maximum time an unused connection can remain idle before being
|
288
|
+
# automatically closed.
|
289
|
+
|
290
|
+
attr_accessor :idle_timeout
|
291
|
+
|
292
|
+
##
|
293
|
+
# Maximum number of requests on a connection before it is considered expired
|
294
|
+
# and automatically closed.
|
295
|
+
|
296
|
+
attr_accessor :max_requests
|
297
|
+
|
298
|
+
##
|
299
|
+
# The value sent in the Keep-Alive header. Defaults to 30. Not needed for
|
300
|
+
# HTTP/1.1 servers.
|
301
|
+
#
|
302
|
+
# This may not work correctly for HTTP/1.0 servers
|
303
|
+
#
|
304
|
+
# This method may be removed in a future version as RFC 2616 does not
|
305
|
+
# require this header.
|
306
|
+
|
307
|
+
attr_accessor :keep_alive
|
308
|
+
|
309
|
+
##
|
310
|
+
# A name for this connection. Allows you to keep your connections apart
|
311
|
+
# from everybody else's.
|
312
|
+
|
313
|
+
attr_reader :name
|
314
|
+
|
315
|
+
##
|
316
|
+
# Seconds to wait until a connection is opened. See Net::HTTP#open_timeout
|
317
|
+
|
318
|
+
attr_accessor :open_timeout
|
319
|
+
|
320
|
+
##
|
321
|
+
# Headers that are added to every request using Net::HTTP#[]=
|
322
|
+
|
323
|
+
attr_reader :override_headers
|
324
|
+
|
325
|
+
##
|
326
|
+
# This client's SSL private key
|
327
|
+
|
328
|
+
attr_reader :private_key
|
329
|
+
|
330
|
+
##
|
331
|
+
# For Net::HTTP parity
|
332
|
+
|
333
|
+
alias key private_key
|
334
|
+
|
335
|
+
##
|
336
|
+
# The URL through which requests will be proxied
|
337
|
+
|
338
|
+
attr_reader :proxy_uri
|
339
|
+
|
340
|
+
##
|
341
|
+
# List of host suffixes which will not be proxied
|
342
|
+
|
343
|
+
attr_reader :no_proxy
|
344
|
+
|
345
|
+
##
|
346
|
+
# Test-only accessor for the connection pool
|
347
|
+
|
348
|
+
attr_reader :pool # :nodoc:
|
349
|
+
|
350
|
+
##
|
351
|
+
# Seconds to wait until reading one block. See Net::HTTP#read_timeout
|
352
|
+
|
353
|
+
attr_accessor :read_timeout
|
354
|
+
|
355
|
+
##
|
356
|
+
# Seconds to wait until writing one block. See Net::HTTP#write_timeout
|
357
|
+
|
358
|
+
attr_accessor :write_timeout
|
359
|
+
|
360
|
+
##
|
361
|
+
# By default SSL sessions are reused to avoid extra SSL handshakes. Set
|
362
|
+
# this to false if you have problems communicating with an HTTPS server
|
363
|
+
# like:
|
364
|
+
#
|
365
|
+
# SSL_connect [...] read finished A: unexpected message (OpenSSL::SSL::SSLError)
|
366
|
+
|
367
|
+
attr_accessor :reuse_ssl_sessions
|
368
|
+
|
369
|
+
##
|
370
|
+
# An array of options for Socket#setsockopt.
|
371
|
+
#
|
372
|
+
# By default the TCP_NODELAY option is set on sockets.
|
373
|
+
#
|
374
|
+
# To set additional options append them to this array:
|
375
|
+
#
|
376
|
+
# http.socket_options << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1]
|
377
|
+
|
378
|
+
attr_reader :socket_options
|
379
|
+
|
380
|
+
##
|
381
|
+
# Current SSL connection generation
|
382
|
+
|
383
|
+
attr_reader :ssl_generation # :nodoc:
|
384
|
+
|
385
|
+
##
|
386
|
+
# SSL session lifetime
|
387
|
+
|
388
|
+
attr_reader :ssl_timeout
|
389
|
+
|
390
|
+
##
|
391
|
+
# SSL version to use.
|
392
|
+
#
|
393
|
+
# By default, the version will be negotiated automatically between client
|
394
|
+
# and server. Ruby 1.9 and newer only. Deprecated since Ruby 2.5.
|
395
|
+
|
396
|
+
attr_reader :ssl_version
|
397
|
+
|
398
|
+
##
|
399
|
+
# Minimum SSL version to use, e.g. :TLS1_1
|
400
|
+
#
|
401
|
+
# By default, the version will be negotiated automatically between client
|
402
|
+
# and server. Ruby 2.5 and newer only.
|
403
|
+
|
404
|
+
attr_reader :min_version
|
405
|
+
|
406
|
+
##
|
407
|
+
# Maximum SSL version to use, e.g. :TLS1_2
|
408
|
+
#
|
409
|
+
# By default, the version will be negotiated automatically between client
|
410
|
+
# and server. Ruby 2.5 and newer only.
|
411
|
+
|
412
|
+
attr_reader :max_version
|
413
|
+
|
414
|
+
##
|
415
|
+
# Where this instance's last-use times live in the thread local variables
|
416
|
+
|
417
|
+
attr_reader :timeout_key # :nodoc:
|
418
|
+
|
419
|
+
##
|
420
|
+
# SSL verification callback. Used when ca_file or ca_path is set.
|
421
|
+
|
422
|
+
attr_reader :verify_callback
|
423
|
+
|
424
|
+
##
|
425
|
+
# Sets the depth of SSL certificate verification
|
426
|
+
|
427
|
+
attr_reader :verify_depth
|
428
|
+
|
429
|
+
##
|
430
|
+
# HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER which verifies
|
431
|
+
# the server certificate.
|
432
|
+
#
|
433
|
+
# If no ca_file, ca_path or cert_store is set the default system certificate
|
434
|
+
# store is used.
|
435
|
+
#
|
436
|
+
# You can use +verify_mode+ to override any default values.
|
437
|
+
|
438
|
+
attr_reader :verify_mode
|
439
|
+
|
440
|
+
##
|
441
|
+
# Creates a new Net::HTTP::Persistent.
|
442
|
+
#
|
443
|
+
# Set +name+ to keep your connections apart from everybody else's. Not
|
444
|
+
# required currently, but highly recommended. Your library name should be
|
445
|
+
# good enough. This parameter will be required in a future version.
|
446
|
+
#
|
447
|
+
# +proxy+ may be set to a URI::HTTP or :ENV to pick up proxy options from
|
448
|
+
# the environment. See proxy_from_env for details.
|
449
|
+
#
|
450
|
+
# In order to use a URI for the proxy you may need to do some extra work
|
451
|
+
# beyond URI parsing if the proxy requires a password:
|
452
|
+
#
|
453
|
+
# proxy = URI 'http://proxy.example'
|
454
|
+
# proxy.user = 'AzureDiamond'
|
455
|
+
# proxy.password = 'hunter2'
|
456
|
+
#
|
457
|
+
# Set +pool_size+ to limit the maximum number of connections allowed.
|
458
|
+
# Defaults to 1/4 the number of allowed file handles. You can have no more
|
459
|
+
# than this many threads with active HTTP transactions.
|
460
|
+
|
461
|
+
def initialize name: nil, proxy: nil, pool_size: DEFAULT_POOL_SIZE
|
462
|
+
@name = name
|
463
|
+
|
464
|
+
@debug_output = nil
|
465
|
+
@proxy_uri = nil
|
466
|
+
@no_proxy = []
|
467
|
+
@headers = {}
|
468
|
+
@override_headers = {}
|
469
|
+
@http_versions = {}
|
470
|
+
@keep_alive = 30
|
471
|
+
@open_timeout = nil
|
472
|
+
@read_timeout = nil
|
473
|
+
@write_timeout = nil
|
474
|
+
@idle_timeout = 5
|
475
|
+
@max_requests = nil
|
476
|
+
@socket_options = []
|
477
|
+
@ssl_generation = 0 # incremented when SSL session variables change
|
478
|
+
|
479
|
+
@socket_options << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if
|
480
|
+
Socket.const_defined? :TCP_NODELAY
|
481
|
+
|
482
|
+
@pool = Net::HTTP::Persistent::Pool.new size: pool_size do |http_args|
|
483
|
+
Net::HTTP::Persistent::Connection.new Net::HTTP, http_args, @ssl_generation
|
484
|
+
end
|
485
|
+
|
486
|
+
@certificate = nil
|
487
|
+
@ca_file = nil
|
488
|
+
@ca_path = nil
|
489
|
+
@ciphers = nil
|
490
|
+
@private_key = nil
|
491
|
+
@ssl_timeout = nil
|
492
|
+
@ssl_version = nil
|
493
|
+
@min_version = nil
|
494
|
+
@max_version = nil
|
495
|
+
@verify_callback = nil
|
496
|
+
@verify_depth = nil
|
497
|
+
@verify_mode = nil
|
498
|
+
@cert_store = nil
|
499
|
+
|
500
|
+
@generation = 0 # incremented when proxy URI changes
|
501
|
+
|
502
|
+
if HAVE_OPENSSL then
|
503
|
+
@verify_mode = OpenSSL::SSL::VERIFY_PEER
|
504
|
+
@reuse_ssl_sessions = OpenSSL::SSL.const_defined? :Session
|
505
|
+
end
|
506
|
+
|
507
|
+
self.proxy = proxy if proxy
|
508
|
+
end
|
509
|
+
|
510
|
+
##
|
511
|
+
# Sets this client's OpenSSL::X509::Certificate
|
512
|
+
|
513
|
+
def certificate= certificate
|
514
|
+
@certificate = certificate
|
515
|
+
|
516
|
+
reconnect_ssl
|
517
|
+
end
|
518
|
+
|
519
|
+
# For Net::HTTP parity
|
520
|
+
alias cert= certificate=
|
521
|
+
|
522
|
+
##
|
523
|
+
# Sets the SSL certificate authority file.
|
524
|
+
|
525
|
+
def ca_file= file
|
526
|
+
@ca_file = file
|
527
|
+
|
528
|
+
reconnect_ssl
|
529
|
+
end
|
530
|
+
|
531
|
+
##
|
532
|
+
# Sets the SSL certificate authority path.
|
533
|
+
|
534
|
+
def ca_path= path
|
535
|
+
@ca_path = path
|
536
|
+
|
537
|
+
reconnect_ssl
|
538
|
+
end
|
539
|
+
|
540
|
+
##
|
541
|
+
# Overrides the default SSL certificate store used for verifying
|
542
|
+
# connections.
|
543
|
+
|
544
|
+
def cert_store= store
|
545
|
+
@cert_store = store
|
546
|
+
|
547
|
+
reconnect_ssl
|
548
|
+
end
|
549
|
+
|
550
|
+
##
|
551
|
+
# The ciphers allowed for SSL connections
|
552
|
+
|
553
|
+
def ciphers= ciphers
|
554
|
+
@ciphers = ciphers
|
555
|
+
|
556
|
+
reconnect_ssl
|
557
|
+
end
|
558
|
+
|
559
|
+
##
|
560
|
+
# Creates a new connection for +uri+
|
561
|
+
|
562
|
+
def connection_for uri
|
563
|
+
use_ssl = uri.scheme.downcase == 'https'
|
564
|
+
|
565
|
+
net_http_args = [uri.host, uri.port]
|
566
|
+
|
567
|
+
net_http_args.concat @proxy_args if
|
568
|
+
@proxy_uri and not proxy_bypass? uri.host, uri.port
|
569
|
+
|
570
|
+
connection = @pool.checkout net_http_args
|
571
|
+
|
572
|
+
http = connection.http
|
573
|
+
|
574
|
+
connection.ressl @ssl_generation if
|
575
|
+
connection.ssl_generation != @ssl_generation
|
576
|
+
|
577
|
+
if not http.started? then
|
578
|
+
ssl http if use_ssl
|
579
|
+
start http
|
580
|
+
elsif expired? connection then
|
581
|
+
reset connection
|
582
|
+
end
|
583
|
+
|
584
|
+
http.read_timeout = @read_timeout if @read_timeout
|
585
|
+
http.write_timeout = @write_timeout if @write_timeout && http.respond_to?(:write_timeout=)
|
586
|
+
http.keep_alive_timeout = @idle_timeout if @idle_timeout
|
587
|
+
|
588
|
+
return yield connection
|
589
|
+
rescue Errno::ECONNREFUSED
|
590
|
+
address = http.proxy_address || http.address
|
591
|
+
port = http.proxy_port || http.port
|
592
|
+
|
593
|
+
raise Error, "connection refused: #{address}:#{port}"
|
594
|
+
rescue Errno::EHOSTDOWN
|
595
|
+
address = http.proxy_address || http.address
|
596
|
+
port = http.proxy_port || http.port
|
597
|
+
|
598
|
+
raise Error, "host down: #{address}:#{port}"
|
599
|
+
ensure
|
600
|
+
@pool.checkin net_http_args
|
601
|
+
end
|
602
|
+
|
603
|
+
##
|
604
|
+
# URI::escape wrapper
|
605
|
+
|
606
|
+
def escape str
|
607
|
+
CGI.escape str if str
|
608
|
+
end
|
609
|
+
|
610
|
+
##
|
611
|
+
# URI::unescape wrapper
|
612
|
+
|
613
|
+
def unescape str
|
614
|
+
CGI.unescape str if str
|
615
|
+
end
|
616
|
+
|
617
|
+
|
618
|
+
##
|
619
|
+
# Returns true if the connection should be reset due to an idle timeout, or
|
620
|
+
# maximum request count, false otherwise.
|
621
|
+
|
622
|
+
def expired? connection
|
623
|
+
return true if @max_requests && connection.requests >= @max_requests
|
624
|
+
return false unless @idle_timeout
|
625
|
+
return true if @idle_timeout.zero?
|
626
|
+
|
627
|
+
Time.now - connection.last_use > @idle_timeout
|
628
|
+
end
|
629
|
+
|
630
|
+
##
|
631
|
+
# Starts the Net::HTTP +connection+
|
632
|
+
|
633
|
+
def start http
|
634
|
+
http.set_debug_output @debug_output if @debug_output
|
635
|
+
http.open_timeout = @open_timeout if @open_timeout
|
636
|
+
|
637
|
+
http.start
|
638
|
+
|
639
|
+
socket = http.instance_variable_get :@socket
|
640
|
+
|
641
|
+
if socket then # for fakeweb
|
642
|
+
@socket_options.each do |option|
|
643
|
+
socket.io.setsockopt(*option)
|
644
|
+
end
|
645
|
+
end
|
646
|
+
end
|
647
|
+
|
648
|
+
##
|
649
|
+
# Finishes the Net::HTTP +connection+
|
650
|
+
|
651
|
+
def finish connection
|
652
|
+
connection.finish
|
653
|
+
|
654
|
+
connection.http.instance_variable_set :@ssl_session, nil unless
|
655
|
+
@reuse_ssl_sessions
|
656
|
+
end
|
657
|
+
|
658
|
+
##
|
659
|
+
# Returns the HTTP protocol version for +uri+
|
660
|
+
|
661
|
+
def http_version uri
|
662
|
+
@http_versions["#{uri.host}:#{uri.port}"]
|
663
|
+
end
|
664
|
+
|
665
|
+
##
|
666
|
+
# Adds "http://" to the String +uri+ if it is missing.
|
667
|
+
|
668
|
+
def normalize_uri uri
|
669
|
+
(uri =~ /^https?:/) ? uri : "http://#{uri}"
|
670
|
+
end
|
671
|
+
|
672
|
+
##
|
673
|
+
# Pipelines +requests+ to the HTTP server at +uri+ yielding responses if a
|
674
|
+
# block is given. Returns all responses received.
|
675
|
+
#
|
676
|
+
# See
|
677
|
+
# Net::HTTP::Pipeline[http://docs.seattlerb.org/net-http-pipeline/Net/HTTP/Pipeline.html]
|
678
|
+
# for further details.
|
679
|
+
#
|
680
|
+
# Only if <tt>net-http-pipeline</tt> was required before
|
681
|
+
# <tt>net-http-persistent</tt> #pipeline will be present.
|
682
|
+
|
683
|
+
def pipeline uri, requests, &block # :yields: responses
|
684
|
+
connection_for uri do |connection|
|
685
|
+
connection.http.pipeline requests, &block
|
686
|
+
end
|
687
|
+
end
|
688
|
+
|
689
|
+
##
|
690
|
+
# Sets this client's SSL private key
|
691
|
+
|
692
|
+
def private_key= key
|
693
|
+
@private_key = key
|
694
|
+
|
695
|
+
reconnect_ssl
|
696
|
+
end
|
697
|
+
|
698
|
+
# For Net::HTTP parity
|
699
|
+
alias key= private_key=
|
700
|
+
|
701
|
+
##
|
702
|
+
# Sets the proxy server. The +proxy+ may be the URI of the proxy server,
|
703
|
+
# the symbol +:ENV+ which will read the proxy from the environment or nil to
|
704
|
+
# disable use of a proxy. See #proxy_from_env for details on setting the
|
705
|
+
# proxy from the environment.
|
706
|
+
#
|
707
|
+
# If the proxy URI is set after requests have been made, the next request
|
708
|
+
# will shut-down and re-open all connections.
|
709
|
+
#
|
710
|
+
# The +no_proxy+ query parameter can be used to specify hosts which shouldn't
|
711
|
+
# be reached via proxy; if set it should be a comma separated list of
|
712
|
+
# hostname suffixes, optionally with +:port+ appended, for example
|
713
|
+
# <tt>example.com,some.host:8080</tt>.
|
714
|
+
|
715
|
+
def proxy= proxy
|
716
|
+
@proxy_uri = case proxy
|
717
|
+
when :ENV then proxy_from_env
|
718
|
+
when URI::HTTP then proxy
|
719
|
+
when nil then # ignore
|
720
|
+
else raise ArgumentError, 'proxy must be :ENV or a URI::HTTP'
|
721
|
+
end
|
722
|
+
|
723
|
+
@no_proxy.clear
|
724
|
+
|
725
|
+
if @proxy_uri then
|
726
|
+
@proxy_args = [
|
727
|
+
@proxy_uri.host,
|
728
|
+
@proxy_uri.port,
|
729
|
+
unescape(@proxy_uri.user),
|
730
|
+
unescape(@proxy_uri.password),
|
731
|
+
]
|
732
|
+
|
733
|
+
@proxy_connection_id = [nil, *@proxy_args].join ':'
|
734
|
+
|
735
|
+
if @proxy_uri.query then
|
736
|
+
@no_proxy = CGI.parse(@proxy_uri.query)['no_proxy'].join(',').downcase.split(',').map { |x| x.strip }.reject { |x| x.empty? }
|
737
|
+
end
|
738
|
+
end
|
739
|
+
|
740
|
+
reconnect
|
741
|
+
reconnect_ssl
|
742
|
+
end
|
743
|
+
|
744
|
+
##
|
745
|
+
# Creates a URI for an HTTP proxy server from ENV variables.
|
746
|
+
#
|
747
|
+
# If +HTTP_PROXY+ is set a proxy will be returned.
|
748
|
+
#
|
749
|
+
# If +HTTP_PROXY_USER+ or +HTTP_PROXY_PASS+ are set the URI is given the
|
750
|
+
# indicated user and password unless HTTP_PROXY contains either of these in
|
751
|
+
# the URI.
|
752
|
+
#
|
753
|
+
# The +NO_PROXY+ ENV variable can be used to specify hosts which shouldn't
|
754
|
+
# be reached via proxy; if set it should be a comma separated list of
|
755
|
+
# hostname suffixes, optionally with +:port+ appended, for example
|
756
|
+
# <tt>example.com,some.host:8080</tt>. When set to <tt>*</tt> no proxy will
|
757
|
+
# be returned.
|
758
|
+
#
|
759
|
+
# For Windows users, lowercase ENV variables are preferred over uppercase ENV
|
760
|
+
# variables.
|
761
|
+
|
762
|
+
def proxy_from_env
|
763
|
+
env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
|
764
|
+
|
765
|
+
return nil if env_proxy.nil? or env_proxy.empty?
|
766
|
+
|
767
|
+
uri = URI normalize_uri env_proxy
|
768
|
+
|
769
|
+
env_no_proxy = ENV['no_proxy'] || ENV['NO_PROXY']
|
770
|
+
|
771
|
+
# '*' is special case for always bypass
|
772
|
+
return nil if env_no_proxy == '*'
|
773
|
+
|
774
|
+
if env_no_proxy then
|
775
|
+
uri.query = "no_proxy=#{escape(env_no_proxy)}"
|
776
|
+
end
|
777
|
+
|
778
|
+
unless uri.user or uri.password then
|
779
|
+
uri.user = escape ENV['http_proxy_user'] || ENV['HTTP_PROXY_USER']
|
780
|
+
uri.password = escape ENV['http_proxy_pass'] || ENV['HTTP_PROXY_PASS']
|
781
|
+
end
|
782
|
+
|
783
|
+
uri
|
784
|
+
end
|
785
|
+
|
786
|
+
##
|
787
|
+
# Returns true when proxy should by bypassed for host.
|
788
|
+
|
789
|
+
def proxy_bypass? host, port
|
790
|
+
host = host.downcase
|
791
|
+
host_port = [host, port].join ':'
|
792
|
+
|
793
|
+
@no_proxy.each do |name|
|
794
|
+
return true if host[-name.length, name.length] == name or
|
795
|
+
host_port[-name.length, name.length] == name
|
796
|
+
end
|
797
|
+
|
798
|
+
false
|
799
|
+
end
|
800
|
+
|
801
|
+
##
|
802
|
+
# Forces reconnection of HTTP connections.
|
803
|
+
|
804
|
+
def reconnect
|
805
|
+
@generation += 1
|
806
|
+
end
|
807
|
+
|
808
|
+
##
|
809
|
+
# Forces reconnection of SSL connections.
|
810
|
+
|
811
|
+
def reconnect_ssl
|
812
|
+
@ssl_generation += 1
|
813
|
+
end
|
814
|
+
|
815
|
+
##
|
816
|
+
# Finishes then restarts the Net::HTTP +connection+
|
817
|
+
|
818
|
+
def reset connection
|
819
|
+
http = connection.http
|
820
|
+
|
821
|
+
finish connection
|
822
|
+
|
823
|
+
start http
|
824
|
+
rescue Errno::ECONNREFUSED
|
825
|
+
e = Error.new "connection refused: #{http.address}:#{http.port}"
|
826
|
+
e.set_backtrace $@
|
827
|
+
raise e
|
828
|
+
rescue Errno::EHOSTDOWN
|
829
|
+
e = Error.new "host down: #{http.address}:#{http.port}"
|
830
|
+
e.set_backtrace $@
|
831
|
+
raise e
|
832
|
+
end
|
833
|
+
|
834
|
+
##
|
835
|
+
# Makes a request on +uri+. If +req+ is nil a Net::HTTP::Get is performed
|
836
|
+
# against +uri+.
|
837
|
+
#
|
838
|
+
# If a block is passed #request behaves like Net::HTTP#request (the body of
|
839
|
+
# the response will not have been read).
|
840
|
+
#
|
841
|
+
# +req+ must be a Net::HTTPRequest subclass (see Net::HTTP for a list).
|
842
|
+
|
843
|
+
def request uri, req = nil, &block
|
844
|
+
uri = URI uri
|
845
|
+
req = request_setup req || uri
|
846
|
+
response = nil
|
847
|
+
|
848
|
+
connection_for uri do |connection|
|
849
|
+
http = connection.http
|
850
|
+
|
851
|
+
begin
|
852
|
+
connection.requests += 1
|
853
|
+
|
854
|
+
response = http.request req, &block
|
855
|
+
|
856
|
+
if req.connection_close? or
|
857
|
+
(response.http_version <= '1.0' and
|
858
|
+
not response.connection_keep_alive?) or
|
859
|
+
response.connection_close? then
|
860
|
+
finish connection
|
861
|
+
end
|
862
|
+
rescue Exception # make sure to close the connection when it was interrupted
|
863
|
+
finish connection
|
864
|
+
|
865
|
+
raise
|
866
|
+
ensure
|
867
|
+
connection.last_use = Time.now
|
868
|
+
end
|
869
|
+
end
|
870
|
+
|
871
|
+
@http_versions["#{uri.host}:#{uri.port}"] ||= response.http_version
|
872
|
+
|
873
|
+
response
|
874
|
+
end
|
875
|
+
|
876
|
+
##
|
877
|
+
# Creates a GET request if +req_or_uri+ is a URI and adds headers to the
|
878
|
+
# request.
|
879
|
+
#
|
880
|
+
# Returns the request.
|
881
|
+
|
882
|
+
def request_setup req_or_uri # :nodoc:
|
883
|
+
req = if URI === req_or_uri then
|
884
|
+
Net::HTTP::Get.new req_or_uri.request_uri
|
885
|
+
else
|
886
|
+
req_or_uri
|
887
|
+
end
|
888
|
+
|
889
|
+
@headers.each do |pair|
|
890
|
+
req.add_field(*pair)
|
891
|
+
end
|
892
|
+
|
893
|
+
@override_headers.each do |name, value|
|
894
|
+
req[name] = value
|
895
|
+
end
|
896
|
+
|
897
|
+
unless req['Connection'] then
|
898
|
+
req.add_field 'Connection', 'keep-alive'
|
899
|
+
req.add_field 'Keep-Alive', @keep_alive
|
900
|
+
end
|
901
|
+
|
902
|
+
req
|
903
|
+
end
|
904
|
+
|
905
|
+
##
|
906
|
+
# Shuts down all connections
|
907
|
+
#
|
908
|
+
# *NOTE*: Calling shutdown for can be dangerous!
|
909
|
+
#
|
910
|
+
# If any thread is still using a connection it may cause an error! Call
|
911
|
+
# #shutdown when you are completely done making requests!
|
912
|
+
|
913
|
+
def shutdown
|
914
|
+
@pool.shutdown { |http| http.finish }
|
915
|
+
end
|
916
|
+
|
917
|
+
##
|
918
|
+
# Enables SSL on +connection+
|
919
|
+
|
920
|
+
def ssl connection
|
921
|
+
connection.use_ssl = true
|
922
|
+
|
923
|
+
connection.ciphers = @ciphers if @ciphers
|
924
|
+
connection.ssl_timeout = @ssl_timeout if @ssl_timeout
|
925
|
+
connection.ssl_version = @ssl_version if @ssl_version
|
926
|
+
connection.min_version = @min_version if @min_version
|
927
|
+
connection.max_version = @max_version if @max_version
|
928
|
+
|
929
|
+
connection.verify_depth = @verify_depth
|
930
|
+
connection.verify_mode = @verify_mode
|
931
|
+
|
932
|
+
if OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE and
|
933
|
+
not Object.const_defined?(:I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG) then
|
934
|
+
warn <<-WARNING
|
935
|
+
!!!SECURITY WARNING!!!
|
936
|
+
|
937
|
+
The SSL HTTP connection to:
|
938
|
+
|
939
|
+
#{connection.address}:#{connection.port}
|
940
|
+
|
941
|
+
!!!MAY NOT BE VERIFIED!!!
|
942
|
+
|
943
|
+
On your platform your OpenSSL implementation is broken.
|
944
|
+
|
945
|
+
There is no difference between the values of VERIFY_NONE and VERIFY_PEER.
|
946
|
+
|
947
|
+
This means that attempting to verify the security of SSL connections may not
|
948
|
+
work. This exposes you to man-in-the-middle exploits, snooping on the
|
949
|
+
contents of your connection and other dangers to the security of your data.
|
950
|
+
|
951
|
+
To disable this warning define the following constant at top-level in your
|
952
|
+
application:
|
953
|
+
|
954
|
+
I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG = nil
|
955
|
+
|
956
|
+
WARNING
|
957
|
+
end
|
958
|
+
|
959
|
+
connection.ca_file = @ca_file if @ca_file
|
960
|
+
connection.ca_path = @ca_path if @ca_path
|
961
|
+
|
962
|
+
if @ca_file or @ca_path then
|
963
|
+
connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
964
|
+
connection.verify_callback = @verify_callback if @verify_callback
|
965
|
+
end
|
966
|
+
|
967
|
+
if @certificate and @private_key then
|
968
|
+
connection.cert = @certificate
|
969
|
+
connection.key = @private_key
|
970
|
+
end
|
971
|
+
|
972
|
+
connection.cert_store = if @cert_store then
|
973
|
+
@cert_store
|
974
|
+
else
|
975
|
+
store = OpenSSL::X509::Store.new
|
976
|
+
store.set_default_paths
|
977
|
+
store
|
978
|
+
end
|
979
|
+
end
|
980
|
+
|
981
|
+
##
|
982
|
+
# SSL session lifetime
|
983
|
+
|
984
|
+
def ssl_timeout= ssl_timeout
|
985
|
+
@ssl_timeout = ssl_timeout
|
986
|
+
|
987
|
+
reconnect_ssl
|
988
|
+
end
|
989
|
+
|
990
|
+
##
|
991
|
+
# SSL version to use
|
992
|
+
|
993
|
+
def ssl_version= ssl_version
|
994
|
+
@ssl_version = ssl_version
|
995
|
+
|
996
|
+
reconnect_ssl
|
997
|
+
end
|
998
|
+
|
999
|
+
##
|
1000
|
+
# Minimum SSL version to use
|
1001
|
+
|
1002
|
+
def min_version= min_version
|
1003
|
+
@min_version = min_version
|
1004
|
+
|
1005
|
+
reconnect_ssl
|
1006
|
+
end
|
1007
|
+
|
1008
|
+
##
|
1009
|
+
# maximum SSL version to use
|
1010
|
+
|
1011
|
+
def max_version= max_version
|
1012
|
+
@max_version = max_version
|
1013
|
+
|
1014
|
+
reconnect_ssl
|
1015
|
+
end
|
1016
|
+
|
1017
|
+
##
|
1018
|
+
# Sets the depth of SSL certificate verification
|
1019
|
+
|
1020
|
+
def verify_depth= verify_depth
|
1021
|
+
@verify_depth = verify_depth
|
1022
|
+
|
1023
|
+
reconnect_ssl
|
1024
|
+
end
|
1025
|
+
|
1026
|
+
##
|
1027
|
+
# Sets the HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER.
|
1028
|
+
#
|
1029
|
+
# Setting this to VERIFY_NONE is a VERY BAD IDEA and should NEVER be used.
|
1030
|
+
# Securely transfer the correct certificate and update the default
|
1031
|
+
# certificate store or set the ca file instead.
|
1032
|
+
|
1033
|
+
def verify_mode= verify_mode
|
1034
|
+
@verify_mode = verify_mode
|
1035
|
+
|
1036
|
+
reconnect_ssl
|
1037
|
+
end
|
1038
|
+
|
1039
|
+
##
|
1040
|
+
# SSL verification callback.
|
1041
|
+
|
1042
|
+
def verify_callback= callback
|
1043
|
+
@verify_callback = callback
|
1044
|
+
|
1045
|
+
reconnect_ssl
|
1046
|
+
end
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
require 'net/http/persistent/connection'
|
1050
|
+
require 'net/http/persistent/pool'
|
1051
|
+
|