0
+# Adapter to run a Rails app with any supported Rack handler.
0
+# By default it will try to load the Rails application in the
0
+# current directory in the development environment.
0
+# root: Root directory of the Rails app
0
+# env: Rails environment to run in (development, production or test)
0
+# Based on http://fuzed.rubyforge.org/ Rails adapter
0
+ def initialize(options={})
0
+ @root = options[:root] || Dir.pwd
0
+ @env = options[:environment] || 'development'
0
+ @prefix = options[:prefix]
0
+ @file_server = Rack::File.new(::File.join(RAILS_ROOT, "public"))
0
+ ENV['RAILS_ENV'] = @env
0
+ require "#{@root}/config/environment"
0
+ ActionController::AbstractRequest.relative_url_root = @prefix if @prefix
0
+ # TODO refactor this in File#can_serve?(path) ??
0
+ full_path = ::File.join(@file_server.root, Utils.unescape(path))
0
+ ::File.file?(full_path) && ::File.readable?(full_path)
0
+ @file_server.call(env)
0
+ request = Request.new(env)
0
+ response = Response.new
0
+ session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
0
+ cgi = CGIWrapper.new(request, response)
0
+ Dispatcher.dispatch(cgi, session_options, response)
0
+ path = env['PATH_INFO'].chomp('/')
0
+ cached_path = (path.empty? ? 'index' : path) + ActionController::Base.page_cache_extension
0
+ if file_exist?(path) # Serve the file if it's there
0
+ elsif file_exist?(cached_path) # Serve the page cache if it's there
0
+ env['PATH_INFO'] = cached_path
0
+ else # No static file, let Rails handle it
0
+ class CGIWrapper < ::CGI
0
+ def initialize(request, response, *args)
0
+ def header(options = "text/html")
0
+ if options.is_a?(String)
0
+ @response['Content-Type'] = options unless @response['Content-Type']
0
+ @response['Content-Length'] = options.delete('Content-Length').to_s if options['Content-Length']
0
+ @response['Content-Type'] = options.delete('type') || "text/html"
0
+ @response['Content-Type'] += "; charset=" + options.delete('charset') if options['charset']
0
+ @response['Content-Language'] = options.delete('language') if options['language']
0
+ @response['Expires'] = options.delete('expires') if options['expires']
0
+ @response.status = options.delete('Status') if options['Status']
0
+ # Convert 'cookie' header to 'Set-Cookie' headers.
0
+ # Because Set-Cookie header can appear more the once in the response body,
0
+ # we store it in a line break seperated string that will be translated to
0
+ # multiple Set-Cookie header by the handler.
0
+ if cookie = options.delete('cookie')
0
+ when Array then cookie.each { |c| cookies << c.to_s }
0
+ when Hash then cookie.each { |_, c| cookies << c.to_s }
0
+ else cookies << cookie.to_s
0
+ @output_cookies.each { |c| cookies << c.to_s } if @output_cookies
0
+ @response['Set-Cookie'] = [@response['Set-Cookie'], cookies].compact.join("\n")
0
+ options.each { |k,v| @response[k] = v }
0
+ @params ||= @request.params
0
+ # Used to wrap the normal args variable used inside CGI.
0
+ # Used to wrap the normal env_table variable used inside CGI.
0
+ # Used to wrap the normal stdinput variable used inside CGI.
0
+ STDERR.puts "stdoutput should not be used."
Comments
No one has commented yet.