Class Heel::RackApp
In: lib/heel/rackapp.rb
lib/heel/rackapp.rb
Parent: Object

Methods

Attributes

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

Public Class methods

[Source]

    # File lib/heel/rackapp.rb, line 23
23:     def initialize(options = {}) 
24:       @ignore_globs               = options[:ignore_globs] ||= %w( *~ .htaccess . )
25:       @document_root              = options[:document_root] ||= Dir.pwd
26:       @directory_listing_allowed  = options[:directory_listing_allowed] ||= true
27:       @directory_index_html       = options[:directory_index_html] ||= "index.html"
28:       @using_icons                = options[:using_icons] ||= true
29:       @icon_url                   = options[:icon_url] ||= "/heel_icons"
30:       @highlighting               = options[:highlighting] ||= false
31:       @options                    = options
32:     end

[Source]

    # File lib/heel/rackapp.rb, line 23
23:     def initialize(options = {}) 
24:       @ignore_globs               = options[:ignore_globs] ||= %w( *~ .htaccess . )
25:       @document_root              = options[:document_root] ||= Dir.pwd
26:       @directory_listing_allowed  = options[:directory_listing_allowed] ||= true
27:       @directory_index_html       = options[:directory_index_html] ||= "index.html"
28:       @using_icons                = options[:using_icons] ||= true
29:       @icon_url                   = options[:icon_url] ||= "/heel_icons"
30:       @highlighting               = options[:highlighting] ||= false
31:       @options                    = options
32:     end

Public Instance methods

interface to rack, env is a hash

returns [ status, headers, body ]

[Source]

     # File lib/heel/rackapp.rb, line 143
143:     def call(env)
144:       req = Heel::Request.new(env, document_root)
145:       if req.get? then
146:         if req.forbidden? or should_ignore?(req.request_path) then
147:           return ErrorResponse.new(req.path_info,"You do not have permissionto view #{req.path_info}",403).finish 
148:         end
149:         return ErrorResponse.new(req.path_info, "File not found: #{req.path_info}",403).finish unless req.found?
150:         return directory_index_response(req)                           if req.for_directory?
151:         return file_response(req)                                      if req.for_file?
152:       else
153:         return ErrorResponse.new(req.path_info,
154:                                  "Method #{req.request_method} Not Allowed. Only GET is honored.", 
155:                                  405, 
156:                                  { "Allow" => "GET" }).finish
157:       end
158:     end

interface to rack, env is a hash

returns [ status, headers, body ]

[Source]

     # File lib/heel/rackapp.rb, line 143
143:     def call(env)
144:       req = Heel::Request.new(env, document_root)
145:       if req.get? then
146:         if req.forbidden? or should_ignore?(req.request_path) then
147:           return ErrorResponse.new(req.path_info,"You do not have permissionto view #{req.path_info}",403).finish 
148:         end
149:         return ErrorResponse.new(req.path_info, "File not found: #{req.path_info}",403).finish unless req.found?
150:         return directory_index_response(req)                           if req.for_directory?
151:         return file_response(req)                                      if req.for_file?
152:       else
153:         return ErrorResponse.new(req.path_info,
154:                                  "Method #{req.request_method} Not Allowed. Only GET is honored.", 
155:                                  405, 
156:                                  { "Allow" => "GET" }).finish
157:       end
158:     end

formulate a directory index response

[Source]

    # File lib/heel/rackapp.rb, line 65
65:     def directory_index_response(req)
66:       response = ::Rack::Response.new
67:       dir_index = File.join(req.request_path, directory_index_html) 
68:       if File.file?(dir_index) and File.readable?(dir_index) then
69:         response['Content-Type']   = mime_map.mime_type_of(dir_index).to_s
70:         response['Content-Length'] = File.size(dir_index).to_s
71:         response.body              = File.open(dir_index)
72:       elsif directory_listing_allowed? then
73:         body                       = directory_indexer.index_page_for(req)
74:         response['Content-Type']   = 'text/html'
75:         response['Content-Length'] = body.length.to_s
76:         response.body             << body
77:       else
78:         return ::Heel::ErrorResponse.new(req.path_info,"Directory index is forbidden", 403).finish
79:       end
80:       return response.finish
81:     end

formulate a directory index response

[Source]

    # File lib/heel/rackapp.rb, line 65
65:     def directory_index_response(req)
66:       response = ::Rack::Response.new
67:       dir_index = File.join(req.request_path, directory_index_html) 
68:       if File.file?(dir_index) and File.readable?(dir_index) then
69:         response['Content-Type']   = mime_map.mime_type_of(dir_index).to_s
70:         response['Content-Length'] = File.size(dir_index).to_s
71:         response.body              = File.open(dir_index)
72:       elsif directory_listing_allowed? then
73:         body                       = directory_indexer.index_page_for(req)
74:         response['Content-Type']   = 'text/html'
75:         response['Content-Length'] = body.length.to_s
76:         response.body             << body
77:       else
78:         return ::Heel::ErrorResponse.new(req.path_info,"Directory index is forbidden", 403).finish
79:       end
80:       return response.finish
81:     end

[Source]

    # File lib/heel/rackapp.rb, line 46
46:     def directory_index_template_file
47:       @directory_index_template_file ||= Heel::Configuration.data_path("listing.rhtml")
48:     end

[Source]

    # File lib/heel/rackapp.rb, line 46
46:     def directory_index_template_file
47:       @directory_index_template_file ||= Heel::Configuration.data_path("listing.rhtml")
48:     end

[Source]

    # File lib/heel/rackapp.rb, line 50
50:     def directory_indexer
51:       indexer_ignore = ( ignore_globs + [ document_root] ).flatten
52:       @directory_indexer ||= DirectoryIndexer.new( directory_index_template_file, @options )
53:     end

[Source]

    # File lib/heel/rackapp.rb, line 50
50:     def directory_indexer
51:       indexer_ignore = ( ignore_globs + [ document_root] ).flatten
52:       @directory_indexer ||= DirectoryIndexer.new( directory_index_template_file, @options )
53:     end

[Source]

    # File lib/heel/rackapp.rb, line 34
34:     def directory_listing_allowed?
35:       @directory_listing_allowed
36:     end

[Source]

    # File lib/heel/rackapp.rb, line 34
34:     def directory_listing_allowed?
35:       @directory_listing_allowed
36:     end

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.

[Source]

     # File lib/heel/rackapp.rb, line 88
 88:     def file_response(req)
 89:       response = ::Rack::Response.new
 90: 
 91:       response['Last-Modified'] = req.stat.mtime.rfc822
 92: 
 93:       if highlighting? and req.highlighting? then 
 94:         # only do a coderay type check if we are going to use coderay in the
 95:         # response
 96:         code_ray_type = CodeRay::FileType[req.request_path, true] 
 97:         if code_ray_type and (code_ray_type != :html) then
 98:           body = "<html>\n<head>\n<title>\#{req.path_info}</title>\n<!-- CodeRay syntax highlighting CSS -->\n<link rel=\"stylesheet\" href=\"/heel_css/coderay-cycnus.css\" type=\"text/css\" />\n</head>\n<body>\n<div class=\"CodeRay\">\n<pre>\n\#{CodeRay.scan_file(req.request_path,:auto).html({:line_numbers => :inline})}\n</pre>\n</div>\n</body>\n</html>\n"
 99:           response['Content-Type']    = 'text/html'
100:           response['Content-Length']  = body.length.to_s
101:           response.body << body
102:           return response.finish
103:         end
104:       end
105: 
106:       # fall through to a default file return
107:       # 
108: 
109:       file_type                   = mime_map.mime_type_of(req.request_path)
110:       response['Content-Type']    = file_type.to_s
111:       response['Content-Length']  = req.stat.size.to_s
112: 
113:       return response.finish do 
114:         File.open(req.request_path) do |f|
115:           while p = f.read(8192)
116:             response.write p
117:           end
118:         end
119:       end
120: 
121:     end

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.

[Source]

     # File lib/heel/rackapp.rb, line 88
 88:     def file_response(req)
 89:       response = ::Rack::Response.new
 90: 
 91:       response['Last-Modified'] = req.stat.mtime.rfc822
 92: 
 93:       if highlighting? and req.highlighting? then 
 94:         # only do a coderay type check if we are going to use coderay in the
 95:         # response
 96:         code_ray_type = CodeRay::FileType[req.request_path, true] 
 97:         if code_ray_type and (code_ray_type != :html) then
 98:           body = "<html>\n<head>\n<title>\#{req.path_info}</title>\n<!-- CodeRay syntax highlighting CSS -->\n<link rel=\"stylesheet\" href=\"/heel_css/coderay-cycnus.css\" type=\"text/css\" />\n</head>\n<body>\n<div class=\"CodeRay\">\n<pre>\n\#{CodeRay.scan_file(req.request_path,:auto).html({:line_numbers => :inline})}\n</pre>\n</div>\n</body>\n</html>\n"
 99:           response['Content-Type']    = 'text/html'
100:           response['Content-Length']  = body.length.to_s
101:           response.body << body
102:           return response.finish
103:         end
104:       end
105: 
106:       # fall through to a default file return
107:       # 
108: 
109:       file_type                   = mime_map.mime_type_of(req.request_path)
110:       response['Content-Type']    = file_type.to_s
111:       response['Content-Length']  = req.stat.size.to_s
112: 
113:       return response.finish do 
114:         File.open(req.request_path) do |f|
115:           while p = f.read(8192)
116:             response.write p
117:           end
118:         end
119:       end
120: 
121:     end

[Source]

    # File lib/heel/rackapp.rb, line 38
38:     def highlighting?
39:       @highlighting
40:     end

[Source]

    # File lib/heel/rackapp.rb, line 38
38:     def highlighting?
39:       @highlighting
40:     end

[Source]

    # File lib/heel/rackapp.rb, line 42
42:     def mime_map
43:       @mime_map ||= Heel::MimeMap.new
44:     end

[Source]

    # File lib/heel/rackapp.rb, line 42
42:     def mime_map
43:       @mime_map ||= Heel::MimeMap.new
44:     end

[Source]

    # File lib/heel/rackapp.rb, line 56
56:     def should_ignore?(fname)
57:       ignore_globs.each do |glob|
58:         return true if ::File.fnmatch(glob,fname)
59:       end
60:       false 
61:     end

[Source]

    # File lib/heel/rackapp.rb, line 56
56:     def should_ignore?(fname)
57:       ignore_globs.each do |glob|
58:         return true if ::File.fnmatch(glob,fname)
59:       end
60:       false 
61:     end

[Validate]