diff --git a/ChangeLog b/ChangeLog index 67d050326b..18b6e0da4e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Jan 12 14:07:31 2010 NARUSE, Yui + + * lib/net/http.rb (Net::HTTP#start): add hash argument to + set ssl related options. when use_ssl is set default value + of verify_mode is OpenSSL::SSL::VERIFY_PEER. [ruby-dev:40003] + Tue Jan 12 14:53:07 2010 NAKAMURA Usaku * win32/win32.c (init_env): use _wputenv() instead of diff --git a/lib/net/http.rb b/lib/net/http.rb index e0d35e598d..31546d3020 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -444,14 +444,44 @@ module Net #:nodoc: end # creates a new Net::HTTP object and opens its TCP connection and - # HTTP session. If the optional block is given, the newly + # HTTP session. + # + # Argments are following: + # _address_ :: hostname or IP address of the server + # _port_ :: port of the server + # _p_addr_ :: address of proxy + # _p_port_ :: port of proxy + # _p_user_ :: user of proxy + # _p_pass_ :: pass of proxy + # _opt_ :: optional hash + # + # _opt_ sets following values by its accessor. + # The keys are ca_file, ca_path, cert, cert_store, ciphers, + # close_on_empty_response, key, open_timeout, read_timeout, ssl_timeout, + # ssl_version, use_ssl, verify_callback, verify_depth and verify_mode. + # If you set :use_ssl as true, you can use https and default value of + # verify_mode is set as OpenSSL::SSL::VERIFY_PEER. + # + # If the optional block is given, the newly # created Net::HTTP object is passed to it and closed when the # block finishes. In this case, the return value of this method # 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 = Hash.try_convert(arg[-1]) + 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.grep(/\A(\w+)=\z/) do |meth| + key = $1.to_sym + opt.key?(key) or next + http.__send__(meth, opt[key]) + end + + http.start(&block) end class << HTTP