Skip to content

Commit

Permalink
Close connection if the request has not been consumed entirely, don't…
Browse files Browse the repository at this point in the history
… skip any bytes
  • Loading branch information
straight-shoota committed Dec 30, 2018
1 parent 2f9d571 commit 2a5edbc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 62 deletions.
42 changes: 2 additions & 40 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -715,44 +715,6 @@ module HTTP
))
end

it "skips body with known length" do
processor = HTTP::Server::RequestProcessor.new do |context|
context.response.content_type = "text/plain"
context.response.puts "Hello world\r"
end

input = IO::Memory.new(requestize(<<-REQUEST
POST / HTTP/1.1
Content-Length: 7
hello
POST / HTTP/1.1
Content-Length: 7
hello
REQUEST
))
output = IO::Memory.new
processor.process(input, output)
output.rewind
output.gets_to_end.should eq(requestize(<<-RESPONSE
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/plain
Content-Length: 13
Hello world
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/plain
Content-Length: 13
Hello world
RESPONSE
))
end

it "fail if body is not consumed" do
processor = HTTP::Server::RequestProcessor.new do |context|
context.response.content_type = "text/plain"
Expand Down Expand Up @@ -827,9 +789,9 @@ module HTTP

input = IO::Memory.new(requestize(<<-REQUEST
POST / HTTP/1.1
Content-Length: 16387
Content-Length: 4
#{"0" * 16_384}1
1
POST / HTTP/1.1
Content-Length: 7
Expand Down
28 changes: 6 additions & 22 deletions src/http/server/request_processor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,17 @@ class HTTP::Server::RequestProcessor
break unless HTTP.keep_alive?(response)

# The request body is either FixedLengthContent or ChunkedContent.
# In case it has not entirely been consumed by the handler, try to
# skip to the end. If the request is larger than maxmum skippable size,
# we close the connection even if keep alive was requested.
# In case it has not entirely been consumed by the handler, the connection is
# closed the connection even if keep alive was requested.
case body = request.body
when FixedLengthContent
if body.read_remaining > 16_384
# Close the connection if remaining length exceeds the maximum skipable size.
if body.read_remaining > 0
# Close the connection if there are bytes remaining
break
else
body.skip_to_end
end
when ChunkedContent
# Try to read maximum skipable number of bytes.
# Close the connection if the IO has still more to read.
break unless skip_to_end(body)
# Close the connection if the IO has still bytes to read.
break unless body.read_byte.nil?
end
end
rescue ex : Errno
Expand All @@ -86,16 +82,4 @@ class HTTP::Server::RequestProcessor
end
end
end

# Reads and discards bytes from `io` until there are no more bytes.
# If there are more than 16_384 bytes to be read from the IO, it returns `false`.
private def skip_to_end(io : IO) : Bool
buffer = uninitialized UInt8[4096]

4.times do
return true if io.read(buffer.to_slice) < 4096
end

false
end
end

0 comments on commit 2a5edbc

Please sign in to comment.