Skip to content
Merged
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
15 changes: 13 additions & 2 deletions lib/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ module HTTP1
VALID_FIELD_NAME = /\A#{FIELD_NAME}\z/.freeze
VALID_FIELD_VALUE = /\A#{FIELD_VALUE}\z/.freeze

DEFAULT_MAXIMUM_LINE_LENGTH = 8192

class Connection
CRLF = "\r\n"
HTTP10 = "HTTP/1.0"
HTTP11 = "HTTP/1.1"

def initialize(stream, persistent: true, state: :idle)
def initialize(stream, persistent: true, state: :idle, maximum_line_length: DEFAULT_MAXIMUM_LINE_LENGTH)
@stream = stream

@persistent = persistent
@state = state

@count = 0

@maximum_line_length = maximum_line_length
end

attr :stream
Expand Down Expand Up @@ -282,7 +286,14 @@ def read(length)
end

def read_line?
@stream.gets(CRLF, chomp: true)
line = @stream.gets(CRLF, @maximum_line_length)

unless line.chomp!(CRLF)
# This basically means that the request line, response line, header, or chunked length line is too long.
raise LineLengthError, "Line too long!"
end

return line
end

def read_line
Expand Down
3 changes: 3 additions & 0 deletions lib/protocol/http1/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Error < HTTP::Error
class ProtocolError < Error
end

class LineLengthError < Error
end

# The request was not able to be parsed correctly, or failed some kind of validation.
class BadRequest < Error
end
Expand Down
Loading