ruby-changes:74236
From: Hiroshi <ko1@a...>
Date: Mon, 24 Oct 2022 23:07:51 +0900 (JST)
Subject: [ruby-changes:74236] f08dee67dc (ruby_3_1): Bump up net-http 0.3.0
https://git.ruby-lang.org/ruby.git/commit/?id=f08dee67dc From f08dee67dc41d961f9032ba18dc597ea072672fb Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA <hsbt@r...> Date: Mon, 24 Oct 2022 18:24:24 +0900 Subject: Bump up net-http 0.3.0 --- lib/net/http.rb | 71 ++++-- lib/net/http/exceptions.rb | 53 ++--- lib/net/http/generic_request.rb | 3 +- lib/net/http/header.rb | 5 +- lib/net/http/net-http.gemspec | 1 - lib/net/http/response.rb | 177 ++++++++++++++- lib/net/http/responses.rb | 451 +++++++++++++++++++------------------ test/net/http/test_http.rb | 102 +++++++-- test/net/http/test_httpheader.rb | 21 ++ test/net/http/test_httpresponse.rb | 250 +++++++++++++++++++- test/net/http/test_https.rb | 12 +- test/net/http/test_https_proxy.rb | 2 +- 12 files changed, 855 insertions(+), 293 deletions(-) diff --git a/lib/net/http.rb b/lib/net/http.rb index 002e3cf10e..b602d2d0b0 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -22,6 +22,7 @@ https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L22 require 'net/protocol' require 'uri' +require 'resolv' autoload :OpenSSL, 'openssl' module Net #:nodoc: @@ -132,7 +133,7 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L133 # puts res.class.name # => 'HTTPOK' # # # Body - # puts res.body if res.response_body_permitted? + # puts res.body # # === Following Redirection # @@ -396,7 +397,7 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L397 class HTTP < Protocol # :stopdoc: - VERSION = "0.2.0" + VERSION = "0.3.0" Revision = %q$Revision$.split[1] HTTPVersion = '1.1' begin @@ -697,6 +698,8 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L698 @continue_timeout = nil @max_retries = 1 @debug_output = nil + @response_body_encoding = false + @ignore_eof = true @proxy_from_env = false @proxy_uri = nil @@ -744,6 +747,18 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L747 # The local port used to establish the connection. attr_accessor :local_port + # The encoding to use for the response body. If Encoding, uses the + # specified encoding. If other true value, tries to detect the response + # body encoding. + attr_reader :response_body_encoding + + # Set the encoding to use for the response body. If given a String, find + # the related Encoding. + def response_body_encoding=(value) + value = Encoding.find(value) if value.is_a?(String) + @response_body_encoding = value + end + attr_writer :proxy_from_env attr_writer :proxy_address attr_writer :proxy_port @@ -825,6 +840,10 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L840 # The default value is 2 seconds. attr_accessor :keep_alive_timeout + # Whether to ignore EOF when reading response bodies with defined + # Content-Length headers. For backwards compatibility, the default is true. + attr_accessor :ignore_eof + # Returns true if the HTTP session has been started. def started? @started @@ -1033,21 +1052,42 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L1052 end end @ssl_context.set_params(ssl_parameters) - @ssl_context.session_cache_mode = - OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT | - OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE - @ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess } + unless @ssl_context.session_cache_mode.nil? # a dummy method on JRuby + @ssl_context.session_cache_mode = + OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT | + OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE + end + if @ssl_context.respond_to?(:session_new_cb) # not implemented under JRuby + @ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess } + end + + # Still do the post_connection_check below even if connecting + # to IP address + verify_hostname = @ssl_context.verify_hostname + + # Server Name Indication (SNI) RFC 3546/6066 + case @address + when Resolv::IPv4::Regex, Resolv::IPv6::Regex + # don't set SNI, as IP addresses in SNI is not valid + # per RFC 6066, section 3. + + # Avoid openssl warning + @ssl_context.verify_hostname = false + else + ssl_host_address = @address + end + debug "starting SSL for #{conn_addr}:#{conn_port}..." s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context) s.sync_close = true - # Server Name Indication (SNI) RFC 3546 - s.hostname = @address if s.respond_to? :hostname= + s.hostname = ssl_host_address if s.respond_to?(:hostname=) && ssl_host_address + if @ssl_session and Process.clock_gettime(Process::CLOCK_REALTIME) < @ssl_session.time.to_f + @ssl_session.timeout s.session = @ssl_session end ssl_socket_connect(s, @open_timeout) - if (@ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE) && @ssl_context.verify_hostname + if (@ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE) && verify_hostname s.post_connection_check(@address) end debug "SSL established, protocol: #{s.ssl_version}, cipher: #{s.cipher[0]}" @@ -1182,16 +1222,9 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L1222 end end - # [Bug #12921] - if /linux|freebsd|darwin/ =~ RUBY_PLATFORM - ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE = true - else - ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE = false - end - # The username of the proxy server, if one is configured. def proxy_user - if ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE && @proxy_from_env + if @proxy_from_env user = proxy_uri&.user unescape(user) if user else @@ -1201,7 +1234,7 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L1234 # The password of the proxy server, if one is configured. def proxy_pass - if ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE && @proxy_from_env + if @proxy_from_env pass = proxy_uri&.password unescape(pass) if pass else @@ -1575,6 +1608,8 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L1608 begin res = HTTPResponse.read_new(@socket) res.decode_content = req.decode_content + res.body_encoding = @response_body_encoding + res.ignore_eof = @ignore_eof end while res.kind_of?(HTTPInformation) res.uri = req.uri diff --git a/lib/net/http/exceptions.rb b/lib/net/http/exceptions.rb index da5f7a70fc..9c425cae16 100644 --- a/lib/net/http/exceptions.rb +++ b/lib/net/http/exceptions.rb @@ -1,33 +1,34 @@ https://github.com/ruby/ruby/blob/trunk/lib/net/http/exceptions.rb#L1 # frozen_string_literal: false -# Net::HTTP exception class. -# You cannot use Net::HTTPExceptions directly; instead, you must use -# its subclasses. -module Net::HTTPExceptions - def initialize(msg, res) #:nodoc: - super msg - @response = res +module Net + # Net::HTTP exception class. + # You cannot use Net::HTTPExceptions directly; instead, you must use + # its subclasses. + module HTTPExceptions + def initialize(msg, res) #:nodoc: + super msg + @response = res + end + attr_reader :response + alias data response #:nodoc: obsolete end - attr_reader :response - alias data response #:nodoc: obsolete -end -class Net::HTTPError < Net::ProtocolError - include Net::HTTPExceptions -end -class Net::HTTPRetriableError < Net::ProtoRetriableError - include Net::HTTPExceptions -end -class Net::HTTPServerException < Net::ProtoServerError - # We cannot use the name "HTTPServerError", it is the name of the response. - include Net::HTTPExceptions -end -# for compatibility -Net::HTTPClientException = Net::HTTPServerException + class HTTPError < ProtocolError + include HTTPExceptions + end -class Net::HTTPFatalError < Net::ProtoFatalError - include Net::HTTPExceptions -end + class HTTPRetriableError < ProtoRetriableError + include HTTPExceptions + end -module Net + class HTTPClientException < ProtoServerError + include HTTPExceptions + end + + class HTTPFatalError < ProtoFatalError + include HTTPExceptions + end + + # We cannot use the name "HTTPServerError", it is the name of the response. + HTTPServerException = HTTPClientException # :nodoc: deprecate_constant(:HTTPServerException) end diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index 313de6ac92..d56835c76f 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -15,7 +15,8 @@ class Net::HTTPGenericRequest https://github.com/ruby/ruby/blob/trunk/lib/net/http/generic_request.rb#L15 if URI === uri_or_path then raise ArgumentError, "not an HTTP URI" unless URI::HTTP === uri_or_path - raise ArgumentError, "no host component for URI" unless uri_or_path.hostname + hostname = uri_or_path.hostname + raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0) @uri = uri_or_path.dup host = @uri.hostname.dup host << ":".freeze << @uri.port.to_s if @uri.port != @uri.default_port diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb index a8901e79cb..b0ec4b0625 100644 --- a/lib/net/http/header.rb +++ b/lib/net/http/header.rb @@ -338,9 +338,10 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L338 # fits inside the full entity body, as ra (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/