lib/rack/file.rb in rack-1.5.5 vs lib/rack/file.rb in rack-1.6.0.beta

- old
+ new

@@ -10,12 +10,12 @@ # # Handlers can detect if bodies are a Rack::File, and use mechanisms # like sendfile on the +path+. class File - SEPS = Regexp.union(*[::File::SEPARATOR, ::File::ALT_SEPARATOR].compact) - ALLOWED_VERBS = %w[GET HEAD] + ALLOWED_VERBS = %w[GET HEAD OPTIONS] + ALLOW_HEADER = ALLOWED_VERBS.join(', ') attr_accessor :root attr_accessor :path attr_accessor :cache_control @@ -33,25 +33,18 @@ F = ::File def _call(env) unless ALLOWED_VERBS.include? env["REQUEST_METHOD"] - return fail(405, "Method Not Allowed") + return fail(405, "Method Not Allowed", {'Allow' => ALLOW_HEADER}) end path_info = Utils.unescape(env["PATH_INFO"]) - parts = path_info.split SEPS + clean_path_info = Utils.clean_path_info(path_info) - clean = [] + @path = F.join(@root, clean_path_info) - parts.each do |part| - next if part.empty? || part == '.' - part == '..' ? clean.pop : clean << part - end - - @path = F.join(@root, *clean) - available = begin F.file?(@path) && F.readable?(@path) rescue SystemCallError false end @@ -62,10 +55,13 @@ fail(404, "File not found: #{path_info}") end end def serving(env) + if env["REQUEST_METHOD"] == "OPTIONS" + return [200, {'Allow' => ALLOW_HEADER, 'Content-Length' => '0'}, []] + end last_modified = F.mtime(@path).httpdate return [304, {}, []] if env['HTTP_IF_MODIFIED_SINCE'] == last_modified headers = { "Last-Modified" => last_modified } mime = Mime.mime_type(F.extname(@path), @default_mime) @@ -119,18 +115,18 @@ end end private - def fail(status, body) + def fail(status, body, headers = {}) body += "\n" [ status, { "Content-Type" => "text/plain", "Content-Length" => body.size.to_s, "X-Cascade" => "pass" - }, + }.merge!(headers), [body] ] end end