Skip to content

Commit ca8a5bb

Browse files
Shorten the framing documentation.
1 parent c5cf73f commit ca8a5bb

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

‎lib/protocol/http1/connection.rb‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module HTTP1
3434
# HTTP/1.x request line parser:
3535
TOKEN = /[!#$%&'*+\-\.\^_`|~0-9a-zA-Z]+/.freeze
3636
VALID_METHOD = /\A#{TOKEN}\z/.freeze
37-
# The HTTP-version is case-sensitive (RFC 9112 Section 2.3). Only the HTTP/1.x family is accepted; any minor version is treated as HTTP/1.1 (RFC 9110 Section 2.5):
37+
# The HTTP-version is case-sensitive, and only HTTP/1.x is accepted:
3838
HTTP_VERSION = /HTTP\/1\.\d/.freeze
3939
VALID_HTTP_VERSION = /\A#{HTTP_VERSION}\z/.freeze
4040
REQUEST_LINE = /\A(#{TOKEN}) ([^\s]+) (#{HTTP_VERSION})\z/.freeze
@@ -441,7 +441,7 @@ def read_request_line
441441
# @yields {|host, method, path, version, headers, body| ...} if a block is given.
442442
# @returns [Tuple(String, String, String, String, HTTP::Headers, Protocol::HTTP1::Body) | Nil] the host, method, path, version, headers, and body of the request, or `nil` if the connection is closed.
443443
# @raises [ProtocolError] if the connection is not in the idle state.
444-
# @raises [BadRequest] if the request line, headers, or body framing are invalid, e.g. an unsupported HTTP version, a malformed header, or a `transfer-encoding` that cannot be used to delimit the body.
444+
# @raises [BadRequest] if the request line, headers, or body framing are invalid.
445445
def read_request
446446
open!
447447

@@ -503,7 +503,7 @@ def interim_status?(status)
503503
# @returns [Tuple(String, Integer, String, HTTP::Headers, Protocol::HTTP1::Body)] the version, status, reason, headers, and body of the response.
504504
# @raises [ProtocolError] if the connection is not in the open or half-closed local state.
505505
# @raises [EOFError] if the connection is closed.
506-
# @raises [BadRequest] if the status line, headers, or body framing are invalid, e.g. an unsupported HTTP version, a malformed header, or a `transfer-encoding` that cannot be used to delimit the body.
506+
# @raises [BadRequest] if the status line, headers, or body framing are invalid.
507507
def read_response(method)
508508
unless @state == :open or @state == :half_closed_local
509509
raise ProtocolError, "Cannot read response in state: #{@state}!"
@@ -985,7 +985,7 @@ def read_response_body(method, status, headers, version = nil)
985985
# Read the body of the request.
986986
#
987987
# - The `CONNECT` method is used to establish a tunnel, so the body is read until the connection is closed.
988-
# - The `upgrade` header is used to upgrade the connection to a different protocol (typically WebSockets), so the body is read until the connection is closed. It is ignored in HTTP/1.0 requests, as required by RFC 9110 Section 7.8.
988+
# - The `upgrade` header is used to upgrade the connection to a different protocol (typically WebSockets), so the body is read until the connection is closed. It is ignored in HTTP/1.0 requests.
989989
# - Otherwise, the body is read according to {read_body}.
990990
#
991991
# @parameter method [String] the HTTP method.
@@ -1003,9 +1003,7 @@ def read_request_body(method, headers, version = nil)
10031003
return read_tunnel_body
10041004
end
10051005

1006-
# A successful upgrade response implies that the connection will become a tunnel immediately after the empty line that concludes the header fields.
1007-
#
1008-
# A server that receives an Upgrade header field in an HTTP/1.0 request MUST ignore that Upgrade field (RFC 9110 Section 7.8), so the body is framed according to the remaining headers instead.
1006+
# A successful upgrade response implies that the connection will become a tunnel immediately after the empty line that concludes the header fields. An HTTP/1.0 request cannot upgrade (RFC 9110 Section 7.8):
10091007
if headers[UPGRADE] and version != HTTP10
10101008
return read_upgrade_body
10111009
end
@@ -1021,9 +1019,7 @@ def read_request_body(method, headers, version = nil)
10211019
# - Otherwise, if the `content-length` is present, the body is read until the content length is reached.
10221020
# - Otherwise, if `remainder` is true, the body is read until the connection is closed.
10231021
#
1024-
# Framing errors (e.g. `transfer-encoding` in an HTTP/1.0 message, `chunked` applied more than once, or a request whose transfer coding is anything other than exactly `chunked`) are rejected and mark the connection as non-persistent, since the remainder of the stream can no longer be delimited reliably.
1025-
#
1026-
# Rejecting HTTP/1.0 responses with `transfer-encoding` follows RFC 9112 Section 6.1; browsers instead ignore the header and read until the connection is closed. If interoperability with such servers ever requires relaxing this, the correct relaxation is close-delimited reading via {read_remainder_body}, never chunked decoding, since an HTTP/1.0 peer cannot be relied upon to have applied the coding.
1022+
# Invalid framing is rejected and marks the connection as non-persistent, since the rest of the stream can no longer be delimited.
10271023
#
10281024
# @parameter headers [Hash] the headers of the message.
10291025
# @parameter remainder [Boolean] whether to read the remainder of the body, i.e. the message is a response which may be delimited by closing the connection.

0 commit comments

Comments
 (0)