Project

General

Profile

Feature #2579 » http_start.patch

naruse (Yui NARUSE), 01/09/2010 08:34 PM

View differences:

lib/net/http.rb
443 443
      BufferedIO
444 444
    end
445 445

  
446
    class << self
447
      def check_convert_type(val, type, meth)
448
        if val.is_a?(type)
449
          return val
450
        elsif val.respond?(meth)
451
          tmp = val.__send__(meth)
452
          return tmp if tmp.is_a?(type)
453

  
454
          raise TypeError, "can't convert %s to %s (%s#%s gives %s)" %
455
          [val.class.name, type.name, val.class.name, method, tmp.class.name]
456
        else
457
          return nil
458
        end
459
      end
460
      private :check_convert_type
461
    end
462

  
446 463
    # creates a new Net::HTTP object and opens its TCP connection and
447 464
    # HTTP session.  If the optional block is given, the newly
448 465
    # created Net::HTTP object is passed to it and closed when the
......
450 467
    # is the return value of the block.  If no block is given, the
451 468
    # return value of this method is the newly created Net::HTTP object
452 469
    # itself, and the caller is responsible for closing it upon completion.
453
    def HTTP.start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) # :yield: +http+
454
      new(address, port, p_addr, p_port, p_user, p_pass).start(&block)
470
    def HTTP.start(address, *arg, &block) # :yield: +http+
471
      arg.pop if opt = check_convert_type(arg[-1], Hash, :to_hash)
472
      port, p_addr, p_port, p_user, p_pass = *arg
473
      port = https_default_port if opt[:use_ssl] && !port
474
      http = new(address, port, p_addr, p_port, p_user, p_pass)
475

  
476
      opt = {verify_mode: OpenSSL::SSL::VERIFY_PEER}.update(opt) if opt[:use_ssl]
477
      http.methods.each do |meth|
478
        /\A(\w+)=\z/ =~ meth or next
479
        key = $1.to_sym
480
        opt.key?(key) or next
481
        http.__send__(meth, opt[key])
482
      end
483

  
484
      http.start(&block)
455 485
    end
456 486

  
457 487
    class << HTTP