Class: Net::HTTP::ConnectionPool::Connection
- Inherits:
-
Object
- Object
- Net::HTTP::ConnectionPool::Connection
- Defined in:
- lib/net/http/connection_pool/connection.rb
Overview
Represents a HTTP connection. Call #request on a connection like you would with a Net::HTTPSession object.
Getting a Connection object
To get a connection object, you start with a connection pool:
pool = Net::HTTP::ConnectionPool.new
connection = pool.connection_for('domain.com')
#connection_for accepts a number of options to control the connection settings (SSL, proxy, timeouts, etc).
Making Requests
Given a connection object, you call #request. #request yields Net::HTTPResponse objects (when given a block). You should read the response (via #body or #read_body) before the end of the block.
connection.request(Net::HTTP::Get.new('/')) do |resp|
puts resp.body
end
Instance Attribute Summary collapse
- #host ⇒ String readonly
- #pool ⇒ ConnectionPool readonly
- #port ⇒ Integer readonly
- #proxy_address ⇒ String? readonly
- #proxy_password ⇒ String? readonly
- #proxy_port ⇒ Integer? readonly
- #proxy_user ⇒ String? readonly
- #read_timeout ⇒ Numeric?
- #ssl ⇒ Boolean readonly
- #ssl_ca_file ⇒ String? readonly
- #ssl_ca_path ⇒ String? readonly
- #ssl_verify_peer ⇒ Boolean readonly
Instance Method Summary collapse
-
#initialize(pool, host, options = {}) ⇒ Connection
constructor
Use #connection_for to construct Connection objects.
-
#key ⇒ String
Returns a key that can be used to group connections that may share the same HTTP sessions.
-
#proxy? ⇒ Boolean
Returns
true
if this connection proxies requests. -
#request(*args) {|response| ... } ⇒ nil
Makes a single HTTP request.
-
#ssl? ⇒ Boolean
Returns
true
if this connection requires SSL. -
#ssl_verify_peer? ⇒ Boolean
Returns
true
if ssl connections should verify the peer certificate.
Constructor Details
#initialize(pool, host, options = {}) ⇒ Connection
Use Net::HTTP::ConnectionPool#connection_for to construct Net::HTTP::ConnectionPool::Connection objects.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/net/http/connection_pool/connection.rb', line 46 def initialize pool, host, = {} @pool = pool @host = host @port = .key?(:port) ? [:port] : ([:ssl] ? 443 : 80) @ssl = .key?(:ssl) ? [:ssl] : (port == 443) @ssl_verify_peer = .key?(:ssl_verify_peer) ? [:ssl_verify_peer] : true @ssl_ca_file = [:ssl_ca_file] @ssl_ca_path = [:ssl_ca_path] if uri = [: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 = [:proxy_address] @proxy_port = [:proxy_port] @proxy_user = [:proxy_user] @proxy_password = [:proxy_password] end @read_timeout = [:read_timeout] || 60 end |
Instance Attribute Details
#host ⇒ String (readonly)
84 85 86 |
# File 'lib/net/http/connection_pool/connection.rb', line 84 def host @host end |
#pool ⇒ ConnectionPool (readonly)
81 82 83 |
# File 'lib/net/http/connection_pool/connection.rb', line 81 def pool @pool end |
#port ⇒ Integer (readonly)
87 88 89 |
# File 'lib/net/http/connection_pool/connection.rb', line 87 def port @port end |
#proxy_address ⇒ String? (readonly)
102 103 104 |
# File 'lib/net/http/connection_pool/connection.rb', line 102 def proxy_address @proxy_address end |
#proxy_password ⇒ String? (readonly)
111 112 113 |
# File 'lib/net/http/connection_pool/connection.rb', line 111 def proxy_password @proxy_password end |
#proxy_port ⇒ Integer? (readonly)
105 106 107 |
# File 'lib/net/http/connection_pool/connection.rb', line 105 def proxy_port @proxy_port end |
#proxy_user ⇒ String? (readonly)
108 109 110 |
# File 'lib/net/http/connection_pool/connection.rb', line 108 def proxy_user @proxy_user end |
#read_timeout ⇒ Numeric?
114 115 116 |
# File 'lib/net/http/connection_pool/connection.rb', line 114 def read_timeout @read_timeout end |
#ssl ⇒ Boolean (readonly)
90 91 92 |
# File 'lib/net/http/connection_pool/connection.rb', line 90 def ssl @ssl end |
#ssl_ca_file ⇒ String? (readonly)
96 97 98 |
# File 'lib/net/http/connection_pool/connection.rb', line 96 def ssl_ca_file @ssl_ca_file end |
#ssl_ca_path ⇒ String? (readonly)
99 100 101 |
# File 'lib/net/http/connection_pool/connection.rb', line 99 def ssl_ca_path @ssl_ca_path end |
#ssl_verify_peer ⇒ Boolean (readonly)
93 94 95 |
# File 'lib/net/http/connection_pool/connection.rb', line 93 def ssl_verify_peer @ssl_verify_peer end |
Instance Method Details
#key ⇒ String
Returns a key that can be used to group connections that may share the same HTTP sessions.
178 179 180 181 182 183 184 185 186 |
# File 'lib/net/http/connection_pool/connection.rb', line 178 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.
128 129 130 |
# File 'lib/net/http/connection_pool/connection.rb', line 128 def proxy? !!proxy_address end |
#request(*args) {|response| ... } ⇒ nil
You should read the yielded response object before the end of the passed block. Do no save a reference to yielded response objects.
Makes a single HTTP request. The Net::HTTPResponse is yielded to the given block.
pool = Net::HTTP::ConnectionPool.new
connection = pool.connection_for('www.google.com')
connection.request(Net::HTTP::Get.new('/')) do |response|
# yeilds a Net::HTTPResponse object
puts "STATUS CODE: #{response.code}"
puts "HEADERS: #{response.to_hash.inspect}"
puts "BODY:\n#{response.body}"
end
If you want to read the HTTP response body in chunks (useful for large responses you do not want to load into memory), you should pass a block to the #read_body method of the yielded response.
File.open('output.txt', 'w') do |file|
connection.request(Net::HTTP::Get.new('/')) do |response|
response.read_body do |chunk|
file.write(chunk)
end
end
end
If you omit the block when calling #request, you will not be able to read the response. This method never returns the Net::HTTPResponse generated.
This method passes *args to Net::HTTPSession#request. See the Ruby standard lib documentation for more documentation about accepted arguments.
172 173 174 |
# File 'lib/net/http/connection_pool/connection.rb', line 172 def request *args, &block pool.request(self, *args, &block) end |
#ssl? ⇒ Boolean
Returns true
if this connection requires SSL.
117 118 119 |
# File 'lib/net/http/connection_pool/connection.rb', line 117 def ssl? @ssl end |
#ssl_verify_peer? ⇒ Boolean
Returns true
if ssl connections should verify the peer certificate.
123 124 125 |
# File 'lib/net/http/connection_pool/connection.rb', line 123 def ssl_verify_peer? @ssl_verify_peer end |