net-http-persistent-3.1.0/0000755000004100000410000000000013516510437015511 5ustar www-datawww-datanet-http-persistent-3.1.0/.travis.yml0000644000004100000410000000055313516510437017625 0ustar www-datawww-data--- after_script: - rake travis:after -t before_script: - gem install hoe-travis --no-document - rake travis:before -t language: ruby notifications: email: - drbrain@segment7.net rvm: - 2.1 - 2.2 - 2.3 - 2.4 - 2.5 - 2.6 script: rake travis install: "" # avoid running default bundler install matrix: include: - rvm: "2.6" env: TRAVIS_MATRIX=pipeline net-http-persistent-3.1.0/test/0000755000004100000410000000000013516510437016470 5ustar www-datawww-datanet-http-persistent-3.1.0/test/test_net_http_persistent.rb0000644000004100000410000011233413516510437024165 0ustar www-datawww-datarequire 'rubygems' require 'minitest/autorun' require 'net/http/persistent' require 'stringio' HAVE_OPENSSL = defined?(OpenSSL::SSL) module Net::HTTP::Persistent::TestConnect def self.included mod mod.send :alias_method, :orig_connect, :connect def mod.use_connect which self.send :remove_method, :connect self.send :alias_method, :connect, which end end def host_down_connect raise Errno::EHOSTDOWN end def test_connect unless use_ssl? then io = Object.new def io.setsockopt(*a) @setsockopts ||= []; @setsockopts << a end @socket = Net::BufferedIO.new io return end io = open '/dev/null' def io.setsockopt(*a) @setsockopts ||= []; @setsockopts << a end @ssl_context ||= OpenSSL::SSL::SSLContext.new @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER unless @ssl_context.verify_mode s = OpenSSL::SSL::SSLSocket.new io, @ssl_context @socket = Net::BufferedIO.new s end def refused_connect raise Errno::ECONNREFUSED end end class Net::HTTP include Net::HTTP::Persistent::TestConnect end class TestNetHttpPersistent < Minitest::Test def setup @http = Net::HTTP::Persistent.new @uri = URI.parse 'http://example.com/path' ENV.delete 'http_proxy' ENV.delete 'HTTP_PROXY' ENV.delete 'http_proxy_user' ENV.delete 'HTTP_PROXY_USER' ENV.delete 'http_proxy_pass' ENV.delete 'HTTP_PROXY_PASS' ENV.delete 'no_proxy' ENV.delete 'NO_PROXY' Net::HTTP.use_connect :test_connect end def teardown Net::HTTP.use_connect :orig_connect end class BasicConnection attr_accessor :started, :finished, :address, :port, :use_ssl, :read_timeout, :open_timeout, :keep_alive_timeout attr_accessor :ciphers, :ssl_timeout, :ssl_version, :min_version, :max_version, :verify_depth, :verify_mode, :cert_store, :ca_file, :ca_path, :cert, :key attr_reader :req, :debug_output def initialize @started, @finished = 0, 0 @address, @port = 'example.com', 80 @use_ssl = false end def finish @finished += 1 @socket = nil end def finished? @finished >= 1 end def pipeline requests, &block requests.map { |r| r.path } end def reset? @started == @finished + 1 end def set_debug_output io @debug_output = io end def start @started += 1 io = Object.new def io.setsockopt(*a) @setsockopts ||= []; @setsockopts << a end @socket = Net::BufferedIO.new io end def started? @started >= 1 end def proxy_address end def proxy_port end end def basic_connection raise "#{@uri} is not HTTP" unless @uri.scheme.downcase == 'http' net_http_args = [@uri.host, @uri.port, nil, nil, nil, nil] connection = Net::HTTP::Persistent::Connection.allocate connection.ssl_generation = @http.ssl_generation connection.http = BasicConnection.new connection.reset @http.pool.available.push connection, connection_args: net_http_args connection end def connection connection = basic_connection connection.last_use = Time.now def (connection.http).request(req) @req = req r = Net::HTTPResponse.allocate r.instance_variable_set :@header, {} def r.http_version() '1.1' end def r.read_body() :read_body end yield r if block_given? r end connection end def ssl_connection raise "#{@uri} is not HTTPS" unless @uri.scheme.downcase == 'https' net_http_args = [@uri.host, @uri.port, nil, nil, nil, nil] connection = Net::HTTP::Persistent::Connection.allocate connection.ssl_generation = @http.ssl_generation connection.http = BasicConnection.new connection.reset @http.pool.available.push connection, connection_args: net_http_args connection end def test_initialize assert_nil @http.proxy_uri assert_empty @http.no_proxy skip 'OpenSSL is missing' unless HAVE_OPENSSL ssl_session_exists = OpenSSL::SSL.const_defined? :Session assert_equal ssl_session_exists, @http.reuse_ssl_sessions end def test_initialize_name http = Net::HTTP::Persistent.new name: 'name' assert_equal 'name', http.name end def test_initialize_no_ssl_session skip 'OpenSSL is missing' unless HAVE_OPENSSL skip "OpenSSL::SSL::Session does not exist on #{RUBY_PLATFORM}" unless OpenSSL::SSL.const_defined? :Session ssl_session = OpenSSL::SSL::Session OpenSSL::SSL.send :remove_const, :Session http = Net::HTTP::Persistent.new refute http.reuse_ssl_sessions ensure OpenSSL::SSL.const_set :Session, ssl_session if ssl_session end def test_initialize_proxy proxy_uri = URI.parse 'http://proxy.example' http = Net::HTTP::Persistent.new proxy: proxy_uri assert_equal proxy_uri, http.proxy_uri end def test_ca_file_equals @http.ca_file = :ca_file assert_equal :ca_file, @http.ca_file assert_equal 1, @http.ssl_generation end def test_ca_path_equals @http.ca_path = :ca_path assert_equal :ca_path, @http.ca_path assert_equal 1, @http.ssl_generation end def test_can_retry_eh_change_requests post = Net::HTTP::Post.new '/' refute @http.can_retry? post @http.retry_change_requests = true assert @http.can_retry? post end def test_can_retry_eh_idempotent head = Net::HTTP::Head.new '/' refute @http.can_retry? head post = Net::HTTP::Post.new '/' refute @http.can_retry? post end def test_cert_store_equals @http.cert_store = :cert_store assert_equal :cert_store, @http.cert_store assert_equal 1, @http.ssl_generation end def test_certificate_equals @http.certificate = :cert assert_equal :cert, @http.certificate assert_equal 1, @http.ssl_generation end def test_ciphers_equals @http.ciphers = :ciphers assert_equal :ciphers, @http.ciphers assert_equal 1, @http.ssl_generation end def test_connection_for @http.open_timeout = 123 @http.read_timeout = 321 @http.idle_timeout = 42 used = @http.connection_for @uri do |c| assert_kind_of Net::HTTP, c.http assert c.http.started? refute c.http.proxy? assert_equal 123, c.http.open_timeout assert_equal 321, c.http.read_timeout assert_equal 42, c.http.keep_alive_timeout c end stored = @http.pool.checkout ['example.com', 80, nil, nil, nil, nil] assert_same used, stored end def test_connection_for_cached cached = basic_connection cached.http.start @http.read_timeout = 5 @http.connection_for @uri do |c| assert c.http.started? assert_equal 5, c.http.read_timeout assert_same cached, c end end def test_connection_for_closed cached = basic_connection cached.http.start if Socket.const_defined? :TCP_NODELAY then io = Object.new def io.setsockopt(*a) raise IOError, 'closed stream' end cached.instance_variable_set :@socket, Net::BufferedIO.new(io) end @http.connection_for @uri do |c| assert c.http.started? socket = c.http.instance_variable_get :@socket refute_includes socket.io.instance_variables, :@setsockopt refute_includes socket.io.instance_variables, '@setsockopt' end end def test_connection_for_debug_output io = StringIO.new @http.debug_output = io @http.connection_for @uri do |c| assert c.http.started? assert_equal io, c.http.instance_variable_get(:@debug_output) end end def test_connection_for_cached_expire_always cached = basic_connection cached.http.start cached.requests = 10 cached.last_use = Time.now # last used right now @http.idle_timeout = 0 @http.connection_for @uri do |c| assert c.http.started? assert_same cached, c assert_equal 0, c.requests, 'connection reset due to timeout' end end def test_connection_for_cached_expire_never cached = basic_connection cached.http.start cached.requests = 10 cached.last_use = Time.now # last used right now @http.idle_timeout = nil @http.connection_for @uri do |c| assert c.http.started? assert_same cached, c assert_equal 10, c.requests, 'connection reset despite no timeout' end end def test_connection_for_cached_expired cached = basic_connection cached.http.start cached.requests = 10 cached.last_use = Time.now - 3600 @http.connection_for @uri do |c| assert c.http.started? assert_same cached, c assert_equal 0, cached.requests, 'connection not reset due to timeout' end end def test_connection_for_finished_ssl skip 'OpenSSL is missing' unless HAVE_OPENSSL uri = URI.parse 'https://example.com/path' @http.connection_for uri do |c| assert c.http.started? assert c.http.use_ssl? @http.finish c refute c.http.started? end @http.connection_for uri do |c2| assert c2.http.started? end end def test_connection_for_ipv6 uri = URI.parse 'https://[::1]/' @http.connection_for uri do |c| assert_equal '::1', c.http.address end end def test_connection_for_host_down c = basic_connection def (c.http).start; raise Errno::EHOSTDOWN end def (c.http).started?; false end e = assert_raises Net::HTTP::Persistent::Error do @http.connection_for @uri do end end assert_equal 'host down: example.com:80', e.message end def test_connection_for_http_class_with_fakeweb Object.send :const_set, :FakeWeb, nil @http.connection_for @uri do |c| assert_instance_of Net::HTTP, c.http end ensure if Object.const_defined?(:FakeWeb) then Object.send :remove_const, :FakeWeb end end def test_connection_for_http_class_with_webmock Object.send :const_set, :WebMock, nil @http.connection_for @uri do |c| assert_instance_of Net::HTTP, c.http end ensure if Object.const_defined?(:WebMock) then Object.send :remove_const, :WebMock end end def test_connection_for_http_class_with_artifice Object.send :const_set, :Artifice, nil @http.connection_for @uri do |c| assert_instance_of Net::HTTP, c.http end ensure if Object.const_defined?(:Artifice) then Object.send :remove_const, :Artifice end end def test_connection_for_name http = Net::HTTP::Persistent.new name: 'name' uri = URI.parse 'http://example/' http.connection_for uri do |c| assert c.http.started? end end def test_connection_for_proxy uri = URI.parse 'http://proxy.example' uri.user = 'johndoe' uri.password = 'muffins' http = Net::HTTP::Persistent.new proxy: uri used = http.connection_for @uri do |c| assert c.http.started? assert c.http.proxy? c end stored = http.pool.checkout ['example.com', 80, 'proxy.example', 80, 'johndoe', 'muffins'] assert_same used, stored end def test_connection_for_proxy_unescaped uri = URI.parse 'http://proxy.example' uri.user = 'john%40doe' uri.password = 'muf%3Afins' uri.freeze http = Net::HTTP::Persistent.new proxy: uri http.connection_for @uri do end stored = http.pool.checkout ['example.com', 80, 'proxy.example', 80, 'john@doe', 'muf:fins'] assert stored end def test_connection_for_proxy_host_down Net::HTTP.use_connect :host_down_connect uri = URI.parse 'http://proxy.example' uri.user = 'johndoe' uri.password = 'muffins' http = Net::HTTP::Persistent.new proxy: uri e = assert_raises Net::HTTP::Persistent::Error do http.connection_for @uri do end end assert_equal 'host down: proxy.example:80', e.message end def test_connection_for_proxy_refused Net::HTTP.use_connect :refused_connect uri = URI.parse 'http://proxy.example' uri.user = 'johndoe' uri.password = 'muffins' http = Net::HTTP::Persistent.new proxy: uri e = assert_raises Net::HTTP::Persistent::Error do http.connection_for @uri do end end assert_equal 'connection refused: proxy.example:80', e.message end def test_connection_for_no_proxy uri = URI.parse 'http://proxy.example' uri.user = 'johndoe' uri.password = 'muffins' uri.query = 'no_proxy=example.com' http = Net::HTTP::Persistent.new proxy: uri http.connection_for @uri do |c| assert c.http.started? refute c.http.proxy? end stored = http.pool.checkout ['example.com', 80] assert stored end def test_connection_for_no_proxy_from_env ENV['http_proxy'] = 'proxy.example' ENV['no_proxy'] = 'localhost, example.com,' ENV['proxy_user'] = 'johndoe' ENV['proxy_password'] = 'muffins' http = Net::HTTP::Persistent.new proxy: :ENV http.connection_for @uri do |c| assert c.http.started? refute c.http.proxy? refute c.http.proxy_from_env? end end def test_connection_for_refused Net::HTTP.use_connect :refused_connect e = assert_raises Net::HTTP::Persistent::Error do @http.connection_for @uri do end end assert_equal 'connection refused: example.com:80', e.message end def test_connection_for_ssl skip 'OpenSSL is missing' unless HAVE_OPENSSL uri = URI.parse 'https://example.com/path' @http.connection_for uri do |c| assert c.http.started? assert c.http.use_ssl? end end def test_connection_for_ssl_cached skip 'OpenSSL is missing' unless HAVE_OPENSSL @uri = URI.parse 'https://example.com/path' cached = ssl_connection @http.connection_for @uri do |c| assert_same cached, c end end def test_connection_for_ssl_cached_reconnect skip 'OpenSSL is missing' unless HAVE_OPENSSL @uri = URI.parse 'https://example.com/path' cached = ssl_connection ssl_generation = @http.ssl_generation @http.reconnect_ssl @http.connection_for @uri do |c| assert_same cached, c refute_equal ssl_generation, c.ssl_generation end end def test_connection_for_ssl_case skip 'OpenSSL is missing' unless HAVE_OPENSSL uri = URI.parse 'HTTPS://example.com/path' @http.connection_for uri do |c| assert c.http.started? assert c.http.use_ssl? end end def test_connection_for_timeout cached = basic_connection cached.http.start cached.requests = 10 cached.last_use = Time.now - 6 @http.connection_for @uri do |c| assert c.http.started? assert_equal 0, c.requests assert_same cached, c end end def test_error_message c = basic_connection c.last_use = Time.now - 1 c.requests = 5 message = @http.error_message c assert_match %r%after 4 requests on #{c.http.object_id}%, message assert_match %r%, last used [\d.]+ seconds ago%, message end def test_escape assert_nil @http.escape nil assert_equal '+%3F', @http.escape(' ?') end def test_unescape assert_nil @http.unescape nil assert_equal ' ?', @http.unescape('+%3F') end def test_expired_eh c = basic_connection c.requests = 0 c.last_use = Time.now - 11 @http.idle_timeout = 0 assert @http.expired? c @http.idle_timeout = 10 assert @http.expired? c @http.idle_timeout = 11 assert @http.expired? c @http.idle_timeout = 12 refute @http.expired? c @http.idle_timeout = nil refute @http.expired? c end def test_expired_due_to_max_requests c = basic_connection c.requests = 0 c.last_use = Time.now refute @http.expired? c c.requests = 10 refute @http.expired? c @http.max_requests = 10 assert @http.expired? c c.requests = 9 refute @http.expired? c end def test_finish c = basic_connection c.requests = 5 @http.finish c refute c.http.started? assert c.http.finished? assert_equal 0, c.requests assert_equal Net::HTTP::Persistent::EPOCH, c.last_use end def test_finish_io_error c = basic_connection def (c.http).finish; @finished += 1; raise IOError end c.requests = 5 @http.finish c refute c.http.started? assert c.http.finished? end def test_finish_ssl_no_session_reuse http = Net::HTTP.new 'localhost', 443, ssl: true http.instance_variable_set :@ssl_session, :something c = Net::HTTP::Persistent::Connection.allocate c.instance_variable_set :@http, http @http.reuse_ssl_sessions = false @http.finish c assert_nil c.http.instance_variable_get :@ssl_session end def test_http_version assert_nil @http.http_version @uri connection @http.request @uri assert_equal '1.1', @http.http_version(@uri) end def test_idempotent_eh assert @http.idempotent? Net::HTTP::Delete.new '/' assert @http.idempotent? Net::HTTP::Get.new '/' assert @http.idempotent? Net::HTTP::Head.new '/' assert @http.idempotent? Net::HTTP::Options.new '/' assert @http.idempotent? Net::HTTP::Put.new '/' assert @http.idempotent? Net::HTTP::Trace.new '/' assert @http.idempotent? Net::HTTPGenericRequest.new('DELETE', false, true, '/') assert @http.idempotent? Net::HTTPGenericRequest.new('GET', false, true, '/') assert @http.idempotent? Net::HTTPGenericRequest.new('HEAD', false, false, '/') assert @http.idempotent? Net::HTTPGenericRequest.new('OPTIONS', false, false, '/') assert @http.idempotent? Net::HTTPGenericRequest.new('PUT', true, true, '/') assert @http.idempotent? Net::HTTPGenericRequest.new('TRACE', false, true, '/') refute @http.idempotent? Net::HTTP::Post.new '/' refute @http.idempotent? Net::HTTPGenericRequest.new('POST', true, true, '/') end def test_normalize_uri assert_equal 'http://example', @http.normalize_uri('example') assert_equal 'http://example', @http.normalize_uri('http://example') assert_equal 'https://example', @http.normalize_uri('https://example') end def test_override_haeders assert_empty @http.override_headers @http.override_headers['User-Agent'] = 'MyCustomAgent' expected = { 'User-Agent' => 'MyCustomAgent' } assert_equal expected, @http.override_headers end def test_pipeline skip 'net-http-pipeline not installed' unless defined?(Net::HTTP::Pipeline) cached = basic_connection cached.http.start requests = [ Net::HTTP::Get.new((@uri + '1').request_uri), Net::HTTP::Get.new((@uri + '2').request_uri), ] responses = @http.pipeline @uri, requests assert_equal 2, responses.length assert_equal '/1', responses.first assert_equal '/2', responses.last end def test_private_key_equals @http.private_key = :private_key assert_equal :private_key, @http.private_key assert_equal 1, @http.ssl_generation end def test_proxy_equals_env ENV['http_proxy'] = 'proxy.example' @http.proxy = :ENV assert_equal URI.parse('http://proxy.example'), @http.proxy_uri assert_equal 1, @http.generation, 'generation' assert_equal 1, @http.ssl_generation, 'ssl_generation' end def test_proxy_equals_nil @http.proxy = nil assert_nil @http.proxy_uri assert_equal 1, @http.generation, 'generation' assert_equal 1, @http.ssl_generation, 'ssl_generation' end def test_proxy_equals_uri proxy_uri = URI.parse 'http://proxy.example' @http.proxy = proxy_uri assert_equal proxy_uri, @http.proxy_uri end def test_proxy_from_env ENV['http_proxy'] = 'proxy.example' ENV['http_proxy_user'] = 'johndoe' ENV['http_proxy_pass'] = 'muffins' ENV['NO_PROXY'] = 'localhost,example.com' uri = @http.proxy_from_env expected = URI.parse 'http://proxy.example' expected.user = 'johndoe' expected.password = 'muffins' expected.query = 'no_proxy=localhost%2Cexample.com' assert_equal expected, uri end def test_proxy_from_env_lower ENV['http_proxy'] = 'proxy.example' ENV['http_proxy_user'] = 'johndoe' ENV['http_proxy_pass'] = 'muffins' ENV['no_proxy'] = 'localhost,example.com' uri = @http.proxy_from_env expected = URI.parse 'http://proxy.example' expected.user = 'johndoe' expected.password = 'muffins' expected.query = 'no_proxy=localhost%2Cexample.com' assert_equal expected, uri end def test_proxy_from_env_nil uri = @http.proxy_from_env assert_nil uri ENV['http_proxy'] = '' uri = @http.proxy_from_env assert_nil uri end def test_proxy_from_env_no_proxy_star uri = @http.proxy_from_env assert_nil uri ENV['http_proxy'] = 'proxy.example' ENV['no_proxy'] = '*' uri = @http.proxy_from_env assert_nil uri end def test_proxy_bypass ENV['http_proxy'] = 'proxy.example' ENV['no_proxy'] = 'localhost,example.com:80' @http.proxy = :ENV assert @http.proxy_bypass? 'localhost', 80 assert @http.proxy_bypass? 'localhost', 443 assert @http.proxy_bypass? 'LOCALHOST', 80 assert @http.proxy_bypass? 'example.com', 80 refute @http.proxy_bypass? 'example.com', 443 assert @http.proxy_bypass? 'www.example.com', 80 refute @http.proxy_bypass? 'www.example.com', 443 assert @http.proxy_bypass? 'endingexample.com', 80 refute @http.proxy_bypass? 'example.org', 80 end def test_proxy_bypass_space ENV['http_proxy'] = 'proxy.example' ENV['no_proxy'] = 'localhost, example.com' @http.proxy = :ENV assert @http.proxy_bypass? 'example.com', 80 refute @http.proxy_bypass? 'example.org', 80 end def test_proxy_bypass_trailing ENV['http_proxy'] = 'proxy.example' ENV['no_proxy'] = 'localhost,example.com,' @http.proxy = :ENV assert @http.proxy_bypass? 'example.com', 80 refute @http.proxy_bypass? 'example.org', 80 end def test_proxy_bypass_double_comma ENV['http_proxy'] = 'proxy.example' ENV['no_proxy'] = 'localhost,,example.com' @http.proxy = :ENV assert @http.proxy_bypass? 'example.com', 80 refute @http.proxy_bypass? 'example.org', 80 end def test_reconnect result = @http.reconnect assert_equal 1, result end def test_reconnect_ssl skip 'OpenSSL is missing' unless HAVE_OPENSSL @uri = URI 'https://example.com' now = Time.now ssl_http = ssl_connection def (ssl_http.http).finish @started = 0 end used1 = @http.connection_for @uri do |c| c.requests = 1 c.last_use = now c end assert_equal OpenSSL::SSL::VERIFY_PEER, used1.http.verify_mode @http.verify_mode = OpenSSL::SSL::VERIFY_NONE @http.reconnect_ssl used2 = @http.connection_for @uri do |c| c end assert_same used1, used2 assert_equal OpenSSL::SSL::VERIFY_NONE, used2.http.verify_mode, 'verify mode must change' assert_equal 0, used2.requests assert_equal Net::HTTP::Persistent::EPOCH, used2.last_use end def test_request @http.override_headers['user-agent'] = 'test ua' @http.headers['accept'] = 'text/*' c = connection res = @http.request @uri req = c.http.req assert_kind_of Net::HTTPResponse, res assert_kind_of Net::HTTP::Get, req assert_equal '/path', req.path assert_equal 'test ua', req['user-agent'] assert_match %r%text/\*%, req['accept'] assert_equal 'keep-alive', req['connection'] assert_equal '30', req['keep-alive'] assert_in_delta Time.now, c.last_use assert_equal 1, c.requests end def test_request_ETIMEDOUT c = basic_connection def (c.http).request(*a) raise Errno::ETIMEDOUT, "timed out" end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert_equal 0, c.requests assert_match %r%too many connection resets%, e.message end def test_request_bad_response c = basic_connection def (c.http).request(*a) raise Net::HTTPBadResponse end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert_equal 0, c.requests assert_match %r%too many bad responses%, e.message end def test_request_bad_response_retry c = basic_connection def (c.http).request(*a) raise Net::HTTPBadResponse end assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert c.http.finished? end def test_request_bad_response_unsafe c = basic_connection def (c.http).request(*a) if instance_variable_defined? :@request then raise 'POST must not be retried' else @request = true raise Net::HTTPBadResponse end end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri, Net::HTTP::Post.new(@uri.path) end assert_equal 0, c.requests assert_match %r%too many bad responses%, e.message end def test_request_block @http.headers['user-agent'] = 'test ua' c = connection body = nil res = @http.request @uri do |r| body = r.read_body end req = c.http.req assert_kind_of Net::HTTPResponse, res refute_nil body assert_kind_of Net::HTTP::Get, req assert_equal '/path', req.path assert_equal 'keep-alive', req['connection'] assert_equal '30', req['keep-alive'] assert_match %r%test ua%, req['user-agent'] assert_equal 1, c.requests end def test_request_close_1_0 c = connection class << c.http remove_method :request end def (c.http).request req @req = req r = Net::HTTPResponse.allocate r.instance_variable_set :@header, {} def r.http_version() '1.0' end def r.read_body() :read_body end yield r if block_given? r end request = Net::HTTP::Get.new @uri.request_uri res = @http.request @uri, request req = c.http.req assert_kind_of Net::HTTPResponse, res assert_kind_of Net::HTTP::Get, req assert_equal '/path', req.path assert_equal 'keep-alive', req['connection'] assert_equal '30', req['keep-alive'] assert c.http.finished? end def test_request_connection_close_request c = connection request = Net::HTTP::Get.new @uri.request_uri request['connection'] = 'close' res = @http.request @uri, request req = c.http.req assert_kind_of Net::HTTPResponse, res assert_kind_of Net::HTTP::Get, req assert_equal '/path', req.path assert_equal 'close', req['connection'] assert_nil req['keep-alive'] assert c.http.finished? end def test_request_connection_close_response c = connection class << c.http remove_method :request end def (c.http).request req @req = req r = Net::HTTPResponse.allocate r.instance_variable_set :@header, {} r['connection'] = 'close' def r.http_version() '1.1' end def r.read_body() :read_body end yield r if block_given? r end request = Net::HTTP::Get.new @uri.request_uri res = @http.request @uri, request req = c.http.req assert_kind_of Net::HTTPResponse, res assert_kind_of Net::HTTP::Get, req assert_equal '/path', req.path assert_equal 'keep-alive', req['connection'] assert_equal '30', req['keep-alive'] assert c.http.finished? end def test_request_exception c = basic_connection def (c.http).request(*a) raise Exception, "very bad things happened" end assert_raises Exception do @http.request @uri end assert_equal 0, c.requests assert c.http.finished? end def test_request_invalid c = basic_connection def (c.http).request(*a) raise Errno::EINVAL, "write" end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert_equal 0, c.requests assert_match %r%too many connection resets%, e.message end def test_request_post c = connection post = Net::HTTP::Post.new @uri.path @http.request @uri, post req = c.http.req assert_same post, req end def test_request_reset c = basic_connection def (c.http).request(*a) raise Errno::ECONNRESET end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert_equal 0, c.requests assert_match %r%too many connection resets%, e.message end def test_request_reset_retry c = basic_connection c.last_use = Time.now def (c.http).request(*a) raise Errno::ECONNRESET end assert_raises Net::HTTP::Persistent::Error do @http.request @uri end refute (c.http).reset? assert (c.http).finished? end def test_request_reset_unsafe c = basic_connection def (c.http).request(*a) if instance_variable_defined? :@request then raise 'POST must not be retried' else @request = true raise Errno::ECONNRESET end end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri, Net::HTTP::Post.new(@uri.path) end assert_equal 0, c.requests assert_match %r%too many connection resets%, e.message end def test_request_ssl_error skip 'OpenSSL is missing' unless HAVE_OPENSSL uri = URI.parse 'https://example.com/path' @http.connection_for uri do |c| def (c.http).request(*) raise OpenSSL::SSL::SSLError, "SSL3_WRITE_PENDING:bad write retry" end e = assert_raises Net::HTTP::Persistent::Error do @http.request uri end assert_match %r%bad write retry%, e.message end end def test_request_setup @http.override_headers['user-agent'] = 'test ua' @http.headers['accept'] = 'text/*' input = Net::HTTP::Post.new '/path' req = @http.request_setup input assert_same input, req assert_equal '/path', req.path assert_equal 'test ua', req['user-agent'] assert_match %r%text/\*%, req['accept'] assert_equal 'keep-alive', req['connection'] assert_equal '30', req['keep-alive'] end def test_request_string @http.override_headers['user-agent'] = 'test ua' @http.headers['accept'] = 'text/*' c = connection res = @http.request @uri.to_s req = c.http.req assert_kind_of Net::HTTPResponse, res assert_kind_of Net::HTTP::Get, req assert_equal '/path', req.path assert_equal 1, c.requests end def test_request_setup_uri uri = @uri + '?a=b' req = @http.request_setup uri assert_kind_of Net::HTTP::Get, req assert_equal '/path?a=b', req.path end def test_request_failed c = basic_connection c.requests = 1 c.last_use = Time.now original = nil begin raise 'original' rescue => original end req = Net::HTTP::Get.new '/' e = assert_raises Net::HTTP::Persistent::Error do @http.request_failed original, req, c end assert_match "too many connection resets (due to original - RuntimeError)", e.message assert_equal original.backtrace, e.backtrace end def test_reset c = basic_connection c.http.start c.last_use = Time.now c.requests = 5 @http.reset c assert c.http.started? assert c.http.finished? assert c.http.reset? assert_equal 0, c.requests assert_equal Net::HTTP::Persistent::EPOCH, c.last_use end def test_reset_host_down c = basic_connection c.last_use = Time.now def (c.http).start; raise Errno::EHOSTDOWN end c.requests = 5 e = assert_raises Net::HTTP::Persistent::Error do @http.reset c end assert_match %r%host down%, e.message assert_match __FILE__, e.backtrace.first end def test_reset_io_error c = basic_connection c.last_use = Time.now c.requests = 5 @http.reset c assert c.http.started? assert c.http.finished? end def test_reset_refused c = basic_connection c.last_use = Time.now def (c.http).start; raise Errno::ECONNREFUSED end c.requests = 5 e = assert_raises Net::HTTP::Persistent::Error do @http.reset c end assert_match %r%connection refused%, e.message assert_match __FILE__, e.backtrace.first end def test_retry_change_requests_equals get = Net::HTTP::Get.new('/') post = Net::HTTP::Post.new('/') refute @http.retry_change_requests refute @http.can_retry?(get) refute @http.can_retry?(post) @http.retry_change_requests = true assert @http.retry_change_requests refute @http.can_retry?(get) assert @http.can_retry?(post) end def test_shutdown c = connection orig = @http @http = Net::HTTP::Persistent.new name: 'name' c2 = connection orig.shutdown @http = orig assert c.http.finished?, 'last-generation connection must be finished' refute c2.http.finished?, 'present generation connection must not be finished' end def test_ssl skip 'OpenSSL is missing' unless HAVE_OPENSSL @http.verify_callback = :callback c = Net::HTTP.new 'localhost', 80 @http.ssl c assert c.use_ssl? assert_equal OpenSSL::SSL::VERIFY_PEER, c.verify_mode assert_kind_of OpenSSL::X509::Store, c.cert_store assert_nil c.verify_callback end def test_ssl_ca_file skip 'OpenSSL is missing' unless HAVE_OPENSSL @http.ca_file = 'ca_file' @http.verify_callback = :callback c = Net::HTTP.new 'localhost', 80 @http.ssl c assert c.use_ssl? assert_equal OpenSSL::SSL::VERIFY_PEER, c.verify_mode assert_equal :callback, c.verify_callback end def test_ssl_ca_path skip 'OpenSSL is missing' unless HAVE_OPENSSL @http.ca_path = 'ca_path' @http.verify_callback = :callback c = Net::HTTP.new 'localhost', 80 @http.ssl c assert c.use_ssl? assert_equal OpenSSL::SSL::VERIFY_PEER, c.verify_mode assert_equal :callback, c.verify_callback end def test_ssl_cert_store skip 'OpenSSL is missing' unless HAVE_OPENSSL store = OpenSSL::X509::Store.new @http.cert_store = store c = Net::HTTP.new 'localhost', 80 @http.ssl c assert c.use_ssl? assert_equal store, c.cert_store end def test_ssl_cert_store_default skip 'OpenSSL is missing' unless HAVE_OPENSSL @http.verify_mode = OpenSSL::SSL::VERIFY_PEER c = Net::HTTP.new 'localhost', 80 @http.ssl c assert c.use_ssl? assert c.cert_store end def test_ssl_certificate skip 'OpenSSL is missing' unless HAVE_OPENSSL @http.certificate = :cert @http.private_key = :key c = Net::HTTP.new 'localhost', 80 @http.ssl c assert c.use_ssl? assert_equal :cert, c.cert assert_equal :key, c.key end def test_ssl_verify_mode skip 'OpenSSL is missing' unless HAVE_OPENSSL @http.verify_mode = OpenSSL::SSL::VERIFY_NONE c = Net::HTTP.new 'localhost', 80 @http.ssl c assert c.use_ssl? assert_equal OpenSSL::SSL::VERIFY_NONE, c.verify_mode end def test_ssl_warning skip 'OpenSSL is missing' unless HAVE_OPENSSL begin orig_verify_peer = OpenSSL::SSL::VERIFY_PEER OpenSSL::SSL.send :remove_const, :VERIFY_PEER OpenSSL::SSL.send :const_set, :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE c = Net::HTTP.new 'localhost', 80 out, err = capture_io do @http.ssl c end assert_empty out assert_match %r%localhost:80%, err assert_match %r%I_KNOW_THAT_OPENSSL%, err Object.send :const_set, :I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG, nil assert_silent do @http.ssl c end ensure OpenSSL::SSL.send :remove_const, :VERIFY_PEER OpenSSL::SSL.send :const_set, :VERIFY_PEER, orig_verify_peer if Object.const_defined?(:I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG) then Object.send :remove_const, :I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG end end end def test_ssl_timeout_equals @http.ssl_timeout = :ssl_timeout assert_equal :ssl_timeout, @http.ssl_timeout assert_equal 1, @http.ssl_generation end def test_ssl_version_equals @http.ssl_version = :ssl_version assert_equal :ssl_version, @http.ssl_version assert_equal 1, @http.ssl_generation end def test_min_version_equals @http.min_version = :min_version assert_equal :min_version, @http.min_version assert_equal 1, @http.ssl_generation end def test_max_version_equals @http.max_version = :max_version assert_equal :max_version, @http.max_version assert_equal 1, @http.ssl_generation end def test_start c = basic_connection c = c.http @http.socket_options << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1] @http.debug_output = $stderr @http.open_timeout = 6 @http.start c assert_equal $stderr, c.debug_output assert_equal 6, c.open_timeout socket = c.instance_variable_get :@socket expected = [] expected << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if Socket.const_defined? :TCP_NODELAY expected << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1] assert_equal expected, socket.io.instance_variable_get(:@setsockopts) end def test_verify_callback_equals @http.verify_callback = :verify_callback assert_equal :verify_callback, @http.verify_callback assert_equal 1, @http.ssl_generation end def test_verify_depth_equals @http.verify_depth = :verify_depth assert_equal :verify_depth, @http.verify_depth assert_equal 1, @http.ssl_generation end def test_verify_mode_equals @http.verify_mode = :verify_mode assert_equal :verify_mode, @http.verify_mode assert_equal 1, @http.ssl_generation end end net-http-persistent-3.1.0/test/test_net_http_persistent_timed_stack_multi.rb0000644000004100000410000000532213516510437027744 0ustar www-datawww-datarequire 'minitest/autorun' require 'net/http/persistent' class TestNetHttpPersistentTimedStackMulti < Minitest::Test class Connection attr_reader :host def initialize(host) @host = host end end def setup @stack = Net::HTTP::Persistent::TimedStackMulti.new { Object.new } end def test_empty_eh stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new } refute_empty stack popped = stack.pop assert_empty stack stack.push connection_args: popped refute_empty stack end def test_length stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new } assert_equal 1, stack.length popped = stack.pop assert_equal 0, stack.length stack.push connection_args: popped assert_equal 1, stack.length end def test_pop object = Object.new @stack.push object popped = @stack.pop assert_same object, popped end def test_pop_empty e = assert_raises Timeout::Error do @stack.pop timeout: 0 end assert_equal 'Waited 0 sec', e.message end def test_pop_full stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new } popped = stack.pop refute_nil popped assert_empty stack end def test_pop_wait thread = Thread.start do @stack.pop end Thread.pass while thread.status == 'run' object = Object.new @stack.push object assert_same object, thread.value end def test_pop_shutdown @stack.shutdown { } assert_raises ConnectionPool::PoolShuttingDownError do @stack.pop end end def test_push stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new } conn = stack.pop stack.push connection_args: conn refute_empty stack end def test_push_shutdown called = [] @stack.shutdown do |object| called << object end @stack.push connection_args: Object.new refute_empty called assert_empty @stack end def test_shutdown @stack.push connection_args: Object.new called = [] @stack.shutdown do |object| called << object end refute_empty called assert_empty @stack end def test_pop_recycle stack = Net::HTTP::Persistent::TimedStackMulti.new(2) { |host| Connection.new(host) } a_conn = stack.pop connection_args: 'a.example' stack.push a_conn, connection_args: 'a.example' b_conn = stack.pop connection_args: 'b.example' stack.push b_conn, connection_args: 'b.example' c_conn = stack.pop connection_args: 'c.example' assert_equal 'c.example', c_conn.host stack.push c_conn, connection_args: 'c.example' recreated = stack.pop connection_args: 'a.example' refute_same a_conn, recreated end end net-http-persistent-3.1.0/data.tar.gz.sig0000444000004100000410000000040013516510437020322 0ustar www-datawww-data@YrWs]Ǘ&nCGaA:]g}:w /U;JwڷU<jԉ&nfĻvM%G4scrJhg;\p][X;W; ŻĨH`$ػ;QrjrDVc2tA;1>+?@zKgf sGK &q3W+I nU{r՚3`iHnet-http-persistent-3.1.0/metadata.gz.sig0000444000004100000410000000040013516510437020404 0ustar www-datawww-dataxL)0嫗f[f+KՆʺ,&L694m(hρXO`w/OXS#yj6c(ܜ6qBk_W;D6*ϴmV_+R\H@ O+)# tB]/崯[1x#wM׳$W>nb}8O %%v1yDft%Pi;@x*_vCW#)net-http-persistent-3.1.0/checksums.yaml.gz.sig0000444000004100000410000000040013516510437021552 0ustar www-datawww-datax"%<5?^_ru/]->ʝqHOR,0$W NXζ~o^Z7,0JA8ڽ0]mMv974m _na)'+oX6-k'_ h.*R|R UՋ;o"Xe0 k3шɑpwoΪKCdÈ%aIFDC`(.,"[g_+ŵsЪnet-http-persistent-3.1.0/History.txt0000644000004100000410000003054513516510437017722 0ustar www-datawww-data=== 3.1.0 2019-07-24 New features: * Support ruby 2.6 Net::HTTP#write_timeout=. Pull request #99 by Víctor Roldán Betancort. * Support setting TLS min/max version. Pull request #94 by Bart. Bug fixes: * Reduce potential for memory leak through Hash default proc bindings. Pull request #64 by Dominic Metzger. * Test proxy auto detection from no_proxy in ENV. Pull request #60 by HINOHARA Hiroshi. * Add missing timestamp for 3.0 release. Pull request #78 by Joe Van Dyk. * Support IPv6 URLs in proxy checks. Pull request #82 by Nicolás Sanguinetti. * Use Net::HTTPGenericRequest#method to check idempotence for improved compatibility. Pull request #83 by Fotos Georgiadis. * Run net-http-pipeline tests in travis. Pull request #86 by T.J. Schuck. * Correct +no_proxy+ support to override Net::HTTP proxy fallback. Pull request #88 by Jared Kauppila. * Mitigate memory leak when combined with faraday. Pull request #105 by Yohei Kitamura. * Set default connection pool size for Windows. Pull request #90 by Jared Kauppila. * Fix missing +name:+ argument in documentation. Pull requests #85 by T.J. Schuck, #84 by James White. * Fix memory leak from connection pooling. Pull request #97 by Aaron Patterson. * Update tests for minitest assert_equal deprecation. Pull request #92 by Olle Jonsson. * Fix typo in Net::HTTP::Persistent#pipeline. Pull request #91 by Kazuma Furuhashi. Other: * Added bundler hoe plugin. Pull request #103 by Michael Grosser. * Updated ruby versions in Travis CI. Pull request #93 by Olle Jonsson. Pull request #103 by Michael Grosser. === 3.0 2016-10-05 Breaking changes: * No longer supports ruby 2.0 and earlier * Net::HTTP::Persistent::new now uses keyword arguments for +name+ and +proxy+. * Removed #max_age, use #expired? New features: * Uses connection_pool to manage all connections for a Net::HTTP::Persistent instance. Bug fixes: * Add missing SSL options ca_path, ciphers, ssl_timeout, verify_depth. Issue #63 by Johnneylee Jack Rollins. === 2.9.4 / 2014-02-10 * Bug fixes * Improve proxy escaping from 2.9.2. Pull request #59 by Mislav Marohnić. === 2.9.3 / 2014-02-06 * Bug fixes * Fix breakage in 2.9.2 for users without proxies. Pull request #56 by Yoshihiro TAKAHARA (merged), #57 by ChuckLin, #58 by Kenny Meyer. === 2.9.2 / 2014-02-05 * Bug fixes * Special characters in proxy passwords are now handled correctly. Issue #48 by Mislav Marohnić. Pull request #54 by Juha Kajava === 2.9.1 / 2014-01-22 * Bug fixes * Added license to gemspec. Issue #47 by Benjamin Fleischer * Set Net::HTTP#keep_alive_timeout when supported by ruby. Pull request #53 by Dylan Thacker-Smith. * The backtrace is preserved for errors in #reset to help with debugging. Issue #41 by Andrew Cholakian. === 2.9 / 2013-07-24 * Minor enhancement * Added Net::HTTP::Persistent#max_requests to avoid ECONNRESET for a server that allows a limited number of requests on a connection. Pull request #42 by James Tucker. * Request failures are now raised with the backtrace of the original exception. This gives better insight into the reason for the failure. See #41 by Andrew Cholakian. * OpenSSL is no longer required. If OpenSSL is not available an exception will be raised when attempting to access HTTPS resources. Feature request by André Arko * Bug fixes * Explain the proper way of sending parameters depending upon the request method. Issue #35 by André Arko. * Handle Errno::ETIMEDOUT by retrying the request. Issue #36 by André Arko. * Requests retried by ruby 2.x are no longer retried by net-http-persistent. * Finish the connection if an otherwise unhandled exception happens during a request. Bug #46 by Mark Oude Veldhuis. * Net::HTTP::Persistent::detect_idle_timeout now assumes a StandardError indicates the idle timeout has been found. Bug #43 by James Tucker. === 2.8 / 2012-10-17 * Minor enhancements * Added Net::HTTP::Persistent::detect_idle_timeout which can be used to determine the idle timeout for a host. * The read timeout may now be updated for every request. Issue #33 by Mislav Marohnić * Added NO_PROXY support. Pull Request #31 by Laurence Rowe. * Added #cert and #key aliases for Net::HTTP compatibility. Pull request #26 by dlee. * The artifice gem now disables SSL session reuse to prevent breakage of testing frameworks. Pull Request #29 by Christopher Cooke. * Disabled Net::HTTP::Persistent::SSLReuse on Ruby 2+. This feature is now built-in to Net::HTTP. * Bug fixes * Socket options are set again following connection reset. Pull request #28 by cmaion. * #shutdown now works even if no connections were made. Pull Request #24 by James Tucker. * Updated test RSA key size to 1024 bits. Bug #25 by Gunnar Wolf. * The correct host:port are shown in the exception when a proxy connection fails. Bug #30 by glebtv. === 2.7 / 2012-06-06 * Minor enhancement * Added JRuby compatibility by default for HTTPS connections. (JRuby lacks OpenSSL::SSL::Session.) === 2.6 / 2012-03-26 * Minor enhancement * Net::HTTP::Persistent#idle_timeout may be set to nil to disable expiration of connections. Pull Request #21 by Aaron Stone === 2.5.2 / 2012-02-13 * Bug fix * Fix variable shadowing warning. === 2.5.1 / 2012-02-10 * Bug fix * Reusing SSL connections with HTTP proxies now works. Issue #15 by Paul Ingham and mcrmfc === 2.5 / 2012-02-07 * Minor enhancements * The proxy may be changed at any time. * The allowed SSL version may now be set via #ssl_version. Issue #16 by astera * Added Net::HTTP::Persistent#override_headers which allows overriding * Net::HTTP default headers like User-Agent. See Net::HTTP::Persistent@Headers for details. Issue #17 by andkerosine * Bug fixes * The ruby 1.8 speed monkeypatch now handles EAGAIN for windows users. Issue #12 by Alwyn Schoeman * Fixed POST example in README. Submitted by injekt. * Fixed various bugs in the shutdown of connections especially cross-thread (which you shouldn't be doing anyways). === 2.4.1 / 2012-02-03 * Bug fixes * When FakeWeb or WebMock are loaded SSL sessions will not be reused to prevent breakage of testing frameworks. Issue #13 by Matt Brictson, pull request #14 by Zachary Scott * SSL connections are reset when the SSL parameters change. Mechanize issue #194 by dsisnero * Last-use times are now cleaned up in #shutdown. === 2.4 / 2012-01-31 * Minor enhancement * net-http-persistent now complains if OpenSSL::SSL::VERIFY_PEER is equal to OpenSSL::SSL::VERIFY_NONE. If you have a platform that is broken this way you must define the constant: I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG = nil at the top level of your application to disable the warning. * Bug fix * Fix persisting SSL sessions through HTTP proxies. Mechanize issue #178 by Robert Poor, net-http-persistent issues #10, #11. === 2.3.2 / 2011-12-21 * Bug fix * Finish connections that were closed by Net::HTTP so they can be restarted. === 2.3.1 / 2011-10-26 * Bug fix * If a request object already contains a Connection header it will no longer be overridden. This allows keep-alive connections to be disabled on a per-request basis. === 2.3 / 2011-10-25 * Minor Enhancement * The time since last use for a connection is now recorded in error messages for the connection. === 2.2 / 2011-10-24 * Minor Enhancements * Added timeouts for idle connections which are set through #idle_timeout. The default timeout is 5 seconds. Reducing the idle timeout is preferred over setting #retry_change_requests to true if you wish to avoid the "too many connection resets" error when POSTing data. * Documented tunables and settings in one place in Net::HTTP::Persistent === 2.1 / 2011-09-19 * Minor Enhancement * For HTTPS connections, SSL sessions are now reused avoiding the extra round trips and computations of extra SSL handshakes. If you have problems with SSL session reuse it can be disabled by Net::HTTP::Persistent#reuse_ssl_sessions * Bug Fixes * The default certificate store is now used even if #verify_mode was not set. Issue #7, Pull Request #8 by Matthew M. Boedicker === 2.0 / 2011-08-26 * Incompatibility * Net::HTTP::Persistent#verify_mode now defaults to OpenSSL::SSL::VERIFY_PEER. This may cause HTTPS request failures if your default certificate store lacks the correct certificates. === 1.9 / 2011-08-26 * Minor Enhancement * Added Net::HTTP::Persistent#cert_store to set an SSL certificate store which defaults to the OpenSSL default certificate store. HTTPS server certificates will be validated when this option is combined with setting Net::HTTP::Persistent#verify_mode to OpenSSL::SSL::VERIFY_PEER. === 1.8.1 / 2011-08-08 * Bug Fix * Requests with OpenSSL errors are retried now. Pull Request #5 by James Tucker. === 1.8 / 2011-06-27 * Minor Enhancement * Added Net::HTTP::Persistent#retry_change_requests which allows POST and other non-idempotent requests to be retried automatically. Take care when enabling this option to ensure the server will handle multiple POSTs with the same data in a sane manner. === 1.7 / 2011-04-17 * Minor Enhancement * Added Net::HTTP::Persistent#pipeline which integrates with net-http-pipeline when it is present. * Bug Fix * Perform a case-insensitive check of the URI scheme for HTTPS URIs === 1.6.1 / 2011-03-08 * Bug Fix * Net::HTTP::Persistent#request now handles Errno::EINVAL as a connection reset and will be retried for idempotent requests. Reported by Aaron Qian. === 1.6 / 2011-03-01 * Minor Enhancement * Added Net::HTTP::Persistent#socket_options to set multiple socket options at socket startup. === 1.5.2 / 2011-02-24 * Bug Fix * Only set TCP_NODELAY if the connection has an @socket. Allows net-http-persistent to be used with fake_web. Reported by Sathish Pasupunuri. === 1.5.1 / 2011-02-10 * Bug fix * Only set TCP_NODELAY at connection start. Reported by Brian Henderson. === 1.5 / 2011-01-25 * Minor Enhancements * Set TCP_NODELAY on created socket if possible. (This will only help for requests that send bodies.) === 1.4.1 / 2010-10-13 * Bug Fixes * Don't finish the connection when we're retrying, reset it. Patch by James Tucker. === 1.4 / 2010-09-29 * Minor Enhancements * Added the very dangerous #shutdown_in_all_threads. IT IS DANGEROUS!. Patch by Torsten Schönebaum. === 1.3.1 / 2010-09-13 * Bug Fixes * #connection_for no longer tries to ssl-enable an existing connection. Patch by Joseph West. === 1.3 / 2010-09-08 * Minor Enhancements * HTTP versions are now recorded. This information is not currently used. * Bug Fixes * #shutdown no longer fails when an unstarted HTTP connection is shut down. === 1.2.5 / 2010-07-27 * Bug Fixes * Fix duplicated test name. Noted by Peter Higgins. * #shutdown now works even when no connections were made. === 1.2.4 / 2010-07-26 * Bug Fixes * Actually have #request only finish a connection. Somehow this got missed. === 1.2.3 / 2010-06-29 * Bug Fixes * Fix example code (pointed out by Alex Stahl) === 1.2.2 / 2010-06-22 * Bug Fixes * #request only finishes a connection instead of restarting it. This helps prevents errors on non-idempotent HTTP requests after errors. * #connection_for handles EHOSTDOWN like #reset === 1.2.1 / 2010-05-25 * Bug Fixes * Don't alter Net::BufferedIO#rbuf_fill on 1.9+ === 1.2 / 2010-05-20 * Minor Enhancements * Net::HTTP#read_timeout is now supported * Net::HTTP#open_timeout is now supported * Net::HTTP::Persistent#request now supports a block like Net::HTTP#request === 1.1 / 2010-05-18 * Minor Enhancements * Proxy support, see Net::HTTP::Persistent::new, Net::HTTP::Persistent#proxy_from_env * Added +name+ parameter to Net::HTTP::Persistent::new for separation of connection pools. * Added Net::HTTP::Persistent#shutdown so you can clean up after yourself * Net::HTTP::Persistent now suppresses "peer certificate won't be verified in this SSL session" warning. * Bug Fixes * Net::HTTP::Persistent retries requests in accordance with RFC 2616. === 1.0.1 / 2010-05-05 * Minor Enhancements * Added #debug_output * Now uses Hoe minitest plugin * Bug Fixes * Tests pass on 1.9 === 1.0.0 / 2010-05-04 * Major Enhancements * Birthday! net-http-persistent-3.1.0/.autotest0000644000004100000410000000021413516510437017357 0ustar www-datawww-data# -*- ruby -*- require 'autotest/restart' Autotest.add_hook :initialize do |at| at.add_exception '.git' at.add_exception '.rdoc' end net-http-persistent-3.1.0/.gemtest0000644000004100000410000000000013516510437017150 0ustar www-datawww-datanet-http-persistent-3.1.0/Rakefile0000644000004100000410000000137213516510437017161 0ustar www-datawww-data# -*- ruby -*- require 'hoe' Hoe.plugin :bundler Hoe.plugin :git Hoe.plugin :minitest Hoe.plugin :travis Hoe.spec 'net-http-persistent' do developer 'Eric Hodel', 'drbrain@segment7.net' self.readme_file = 'README.rdoc' self.extra_rdoc_files += Dir['*.rdoc'] self.require_ruby_version '~> 2.1' license 'MIT' rdoc_locations << 'docs.seattlerb.org:/data/www/docs.seattlerb.org/net-http-persistent/' dependency 'connection_pool', '~> 2.2' dependency 'minitest', '~> 5.2', :development dependency 'hoe-bundler', '~> 1.5', :development dependency 'hoe-travis', ['~> 1.4', '>= 1.4.1'], :development dependency 'net-http-pipeline', '~> 1.0' if ENV['TRAVIS_MATRIX'] == 'pipeline' end # vim: syntax=Ruby net-http-persistent-3.1.0/lib/0000755000004100000410000000000013516510437016257 5ustar www-datawww-datanet-http-persistent-3.1.0/lib/net/0000755000004100000410000000000013516510437017045 5ustar www-datawww-datanet-http-persistent-3.1.0/lib/net/http/0000755000004100000410000000000013516510437020024 5ustar www-datawww-datanet-http-persistent-3.1.0/lib/net/http/persistent.rb0000644000004100000410000007475613516510437022574 0ustar www-datawww-datarequire 'net/http' require 'uri' require 'cgi' # for escaping require 'connection_pool' begin require 'net/http/pipeline' rescue LoadError end autoload :OpenSSL, 'openssl' ## # Persistent connections for Net::HTTP # # Net::HTTP::Persistent maintains persistent connections across all the # servers you wish to talk to. For each host:port you communicate with a # single persistent connection is created. # # Multiple Net::HTTP::Persistent objects will share the same set of # connections. # # For each thread you start a new connection will be created. A # Net::HTTP::Persistent connection will not be shared across threads. # # You can shut down the HTTP connections when done by calling #shutdown. You # should name your Net::HTTP::Persistent object if you intend to call this # method. # # Example: # # require 'net/http/persistent' # # uri = URI 'http://example.com/awesome/web/service' # # http = Net::HTTP::Persistent.new name: 'my_app_name' # # # perform a GET # response = http.request uri # # # or # # get = Net::HTTP::Get.new uri.request_uri # response = http.request get # # # create a POST # post_uri = uri + 'create' # post = Net::HTTP::Post.new post_uri.path # post.set_form_data 'some' => 'cool data' # # # perform the POST, the URI is always required # response http.request post_uri, post # # Note that for GET, HEAD and other requests that do not have a body you want # to use URI#request_uri not URI#path. The request_uri contains the query # params which are sent in the body for other requests. # # == SSL # # SSL connections are automatically created depending upon the scheme of the # URI. SSL connections are automatically verified against the default # certificate store for your computer. You can override this by changing # verify_mode or by specifying an alternate cert_store. # # Here are the SSL settings, see the individual methods for documentation: # # #certificate :: This client's certificate # #ca_file :: The certificate-authorities # #ca_path :: Directory with certificate-authorities # #cert_store :: An SSL certificate store # #ciphers :: List of SSl ciphers allowed # #private_key :: The client's SSL private key # #reuse_ssl_sessions :: Reuse a previously opened SSL session for a new # connection # #ssl_timeout :: SSL session lifetime # #ssl_version :: Which specific SSL version to use # #verify_callback :: For server certificate verification # #verify_depth :: Depth of certificate verification # #verify_mode :: How connections should be verified # # == Proxies # # A proxy can be set through #proxy= or at initialization time by providing a # second argument to ::new. The proxy may be the URI of the proxy server or # :ENV which will consult environment variables. # # See #proxy= and #proxy_from_env for details. # # == Headers # # Headers may be specified for use in every request. #headers are appended to # any headers on the request. #override_headers replace existing headers on # the request. # # The difference between the two can be seen in setting the User-Agent. Using # http.headers['User-Agent'] = 'MyUserAgent' will send "Ruby, # MyUserAgent" while http.override_headers['User-Agent'] = # 'MyUserAgent' will send "MyUserAgent". # # == Tuning # # === Segregation # # By providing an application name to ::new you can separate your connections # from the connections of other applications. # # === Idle Timeout # # If a connection hasn't been used for this number of seconds it will automatically be # reset upon the next use to avoid attempting to send to a closed connection. # The default value is 5 seconds. nil means no timeout. Set through #idle_timeout. # # Reducing this value may help avoid the "too many connection resets" error # when sending non-idempotent requests while increasing this value will cause # fewer round-trips. # # === Read Timeout # # The amount of time allowed between reading two chunks from the socket. Set # through #read_timeout # # === Max Requests # # The number of requests that should be made before opening a new connection. # Typically many keep-alive capable servers tune this to 100 or less, so the # 101st request will fail with ECONNRESET. If unset (default), this value has no # effect, if set, connections will be reset on the request after max_requests. # # === Open Timeout # # The amount of time to wait for a connection to be opened. Set through # #open_timeout. # # === Socket Options # # Socket options may be set on newly-created connections. See #socket_options # for details. # # === Non-Idempotent Requests # # By default non-idempotent requests will not be retried per RFC 2616. By # setting retry_change_requests to true requests will automatically be retried # once. # # Only do this when you know that retrying a POST or other non-idempotent # request is safe for your application and will not create duplicate # resources. # # The recommended way to handle non-idempotent requests is the following: # # require 'net/http/persistent' # # uri = URI 'http://example.com/awesome/web/service' # post_uri = uri + 'create' # # http = Net::HTTP::Persistent.new name: 'my_app_name' # # post = Net::HTTP::Post.new post_uri.path # # ... fill in POST request # # begin # response = http.request post_uri, post # rescue Net::HTTP::Persistent::Error # # # POST failed, make a new request to verify the server did not process # # the request # exists_uri = uri + '...' # response = http.get exists_uri # # # Retry if it failed # retry if response.code == '404' # end # # The method of determining if the resource was created or not is unique to # the particular service you are using. Of course, you will want to add # protection from infinite looping. # # === Connection Termination # # If you are done using the Net::HTTP::Persistent instance you may shut down # all the connections in the current thread with #shutdown. This is not # recommended for normal use, it should only be used when it will be several # minutes before you make another HTTP request. # # If you are using multiple threads, call #shutdown in each thread when the # thread is done making requests. If you don't call shutdown, that's OK. # Ruby will automatically garbage collect and shutdown your HTTP connections # when the thread terminates. class Net::HTTP::Persistent ## # The beginning of Time EPOCH = Time.at 0 # :nodoc: ## # Is OpenSSL available? This test works with autoload HAVE_OPENSSL = defined? OpenSSL::SSL # :nodoc: ## # The default connection pool size is 1/4 the allowed open files. if Gem.win_platform? then DEFAULT_POOL_SIZE = 256 else DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4 end ## # The version of Net::HTTP::Persistent you are using VERSION = '3.1.0' ## # Exceptions rescued for automatic retry on ruby 2.0.0. This overlaps with # the exception list for ruby 1.x. RETRIED_EXCEPTIONS = [ # :nodoc: (Net::ReadTimeout if Net.const_defined? :ReadTimeout), IOError, EOFError, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE, (OpenSSL::SSL::SSLError if HAVE_OPENSSL), Timeout::Error, ].compact ## # Error class for errors raised by Net::HTTP::Persistent. Various # SystemCallErrors are re-raised with a human-readable message under this # class. class Error < StandardError; end ## # Use this method to detect the idle timeout of the host at +uri+. The # value returned can be used to configure #idle_timeout. +max+ controls the # maximum idle timeout to detect. # # After # # Idle timeout detection is performed by creating a connection then # performing a HEAD request in a loop until the connection terminates # waiting one additional second per loop. # # NOTE: This may not work on ruby > 1.9. def self.detect_idle_timeout uri, max = 10 uri = URI uri unless URI::Generic === uri uri += '/' req = Net::HTTP::Head.new uri.request_uri http = new 'net-http-persistent detect_idle_timeout' http.connection_for uri do |connection| sleep_time = 0 http = connection.http loop do response = http.request req $stderr.puts "HEAD #{uri} => #{response.code}" if $DEBUG unless Net::HTTPOK === response then raise Error, "bad response code #{response.code} detecting idle timeout" end break if sleep_time >= max sleep_time += 1 $stderr.puts "sleeping #{sleep_time}" if $DEBUG sleep sleep_time end end rescue # ignore StandardErrors, we've probably found the idle timeout. ensure return sleep_time unless $! end ## # This client's OpenSSL::X509::Certificate attr_reader :certificate ## # For Net::HTTP parity alias cert certificate ## # An SSL certificate authority. Setting this will set verify_mode to # VERIFY_PEER. attr_reader :ca_file ## # A directory of SSL certificates to be used as certificate authorities. # Setting this will set verify_mode to VERIFY_PEER. attr_reader :ca_path ## # An SSL certificate store. Setting this will override the default # certificate store. See verify_mode for more information. attr_reader :cert_store ## # The ciphers allowed for SSL connections attr_reader :ciphers ## # Sends debug_output to this IO via Net::HTTP#set_debug_output. # # Never use this method in production code, it causes a serious security # hole. attr_accessor :debug_output ## # Current connection generation attr_reader :generation # :nodoc: ## # Headers that are added to every request using Net::HTTP#add_field attr_reader :headers ## # Maps host:port to an HTTP version. This allows us to enable version # specific features. attr_reader :http_versions ## # Maximum time an unused connection can remain idle before being # automatically closed. attr_accessor :idle_timeout ## # Maximum number of requests on a connection before it is considered expired # and automatically closed. attr_accessor :max_requests ## # The value sent in the Keep-Alive header. Defaults to 30. Not needed for # HTTP/1.1 servers. # # This may not work correctly for HTTP/1.0 servers # # This method may be removed in a future version as RFC 2616 does not # require this header. attr_accessor :keep_alive ## # A name for this connection. Allows you to keep your connections apart # from everybody else's. attr_reader :name ## # Seconds to wait until a connection is opened. See Net::HTTP#open_timeout attr_accessor :open_timeout ## # Headers that are added to every request using Net::HTTP#[]= attr_reader :override_headers ## # This client's SSL private key attr_reader :private_key ## # For Net::HTTP parity alias key private_key ## # The URL through which requests will be proxied attr_reader :proxy_uri ## # List of host suffixes which will not be proxied attr_reader :no_proxy ## # Test-only accessor for the connection pool attr_reader :pool # :nodoc: ## # Seconds to wait until reading one block. See Net::HTTP#read_timeout attr_accessor :read_timeout ## # Seconds to wait until writing one block. See Net::HTTP#write_timeout attr_accessor :write_timeout ## # By default SSL sessions are reused to avoid extra SSL handshakes. Set # this to false if you have problems communicating with an HTTPS server # like: # # SSL_connect [...] read finished A: unexpected message (OpenSSL::SSL::SSLError) attr_accessor :reuse_ssl_sessions ## # An array of options for Socket#setsockopt. # # By default the TCP_NODELAY option is set on sockets. # # To set additional options append them to this array: # # http.socket_options << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1] attr_reader :socket_options ## # Current SSL connection generation attr_reader :ssl_generation # :nodoc: ## # SSL session lifetime attr_reader :ssl_timeout ## # SSL version to use. # # By default, the version will be negotiated automatically between client # and server. Ruby 1.9 and newer only. Deprecated since Ruby 2.5. attr_reader :ssl_version ## # Minimum SSL version to use, e.g. :TLS1_1 # # By default, the version will be negotiated automatically between client # and server. Ruby 2.5 and newer only. attr_reader :min_version ## # Maximum SSL version to use, e.g. :TLS1_2 # # By default, the version will be negotiated automatically between client # and server. Ruby 2.5 and newer only. attr_reader :max_version ## # Where this instance's last-use times live in the thread local variables attr_reader :timeout_key # :nodoc: ## # SSL verification callback. Used when ca_file or ca_path is set. attr_reader :verify_callback ## # Sets the depth of SSL certificate verification attr_reader :verify_depth ## # HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER which verifies # the server certificate. # # If no ca_file, ca_path or cert_store is set the default system certificate # store is used. # # You can use +verify_mode+ to override any default values. attr_reader :verify_mode ## # Enable retries of non-idempotent requests that change data (e.g. POST # requests) when the server has disconnected. # # This will in the worst case lead to multiple requests with the same data, # but it may be useful for some applications. Take care when enabling # this option to ensure it is safe to POST or perform other non-idempotent # requests to the server. attr_accessor :retry_change_requests ## # Creates a new Net::HTTP::Persistent. # # Set +name+ to keep your connections apart from everybody else's. Not # required currently, but highly recommended. Your library name should be # good enough. This parameter will be required in a future version. # # +proxy+ may be set to a URI::HTTP or :ENV to pick up proxy options from # the environment. See proxy_from_env for details. # # In order to use a URI for the proxy you may need to do some extra work # beyond URI parsing if the proxy requires a password: # # proxy = URI 'http://proxy.example' # proxy.user = 'AzureDiamond' # proxy.password = 'hunter2' # # Set +pool_size+ to limit the maximum number of connections allowed. # Defaults to 1/4 the number of allowed file handles. You can have no more # than this many threads with active HTTP transactions. def initialize name: nil, proxy: nil, pool_size: DEFAULT_POOL_SIZE @name = name @debug_output = nil @proxy_uri = nil @no_proxy = [] @headers = {} @override_headers = {} @http_versions = {} @keep_alive = 30 @open_timeout = nil @read_timeout = nil @write_timeout = nil @idle_timeout = 5 @max_requests = nil @socket_options = [] @ssl_generation = 0 # incremented when SSL session variables change @socket_options << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if Socket.const_defined? :TCP_NODELAY @pool = Net::HTTP::Persistent::Pool.new size: pool_size do |http_args| Net::HTTP::Persistent::Connection.new Net::HTTP, http_args, @ssl_generation end @certificate = nil @ca_file = nil @ca_path = nil @ciphers = nil @private_key = nil @ssl_timeout = nil @ssl_version = nil @min_version = nil @max_version = nil @verify_callback = nil @verify_depth = nil @verify_mode = nil @cert_store = nil @generation = 0 # incremented when proxy URI changes if HAVE_OPENSSL then @verify_mode = OpenSSL::SSL::VERIFY_PEER @reuse_ssl_sessions = OpenSSL::SSL.const_defined? :Session end @retry_change_requests = false self.proxy = proxy if proxy end ## # Sets this client's OpenSSL::X509::Certificate def certificate= certificate @certificate = certificate reconnect_ssl end # For Net::HTTP parity alias cert= certificate= ## # Sets the SSL certificate authority file. def ca_file= file @ca_file = file reconnect_ssl end ## # Sets the SSL certificate authority path. def ca_path= path @ca_path = path reconnect_ssl end ## # Overrides the default SSL certificate store used for verifying # connections. def cert_store= store @cert_store = store reconnect_ssl end ## # The ciphers allowed for SSL connections def ciphers= ciphers @ciphers = ciphers reconnect_ssl end ## # Creates a new connection for +uri+ def connection_for uri use_ssl = uri.scheme.downcase == 'https' net_http_args = [uri.hostname, uri.port] if @proxy_uri and not proxy_bypass? uri.hostname, uri.port then net_http_args.concat @proxy_args else net_http_args.concat [nil, nil, nil, nil] end connection = @pool.checkout net_http_args http = connection.http connection.ressl @ssl_generation if connection.ssl_generation != @ssl_generation if not http.started? then ssl http if use_ssl start http elsif expired? connection then reset connection end http.read_timeout = @read_timeout if @read_timeout http.write_timeout = @write_timeout if @write_timeout && http.respond_to?(:write_timeout=) http.keep_alive_timeout = @idle_timeout if @idle_timeout return yield connection rescue Errno::ECONNREFUSED address = http.proxy_address || http.address port = http.proxy_port || http.port raise Error, "connection refused: #{address}:#{port}" rescue Errno::EHOSTDOWN address = http.proxy_address || http.address port = http.proxy_port || http.port raise Error, "host down: #{address}:#{port}" ensure @pool.checkin net_http_args end ## # Returns an error message containing the number of requests performed on # this connection def error_message connection connection.requests -= 1 # fixup age = Time.now - connection.last_use "after #{connection.requests} requests on #{connection.http.object_id}, " \ "last used #{age} seconds ago" end ## # URI::escape wrapper def escape str CGI.escape str if str end ## # URI::unescape wrapper def unescape str CGI.unescape str if str end ## # Returns true if the connection should be reset due to an idle timeout, or # maximum request count, false otherwise. def expired? connection return true if @max_requests && connection.requests >= @max_requests return false unless @idle_timeout return true if @idle_timeout.zero? Time.now - connection.last_use > @idle_timeout end ## # Starts the Net::HTTP +connection+ def start http http.set_debug_output @debug_output if @debug_output http.open_timeout = @open_timeout if @open_timeout http.start socket = http.instance_variable_get :@socket if socket then # for fakeweb @socket_options.each do |option| socket.io.setsockopt(*option) end end end ## # Finishes the Net::HTTP +connection+ def finish connection connection.finish connection.http.instance_variable_set :@ssl_session, nil unless @reuse_ssl_sessions end ## # Returns the HTTP protocol version for +uri+ def http_version uri @http_versions["#{uri.host}:#{uri.port}"] end ## # Is +req+ idempotent according to RFC 2616? def idempotent? req case req.method when 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT', 'TRACE' then true end end ## # Is the request +req+ idempotent or is retry_change_requests allowed. def can_retry? req @retry_change_requests && !idempotent?(req) end ## # Adds "http://" to the String +uri+ if it is missing. def normalize_uri uri (uri =~ /^https?:/) ? uri : "http://#{uri}" end ## # Pipelines +requests+ to the HTTP server at +uri+ yielding responses if a # block is given. Returns all responses received. # # See # Net::HTTP::Pipeline[http://docs.seattlerb.org/net-http-pipeline/Net/HTTP/Pipeline.html] # for further details. # # Only if net-http-pipeline was required before # net-http-persistent #pipeline will be present. def pipeline uri, requests, &block # :yields: responses connection_for uri do |connection| connection.http.pipeline requests, &block end end ## # Sets this client's SSL private key def private_key= key @private_key = key reconnect_ssl end # For Net::HTTP parity alias key= private_key= ## # Sets the proxy server. The +proxy+ may be the URI of the proxy server, # the symbol +:ENV+ which will read the proxy from the environment or nil to # disable use of a proxy. See #proxy_from_env for details on setting the # proxy from the environment. # # If the proxy URI is set after requests have been made, the next request # will shut-down and re-open all connections. # # The +no_proxy+ query parameter can be used to specify hosts which shouldn't # be reached via proxy; if set it should be a comma separated list of # hostname suffixes, optionally with +:port+ appended, for example # example.com,some.host:8080. def proxy= proxy @proxy_uri = case proxy when :ENV then proxy_from_env when URI::HTTP then proxy when nil then # ignore else raise ArgumentError, 'proxy must be :ENV or a URI::HTTP' end @no_proxy.clear if @proxy_uri then @proxy_args = [ @proxy_uri.host, @proxy_uri.port, unescape(@proxy_uri.user), unescape(@proxy_uri.password), ] @proxy_connection_id = [nil, *@proxy_args].join ':' if @proxy_uri.query then @no_proxy = CGI.parse(@proxy_uri.query)['no_proxy'].join(',').downcase.split(',').map { |x| x.strip }.reject { |x| x.empty? } end end reconnect reconnect_ssl end ## # Creates a URI for an HTTP proxy server from ENV variables. # # If +HTTP_PROXY+ is set a proxy will be returned. # # If +HTTP_PROXY_USER+ or +HTTP_PROXY_PASS+ are set the URI is given the # indicated user and password unless HTTP_PROXY contains either of these in # the URI. # # The +NO_PROXY+ ENV variable can be used to specify hosts which shouldn't # be reached via proxy; if set it should be a comma separated list of # hostname suffixes, optionally with +:port+ appended, for example # example.com,some.host:8080. When set to * no proxy will # be returned. # # For Windows users, lowercase ENV variables are preferred over uppercase ENV # variables. def proxy_from_env env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY'] return nil if env_proxy.nil? or env_proxy.empty? uri = URI normalize_uri env_proxy env_no_proxy = ENV['no_proxy'] || ENV['NO_PROXY'] # '*' is special case for always bypass return nil if env_no_proxy == '*' if env_no_proxy then uri.query = "no_proxy=#{escape(env_no_proxy)}" end unless uri.user or uri.password then uri.user = escape ENV['http_proxy_user'] || ENV['HTTP_PROXY_USER'] uri.password = escape ENV['http_proxy_pass'] || ENV['HTTP_PROXY_PASS'] end uri end ## # Returns true when proxy should by bypassed for host. def proxy_bypass? host, port host = host.downcase host_port = [host, port].join ':' @no_proxy.each do |name| return true if host[-name.length, name.length] == name or host_port[-name.length, name.length] == name end false end ## # Forces reconnection of HTTP connections. def reconnect @generation += 1 end ## # Forces reconnection of SSL connections. def reconnect_ssl @ssl_generation += 1 end ## # Finishes then restarts the Net::HTTP +connection+ def reset connection http = connection.http finish connection start http rescue Errno::ECONNREFUSED e = Error.new "connection refused: #{http.address}:#{http.port}" e.set_backtrace $@ raise e rescue Errno::EHOSTDOWN e = Error.new "host down: #{http.address}:#{http.port}" e.set_backtrace $@ raise e end ## # Makes a request on +uri+. If +req+ is nil a Net::HTTP::Get is performed # against +uri+. # # If a block is passed #request behaves like Net::HTTP#request (the body of # the response will not have been read). # # +req+ must be a Net::HTTPGenericRequest subclass (see Net::HTTP for a list). # # If there is an error and the request is idempotent according to RFC 2616 # it will be retried automatically. def request uri, req = nil, &block retried = false bad_response = false uri = URI uri req = request_setup req || uri response = nil connection_for uri do |connection| http = connection.http begin connection.requests += 1 response = http.request req, &block if req.connection_close? or (response.http_version <= '1.0' and not response.connection_keep_alive?) or response.connection_close? then finish connection end rescue Net::HTTPBadResponse => e message = error_message connection finish connection raise Error, "too many bad responses #{message}" if bad_response or not can_retry? req bad_response = true retry rescue *RETRIED_EXCEPTIONS => e request_failed e, req, connection if retried or not can_retry? req reset connection retried = true retry rescue Errno::EINVAL, Errno::ETIMEDOUT => e # not retried on ruby 2 request_failed e, req, connection if retried or not can_retry? req reset connection retried = true retry rescue Exception => e finish connection raise ensure connection.last_use = Time.now end end @http_versions["#{uri.host}:#{uri.port}"] ||= response.http_version response end ## # Raises an Error for +exception+ which resulted from attempting the request # +req+ on the +connection+. # # Finishes the +connection+. def request_failed exception, req, connection # :nodoc: due_to = "(due to #{exception.message} - #{exception.class})" message = "too many connection resets #{due_to} #{error_message connection}" finish connection raise Error, message, exception.backtrace end ## # Creates a GET request if +req_or_uri+ is a URI and adds headers to the # request. # # Returns the request. def request_setup req_or_uri # :nodoc: req = if URI === req_or_uri then Net::HTTP::Get.new req_or_uri.request_uri else req_or_uri end @headers.each do |pair| req.add_field(*pair) end @override_headers.each do |name, value| req[name] = value end unless req['Connection'] then req.add_field 'Connection', 'keep-alive' req.add_field 'Keep-Alive', @keep_alive end req end ## # Shuts down all connections # # *NOTE*: Calling shutdown for can be dangerous! # # If any thread is still using a connection it may cause an error! Call # #shutdown when you are completely done making requests! def shutdown @pool.shutdown { |http| http.finish } end ## # Enables SSL on +connection+ def ssl connection connection.use_ssl = true connection.ciphers = @ciphers if @ciphers connection.ssl_timeout = @ssl_timeout if @ssl_timeout connection.ssl_version = @ssl_version if @ssl_version connection.min_version = @min_version if @min_version connection.max_version = @max_version if @max_version connection.verify_depth = @verify_depth connection.verify_mode = @verify_mode if OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE and not Object.const_defined?(:I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG) then warn <<-WARNING !!!SECURITY WARNING!!! The SSL HTTP connection to: #{connection.address}:#{connection.port} !!!MAY NOT BE VERIFIED!!! On your platform your OpenSSL implementation is broken. There is no difference between the values of VERIFY_NONE and VERIFY_PEER. This means that attempting to verify the security of SSL connections may not work. This exposes you to man-in-the-middle exploits, snooping on the contents of your connection and other dangers to the security of your data. To disable this warning define the following constant at top-level in your application: I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG = nil WARNING end connection.ca_file = @ca_file if @ca_file connection.ca_path = @ca_path if @ca_path if @ca_file or @ca_path then connection.verify_mode = OpenSSL::SSL::VERIFY_PEER connection.verify_callback = @verify_callback if @verify_callback end if @certificate and @private_key then connection.cert = @certificate connection.key = @private_key end connection.cert_store = if @cert_store then @cert_store else store = OpenSSL::X509::Store.new store.set_default_paths store end end ## # SSL session lifetime def ssl_timeout= ssl_timeout @ssl_timeout = ssl_timeout reconnect_ssl end ## # SSL version to use def ssl_version= ssl_version @ssl_version = ssl_version reconnect_ssl end ## # Minimum SSL version to use def min_version= min_version @min_version = min_version reconnect_ssl end ## # maximum SSL version to use def max_version= max_version @max_version = max_version reconnect_ssl end ## # Sets the depth of SSL certificate verification def verify_depth= verify_depth @verify_depth = verify_depth reconnect_ssl end ## # Sets the HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER. # # Setting this to VERIFY_NONE is a VERY BAD IDEA and should NEVER be used. # Securely transfer the correct certificate and update the default # certificate store or set the ca file instead. def verify_mode= verify_mode @verify_mode = verify_mode reconnect_ssl end ## # SSL verification callback. def verify_callback= callback @verify_callback = callback reconnect_ssl end end require 'net/http/persistent/connection' require 'net/http/persistent/pool' net-http-persistent-3.1.0/lib/net/http/persistent/0000755000004100000410000000000013516510437022224 5ustar www-datawww-datanet-http-persistent-3.1.0/lib/net/http/persistent/connection.rb0000644000004100000410000000123613516510437024712 0ustar www-datawww-data## # A Net::HTTP connection wrapper that holds extra information for managing the # connection's lifetime. class Net::HTTP::Persistent::Connection # :nodoc: attr_accessor :http attr_accessor :last_use attr_accessor :requests attr_accessor :ssl_generation def initialize http_class, http_args, ssl_generation @http = http_class.new(*http_args) @ssl_generation = ssl_generation reset end def finish @http.finish rescue IOError ensure reset end def reset @last_use = Net::HTTP::Persistent::EPOCH @requests = 0 end def ressl ssl_generation @ssl_generation = ssl_generation finish end end net-http-persistent-3.1.0/lib/net/http/persistent/timed_stack_multi.rb0000644000004100000410000000311013516510437026245 0ustar www-datawww-dataclass Net::HTTP::Persistent::TimedStackMulti < ConnectionPool::TimedStack # :nodoc: ## # Returns a new hash that has arrays for keys # # Using a class method to limit the bindings referenced by the hash's # default_proc def self.hash_of_arrays # :nodoc: Hash.new { |h,k| h[k] = [] } end def initialize(size = 0, &block) super @enqueued = 0 @ques = self.class.hash_of_arrays @lru = {} @key = :"connection_args-#{object_id}" end def empty? (@created - @enqueued) >= @max end def length @max - @created + @enqueued end private def connection_stored? options = {} # :nodoc: !@ques[options[:connection_args]].empty? end def fetch_connection options = {} # :nodoc: connection_args = options[:connection_args] @enqueued -= 1 lru_update connection_args @ques[connection_args].pop end def lru_update connection_args # :nodoc: @lru.delete connection_args @lru[connection_args] = true end def shutdown_connections # :nodoc: @ques.each_key do |key| super connection_args: key end end def store_connection obj, options = {} # :nodoc: @ques[options[:connection_args]].push obj @enqueued += 1 end def try_create options = {} # :nodoc: connection_args = options[:connection_args] if @created >= @max && @enqueued >= 1 oldest, = @lru.first @lru.delete oldest @ques[oldest].pop @created -= 1 end if @created < @max @created += 1 lru_update connection_args return @create_block.call(connection_args) end end end net-http-persistent-3.1.0/lib/net/http/persistent/pool.rb0000644000004100000410000000212413516510437023521 0ustar www-datawww-dataclass Net::HTTP::Persistent::Pool < ConnectionPool # :nodoc: attr_reader :available # :nodoc: attr_reader :key # :nodoc: def initialize(options = {}, &block) super @available = Net::HTTP::Persistent::TimedStackMulti.new(@size, &block) @key = "current-#{@available.object_id}" end def checkin net_http_args stack = Thread.current[@key][net_http_args] ||= [] raise ConnectionPool::Error, 'no connections are checked out' if stack.empty? conn = stack.pop if stack.empty? @available.push conn, connection_args: net_http_args Thread.current[@key].delete(net_http_args) Thread.current[@key] = nil if Thread.current[@key].empty? end nil end def checkout net_http_args stacks = Thread.current[@key] ||= {} stack = stacks[net_http_args] ||= [] if stack.empty? then conn = @available.pop connection_args: net_http_args else conn = stack.last end stack.push conn conn end def shutdown Thread.current[@key] = nil super end end require 'net/http/persistent/timed_stack_multi' net-http-persistent-3.1.0/README.rdoc0000644000004100000410000000530713516510437017324 0ustar www-datawww-data= net-http-persistent * http://docs.seattlerb.org/net-http-persistent * https://github.com/drbrain/net-http-persistent == DESCRIPTION: Manages persistent connections using Net::HTTP plus a speed fix for Ruby 1.8. It's thread-safe too! Using persistent HTTP connections can dramatically increase the speed of HTTP. Creating a new HTTP connection for every request involves an extra TCP round-trip and causes TCP congestion avoidance negotiation to start over. Net::HTTP supports persistent connections with some API methods but does not handle reconnection gracefully. Net::HTTP::Persistent supports reconnection and retry according to RFC 2616. == FEATURES/PROBLEMS: * Supports SSL * Thread-safe * Pure ruby * Timeout-less speed boost for Ruby 1.8 (by Aaron Patterson) == SYNOPSIS The following example will make two requests to the same server. The connection is kept alive between requests: require 'net/http/persistent' uri = URI 'http://example.com/awesome/web/service' http = Net::HTTP::Persistent.new name: 'my_app_name' # perform a GET response = http.request uri # create a POST post_uri = uri + 'create' post = Net::HTTP::Post.new post_uri.path post.set_form_data 'some' => 'cool data' # perform the POST, the URI is always required response = http.request post_uri, post # if you are done making http requests, or won't make requests for several # minutes http.shutdown Please see the documentation on Net::HTTP::Persistent for more information, including SSL connection verification, header handling and tunable options. == INSTALL: gem install net-http-persistent == LICENSE: (The MIT License) Copyright (c) 2010 Eric Hodel, Aaron Patterson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. net-http-persistent-3.1.0/Gemfile0000644000004100000410000000074213516510437017007 0ustar www-datawww-data# -*- ruby -*- # DO NOT EDIT THIS FILE. Instead, edit Rakefile, and run `rake bundler:gemfile`. source "https://rubygems.org/" gem "connection_pool", "~>2.2" gem "minitest", "~>5.11", :group => [:development, :test] gem "hoe-bundler", "~>1.5", :group => [:development, :test] gem "hoe-travis", "~>1.4", ">=1.4.1", :group => [:development, :test] gem "rdoc", ">=4.0", "<7", :group => [:development, :test] gem "hoe", "~>3.17", :group => [:development, :test] # vim: syntax=ruby net-http-persistent-3.1.0/Manifest.txt0000644000004100000410000000046713516510437020027 0ustar www-datawww-data.autotest .gemtest .travis.yml Gemfile History.txt Manifest.txt README.rdoc Rakefile lib/net/http/persistent.rb lib/net/http/persistent/connection.rb lib/net/http/persistent/pool.rb lib/net/http/persistent/timed_stack_multi.rb test/test_net_http_persistent.rb test/test_net_http_persistent_timed_stack_multi.rb net-http-persistent-3.1.0/net-http-persistent.gemspec0000644000004100000410000001077613516510437023032 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: net-http-persistent 3.1.0 ruby lib Gem::Specification.new do |s| s.name = "net-http-persistent".freeze s.version = "3.1.0" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Eric Hodel".freeze] s.cert_chain = ["-----BEGIN CERTIFICATE-----\nMIIDNjCCAh6gAwIBAgIBBjANBgkqhkiG9w0BAQsFADBBMRAwDgYDVQQDDAdkcmJy\nYWluMRgwFgYKCZImiZPyLGQBGRYIc2VnbWVudDcxEzARBgoJkiaJk/IsZAEZFgNu\nZXQwHhcNMTkwNDA4MjEwOTU2WhcNMjAwNDA3MjEwOTU2WjBBMRAwDgYDVQQDDAdk\ncmJyYWluMRgwFgYKCZImiZPyLGQBGRYIc2VnbWVudDcxEzARBgoJkiaJk/IsZAEZ\nFgNuZXQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCbbgLrGLGIDE76\nLV/cvxdEzCuYuS3oG9PrSZnuDweySUfdp/so0cDq+j8bqy6OzZSw07gdjwFMSd6J\nU5ddZCVywn5nnAQ+Ui7jMW54CYt5/H6f2US6U0hQOjJR6cpfiymgxGdfyTiVcvTm\nGj/okWrQl0NjYOYBpDi+9PPmaH2RmLJu0dB/NylsDnW5j6yN1BEI8MfJRR+HRKZY\nmUtgzBwF1V4KIZQ8EuL6I/nHVu07i6IkrpAgxpXUfdJQJi0oZAqXurAV3yTxkFwd\ng62YrrW26mDe+pZBzR6bpLE+PmXCzz7UxUq3AE0gPHbiMXie3EFE0oxnsU3lIduh\nsCANiQ8BAgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQW\nBBS5k4Z75VSpdM0AclG2UvzFA/VW5DANBgkqhkiG9w0BAQsFAAOCAQEAP5FfXeij\n/fkvIZDdN0LV1ES3Thqoz4aQFbJv1Gf3VccYMs7/Rop5oWBOtiHMIVc855bgv5fx\nuzRtuwiuiq1mZ6IZWkFnEw+vi+M6Q5e/8v+dhej1r7rPW71y4I1wH972O8qiuRXZ\nEVu1y+fPhNAu6OTMgVtgkijEuA9d4OQ2xusF/YKWkaVkjrdHcDAEaquxYUKrswxM\nDohqfAYWGDt2dmCWfRWTsBLm3p3R0mwKe8uOy4gSwcvG5SG57oSZoxrAN9CgsJoR\nP+3YOaiDtZ7g4lYXhpJrMooDnoWr4TPbGIVuq0xfPlFinjBH0o1W+LfGS+3aCN6b\njT8g+1iKSQKJYA==\n-----END CERTIFICATE-----\n".freeze] s.date = "2019-07-25" s.description = "Manages persistent connections using Net::HTTP plus a speed fix for Ruby 1.8.\nIt's thread-safe too!\n\nUsing persistent HTTP connections can dramatically increase the speed of HTTP.\nCreating a new HTTP connection for every request involves an extra TCP\nround-trip and causes TCP congestion avoidance negotiation to start over.\n\nNet::HTTP supports persistent connections with some API methods but does not\nhandle reconnection gracefully. Net::HTTP::Persistent supports reconnection\nand retry according to RFC 2616.".freeze s.email = ["drbrain@segment7.net".freeze] s.extra_rdoc_files = ["History.txt".freeze, "Manifest.txt".freeze, "README.rdoc".freeze] s.files = [".autotest".freeze, ".gemtest".freeze, ".travis.yml".freeze, "Gemfile".freeze, "History.txt".freeze, "Manifest.txt".freeze, "README.rdoc".freeze, "Rakefile".freeze, "lib/net/http/persistent.rb".freeze, "lib/net/http/persistent/connection.rb".freeze, "lib/net/http/persistent/pool.rb".freeze, "lib/net/http/persistent/timed_stack_multi.rb".freeze, "test/test_net_http_persistent.rb".freeze, "test/test_net_http_persistent_timed_stack_multi.rb".freeze] s.homepage = "http://docs.seattlerb.org/net-http-persistent".freeze s.licenses = ["MIT".freeze] s.rdoc_options = ["--main".freeze, "README.rdoc".freeze] s.required_ruby_version = Gem::Requirement.new("~> 2.1".freeze) s.rubygems_version = "2.5.2.1".freeze s.summary = "Manages persistent connections using Net::HTTP plus a speed fix for Ruby 1.8".freeze if s.respond_to? :specification_version then s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q.freeze, ["~> 2.2"]) s.add_development_dependency(%q.freeze, ["~> 3.17"]) s.add_development_dependency(%q.freeze, ["~> 1.5"]) s.add_development_dependency(%q.freeze, [">= 1.4.1", "~> 1.4"]) s.add_development_dependency(%q.freeze, ["~> 5.11"]) s.add_development_dependency(%q.freeze, ["< 7", ">= 4.0"]) else s.add_dependency(%q.freeze, ["~> 2.2"]) s.add_dependency(%q.freeze, ["~> 3.17"]) s.add_dependency(%q.freeze, ["~> 1.5"]) s.add_dependency(%q.freeze, [">= 1.4.1", "~> 1.4"]) s.add_dependency(%q.freeze, ["~> 5.11"]) s.add_dependency(%q.freeze, ["< 7", ">= 4.0"]) end else s.add_dependency(%q.freeze, ["~> 2.2"]) s.add_dependency(%q.freeze, ["~> 3.17"]) s.add_dependency(%q.freeze, ["~> 1.5"]) s.add_dependency(%q.freeze, [">= 1.4.1", "~> 1.4"]) s.add_dependency(%q.freeze, ["~> 5.11"]) s.add_dependency(%q.freeze, ["< 7", ">= 4.0"]) end end