From 03ee5ec3c74b22fae7193b6ac45f5f4f0497e45f Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Tue, 3 May 2016 16:17:30 -0400 Subject: Omit response body for HEAD requests to Rack::Directory --- lib/rack/directory.rb | 6 ++++++ test/spec_directory.rb | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/rack/directory.rb b/lib/rack/directory.rb index 5baee3c8..c026c42a 100644 --- a/lib/rack/directory.rb +++ b/lib/rack/directory.rb @@ -59,9 +59,15 @@ table { width:100%%; } def initialize(root, app=nil) @root = ::File.expand_path(root) @app = app || Rack::File.new(@root) + @head = Rack::Head.new(lambda { |env| get env }) end def call(env) + # strip body if this is a HEAD call + @head.call env + end + + def get(env) script_name = env[SCRIPT_NAME] path_info = Utils.unescape_path(env[PATH_INFO]) diff --git a/test/spec_directory.rb b/test/spec_directory.rb index 1a97e9e5..6ba0d406 100644 --- a/test/spec_directory.rb +++ b/test/spec_directory.rb @@ -130,4 +130,12 @@ describe Rack::Directory do res = mr.get("/script-path/cgi/test+directory/test+file") res.must_be :ok? end + + it "return error when file not found for head request" do + res = Rack::MockRequest.new(Rack::Lint.new(app)). + head("/cgi/missing") + + res.must_be :not_found? + res.body.must_be :empty? + end end -- cgit v1.2.3-24-ge0c7