Class: Net::HTTP::ConnectionPool::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/net/http/connection_pool/connection.rb

Overview

A light wrapper around Net::HTTP.

You should not need to construct connection objects yourself.

You receive them as a response to #connection_for.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, host, options = {}) ⇒ Connection

Parameters:

  • pool (ConnectionPool)
  • host (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :port (Integer)

    Which port the connection should use. Defaults to 80, unless :ssl is true, then it defaults to 443.

  • :ssl (Boolean)

    If the connection should be made over SSL. Defaults to false, unless :port is 443, then it defaults to true.

  • :ssl_verify_peer (Boolean) — default: true

    If true, ssl connections will verify peer certificates. This should only ever be set false false for debugging purposes.

  • :ssl_ca_file (String)

    Full path to the SSL certificate authority bundle file that should be used when verifying peer certificates. If you do not pass :ssl_ca_file or :ssl_ca_path the the system default will be used if available.

  • :ssl_ca_path (String)

    Full path of the directory that contains the unbundled SSL certificate authority files for verifying peer certificates. If you do not pass :ssl_ca_file or :ssl_ca_path the the system default will be used if available.

  • :proxy_uri (URI::HTTP, String) — default: nil

    A URI string or URI::HTTP for a proxy reqeusts should be made through. You should not pass both :proxy_uri with any of the other proxy options.

    :proxy_uri => 'http://user:[email protected]:80'
    
  • :proxy_address (String)
  • :proxy_port (String)
  • :proxy_user (String)
  • :proxy_password (String)

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/net/http/connection_pool/connection.rb', line 29

def initialize pool, host, options = {}

  @pool = pool

  @host = host

  @port = options.key?(:port) ? options[:port] : (options[:ssl] ? 443 : 80)

  @ssl = options.key?(:ssl) ? options[:ssl] : (port == 443) 

  @ssl_verify_peer = options.key?(:ssl_verify_peer) ?
    options[:ssl_verify_peer] : true

  @ssl_ca_file = options[:ssl_ca_file]

  @ssl_ca_path = options[:ssl_ca_path]

  if uri = options[:proxy_uri]
    uri = URI.parse(uri) if uri.is_a?(String)
    @proxy_address = uri.host
    @proxy_port = uri.port
    @proxy_user = uri.user
    @proxy_password = uri.password
  else
    @proxy_address = options[:proxy_address]
    @proxy_port = options[:proxy_port]
    @proxy_user = options[:proxy_user]
    @proxy_password = options[:proxy_password]
  end

  @read_timeout = options[:read_timeout] || 60

end

Instance Attribute Details

#hostString (readonly)

Returns:

  • (String)

67
68
69
# File 'lib/net/http/connection_pool/connection.rb', line 67

def host
  @host
end

#poolConnectionPool (readonly)

Returns:


64
65
66
# File 'lib/net/http/connection_pool/connection.rb', line 64

def pool
  @pool
end

#portInteger (readonly)

Returns:

  • (Integer)

70
71
72
# File 'lib/net/http/connection_pool/connection.rb', line 70

def port
  @port
end

#proxy_addressString? (readonly)

Returns:

  • (String, nil)

85
86
87
# File 'lib/net/http/connection_pool/connection.rb', line 85

def proxy_address
  @proxy_address
end

#proxy_passwordString? (readonly)

Returns:

  • (String, nil)

94
95
96
# File 'lib/net/http/connection_pool/connection.rb', line 94

def proxy_password
  @proxy_password
end

#proxy_portInteger? (readonly)

Returns:

  • (Integer, nil)

88
89
90
# File 'lib/net/http/connection_pool/connection.rb', line 88

def proxy_port
  @proxy_port
end

#proxy_userString? (readonly)

Returns:

  • (String, nil)

91
92
93
# File 'lib/net/http/connection_pool/connection.rb', line 91

def proxy_user
  @proxy_user
end

#read_timeoutNumeric?

Returns:

  • (Numeric, nil)

97
98
99
# File 'lib/net/http/connection_pool/connection.rb', line 97

def read_timeout
  @read_timeout
end

#sslBoolean (readonly)

Returns:

  • (Boolean)

73
74
75
# File 'lib/net/http/connection_pool/connection.rb', line 73

def ssl
  @ssl
end

#ssl_ca_fileString? (readonly)

Returns:

  • (String, nil)

79
80
81
# File 'lib/net/http/connection_pool/connection.rb', line 79

def ssl_ca_file
  @ssl_ca_file
end

#ssl_ca_pathString? (readonly)

Returns:

  • (String, nil)

82
83
84
# File 'lib/net/http/connection_pool/connection.rb', line 82

def ssl_ca_path
  @ssl_ca_path
end

#ssl_verify_peerBoolean (readonly)

Returns:

  • (Boolean)

76
77
78
# File 'lib/net/http/connection_pool/connection.rb', line 76

def ssl_verify_peer
  @ssl_verify_peer
end

Instance Method Details

#keyString

Returns a key that can be used to group connections that connection to the same host.

Returns:

  • (String)

    Returns a key that can be used to group connections that connection to the same host.


121
122
123
124
125
126
127
128
129
# File 'lib/net/http/connection_pool/connection.rb', line 121

def key
  @key ||= begin
    %w(
      host port 
      ssl ssl_verify_peer ssl_ca_file ssl_ca_path
      proxy_address proxy_port proxy_user proxy_password
    ).map{|part| send(part).to_s }.join(":")
  end
end

#proxy?Boolean

Returns true if this connection proxies requests.

Returns:

  • (Boolean)

    Returns true if this connection proxies requests.


111
112
113
# File 'lib/net/http/connection_pool/connection.rb', line 111

def proxy?
  !!proxy_address
end

#request(*args, &block) ⇒ Object


115
116
117
# File 'lib/net/http/connection_pool/connection.rb', line 115

def request *args, &block
  pool.request(self, *args, &block)
end

#ssl?Boolean

Returns true if this connection requires SSL.

Returns:

  • (Boolean)

    Returns true if this connection requires SSL.


100
101
102
# File 'lib/net/http/connection_pool/connection.rb', line 100

def ssl?
  @ssl
end

#ssl_verify_peer?Boolean

Returns true if ssl connections should verify the peer certificate.

Returns:

  • (Boolean)

    Returns true if ssl connections should verify the peer certificate.


106
107
108
# File 'lib/net/http/connection_pool/connection.rb', line 106

def ssl_verify_peer?
  @ssl_verify_peer
end