lib/httpx/io/ssl.rb in httpx-0.24.7 vs lib/httpx/io/ssl.rb in httpx-1.0.0

- old
+ new

@@ -4,19 +4,15 @@ 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 def initialize(_, _, options) @@ -56,10 +52,24 @@ @io.alpn_protocol || super rescue StandardError super end + if RUBY_ENGINE == "jruby" + # in jruby, alpn_protocol may return "" + # https://github.com/jruby/jruby-openssl/issues/287 + def protocol + proto = @io.alpn_protocol + + return super if proto.nil? || proto.empty? + + proto + rescue StandardError + super + end + end + def can_verify_peer? @ctx.verify_mode == OpenSSL::SSL::VERIFY_PEER end def verify_hostname(host) @@ -101,64 +111,21 @@ @io.sync_close = true end try_ssl_connect end - if RUBY_VERSION < "2.3" - # :nocov: - def try_ssl_connect - @io.connect_nonblock - @io.post_connection_check(@sni_hostname) if @ctx.verify_mode != OpenSSL::SSL::VERIFY_NONE && @verify_hostname - transition(:negotiated) - @interests = :w - rescue ::IO::WaitReadable + def try_ssl_connect + case @io.connect_nonblock(exception: false) + when :wait_readable @interests = :r - rescue ::IO::WaitWritable + return + when :wait_writable @interests = :w + return end - - def read(_, buffer) - super - rescue ::IO::WaitWritable - buffer.clear - 0 - end - - def write(*) - super - rescue ::IO::WaitReadable - 0 - end - # :nocov: - else - def try_ssl_connect - case @io.connect_nonblock(exception: false) - when :wait_readable - @interests = :r - return - when :wait_writable - @interests = :w - return - end - @io.post_connection_check(@sni_hostname) if @ctx.verify_mode != OpenSSL::SSL::VERIFY_NONE && @verify_hostname - transition(:negotiated) - @interests = :w - end - - # :nocov: - if OpenSSL::VERSION < "2.0.6" - def read(size, buffer) - @io.read_nonblock(size, buffer) - buffer.bytesize - rescue ::IO::WaitReadable, - ::IO::WaitWritable - buffer.clear - 0 - rescue EOFError - nil - end - end - # :nocov: + @io.post_connection_check(@sni_hostname) if @ctx.verify_mode != OpenSSL::SSL::VERIFY_NONE && @verify_hostname + transition(:negotiated) + @interests = :w end private def transition(nextstate)