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
48 cattr_accessor :before_filters
52 self.before_filters = []
53 self.after_filters = []
56 self.before_filters.clear
57 self.after_filters.clear
60 def self.before_attend(method_name = nil, options ={}, &block)
61 setup_filter(:before_filters, method_name, options, &block)
64 def self.after_attend(method_name = nil, options = {}, &block)
65 setup_filter(:after_filters, method_name, options, &block)
68 def self.setup_filter(filter_set_name, method_name, options = {}, &block)
69 raise "Must specify method or block" if method_name.nil? and !block_given?
70 value = if block_given?
75 insert_index = options[:infront] == true ? 0 : -1
76 send(filter_set_name).insert(insert_index, value)
79 attr_reader :path, :verb
81 def initialize(verb, path, register = true, &block)
84 @route = Route.new(path)
86 EventManager.register_event(self) if register
90 request.params.merge!(@route.params)
91 context = EventContext.new(request)
93 caught = catch(:halt) do
94 call_filters(before_filters, context)
97 when :filter_chain_completed
99 context.instance_eval(&@block) if @block
108 context.status caught
110 context.body context.body || body || ''
111 call_filters(after_filters, context)
119 @route.recognize(path)
125 if Options.use_mutex?
126 @@mutex.synchronize do
135 # calls a filter chain according to rules.
136 def call_filters(filter_set, context)
137 filter_set.each do |filter|
142 context.instance_eval(&filter)
145 :filter_chain_completed
150 class StaticEvent < Event # :nodoc:
152 def initialize(path, root, register = true)
154 super(:get, path, register)
158 filename = physical_path_for(path)
159 File.exists?(filename) && File.file?(filename)
162 def physical_path_for(path)
163 path.gsub(/^#{@path}/, @root)
167 @filename = physical_path_for(request.path_info)
168 context = EventContext.new(request)
170 context.header 'Content-Type' => MIME_TYPES[File.extname(@filename)[1..-1]]
171 context.header 'Content-Length' => File.size(@filename).to_s
176 File.open(@filename, "rb") do |file|
177 while part = file.read(8192)
186 "ai" => "application/postscript",
187 "asc" => "text/plain",
188 "avi" => "video/x-msvideo",
189 "bin" => "application/octet-stream",
190 "bmp" => "image/bmp",
191 "class" => "application/octet-stream",
192 "cer" => "application/pkix-cert",
193 "crl" => "application/pkix-crl",
194 "crt" => "application/x-x509-ca-cert",
195 #"crl" => "application/x-pkcs7-crl",
197 "dms" => "application/octet-stream",
198 "doc" => "application/msword",
199 "dvi" => "application/x-dvi",
200 "eps" => "application/postscript",
201 "etx" => "text/x-setext",
202 "exe" => "application/octet-stream",
203 "gif" => "image/gif",
204 "htm" => "text/html",
205 "html" => "text/html",
206 "jpe" => "image/jpeg",
207 "jpeg" => "image/jpeg",
208 "jpg" => "image/jpeg",
209 "lha" => "application/octet-stream",
210 "lzh" => "application/octet-stream",
211 "mov" => "video/quicktime",
212 "mpe" => "video/mpeg",
213 "mpeg" => "video/mpeg",
214 "mpg" => "video/mpeg",
215 "pbm" => "image/x-portable-bitmap",
216 "pdf" => "application/pdf",
217 "pgm" => "image/x-portable-graymap",
218 "png" => "image/png",
219 "pnm" => "image/x-portable-anymap",
220 "ppm" => "image/x-portable-pixmap",
221 "ppt" => "application/vnd.ms-powerpoint",
222 "ps" => "application/postscript",
223 "qt" => "video/quicktime",
224 "ras" => "image/x-cmu-raster",
225 "rb" => "text/plain",
226 "rd" => "text/plain",
227 "rtf" => "application/rtf",
228 "sgm" => "text/sgml",
229 "sgml" => "text/sgml",
230 "tif" => "image/tiff",
231 "tiff" => "image/tiff",
232 "txt" => "text/plain",
233 "xbm" => "image/x-xbitmap",
234 "xls" => "application/vnd.ms-excel",
236 "xpm" => "image/x-xpixmap",
237 "xwd" => "image/x-xwindowdump",
238 "zip" => "application/zip",