0
require 'action_controller/cgi_ext'
0
module ActionController #:nodoc:
0
- # Process a request extracted from a CGI object and return a response. Pass false as <tt>session_options</tt> to disable
0
- # sessions (large performance increase if sessions are not needed). The <tt>session_options</tt> are the same as for CGI::Session:
0
- # * <tt>:database_manager</tt> - standard options are CGI::Session::FileStore, CGI::Session::MemoryStore, and CGI::Session::PStore
0
- # (default). Additionally, there is CGI::Session::DRbStore and CGI::Session::ActiveRecordStore. Read more about these in
0
- # lib/action_controller/session.
0
- # * <tt>:session_key</tt> - the parameter name used for the session id. Defaults to '_session_id'.
0
- # * <tt>:session_id</tt> - the session id to use. If not provided, then it is retrieved from the +session_key+ cookie, or
0
- # automatically generated for a new session.
0
- # * <tt>:new_session</tt> - if true, force creation of a new session. If not set, a new session is only created if none currently
0
- # exists. If false, a new session is never created, and if none currently exists and the +session_id+ option is not set,
0
- # an ArgumentError is raised.
0
- # * <tt>:session_expires</tt> - the time the current session expires, as a Time object. If not set, the session will continue
0
- # * <tt>:session_domain</tt> - the hostname domain for which this session is valid. If not set, defaults to the hostname of the
0
- # * <tt>:session_secure</tt> - if +true+, this session will only work over HTTPS.
0
- # * <tt>:session_path</tt> - the path for which this session applies. Defaults to the directory of the CGI script.
0
- # * <tt>:cookie_only</tt> - if +true+ (the default), session IDs will only be accepted from cookies and not from
0
- # the query string or POST parameters. This protects against session fixation attacks.
0
- def self.process_cgi(cgi = CGI.new, session_options = {})
0
- new.process_cgi(cgi, session_options)
0
- def process_cgi(cgi, session_options = {}) #:nodoc:
0
- process(CgiRequest.new(cgi, session_options), CgiResponse.new(cgi)).out
0
- class CgiRequest < AbstractRequest #:nodoc:
0
- attr_accessor :cgi, :session_options
0
- class SessionFixationAttempt < StandardError #:nodoc:
0
- DEFAULT_SESSION_OPTIONS = {
0
- :database_manager => CGI::Session::CookieStore, # store data in cookie
0
- :prefix => "ruby_sess.", # prefix session file names
0
- :session_path => "/", # available to all paths in app
0
- :session_key => "_session_id",
0
- :session_http_only=> true
0
- def initialize(cgi, session_options = {})
0
- @session_options = session_options
0
- @env = @cgi.__send__(:env_table)
0
- qs = @cgi.query_string if @cgi.respond_to?(:query_string)
0
- def body_stream #:nodoc:
0
- unless defined?(@session)
0
- if @session_options == false
0
- stale_session_check! do
0
- if cookie_only? && query_parameters[session_options_with_string_keys['session_key']]
0
- raise SessionFixationAttempt
0
- case value = session_options_with_string_keys['new_session']
0
- @session = new_session
0
- @session = CGI::Session.new(@cgi, session_options_with_string_keys)
0
- # CGI::Session raises ArgumentError if 'new_session' == false
0
- # and no session cookie or query param is present.
0
- @session = CGI::Session.new(@cgi, session_options_with_string_keys)
0
- raise ArgumentError, "Invalid new_session option: #{value}"
0
- @session['__valid_session']
0
- @session.delete if defined?(@session) && @session.is_a?(CGI::Session)
0
- @session = new_session
0
- def method_missing(method_id, *arguments)
0
- @cgi.__send__(method_id, *arguments) rescue super
0
- # Delete an old session if it exists then create a new one.
0
- if @session_options == false
0
- CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => false)).delete rescue nil
0
- CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => true))
0
- session_options_with_string_keys['cookie_only']
0
+ def self.dispatch_cgi(app, cgi, out = $stdout)
0
+ env = cgi.__send__(:env_table)
0
+ env.delete "HTTP_CONTENT_LENGTH"
0
- def stale_session_check!
0
- rescue ArgumentError => argument_error
0
- if argument_error.message =~ %r{undefined class/module ([\w:]*\w)}
0
- # Note that the regexp does not allow $1 to end with a ':'
0
- rescue LoadError, NameError => const_error
0
- raise ActionController::SessionRestoreError, <<-end_msg
0
-Session contains objects whose class definition isn\'t available.
0
-Remember to require the classes for all objects kept in the session.
0
-(Original exception: #{const_error.message} [#{const_error.class}])
0
+ cgi.stdinput.extend ProperStream
0
+ env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
0
- def session_options_with_string_keys
0
- @session_options_with_string_keys ||= DEFAULT_SESSION_OPTIONS.merge(@session_options).stringify_keys
0
+ "rack.version" => [0,1],
0
+ "rack.input" => cgi.stdinput,
0
+ "rack.errors" => $stderr,
0
+ "rack.multithread" => false,
0
+ "rack.multiprocess" => true,
0
+ "rack.run_once" => false,
0
+ "rack.url_scheme" => ["yes", "on", "1"].include?(env["HTTPS"]) ? "https" : "http"
0
- class CgiResponse < AbstractResponse #:nodoc:
0
- def out(output = $stdout)
0
- output.binmode if output.respond_to?(:binmode)
0
- output.sync = false if output.respond_to?(:sync=)
0
+ env["QUERY_STRING"] ||= ""
0
+ env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
0
+ env["REQUEST_PATH"] ||= "/"
0
+ env.delete "PATH_INFO" if env["PATH_INFO"] == ""
0
+ status, headers, body = app.call(env)
0
- output.write(@cgi.header(@headers))
0
- if @cgi.__send__(:env_table)['REQUEST_METHOD'] == 'HEAD'
0
- elsif @body.respond_to?(:call)
0
- # Flush the output now in case the @body Proc uses
0
- output.flush if output.respond_to?(:flush)
0
- @body.call(self, output)
0
- output.flush if output.respond_to?(:flush)
0
- rescue Errno::EPIPE, Errno::ECONNRESET
0
- # lost connection to parent process, ignore output
0
+ out.binmode if out.respond_to?(:binmode)
0
+ out.sync = false if out.respond_to?(:sync=)
0
+ headers['Status'] = status.to_s
0
+ out.write(cgi.header(headers))
0
+ out.flush if out.respond_to?(:flush)
0
+ body.close if body.respond_to?(:close)
0
+ class CgiRequest #:nodoc:
0
+ DEFAULT_SESSION_OPTIONS = {
0
+ :database_manager => CGI::Session::CookieStore,
0
+ :prefix => "ruby_sess.",
0
+ :session_key => "_session_id",
0
+ :session_http_only => true
Finally …
Excellent. Leaner and meaner!
it’s not finished yet, but:
http://github.com/raggi/rails/commit/935d49846de3b194e61b049cd220e54fd11002cf
I’ll have to merge this one :-/
@raggi we need to get in touch. I don’t want to start hacking on some of that stuff if you’ve already done the hard work.
Please email me, josh at joshpeek dot com