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