Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to configure default settings for new connections #177

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1091,29 +1091,61 @@ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_p
http
end

class << HTTP
# Allows to set the default configuration that will be used
# when creating a new connection.
#
# Example:
#
# Net::HTTP.default_configuration = {
# read_timeout: 1,
# write_timeout: 1
# }
# http = Net::HTTP.new(hostname)
# http.open_timeout # => 60
# http.read_timeout # => 1
# http.write_timeout # => 1
#
attr_accessor :default_configuration
end

# Creates a new \Net::HTTP object for the specified server address,
# without opening the TCP connection or initializing the \HTTP session.
# The +address+ should be a DNS hostname or IP address.
def initialize(address, port = nil) # :nodoc:
defaults = {
keep_alive_timeout: 2,
close_on_empty_response: false,
open_timeout: 60,
read_timeout: 60,
write_timeout: 60,
continue_timeout: nil,
max_retries: 1,
debug_output: nil,
response_body_encoding: false,
ignore_eof: true
}
options = defaults.merge(self.class.default_configuration || {})

@address = address
@port = (port || HTTP.default_port)
@ipaddr = nil
@local_host = nil
@local_port = nil
@curr_http_version = HTTPVersion
@keep_alive_timeout = 2
@keep_alive_timeout = options[:keep_alive_timeout]
@last_communicated = nil
@close_on_empty_response = false
@close_on_empty_response = options[:close_on_empty_response]
@socket = nil
@started = false
@open_timeout = 60
@read_timeout = 60
@write_timeout = 60
@continue_timeout = nil
@max_retries = 1
@debug_output = nil
@response_body_encoding = false
@ignore_eof = true
@open_timeout = options[:open_timeout]
@read_timeout = options[:read_timeout]
@write_timeout = options[:write_timeout]
@continue_timeout = options[:continue_timeout]
@max_retries = options[:max_retries]
@debug_output = options[:debug_output]
@response_body_encoding = options[:response_body_encoding]
@ignore_eof = options[:ignore_eof]

@proxy_from_env = false
@proxy_uri = nil
Expand Down
12 changes: 12 additions & 0 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ def host.to_str; raise SocketError, "open failure"; end
end
end

def test_default_configuration
Net::HTTP.default_configuration = { open_timeout: 5 }
http = Net::HTTP.new 'hostname.example'
assert_equal 5, http.open_timeout
assert_equal 60, http.read_timeout

http.open_timeout = 10
assert_equal 10, http.open_timeout
ensure
Net::HTTP.default_configuration = nil
end

end

module TestNetHTTP_version_1_1_methods
Expand Down