@@ -1128,29 +1128,61 @@ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_p
11281128 http
11291129 end
11301130
1131+ class << HTTP
1132+ # Allows to set the default configuration that will be used
1133+ # when creating a new connection.
1134+ #
1135+ # Example:
1136+ #
1137+ # Net::HTTP.default_configuration = {
1138+ # read_timeout: 1,
1139+ # write_timeout: 1
1140+ # }
1141+ # http = Net::HTTP.new(hostname)
1142+ # http.open_timeout # => 60
1143+ # http.read_timeout # => 1
1144+ # http.write_timeout # => 1
1145+ #
1146+ attr_accessor :default_configuration
1147+ end
1148+
11311149 # Creates a new \Net::HTTP object for the specified server address,
11321150 # without opening the TCP connection or initializing the \HTTP session.
11331151 # The +address+ should be a DNS hostname or IP address.
11341152 def initialize ( address , port = nil ) # :nodoc:
1153+ defaults = {
1154+ keep_alive_timeout : 2 ,
1155+ close_on_empty_response : false ,
1156+ open_timeout : 60 ,
1157+ read_timeout : 60 ,
1158+ write_timeout : 60 ,
1159+ continue_timeout : nil ,
1160+ max_retries : 1 ,
1161+ debug_output : nil ,
1162+ response_body_encoding : false ,
1163+ ignore_eof : true
1164+ }
1165+ options = defaults . merge ( self . class . default_configuration || { } )
1166+
11351167 @address = address
11361168 @port = ( port || HTTP . default_port )
11371169 @ipaddr = nil
11381170 @local_host = nil
11391171 @local_port = nil
11401172 @curr_http_version = HTTPVersion
1141- @keep_alive_timeout = 2
1173+ @keep_alive_timeout = options [ :keep_alive_timeout ]
11421174 @last_communicated = nil
1143- @close_on_empty_response = false
1175+ @close_on_empty_response = options [ :close_on_empty_response ]
11441176 @socket = nil
11451177 @started = false
1146- @open_timeout = 60
1147- @read_timeout = 60
1148- @write_timeout = 60
1149- @continue_timeout = nil
1150- @max_retries = 1
1151- @debug_output = nil
1152- @response_body_encoding = false
1153- @ignore_eof = true
1178+ @open_timeout = options [ :open_timeout ]
1179+ @read_timeout = options [ :read_timeout ]
1180+ @write_timeout = options [ :write_timeout ]
1181+ @continue_timeout = options [ :continue_timeout ]
1182+ @max_retries = options [ :max_retries ]
1183+ @debug_output = options [ :debug_output ]
1184+ @response_body_encoding = options [ :response_body_encoding ]
1185+ @ignore_eof = options [ :ignore_eof ]
11541186
11551187 @proxy_from_env = false
11561188 @proxy_uri = nil
0 commit comments