5 module EventManager # :nodoc:
9 @events.clear if @events
16 def register_event(event)
17 (@events ||= []) << event
20 def determine_event(verb, path, if_nil = :present_error)
21 event = events.find(method(if_nil)) do |e|
22 e.verb == verb && e.recognize(path)
27 determine_event(:get, '404', :not_found)
31 Event.new(:get, 'not_found', false) do
34 if request.path_info == '/' && request.request_method == 'GET'
35 erb :default_index, :views_directory => SINATRA_ROOT + '/files'
37 erb :not_found, :views_directory => SINATRA_ROOT + '/files'
46 cattr_accessor :logger
47 cattr_accessor :after_filters
51 self.after_filters = []
53 def self.after_attend(filter)
54 after_filters << filter
57 after_attend :log_event
59 attr_reader :path, :verb
61 def initialize(verb, path, register = true, &block)
64 @route = Route.new(path)
66 EventManager.register_event(self) if register
70 request.params.merge!(@route.params)
71 context = EventContext.new(request)
73 result = run_safely { context.instance_eval(&@block) if @block }
74 context.body context.body || result || ''
78 run_safely { run_through_after_filters(context) }
84 @route.recognize(path)
91 @@mutex.synchronize do
99 def run_through_after_filters(context)
100 after_filters.each { |filter| context.send(filter) }
105 class StaticEvent < Event # :nodoc:
107 def initialize(path, root, register = true)
109 super(:get, path, register)
113 File.exists?(physical_path_for(path))
116 def physical_path_for(path)
117 path.gsub(/^#{@path}/, @root)
121 @filename = physical_path_for(request.path_info)
122 context = EventContext.new(request)
124 context.header 'Content-Type' => MIME_TYPES[File.extname(@filename)[1..-1]]
125 context.header 'Content-Length' => File.size(@filename).to_s
130 File.open(@filename, "rb") do |file|
131 while part = file.read(8192)
140 "ai" => "application/postscript",
141 "asc" => "text/plain",
142 "avi" => "video/x-msvideo",
143 "bin" => "application/octet-stream",
144 "bmp" => "image/bmp",
145 "class" => "application/octet-stream",
146 "cer" => "application/pkix-cert",
147 "crl" => "application/pkix-crl",
148 "crt" => "application/x-x509-ca-cert",
149 #"crl" => "application/x-pkcs7-crl",
151 "dms" => "application/octet-stream",
152 "doc" => "application/msword",
153 "dvi" => "application/x-dvi",
154 "eps" => "application/postscript",
155 "etx" => "text/x-setext",
156 "exe" => "application/octet-stream",
157 "gif" => "image/gif",
158 "htm" => "text/html",
159 "html" => "text/html",
160 "jpe" => "image/jpeg",
161 "jpeg" => "image/jpeg",
162 "jpg" => "image/jpeg",
163 "lha" => "application/octet-stream",
164 "lzh" => "application/octet-stream",
165 "mov" => "video/quicktime",
166 "mpe" => "video/mpeg",
167 "mpeg" => "video/mpeg",
168 "mpg" => "video/mpeg",
169 "pbm" => "image/x-portable-bitmap",
170 "pdf" => "application/pdf",
171 "pgm" => "image/x-portable-graymap",
172 "png" => "image/png",
173 "pnm" => "image/x-portable-anymap",
174 "ppm" => "image/x-portable-pixmap",
175 "ppt" => "application/vnd.ms-powerpoint",
176 "ps" => "application/postscript",
177 "qt" => "video/quicktime",
178 "ras" => "image/x-cmu-raster",
179 "rb" => "text/plain",
180 "rd" => "text/plain",
181 "rtf" => "application/rtf",
182 "sgm" => "text/sgml",
183 "sgml" => "text/sgml",
184 "tif" => "image/tiff",
185 "tiff" => "image/tiff",
186 "txt" => "text/plain",
187 "xbm" => "image/x-xbitmap",
188 "xls" => "application/vnd.ms-excel",
190 "xpm" => "image/x-xpixmap",
191 "xwd" => "image/x-xwindowdump",
192 "zip" => "application/zip",