Skip to content

Commit 076902f

Browse files
committed
Treat missing CRLF separator after headers as an EOFError
Fix tests that did not have correctly formatted headers. Fixes #140
1 parent ee60354 commit 076902f

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/webrick/httprequest.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,13 @@ def read_request_line(socket)
470470

471471
def read_header(socket)
472472
if socket
473+
end_of_headers = false
474+
473475
while line = read_line(socket)
474-
break if /\A#{CRLF}\z/om =~ line
476+
if line == CRLF
477+
end_of_headers = true
478+
break
479+
end
475480
if (@request_bytes += line.bytesize) > MAX_HEADER_LENGTH
476481
raise HTTPStatus::RequestEntityTooLarge, 'headers too large'
477482
end
@@ -480,6 +485,9 @@ def read_header(socket)
480485
end
481486
@raw_header << line
482487
end
488+
489+
# Allow if @header already set to support chunked trailers
490+
raise HTTPStatus::EOFError unless end_of_headers || @header
483491
end
484492
@header = HTTPUtils::parse_header(@raw_header.join)
485493

test/webrick/test_httprequest.rb

+21
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def test_invalid_content_length_header
8686
msg = <<-_end_of_message_
8787
GET / HTTP/1.1
8888
Content-Length:#{cl}
89+
8990
_end_of_message_
9091
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
9192
assert_raise(WEBrick::HTTPStatus::BadRequest){
@@ -189,6 +190,7 @@ def test_duplicate_content_length_header
189190
GET / HTTP/1.1
190191
Content-Length: 1
191192
Content-Length: 2
193+
192194
_end_of_message_
193195
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
194196
assert_raise(WEBrick::HTTPStatus::BadRequest){
@@ -632,6 +634,25 @@ def test_eof_raised_when_line_is_nil
632634
}
633635
end
634636

637+
def test_eof_raised_with_missing_line_between_headers_and_body
638+
msg = <<-_end_of_message_
639+
GET / HTTP/1.0
640+
_end_of_message_
641+
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
642+
assert_raise(WEBrick::HTTPStatus::EOFError) {
643+
req.parse(StringIO.new(msg.gsub(/^ {6}/, "").gsub("\n", "\r\n")))
644+
}
645+
646+
msg = <<-_end_of_message_
647+
GET / HTTP/1.0
648+
Foo: 1
649+
_end_of_message_
650+
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
651+
assert_raise(WEBrick::HTTPStatus::EOFError) {
652+
req.parse(StringIO.new(msg.gsub(/^ {6}/, "").gsub("\n", "\r\n")))
653+
}
654+
end
655+
635656
def test_cookie_join
636657
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
637658
req.parse(StringIO.new("GET / HTTP/1.1\r\ncookie: a=1\r\ncookie: b=2\r\n\r\n"))

0 commit comments

Comments
 (0)