Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/net/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ def each_response_header(sock)
key = value = nil
remaining = MAX_RESPONSE_HEADER_LENGTH
while true
line = read_line(sock, MAX_RESPONSE_HEADER_LENGTH, true)
# A missing blank line is a truncated message, not the end of the
# headers (net-http#377). ignore_eof stays false.
line = read_line(sock, MAX_RESPONSE_HEADER_LENGTH)
remaining -= line.bytesize
raise Net::HTTPBadResponse, 'response header too large' if remaining < 0
line = line.chomp
Expand All @@ -203,6 +205,8 @@ def each_response_header(sock)
end
end
yield key, value if key
rescue EOFError
raise Net::HTTPBadResponse, 'end of file reached before the end of the headers'
end
end

Expand Down
16 changes: 16 additions & 0 deletions test/net/http/test_httpresponse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ def test_response_header_too_large
assert_equal 'response header too large', e.message
end

def test_eof_before_the_end_of_the_headers_is_a_protocol_error
io = dummy_io("HTTP/1.1 200 OK\nContent-Type: application/json\n")
e = assert_raise(Net::HTTPBadResponse) do
Net::HTTPResponse.read_new(io)
end
assert_equal 'end of file reached before the end of the headers', e.message
end

def test_eof_in_the_middle_of_a_header_line_is_a_protocol_error
io = dummy_io("HTTP/1.1 200 OK\nX-Classification: malic")
e = assert_raise(Net::HTTPBadResponse) do
Net::HTTPResponse.read_new(io)
end
assert_equal 'end of file reached before the end of the headers', e.message
end

def test_response_header_within_limit
big_value = 'a' * (Net::HTTPHeader::MAX_FIELD_LENGTH - 100)
count = (Net::HTTPResponse::MAX_RESPONSE_HEADER_LENGTH / big_value.bytesize) - 1
Expand Down