Class: Rack::Directory

### Home Classes Methods

### In Files

- lib/rack/directory.rb

### Parent

Object

### Methods

- ::new
- #_call
- #call
- #check_forbidden
- #each
- #entity_not_found
- #filesize_format
- #list_directory
- #list_path
- #stat

### Files

- KNOWN-ISSUES

### Class/Module Index

Quicksearch

- Rack
- Rack::Auth
- Rack::Auth::AbstractHandler
- Rack::Auth::AbstractRequest
- Rack::Auth::Basic
- Rack::Auth::Basic::Request
- Rack::Auth::Digest
- Rack::Auth::Digest::MD5
- Rack::Auth::Digest::Nonce
- Rack::Auth::Digest::Params
- Rack::Auth::Digest::Request
- Rack::BodyProxy
- Rack::Builder
- Rack::Cascade
- Rack::Chunked
- Rack::Chunked::Body
- Rack::CommonLogger
- Rack::ConditionalGet
- Rack::Config
- Rack::ContentLength
- Rack::ContentType
- Rack::Deflater
- Rack::Deflater::DeflateStream
- Rack::Deflater::GzipStream
- Rack::Directory
- Rack::ETag
- Rack::File
- Rack::ForwardRequest
- Rack::Handler
- Rack::Handler::CGI
- Rack::Handler::EventedMongrel
- Rack::Handler::FastCGI
- Rack::Handler::LSWS
- Rack::Handler::Mongrel
- Rack::Handler::SCGI
- Rack::Handler::SwiftipliedMongrel
- Rack::Handler::Thin
- Rack::Handler::WEBrick
- Rack::Head
- Rack::Lint
- Rack::Lobster
- Rack::Lock
- Rack::Logger
- Rack::MethodOverride
- Rack::Mime
- Rack::MockRequest
- Rack::MockRequest::FatalWarner
- Rack::MockRequest::FatalWarning
- Rack::MockResponse
- Rack::Multipart
- Rack::Multipart::Generator
- Rack::Multipart::MultipartPartLimitError
- Rack::Multipart::Parser
- Rack::Multipart::UploadedFile
- Rack::NullLogger
- Rack::Recursive
- Rack::Reloader
- Rack::Reloader::Stat
- Rack::Request
- Rack::Response
- Rack::Response::Helpers
- Rack::RewindableInput
- Rack::RewindableInput::Tempfile
- Rack::Runtime
- Rack::Sendfile
- Rack::Server
- Rack::Server::Options
- Rack::Session
- Rack::Session::Abstract
- Rack::Session::Abstract::ID
- Rack::Session::Abstract::SessionHash
- Rack::Session::Cookie
- Rack::Session::Cookie::Base64
- Rack::Session::Cookie::Base64::JSON
- Rack::Session::Cookie::Base64::Marshal
- Rack::Session::Cookie::Base64::ZipJSON
- Rack::Session::Cookie::Identity
- Rack::Session::Memcache
- Rack::Session::Pool
- Rack::ShowExceptions
- Rack::ShowStatus
- Rack::Static
- Rack::TempfileReaper
- Rack::URLMap
- Rack::Utils
- Rack::Utils::Context
- Rack::Utils::HeaderHash
- Rack::Utils::InvalidParameterError
- Rack::Utils::KeySpaceConstrainedParams
- Rack::Utils::Multipart
- Rack::Utils::OkJson
- Rack::Utils::OkJson::Error
- Rack::Utils::OkJson::Utf8Error
- Rack::Utils::ParameterTypeError
- FCGI
- FCGI::Stream
- WEBrick
- WEBrick::HTTPResponse
- Object

No matching classes.

# 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]
path[RW]
root[RW]

### Public Class Methods

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

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

### Public Instance Methods

_call(env) click to toggle source

    # File lib/rack/directory.rb, line 56
    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) click to toggle source

    # File lib/rack/directory.rb, line 50
    def call(env)
      dup._call(env)
    end

check_forbidden() click to toggle source

    # File lib/rack/directory.rb, line 69
    def check_forbidden
      return unless @path_info.include? ".."

      body = "Forbidden\n"
      size = Rack::Utils.bytesize(body)
      return [403, {"Content-Type" => "text/plain",
        CONTENT_LENGTH => size.to_s,
        "X-Cascade" => "pass"}, [body]]
    end

each() click to toggle source

    # File lib/rack/directory.rb, line 137
    def each
      show_path = Rack::Utils.escape_html(@path.sub(/^#{@root}/,''))
      files = @files.map{|f| DIR_FILE % DIR_FILE_escape(*f) }*"\n"
      page  = DIR_PAGE % [ show_path, show_path , files ]
      page.each_line{|l| yield l }
    end

entity_not_found() click to toggle source

    # File lib/rack/directory.rb, line 129
    def entity_not_found
      body = "Entity not found: #{@path_info}\n"
      size = Rack::Utils.bytesize(body)
      return [404, {"Content-Type" => "text/plain",
        CONTENT_LENGTH => size.to_s,
        "X-Cascade" => "pass"}, [body]]
    end

filesize_format(int) click to toggle source

    # File lib/rack/directory.rb, line 153
    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() click to toggle source

    # File lib/rack/directory.rb, line 79
    def list_directory
      @files = [['../','Parent Directory','','','']]
      glob = F.join(@path, '*')

      url_head = (@script_name.split('/') + @path_info.split('/')).map do |part|
        Rack::Utils.escape part
      end

      Dir[glob].sort.each do |node|
        stat = stat(node)
        next  unless stat
        basename = F.basename(node)
        ext = F.extname(node)

        url = F.join(*url_head + [Rack::Utils.escape(basename)])
        size = stat.size
        type = stat.directory? ? 'directory' : Mime.mime_type(ext)
        size = stat.directory? ? '-' : filesize_format(size)
        mtime = stat.mtime.httpdate
        url << '/'  if stat.directory?
        basename << '/'  if stat.directory?

        @files << [ url, basename, size, type, mtime ]
      end

      return [ 200, { CONTENT_TYPE =>'text/html; charset=utf-8'}, self ]
    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 115
    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

stat(node, max = 10) click to toggle source

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.