Class: Rack::Directory
- Defined in:
- lib/gems/rack-0.9.1/lib/rack/directory.rb
Overview
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.
Constant Summary collapse
- 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> <title>%s</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style type='text/css'> table { width:100%%; } .name { text-align:left; } .size, .mtime { text-align:right; } .type { width:11em; } .mtime { width:15em; } </style> </head><body> <h1>%s</h1> <hr /> <table> <tr> <th class='name'>Name</th> <th class='size'>Size</th> <th class='type'>Type</th> <th class='mtime'>Last Modified</th> </tr> %s </table> <hr /> </body></html> PAGE
- F =
::File
- FILESIZE_FORMAT =
Stolen from Ramaze
[ ['%.1fT', 1 << 40], ['%.1fG', 1 << 30], ['%.1fM', 1 << 20], ['%.1fK', 1 << 10], ]
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#path ⇒ Object
Returns the value of attribute path.
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
- #check_forbidden ⇒ Object
- #each ⇒ Object
- #entity_not_found ⇒ Object
- #filesize_format(int) ⇒ Object
-
#initialize(root, app = nil) ⇒ Directory
constructor
A new instance of Directory.
- #list_directory ⇒ Object
-
#list_path ⇒ Object
TODO: add correct response if not readable, not sure if 404 is the best option.
- #stat(node, max = 10) ⇒ Object
Constructor Details
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
41 42 43 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 41 def files @files end |
#path ⇒ Object
Returns the value of attribute path.
42 43 44 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 42 def path @path end |
Instance Method Details
#_call(env) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 55 def _call(env) @env = env @script_name = env['SCRIPT_NAME'] @path_info = Utils.unescape(env['PATH_INFO']) if forbidden = check_forbidden forbidden else @path = F.join(@root, @path_info) list_path end end |
#call(env) ⇒ Object
49 50 51 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 49 def call(env) dup._call(env) end |
#check_forbidden ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 68 def check_forbidden return unless @path_info.include? ".." body = "Forbidden\n" size = body.respond_to?(:bytesize) ? body.bytesize : body.size return [403, {"Content-Type" => "text/plain","Content-Length" => size.to_s}, [body]] end |
#each ⇒ Object
126 127 128 129 130 131 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 126 def each show_path = @path.sub(/^#{@root}/,'') files = @files.map{|f| DIR_FILE % f }*"\n" page = DIR_PAGE % [ show_path, show_path , files ] page.each_line{|l| yield l } end |
#entity_not_found ⇒ Object
120 121 122 123 124 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 120 def entity_not_found body = "Entity not found: #{@path_info}\n" size = body.respond_to?(:bytesize) ? body.bytesize : body.size return [404, {"Content-Type" => "text/plain", "Content-Length" => size.to_s}, [body]] end |
#filesize_format(int) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 142 def filesize_format(int) FILESIZE_FORMAT.each do |format, size| return format % (int.to_f / size) if int >= size end int.to_s + 'B' end |
#list_directory ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 76 def list_directory @files = [['../','Parent Directory','','','']] glob = F.join(@path, '*') Dir[glob].sort.each do |node| stat = stat(node) next unless stat basename = F.basename(node) ext = F.extname(node) url = F.join(@script_name, @path_info, basename) size = stat.size type = stat.directory? ? 'directory' : Mime.mime_type(ext) size = stat.directory? ? '-' : filesize_format(size) mtime = stat.mtime.httpdate @files << [ url, basename, size, type, mtime ] end return [ 200, {'Content-Type'=>'text/html; charset=utf-8'}, self ] end |
#list_path ⇒ Object
TODO: add correct response if not readable, not sure if 404 is the best
option
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/gems/rack-0.9.1/lib/rack/directory.rb', line 106 def list_path @stat = F.stat(@path) if @stat.readable? return @app.call(@env) if @stat.file? return list_directory if @stat.directory? else raise Errno::ENOENT, 'No such file or directory' end rescue Errno::ENOENT, Errno::ELOOP return entity_not_found end |