Skip to content
Browse files

Fix race conditions in head middleware

  • Loading branch information...
1 parent d860397 commit f27b08e648732177dd945ba9c1fdd056cf7d4f96 @raggi raggi committed Dec 28, 2013
Showing with 9 additions and 2 deletions.
  1. +7 −2 lib/rack/head.rb
  2. +2 −0 test/spec_head.rb
View
9 lib/rack/head.rb
@@ -1,3 +1,5 @@
+require 'rack/body_proxy'
+
module Rack
class Head
@@ -11,8 +13,11 @@ def call(env)
status, headers, body = @app.call(env)
if env["REQUEST_METHOD"] == "HEAD"
- body.close if body.respond_to? :close
- [status, headers, []]
+ [
+ status, headers, Rack::BodyProxy.new([]) do
+ body.close if body.respond_to? :close
+ end
+ ]
else
[status, headers, body]
end
View
2 test/spec_head.rb
@@ -38,6 +38,8 @@ def test_response(headers = {})
resp[0].should.equal(200)
resp[1].should.equal({"Content-type" => "test/plain", "Content-length" => "3"})
resp[2].to_enum.to_a.should.equal([])
+ body.should.not.be.closed
+ resp[2].close
body.should.be.closed
end
end

0 comments on commit f27b08e

Please sign in to comment.
Something went wrong with that request. Please try again.