lib/rack/file.rb in rack-2.0.0.alpha vs lib/rack/file.rb in rack-2.0.0.rc1
- old
+ new
@@ -1,9 +1,10 @@
require 'time'
require 'rack/utils'
require 'rack/mime'
require 'rack/request'
+require 'rack/head'
module Rack
# Rack::File serves files below the +root+ directory given, according to the
# path info of the Rack request.
# e.g. when Rack::File.new("/etc") is used, you can access 'passwd' file
@@ -20,21 +21,28 @@
def initialize(root, headers={}, default_mime = 'text/plain')
@root = root
@headers = headers
@default_mime = default_mime
+ @head = Rack::Head.new(lambda { |env| get env })
end
def call(env)
+ # HEAD requests drop the response body, including 4xx error messages.
+ @head.call env
+ end
+
+ def get(env)
request = Rack::Request.new env
unless ALLOWED_VERBS.include? request.request_method
return fail(405, "Method Not Allowed", {'Allow' => ALLOW_HEADER})
end
path_info = Utils.unescape_path request.path_info
- clean_path_info = Utils.clean_path_info(path_info)
+ return fail(400, "Bad Request") unless Utils.valid_path?(path_info)
+ clean_path_info = Utils.clean_path_info(path_info)
path = ::File.join(@root, clean_path_info)
available = begin
::File.file?(path) && ::File.readable?(path)
rescue SystemCallError
@@ -129,9 +137,10 @@
end
end
def fail(status, body, headers = {})
body += "\n"
+
[
status,
{
CONTENT_TYPE => "text/plain",
CONTENT_LENGTH => body.size.to_s,