class Async::HTTP::Protocol::HTTP11

def read_body(headers)

def read_body(headers)
	# 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[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 Body::Chunked.new(self)
		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 Body::Remainder.new(@stream)
		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[CONTENT_LENGTH]
		length = Integer(content_length)
		if length >= 0
			return Body::Fixed.new(@stream, length)
		else
			raise BadRequest, "Invalid content length: #{content_length}"
		end
	end
end