From 88d44ecd57b44cf4ac6b579d3d83d72793f0cc4b Mon Sep 17 00:00:00 2001 From: Evan Weaver Date: Sat, 31 Jan 2009 20:45:09 -0800 Subject: Remove CGIWrapper. --- lib/mongrel/cgi.rb | 180 --------------------------------------------------- lib/mongrel/rails.rb | 180 --------------------------------------------------- 2 files changed, 360 deletions(-) delete mode 100644 lib/mongrel/cgi.rb delete mode 100644 lib/mongrel/rails.rb (limited to 'lib') diff --git a/lib/mongrel/cgi.rb b/lib/mongrel/cgi.rb deleted file mode 100644 index 05d2d54..0000000 --- a/lib/mongrel/cgi.rb +++ /dev/null @@ -1,180 +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 'cgi' - -module Mongrel - # The beginning of a complete wrapper around Mongrel's internal HTTP processing - # system but maintaining the original Ruby CGI module. Use this only as a crutch - # to get existing CGI based systems working. It should handle everything, but please - # notify me if you see special warnings. This work is still very alpha so I need - # testers to help work out the various corner cases. - # - # The CGIWrapper.handler attribute is normally not set and is available for - # frameworks that need to get back to the handler. Rails uses this to give - # people access to the RailsHandler#files (DirHandler really) so they can - # look-up paths and do other things with the files managed there. - # - # In Rails you can get the real file for a request with: - # - # path = @request.cgi.handler.files.can_serve(@request['PATH_INFO']) - # - # Which is ugly but does the job. Feel free to write a Rails helper for that. - # Refer to DirHandler#can_serve for more information on this. - class CGIWrapper < ::CGI - public :env_table - attr_reader :head - attr_accessor :handler - # Set this to false if you want calls to CGIWrapper.out to not actually send - # the response until you force it. - attr_accessor :default_really_final - - # these are stripped out of any keys passed to CGIWrapper.header function - REMOVED_KEYS = [ "nph","status","server","connection","type", - "charset","length","language","expires"] - - # Takes an HttpRequest and HttpResponse object, plus any additional arguments - # normally passed to CGI. These are used internally to create a wrapper around - # the real CGI while maintaining Mongrel's view of the world. - def initialize(request, response, *args) - @request = request - @response = response - @args = *args - @input = request.body - @head = {} - @out_called = false - @default_really_final=true - super(*args) - end - - # The header is typically called to send back the header. In our case we - # collect it into a hash for later usage. - # - # nph -- Mostly ignored. It'll output the date. - # connection -- Completely ignored. Why is CGI doing this? - # length -- Ignored since Mongrel figures this out from what you write to output. - # - def header(options = "text/html") - # if they pass in a string then just write the Content-Type - if options.class == String - @head['Content-Type'] = options unless @head['Content-Type'] - else - # convert the given options into what Mongrel wants - @head['Content-Type'] = options['type'] || "text/html" - @head['Content-Type'] += "; charset=" + options['charset'] if options.has_key? "charset" if options['charset'] - - # setup date only if they use nph - @head['Date'] = CGI::rfc1123_date(Time.now) if options['nph'] - - # setup the server to use the default or what they set - @head['Server'] = options['server'] || env_table['SERVER_SOFTWARE'] - - # remaining possible options they can give - @head['Status'] = options['status'] if options['status'] - @head['Content-Language'] = options['language'] if options['language'] - @head['Expires'] = options['expires'] if options['expires'] - - # drop the keys we don't want anymore - REMOVED_KEYS.each {|k| options.delete(k) } - - # finally just convert the rest raw (which puts 'cookie' directly) - # 'cookie' is translated later as we write the header out - options.each{|k,v| @head[k] = v} - end - - # doing this fakes out the cgi library to think the headers are empty - # we then do the real headers in the out function call later - "" - end - - # Takes any 'cookie' setting and sends it over the Mongrel header, - # then removes the setting from the options. If cookie is an - # Array or Hash then it sends those on with .to_s, otherwise - # it just calls .to_s on it and hopefully your "cookie" can - # write itself correctly. - def send_cookies(to) - # convert the cookies based on the myriad of possible ways to set a cookie - if @head['cookie'] - cookie = @head['cookie'] - case cookie - when Array - cookie.each {|c| to['Set-Cookie'] = c.to_s } - when Hash - cookie.each_value {|c| to['Set-Cookie'] = c.to_s} - else - to['Set-Cookie'] = head['cookie'].to_s - end - - @head.delete('cookie') - end - - # @output_cookies seems to never be used, but we'll process it just in case - @output_cookies.each {|c| to['Set-Cookie'] = c.to_s } if @output_cookies - end - - # The dumb thing is people can call header or this or both and in any order. - # So, we just reuse header and then finalize the HttpResponse the right way. - # Status is taken from the various options and converted to what Mongrel needs - # via the CGIWrapper.status function. - # - # We also prevent Rails from actually doing the final send by adding a - # second parameter "really_final". Only Mongrel calls this after Rails - # is done. Since this will break other frameworks, it defaults to - # a different setting for rails (false) and (true) for others. - def out(options = "text/html", really_final=@default_really_final) - if @out_called || !really_final - # don't do it more than once or if it's not the really final call - return - end - - header(options) - - @response.start status do |head, body| - send_cookies(head) - - @head.each {|k,v| head[k] = v} - body.write(yield || "") - end - - @out_called = true - end - - # Computes the status once, but lazily so that people who call header twice - # don't get penalized. Because CGI insists on including the options status - # message in the status we have to do a bit of parsing. - def status - if not @status - stat = @head["Status"] - stat = stat.split(' ')[0] if stat - - @status = stat || "200" - end - - @status - end - - # Used to wrap the normal args variable used inside CGI. - def args - @args - end - - # Used to wrap the normal env_table variable used inside CGI. - def env_table - @request.params - end - - # Used to wrap the normal stdinput variable used inside CGI. - def stdinput - @input - end - - # The stdoutput should be completely bypassed but we'll drop a warning just in case - def stdoutput - Mongrel.logger.warn "WARNING: stdoutput used." - @response.body - end - end -end diff --git a/lib/mongrel/rails.rb b/lib/mongrel/rails.rb deleted file mode 100644 index 2cc5e41..0000000 --- a/lib/mongrel/rails.rb +++ /dev/null @@ -1,180 +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) - # We don't want the output to be really final until the dispatch returns a response. - cgi.default_really_final = false - - Dispatcher.dispatch(cgi, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, response.body) - - # 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 -- cgit v1.2.3-24-ge0c7