From bd397ee11b60243ef15c5558c4309e46e27e6192 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 5 Jan 2011 11:41:36 -0800 Subject: http_response: simplify the status == 100 comparison No need to preserve the response tuplet if we're just going to unpack it eventually. --- lib/unicorn/http_response.rb | 5 ++--- lib/unicorn/http_server.rb | 11 +++++------ test/unit/test_response.rb | 18 +++++++++--------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb index c59ce2c..3a03cd6 100644 --- a/lib/unicorn/http_response.rb +++ b/lib/unicorn/http_response.rb @@ -3,7 +3,7 @@ # You use it by simply doing: # # status, headers, body = rack_app.call(env) -# http_response_write(socket, [ status, headers, body ]) +# http_response_write(socket, status, headers, body) # # Most header correctness (including Content-Length and Content-Type) # is the job of Rack, with the exception of the "Date" and "Status" header. @@ -17,8 +17,7 @@ module Unicorn::HttpResponse CRLF = "\r\n" # writes the rack_response to socket as an HTTP response - def http_response_write(socket, rack_response) - status, headers, body = rack_response + def http_response_write(socket, status, headers, body) status = CODES[status.to_i] || status if headers diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb index fbc9a68..e2a4db7 100644 --- a/lib/unicorn/http_server.rb +++ b/lib/unicorn/http_server.rb @@ -529,16 +529,15 @@ class Unicorn::HttpServer # once a client is accepted, it is processed in its entirety here # in 3 easy steps: read request, call app, write app response def process_client(client) - r = @app.call(env = @request.read(client)) + status, headers, body = @app.call(env = @request.read(client)) - if 100 == r[0].to_i + if 100 == status.to_i client.write(Unicorn::Const::EXPECT_100_RESPONSE) env.delete(Unicorn::Const::HTTP_EXPECT) - r = @app.call(env) + status, headers, body = @app.call(env) end - # r may be frozen or const, so don't modify it - @request.headers? or r = [ r[0], nil, r[2] ] - http_response_write(client, r) + @request.headers? or headers = nil + http_response_write(client, status, headers, body) rescue => e handle_error(client, e) end diff --git a/test/unit/test_response.rb b/test/unit/test_response.rb index ac549bc..e2986c6 100644 --- a/test/unit/test_response.rb +++ b/test/unit/test_response.rb @@ -26,7 +26,7 @@ class ResponseTest < Test::Unit::TestCase def test_response_headers out = StringIO.new - http_response_write(out,[200, {"X-Whatever" => "stuff"}, ["cool"]]) + http_response_write(out, 200, {"X-Whatever" => "stuff"}, ["cool"]) assert out.closed? assert out.length > 0, "output didn't have data" @@ -34,7 +34,7 @@ class ResponseTest < Test::Unit::TestCase def test_response_string_status out = StringIO.new - http_response_write(out,['200', {}, []]) + http_response_write(out,'200', {}, []) assert out.closed? assert out.length > 0, "output didn't have data" assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/).size @@ -42,7 +42,7 @@ class ResponseTest < Test::Unit::TestCase def test_response_200 io = StringIO.new - http_response_write(io, [200, {}, []]) + http_response_write(io, 200, {}, []) assert io.closed? assert io.length > 0, "output didn't have data" end @@ -50,7 +50,7 @@ class ResponseTest < Test::Unit::TestCase def test_response_with_default_reason code = 400 io = StringIO.new - http_response_write(io, [code, {}, []]) + http_response_write(io, code, {}, []) assert io.closed? lines = io.string.split(/\r\n/) assert_match(/.* Bad Request$/, lines.first, @@ -59,7 +59,7 @@ class ResponseTest < Test::Unit::TestCase def test_rack_multivalue_headers out = StringIO.new - http_response_write(out,[200, {"X-Whatever" => "stuff\nbleh"}, []]) + http_response_write(out,200, {"X-Whatever" => "stuff\nbleh"}, []) assert out.closed? assert_match(/^X-Whatever: stuff\r\nX-Whatever: bleh\r\n/, out.string) end @@ -68,7 +68,7 @@ class ResponseTest < Test::Unit::TestCase # some broken clients still rely on it def test_status_header_added out = StringIO.new - http_response_write(out,[200, {"X-Whatever" => "stuff"}, []]) + http_response_write(out,200, {"X-Whatever" => "stuff"}, []) assert out.closed? assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size end @@ -79,7 +79,7 @@ class ResponseTest < Test::Unit::TestCase def test_status_header_ignores_app_hash out = StringIO.new header_hash = {"X-Whatever" => "stuff", 'StaTus' => "666" } - http_response_write(out,[200, header_hash, []]) + http_response_write(out,200, header_hash, []) assert out.closed? assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size assert_equal 1, out.string.split(/\r\n/).grep(/^Status:/i).size @@ -90,7 +90,7 @@ class ResponseTest < Test::Unit::TestCase body = StringIO.new(expect_body) body.rewind out = StringIO.new - http_response_write(out,[200, {}, body]) + http_response_write(out,200, {}, body) assert out.closed? assert body.closed? assert_match(expect_body, out.string.split(/\r\n/).last) @@ -98,7 +98,7 @@ class ResponseTest < Test::Unit::TestCase def test_unknown_status_pass_through out = StringIO.new - http_response_write(out,["666 I AM THE BEAST", {}, [] ]) + http_response_write(out,"666 I AM THE BEAST", {}, [] ) assert out.closed? headers = out.string.split(/\r\n\r\n/).first.split(/\r\n/) assert %r{\AHTTP/\d\.\d 666 I AM THE BEAST\z}.match(headers[0]) -- cgit v1.2.3-24-ge0c7