From f3b379f938719ed7cdfdf2eb92491db276e2da07 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 14 Aug 2009 21:03:39 -0700 Subject: http_request: reinstate empty StringIO optimization This makes a noticeable difference on light GET/HEAD requests. Heck, even the tests run a few seconds faster. --- lib/unicorn/http_request.rb | 5 ++++- test/unit/test_request.rb | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) 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" \ -- cgit v1.2.3-24-ge0c7