ID sets up a basic framework for implementing an id based sessioning service. Cookies sent to the client for maintaining sessions will only contain an id reference. Only get_session and set_session are required to be overwritten.
All parameters are optional.
These options can be set on a per request basis, at the location of env. Additionally the id of the session can be found within the options hash at the key :id. It is highly not recommended to change its value.
Is Rack::Utils::Context compatible.
Acquires the session from the environment and the session id from the session options and passes them to set_session. If successful and the :defer option is not true, a cookie will be added to the response with the session’s id.
# File lib/rack/session/abstract/id.rb, line 103
103: def commit_session(env, status, headers, body)
104: session = env['rack.session']
105: options = env['rack.session.options']
106: session_id = options[:id]
107:
108: if not session_id = set_session(env, session_id, session, options)
109: env["rack.errors"].puts("Warning! #{self.class.name} failed to save session. Content dropped.")
110: elsif options[:defer] and not options[:renew]
111: env["rack.errors"].puts("Defering cookie for #{session_id}") if $VERBOSE
112: else
113: cookie = Hash.new
114: cookie[:value] = session_id
115: cookie[:expires] = Time.now + options[:expire_after] unless options[:expire_after].nil?
116: Utils.set_cookie_header!(headers, @key, cookie.merge(options))
117: end
118:
119: [status, headers, body]
120: end
Generate a new session id using Ruby rand. The size of the session id is controlled by the :sidbits option. Monkey patch this to use custom methods for session id generation.
# File lib/rack/session/abstract/id.rb, line 73
73: def generate_sid
74: "%0#{@default_options[:sidbits] / 4}x" %
75: rand(2**@default_options[:sidbits] - 1)
76: end
All thread safety and session retrival proceedures should occur here. Should return [session_id, session]. If nil is provided as the session id, generation of a new valid id should occur within.
# File lib/rack/session/abstract/id.rb, line 127
127: def get_session(env, sid)
128: raise '#get_session not implemented.'
129: end
Extracts the session id from provided cookies and passes it and the environment to get_session. It then sets the resulting session into ‘rack.session’, and places options and session metadata into ‘rack.session.options’.
# File lib/rack/session/abstract/id.rb, line 83
83: def load_session(env)
84: request = Rack::Request.new(env)
85: session_id = request.cookies[@key]
86:
87: begin
88: session_id, session = get_session(env, session_id)
89: env['rack.session'] = session
90: rescue
91: env['rack.session'] = Hash.new
92: end
93:
94: env['rack.session.options'] = @default_options.
95: merge(:id => session_id)
96: end
All thread safety and session storage proceedures should occur here. Should return true or false dependant on whether or not the session was saved or not.
# File lib/rack/session/abstract/id.rb, line 134
134: def set_session(env, sid, session, options)
135: raise '#set_session not implemented.'
136: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.