lib/rack/directory.rb in rack-2.0.0.alpha vs lib/rack/directory.rb in rack-2.0.0.rc1

- old
+ new

@@ -57,24 +57,42 @@ attr_reader :root, :path 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]) - if forbidden = check_forbidden(path_info) + if bad_request = check_bad_request(path_info) + bad_request + elsif forbidden = check_forbidden(path_info) forbidden else path = ::File.join(@root, path_info) list_path(env, path, path_info, script_name) end end + def check_bad_request(path_info) + return if Utils.valid_path?(path_info) + + body = "Bad Request\n" + size = body.bytesize + return [400, {CONTENT_TYPE => "text/plain", + CONTENT_LENGTH => size.to_s, + "X-Cascade" => "pass"}, [body]] + end + def check_forbidden(path_info) return unless path_info.include? ".." body = "Forbidden\n" size = body.bytesize @@ -153,9 +171,9 @@ def filesize_format(int) FILESIZE_FORMAT.each do |format, size| return format % (int.to_f / size) if int >= size end - int.to_s + 'B' + "#{int}B" end end end