net-http-persistent 2.9.4 → 3.0.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.

Potentially problematic release.


This version of net-http-persistent might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21b9c2d72e25672250e7d4544bed635d05a583cd
4
- data.tar.gz: 8baa746d3b463cc46282e93e1c95fea48ce34de7
3
+ metadata.gz: 2ac4ced8426e3492194191bc08b33f911d09191a
4
+ data.tar.gz: a6b022f6d8a64047938b72a3e8bed913b435f547
5
5
  SHA512:
6
- metadata.gz: 34d31b4bb74a92791b8091bec2e098935440b522a22a00cf189a7d48652415a4729496758c30af51ce38cd1c227b3414040cb3396b348dcc355634f5fb1d012d
7
- data.tar.gz: 6afc70cd26e21e4d7fbce7d5531543ba55df0da22dd2b4ac5f697b34290f567167afc3a76a98fd7faa351e539f94cf1d2dae0295f0840036d482a6537bd0271f
6
+ metadata.gz: 10729b7555b94f553ee0aed2c51d8ac950f92dc7e669dc39638e6ee76ac64914e8fb54cab8c97cb1087a7f224d8f5ea62d404a81f2d4da00446d1c13c4458533
7
+ data.tar.gz: fd5bf2624f9cb961e1086480c9085a4ae14fdc9ae28797b95dd8493887ec0d87e0014e64bb5c52cf16eae4eef5e8aec3b23c745c8698d841163385ae6b9b447f
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -0,0 +1,15 @@
1
+ ---
2
+ after_script:
3
+ - rake travis:after -t
4
+ before_script:
5
+ - gem install hoe-travis --no-rdoc --no-ri
6
+ - rake travis:before -t
7
+ language: ruby
8
+ notifications:
9
+ email:
10
+ - drbrain@segment7.net
11
+ rvm:
12
+ - 2.1.10
13
+ - 2.2.5
14
+ - 2.3.1
15
+ script: rake travis
@@ -1,3 +1,22 @@
1
+ === 3.0
2
+
3
+ Breaking changes:
4
+
5
+ * No longer supports ruby 2.0 and earlier
6
+ * Net::HTTP::Persistent::new now uses keyword arguments for +name+ and
7
+ +proxy+.
8
+ * Removed #max_age, use #expired?
9
+
10
+ New features:
11
+
12
+ * Uses connection_pool to manage all connections for a Net::HTTP::Persistent
13
+ instance.
14
+
15
+ Bug fixes:
16
+
17
+ * Add missing SSL options ca_path, ciphers, ssl_timeout, verify_depth.
18
+ Issue #63 by Johnneylee Jack Rollins.
19
+
1
20
  === 2.9.4 / 2014-02-10
2
21
 
3
22
  * Bug fixes
@@ -1,11 +1,13 @@
1
1
  .autotest
2
2
  .gemtest
3
+ .travis.yml
3
4
  History.txt
4
5
  Manifest.txt
5
6
  README.rdoc
6
7
  Rakefile
7
- lib/net/http/faster.rb
8
8
  lib/net/http/persistent.rb
9
- lib/net/http/persistent/ssl_reuse.rb
9
+ lib/net/http/persistent/connection.rb
10
+ lib/net/http/persistent/pool.rb
11
+ lib/net/http/persistent/timed_stack_multi.rb
10
12
  test/test_net_http_persistent.rb
11
- test/test_net_http_persistent_ssl_reuse.rb
13
+ test/test_net_http_persistent_timed_stack_multi.rb
data/Rakefile CHANGED
@@ -13,12 +13,15 @@ Hoe.spec 'net-http-persistent' do
13
13
  self.readme_file = 'README.rdoc'
14
14
  self.extra_rdoc_files += Dir['*.rdoc']
15
15
 
16
+ self.require_ruby_version '~> 2.1'
17
+
16
18
  license 'MIT'
17
19
 
18
20
  rdoc_locations <<
19
21
  'docs.seattlerb.org:/data/www/docs.seattlerb.org/net-http-persistent/'
20
22
 
21
- dependency 'minitest', '~> 5.2', :development
23
+ dependency 'connection_pool', '~> 2.2'
24
+ dependency 'minitest', '~> 5.2', :development
22
25
  end
23
26
 
24
27
  # vim: syntax=Ruby
@@ -1,12 +1,7 @@
1
1
  require 'net/http'
2
- begin
3
- require 'net/https'
4
- rescue LoadError
5
- # net/https or openssl
6
- end if RUBY_VERSION < '1.9' # but only for 1.8
7
- require 'net/http/faster'
8
2
  require 'uri'
9
3
  require 'cgi' # for escaping
4
+ require 'connection_pool'
10
5
 
11
6
  begin
12
7
  require 'net/http/pipeline'
@@ -70,13 +65,17 @@ autoload :OpenSSL, 'openssl'
70
65
  # Here are the SSL settings, see the individual methods for documentation:
71
66
  #
72
67
  # #certificate :: This client's certificate
73
- # #ca_file :: The certificate-authority
68
+ # #ca_file :: The certificate-authorities
69
+ # #ca_path :: Directory with certificate-authorities
74
70
  # #cert_store :: An SSL certificate store
71
+ # #ciphers :: List of SSl ciphers allowed
75
72
  # #private_key :: The client's SSL private key
76
73
  # #reuse_ssl_sessions :: Reuse a previously opened SSL session for a new
77
74
  # connection
75
+ # #ssl_timeout :: SSL session lifetime
78
76
  # #ssl_version :: Which specific SSL version to use
79
77
  # #verify_callback :: For server certificate verification
78
+ # #verify_depth :: Depth of certificate verification
80
79
  # #verify_mode :: How connections should be verified
81
80
  #
82
81
  # == Proxies
@@ -200,10 +199,15 @@ class Net::HTTP::Persistent
200
199
 
201
200
  HAVE_OPENSSL = defined? OpenSSL::SSL # :nodoc:
202
201
 
202
+ ##
203
+ # The default connection pool size is 1/4 the allowed open files.
204
+
205
+ DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4
206
+
203
207
  ##
204
208
  # The version of Net::HTTP::Persistent you are using
205
209
 
206
- VERSION = '2.9.4'
210
+ VERSION = '3.0.0'
207
211
 
208
212
  ##
209
213
  # Exceptions rescued for automatic retry on ruby 2.0.0. This overlaps with
@@ -248,31 +252,31 @@ class Net::HTTP::Persistent
248
252
 
249
253
  http = new 'net-http-persistent detect_idle_timeout'
250
254
 
251
- connection = http.connection_for uri
255
+ http.connection_for uri do |connection|
256
+ sleep_time = 0
252
257
 
253
- sleep_time = 0
258
+ http = connection.http
254
259
 
255
- loop do
256
- response = connection.request req
260
+ loop do
261
+ response = http.request req
257
262
 
258
- $stderr.puts "HEAD #{uri} => #{response.code}" if $DEBUG
263
+ $stderr.puts "HEAD #{uri} => #{response.code}" if $DEBUG
259
264
 
260
- unless Net::HTTPOK === response then
261
- raise Error, "bad response code #{response.code} detecting idle timeout"
262
- end
265
+ unless Net::HTTPOK === response then
266
+ raise Error, "bad response code #{response.code} detecting idle timeout"
267
+ end
263
268
 
264
- break if sleep_time >= max
269
+ break if sleep_time >= max
265
270
 
266
- sleep_time += 1
271
+ sleep_time += 1
267
272
 
268
- $stderr.puts "sleeping #{sleep_time}" if $DEBUG
269
- sleep sleep_time
273
+ $stderr.puts "sleeping #{sleep_time}" if $DEBUG
274
+ sleep sleep_time
275
+ end
270
276
  end
271
277
  rescue
272
278
  # ignore StandardErrors, we've probably found the idle timeout.
273
279
  ensure
274
- http.shutdown
275
-
276
280
  return sleep_time unless $!
277
281
  end
278
282
 
@@ -281,7 +285,9 @@ class Net::HTTP::Persistent
281
285
 
282
286
  attr_reader :certificate
283
287
 
288
+ ##
284
289
  # For Net::HTTP parity
290
+
285
291
  alias cert certificate
286
292
 
287
293
  ##
@@ -290,12 +296,23 @@ class Net::HTTP::Persistent
290
296
 
291
297
  attr_reader :ca_file
292
298
 
299
+ ##
300
+ # A directory of SSL certificates to be used as certificate authorities.
301
+ # Setting this will set verify_mode to VERIFY_PEER.
302
+
303
+ attr_reader :ca_path
304
+
293
305
  ##
294
306
  # An SSL certificate store. Setting this will override the default
295
307
  # certificate store. See verify_mode for more information.
296
308
 
297
309
  attr_reader :cert_store
298
310
 
311
+ ##
312
+ # The ciphers allowed for SSL connections
313
+
314
+ attr_reader :ciphers
315
+
299
316
  ##
300
317
  # Sends debug_output to this IO via Net::HTTP#set_debug_output.
301
318
  #
@@ -309,11 +326,6 @@ class Net::HTTP::Persistent
309
326
 
310
327
  attr_reader :generation # :nodoc:
311
328
 
312
- ##
313
- # Where this instance's connections live in the thread local variables
314
-
315
- attr_reader :generation_key # :nodoc:
316
-
317
329
  ##
318
330
  # Headers that are added to every request using Net::HTTP#add_field
319
331
 
@@ -369,7 +381,9 @@ class Net::HTTP::Persistent
369
381
 
370
382
  attr_reader :private_key
371
383
 
384
+ ##
372
385
  # For Net::HTTP parity
386
+
373
387
  alias key private_key
374
388
 
375
389
  ##
@@ -383,14 +397,14 @@ class Net::HTTP::Persistent
383
397
  attr_reader :no_proxy
384
398
 
385
399
  ##
386
- # Seconds to wait until reading one block. See Net::HTTP#read_timeout
400
+ # Test-only accessor for the connection pool
387
401
 
388
- attr_accessor :read_timeout
402
+ attr_reader :pool # :nodoc:
389
403
 
390
404
  ##
391
- # Where this instance's request counts live in the thread local variables
405
+ # Seconds to wait until reading one block. See Net::HTTP#read_timeout
392
406
 
393
- attr_reader :request_key # :nodoc:
407
+ attr_accessor :read_timeout
394
408
 
395
409
  ##
396
410
  # By default SSL sessions are reused to avoid extra SSL handshakes. Set
@@ -418,9 +432,9 @@ class Net::HTTP::Persistent
418
432
  attr_reader :ssl_generation # :nodoc:
419
433
 
420
434
  ##
421
- # Where this instance's SSL connections live in the thread local variables
435
+ # SSL session lifetime
422
436
 
423
- attr_reader :ssl_generation_key # :nodoc:
437
+ attr_reader :ssl_timeout
424
438
 
425
439
  ##
426
440
  # SSL version to use.
@@ -428,7 +442,7 @@ class Net::HTTP::Persistent
428
442
  # By default, the version will be negotiated automatically between client
429
443
  # and server. Ruby 1.9 and newer only.
430
444
 
431
- attr_reader :ssl_version if RUBY_VERSION > '1.9'
445
+ attr_reader :ssl_version
432
446
 
433
447
  ##
434
448
  # Where this instance's last-use times live in the thread local variables
@@ -436,16 +450,21 @@ class Net::HTTP::Persistent
436
450
  attr_reader :timeout_key # :nodoc:
437
451
 
438
452
  ##
439
- # SSL verification callback. Used when ca_file is set.
453
+ # SSL verification callback. Used when ca_file or ca_path is set.
440
454
 
441
455
  attr_reader :verify_callback
442
456
 
457
+ ##
458
+ # Sets the depth of SSL certificate verification
459
+
460
+ attr_reader :verify_depth
461
+
443
462
  ##
444
463
  # HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER which verifies
445
464
  # the server certificate.
446
465
  #
447
- # If no ca_file or cert_store is set the default system certificate store is
448
- # used.
466
+ # If no ca_file, ca_path or cert_store is set the default system certificate
467
+ # store is used.
449
468
  #
450
469
  # You can use +verify_mode+ to override any default values.
451
470
 
@@ -478,8 +497,12 @@ class Net::HTTP::Persistent
478
497
  # proxy = URI 'http://proxy.example'
479
498
  # proxy.user = 'AzureDiamond'
480
499
  # proxy.password = 'hunter2'
500
+ #
501
+ # Set +pool_size+ to limit the maximum number of connections allowed.
502
+ # Defaults to 1/4 the number of allowed file handles. You can have no more
503
+ # than this many threads with active HTTP transactions.
481
504
 
482
- def initialize name = nil, proxy = nil
505
+ def initialize name: nil, proxy: nil, pool_size: DEFAULT_POOL_SIZE
483
506
  @name = name
484
507
 
485
508
  @debug_output = nil
@@ -494,26 +517,28 @@ class Net::HTTP::Persistent
494
517
  @idle_timeout = 5
495
518
  @max_requests = nil
496
519
  @socket_options = []
520
+ @ssl_generation = 0 # incremented when SSL session variables change
497
521
 
498
522
  @socket_options << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if
499
523
  Socket.const_defined? :TCP_NODELAY
500
524
 
501
- key = ['net_http_persistent', name].compact
502
- @generation_key = [key, 'generations' ].join('_').intern
503
- @ssl_generation_key = [key, 'ssl_generations'].join('_').intern
504
- @request_key = [key, 'requests' ].join('_').intern
505
- @timeout_key = [key, 'timeouts' ].join('_').intern
525
+ @pool = Net::HTTP::Persistent::Pool.new size: pool_size do |http_args|
526
+ Net::HTTP::Persistent::Connection.new Net::HTTP, http_args, @ssl_generation
527
+ end
506
528
 
507
529
  @certificate = nil
508
530
  @ca_file = nil
531
+ @ca_path = nil
532
+ @ciphers = nil
509
533
  @private_key = nil
534
+ @ssl_timeout = nil
510
535
  @ssl_version = nil
511
536
  @verify_callback = nil
537
+ @verify_depth = nil
512
538
  @verify_mode = nil
513
539
  @cert_store = nil
514
540
 
515
541
  @generation = 0 # incremented when proxy URI changes
516
- @ssl_generation = 0 # incremented when SSL session variables change
517
542
 
518
543
  if HAVE_OPENSSL then
519
544
  @verify_mode = OpenSSL::SSL::VERIFY_PEER
@@ -522,9 +547,6 @@ class Net::HTTP::Persistent
522
547
 
523
548
  @retry_change_requests = false
524
549
 
525
- @ruby_1 = RUBY_VERSION < '2'
526
- @retried_on_ruby_2 = !@ruby_1
527
-
528
550
  self.proxy = proxy if proxy
529
551
  end
530
552
 
@@ -549,6 +571,15 @@ class Net::HTTP::Persistent
549
571
  reconnect_ssl
550
572
  end
551
573
 
574
+ ##
575
+ # Sets the SSL certificate authority path.
576
+
577
+ def ca_path= path
578
+ @ca_path = path
579
+
580
+ reconnect_ssl
581
+ end
582
+
552
583
  ##
553
584
  # Overrides the default SSL certificate store used for verifying
554
585
  # connections.
@@ -560,90 +591,55 @@ class Net::HTTP::Persistent
560
591
  end
561
592
 
562
593
  ##
563
- # Finishes all connections on the given +thread+ that were created before
564
- # the given +generation+ in the threads +generation_key+ list.
565
- #
566
- # See #shutdown for a bunch of scary warning about misusing this method.
567
-
568
- def cleanup(generation, thread = Thread.current,
569
- generation_key = @generation_key) # :nodoc:
570
- timeouts = thread[@timeout_key]
594
+ # The ciphers allowed for SSL connections
571
595
 
572
- (0...generation).each do |old_generation|
573
- next unless thread[generation_key]
596
+ def ciphers= ciphers
597
+ @ciphers = ciphers
574
598
 
575
- conns = thread[generation_key].delete old_generation
576
-
577
- conns.each_value do |conn|
578
- finish conn, thread
579
-
580
- timeouts.delete conn.object_id if timeouts
581
- end if conns
582
- end
599
+ reconnect_ssl
583
600
  end
584
601
 
585
602
  ##
586
603
  # Creates a new connection for +uri+
587
604
 
588
605
  def connection_for uri
589
- Thread.current[@generation_key] ||= Hash.new { |h,k| h[k] = {} }
590
- Thread.current[@ssl_generation_key] ||= Hash.new { |h,k| h[k] = {} }
591
- Thread.current[@request_key] ||= Hash.new 0
592
- Thread.current[@timeout_key] ||= Hash.new EPOCH
593
-
594
606
  use_ssl = uri.scheme.downcase == 'https'
595
607
 
596
- if use_ssl then
597
- raise Net::HTTP::Persistent::Error, 'OpenSSL is not available' unless
598
- HAVE_OPENSSL
599
-
600
- ssl_generation = @ssl_generation
601
-
602
- ssl_cleanup ssl_generation
608
+ net_http_args = [uri.host, uri.port]
603
609
 
604
- connections = Thread.current[@ssl_generation_key][ssl_generation]
605
- else
606
- generation = @generation
610
+ net_http_args.concat @proxy_args if
611
+ @proxy_uri and not proxy_bypass? uri.host, uri.port
607
612
 
608
- cleanup generation
613
+ connection = @pool.checkout net_http_args
609
614
 
610
- connections = Thread.current[@generation_key][generation]
611
- end
615
+ http = connection.http
612
616
 
613
- net_http_args = [uri.host, uri.port]
614
- connection_id = net_http_args.join ':'
617
+ connection.ressl @ssl_generation if
618
+ connection.ssl_generation != @ssl_generation
615
619
 
616
- if @proxy_uri and not proxy_bypass? uri.host, uri.port then
617
- connection_id << @proxy_connection_id
618
- net_http_args.concat @proxy_args
619
- end
620
-
621
- connection = connections[connection_id]
622
-
623
- unless connection = connections[connection_id] then
624
- connections[connection_id] = http_class.new(*net_http_args)
625
- connection = connections[connection_id]
626
- ssl connection if use_ssl
627
- else
628
- reset connection if expired? connection
620
+ if not http.started? then
621
+ ssl http if use_ssl
622
+ start http
623
+ elsif expired? connection then
624
+ reset connection
629
625
  end
630
626
 
631
- start connection unless connection.started?
632
-
633
- connection.read_timeout = @read_timeout if @read_timeout
634
- connection.keep_alive_timeout = @idle_timeout if @idle_timeout && connection.respond_to?(:keep_alive_timeout=)
627
+ http.read_timeout = @read_timeout if @read_timeout
628
+ http.keep_alive_timeout = @idle_timeout if @idle_timeout
635
629
 
636
- connection
630
+ return yield connection
637
631
  rescue Errno::ECONNREFUSED
638
- address = connection.proxy_address || connection.address
639
- port = connection.proxy_port || connection.port
632
+ address = http.proxy_address || http.address
633
+ port = http.proxy_port || http.port
640
634
 
641
635
  raise Error, "connection refused: #{address}:#{port}"
642
636
  rescue Errno::EHOSTDOWN
643
- address = connection.proxy_address || connection.address
644
- port = connection.proxy_port || connection.port
637
+ address = http.proxy_address || http.address
638
+ port = http.proxy_port || http.port
645
639
 
646
640
  raise Error, "host down: #{address}:#{port}"
641
+ ensure
642
+ @pool.checkin net_http_args
647
643
  end
648
644
 
649
645
  ##
@@ -651,12 +647,11 @@ class Net::HTTP::Persistent
651
647
  # this connection
652
648
 
653
649
  def error_message connection
654
- requests = Thread.current[@request_key][connection.object_id] - 1 # fixup
655
- last_use = Thread.current[@timeout_key][connection.object_id]
650
+ connection.requests -= 1 # fixup
656
651
 
657
- age = Time.now - last_use
652
+ age = Time.now - connection.last_use
658
653
 
659
- "after #{requests} requests on #{connection.object_id}, " \
654
+ "after #{connection.requests} requests on #{connection.http.object_id}, " \
660
655
  "last used #{age} seconds ago"
661
656
  end
662
657
 
@@ -680,26 +675,23 @@ class Net::HTTP::Persistent
680
675
  # maximum request count, false otherwise.
681
676
 
682
677
  def expired? connection
683
- requests = Thread.current[@request_key][connection.object_id]
684
- return true if @max_requests && requests >= @max_requests
678
+ return true if @max_requests && connection.requests >= @max_requests
685
679
  return false unless @idle_timeout
686
680
  return true if @idle_timeout.zero?
687
681
 
688
- last_used = Thread.current[@timeout_key][connection.object_id]
689
-
690
- Time.now - last_used > @idle_timeout
682
+ Time.now - connection.last_use > @idle_timeout
691
683
  end
692
684
 
693
685
  ##
694
686
  # Starts the Net::HTTP +connection+
695
687
 
696
- def start connection
697
- connection.set_debug_output @debug_output if @debug_output
698
- connection.open_timeout = @open_timeout if @open_timeout
688
+ def start http
689
+ http.set_debug_output @debug_output if @debug_output
690
+ http.open_timeout = @open_timeout if @open_timeout
699
691
 
700
- connection.start
692
+ http.start
701
693
 
702
- socket = connection.instance_variable_get :@socket
694
+ socket = http.instance_variable_get :@socket
703
695
 
704
696
  if socket then # for fakeweb
705
697
  @socket_options.each do |option|
@@ -711,25 +703,11 @@ class Net::HTTP::Persistent
711
703
  ##
712
704
  # Finishes the Net::HTTP +connection+
713
705
 
714
- def finish connection, thread = Thread.current
715
- if requests = thread[@request_key] then
716
- requests.delete connection.object_id
717
- end
718
-
706
+ def finish connection
719
707
  connection.finish
720
- rescue IOError
721
- end
722
708
 
723
- def http_class # :nodoc:
724
- if RUBY_VERSION > '2.0' then
725
- Net::HTTP
726
- elsif [:Artifice, :FakeWeb, :WebMock].any? { |klass|
727
- Object.const_defined?(klass)
728
- } or not @reuse_ssl_sessions then
729
- Net::HTTP
730
- else
731
- Net::HTTP::Persistent::SSLReuse
732
- end
709
+ connection.http.instance_variable_set :@ssl_session, nil unless
710
+ @reuse_ssl_sessions
733
711
  end
734
712
 
735
713
  ##
@@ -752,55 +730,9 @@ class Net::HTTP::Persistent
752
730
 
753
731
  ##
754
732
  # Is the request +req+ idempotent or is retry_change_requests allowed.
755
- #
756
- # If +retried_on_ruby_2+ is true, true will be returned if we are on ruby,
757
- # retry_change_requests is allowed and the request is not idempotent.
758
-
759
- def can_retry? req, retried_on_ruby_2 = false
760
- return @retry_change_requests && !idempotent?(req) if retried_on_ruby_2
761
733
 
762
- @retry_change_requests || idempotent?(req)
763
- end
764
-
765
- if RUBY_VERSION > '1.9' then
766
- ##
767
- # Workaround for missing Net::HTTPHeader#connection_close? on Ruby 1.8
768
-
769
- def connection_close? header
770
- header.connection_close?
771
- end
772
-
773
- ##
774
- # Workaround for missing Net::HTTPHeader#connection_keep_alive? on Ruby 1.8
775
-
776
- def connection_keep_alive? header
777
- header.connection_keep_alive?
778
- end
779
- else
780
- ##
781
- # Workaround for missing Net::HTTPRequest#connection_close? on Ruby 1.8
782
-
783
- def connection_close? header
784
- header['connection'] =~ /close/ or header['proxy-connection'] =~ /close/
785
- end
786
-
787
- ##
788
- # Workaround for missing Net::HTTPRequest#connection_keep_alive? on Ruby
789
- # 1.8
790
-
791
- def connection_keep_alive? header
792
- header['connection'] =~ /keep-alive/ or
793
- header['proxy-connection'] =~ /keep-alive/
794
- end
795
- end
796
-
797
- ##
798
- # Deprecated in favor of #expired?
799
-
800
- def max_age # :nodoc:
801
- return Time.now + 1 unless @idle_timeout
802
-
803
- Time.now - @idle_timeout
734
+ def can_retry? req
735
+ @retry_change_requests && !idempotent?(req)
804
736
  end
805
737
 
806
738
  ##
@@ -822,9 +754,9 @@ class Net::HTTP::Persistent
822
754
  # <tt>net-http-persistent</tt> #pipeline will be present.
823
755
 
824
756
  def pipeline uri, requests, &block # :yields: responses
825
- connection = connection_for uri
826
-
827
- connection.pipeline requests, &block
757
+ connection_for uri do |connection|
758
+ connection.http.pipeline requests, &block
759
+ end
828
760
  end
829
761
 
830
762
  ##
@@ -957,18 +889,17 @@ class Net::HTTP::Persistent
957
889
  # Finishes then restarts the Net::HTTP +connection+
958
890
 
959
891
  def reset connection
960
- Thread.current[@request_key].delete connection.object_id
961
- Thread.current[@timeout_key].delete connection.object_id
892
+ http = connection.http
962
893
 
963
894
  finish connection
964
895
 
965
- start connection
896
+ start http
966
897
  rescue Errno::ECONNREFUSED
967
- e = Error.new "connection refused: #{connection.address}:#{connection.port}"
898
+ e = Error.new "connection refused: #{http.address}:#{http.port}"
968
899
  e.set_backtrace $@
969
900
  raise e
970
901
  rescue Errno::EHOSTDOWN
971
- e = Error.new "host down: #{connection.address}:#{connection.port}"
902
+ e = Error.new "host down: #{http.address}:#{http.port}"
972
903
  e.set_backtrace $@
973
904
  raise e
974
905
  end
@@ -989,52 +920,56 @@ class Net::HTTP::Persistent
989
920
  retried = false
990
921
  bad_response = false
991
922
 
992
- req = request_setup req || uri
923
+ uri = URI uri
924
+ req = request_setup req || uri
925
+ response = nil
993
926
 
994
- connection = connection_for uri
995
- connection_id = connection.object_id
927
+ connection_for uri do |connection|
928
+ http = connection.http
996
929
 
997
- begin
998
- Thread.current[@request_key][connection_id] += 1
999
- response = connection.request req, &block
930
+ begin
931
+ connection.requests += 1
1000
932
 
1001
- if connection_close?(req) or
1002
- (response.http_version <= '1.0' and
1003
- not connection_keep_alive?(response)) or
1004
- connection_close?(response) then
1005
- connection.finish
1006
- end
1007
- rescue Net::HTTPBadResponse => e
1008
- message = error_message connection
933
+ response = http.request req, &block
1009
934
 
1010
- finish connection
935
+ if req.connection_close? or
936
+ (response.http_version <= '1.0' and
937
+ not response.connection_keep_alive?) or
938
+ response.connection_close? then
939
+ finish connection
940
+ end
941
+ rescue Net::HTTPBadResponse => e
942
+ message = error_message connection
1011
943
 
1012
- raise Error, "too many bad responses #{message}" if
944
+ finish connection
945
+
946
+ raise Error, "too many bad responses #{message}" if
1013
947
  bad_response or not can_retry? req
1014
948
 
1015
- bad_response = true
1016
- retry
1017
- rescue *RETRIED_EXCEPTIONS => e # retried on ruby 2
1018
- request_failed e, req, connection if
1019
- retried or not can_retry? req, @retried_on_ruby_2
949
+ bad_response = true
950
+ retry
951
+ rescue *RETRIED_EXCEPTIONS => e
952
+ request_failed e, req, connection if
953
+ retried or not can_retry? req
1020
954
 
1021
- reset connection
955
+ reset connection
1022
956
 
1023
- retried = true
1024
- retry
1025
- rescue Errno::EINVAL, Errno::ETIMEDOUT => e # not retried on ruby 2
1026
- request_failed e, req, connection if retried or not can_retry? req
957
+ retried = true
958
+ retry
959
+ rescue Errno::EINVAL, Errno::ETIMEDOUT => e # not retried on ruby 2
960
+ request_failed e, req, connection if retried or not can_retry? req
1027
961
 
1028
- reset connection
962
+ reset connection
1029
963
 
1030
- retried = true
1031
- retry
1032
- rescue Exception => e
1033
- finish connection
964
+ retried = true
965
+ retry
966
+ rescue Exception => e
967
+ finish connection
1034
968
 
1035
- raise
1036
- ensure
1037
- Thread.current[@timeout_key][connection_id] = Time.now
969
+ raise
970
+ ensure
971
+ connection.last_use = Time.now
972
+ end
1038
973
  end
1039
974
 
1040
975
  @http_versions["#{uri.host}:#{uri.port}"] ||= response.http_version
@@ -1054,7 +989,6 @@ class Net::HTTP::Persistent
1054
989
 
1055
990
  finish connection
1056
991
 
1057
-
1058
992
  raise Error, message, exception.backtrace
1059
993
  end
1060
994
 
@@ -1088,45 +1022,17 @@ class Net::HTTP::Persistent
1088
1022
  end
1089
1023
 
1090
1024
  ##
1091
- # Shuts down all connections for +thread+.
1092
- #
1093
- # Uses the current thread by default.
1094
- #
1095
- # If you've used Net::HTTP::Persistent across multiple threads you should
1096
- # call this in each thread when you're done making HTTP requests.
1025
+ # Shuts down all connections
1097
1026
  #
1098
- # *NOTE*: Calling shutdown for another thread can be dangerous!
1027
+ # *NOTE*: Calling shutdown for can be dangerous!
1099
1028
  #
1100
- # If the thread is still using the connection it may cause an error! It is
1101
- # best to call #shutdown in the thread at the appropriate time instead!
1029
+ # If any thread is still using a connection it may cause an error! Call
1030
+ # #shutdown when you are completely done making requests!
1102
1031
 
1103
- def shutdown thread = Thread.current
1104
- generation = reconnect
1105
- cleanup generation, thread, @generation_key
1106
-
1107
- ssl_generation = reconnect_ssl
1108
- cleanup ssl_generation, thread, @ssl_generation_key
1109
-
1110
- thread[@request_key] = nil
1111
- thread[@timeout_key] = nil
1112
- end
1113
-
1114
- ##
1115
- # Shuts down all connections in all threads
1116
- #
1117
- # *NOTE*: THIS METHOD IS VERY DANGEROUS!
1118
- #
1119
- # Do not call this method if other threads are still using their
1120
- # connections! Call #shutdown at the appropriate time instead!
1121
- #
1122
- # Use this method only as a last resort!
1123
-
1124
- def shutdown_in_all_threads
1125
- Thread.list.each do |thread|
1126
- shutdown thread
1032
+ def shutdown
1033
+ @pool.available.shutdown do |http|
1034
+ http.finish
1127
1035
  end
1128
-
1129
- nil
1130
1036
  end
1131
1037
 
1132
1038
  ##
@@ -1135,9 +1041,12 @@ class Net::HTTP::Persistent
1135
1041
  def ssl connection
1136
1042
  connection.use_ssl = true
1137
1043
 
1044
+ connection.ciphers = @ciphers if @ciphers
1045
+ connection.ssl_timeout = @ssl_timeout if @ssl_timeout
1138
1046
  connection.ssl_version = @ssl_version if @ssl_version
1139
1047
 
1140
- connection.verify_mode = @verify_mode
1048
+ connection.verify_depth = @verify_depth
1049
+ connection.verify_mode = @verify_mode
1141
1050
 
1142
1051
  if OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE and
1143
1052
  not Object.const_defined?(:I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG) then
@@ -1166,8 +1075,10 @@ application:
1166
1075
  WARNING
1167
1076
  end
1168
1077
 
1169
- if @ca_file then
1170
- connection.ca_file = @ca_file
1078
+ connection.ca_file = @ca_file if @ca_file
1079
+ connection.ca_path = @ca_path if @ca_path
1080
+
1081
+ if @ca_file or @ca_path then
1171
1082
  connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
1172
1083
  connection.verify_callback = @verify_callback if @verify_callback
1173
1084
  end
@@ -1187,11 +1098,12 @@ application:
1187
1098
  end
1188
1099
 
1189
1100
  ##
1190
- # Finishes all connections that existed before the given SSL parameter
1191
- # +generation+.
1101
+ # SSL session lifetime
1192
1102
 
1193
- def ssl_cleanup generation # :nodoc:
1194
- cleanup generation, Thread.current, @ssl_generation_key
1103
+ def ssl_timeout= ssl_timeout
1104
+ @ssl_timeout = ssl_timeout
1105
+
1106
+ reconnect_ssl
1195
1107
  end
1196
1108
 
1197
1109
  ##
@@ -1201,7 +1113,16 @@ application:
1201
1113
  @ssl_version = ssl_version
1202
1114
 
1203
1115
  reconnect_ssl
1204
- end if RUBY_VERSION > '1.9'
1116
+ end
1117
+
1118
+ ##
1119
+ # Sets the depth of SSL certificate verification
1120
+
1121
+ def verify_depth= verify_depth
1122
+ @verify_depth = verify_depth
1123
+
1124
+ reconnect_ssl
1125
+ end
1205
1126
 
1206
1127
  ##
1207
1128
  # Sets the HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER.
@@ -1227,5 +1148,6 @@ application:
1227
1148
 
1228
1149
  end
1229
1150
 
1230
- require 'net/http/persistent/ssl_reuse'
1151
+ require 'net/http/persistent/connection'
1152
+ require 'net/http/persistent/pool'
1231
1153