From 9f1131f5972ba90c1c54c76cc97633447142b307 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 3 May 2010 15:19:53 -0700 Subject: add client_max_body_size config directive Since Rainbows! is supported when exposed directly to the Internet, administrators may want to limit the amount of data a user may upload in a single request body to prevent a denial-of-service via disk space exhaustion. This amount may be specified in bytes, the default limit being 1024*1024 bytes (1 megabyte). To override this default, a user may specify `client_max_body_size' in the Rainbows! block of their server config file: Rainbows! do client_max_body_size 10 * 1024 * 1024 end Clients that exceed the limit will get a "413 Request Entity Too Large" response if the request body is too large and the connection will close. For chunked requests, we have no choice but to interrupt during the client upload since we have no prior knowledge of the request body size. --- lib/rainbows/ev_core.rb | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'lib/rainbows/ev_core.rb') diff --git a/lib/rainbows/ev_core.rb b/lib/rainbows/ev_core.rb index 682bdd6..d4ad040 100644 --- a/lib/rainbows/ev_core.rb +++ b/lib/rainbows/ev_core.rb @@ -49,7 +49,7 @@ module Rainbows write(EXPECT_100_RESPONSE) @env.delete(HTTP_EXPECT) end - @input = len && len <= MAX_BODY ? StringIO.new("") : Util.tmpio + @input = CapInput.new(len, self) @hp.filter_body(@buf2 = "", @buf) @input << @buf2 on_read("") @@ -73,5 +73,45 @@ module Rainbows handle_error(e) end + class CapInput < Struct.new(:io, :client, :bytes_left) + MAX_BODY = Unicorn::Const::MAX_BODY + Util = Unicorn::Util + + def self.err(client, msg) + client.write(Const::ERROR_413_RESPONSE) + client.quit + + # zip back up the stack + raise IOError, msg, [] + end + + def self.new(len, client) + max = Rainbows.max_bytes + if len + if max && (len > max) + err(client, "Content-Length too big: #{len} > #{max}") + end + len <= MAX_BODY ? StringIO.new("") : Util.tmpio + else + max ? super(Util.tmpio, client, max) : Util.tmpio + end + end + + def <<(buf) + if (self.bytes_left -= buf.size) < 0 + io.close + CapInput.err(client, "chunked request body too big") + end + io << buf + end + + def gets; io.gets; end + def each(&block); io.each(&block); end + def size; io.size; end + def rewind; io.rewind; end + def read(*args); io.read(*args); end + + end + end end -- cgit v1.2.3-24-ge0c7