Skip to content

Commit

Permalink
Remove Ruby 1.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Oct 5, 2016
1 parent e6faeb2 commit c424e45
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 184 deletions.
77 changes: 18 additions & 59 deletions lib/net/http/persistent.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
require '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
Expand Down Expand Up @@ -449,7 +444,7 @@ def self.detect_idle_timeout uri, max = 10
# 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'
attr_reader :ssl_version

##
# Where this instance's last-use times live in the thread local variables
Expand Down Expand Up @@ -532,7 +527,7 @@ def initialize name = nil, proxy = nil

pool_size = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4
@pool = Net::HTTP::Persistent::Pool.new size: pool_size do |http_args|
Net::HTTP::Persistent::Connection.new http_class, http_args, @ssl_generation
Net::HTTP::Persistent::Connection.new Net::HTTP, http_args, @ssl_generation
end

@certificate = nil
Expand All @@ -556,9 +551,6 @@ def initialize name = nil, proxy = nil

@retry_change_requests = false

@ruby_1 = RUBY_VERSION < '2'
@retried_on_ruby_2 = !@ruby_1

self.proxy = proxy if proxy
end

Expand Down Expand Up @@ -744,18 +736,6 @@ def finish connection
connection.finish
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+

Expand All @@ -776,46 +756,25 @@ def idempotent? req

##
# 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)
def can_retry? req
@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
##
# Workaround for missing Net::HTTPRequest#connection_close? on Ruby 1.8

def connection_close? header
header['connection'] =~ /close/ or header['proxy-connection'] =~ /close/
end
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
##
# 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
def connection_keep_alive? header
header['connection'] =~ /keep-alive/ or
header['proxy-connection'] =~ /keep-alive/
end

##
Expand Down Expand Up @@ -1040,9 +999,9 @@ def request uri, req = nil, &block

bad_response = true
retry
rescue *RETRIED_EXCEPTIONS => e # retried on ruby 2
rescue *RETRIED_EXCEPTIONS => e
request_failed e, req, connection if
retried or not can_retry? req, @retried_on_ruby_2
retried or not can_retry? req

reset connection

Expand Down Expand Up @@ -1205,7 +1164,7 @@ def ssl_version= ssl_version
@ssl_version = ssl_version

reconnect_ssl
end if RUBY_VERSION > '1.9'
end

##
# Sets the depth of SSL certificate verification
Expand Down
152 changes: 27 additions & 125 deletions test/test_net_http_persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,7 @@ class Net::HTTP::Persistent::SSLReuse

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'
Expand Down Expand Up @@ -244,27 +236,14 @@ def test_can_retry_eh_change_requests
assert @http.can_retry? post
end

if RUBY_1 then
def test_can_retry_eh_idempotent
head = Net::HTTP::Head.new '/'
def test_can_retry_eh_idempotent
head = Net::HTTP::Head.new '/'

assert @http.can_retry? head
refute @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 '/'
post = Net::HTTP::Post.new '/'

refute @http.can_retry? post
end
refute @http.can_retry? post
end

def test_cert_store_equals
Expand Down Expand Up @@ -294,14 +273,14 @@ def test_connection_for
@http.idle_timeout = 42

used = @http.connection_for @uri do |c|
assert_kind_of @http_class, c.http
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 unless RUBY_1
assert_equal 42, c.http.keep_alive_timeout

c
end
Expand Down Expand Up @@ -1027,38 +1006,17 @@ def (c.http).request(*a) raise Net::HTTPBadResponse end
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.http).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
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

assert c.http.finished?
end
else
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
assert c.http.finished?
end

def test_request_bad_response_unsafe
Expand Down Expand Up @@ -1213,28 +1171,6 @@ def (c.http).request(*a) raise Errno::EINVAL, "write" end
assert_match %r%too many connection resets%, e.message
end

def test_request_invalid_retry
c = basic_connection
c.last_use = Time.now

def (c.http).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.http.reset?
assert c.http.finished?
end

def test_request_post
c = connection

Expand All @@ -1258,43 +1194,20 @@ def (c.http).request(*a) raise Errno::ECONNRESET end
assert_match %r%too many connection resets%, e.message
end

if RUBY_1 then
def test_request_reset_retry
c = basic_connection
c.last_use = Time.now
def (c.http).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
def test_request_reset_retry
c = basic_connection
c.last_use = Time.now

assert c.http.reset?
assert c.http.finished?
def (c.http).request(*a)
raise Errno::ECONNRESET
end
else
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?
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
Expand Down Expand Up @@ -1459,25 +1372,14 @@ def test_retry_change_requests_equals

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?(get)
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

refute @http.can_retry?(get)
assert @http.can_retry?(post)
end

Expand Down Expand Up @@ -1636,7 +1538,7 @@ def test_ssl_version_equals

assert_equal :ssl_version, @http.ssl_version
assert_equal 1, @http.ssl_generation
end unless RUBY_1
end

def test_start
c = basic_connection
Expand Down

0 comments on commit c424e45

Please sign in to comment.