lib/merb/merb_handler.rb in merb-0.0.5 vs lib/merb/merb_handler.rb in merb-0.0.6

- old
+ new

@@ -1,23 +1,23 @@ -require 'sync' + class MerbHandler < Mongrel::HttpHandler @@file_only_methods = ["GET","HEAD"] # take the name of a directory and use that as the doc root or public # directory of your site. This is set to the root of your merb app + '/public' # by default. def initialize(dir, opts = {}) @files = Mongrel::DirHandler.new(dir,false) - @guard = Sync.new + @guard = Mutex.new end # process incoming http requests and do a number of things # 1. check for rails style cached pages. add .html to the # url and see if there is a static file in public that matches. # serve that file directly without invoking Merb and be done with it. # 2. Serve static asset and html files directly from public/ if - # they exist and fall back to Merb otherwise + # they exist. # 3. If none of the above apply, we take apart the request url # and feed it into Merb::RouteMatcher to let it decide which # controller and method will serve the request. # 4. after the controller has done its thing, we check for the # X-SENDFILE header. if you set this header to the path to a file @@ -56,27 +56,18 @@ controller, action = handle(request) MERB_LOGGER.info("Routing to controller: #{controller.class} action: #{action}") output = nil # synchronize here because this is where ActiveRecord or your db # calls will be run in your controller methods. - @guard.synchronize(:EX) { - output = - if (controller && controller.kind_of?(Merb::Controller)) - if action - controller.send(action) - else - controller.to_s - end - else - nil - end + @guard.synchronize { + output = controller.dispatch(action) } rescue Exception => e response.start(500) do |head,out| head["Content-Type"] = "text/html" - MERB_LOGGER.info(ex = exception(e)) - out << ex + MERB_LOGGER.info(exception(e)) + out << html_exception(e) end return end sendfile, clength = nil @@ -105,11 +96,11 @@ response.send_header response.send_file(sendfile) else MERB_LOGGER.info("Response status: #{response.status}\n\n") # render response from successful controller - response.send_status(output.length) + response.send_status((output||'').length) response.send_header response.write(output) end end end @@ -132,12 +123,12 @@ ["<html><body>Error: no route matches!</body></html>", nil] end end # take a controller class name string and reload or require - # the right controller file then upcase the first letter and - # trun it into a new object passing in the request and response. + # the right controller file then CamelCase it and turn it + # into a new object passing in the request and response. # this is where your Merb::Controller is instantiated. def instantiate_controller(controller_name, req, env, params) if !File.exist?(Merb::Server.config[:dist_root]+"/app/controllers/#{controller_name.snake_case}.rb") return Object.const_get(:Noroutefound).new(req, env, params) end @@ -149,11 +140,16 @@ raise $! end end # format exception message for browser display - def exception(e) + def html_exception(e) "<html><h2>Merb Error!</h2><p>#{ e.message } - (#{ e.class })\n" << "#{(e.backtrace or []).join('<br />')}</p></html>" end + + def exception(e) + "#{ e.message } - (#{ e.class })\n" << + "#{(e.backtrace or []).join("\n")}" + end end \ No newline at end of file