Class: Protocol::HTTP1::Connection
- Inherits:
-
Object
- Object
- Protocol::HTTP1::Connection
- Defined in:
- lib/protocol/http1/connection.rb
Constant Summary collapse
- CRLF =
"\r\n".freeze
- HTTP10 =
"HTTP/1.0".freeze
- HTTP11 =
"HTTP/1.1".freeze
- HEAD =
"HEAD".freeze
- CONNECT =
"CONNECT".freeze
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
The number of requests processed.
-
#persistent ⇒ Object
readonly
Whether the connection is persistent.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#upgrade ⇒ Object
readonly
Whether the connection has been upgraded, and to what.
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection and underlying stream.
- #each_line ⇒ Object
-
#hijack! ⇒ IO
Effectively close the connection and return the underlying IO.
-
#initialize(stream, persistent = true) ⇒ Connection
constructor
A new instance of Connection.
- #persistent?(version, headers) ⇒ Boolean
- #read_body(headers, remainder = false) ⇒ Object
- #read_chunk ⇒ Object
- #read_chunked_body ⇒ Object
- #read_fixed_body(length) ⇒ Object
- #read_headers ⇒ Object
- #read_line ⇒ Object
- #read_remainder_body ⇒ Object
- #read_request ⇒ Object
- #read_request_body(headers) ⇒ Object
- #read_response(method) ⇒ Object
- #read_response_body(method, status, headers) ⇒ Object
- #read_tunnel_body ⇒ Object
- #upgrade!(protocol) ⇒ Object
- #upgrade?(headers) ⇒ Boolean
- #write_body(body, chunked = true, head = false) ⇒ Object
- #write_body_and_close(body, head) ⇒ Object
- #write_chunked_body(body, head) ⇒ Object
-
#write_connection_header(version) ⇒ Object
Write the appropriate header for connection persistence.
- #write_empty_body(body) ⇒ Object
- #write_fixed_length_body(body, length, head) ⇒ Object
- #write_headers(headers) ⇒ Object
- #write_request(authority, method, path, version, headers) ⇒ Object
- #write_response(version, status, headers, body = nil, head = false) ⇒ Object
- #write_upgrade_body(body = nil) ⇒ Object
Constructor Details
#initialize(stream, persistent = true) ⇒ Connection
Returns a new instance of Connection.
44 45 46 47 48 49 50 51 |
# File 'lib/protocol/http1/connection.rb', line 44 def initialize(stream, persistent = true) @stream = stream @persistent = persistent @upgrade = nil @count = 0 end |
Instance Attribute Details
#count ⇒ Object (readonly)
The number of requests processed.
62 63 64 |
# File 'lib/protocol/http1/connection.rb', line 62 def count @count end |
#persistent ⇒ Object (readonly)
Whether the connection is persistent.
56 57 58 |
# File 'lib/protocol/http1/connection.rb', line 56 def persistent @persistent end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
53 54 55 |
# File 'lib/protocol/http1/connection.rb', line 53 def stream @stream end |
#upgrade ⇒ Object (readonly)
Whether the connection has been upgraded, and to what.
59 60 61 |
# File 'lib/protocol/http1/connection.rb', line 59 def upgrade @upgrade end |
Instance Method Details
#close ⇒ Object
Close the connection and underlying stream.
118 119 120 |
# File 'lib/protocol/http1/connection.rb', line 118 def close @stream.close end |
#each_line ⇒ Object
144 145 146 147 148 |
# File 'lib/protocol/http1/connection.rb', line 144 def each_line while line = read_line yield line end end |
#hijack! ⇒ IO
Effectively close the connection and return the underlying IO.
109 110 111 112 113 114 115 |
# File 'lib/protocol/http1/connection.rb', line 109 def hijack! @persistent = false @stream.flush return @stream end |
#persistent?(version, headers) ⇒ Boolean
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/protocol/http1/connection.rb', line 70 def persistent?(version, headers) if version == HTTP10 if connection = headers[CONNECTION] return connection.include?(KEEP_ALIVE) else return false end else if connection = headers[CONNECTION] return !connection.include?(CLOSE) else return true end end end |
#read_body(headers, remainder = false) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/protocol/http1/connection.rb', line 382 def read_body(headers, remainder = false) # 3. If a Transfer-Encoding header field is present and the chunked # transfer coding (Section 4.1) is the final encoding, the message # body length is determined by reading and decoding the chunked # data until the transfer coding indicates the data is complete. if transfer_encoding = headers.delete(TRANSFER_ENCODING) # If a message is received with both a Transfer-Encoding and a # Content-Length header field, the Transfer-Encoding overrides the # Content-Length. Such a message might indicate an attempt to # perform request smuggling (Section 9.5) or response splitting # (Section 9.4) and ought to be handled as an error. A sender MUST # remove the received Content-Length field prior to forwarding such # a message downstream. if headers[CONTENT_LENGTH] raise BadRequest, "Message contains both transfer encoding and content length!" end if transfer_encoding.last == CHUNKED return read_chunked_body else # If a Transfer-Encoding header field is present in a response and # the chunked transfer coding is not the final encoding, the # message body length is determined by reading the connection until # it is closed by the server. If a Transfer-Encoding header field # is present in a request and the chunked transfer coding is not # the final encoding, the message body length cannot be determined # reliably; the server MUST respond with the 400 (Bad Request) # status code and then close the connection. return read_remainder_body end end # 5. If a valid Content-Length header field is present without # Transfer-Encoding, its decimal value defines the expected message # body length in octets. If the sender closes the connection or # the recipient times out before the indicated number of octets are # received, the recipient MUST consider the message to be # incomplete and close the connection. if content_length = headers.delete(CONTENT_LENGTH) length = Integer(content_length) if length > 0 return read_fixed_body(length) elsif length == 0 return nil else raise BadRequest, "Invalid content length: #{content_length}" end end if remainder # 7. Otherwise, this is a response message without a declared message # body length, so the message body length is determined by the # number of octets received prior to the server closing the # connection. return read_remainder_body end end |
#read_chunk ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/protocol/http1/connection.rb', line 198 def read_chunk length = self.read_line.to_i(16) if length == 0 self.read_line return nil end # Read the data: chunk = @stream.read(length) # Consume the trailing CRLF: @stream.read(2) return chunk end |
#read_chunked_body ⇒ Object
327 328 329 330 331 332 333 334 335 336 |
# File 'lib/protocol/http1/connection.rb', line 327 def read_chunked_body buffer = String.new.b while chunk = read_chunk buffer << chunk chunk.clear end return buffer end |
#read_fixed_body(length) ⇒ Object
338 339 340 |
# File 'lib/protocol/http1/connection.rb', line 338 def read_fixed_body(length) @stream.read(length) end |
#read_headers ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/protocol/http1/connection.rb', line 184 def read_headers fields = [] self.each_line do |line| if line =~ /^([a-zA-Z\-\d]+):\s*(.+?)\s*$/ fields << [$1, $2] else break end end return HTTP::Headers.new(fields) end |
#read_line ⇒ Object
150 151 152 |
# File 'lib/protocol/http1/connection.rb', line 150 def read_line @stream.gets(CRLF, chomp: true) or raise EOFError end |
#read_remainder_body ⇒ Object
346 347 348 |
# File 'lib/protocol/http1/connection.rb', line 346 def read_remainder_body @stream.read end |
#read_request ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/protocol/http1/connection.rb', line 154 def read_request method, path, version = read_line.split(/\s+/, 3) headers = read_headers @persistent = persistent?(version, headers) @upgrade = upgrade?(headers) body = read_request_body(headers) @count += 1 return headers.delete(HOST), method, path, version, headers, body end |
#read_request_body(headers) ⇒ Object
376 377 378 379 380 |
# File 'lib/protocol/http1/connection.rb', line 376 def read_request_body(headers) # 6. If this is a request message and none of the above are true, then # the message body length is zero (no message body is present). return read_body(headers) end |
#read_response(method) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/protocol/http1/connection.rb', line 168 def read_response(method) version, status, reason = read_line.split(/\s+/, 3) status = Integer(status) headers = read_headers @persistent = persistent?(version, headers) body = read_response_body(method, status, headers) @count += 1 return version, status, reason, headers, body end |
#read_response_body(method, status, headers) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/protocol/http1/connection.rb', line 353 def read_response_body(method, status, headers) # RFC 7230 3.3.3 # 1. Any response to a HEAD request and any response with a 1xx # (Informational), 204 (No Content), or 304 (Not Modified) status # code is always terminated by the first empty line after the # header fields, regardless of the header fields present in the # message, and thus cannot contain a message body. if method == "HEAD" or (status >= 100 and status < 200) or status == 204 or status == 304 return nil end # 2. Any 2xx (Successful) response to a CONNECT request implies that # the connection will become a tunnel immediately after the empty # line that concludes the header fields. A client MUST ignore any # Content-Length or Transfer-Encoding header fields received in # such a message. if method == "CONNECT" and status == 200 return read_tunnel_body end return read_body(headers, true) end |
#read_tunnel_body ⇒ Object
342 343 344 |
# File 'lib/protocol/http1/connection.rb', line 342 def read_tunnel_body read_remainder_body end |
#upgrade!(protocol) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/protocol/http1/connection.rb', line 86 def upgrade!(protocol) @persistent = false @upgrade = protocol return @stream end |
#upgrade?(headers) ⇒ Boolean
64 65 66 67 68 |
# File 'lib/protocol/http1/connection.rb', line 64 def upgrade?(headers) if upgrade = headers[UPGRADE] return upgrade.first end end |
#write_body(body, chunked = true, head = false) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/protocol/http1/connection.rb', line 310 def write_body(body, chunked = true, head = false) if body.nil? or body.empty? write_empty_body(body) elsif body.respond_to?(:call) write_upgrade_body(body) elsif length = body.length write_fixed_length_body(body, length, head) elsif @persistent and chunked # We specifically ensure that non-persistent connections do not use chunked response, so that hijacking works as expected. write_chunked_body(body, head) else write_body_and_close(body, head) end @stream.flush end |
#write_body_and_close(body, head) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/protocol/http1/connection.rb', line 292 def write_body_and_close(body, head) # We can't be persistent because we don't know the data length: @persistent = false @stream.write("\r\n") @stream.flush if head body.close if body.respond_to?(:close) else body.each do |chunk| @stream.write(chunk) @stream.flush end end @stream.close_write end |
#write_chunked_body(body, head) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/protocol/http1/connection.rb', line 269 def write_chunked_body(body, head) @stream.write("transfer-encoding: chunked\r\n\r\n") @stream.flush if head body.close if body.respond_to?(:close) return end body.each do |chunk| next if chunk.size == 0 @stream.write("#{chunk.bytesize.to_s(16).upcase}\r\n") @stream.write(chunk) @stream.write(CRLF) @stream.flush end @stream.write("0\r\n\r\n") @stream.flush end |
#write_connection_header(version) ⇒ Object
Write the appropriate header for connection persistence.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/protocol/http1/connection.rb', line 95 def write_connection_header(version) if @upgrade @stream.write("connection: upgrade\r\nupgrade: #{@upgrade}\r\n") else if version == HTTP10 @stream.write("connection: keep-alive\r\n") if @persistent else @stream.write("connection: close\r\n") unless @persistent end end end |
#write_empty_body(body) ⇒ Object
232 233 234 235 236 237 238 239 |
# File 'lib/protocol/http1/connection.rb', line 232 def write_empty_body(body) @stream.write("content-length: 0\r\n\r\n") @stream.flush if body body.close if body.respond_to?(:close) end end |
#write_fixed_length_body(body, length, head) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/protocol/http1/connection.rb', line 241 def write_fixed_length_body(body, length, head) @stream.write("content-length: #{length}\r\n\r\n") @stream.flush if head body.close if body.respond_to?(:close) return end chunk_length = 0 body.each do |chunk| chunk_length += chunk.bytesize if chunk_length > length raise Error, "Trying to write #{chunk_length} bytes, but content length was #{length} bytes!" end @stream.write(chunk) end @stream.flush if chunk_length != length raise Error, "Wrote #{chunk_length} bytes, but content length was #{length} bytes!" end end |
#write_headers(headers) ⇒ Object
138 139 140 141 142 |
# File 'lib/protocol/http1/connection.rb', line 138 def write_headers(headers) headers.each do |name, value| @stream.write("#{name}: #{value}\r\n") end end |
#write_request(authority, method, path, version, headers) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/protocol/http1/connection.rb', line 122 def write_request(, method, path, version, headers) @stream.write("#{method} #{path} #{version}\r\n") @stream.write("host: #{authority}\r\n") write_headers(headers) write_connection_header(version) end |
#write_response(version, status, headers, body = nil, head = false) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/protocol/http1/connection.rb', line 130 def write_response(version, status, headers, body = nil, head = false) @stream.write("#{version} #{status}\r\n") write_headers(headers) write_connection_header(version) write_body(body, version == HTTP11, head) end |
#write_upgrade_body(body = nil) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/protocol/http1/connection.rb', line 216 def write_upgrade_body(body = nil) # Any time we are potentially closing the stream, we should ensure no further requests are processed: @persistent = false @stream.write("\r\n") @stream.flush return @stream unless body begin body.call(@stream) rescue @stream.close_write end end |