net-http-persistent-2.9.4/0000755000004100000410000000000012306661304015520 5ustar www-datawww-datanet-http-persistent-2.9.4/Rakefile0000644000004100000410000000071212306661304017165 0ustar www-datawww-data# -*- ruby -*- require 'rubygems' require 'hoe' 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'] license 'MIT' rdoc_locations << 'docs.seattlerb.org:/data/www/docs.seattlerb.org/net-http-persistent/' dependency 'minitest', '~> 5.2', :development end # vim: syntax=Ruby net-http-persistent-2.9.4/.gemtest0000644000004100000410000000000012306661304017157 0ustar www-datawww-datanet-http-persistent-2.9.4/Manifest.txt0000644000004100000410000000034412306661304020030 0ustar www-datawww-data.autotest .gemtest History.txt Manifest.txt README.rdoc Rakefile lib/net/http/faster.rb lib/net/http/persistent.rb lib/net/http/persistent/ssl_reuse.rb test/test_net_http_persistent.rb test/test_net_http_persistent_ssl_reuse.rb net-http-persistent-2.9.4/.autotest0000644000004100000410000000021412306661304017366 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-2.9.4/data.tar.gz.sig0000444000004100000410000000040012306661304020331 0ustar www-datawww-data\sg8Czni_dBTuD8(z;4bziqWF^E] qm=ABYiAhx{.`sU4_CtobScf:šJ"JGU!Ӳ{p jײOߔ&E G:u{d\«=,+j`k+9qf>`Xgq߶gI5net-http-persistent-2.9.4/History.txt0000644000004100000410000002456112306661304017732 0ustar www-datawww-data=== 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-2.9.4/README.rdoc0000644000004100000410000000530112306661304017325 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 '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-2.9.4/lib/0000755000004100000410000000000012306661304016266 5ustar www-datawww-datanet-http-persistent-2.9.4/lib/net/0000755000004100000410000000000012306661304017054 5ustar www-datawww-datanet-http-persistent-2.9.4/lib/net/http/0000755000004100000410000000000012306661304020033 5ustar www-datawww-datanet-http-persistent-2.9.4/lib/net/http/persistent/0000755000004100000410000000000012306661304022233 5ustar www-datawww-datanet-http-persistent-2.9.4/lib/net/http/persistent/ssl_reuse.rb0000644000004100000410000000776312306661304024601 0ustar www-datawww-data## # This Net::HTTP subclass adds SSL session reuse and Server Name Indication # (SNI) RFC 3546. # # DO NOT DEPEND UPON THIS CLASS # # This class is an implementation detail and is subject to change or removal # at any time. class Net::HTTP::Persistent::SSLReuse < Net::HTTP @is_proxy_class = false @proxy_addr = nil @proxy_port = nil @proxy_user = nil @proxy_pass = nil def initialize address, port = nil # :nodoc: super @ssl_session = nil end ## # From ruby trunk r33086 including http://redmine.ruby-lang.org/issues/5341 def connect # :nodoc: D "opening connection to #{conn_address()}..." s = timeout(@open_timeout) { TCPSocket.open(conn_address(), conn_port()) } D "opened" if use_ssl? ssl_parameters = Hash.new iv_list = instance_variables SSL_ATTRIBUTES.each do |name| ivname = "@#{name}".intern if iv_list.include?(ivname) and value = instance_variable_get(ivname) ssl_parameters[name] = value end end unless @ssl_context then @ssl_context = OpenSSL::SSL::SSLContext.new @ssl_context.set_params(ssl_parameters) end s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context) s.sync_close = true end @socket = Net::BufferedIO.new(s) @socket.read_timeout = @read_timeout @socket.continue_timeout = @continue_timeout if @socket.respond_to? :continue_timeout @socket.debug_output = @debug_output if use_ssl? begin if proxy? @socket.writeline sprintf('CONNECT %s:%s HTTP/%s', @address, @port, HTTPVersion) @socket.writeline "Host: #{@address}:#{@port}" if proxy_user credential = ["#{proxy_user}:#{proxy_pass}"].pack('m') credential.delete!("\r\n") @socket.writeline "Proxy-Authorization: Basic #{credential}" end @socket.writeline '' Net::HTTPResponse.read_new(@socket).value end s.session = @ssl_session if @ssl_session # Server Name Indication (SNI) RFC 3546 s.hostname = @address if s.respond_to? :hostname= timeout(@open_timeout) { s.connect } if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE s.post_connection_check(@address) end @ssl_session = s.session rescue => exception D "Conn close because of connect error #{exception}" @socket.close if @socket and not @socket.closed? raise exception end end on_connect end if RUBY_VERSION > '1.9' ## # From ruby_1_8_7 branch r29865 including a modified # http://redmine.ruby-lang.org/issues/5341 def connect # :nodoc: D "opening connection to #{conn_address()}..." s = timeout(@open_timeout) { TCPSocket.open(conn_address(), conn_port()) } D "opened" if use_ssl? unless @ssl_context.verify_mode warn "warning: peer certificate won't be verified in this SSL session" @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE end s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context) s.sync_close = true end @socket = Net::BufferedIO.new(s) @socket.read_timeout = @read_timeout @socket.debug_output = @debug_output if use_ssl? if proxy? @socket.writeline sprintf('CONNECT %s:%s HTTP/%s', @address, @port, HTTPVersion) @socket.writeline "Host: #{@address}:#{@port}" if proxy_user credential = ["#{proxy_user}:#{proxy_pass}"].pack('m') credential.delete!("\r\n") @socket.writeline "Proxy-Authorization: Basic #{credential}" end @socket.writeline '' Net::HTTPResponse.read_new(@socket).value end s.session = @ssl_session if @ssl_session s.connect if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE s.post_connection_check(@address) end @ssl_session = s.session end on_connect end if RUBY_VERSION < '1.9' private :connect end net-http-persistent-2.9.4/lib/net/http/persistent.rb0000644000004100000410000010065212306661304022564 0ustar www-datawww-datarequire 'net/http' begin require 'net/https' rescue LoadError # net/https or openssl end if RUBY_VERSION < '1.9' # but only for 1.8 require 'net/http/faster' require 'uri' require 'cgi' # for escaping 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 '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-authority # #cert_store :: An SSL certificate store # #private_key :: The client's SSL private key # #reuse_ssl_sessions :: Reuse a previously opened SSL session for a new # connection # #ssl_version :: Which specific SSL version to use # #verify_callback :: For server 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 '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 version of Net::HTTP::Persistent you are using VERSION = '2.9.4' ## # 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' connection = http.connection_for uri sleep_time = 0 loop do response = connection.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 rescue # ignore StandardErrors, we've probably found the idle timeout. ensure http.shutdown 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 ## # An SSL certificate store. Setting this will override the default # certificate store. See verify_mode for more information. attr_reader :cert_store ## # 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: ## # Where this instance's connections live in the thread local variables attr_reader :generation_key # :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 ## # Seconds to wait until reading one block. See Net::HTTP#read_timeout attr_accessor :read_timeout ## # Where this instance's request counts live in the thread local variables attr_reader :request_key # :nodoc: ## # 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: ## # Where this instance's SSL connections live in the thread local variables attr_reader :ssl_generation_key # :nodoc: ## # SSL version to use. # # By default, the version will be negotiated automatically between client # and server. Ruby 1.9 and newer only. attr_reader :ssl_version if RUBY_VERSION > '1.9' ## # 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 is set. attr_reader :verify_callback ## # HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER which verifies # the server certificate. # # If no ca_file 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' def initialize name = nil, proxy = nil @name = name @debug_output = nil @proxy_uri = nil @no_proxy = [] @headers = {} @override_headers = {} @http_versions = {} @keep_alive = 30 @open_timeout = nil @read_timeout = nil @idle_timeout = 5 @max_requests = nil @socket_options = [] @socket_options << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if Socket.const_defined? :TCP_NODELAY key = ['net_http_persistent', name].compact @generation_key = [key, 'generations' ].join('_').intern @ssl_generation_key = [key, 'ssl_generations'].join('_').intern @request_key = [key, 'requests' ].join('_').intern @timeout_key = [key, 'timeouts' ].join('_').intern @certificate = nil @ca_file = nil @private_key = nil @ssl_version = nil @verify_callback = nil @verify_mode = nil @cert_store = nil @generation = 0 # incremented when proxy URI changes @ssl_generation = 0 # incremented when SSL session variables change if HAVE_OPENSSL then @verify_mode = OpenSSL::SSL::VERIFY_PEER @reuse_ssl_sessions = OpenSSL::SSL.const_defined? :Session end @retry_change_requests = false @ruby_1 = RUBY_VERSION < '2' @retried_on_ruby_2 = !@ruby_1 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 ## # Overrides the default SSL certificate store used for verifying # connections. def cert_store= store @cert_store = store reconnect_ssl end ## # Finishes all connections on the given +thread+ that were created before # the given +generation+ in the threads +generation_key+ list. # # See #shutdown for a bunch of scary warning about misusing this method. def cleanup(generation, thread = Thread.current, generation_key = @generation_key) # :nodoc: timeouts = thread[@timeout_key] (0...generation).each do |old_generation| next unless thread[generation_key] conns = thread[generation_key].delete old_generation conns.each_value do |conn| finish conn, thread timeouts.delete conn.object_id if timeouts end if conns end end ## # Creates a new connection for +uri+ def connection_for uri Thread.current[@generation_key] ||= Hash.new { |h,k| h[k] = {} } Thread.current[@ssl_generation_key] ||= Hash.new { |h,k| h[k] = {} } Thread.current[@request_key] ||= Hash.new 0 Thread.current[@timeout_key] ||= Hash.new EPOCH use_ssl = uri.scheme.downcase == 'https' if use_ssl then raise Net::HTTP::Persistent::Error, 'OpenSSL is not available' unless HAVE_OPENSSL ssl_generation = @ssl_generation ssl_cleanup ssl_generation connections = Thread.current[@ssl_generation_key][ssl_generation] else generation = @generation cleanup generation connections = Thread.current[@generation_key][generation] end net_http_args = [uri.host, uri.port] connection_id = net_http_args.join ':' if @proxy_uri and not proxy_bypass? uri.host, uri.port then connection_id << @proxy_connection_id net_http_args.concat @proxy_args end connection = connections[connection_id] unless connection = connections[connection_id] then connections[connection_id] = http_class.new(*net_http_args) connection = connections[connection_id] ssl connection if use_ssl else reset connection if expired? connection end start connection unless connection.started? connection.read_timeout = @read_timeout if @read_timeout connection.keep_alive_timeout = @idle_timeout if @idle_timeout && connection.respond_to?(:keep_alive_timeout=) connection rescue Errno::ECONNREFUSED address = connection.proxy_address || connection.address port = connection.proxy_port || connection.port raise Error, "connection refused: #{address}:#{port}" rescue Errno::EHOSTDOWN address = connection.proxy_address || connection.address port = connection.proxy_port || connection.port raise Error, "host down: #{address}:#{port}" end ## # Returns an error message containing the number of requests performed on # this connection def error_message connection requests = Thread.current[@request_key][connection.object_id] - 1 # fixup last_use = Thread.current[@timeout_key][connection.object_id] age = Time.now - last_use "after #{requests} requests on #{connection.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 requests = Thread.current[@request_key][connection.object_id] return true if @max_requests && requests >= @max_requests return false unless @idle_timeout return true if @idle_timeout.zero? last_used = Thread.current[@timeout_key][connection.object_id] Time.now - last_used > @idle_timeout end ## # Starts the Net::HTTP +connection+ def start connection connection.set_debug_output @debug_output if @debug_output connection.open_timeout = @open_timeout if @open_timeout connection.start socket = connection.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, thread = Thread.current if requests = thread[@request_key] then requests.delete connection.object_id end connection.finish rescue IOError end def http_class # :nodoc: if RUBY_VERSION > '2.0' then Net::HTTP elsif [:Artifice, :FakeWeb, :WebMock].any? { |klass| Object.const_defined?(klass) } or not @reuse_ssl_sessions then Net::HTTP else Net::HTTP::Persistent::SSLReuse end 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 when Net::HTTP::Delete, Net::HTTP::Get, Net::HTTP::Head, Net::HTTP::Options, Net::HTTP::Put, Net::HTTP::Trace then true end end ## # Is the request +req+ idempotent or is retry_change_requests allowed. # # If +retried_on_ruby_2+ is true, true will be returned if we are on ruby, # retry_change_requests is allowed and the request is not idempotent. def can_retry? req, retried_on_ruby_2 = false return @retry_change_requests && !idempotent?(req) if retried_on_ruby_2 @retry_change_requests || idempotent?(req) end if RUBY_VERSION > '1.9' then ## # Workaround for missing Net::HTTPHeader#connection_close? on Ruby 1.8 def connection_close? header header.connection_close? end ## # Workaround for missing Net::HTTPHeader#connection_keep_alive? on Ruby 1.8 def connection_keep_alive? header header.connection_keep_alive? end else ## # Workaround for missing Net::HTTPRequest#connection_close? on Ruby 1.8 def connection_close? header header['connection'] =~ /close/ or header['proxy-connection'] =~ /close/ end ## # Workaround for missing Net::HTTPRequest#connection_keep_alive? on Ruby # 1.8 def connection_keep_alive? header header['connection'] =~ /keep-alive/ or header['proxy-connection'] =~ /keep-alive/ end end ## # Deprecated in favor of #expired? def max_age # :nodoc: return Time.now + 1 unless @idle_timeout Time.now - @idle_timeout 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 recieved. # # 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 = connection_for uri connection.pipeline requests, &block 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 Thread.current[@request_key].delete connection.object_id Thread.current[@timeout_key].delete connection.object_id finish connection start connection rescue Errno::ECONNREFUSED e = Error.new "connection refused: #{connection.address}:#{connection.port}" e.set_backtrace $@ raise e rescue Errno::EHOSTDOWN e = Error.new "host down: #{connection.address}:#{connection.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::HTTPRequest 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 req = request_setup req || uri connection = connection_for uri connection_id = connection.object_id begin Thread.current[@request_key][connection_id] += 1 response = connection.request req, &block if connection_close?(req) or (response.http_version <= '1.0' and not connection_keep_alive?(response)) or connection_close?(response) then connection.finish 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 # retried on ruby 2 request_failed e, req, connection if retried or not can_retry? req, @retried_on_ruby_2 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 Thread.current[@timeout_key][connection_id] = Time.now 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 for +thread+. # # Uses the current thread by default. # # If you've used Net::HTTP::Persistent across multiple threads you should # call this in each thread when you're done making HTTP requests. # # *NOTE*: Calling shutdown for another thread can be dangerous! # # If the thread is still using the connection it may cause an error! It is # best to call #shutdown in the thread at the appropriate time instead! def shutdown thread = Thread.current generation = reconnect cleanup generation, thread, @generation_key ssl_generation = reconnect_ssl cleanup ssl_generation, thread, @ssl_generation_key thread[@request_key] = nil thread[@timeout_key] = nil end ## # Shuts down all connections in all threads # # *NOTE*: THIS METHOD IS VERY DANGEROUS! # # Do not call this method if other threads are still using their # connections! Call #shutdown at the appropriate time instead! # # Use this method only as a last resort! def shutdown_in_all_threads Thread.list.each do |thread| shutdown thread end nil end ## # Enables SSL on +connection+ def ssl connection connection.use_ssl = true connection.ssl_version = @ssl_version if @ssl_version 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 if @ca_file then connection.ca_file = @ca_file 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 ## # Finishes all connections that existed before the given SSL parameter # +generation+. def ssl_cleanup generation # :nodoc: cleanup generation, Thread.current, @ssl_generation_key end ## # SSL version to use def ssl_version= ssl_version @ssl_version = ssl_version reconnect_ssl end if RUBY_VERSION > '1.9' ## # 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/ssl_reuse' net-http-persistent-2.9.4/lib/net/http/faster.rb0000644000004100000410000000122512306661304021644 0ustar www-datawww-datarequire 'net/protocol' ## # Aaron Patterson's monkeypatch (accepted into 1.9.1) to fix Net::HTTP's speed # problems. # # http://gist.github.com/251244 class Net::BufferedIO #:nodoc: alias :old_rbuf_fill :rbuf_fill def rbuf_fill if @io.respond_to? :read_nonblock then begin @rbuf << @io.read_nonblock(65536) rescue Errno::EWOULDBLOCK, Errno::EAGAIN => e retry if IO.select [@io], nil, nil, @read_timeout raise Timeout::Error, e.message end else # SSL sockets do not have read_nonblock timeout @read_timeout do @rbuf << @io.sysread(65536) end end end end if RUBY_VERSION < '1.9' net-http-persistent-2.9.4/metadata.yml0000644000004100000410000001031012306661304020016 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: net-http-persistent version: !ruby/object:Gem::Version version: 2.9.4 platform: ruby authors: - Eric Hodel autorequire: bindir: bin cert_chain: - | -----BEGIN CERTIFICATE----- MIIDeDCCAmCgAwIBAgIBATANBgkqhkiG9w0BAQUFADBBMRAwDgYDVQQDDAdkcmJy YWluMRgwFgYKCZImiZPyLGQBGRYIc2VnbWVudDcxEzARBgoJkiaJk/IsZAEZFgNu ZXQwHhcNMTMwMjI4MDUyMjA4WhcNMTQwMjI4MDUyMjA4WjBBMRAwDgYDVQQDDAdk cmJyYWluMRgwFgYKCZImiZPyLGQBGRYIc2VnbWVudDcxEzARBgoJkiaJk/IsZAEZ FgNuZXQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCbbgLrGLGIDE76 LV/cvxdEzCuYuS3oG9PrSZnuDweySUfdp/so0cDq+j8bqy6OzZSw07gdjwFMSd6J U5ddZCVywn5nnAQ+Ui7jMW54CYt5/H6f2US6U0hQOjJR6cpfiymgxGdfyTiVcvTm Gj/okWrQl0NjYOYBpDi+9PPmaH2RmLJu0dB/NylsDnW5j6yN1BEI8MfJRR+HRKZY mUtgzBwF1V4KIZQ8EuL6I/nHVu07i6IkrpAgxpXUfdJQJi0oZAqXurAV3yTxkFwd g62YrrW26mDe+pZBzR6bpLE+PmXCzz7UxUq3AE0gPHbiMXie3EFE0oxnsU3lIduh sCANiQ8BAgMBAAGjezB5MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQW BBS5k4Z75VSpdM0AclG2UvzFA/VW5DAfBgNVHREEGDAWgRRkcmJyYWluQHNlZ21l bnQ3Lm5ldDAfBgNVHRIEGDAWgRRkcmJyYWluQHNlZ21lbnQ3Lm5ldDANBgkqhkiG 9w0BAQUFAAOCAQEAOflo4Md5aJF//EetzXIGZ2EI5PzKWX/mMpp7cxFyDcVPtTv0 js/6zWrWSbd60W9Kn4ch3nYiATFKhisgeYotDDz2/pb/x1ivJn4vEvs9kYKVvbF8 V7MV/O5HDW8Q0pA1SljI6GzcOgejtUMxZCyyyDdbUpyAMdt9UpqTZkZ5z1sicgQk 5o2XJ+OhceOIUVqVh1r6DNY5tLVaGJabtBmJAYFVznDcHiSFybGKBa5n25Egql1t KDyY1VIazVgoC8XvR4h/95/iScPiuglzA+DBG1hip1xScAtw05BrXyUNrc9CEMYU wgF94UVoHRp6ywo8I7NP3HcwFQDFNEZPNGXsng== -----END CERTIFICATE----- date: 2014-02-10 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: minitest requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '5.2' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '5.2' - !ruby/object:Gem::Dependency name: rdoc requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '4.0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '4.0' - !ruby/object:Gem::Dependency name: hoe requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '3.7' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '3.7' 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. email: - drbrain@segment7.net executables: [] extensions: [] extra_rdoc_files: - History.txt - Manifest.txt - README.rdoc files: - ".autotest" - ".gemtest" - History.txt - Manifest.txt - README.rdoc - Rakefile - lib/net/http/faster.rb - lib/net/http/persistent.rb - lib/net/http/persistent/ssl_reuse.rb - test/test_net_http_persistent.rb - test/test_net_http_persistent_ssl_reuse.rb homepage: http://docs.seattlerb.org/net-http-persistent licenses: - MIT metadata: {} post_install_message: rdoc_options: - "--main" - README.rdoc require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: net-http-persistent rubygems_version: 2.2.1 signing_key: specification_version: 4 summary: Manages persistent connections using Net::HTTP plus a speed fix for Ruby 1.8 test_files: - test/test_net_http_persistent.rb - test/test_net_http_persistent_ssl_reuse.rb net-http-persistent-2.9.4/test/0000755000004100000410000000000012306661304016477 5ustar www-datawww-datanet-http-persistent-2.9.4/test/test_net_http_persistent.rb0000644000004100000410000011532412306661304024176 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 Net::HTTP::Persistent::SSLReuse include Net::HTTP::Persistent::TestConnect end class TestNetHttpPersistent < Minitest::Test RUBY_1 = RUBY_VERSION < '2' def setup @http_class = if RUBY_1 and HAVE_OPENSSL then Net::HTTP::Persistent::SSLReuse else Net::HTTP end @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 Net::HTTP::Persistent::SSLReuse.use_connect :test_connect end def teardown Thread.current.keys.each do |key| Thread.current[key] = nil end Net::HTTP.use_connect :orig_connect Net::HTTP::Persistent::SSLReuse.use_connect :orig_connect end class BasicConnection attr_accessor :started, :finished, :address, :port, :read_timeout, :open_timeout attr_reader :req, :debug_output def initialize @started, @finished = 0, 0 @address, @port = 'example.com', 80 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 end def basic_connection raise "#{@uri} is not HTTP" unless @uri.scheme.downcase == 'http' c = BasicConnection.new conns[0]["#{@uri.host}:#{@uri.port}"] = c c end def connection c = basic_connection touts[c.object_id] = Time.now def c.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 c end def conns Thread.current[@http.generation_key] ||= Hash.new { |h,k| h[k] = {} } end def reqs Thread.current[@http.request_key] ||= Hash.new 0 end def ssl_conns Thread.current[@http.ssl_generation_key] ||= Hash.new { |h,k| h[k] = {} } end def ssl_connection generation = 0 raise "#{@uri} is not HTTPS" unless @uri.scheme.downcase == 'https' c = BasicConnection.new ssl_conns[generation]["#{@uri.host}:#{@uri.port}"] = c c end def touts Thread.current[@http.timeout_key] ||= Hash.new Net::HTTP::Persistent::EPOCH 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' 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 nil, 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_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 if RUBY_1 then def test_can_retry_eh_idempotent head = Net::HTTP::Head.new '/' assert @http.can_retry? head post = Net::HTTP::Post.new '/' refute @http.can_retry? post end else def test_can_retry_eh_idempotent head = Net::HTTP::Head.new '/' assert @http.can_retry? head refute @http.can_retry? head, true post = Net::HTTP::Post.new '/' refute @http.can_retry? post end 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_connection_for @http.open_timeout = 123 @http.read_timeout = 321 @http.idle_timeout = 42 c = @http.connection_for @uri assert_kind_of @http_class, c assert c.started? refute c.proxy? assert_equal 123, c.open_timeout assert_equal 321, c.read_timeout assert_equal 42, c.keep_alive_timeout unless RUBY_1 assert_includes conns[0].keys, 'example.com:80' assert_same c, conns[0]['example.com:80'] end def test_connection_for_cached cached = basic_connection cached.start conns[0]['example.com:80'] = cached @http.read_timeout = 5 c = @http.connection_for @uri assert c.started? assert_equal 5, c.read_timeout assert_same cached, c end def test_connection_for_closed cached = basic_connection cached.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 conns['example.com:80'] = cached c = @http.connection_for @uri assert c.started? assert_includes conns.keys, 'example.com:80' assert_same c, conns[0]['example.com:80'] socket = c.instance_variable_get :@socket refute_includes socket.io.instance_variables, :@setsockopt refute_includes socket.io.instance_variables, '@setsockopt' end def test_connection_for_debug_output io = StringIO.new @http.debug_output = io c = @http.connection_for @uri assert c.started? assert_equal io, c.instance_variable_get(:@debug_output) assert_includes conns[0].keys, 'example.com:80' assert_same c, conns[0]['example.com:80'] end def test_connection_for_cached_expire_always cached = basic_connection cached.start conns[0]['example.com:80'] = cached reqs[cached.object_id] = 10 touts[cached.object_id] = Time.now # last used right now @http.idle_timeout = 0 c = @http.connection_for @uri assert c.started? assert_same cached, c assert_equal 0, reqs[cached.object_id], 'connection reset due to timeout' end def test_connection_for_cached_expire_never cached = basic_connection cached.start conns[0]['example.com:80'] = cached reqs[cached.object_id] = 10 touts[cached.object_id] = Time.now # last used right now @http.idle_timeout = nil c = @http.connection_for @uri assert c.started? assert_same cached, c assert_equal 10, reqs[cached.object_id], 'connection reset despite no timeout' end def test_connection_for_cached_expired cached = basic_connection cached.start conns[0]['example.com:80'] = cached reqs[cached.object_id] = 10 touts[cached.object_id] = Time.now - 3600 c = @http.connection_for @uri assert c.started? assert_same cached, c assert_equal 0, reqs[cached.object_id], 'connection not reset due to timeout' end def test_connection_for_refused Net::HTTP.use_connect :refused_connect Net::HTTP::Persistent::SSLReuse.use_connect :refused_connect e = assert_raises Net::HTTP::Persistent::Error do @http.connection_for @uri end assert_equal 'connection refused: example.com:80', e.message end def test_connection_for_finished_ssl skip 'OpenSSL is missing' unless HAVE_OPENSSL uri = URI.parse 'https://example.com/path' c = @http.connection_for uri assert c.started? assert c.use_ssl? @http.finish c refute c.started? c2 = @http.connection_for uri assert c2.started? end def test_connection_for_host_down cached = basic_connection def cached.start; raise Errno::EHOSTDOWN end def cached.started?; false end conns[0]['example.com:80'] = cached e = assert_raises Net::HTTP::Persistent::Error do @http.connection_for @uri 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 c = @http.connection_for @uri assert_instance_of Net::HTTP, c 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 c = @http.connection_for @uri assert_instance_of Net::HTTP, c 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 c = @http.connection_for @uri assert_instance_of Net::HTTP, c 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' uri = URI.parse 'http://example/' c = http.connection_for uri assert c.started? refute_includes conns.keys, 'example:80' end def test_connection_for_no_ssl_reuse @http.reuse_ssl_sessions = false @http.open_timeout = 123 @http.read_timeout = 321 c = @http.connection_for @uri assert_instance_of Net::HTTP, c end def test_connection_for_proxy uri = URI.parse 'http://proxy.example' uri.user = 'johndoe' uri.password = 'muffins' http = Net::HTTP::Persistent.new nil, uri c = http.connection_for @uri assert c.started? assert c.proxy? assert_includes conns[1].keys, 'example.com:80:proxy.example:80:johndoe:muffins' assert_same c, conns[1]['example.com:80:proxy.example:80:johndoe:muffins'] 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 nil, uri c = http.connection_for @uri assert_includes conns[1].keys, 'example.com:80:proxy.example:80:john@doe:muf:fins' end def test_connection_for_proxy_host_down Net::HTTP.use_connect :host_down_connect Net::HTTP::Persistent::SSLReuse.use_connect :host_down_connect uri = URI.parse 'http://proxy.example' uri.user = 'johndoe' uri.password = 'muffins' http = Net::HTTP::Persistent.new nil, uri e = assert_raises Net::HTTP::Persistent::Error do http.connection_for @uri end assert_equal 'host down: proxy.example:80', e.message end def test_connection_for_proxy_refused Net::HTTP.use_connect :refused_connect Net::HTTP::Persistent::SSLReuse.use_connect :refused_connect uri = URI.parse 'http://proxy.example' uri.user = 'johndoe' uri.password = 'muffins' http = Net::HTTP::Persistent.new nil, uri e = assert_raises Net::HTTP::Persistent::Error do http.connection_for @uri 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 nil, uri c = http.connection_for @uri assert c.started? refute c.proxy? assert_includes conns[1].keys, 'example.com:80' assert_same c, conns[1]['example.com:80'] end def test_connection_for_refused cached = basic_connection def cached.start; raise Errno::ECONNREFUSED end def cached.started?; false end conns[0]['example.com:80'] = cached e = assert_raises Net::HTTP::Persistent::Error do @http.connection_for @uri end assert_match %r%connection refused%, e.message end def test_connection_for_ssl skip 'OpenSSL is missing' unless HAVE_OPENSSL uri = URI.parse 'https://example.com/path' c = @http.connection_for uri assert c.started? assert c.use_ssl? end def test_connection_for_ssl_cached skip 'OpenSSL is missing' unless HAVE_OPENSSL @uri = URI.parse 'https://example.com/path' cached = ssl_connection 0 c = @http.connection_for @uri assert_same cached, c 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 @http.reconnect_ssl c = @http.connection_for @uri refute_same cached, c end def test_connection_for_ssl_case skip 'OpenSSL is missing' unless HAVE_OPENSSL uri = URI.parse 'HTTPS://example.com/path' c = @http.connection_for uri assert c.started? assert c.use_ssl? end def test_connection_for_timeout cached = basic_connection cached.start reqs[cached.object_id] = 10 touts[cached.object_id] = Time.now - 6 conns[0]['example.com:80'] = cached c = @http.connection_for @uri assert c.started? assert_equal 0, reqs[c.object_id] assert_same cached, c end def test_error_message c = basic_connection touts[c.object_id] = Time.now - 1 reqs[c.object_id] = 5 message = @http.error_message(c) assert_match %r%after 4 requests on #{c.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 reqs[c.object_id] = 0 touts[c.object_id] = 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 reqs[c.object_id] = 0 touts[c.object_id] = Time.now refute @http.expired? c reqs[c.object_id] = 10 refute @http.expired? c @http.max_requests = 10 assert @http.expired? c reqs[c.object_id] = 9 refute @http.expired? c end def test_finish c = basic_connection reqs[c.object_id] = 5 @http.finish c refute c.started? assert c.finished? assert_equal 0, reqs[c.object_id] end def test_finish_io_error c = basic_connection def c.finish; @finished += 1; raise IOError end reqs[c.object_id] = 5 @http.finish c refute c.started? assert c.finished? 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 '/' refute @http.idempotent? Net::HTTP::Post.new '/' end def test_max_age assert_in_delta Time.now - 5, @http.max_age @http.idle_timeout = nil assert_in_delta Time.now + 1, @http.max_age 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.start conns['example.com:80'] = cached 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_equal 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 result = @http.reconnect_ssl assert_equal 1, result end def test_request @http.override_headers['user-agent'] = 'test ua' @http.headers['accept'] = 'text/*' c = connection res = @http.request @uri req = c.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, touts[c.object_id] assert_equal 1, reqs[c.object_id] end def test_request_ETIMEDOUT c = basic_connection def c.request(*a) raise Errno::ETIMEDOUT, "timed out" end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert_equal 0, reqs[c.object_id] assert_match %r%too many connection resets%, e.message end def test_request_bad_response c = basic_connection def c.request(*a) raise Net::HTTPBadResponse end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert_equal 0, reqs[c.object_id] assert_match %r%too many bad responses%, e.message end if RUBY_1 then def test_request_bad_response_retry c = basic_connection def c.request(*a) if defined? @called then r = Net::HTTPResponse.allocate r.instance_variable_set :@header, {} def r.http_version() '1.1' end r else @called = true raise Net::HTTPBadResponse end end @http.request @uri assert c.finished? end else def test_request_bad_response_retry c = basic_connection def c.request(*a) raise Net::HTTPBadResponse end assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert c.finished? end end def test_request_bad_response_unsafe c = basic_connection def c.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, reqs[c.object_id] 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.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, reqs[c.object_id] end def test_request_close_1_0 c = connection class << c remove_method :request end def c.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.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.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.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_equal nil, req['keep-alive'] assert c.finished? end def test_request_connection_close_response c = connection class << c remove_method :request end def c.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.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.finished? end def test_request_exception c = basic_connection def c.request(*a) raise Exception, "very bad things happened" end assert_raises Exception do @http.request @uri end assert_equal 0, reqs[c.object_id] assert c.finished? end def test_request_invalid c = basic_connection def c.request(*a) raise Errno::EINVAL, "write" end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert_equal 0, reqs[c.object_id] assert_match %r%too many connection resets%, e.message end def test_request_invalid_retry c = basic_connection touts[c.object_id] = Time.now def c.request(*a) if defined? @called then r = Net::HTTPResponse.allocate r.instance_variable_set :@header, {} def r.http_version() '1.1' end r else @called = true raise Errno::EINVAL, "write" end end @http.request @uri assert c.reset? assert c.finished? end def test_request_post c = connection post = Net::HTTP::Post.new @uri.path @http.request @uri, post req = c.req assert_same post, req end def test_request_reset c = basic_connection def c.request(*a) raise Errno::ECONNRESET end e = assert_raises Net::HTTP::Persistent::Error do @http.request @uri end assert_equal 0, reqs[c.object_id] assert_match %r%too many connection resets%, e.message end if RUBY_1 then def test_request_reset_retry c = basic_connection touts[c.object_id] = Time.now def c.request(*a) if defined? @called then r = Net::HTTPResponse.allocate r.instance_variable_set :@header, {} def r.http_version() '1.1' end r else @called = true raise Errno::ECONNRESET end end @http.request @uri assert c.reset? assert c.finished? end else def test_request_reset_retry c = basic_connection touts[c.object_id] = Time.now def c.request(*a) raise Errno::ECONNRESET end assert_raises Net::HTTP::Persistent::Error do @http.request @uri end refute c.reset? assert c.finished? end end def test_request_reset_unsafe c = basic_connection def c.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, reqs[c.object_id] 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' c = @http.connection_for uri def c.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 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_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 reqs[c.object_id] = 1 touts[c.object_id] = 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.start touts[c.object_id] = Time.now reqs[c.object_id] = 5 @http.reset c assert c.started? assert c.finished? assert c.reset? assert_equal 0, reqs[c.object_id] assert_equal Net::HTTP::Persistent::EPOCH, touts[c.object_id] end def test_reset_host_down c = basic_connection touts[c.object_id] = Time.now def c.start; raise Errno::EHOSTDOWN end reqs[c.object_id] = 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 touts[c.object_id] = Time.now reqs[c.object_id] = 5 @http.reset c assert c.started? assert c.finished? end def test_reset_refused c = basic_connection touts[c.object_id] = Time.now def c.start; raise Errno::ECONNREFUSED end reqs[c.object_id] = 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 if RUBY_1 then assert @http.can_retry?(get) else assert @http.can_retry?(get) end refute @http.can_retry?(get, true) refute @http.can_retry?(post) @http.retry_change_requests = true assert @http.retry_change_requests if RUBY_1 then assert @http.can_retry?(get) else assert @http.can_retry?(get) refute @http.can_retry?(get, true) end assert @http.can_retry?(post) end def test_shutdown ssl_conns c = connection rs = reqs ts = touts orig = @http @http = Net::HTTP::Persistent.new 'name' c2 = connection orig.shutdown @http = orig assert c.finished?, 'last-generation connection must be finished' refute c2.finished?, 'present generation connection must not be finished' refute_same rs, reqs refute_same ts, touts assert_empty conns assert_empty ssl_conns assert_empty reqs assert_empty touts end def test_shutdown_in_all_threads conns ssl_conns t = Thread.new do c = connection ssl_conns conns reqs Thread.stop c end Thread.pass until t.status == 'sleep' c = connection assert_nil @http.shutdown_in_all_threads assert c.finished?, 'connection in same thread must be finished' assert_empty Thread.current[@http.generation_key] assert_nil Thread.current[@http.request_key] t.run assert t.value.finished?, 'connection in other thread must be finished' assert_empty t[@http.generation_key] assert_nil t[@http.request_key] end def test_shutdown_no_connections @http.shutdown assert_nil Thread.current[@http.generation_key] assert_nil Thread.current[@http.ssl_generation_key] assert_nil Thread.current[@http.request_key] assert_nil Thread.current[@http.timeout_key] end def test_shutdown_not_started ssl_conns c = basic_connection def c.finish() raise IOError end conns[0]["#{@uri.host}:#{@uri.port}"] = c @http.shutdown assert_empty Thread.current[@http.generation_key] assert_empty Thread.current[@http.ssl_generation_key] assert_nil Thread.current[@http.request_key] assert_nil Thread.current[@http.timeout_key] end def test_shutdown_ssl skip 'OpenSSL is missing' unless HAVE_OPENSSL @uri = URI 'https://example' @http.connection_for @uri @http.shutdown assert_empty ssl_conns end def test_shutdown_thread t = Thread.new do c = connection conns ssl_conns reqs Thread.stop c end Thread.pass until t.status == 'sleep' c = connection @http.shutdown t refute c.finished? t.run assert t.value.finished? assert_empty t[@http.generation_key] assert_empty t[@http.ssl_generation_key] assert_nil t[@http.request_key] assert_nil t[@http.timeout_key] 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_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_cleanup skip 'OpenSSL is missing' unless HAVE_OPENSSL uri1 = URI.parse 'https://one.example' c1 = @http.connection_for uri1 touts[c1.object_id] = Time.now reqs[c1.object_id] = 5 @http.reconnect_ssl @http.ssl_cleanup @http.ssl_generation assert_empty ssl_conns assert_empty touts assert_empty reqs # sanity check, performed by #finish 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 unless RUBY_1 def test_start c = basic_connection @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_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-2.9.4/test/test_net_http_persistent_ssl_reuse.rb0000644000004100000410000000466512306661304026267 0ustar www-datawww-datarequire 'rubygems' require 'minitest/autorun' require 'net/http/persistent' have_ssl = begin require 'openssl' require 'webrick' require 'webrick/ssl' true rescue LoadError false end ## # This test is based on (and contains verbatim code from) the Net::HTTP tests # in ruby class TestNetHttpPersistentSSLReuse < Minitest::Test class NullWriter def <<(s) end def puts(*args) end def print(*args) end def printf(*args) end end def setup @name = OpenSSL::X509::Name.parse 'CN=localhost/DC=localdomain' @key = OpenSSL::PKey::RSA.new 1024 @cert = OpenSSL::X509::Certificate.new @cert.version = 2 @cert.serial = 0 @cert.not_before = Time.now @cert.not_after = Time.now + 300 @cert.public_key = @key.public_key @cert.subject = @name @cert.issuer = @name @cert.sign @key, OpenSSL::Digest::SHA1.new @host = 'localhost' @port = 10082 config = { :BindAddress => @host, :Port => @port, :Logger => WEBrick::Log.new(NullWriter.new), :AccessLog => [], :ShutDownSocketWithoutClose => true, :ServerType => Thread, :SSLEnable => true, :SSLCertificate => @cert, :SSLPrivateKey => @key, :SSLStartImmediately => true, } @server = WEBrick::HTTPServer.new config @server.mount_proc '/' do |req, res| res.body = "ok" end @server.start begin TCPSocket.open(@host, @port).close rescue Errno::ECONNREFUSED sleep 0.2 n_try_max -= 1 raise 'cannot spawn server; give up' if n_try_max < 0 retry end end def teardown if @server then @server.shutdown sleep 0.01 until @server.status == :Stop end end def test_ssl_connection_reuse store = OpenSSL::X509::Store.new store.add_cert @cert @http = Net::HTTP::Persistent::SSLReuse.new @host, @port @http.cert_store = store @http.ssl_version = :SSLv3 if @http.respond_to? :ssl_version= @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_PEER @http.start @http.get '/' @http.finish @http.start @http.get '/' @http.finish @http.start @http.get '/' socket = @http.instance_variable_get :@socket ssl_socket = socket.io assert ssl_socket.session_reused? end end if have_ssl net-http-persistent-2.9.4/checksums.yaml.gz.sig0000444000004100000410000000040012306661304021561 0ustar www-datawww-data.Nj^K^;YQ-_QaJmcݭ|tW PA{X'5]1]ǴtHҫuY .muN ? lV'"$lvDSnKAg%v}Gc"yt_uޢu11jhSf2x9VS]ԕ0ާ)77kN΅l#~yTrWQw A ñOlOE%a/7net-http-persistent-2.9.4/metadata.gz.sig0000444000004100000410000000040012306661304020413 0ustar www-datawww-dataoX[Me-[73Y5j )A $.|w+7-7(Jݝw@v[O:"A/^(bFSnet-http-persistent-2.9.4/checksums.yaml.gz0000444000004100000410000000041612306661304021007 0ustar www-datawww-data=Re;n@D{pğt,JZBH vQq+II͉e:8uO=~"\xq ړ7ԳCFnpU^2.Y%&5N0&Dnj$!NSdv#  bӨLV5ga?'6򳲓PeTuDp C>sVGVgJUl+쫀m