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
9 changes: 6 additions & 3 deletions lib/net/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,14 @@ def each_response_header(sock)
line = read_line(sock, MAX_RESPONSE_HEADER_LENGTH, true)
remaining -= line.bytesize
raise Net::HTTPBadResponse, 'response header too large' if remaining < 0
line = line.sub(/\s+\z/, '')
line = line.chomp
break if line.empty?
if line[0] == ?\s or line[0] == ?\t and value
value << ' ' unless value.empty?
value << line.strip
folded = line.strip
unless folded.empty?
value << ' ' unless value.empty?
value << folded
end
else
yield key, value if key
key, value = line.strip.split(/\s*:\s*/, 2)
Expand Down
34 changes: 34 additions & 0 deletions test/net/http/test_httpresponse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,40 @@ def test_multiline_header
assert_equal('XXX YYY', res['x-bar'])
end

def test_multiline_header_whitespace_only
io = dummy_io(<<EOS)
HTTP/1.1 200 OK
Content-Length: 5

X-After: value

hello
EOS
res = Net::HTTPResponse.read_new(io)
# Whitespace only line is ignored
assert_equal('5', res['content-length'])
assert_equal('value', res['x-after'])
body = nil
res.reading_body io, true do
body = res.read_body
end
assert_equal('hello', body)
end

def test_multiline_header_no_preceding_header
io = dummy_io(<<EOS)
HTTP/1.1 200 OK

Content-Length: 5

hello
EOS
e = assert_raise(Net::HTTPBadResponse) do
Net::HTTPResponse.read_new(io)
end
assert_equal 'wrong header line format', e.message
end

def test_read_body
io = dummy_io(<<EOS)
HTTP/1.1 200 OK
Expand Down
Loading