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
DIR_PAGE
F
FILESIZE_FORMAT

Stolen from Ramaze

Attributes

files[R]
root[RW]
path[RW]

Public Class Methods

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

Public Instance Methods

_call(env) click to toggle source
    # 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
call(env) click to toggle source
    # File lib/rack/directory.rb, line 50
50:     def call(env)
51:       dup._call(env)
52:     end
check_forbidden() click to toggle source
    # 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
each() click to toggle source
     # File lib/rack/directory.rb, line 133
133:     def each
134:       show_path = @path.sub(/^#{@root}/,'')
135:       files = @files.map{|f| DIR_FILE % f }*"\n"
136:       page  = DIR_PAGE % [ show_path, show_path , files ]
137:       page.each_line{|l| yield l }
138:     end
entity_not_found() click to toggle source
     # File lib/rack/directory.rb, line 125
125:     def entity_not_found
126:       body = "Entity not found: #{@path_info}\n"
127:       size = Rack::Utils.bytesize(body)
128:       return [404, {"Content-Type" => "text/plain",
129:         "Content-Length" => size.to_s,
130:         "X-Cascade" => "pass"}, [body]]
131:     end
filesize_format(int) click to toggle source
     # File lib/rack/directory.rb, line 149
149:     def filesize_format(int)
150:       FILESIZE_FORMAT.each do |format, size|
151:         return format % (int.to_f / size) if int >= size
152:       end
153: 
154:       int.to_s + 'B'
155:     end
list_directory() click to toggle source
     # File lib/rack/directory.rb, line 79
 79:     def list_directory
 80:       @files = [['../','Parent Directory','','','']]
 81:       glob = F.join(@path, '*')
 82: 
 83:       Dir[glob].sort.each do |node|
 84:         stat = stat(node)
 85:         next  unless stat
 86:         basename = F.basename(node)
 87:         ext = F.extname(node)
 88: 
 89:         url = F.join(@script_name, @path_info, basename)
 90:         size = stat.size
 91:         type = stat.directory? ? 'directory' : Mime.mime_type(ext)
 92:         size = stat.directory? ? '-' : filesize_format(size)
 93:         mtime = stat.mtime.httpdate
 94:         url << '/'  if stat.directory?
 95:         basename << '/'  if stat.directory?
 96: 
 97:         @files << [ url, basename, size, type, mtime ]
 98:       end
 99: 
100:       return [ 200, {'Content-Type'=>'text/html; charset=utf-8'}, self ]
101:     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 111
111:     def list_path
112:       @stat = F.stat(@path)
113: 
114:       if @stat.readable?
115:         return @app.call(@env) if @stat.file?
116:         return list_directory if @stat.directory?
117:       else
118:         raise Errno::ENOENT, 'No such file or directory'
119:       end
120: 
121:     rescue Errno::ENOENT, Errno::ELOOP
122:       return entity_not_found
123:     end
stat(node, max = 10) click to toggle source
     # File lib/rack/directory.rb, line 103
103:     def stat(node, max = 10)
104:       F.stat(node)
105:     rescue Errno::ENOENT, Errno::ELOOP
106:       return nil
107:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.