1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Ignore the body on a HEAD request. Fixes #278

This commit is contained in:
Evan Phoenix 2013-06-18 14:27:46 -07:00
parent d996daa15a
commit 831f09d5d9
2 changed files with 47 additions and 3 deletions

View file

@ -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

View file

@ -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