From c4811c01f853f26ffeb75455edba7f52fc3617ae Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Sun, 28 Jan 2018 20:13:04 +0000 Subject: [PATCH] added support for rubies between 2.1 and 2.5 (polyfills) --- lib/httpx.rb | 2 ++ lib/httpx/extensions.rb | 33 ++++++++++++++++++++++++++ lib/httpx/io.rb | 5 +++- lib/httpx/options.rb | 2 +- lib/httpx/plugins/proxy/socks4.rb | 3 ++- lib/httpx/plugins/proxy/socks5.rb | 3 ++- test/http2_test.rb | 4 +++- test/options_test.rb | 2 +- test/support/requests/io.rb | 2 +- test/support/requests/response_body.rb | 2 +- 10 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 lib/httpx/extensions.rb diff --git a/lib/httpx.rb b/lib/httpx.rb index c6fa3e46..8df4b366 100644 --- a/lib/httpx.rb +++ b/lib/httpx.rb @@ -2,6 +2,8 @@ require "httpx/version" +require "httpx/extensions" + require "httpx/errors" require "httpx/callbacks" require "httpx/loggable" diff --git a/lib/httpx/extensions.rb b/lib/httpx/extensions.rb new file mode 100644 index 00000000..f4911dd9 --- /dev/null +++ b/lib/httpx/extensions.rb @@ -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 diff --git a/lib/httpx/io.rb b/lib/httpx/io.rb index 2ab69b61..5b399001 100644 --- a/lib/httpx/io.rb +++ b/lib/httpx/io.rb @@ -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 diff --git a/lib/httpx/options.rb b/lib/httpx/options.rb index 75d95e80..f10fed2b 100644 --- a/lib/httpx/options.rb +++ b/lib/httpx/options.rb @@ -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, diff --git a/lib/httpx/plugins/proxy/socks4.rb b/lib/httpx/plugins/proxy/socks4.rb index 921e1ca7..885f8233 100644 --- a/lib/httpx/plugins/proxy/socks4.rb +++ b/lib/httpx/plugins/proxy/socks4.rb @@ -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 diff --git a/lib/httpx/plugins/proxy/socks5.rb b/lib/httpx/plugins/proxy/socks5.rb index c2a72bfb..8b73d004 100644 --- a/lib/httpx/plugins/proxy/socks5.rb +++ b/lib/httpx/plugins/proxy/socks5.rb @@ -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 diff --git a/test/http2_test.rb b/test/http2_test.rb index 1fcf4295..3f6efe8f 100644 --- a/test/http2_test.rb +++ b/test/http2_test.rb @@ -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 diff --git a/test/options_test.rb b/test/options_test.rb index fc690ca7..2ab62e70 100644 --- a/test/options_test.rb +++ b/test/options_test.rb @@ -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"}, diff --git a/test/support/requests/io.rb b/test/support/requests/io.rb index 986767a1..e3024e56 100644 --- a/test/support/requests/io.rb +++ b/test/support/requests/io.rb @@ -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 diff --git a/test/support/requests/response_body.rb b/test/support/requests/response_body.rb index 9a09c552..e424c51f 100644 --- a/test/support/requests/response_body.rb +++ b/test/support/requests/response_body.rb @@ -38,7 +38,7 @@ module Requests attr_reader :file def initialize(response, **) - @file = Tempfile.new + @file = Tempfile.new("httpx-test") end def write(data)