Skip to content

Commit 5dba537

Browse files
Document BadRequest raised by request and response readers and the HTTP/1.0 transfer-encoding trade-off.
1 parent b7e6bba commit 5dba537

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

‎lib/protocol/http1/connection.rb‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +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.
444445
def read_request
445446
open!
446447

@@ -502,6 +503,7 @@ def interim_status?(status)
502503
# @returns [Tuple(String, Integer, String, HTTP::Headers, Protocol::HTTP1::Body)] the version, status, reason, headers, and body of the response.
503504
# @raises [ProtocolError] if the connection is not in the open or half-closed local state.
504505
# @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.
505507
def read_response(method)
506508
unless @state == :open or @state == :half_closed_local
507509
raise ProtocolError, "Cannot read response in state: #{@state}!"
@@ -938,6 +940,8 @@ def extract_content_length(headers)
938940
# @parameter status [Integer] the HTTP status code.
939941
# @parameter headers [Hash] the headers of the response.
940942
# @parameter version [String | Nil] the HTTP version of the response, if known.
943+
# @returns [Object | Nil] the body, or `nil` if the response has no body.
944+
# @raises [BadRequest] if the `content-length` is invalid or the message framing is invalid, see {read_body}.
941945
def read_response_body(method, status, headers, version = nil)
942946
# RFC 7230 3.3.3
943947
# 1. Any response to a HEAD request and any response with a 1xx
@@ -987,6 +991,8 @@ def read_response_body(method, status, headers, version = nil)
987991
# @parameter method [String] the HTTP method.
988992
# @parameter headers [Hash] the headers of the request.
989993
# @parameter version [String | Nil] the HTTP version of the request, if known.
994+
# @returns [Object | Nil] the body, or `nil` if the request has no body.
995+
# @raises [BadRequest] if the `content-length` is invalid or the message framing is invalid, see {read_body}.
990996
def read_request_body(method, headers, version = nil)
991997
# 2. Any 2xx (Successful) response to a CONNECT request implies that
992998
# the connection will become a tunnel immediately after the empty
@@ -1017,6 +1023,8 @@ def read_request_body(method, headers, version = nil)
10171023
#
10181024
# 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.
10191025
#
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.
1027+
#
10201028
# @parameter headers [Hash] the headers of the message.
10211029
# @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.
10221030
# @parameter version [String | Nil] the HTTP version of the message, if known.

0 commit comments

Comments
 (0)