From 831f09d5d92da836b9a737ff28c83e5c94dc2521 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Tue, 18 Jun 2013 14:27:46 -0700 Subject: [PATCH] Ignore the body on a HEAD request. Fixes #278 --- lib/puma/server.rb | 8 +++++--- test/test_puma_server.rb | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/puma/server.rb b/lib/puma/server.rb index b74cda7e..9f3e3cfd 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -354,6 +354,8 @@ module Puma body = req.body + head = env[REQUEST_METHOD] == HEAD + env[RACK_INPUT] = body env[RACK_URL_SCHEME] = env[HTTPS_KEY] ? HTTPS : HTTP @@ -384,7 +386,7 @@ module Puma end content_length = nil - no_body = false + no_body = head if res_body.kind_of? Array and res_body.size == 1 content_length = res_body[0].bytesize @@ -410,7 +412,7 @@ module Puma lines.append "HTTP/1.1 ", status.to_s, " ", HTTP_STATUS_CODES[status], line_ending - no_body = status < 200 || STATUS_WITH_NO_ENTITY_BODY[status] + no_body ||= status < 200 || STATUS_WITH_NO_ENTITY_BODY[status] end else allow_chunked = false @@ -425,7 +427,7 @@ module Puma lines.append "HTTP/1.0 ", status.to_s, " ", HTTP_STATUS_CODES[status], line_ending - no_body = status < 200 || STATUS_WITH_NO_ENTITY_BODY[status] + no_body ||= status < 200 || STATUS_WITH_NO_ENTITY_BODY[status] end end diff --git a/test/test_puma_server.rb b/test/test_puma_server.rb index e5feb012..841e03f7 100644 --- a/test/test_puma_server.rb +++ b/test/test_puma_server.rb @@ -162,4 +162,46 @@ class TestPumaServer < Test::Unit::TestCase assert_equal "80", res.body end + + def test_HEAD_has_no_body + @server.app = proc { |env| [200, {"Foo" => "Bar"}, ["hello"]] } + + @server.add_tcp_listener @host, @port + @server.run + + sock = TCPSocket.new @host, @port + sock << "HEAD / HTTP/1.0\r\n\r\n" + + data = sock.read + + assert_equal "HTTP/1.0 200 OK\r\nFoo: Bar\r\n\r\n", data + end + + def test_GET_with_empty_body_has_sane_chunking + @server.app = proc { |env| [200, {}, [""]] } + + @server.add_tcp_listener @host, @port + @server.run + + sock = TCPSocket.new @host, @port + sock << "HEAD / HTTP/1.0\r\n\r\n" + + data = sock.read + + assert_equal "HTTP/1.0 200 OK\r\n\r\n", data + end + + def test_GET_with_no_body_has_sane_chunking + @server.app = proc { |env| [200, {}, [""]] } + + @server.add_tcp_listener @host, @port + @server.run + + sock = TCPSocket.new @host, @port + sock << "HEAD / HTTP/1.0\r\n\r\n" + + data = sock.read + + assert_equal "HTTP/1.0 200 OK\r\n\r\n", data + end end