Default session class – simple in-memory, cookie-based base-bones session.
Default configuration
The namespace that determines which subhash of the session is exposed to the application.
Configure the session class with the given options
, which
should be a Hash or an object that has a Hash-like interface.
Valid keys:
The name of the cookie that will be used for persisting the session key.
A hash of options for the session cookie; see Strelka::Cookie for available options.
# File lib/strelka/session/default.rb, line 116
def self::configure( config=nil )
if config
self.cookie_name = config[:cookie_name]
self.cookie_options = config[:cookie_options]
else
self.cookie_name = CONFIG_DEFAULTS[:cookie_name]
self.cookie_options = CONFIG_DEFAULTS[:cookie_options].dup
end
end
Delete the data associated with the given session_id
from
memory.
# File lib/strelka/session/default.rb, line 68
def self::delete_session_data( session_id )
self.sessions.delete( session_id )
end
Try to fetch a session ID from the specified request
,
returning nil
if there isn't one.
# File lib/strelka/session/default.rb, line 75
def self::get_existing_session_id( request )
cookie = request.cookies[ self.cookie_name ] or return nil
if cookie.value =~ /^([[:xdigit:]]+)$/i
return $1.untaint
else
self.log.warn "Request with a malformed session cookie: %p" % [ request ]
return nil
end
end
Fetch the session ID from the given request
, or create a new
one if the request doesn't have the necessary attributes.
# File lib/strelka/session/default.rb, line 89
def self::get_session_id( request=nil )
id = self.get_existing_session_id( request ) if request
return id || SecureRandom.hex
end
Return true
if the given request
has a session
token which corresponds to an existing session key.
# File lib/strelka/session/default.rb, line 97
def self::has_session_for?( request )
self.log.debug "Checking request (%s/%d) for session." % [ request.sender_id, request.conn_id ]
id = self.get_existing_session_id( request ) or return false
self.log.debug " got a session ID: %p" % [ id ]
return @sessions.key?( id )
end
Load a session instance from storage using the given
session_id
and return it. Returns nil
if no
session could be loaded.
# File lib/strelka/session/default.rb, line 56
def self::load_session_data( session_id )
return Strelka::DataUtilities.deep_copy( self.sessions[session_id] )
end
Create a new session store using the given hash
for initial
values.
# File lib/strelka/session/default.rb, line 132
def initialize( session_id=nil, initial_values={} )
@hash = Hash.new {|h,k| h[k] = {} }
@hash.merge!( initial_values )
@namespace = nil
super
end
Save the given data
to memory associated with the given
session_id
.
# File lib/strelka/session/default.rb, line 62
def self::save_session_data( session_id, data )
self.sessions[ session_id ] = Strelka::DataUtilities.deep_copy( data )
end
Configurability API – use the 'defaultsession' section of the config
# File lib/strelka/session/default.rb, line 24
config_key :defaultsession
Delete the session from storage and expire the session cookie from the
given response
.
# File lib/strelka/session/default.rb, line 192
def destroy( response )
self.log.debug "Destroying session %s" % [ self.session_id ]
self.class.delete_session_data( self.session_id )
self.log.debug "Adding expired session cookie to the response"
session_cookie = Strelka::Cookie.new(
self.class.cookie_name,
self.session_id,
self.class.cookie_options || {}
)
session_cookie.expire!
response.cookies << session_cookie
end
Set the current namespace for session values to namespace
.
Setting namespace
to nil
exposes the toplevel
namespace (keys are named namespaces)
# File lib/strelka/session/default.rb, line 170
def namespace=( namespace )
@namespace = namespace.nil? ? nil : namespace.to_sym
end
Delegate Hash methods to whichever Hash is the current namespace
# File lib/strelka/session/default.rb, line 164
def_method_delegators :namespaced_hash, :[], :[]=, :delete, :key?
Save the session to storage and add the session cookie to the given
response
.
# File lib/strelka/session/default.rb, line 176
def save( response )
self.log.debug "Saving session %s" % [ self.session_id ]
self.class.save_session_data( self.session_id, @hash )
self.log.debug "Adding session cookie to the response."
session_cookie = Strelka::Cookie.new(
self.class.cookie_name,
self.session_id,
self.class.cookie_options || {}
)
response.cookies << session_cookie
end
In-memory session store
# File lib/strelka/session/default.rb, line 42
singleton_attr_reader :sessions
Create a Proc that will act as a getter for the given key
# File lib/strelka/session/default.rb, line 254
def make_getter( key )
return Proc.new { self.namespaced_hash[key.to_sym] }
end
Create a Proc that will act as a setter for the given key
# File lib/strelka/session/default.rb, line 248
def make_setter( key )
return Proc.new {|new_value| self.namespaced_hash[ key.to_sym ] = new_value }
end
Proxy method: handle getting/setting headers via methods instead of the index operator.
# File lib/strelka/session/default.rb, line 228
def method_missing( sym, *args )
# work magic
return super unless sym.to_s =~ /^([a-z]\w+)(=)?$/
# If it's an assignment, the (=)? will have matched
key, assignment = $1, $2
method_body = nil
if assignment
method_body = self.make_setter( key )
else
method_body = self.make_getter( key )
end
self.class.send( :define_method, sym, &method_body )
return self.method( sym ).call( *args )
end