lib/rack/directory.rb in rack-2.0.9.4 vs lib/rack/directory.rb in rack-2.1.0
- old
+ new
@@ -1,16 +1,19 @@
+# frozen_string_literal: true
+
require 'time'
require 'rack/utils'
require 'rack/mime'
+require 'rack/files'
module Rack
# Rack::Directory serves entries below the +root+ given, according to the
# path info of the Rack request. If a directory is found, the file's contents
# will be presented in an html based index. If a file is found, the env will
# be passed to the specified +app+.
#
- # If +app+ is not specified, a Rack::File of the same +root+ will be used.
+ # If +app+ is not specified, a Rack::Files of the same +root+ will be used.
class Directory
DIR_FILE = "<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>"
DIR_PAGE = <<-PAGE
<html><head>
@@ -39,13 +42,13 @@
</body></html>
PAGE
class DirectoryBody < Struct.new(:root, :path, :files)
def each
- show_path = Rack::Utils.escape_html(path.sub(/^#{root}/,''))
- listings = files.map{|f| DIR_FILE % DIR_FILE_escape(*f) }*"\n"
- page = DIR_PAGE % [ show_path, show_path , listings ]
+ show_path = Rack::Utils.escape_html(path.sub(/^#{root}/, ''))
+ listings = files.map{|f| DIR_FILE % DIR_FILE_escape(*f) } * "\n"
+ page = DIR_PAGE % [ show_path, show_path, listings ]
page.each_line{|l| yield l }
end
private
# Assumes url is already escaped.
@@ -54,13 +57,13 @@
end
end
attr_reader :root, :path
- def initialize(root, app=nil)
+ def initialize(root, app = nil)
@root = ::File.expand_path(root)
- @app = app || Rack::File.new(@root)
+ @app = app || Rack::Files.new(@root)
@head = Rack::Head.new(lambda { |env| get env })
end
def call(env)
# strip body if this is a HEAD call
@@ -84,27 +87,27 @@
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",
+ return [400, { CONTENT_TYPE => "text/plain",
CONTENT_LENGTH => size.to_s,
- "X-Cascade" => "pass"}, [body]]
+ "X-Cascade" => "pass" }, [body]]
end
def check_forbidden(path_info)
return unless path_info.include? ".."
body = "Forbidden\n"
size = body.bytesize
- return [403, {CONTENT_TYPE => "text/plain",
+ return [403, { CONTENT_TYPE => "text/plain",
CONTENT_LENGTH => size.to_s,
- "X-Cascade" => "pass"}, [body]]
+ "X-Cascade" => "pass" }, [body]]
end
def list_directory(path_info, path, script_name)
- files = [['../','Parent Directory','','','']]
+ files = [['../', 'Parent Directory', '', '', '']]
glob = ::File.join(path, '*')
url_head = (script_name.split('/') + path_info.split('/')).map do |part|
Rack::Utils.escape_path part
end
@@ -124,11 +127,11 @@
basename << '/' if stat.directory?
files << [ url, basename, size, type, mtime ]
end
- return [ 200, { CONTENT_TYPE =>'text/html; charset=utf-8'}, DirectoryBody.new(@root, path, files) ]
+ return [ 200, { CONTENT_TYPE => 'text/html; charset=utf-8' }, DirectoryBody.new(@root, path, files) ]
end
def stat(node)
::File.stat(node)
rescue Errno::ENOENT, Errno::ELOOP
@@ -152,12 +155,12 @@
end
def entity_not_found(path_info)
body = "Entity not found: #{path_info}\n"
size = body.bytesize
- return [404, {CONTENT_TYPE => "text/plain",
+ return [404, { CONTENT_TYPE => "text/plain",
CONTENT_LENGTH => size.to_s,
- "X-Cascade" => "pass"}, [body]]
+ "X-Cascade" => "pass" }, [body]]
end
# Stolen from Ramaze
FILESIZE_FORMAT = [