[ruby/net-http] Redirection revision

https://github.com/ruby/net-http/commit/9a4e2d3a2a
This commit is contained in:
BurdetteLamar 2023-01-31 17:05:56 +00:00 committed by git
parent 97740a525e
commit 3ebc80314c

View File

@ -262,28 +262,26 @@ module Net #:nodoc:
# #
# == Following Redirection # == Following Redirection
# #
# Each Net::HTTPResponse object belongs to a class for its response code. # Each returned response is an instance of a subclass of Net::HTTPResponse.
# See the {response class hierarchy}[rdoc-ref:Net::HTTPResponse@Response+Subclasses].
# #
# For example, all 2XX responses are instances of a Net::HTTPSuccess # In particular, class Net::HTTPRedirection is the parent
# subclass, a 3XX response is an instance of a Net::HTTPRedirection # of all redirection classes.
# subclass and a 200 response is an instance of the Net::HTTPOK class. # This allows you to craft a case statement to handle redirections properly:
# For details, see HTTPResponse.
#
# Using a case statement you can handle various types of responses properly:
# #
# def fetch(uri, limit = 10) # def fetch(uri, limit = 10)
# # You should choose a better exception. # # You should choose a better exception.
# raise ArgumentError, 'too many HTTP redirects' if limit == 0 # raise ArgumentError, 'Too many HTTP redirects' if limit == 0
# #
# res = Net::HTTP.get_response(URI(uri)) # res = Net::HTTP.get_response(URI(uri))
# case res # case res
# when Net::HTTPSuccess then # when Net::HTTPSuccess # Any success class.
# res # res
# when Net::HTTPRedirection then # when Net::HTTPRedirection # Any redirection class.
# location = res['location'] # location = res['Location']
# warn "redirected to #{location}" # warn "Redirected to #{location}"
# fetch(location, limit - 1) # fetch(location, limit - 1)
# else # else # Any other class.
# res.value # res.value
# end # end
# end # end
@ -320,7 +318,7 @@ module Net #:nodoc:
# #
# == HTTPS # == HTTPS
# #
# HTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=: # HTTPS is enabled for an \HTTP connection by Net::HTTP#use_ssl=:
# #
# Net::HTTP.start(hostname, :use_ssl => true) do |http| # Net::HTTP.start(hostname, :use_ssl => true) do |http|
# req = Net::HTTP::Get.new(uri) # req = Net::HTTP::Get.new(uri)
@ -328,7 +326,7 @@ module Net #:nodoc:
# end # end
# #
# Or if you simply want to make a GET request, you may pass in a URI # Or if you simply want to make a GET request, you may pass in a URI
# object that has an HTTPS URL. \Net::HTTP automatically turns on TLS # object that has an \HTTPS URL. \Net::HTTP automatically turns on TLS
# verification if the URI object has a 'https' URI scheme: # verification if the URI object has a 'https' URI scheme:
# #
# uri # => #<URI::HTTPS https://jsonplaceholder.typicode.com/> # uri # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
@ -529,10 +527,10 @@ module Net #:nodoc:
end end
# #
# HTTP session management # \HTTP session management
# #
# Returns intger +80+, the default port to use for HTTP requests: # Returns intger +80+, the default port to use for \HTTP requests:
# #
# Net::HTTP.default_port # => 80 # Net::HTTP.default_port # => 80
# #
@ -540,7 +538,7 @@ module Net #:nodoc:
http_default_port() http_default_port()
end end
# Returns integer +80+, the default port to use for HTTP requests: # Returns integer +80+, the default port to use for \HTTP requests:
# #
# Net::HTTP.http_default_port # => 80 # Net::HTTP.http_default_port # => 80
# #
@ -673,7 +671,7 @@ module Net #:nodoc:
end end
# Returns a new \Net::HTTP object +http+ # Returns a new \Net::HTTP object +http+
# (but does not open a TCP connection or HTTP session). # (but does not open a TCP connection or \HTTP session).
# #
# <b>No Proxy</b> # <b>No Proxy</b>
# #
@ -806,7 +804,7 @@ module Net #:nodoc:
end end
# Creates a new \Net::HTTP object for the specified server address, # Creates a new \Net::HTTP object for the specified server address,
# without opening the TCP connection or initializing the HTTP session. # without opening the TCP connection or initializing the \HTTP session.
# The +address+ should be a DNS hostname or IP address. # The +address+ should be a DNS hostname or IP address.
def initialize(address, port = nil) def initialize(address, port = nil)
@address = address @address = address
@ -991,20 +989,20 @@ module Net #:nodoc:
end end
# Number of seconds to wait for the connection to open. Any number # Number of seconds to wait for the connection to open. Any number
# may be used, including Floats for fractional seconds. If the HTTP # may be used, including Floats for fractional seconds. If the \HTTP
# object cannot open a connection in this many seconds, it raises a # object cannot open a connection in this many seconds, it raises a
# \Net::OpenTimeout exception. The default value is 60 seconds. # \Net::OpenTimeout exception. The default value is 60 seconds.
attr_accessor :open_timeout attr_accessor :open_timeout
# Number of seconds to wait for one block to be read (via one read(2) # Number of seconds to wait for one block to be read (via one read(2)
# call). Any number may be used, including Floats for fractional # call). Any number may be used, including Floats for fractional
# seconds. If the HTTP object cannot read data in this many seconds, # seconds. If the \HTTP object cannot read data in this many seconds,
# it raises a Net::ReadTimeout exception. The default value is 60 seconds. # it raises a Net::ReadTimeout exception. The default value is 60 seconds.
attr_reader :read_timeout attr_reader :read_timeout
# Number of seconds to wait for one block to be written (via one write(2) # Number of seconds to wait for one block to be written (via one write(2)
# call). Any number may be used, including Floats for fractional # call). Any number may be used, including Floats for fractional
# seconds. If the HTTP object cannot write data in this many seconds, # seconds. If the \HTTP object cannot write data in this many seconds,
# it raises a \Net::WriteTimeout exception. The default value is 60 seconds. # it raises a \Net::WriteTimeout exception. The default value is 60 seconds.
# \Net::WriteTimeout is not raised on Windows. # \Net::WriteTimeout is not raised on Windows.
attr_reader :write_timeout attr_reader :write_timeout
@ -1057,7 +1055,7 @@ module Net #:nodoc:
@write_timeout = sec @write_timeout = sec
end end
# Seconds to wait for 100 Continue response. If the HTTP object does not # Seconds to wait for 100 Continue response. If the \HTTP object does not
# receive a response in this many seconds it sends the request body. The # receive a response in this many seconds it sends the request body. The
# default value is +nil+. # default value is +nil+.
attr_reader :continue_timeout attr_reader :continue_timeout
@ -1078,7 +1076,7 @@ module Net #:nodoc:
# Content-Length headers. For backwards compatibility, the default is true. # Content-Length headers. For backwards compatibility, the default is true.
attr_accessor :ignore_eof attr_accessor :ignore_eof
# Returns true if the HTTP session has been started. # Returns true if the \HTTP session has been started.
def started? def started?
@started @started
end end
@ -1087,7 +1085,7 @@ module Net #:nodoc:
attr_accessor :close_on_empty_response attr_accessor :close_on_empty_response
# Returns true if SSL/TLS is being used with HTTP. # Returns true if SSL/TLS is being used with \HTTP.
def use_ssl? def use_ssl?
@use_ssl @use_ssl
end end
@ -1202,10 +1200,10 @@ module Net #:nodoc:
@socket.io.peer_cert @socket.io.peer_cert
end end
# Opens a TCP connection and HTTP session. # Opens a TCP connection and \HTTP session.
# #
# When this method is called with a block, it passes the \Net::HTTP # When this method is called with a block, it passes the \Net::HTTP
# object to the block, and closes the TCP connection and HTTP session # object to the block, and closes the TCP connection and \HTTP session
# after the block has been executed. # after the block has been executed.
# #
# When called with a block, it returns the return value of the # When called with a block, it returns the return value of the
@ -1345,7 +1343,7 @@ module Net #:nodoc:
end end
private :on_connect private :on_connect
# Finishes the HTTP session and closes the TCP connection. # Finishes the \HTTP session and closes the TCP connection.
# Raises IOError if the session has not been started. # Raises IOError if the session has not been started.
def finish def finish
raise IOError, 'HTTP session not yet started' unless started? raise IOError, 'HTTP session not yet started' unless started?
@ -1373,7 +1371,7 @@ module Net #:nodoc:
@proxy_user = nil @proxy_user = nil
@proxy_pass = nil @proxy_pass = nil
# Creates an HTTP proxy class which behaves like \Net::HTTP, but # Creates an \HTTP proxy class which behaves like \Net::HTTP, but
# performs all access via the specified proxy. # performs all access via the specified proxy.
# #
# This class is obsolete. You may pass these same parameters directly to # This class is obsolete. You may pass these same parameters directly to
@ -1762,7 +1760,7 @@ module Net #:nodoc:
alias put2 request_put #:nodoc: obsolete alias put2 request_put #:nodoc: obsolete
# Sends an HTTP request to the HTTP server. # Sends an \HTTP request to the \HTTP server.
# Also sends a DATA string if +data+ is given. # Also sends a DATA string if +data+ is given.
# #
# Returns a Net::HTTPResponse object. # Returns a Net::HTTPResponse object.
@ -1778,7 +1776,7 @@ module Net #:nodoc:
request r, data request r, data
end end
# Sends an HTTPRequest object +req+ to the HTTP server. # Sends an HTTPRequest object +req+ to the \HTTP server.
# #
# If +req+ is a Net::HTTP::Post or Net::HTTP::Put request containing # If +req+ is a Net::HTTP::Post or Net::HTTP::Put request containing
# data, the data is also sent. Providing data for a Net::HTTP::Head or # data, the data is also sent. Providing data for a Net::HTTP::Head or