Class: HTTP::Connection
- Inherits:
-
Object
- Object
- HTTP::Connection
- Extended by:
- Forwardable
- Defined in:
- lib/http/connection.rb
Overview
A connection to the HTTP server
Constant Summary collapse
- KEEP_ALIVE =
Allowed values for CONNECTION header
"Keep-Alive"
- CLOSE =
"close"
- BUFFER_SIZE =
Attempt to read this much data
16_384
- HTTP_1_0 =
HTTP/1.0
"1.0"
- HTTP_1_1 =
HTTP/1.1
"1.1"
Instance Attribute Summary collapse
-
#proxy_response_headers ⇒ Object
readonly
Returned after HTTP CONNECT (via proxy).
Instance Method Summary collapse
-
#close ⇒ void
Close the connection.
-
#expired? ⇒ Boolean
Whether our connection has expired.
-
#failed_proxy_connect? ⇒ Boolean
Whenever proxy connect failed.
-
#finish_response ⇒ void
Callback for when we've reached the end of a response.
-
#initialize(req, options) ⇒ Connection
constructor
A new instance of Connection.
-
#keep_alive? ⇒ Boolean
Whether we're keeping the conn alive.
-
#read_headers! ⇒ void
Reads data from socket up until headers are loaded.
-
#readpartial(size = BUFFER_SIZE) ⇒ String?
Read a chunk of the body.
-
#send_request(req) ⇒ nil
Send a request to the server.
Constructor Details
#initialize(req, options) ⇒ Connection
Returns a new instance of Connection.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/http/connection.rb', line 32 def initialize(req, ) @persistent = .persistent? @keep_alive_timeout = .keep_alive_timeout.to_f @pending_request = false @pending_response = false @failed_proxy_connect = false @buffer = "".b @parser = Response::Parser.new @socket = .timeout_class.new(.) @socket.connect(.socket_class, req.socket_host, req.socket_port, .nodelay) send_proxy_connect_request(req) start_tls(req, ) reset_timer rescue IOError, SocketError, SystemCallError => ex raise ConnectionError, "failed to connect: #{ex}", ex.backtrace end |
Instance Attribute Details
#proxy_response_headers ⇒ Object (readonly)
Returned after HTTP CONNECT (via proxy)
27 28 29 |
# File 'lib/http/connection.rb', line 27 def proxy_response_headers @proxy_response_headers end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the connection
124 125 126 127 128 129 |
# File 'lib/http/connection.rb', line 124 def close @socket.close unless @socket.closed? @pending_response = false @pending_request = false end |
#expired? ⇒ Boolean
Whether our connection has expired
139 140 141 |
# File 'lib/http/connection.rb', line 139 def expired? !@conn_expires_at || @conn_expires_at < Time.now end |
#failed_proxy_connect? ⇒ Boolean
Returns whenever proxy connect failed.
62 63 64 |
# File 'lib/http/connection.rb', line 62 def failed_proxy_connect? @failed_proxy_connect end |
#finish_response ⇒ void
This method returns an undefined value.
Callback for when we've reached the end of a response
112 113 114 115 116 117 118 119 120 |
# File 'lib/http/connection.rb', line 112 def finish_response close unless keep_alive? @parser.reset @socket.reset_counter if @socket.respond_to?(:reset_counter) reset_timer @pending_response = false end |
#keep_alive? ⇒ Boolean
Whether we're keeping the conn alive
133 134 135 |
# File 'lib/http/connection.rb', line 133 def keep_alive? !!@keep_alive && !@socket.closed? end |
#read_headers! ⇒ void
This method returns an undefined value.
Reads data from socket up until headers are loaded
101 102 103 104 105 106 107 108 |
# File 'lib/http/connection.rb', line 101 def read_headers! until @parser.headers? result = read_more(BUFFER_SIZE) raise ConnectionError, "couldn't read response headers" if result == :eof end set_keep_alive end |
#readpartial(size = BUFFER_SIZE) ⇒ String?
Read a chunk of the body
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/http/connection.rb', line 86 def readpartial(size = BUFFER_SIZE) return unless @pending_response chunk = @parser.read(size) return chunk if chunk finished = (read_more(size) == :eof) || @parser.finished? chunk = @parser.read(size) finish_response if finished chunk.to_s end |
#send_request(req) ⇒ nil
Send a request to the server
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/http/connection.rb', line 70 def send_request(req) raise StateError, "Tried to send a request while one is pending already. Make sure you read off the body." if @pending_response raise StateError, "Tried to send a request while a response is pending. Make sure you read off the body." if @pending_request @pending_request = true req.stream @socket @pending_response = true @pending_request = false end |