diff --git a/lib/net/http.rb b/lib/net/http.rb index e0d35e5..8caec85 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -443,6 +443,23 @@ module Net #:nodoc: BufferedIO end + class << self + def check_convert_type(val, type, meth) + if val.is_a?(type) + return val + elsif val.respond?(meth) + tmp = val.__send__(meth) + return tmp if tmp.is_a?(type) + + raise TypeError, "can't convert %s to %s (%s#%s gives %s)" % + [val.class.name, type.name, val.class.name, method, tmp.class.name] + else + return nil + end + end + private :check_convert_type + end + # creates a new Net::HTTP object and opens its TCP connection and # HTTP session. If the optional block is given, the newly # created Net::HTTP object is passed to it and closed when the @@ -450,8 +467,21 @@ module Net #:nodoc: # is the return value of the block. If no block is given, the # return value of this method is the newly created Net::HTTP object # itself, and the caller is responsible for closing it upon completion. - def HTTP.start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) # :yield: +http+ - new(address, port, p_addr, p_port, p_user, p_pass).start(&block) + def HTTP.start(address, *arg, &block) # :yield: +http+ + arg.pop if opt = check_convert_type(arg[-1], Hash, :to_hash) + port, p_addr, p_port, p_user, p_pass = *arg + port = https_default_port if opt[:use_ssl] && !port + http = new(address, port, p_addr, p_port, p_user, p_pass) + + opt = {verify_mode: OpenSSL::SSL::VERIFY_PEER}.update(opt) if opt[:use_ssl] + http.methods.each do |meth| + /\A(\w+)=\z/ =~ meth or next + key = $1.to_sym + opt.key?(key) or next + http.__send__(meth, opt[key]) + end + + http.start(&block) end class << HTTP