Parent

Class Index [+]

Quicksearch

Rack::Session::Abstract::ID

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.

Constants

DEFAULT_OPTIONS
(Not documented)

Attributes

key[R]

(Not documented)

default_options[R]

(Not documented)

Public Class Methods

new(app, options={}) click to toggle source

(Not documented)

    # File lib/rack/session/abstract/id.rb, line 51
51:         def initialize(app, options={})
52:           @app = app
53:           @key = options[:key] || "rack.session"
54:           @default_options = self.class::DEFAULT_OPTIONS.merge(options)
55:         end

Public Instance Methods

call(env) click to toggle source

(Not documented)

    # File lib/rack/session/abstract/id.rb, line 57
57:         def call(env)
58:           context(env)
59:         end
context(env, app=@app) click to toggle source

(Not documented)

    # File lib/rack/session/abstract/id.rb, line 61
61:         def context(env, app=@app)
62:           load_session(env)
63:           status, headers, body = app.call(env)
64:           commit_session(env, status, headers, body)
65:         end

Private Instance Methods

commit_session(env, status, headers, body) click to toggle source

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:             [status, headers, body]
111:           elsif options[:defer] and not options[:renew]
112:             env["rack.errors"].puts("Defering cookie for #{session_id}") if $VERBOSE
113:             [status, headers, body]
114:           else
115:             cookie = Hash.new
116:             cookie[:value] = session_id
117:             cookie[:expires] = Time.now + options[:expire_after] unless options[:expire_after].nil?
118:             response = Rack::Response.new(body, status, headers)
119:             response.set_cookie(@key, cookie.merge(options))
120:             response.to_a
121:           end
122:         end
generate_sid() click to toggle source

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
get_session(env, sid) click to toggle source

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 129
129:         def get_session(env, sid)
130:           raise '#get_session not implemented.'
131:         end
load_session(env) click to toggle source

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
set_session(env, sid, session, options) click to toggle source

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 136
136:         def set_session(env, sid, session, options)
137:           raise '#set_session not implemented.'
138:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.