added support for rubies between 2.1 and 2.5 (polyfills)

This commit is contained in:
HoneyryderChuck 2018-01-28 20:13:04 +00:00
parent c64976971d
commit c4811c01f8
10 changed files with 50 additions and 8 deletions

View File

@ -2,6 +2,8 @@
require "httpx/version"
require "httpx/extensions"
require "httpx/errors"
require "httpx/callbacks"
require "httpx/loggable"

33
lib/httpx/extensions.rb Normal file
View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
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

View File

@ -139,9 +139,12 @@ module HTTPX
end
class SSL < TCP
TLS_OPTIONS = OpenSSL::SSL::SSLContext.instance_methods.include?(:alpn_protocols) ?
{ alpn_protocols: %w[h2 http/1.1] } : {}
def initialize(_, _, options)
@ctx = OpenSSL::SSL::SSLContext.new
@ctx.set_params(options.ssl)
@ctx.set_params(TLS_OPTIONS.merge(options.ssl))
super
@state = :negotiated if @keep_open
end

View File

@ -41,7 +41,7 @@ module HTTPX
defaults = {
:debug => ENV.key?("HTTPX_DEBUG") ? $stderr : nil,
:debug_level => (ENV["HTTPX_DEBUG"] || 1).to_i,
:ssl => { alpn_protocols: %w[h2 http/1.1] },
:ssl => {},
:http2_settings => { settings_enable_push: 0 },
:fallback_protocol => "http/1.1",
:timeout => Timeout.new,

View File

@ -33,7 +33,8 @@ module HTTPX
throw(:called)
else
response = ErrorResponse.new("socks error: #{status}", 0)
while (req, _ = @pending.shift)
until @pending.empty?
req, _ = @pending.shift
emit(:response, req, response)
end
end

View File

@ -89,7 +89,8 @@ module HTTPX
def on_error_response(error)
response = ErrorResponse.new(error, 0)
while (req, _ = @pending.shift)
until @pending.empty?
req, _ = @pending.shift
emit(:response, req, response)
end
end

View File

@ -17,7 +17,9 @@ class HTTP2Test < HTTPTest
include Plugins::FollowRedirects
include Plugins::Cookies
include Plugins::Compression
include Plugins::PushPromise
if OpenSSL::SSL::SSLContext.instance_methods.include?(:alpn_protocols)
include Plugins::PushPromise
end
private

View File

@ -65,7 +65,7 @@ class OptionsSpec < Minitest::Test
:body_threshold_size => 114_688,
:form => {:bar => "bar"},
:timeout => Timeout.new,
:ssl => {:foo => "bar", :alpn_protocols => %w[h2 http/1.1] },
:ssl => {:foo => "bar" },
:http2_settings => { :settings_enable_push => 0 },
:fallback_protocol => "http/1.1",
:headers => {"Foo" => "foo", "Accept" => "xml", "Bar" => "bar"},

View File

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

View File

@ -38,7 +38,7 @@ module Requests
attr_reader :file
def initialize(response, **)
@file = Tempfile.new
@file = Tempfile.new("httpx-test")
end
def write(data)