Skip to content

Commit

Permalink
Add support for maximum line length.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 14, 2024
1 parent de343cc commit 69c735a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit 69c735a

Please sign in to comment.