Method: Gem::Net::HTTP::Persistent#connection_for

Defined in:
lib/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb

#connection_for(uri) ⇒ Object

Creates a new connection for uri



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb', line 593

def connection_for uri
  use_ssl = uri.scheme.downcase == 'https'

  net_http_args = [uri.hostname, uri.port]

  # I'm unsure if uri.host or uri.hostname should be checked against
  # the proxy bypass list.
  if @proxy_uri and not proxy_bypass? uri.host, uri.port then
    net_http_args.concat @proxy_args
  else
    net_http_args.concat [nil, nil, nil, nil]
  end

  connection = @pool.checkout net_http_args

  http = connection.http

  connection.ressl @ssl_generation if
    connection.ssl_generation != @ssl_generation

  if not http.started? then
    ssl   http if use_ssl
    start http
  elsif expired? connection then
    reset connection
  end

  http.keep_alive_timeout = @idle_timeout  if @idle_timeout
  http.max_retries        = @max_retries   if http.respond_to?(:max_retries=)
  http.read_timeout       = @read_timeout  if @read_timeout
  http.write_timeout      = @write_timeout if
    @write_timeout && http.respond_to?(:write_timeout=)

  return yield connection
rescue Errno::ECONNREFUSED
  if http.proxy?
    address = http.proxy_address
    port    = http.proxy_port
  else
    address = http.address
    port    = http.port
  end

  raise Error, "connection refused: #{address}:#{port}"
rescue Errno::EHOSTDOWN
  if http.proxy?
    address = http.proxy_address
    port    = http.proxy_port
  else
    address = http.address
    port    = http.port
  end

  raise Error, "host down: #{address}:#{port}"
ensure
  @pool.checkin net_http_args
end