Parent

Class Index [+]

Quicksearch

Rack::Directory

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.

Constants

DIR_FILE
(Not documented)
DIR_PAGE
(Not documented)
F
(Not documented)
FILESIZE_FORMAT

Stolen from Ramaze

Attributes

files[R]

(Not documented)

root[RW]

(Not documented)

path[RW]

(Not documented)

Public Class Methods

new(root, app=nil) click to toggle source

(Not documented)

    # File lib/rack/directory.rb, line 46
46:     def initialize(root, app=nil)
47:       @root = F.expand_path(root)
48:       @app = app || Rack::File.new(@root)
49:     end

Public Instance Methods

_call(env) click to toggle source

(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
call(env) click to toggle source

(Not documented)

    # File lib/rack/directory.rb, line 51
51:     def call(env)
52:       dup._call(env)
53:     end
check_forbidden() click to toggle source

(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","Content-Length" => size.to_s}, [body]]
76:     end
each() click to toggle source

(Not documented)

     # File lib/rack/directory.rb, line 130
130:     def each
131:       show_path = @path.sub(/^#{@root}/,'')
132:       files = @files.map{|f| DIR_FILE % f }*"\n"
133:       page  = DIR_PAGE % [ show_path, show_path , files ]
134:       page.each_line{|l| yield l }
135:     end
entity_not_found() click to toggle source

(Not documented)

     # File lib/rack/directory.rb, line 124
124:     def entity_not_found
125:       body = "Entity not found: #{@path_info}\n"
126:       size = Rack::Utils.bytesize(body)
127:       return [404, {"Content-Type" => "text/plain", "Content-Length" => size.to_s}, [body]]
128:     end
filesize_format(int) click to toggle source

(Not documented)

     # File lib/rack/directory.rb, line 146
146:     def filesize_format(int)
147:       FILESIZE_FORMAT.each do |format, size|
148:         return format % (int.to_f / size) if int >= size
149:       end
150: 
151:       int.to_s + 'B'
152:     end
list_directory() click to toggle source

(Not documented)

     # File lib/rack/directory.rb, line 78
 78:     def list_directory
 79:       @files = [['../','Parent Directory','','','']]
 80:       glob = F.join(@path, '*')
 81: 
 82:       Dir[glob].sort.each do |node|
 83:         stat = stat(node)
 84:         next  unless stat
 85:         basename = F.basename(node)
 86:         ext = F.extname(node)
 87: 
 88:         url = F.join(@script_name, @path_info, basename)
 89:         size = stat.size
 90:         type = stat.directory? ? 'directory' : Mime.mime_type(ext)
 91:         size = stat.directory? ? '-' : filesize_format(size)
 92:         mtime = stat.mtime.httpdate
 93:         url << '/'  if stat.directory?
 94:         basename << '/'  if stat.directory?
 95: 
 96:         @files << [ url, basename, size, type, mtime ]
 97:       end
 98: 
 99:       return [ 200, {'Content-Type'=>'text/html; charset=utf-8'}, self ]
100:     end
list_path() click to toggle source

TODO: add correct response if not readable, not sure if 404 is the best

      option
     # File lib/rack/directory.rb, line 110
110:     def list_path
111:       @stat = F.stat(@path)
112: 
113:       if @stat.readable?
114:         return @app.call(@env) if @stat.file?
115:         return list_directory if @stat.directory?
116:       else
117:         raise Errno::ENOENT, 'No such file or directory'
118:       end
119: 
120:     rescue Errno::ENOENT, Errno::ELOOP
121:       return entity_not_found
122:     end
stat(node, max = 10) click to toggle source

(Not documented)

     # File lib/rack/directory.rb, line 102
102:     def stat(node, max = 10)
103:       F.stat(node)
104:     rescue Errno::ENOENT, Errno::ELOOP
105:       return nil
106:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.