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.
Stolen from Ramaze
(Not documented)
# File lib/rack/directory.rb, line 57
57: def _call(env)
58: @env = env
59: @script_name = env['SCRIPT_NAME']
60: @path_info = Utils.unescape(env['PATH_INFO'])
61:
62: if forbidden = check_forbidden
63: forbidden
64: else
65: @path = F.join(@root, @path_info)
66: list_path
67: end
68: end
(Not documented)
# File lib/rack/directory.rb, line 51
51: def call(env)
52: dup._call(env)
53: end
(Not documented)
# File lib/rack/directory.rb, line 70
70: def check_forbidden
71: return unless @path_info.include? ".."
72:
73: body = "Forbidden\n"
74: size = Rack::Utils.bytesize(body)
75: return [403, {"Content-Type" => "text/plain",
76: "Content-Length" => size.to_s,
77: "X-Cascade" => "pass"}, [body]]
78: end
(Not documented)
# File lib/rack/directory.rb, line 134
134: def each
135: show_path = @path.sub(/^#{@root}/,'')
136: files = @files.map{|f| DIR_FILE % f }*"\n"
137: page = DIR_PAGE % [ show_path, show_path , files ]
138: page.each_line{|l| yield l }
139: end
(Not documented)
# File lib/rack/directory.rb, line 126
126: def entity_not_found
127: body = "Entity not found: #{@path_info}\n"
128: size = Rack::Utils.bytesize(body)
129: return [404, {"Content-Type" => "text/plain",
130: "Content-Length" => size.to_s,
131: "X-Cascade" => "pass"}, [body]]
132: end
(Not documented)
# File lib/rack/directory.rb, line 150
150: def filesize_format(int)
151: FILESIZE_FORMAT.each do |format, size|
152: return format % (int.to_f / size) if int >= size
153: end
154:
155: int.to_s + 'B'
156: end
(Not documented)
# File lib/rack/directory.rb, line 80
80: def list_directory
81: @files = [['../','Parent Directory','','','']]
82: glob = F.join(@path, '*')
83:
84: Dir[glob].sort.each do |node|
85: stat = stat(node)
86: next unless stat
87: basename = F.basename(node)
88: ext = F.extname(node)
89:
90: url = F.join(@script_name, @path_info, basename)
91: size = stat.size
92: type = stat.directory? ? 'directory' : Mime.mime_type(ext)
93: size = stat.directory? ? '-' : filesize_format(size)
94: mtime = stat.mtime.httpdate
95: url << '/' if stat.directory?
96: basename << '/' if stat.directory?
97:
98: @files << [ url, basename, size, type, mtime ]
99: end
100:
101: return [ 200, {'Content-Type'=>'text/html; charset=utf-8'}, self ]
102: end
TODO: add correct response if not readable, not sure if 404 is the best
option
# File lib/rack/directory.rb, line 112
112: def list_path
113: @stat = F.stat(@path)
114:
115: if @stat.readable?
116: return @app.call(@env) if @stat.file?
117: return list_directory if @stat.directory?
118: else
119: raise Errno::ENOENT, 'No such file or directory'
120: end
121:
122: rescue Errno::ENOENT, Errno::ELOOP
123: return entity_not_found
124: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.