Permalink
Browse files

Bug fix for Tempfile POST bodies under Ruby 1.8

The Tempfile class in Ruby 1.8 doesn't implement the == method correctly.
This causes Rack::Requests to re-parse the input (when the input is a
Tempfile) each time the POST method is called, this in turn raises an
EOFError because the input has already been read.

One example of when this happens is when handling large POST requests
(e.g. file uploads) under Mongrel.

This issue only effects Ruby 1.8 (tested against 1.8.6).  Ruby 1.9 does
not suffer from this issue (presumably due to changes in the Delegate
implementation.)

darcs-hash:20080327110953-ea3b8-44b6f22da80c9dae70dcd61adc43111547371daf.gz
  • Loading branch information...
Adam Harper
Adam Harper committed Mar 27, 2008
1 parent 7d22320 commit 7f343291387f72033cf8759d732af39d6c4cdeb2
Showing with 23 additions and 1 deletion.
  1. +1 −1 lib/rack/request.rb
  2. +22 −0 test/spec_rack_request.rb
View
@@ -105,7 +105,7 @@ def GET
# This method support both application/x-www-form-urlencoded and
# multipart/form-data.
def POST
- if @env["rack.request.form_input"] == @env["rack.input"]
+ if @env["rack.request.form_input"].eql? @env["rack.input"]
@env["rack.request.form_hash"]
elsif form_data?
@env["rack.request.form_input"] = @env["rack.input"]
View
@@ -334,6 +334,28 @@
lambda { req.POST }.should.raise(EOFError)
end
+ specify "should work around buggy 1.8.* Tempfile equality" do
+ input = <<EOF
+--AaB03x\r
+content-disposition: form-data; name="huge"; filename="huge"\r
+\r
+foo\r
+--AaB03x--
+EOF
+
+ rack_input = Tempfile.new("rackspec")
+ rack_input.write(input)
+ rack_input.rewind
+
+ req = Rack::Request.new Rack::MockRequest.env_for("/",
+ "CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
+ "CONTENT_LENGTH" => input.size,
+ :input => rack_input)
+
+ lambda {req.POST}.should.not.raise
+ lambda {req.POST}.should.blaming("input re-processed!").not.raise
+ end
+
specify "does conform to the Rack spec" do
app = lambda { |env|
content = Rack::Request.new(env).POST["file"].inspect

0 comments on commit 7f34329

Please sign in to comment.