mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Merge pull request #451 from pwiebe/status_408
Add status 408 for when server times out waiting for body content.
This commit is contained in:
commit
491cffffda
6 changed files with 33 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -11,3 +11,4 @@ pkg
|
|||
tmp
|
||||
.rbx/
|
||||
Gemfile.lock
|
||||
.idea/
|
||||
|
|
|
@ -59,6 +59,10 @@ module Puma
|
|||
env[HIJACK_IO] ||= @io
|
||||
end
|
||||
|
||||
def in_data_phase
|
||||
!@read_header
|
||||
end
|
||||
|
||||
def set_timeout(val)
|
||||
@timeout_at = Time.now + val
|
||||
end
|
||||
|
@ -100,6 +104,7 @@ module Puma
|
|||
EmptyBody = NullIO.new
|
||||
|
||||
def setup_body
|
||||
@in_data_phase = true
|
||||
body = @parser.body
|
||||
cl = @env[CONTENT_LENGTH]
|
||||
|
||||
|
@ -267,6 +272,13 @@ module Puma
|
|||
end
|
||||
end
|
||||
|
||||
def write_408
|
||||
begin
|
||||
@io << ERROR_408_RESPONSE
|
||||
rescue StandardError
|
||||
end
|
||||
end
|
||||
|
||||
def write_500
|
||||
begin
|
||||
@io << ERROR_500_RESPONSE
|
||||
|
|
|
@ -59,6 +59,9 @@ module Puma
|
|||
# The standard empty 404 response for bad requests. Use Error4040Handler for custom stuff.
|
||||
ERROR_404_RESPONSE = "HTTP/1.1 404 Not Found\r\nConnection: close\r\nServer: Puma #{PUMA_VERSION}\r\n\r\nNOT FOUND".freeze
|
||||
|
||||
# The standard empty 408 response for requests that timed out.
|
||||
ERROR_408_RESPONSE = "HTTP/1.1 408 Request Timeout\r\nConnection: close\r\nServer: Puma #{PUMA_VERSION}\r\n\r\n".freeze
|
||||
|
||||
CONTENT_LENGTH = "CONTENT_LENGTH".freeze
|
||||
|
||||
# Indicate that there was an internal error, obviously.
|
||||
|
|
|
@ -98,8 +98,9 @@ module Puma
|
|||
|
||||
while @timeouts.first.timeout_at < now
|
||||
c = @timeouts.shift
|
||||
sockets.delete c
|
||||
c.write_408 if c.in_data_phase
|
||||
c.close
|
||||
sockets.delete c
|
||||
|
||||
break if @timeouts.empty?
|
||||
end
|
||||
|
|
|
@ -39,6 +39,7 @@ module Puma
|
|||
attr_accessor :max_threads
|
||||
attr_accessor :persistent_timeout
|
||||
attr_accessor :auto_trim_time
|
||||
attr_accessor :first_data_timeout
|
||||
|
||||
# Create a server for the rack app +app+.
|
||||
#
|
||||
|
|
|
@ -289,4 +289,18 @@ class TestPumaServer < Test::Unit::TestCase
|
|||
|
||||
assert_equal [:booting, :running, :stop, :done], states
|
||||
end
|
||||
|
||||
def test_timeout_in_data_phase
|
||||
@server.first_data_timeout = 2
|
||||
@server.add_tcp_listener @host, @port
|
||||
@server.run
|
||||
|
||||
client = TCPSocket.new @host, @port
|
||||
|
||||
client << "POST / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\n"
|
||||
|
||||
data = client.gets
|
||||
|
||||
assert_equal "HTTP/1.1 408 Request Timeout\r\n", data
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue