Skip to content

Commit 60929d0

Browse files
authored
Add support for maximum line length. (#40)
1 parent de343cc commit 60929d0

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/protocol/http1/connection.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ module HTTP1
4545
VALID_FIELD_NAME = /\A#{FIELD_NAME}\z/.freeze
4646
VALID_FIELD_VALUE = /\A#{FIELD_VALUE}\z/.freeze
4747

48+
DEFAULT_MAXIMUM_LINE_LENGTH = 8192
49+
4850
class Connection
4951
CRLF = "\r\n"
5052
HTTP10 = "HTTP/1.0"
5153
HTTP11 = "HTTP/1.1"
5254

53-
def initialize(stream, persistent: true, state: :idle)
55+
def initialize(stream, persistent: true, state: :idle, maximum_line_length: DEFAULT_MAXIMUM_LINE_LENGTH)
5456
@stream = stream
5557

5658
@persistent = persistent
5759
@state = state
5860

5961
@count = 0
62+
63+
@maximum_line_length = maximum_line_length
6064
end
6165

6266
attr :stream
@@ -282,7 +286,14 @@ def read(length)
282286
end
283287

284288
def read_line?
285-
@stream.gets(CRLF, chomp: true)
289+
line = @stream.gets(CRLF, @maximum_line_length)
290+
291+
unless line.chomp!(CRLF)
292+
# This basically means that the request line, response line, header, or chunked length line is too long.
293+
raise LineLengthError, "Line too long!"
294+
end
295+
296+
return line
286297
end
287298

288299
def read_line

lib/protocol/http1/error.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class Error < HTTP::Error
1414
class ProtocolError < Error
1515
end
1616

17+
class LineLengthError < Error
18+
end
19+
1720
# The request was not able to be parsed correctly, or failed some kind of validation.
1821
class BadRequest < Error
1922
end

0 commit comments

Comments
 (0)