diff options
author | Evan Weaver <eweaver@twitter.com> | 2008-11-22 18:59:21 -0800 |
---|---|---|
committer | Evan Weaver <eweaver@twitter.com> | 2008-11-22 18:59:21 -0800 |
commit | 3e1c8c363126814b60c164922ffa26b8227defda (patch) | |
tree | fc02630aed03cfd6f13aad8074d5459020e221d2 /lib | |
parent | 559edfb1fbf18e9496a4235831fd49c5b0d8b459 (diff) | |
parent | c365ba16d12a14bdf1cc50a26f67dd3b45f5a4d8 (diff) | |
download | unicorn-3e1c8c363126814b60c164922ffa26b8227defda.tar.gz |
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mongrel/camping.rb | 107 | ||||
-rw-r--r-- | lib/mongrel/debug.rb | 203 | ||||
-rw-r--r-- | lib/mongrel/gems.rb | 22 | ||||
-rw-r--r-- | lib/mongrel/rails.rb | 185 |
4 files changed, 0 insertions, 517 deletions
diff --git a/lib/mongrel/camping.rb b/lib/mongrel/camping.rb deleted file mode 100644 index 31bd196..0000000 --- a/lib/mongrel/camping.rb +++ /dev/null @@ -1,107 +0,0 @@ -# Copyright (c) 2005 Zed A. Shaw -# You can redistribute it and/or modify it under the same terms as Ruby. -# -# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html -# for more information. - -require 'mongrel' - - -module Mongrel - # Support for the Camping micro framework at http://camping.rubyforge.org - # This implements the unusually long Postamble that Camping usually - # needs and shrinks it down to just a single line or two. - # - # Your Postamble would now be: - # - # Mongrel::Camping::start("0.0.0.0",3001,"/tepee",Tepee).join - # - # If you wish to get fancier than this then you can use the - # Camping::CampingHandler directly instead and do your own - # wiring: - # - # h = Mongrel::HttpServer.new(server, port) - # h.register(uri, CampingHandler.new(Tepee)) - # h.register("/favicon.ico", Mongrel::Error404Handler.new("")) - # - # I add the /favicon.ico since camping apps typically don't - # have them and it's just annoying anyway. - module Camping - - # This is a specialized handler for Camping applications - # that has them process the request and then translates - # the results into something the Mongrel::HttpResponse - # needs. - class CampingHandler < Mongrel::HttpHandler - attr_reader :files - attr_reader :guard - @@file_only_methods = ["GET","HEAD"] - - def initialize(klass) - @files = Mongrel::DirHandler.new(nil, false) - @guard = Mutex.new - @klass = klass - end - - def process(request, response) - if response.socket.closed? - return - end - - controller = nil - @guard.synchronize { - controller = @klass.run(request.body, request.params) - } - - sendfile, clength = nil - response.status = controller.status - controller.headers.each do |k, v| - if k =~ /^X-SENDFILE$/i - sendfile = v - elsif k =~ /^CONTENT-LENGTH$/i - clength = v.to_i - else - [*v].each do |vi| - response.header[k] = vi - end - end - end - - if sendfile - request.params[Mongrel::Const::PATH_INFO] = sendfile - @files.process(request, response) - elsif controller.body.respond_to? :read - response.send_status(clength) - response.send_header - while chunk = controller.body.read(16384) - response.write(chunk) - end - if controller.body.respond_to? :close - controller.body.close - end - else - body = controller.body.to_s - response.send_status(body.length) - response.send_header - response.write(body) - end - end - end - - # This is a convenience method that wires up a CampingHandler - # for your application on a given port and uri. It's pretty - # much all you need for a camping application to work right. - # - # It returns the Mongrel::HttpServer which you should either - # join or somehow manage. The thread is running when - # returned. - - def Camping.start(server, port, uri, klass) - h = Mongrel::HttpServer.new(server, port) - h.register(uri, CampingHandler.new(klass)) - h.register("/favicon.ico", Mongrel::Error404Handler.new("")) - h.run - return h - end - end -end diff --git a/lib/mongrel/debug.rb b/lib/mongrel/debug.rb deleted file mode 100644 index 2686c1e..0000000 --- a/lib/mongrel/debug.rb +++ /dev/null @@ -1,203 +0,0 @@ -# Copyright (c) 2005 Zed A. Shaw -# You can redistribute it and/or modify it under the same terms as Ruby. -# -# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html -# for more information. - -require 'logger' -require 'set' -require 'socket' -require 'fileutils' - -module MongrelDbg - SETTINGS = { :tracing => {}} - LOGGING = { } - - def MongrelDbg::configure(log_dir = File.join("log","mongrel_debug")) - FileUtils.mkdir_p(log_dir) - @log_dir = log_dir - $objects_out=open(File.join("log","mongrel_debug","objects.log"),"w") - $objects_out.puts "run,classname,last,count,delta,lenmean,lensd,lenmax" - $objects_out.sync = true - $last_stat = nil - $run_count = 0 - end - - - def MongrelDbg::trace(target, message) - if SETTINGS[:tracing][target] and LOGGING[target] - LOGGING[target].log(Logger::DEBUG, message) - end - end - - def MongrelDbg::begin_trace(target) - SETTINGS[:tracing][target] = true - if not LOGGING[target] - LOGGING[target] = Logger.new(File.join(@log_dir, "#{target.to_s}.log")) - end - MongrelDbg::trace(target, "TRACING ON #{Time.now}") - end - - def MongrelDbg::end_trace(target) - SETTINGS[:tracing][target] = false - MongrelDbg::trace(target, "TRACING OFF #{Time.now}") - LOGGING[target].close - LOGGING[target] = nil - end - - def MongrelDbg::tracing?(target) - SETTINGS[:tracing][target] - end -end - - - -$open_files = {} - -class IO - alias_method :orig_open, :open - alias_method :orig_close, :close - - def open(*arg, &blk) - $open_files[self] = args.inspect - orig_open(*arg,&blk) - end - - def close(*arg,&blk) - $open_files.delete self - orig_close(*arg,&blk) - end -end - - -module Kernel - alias_method :orig_open, :open - - def open(*arg, &blk) - $open_files[self] = arg[0] - orig_open(*arg,&blk) - end - - def log_open_files - open_counts = {} - $open_files.each do |f,args| - open_counts[args] ||= 0 - open_counts[args] += 1 - end - MongrelDbg::trace(:files, open_counts.to_yaml) - end -end - - - -module RequestLog - - # Just logs whatever requests it gets to STDERR (which ends up in the mongrel - # log when daemonized). - class Access < GemPlugin::Plugin "/handlers" - include Mongrel::HttpHandlerPlugin - - def process(request,response) - p = request.params - STDERR.puts "#{p['REMOTE_ADDR']} - [#{Time.now.httpdate}] \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"" - end - end - - - class Files < GemPlugin::Plugin "/handlers" - include Mongrel::HttpHandlerPlugin - - def process(request, response) - MongrelDbg::trace(:files, "#{Time.now} FILES OPEN BEFORE REQUEST #{request.params['PATH_INFO']}") - log_open_files - end - - end - - # stolen from Robert Klemme - class Objects < GemPlugin::Plugin "/handlers" - include Mongrel::HttpHandlerPlugin - - def process(request,response) - begin - stats = Hash.new(0) - lengths = {} - begin - ObjectSpace.each_object do |o| - begin - if o.respond_to? :length - len = o.length - lengths[o.class] ||= Mongrel::Stats.new(o.class) - lengths[o.class].sample(len) - end - rescue Object - end - - stats[o.class] += 1 - end - rescue Object # Ignore since ObjectSpace might not be loaded on JRuby - end - - stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v| - if $last_stat - delta = v - $last_stat[k] - if v > 10 and delta != 0 - if lengths[k] - $objects_out.printf "%d,%s,%d,%d,%d,%f,%f,%f\n", $run_count, k, $last_stat[k], v, delta,lengths[k].mean,lengths[k].sd,lengths[k].max - else - $objects_out.printf "%d,%s,%d,%d,%d,,,\n", $run_count, k, $last_stat[k], v, delta - end - end - end - end - - $run_count += 1 - $last_stat = stats - rescue Object - STDERR.puts "object.log ERROR: #$!" - end - end - end - - class Params < GemPlugin::Plugin "/handlers" - include Mongrel::HttpHandlerPlugin - - def process(request, response) - MongrelDbg::trace(:rails, "#{Time.now} REQUEST #{request.params['PATH_INFO']}") - MongrelDbg::trace(:rails, request.params.to_yaml) - end - - end - - class Threads < GemPlugin::Plugin "/handlers" - include Mongrel::HttpHandlerPlugin - - def process(request, response) - MongrelDbg::trace(:threads, "#{Time.now} REQUEST #{request.params['PATH_INFO']}") - begin - ObjectSpace.each_object do |obj| - begin - if obj.class == Mongrel::HttpServer - worker_list = obj.workers.list - - if worker_list.length > 0 - keys = "-----\n\tKEYS:" - worker_list.each {|t| keys << "\n\t\t-- #{t}: #{t.keys.inspect}" } - end - - MongrelDbg::trace(:threads, "#{obj.host}:#{obj.port} -- THREADS: #{worker_list.length} #{keys}") - end - rescue Object # Ignore since obj.class can sometimes take parameters - end - end - rescue Object # Ignore since ObjectSpace might not be loaded on JRuby - end - end - end -end - - -END { - MongrelDbg::trace(:files, "FILES OPEN AT EXIT") - log_open_files -} diff --git a/lib/mongrel/gems.rb b/lib/mongrel/gems.rb deleted file mode 100644 index eb6d0d6..0000000 --- a/lib/mongrel/gems.rb +++ /dev/null @@ -1,22 +0,0 @@ -module Mongrel - module Gems - class << self - - def require(library, version = nil) - begin - Kernel.require library - rescue LoadError, RuntimeError => e - begin - # ActiveSupport breaks 'require' by making it always return a true value - Kernel.require 'rubygems' - version ? gem(library, version) : gem(library) - retry - rescue Gem::LoadError, LoadError, RuntimeError - # puts "** #{library.inspect} could not be loaded" unless library == "mongrel_experimental" - end - end - end - - end - end -end
\ No newline at end of file diff --git a/lib/mongrel/rails.rb b/lib/mongrel/rails.rb deleted file mode 100644 index 7f66a5e..0000000 --- a/lib/mongrel/rails.rb +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) 2005 Zed A. Shaw -# You can redistribute it and/or modify it under the same terms as Ruby. -# -# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html -# for more information. - -require 'mongrel' -require 'cgi' - - -module Mongrel - module Rails - # Implements a handler that can run Rails and serve files out of the - # Rails application's public directory. This lets you run your Rails - # application with Mongrel during development and testing, then use it - # also in production behind a server that's better at serving the - # static files. - # - # The RailsHandler takes a mime_map parameter which is a simple suffix=mimetype - # mapping that it should add to the list of valid mime types. - # - # It also supports page caching directly and will try to resolve a request - # in the following order: - # - # * If the requested exact PATH_INFO exists as a file then serve it. - # * If it exists at PATH_INFO+".html" exists then serve that. - # * Finally, construct a Mongrel::CGIWrapper and run Dispatcher.dispatch to have Rails go. - # - # This means that if you are using page caching it will actually work with Mongrel - # and you should see a decent speed boost (but not as fast as if you use a static - # server like Apache or Litespeed). - class RailsHandler < Mongrel::HttpHandler - attr_reader :files - attr_reader :guard - @@file_only_methods = ["GET","HEAD"] - - def initialize(dir, mime_map = {}) - @files = Mongrel::DirHandler.new(dir,false) - @guard = Mutex.new - - # Register the requested MIME types - mime_map.each {|k,v| Mongrel::DirHandler::add_mime_type(k,v) } - end - - # Attempts to resolve the request as follows: - # - # * If the requested exact PATH_INFO exists as a file then serve it. - # * If it exists at PATH_INFO+".html" exists then serve that. - # * Finally, construct a Mongrel::CGIWrapper and run Dispatcher.dispatch to have Rails go. - def process(request, response) - return if response.socket.closed? - - path_info = request.params[Mongrel::Const::PATH_INFO] - rest_operator = request.params[Mongrel::Const::REQUEST_URI][/^#{Regexp.escape path_info}(;[^\?]+)/, 1].to_s - path_info.chomp!("/") - - page_cached = path_info + rest_operator + ActionController::Base.page_cache_extension - get_or_head = @@file_only_methods.include? request.params[Mongrel::Const::REQUEST_METHOD] - - if get_or_head and @files.can_serve(path_info) - # File exists as-is so serve it up - @files.process(request,response) - elsif get_or_head and @files.can_serve(page_cached) - # Possible cached page, serve it up - request.params[Mongrel::Const::PATH_INFO] = page_cached - @files.process(request,response) - else - begin - cgi = Mongrel::CGIWrapper.new(request, response) - cgi.handler = self - # We don't want the output to be really final until we're out of the lock - cgi.default_really_final = false - - @guard.synchronize { - @active_request_path = request.params[Mongrel::Const::PATH_INFO] - Dispatcher.dispatch(cgi, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, response.body) - @active_request_path = nil - } - - # This finalizes the output using the proper HttpResponse way - cgi.out("text/html",true) {""} - rescue Errno::EPIPE - response.socket.close - rescue Object => rails_error - STDERR.puts "#{Time.now}: Error calling Dispatcher.dispatch #{rails_error.inspect}" - STDERR.puts rails_error.backtrace.join("\n") - end - end - end - - # Does the internal reload for Rails. It might work for most cases, but - # sometimes you get exceptions. In that case just do a real restart. - def reload! - begin - @guard.synchronize { - $".replace $orig_dollar_quote - GC.start - Dispatcher.reset_application! - ActionController::Routing::Routes.reload - } - end - end - end - - # Creates Rails specific configuration options for people to use - # instead of the base Configurator. - class RailsConfigurator < Mongrel::Configurator - - # Creates a single rails handler and returns it so you - # can add it to a URI. You can actually attach it to - # as many URIs as you want, but this returns the - # same RailsHandler for each call. - # - # Requires the following options: - # - # * :docroot => The public dir to serve from. - # * :environment => Rails environment to use. - # * :cwd => The change to working directory - # - # And understands the following optional settings: - # - # * :mime => A map of mime types. - # - # Because of how Rails is designed you can only have - # one installed per Ruby interpreter (talk to them - # about thread safety). Because of this the first - # time you call this function it does all the config - # needed to get your Rails working. After that - # it returns the one handler you've configured. - # This lets you attach Rails to any URI(s) you want, - # but it still protects you from threads destroying - # your handler. - def rails(options={}) - - return @rails_handler if @rails_handler - - ops = resolve_defaults(options) - - # fix up some defaults - ops[:environment] ||= "development" - ops[:docroot] ||= "public" - ops[:mime] ||= {} - - $orig_dollar_quote = $".clone - ENV['RAILS_ENV'] = ops[:environment] - env_location = "#{ops[:cwd]}/config/environment" - require env_location - require 'dispatcher' - require 'mongrel/rails' - - ActionController::AbstractRequest.relative_url_root = ops[:prefix] if ops[:prefix] - - @rails_handler = RailsHandler.new(ops[:docroot], ops[:mime]) - end - - # Reloads Rails. This isn't too reliable really, but it - # should work for most minimal reload purposes. The only reliable - # way to reload properly is to stop and then start the process. - def reload! - if not @rails_handler - raise "Rails was not configured. Read the docs for RailsConfigurator." - end - - log "Reloading Rails..." - @rails_handler.reload! - log "Done reloading Rails." - - end - - # Takes the exact same configuration as Mongrel::Configurator (and actually calls that) - # but sets up the additional HUP handler to call reload!. - def setup_rails_signals(options={}) - ops = resolve_defaults(options) - setup_signals(options) - - if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ - # rails reload - trap("HUP") { log "HUP signal received."; reload! } - - log "Rails signals registered. HUP => reload (without restart). It might not work well." - end - end - end - end -end |