removed overrides and refinements of methods prior to 2.7

This commit is contained in:
HoneyryderChuck 2023-06-17 23:46:23 +01:00
parent 477c3601fc
commit 134bef69e0
22 changed files with 16 additions and 253 deletions

View File

@ -33,7 +33,6 @@ module HTTPX
include Callbacks
using URIExtensions
using NumericExtensions
require "httpx/connection/http2"
require "httpx/connection/http1"

View File

@ -3,96 +3,6 @@
require "uri"
module HTTPX
unless Method.method_defined?(:curry)
# Backport
#
# Ruby 2.1 and lower implement curry only for Procs.
#
# Why not using Refinements? Because they don't work for Method (tested with ruby 2.1.9).
#
module CurryMethods
# Backport for the Method#curry method, which is part of ruby core since 2.2 .
#
def curry(*args)
to_proc.curry(*args)
end
end
Method.__send__(:include, CurryMethods)
end
unless String.method_defined?(:+@)
# Backport for +"", to initialize unfrozen strings from the string literal.
#
module LiteralStringExtensions
def +@
frozen? ? dup : self
end
end
String.__send__(:include, LiteralStringExtensions)
end
unless Numeric.method_defined?(:positive?)
# Ruby 2.3 Backport (Numeric#positive?)
#
module PosMethods
def positive?
self > 0
end
end
Numeric.__send__(:include, PosMethods)
end
unless Numeric.method_defined?(:negative?)
# Ruby 2.3 Backport (Numeric#negative?)
#
module NegMethods
def negative?
self < 0
end
end
Numeric.__send__(:include, NegMethods)
end
module NumericExtensions
# Ruby 2.4 backport
refine Numeric do
def infinite?
self == Float::INFINITY
end unless Numeric.method_defined?(:infinite?)
end
end
module StringExtensions
refine String do
# Ruby 2.5 backport
def delete_suffix!(suffix)
suffix = Backports.coerce_to_str(suffix)
chomp! if frozen?
len = suffix.length
if len > 0 && index(suffix, -len)
self[-len..-1] = ''
self
else
nil
end
end unless String.method_defined?(:delete_suffix!)
end
end
module HashExtensions
refine Hash do
# Ruby 2.4 backport
def compact
h = {}
each do |key, value|
h[key] = value unless value == nil
end
h
end unless Hash.method_defined?(:compact)
end
end
module ArrayExtensions
module FilterMap
refine Array do
@ -108,16 +18,6 @@ module HTTPX
end unless Array.method_defined?(:filter_map)
end
module Sum
refine Array do
# Ruby 2.6 backport
def sum(accumulator = 0, &block)
values = block_given? ? map(&block) : self
values.inject(accumulator, :+)
end
end unless Array.method_defined?(:sum)
end
module Intersect
refine Array do
# Ruby 3.1 backport
@ -133,30 +33,6 @@ module HTTPX
end
end
module IOExtensions
refine IO do
# Ruby 2.3 backport
# provides a fallback for rubies where IO#wait isn't implemented,
# but IO#wait_readable and IO#wait_writable are.
def wait(timeout = nil, _mode = :read_write)
r, w = IO.select([self], [self], nil, timeout)
return unless r || w
self
end unless IO.method_defined?(:wait) && IO.instance_method(:wait).arity == 2
end
end
module RegexpExtensions
refine(Regexp) do
# Ruby 2.4 backport
def match?(*args)
!match(*args).nil?
end
end
end
module URIExtensions
# uri 0.11 backport, ships with ruby 3.1
refine URI::Generic do

View File

@ -6,15 +6,11 @@ module HTTPX
TLSError = OpenSSL::SSL::SSLError
class SSL < TCP
using RegexpExtensions unless Regexp.method_defined?(:match?)
TLS_OPTIONS = if OpenSSL::SSL::SSLContext.instance_methods.include?(:alpn_protocols)
{ alpn_protocols: %w[h2 http/1.1].freeze }
else
{}
end
# rubocop:disable Style/MutableConstant
TLS_OPTIONS = { alpn_protocols: %w[h2 http/1.1].freeze }
# https://github.com/jruby/jruby-openssl/issues/284
TLS_OPTIONS[:verify_hostname] = true if RUBY_ENGINE == "jruby"
# rubocop:enable Style/MutableConstant
TLS_OPTIONS.freeze
attr_writer :ssl_session

View File

@ -58,6 +58,5 @@ module HTTPX
buffer.bytesize
rescue IOError
end
end
end

View File

@ -24,26 +24,11 @@ module HTTPX
debug_stream << message
end
if Exception.instance_methods.include?(:full_message)
def log_exception(ex, level: @options.debug_level, color: nil)
return unless @options.debug
return unless @options.debug_level >= level
log(level: level, color: color) { ex.full_message }
end
else
def log_exception(ex, level: @options.debug_level, color: nil)
return unless @options.debug
return unless @options.debug_level >= level
message = +"#{ex.message} (#{ex.class})"
message << "\n" << ex.backtrace.join("\n") unless ex.backtrace.nil?
log(level: level, color: color) { message }
end
def log_exception(ex, level: @options.debug_level, color: nil)
return unless @options.debug
return unless @options.debug_level >= level
log(level: level, color: color) { ex.full_message }
end
end
end

View File

@ -60,28 +60,6 @@ module HTTPX
:ip_families => ip_address_families,
}.freeze
begin
module HashExtensions
refine Hash do
def >=(other)
Hash[other] <= self
end
def <=(other)
other = Hash[other]
return false unless size <= other.size
each do |k, v|
v2 = other.fetch(k) { return false }
return false unless v2 == v
end
true
end
end
end
using HashExtensions
end unless Hash.method_defined?(:>=)
class << self
def new(options = {})
# let enhanced options go through

View File

@ -8,8 +8,6 @@ module HTTPX
module Plugins
module Authentication
class Digest
using RegexpExtensions unless Regexp.method_defined?(:match?)
def initialize(user, password, hashed: false, **)
@user = user
@password = password

View File

@ -7,8 +7,6 @@ module HTTPX
module Plugins
module Authentication
class Ntlm
using RegexpExtensions unless Regexp.method_defined?(:match?)
def initialize(user, password, domain: nil)
@user = user
@password = password

View File

@ -6,8 +6,6 @@ require "time"
module HTTPX
module Plugins::Cookies
module SetCookieParser
using(RegexpExtensions) unless Regexp.method_defined?(:match?)
# Whitespace.
RE_WSP = /[ \t]+/.freeze

View File

@ -28,35 +28,6 @@ module HTTPX
def extra_options(options)
options.merge(supported_proxy_protocols: [])
end
if URI::Generic.methods.include?(:use_proxy?)
def use_proxy?(*args)
URI::Generic.use_proxy?(*args)
end
else
# https://github.com/ruby/uri/blob/ae07f956a4bea00b4f54a75bd40b8fa918103eed/lib/uri/generic.rb
def use_proxy?(hostname, addr, port, no_proxy)
hostname = hostname.downcase
dothostname = ".#{hostname}"
no_proxy.scan(/([^:,\s]+)(?::(\d+))?/) do |p_host, p_port|
if !p_port || port == p_port.to_i
if p_host.start_with?(".")
return false if hostname.end_with?(p_host.downcase)
else
return false if dothostname.end_with?(".#{p_host.downcase}")
end
if addr
begin
return false if IPAddr.new(p_host).include?(addr)
rescue IPAddr::InvalidAddressError
next
end
end
end
end
true
end
end
end
class Parameters
@ -162,8 +133,8 @@ module HTTPX
no_proxy = proxy[:no_proxy]
no_proxy = no_proxy.join(",") if no_proxy.is_a?(Array)
return super(request, connections, options.merge(proxy: nil)) unless Proxy.use_proxy?(uri.host, next_proxy.host,
next_proxy.port, no_proxy)
return super(request, connections, options.merge(proxy: nil)) unless URI::Generic.use_proxy?(uri.host, next_proxy.host,
next_proxy.port, no_proxy)
end
proxy.merge(uri: next_proxy)

View File

@ -9,7 +9,6 @@ module HTTPX
class Resolver::HTTPS < Resolver::Resolver
extend Forwardable
using URIExtensions
using StringExtensions
module DNSExtensions
refine Resolv::DNS do

View File

@ -363,15 +363,8 @@ module HTTPX
@error.message
end
if Exception.method_defined?(:full_message)
def to_s
@error.full_message(highlight: false)
end
else
def to_s
"#{@error.message} (#{@error.class})\n" \
"#{@error.backtrace.join("\n") if @error.backtrace}"
end
def to_s
@error.full_message(highlight: false)
end
def close

View File

@ -9,8 +9,6 @@ class HTTPX::Selector
private_constant :READABLE
private_constant :WRITABLE
using HTTPX::IOExtensions
def initialize
@selectables = []
end

View File

@ -2,8 +2,6 @@
module HTTPX
module Transcoder
using RegexpExtensions unless Regexp.method_defined?(:match?)
module_function
def normalize_keys(key, value, cond = nil, &block)

View File

@ -9,7 +9,6 @@ module HTTPX::Transcoder
module_function
class Encoder
using HTTPX::ArrayExtensions::Sum
extend Forwardable
def_delegator :@raw, :to_s

View File

@ -4,12 +4,10 @@ require "forwardable"
module HTTPX::Transcoder
module JSON
JSON_REGEX = %r{\bapplication/(?:vnd\.api\+)?json\b}i.freeze
using HTTPX::RegexpExtensions unless Regexp.method_defined?(:match?)
module_function
JSON_REGEX = %r{\bapplication/(?:vnd\.api\+)?json\b}i.freeze
class Encoder
extend Forwardable

View File

@ -6,8 +6,6 @@ require "uri"
module HTTPX::Transcoder
module Xml
using HTTPX::RegexpExtensions
module_function
MIME_TYPES = %r{\b(application|text)/(.+\+)?xml\b}.freeze

View File

@ -3,7 +3,6 @@
module HTTPX
module Utils
using URIExtensions
using HTTPX::RegexpExtensions unless Regexp.method_defined?(:match?)
TOKEN = %r{[^\s()<>,;:\\"/\[\]?=]+}.freeze
VALUE = /"(?:\\"|[^"])*"|#{TOKEN}/.freeze

View File

@ -2,22 +2,6 @@
require "objspace"
unless ObjectSpace.method_defined?(:memsize_of_all)
module ObjectSpace
module_function
def memsize_of_all(klass = false)
total = 0
total_mem = 0
ObjectSpace.each_object(klass) do |e|
total += 1
total_mem += ObjectSpace.memsize_of(e)
end
[total, total_mem]
end
end
end
module ProfilerHelpers
module_function

View File

@ -4,7 +4,6 @@ require_relative "test_helper"
class OptionsTest < Minitest::Test
include HTTPX
using HashExtensions
def test_options_unknown
ex = assert_raises(Error) { Options.new(foo: "bar") }

View File

@ -36,7 +36,7 @@ module Requests
TCPSocket.new(uri.host, uri.port)
when "https"
ctx = OpenSSL::SSL::SSLContext.new
ctx.alpn_protocols = %w[h2 http/1.1] if ctx.respond_to?(:alpn_protocols)
ctx.alpn_protocols = %w[h2 http/1.1]
sock = OpenSSL::SSL::SSLSocket.new(TCPSocket.new(uri.host, uri.port), ctx)
sock.hostname = uri.host
sock.sync_close = true

View File

@ -66,7 +66,7 @@ module Requests
verify_status(response, 200)
verify_body_length(response)
assert response.proxied?
end if OpenSSL::SSL::SSLContext.method_defined?(:alpn_protocols=)
end
# TODO: uncomment when supporting H2 CONNECT
# def test_plugin_https_connect_h2_proxy
@ -77,7 +77,7 @@ module Requests
# response = session.get(uri)
# verify_status(response, 200)
# verify_body_length(response)
# end if OpenSSL::SSL::SSLContext.method_defined?(:alpn_protocols=)
# end
def test_plugin_http_next_proxy
session = HTTPX.plugin(SessionWithPool)