about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/unicorn/http_request.rb5
-rw-r--r--test/unit/test_request.rb25
2 files changed, 29 insertions, 1 deletions
diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb
index 26eff1f..a0d811c 100644
--- a/lib/unicorn/http_request.rb
+++ b/lib/unicorn/http_request.rb
@@ -1,4 +1,5 @@
 # coding:binary
+require 'stringio'
 require 'unicorn_http'
 
 module Unicorn
@@ -17,6 +18,7 @@ module Unicorn
       "SERVER_SOFTWARE" => "Unicorn #{Const::UNICORN_VERSION}".freeze
     }
 
+    NULL_IO = StringIO.new(Z)
     LOCALHOST = '127.0.0.1'.freeze
 
     # Being explicitly single-threaded, we have certain advantages in
@@ -71,7 +73,8 @@ module Unicorn
     # Handles dealing with the rest of the request
     # returns a # Rack environment if successful
     def handle_body(socket)
-      REQ[Const::RACK_INPUT] = Unicorn::TeeInput.new(socket)
+      REQ[Const::RACK_INPUT] = 0 == PARSER.content_length ?
+                               NULL_IO : Unicorn::TeeInput.new(socket)
       REQ.update(DEFAULTS)
     end
 
diff --git a/test/unit/test_request.rb b/test/unit/test_request.rb
index 139fc82..edd4c55 100644
--- a/test/unit/test_request.rb
+++ b/test/unit/test_request.rb
@@ -120,6 +120,31 @@ class RequestTest < Test::Unit::TestCase
     assert_nothing_raised { res = @lint.call(env) }
   end
 
+  def test_no_content_stringio
+    client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")
+    res = env = nil
+    assert_nothing_raised { env = @request.read(client) }
+    assert_equal StringIO, env['rack.input'].class
+  end
+
+  def test_zero_content_stringio
+    client = MockRequest.new("PUT / HTTP/1.1\r\n" \
+                             "Content-Length: 0\r\n" \
+                             "Host: foo\r\n\r\n")
+    res = env = nil
+    assert_nothing_raised { env = @request.read(client) }
+    assert_equal StringIO, env['rack.input'].class
+  end
+
+  def test_real_content_not_stringio
+    client = MockRequest.new("PUT / HTTP/1.1\r\n" \
+                             "Content-Length: 1\r\n" \
+                             "Host: foo\r\n\r\n")
+    res = env = nil
+    assert_nothing_raised { env = @request.read(client) }
+    assert_equal Unicorn::TeeInput, env['rack.input'].class
+  end
+
   def test_rack_lint_put
     client = MockRequest.new(
       "PUT / HTTP/1.1\r\n" \