class Heel::RackApp

Attributes

directory_index_html[R]
document_root[R]
highlighting[R]
icon_url[R]
ignore_globs[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/heel/rackapp.rb, line 22
def initialize(options = {})
  @ignore_globs               = options[:ignore_globs] ||= %w( *~ .htaccess . )
  @document_root              = options[:document_root] ||= Dir.pwd
  @directory_listing_allowed  = options[:directory_listing_allowed] ||= true
  @directory_index_html       = options[:directory_index_html] ||= "index.html"
  @using_icons                = options[:using_icons] ||= true
  @icon_url                   = options[:icon_url] ||= "/heel_icons"
  @highlighting               = options[:highlighting] ||= false
  @options                    = options
end

Public Instance Methods

call(env) click to toggle source

interface to rack, env is a hash

returns [ status, headers, body ]

# File lib/heel/rackapp.rb, line 129
def call(env)
  req = Heel::Request.new(env, document_root)
  if req.get? then
    if req.forbidden? or should_ignore?(req.request_path) then
      return ErrorResponse.new(req.path_info,"You do not have permissionto view #{req.path_info}",403).finish 
    end
    return ErrorResponse.new(req.path_info, "File not found: #{req.path_info}",404).finish unless req.found?
    return directory_index_response(req)                           if req.for_directory?
    return file_response(req)                                      if req.for_file?
  else
    return ErrorResponse.new(req.path_info,
                             "Method #{req.request_method} Not Allowed. Only GET is honored.", 
                             405, 
                             { "Allow" => "GET" }).finish
  end
end
directory_index_response(req) click to toggle source

formulate a directory index response

# File lib/heel/rackapp.rb, line 63
def directory_index_response(req)
  response = ::Rack::Response.new
  dir_index = File.join(req.request_path, directory_index_html) 
  if File.file?(dir_index) and File.readable?(dir_index) then
    response['Content-Type']   = mime_map.mime_type_of(dir_index).to_s
    response.write( File.read( dir_index ) )
  elsif directory_listing_allowed? then
    body                       = directory_indexer.index_page_for(req)
    response['Content-Type']   = 'text/html'
    response.write( body )
  else
    return ::Heel::ErrorResponse.new(req.path_info,"Directory index is forbidden", 403).finish
  end
  return response.finish
end
directory_index_template_file() click to toggle source
# File lib/heel/rackapp.rb, line 45
def directory_index_template_file
  @directory_index_template_file ||= Heel::Configuration.data_path("listing.rhtml")
end
directory_indexer() click to toggle source
# File lib/heel/rackapp.rb, line 49
def directory_indexer
  @directory_indexer ||= DirectoryIndexer.new( directory_index_template_file, @options )
end
directory_listing_allowed?() click to toggle source
# File lib/heel/rackapp.rb, line 33
def directory_listing_allowed?
  @directory_listing_allowed
end
file_response(req) click to toggle source

formulate a file content response. Possibly a coderay highlighted file if it is a type that code ray can deal with and the file is not already an html file.

# File lib/heel/rackapp.rb, line 84
    def file_response(req)
      response = ::Rack::Response.new

      response['Last-Modified'] = req.stat.mtime.rfc822

      if highlighting? and req.highlighting? then 
        # only do a coderay type check if we are going to use coderay in the
        # response
        code_ray_type = CodeRay::FileType[req.request_path, true] 
        if code_ray_type and (code_ray_type != :html) then
          body = <<-EOM
          <html>
            <head>
            <title>#{req.path_info}</title>
            <!-- CodeRay syntax highlighting CSS -->
            <link rel="stylesheet" href="/heel_css/coderay-alpha.css" type="text/css" />
            </head>
            <body>
  #{CodeRay.scan_file(req.request_path,:auto).html({ :wrap => :div, :line_numbers => :inline })}
            </body>
          </html>
          EOM
          response['Content-Type']    = 'text/html'
          response['Content-Length']  = body.length.to_s
          response.write( body )
          return response.finish
        end
      end

      # fall through to a default file return

      file_type                   = mime_map.mime_type_of(req.request_path)
      response['Content-Type']    = file_type.to_s
      File.open( req.request_path ) do |f|
        while p = f.read( 8192 ) do
          response.write( p )
        end
      end
      return response.finish
    end
highlighting?() click to toggle source
# File lib/heel/rackapp.rb, line 37
def highlighting?
  @highlighting
end
mime_map() click to toggle source
# File lib/heel/rackapp.rb, line 41
def mime_map
  @mime_map ||= Heel::MimeMap.new
end
should_ignore?(fname) click to toggle source
# File lib/heel/rackapp.rb, line 54
def should_ignore?(fname)
  ignore_globs.each do |glob|
    return true if ::File.fnmatch(glob,fname)
  end
  false 
end