2017-02-18 00:52:16 -05:00
|
|
|
# frozen_string_literal: true
|
2003-08-28 23:52:24 -04:00
|
|
|
#
|
|
|
|
# cgi/session.rb - session support for cgi scripts
|
|
|
|
#
|
2001-01-09 02:26:21 -05:00
|
|
|
# Copyright (C) 2001 Yukihiro "Matz" Matsumoto
|
2000-05-01 05:42:38 -04:00
|
|
|
# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 00:53:16 -04:00
|
|
|
# Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
2003-08-28 23:52:24 -04:00
|
|
|
#
|
|
|
|
# Author: Yukihiro "Matz" Matsumoto
|
|
|
|
#
|
|
|
|
# Documentation: William Webber (william@williamwebber.com)
|
1999-11-25 04:03:08 -05:00
|
|
|
|
1999-12-02 01:58:52 -05:00
|
|
|
require 'cgi'
|
2003-07-21 11:34:18 -04:00
|
|
|
require 'tmpdir'
|
1999-11-25 04:03:08 -05:00
|
|
|
|
|
|
|
class CGI
|
2003-08-28 23:52:24 -04:00
|
|
|
|
2011-05-16 17:21:35 -04:00
|
|
|
# == Overview
|
|
|
|
#
|
|
|
|
# This file provides the CGI::Session class, which provides session
|
|
|
|
# support for CGI scripts. A session is a sequence of HTTP requests
|
|
|
|
# and responses linked together and associated with a single client.
|
|
|
|
# Information associated with the session is stored
|
|
|
|
# on the server between requests. A session id is passed between client
|
|
|
|
# and server with every request and response, transparently
|
|
|
|
# to the user. This adds state information to the otherwise stateless
|
|
|
|
# HTTP request/response protocol.
|
2003-08-28 23:52:24 -04:00
|
|
|
#
|
|
|
|
# == Lifecycle
|
|
|
|
#
|
|
|
|
# A CGI::Session instance is created from a CGI object. By default,
|
|
|
|
# this CGI::Session instance will start a new session if none currently
|
|
|
|
# exists, or continue the current session for this client if one does
|
|
|
|
# exist. The +new_session+ option can be used to either always or
|
|
|
|
# never create a new session. See #new() for more details.
|
|
|
|
#
|
|
|
|
# #delete() deletes a session from session storage. It
|
|
|
|
# does not however remove the session id from the client. If the client
|
|
|
|
# makes another request with the same id, the effect will be to start
|
|
|
|
# a new session with the old session's id.
|
|
|
|
#
|
|
|
|
# == Setting and retrieving session data.
|
|
|
|
#
|
|
|
|
# The Session class associates data with a session as key-value pairs.
|
2009-03-05 22:56:38 -05:00
|
|
|
# This data can be set and retrieved by indexing the Session instance
|
2003-08-28 23:52:24 -04:00
|
|
|
# using '[]', much the same as hashes (although other hash methods
|
|
|
|
# are not supported).
|
|
|
|
#
|
|
|
|
# When session processing has been completed for a request, the
|
|
|
|
# session should be closed using the close() method. This will
|
|
|
|
# store the session's state to persistent storage. If you want
|
|
|
|
# to store the session's state to persistent storage without
|
|
|
|
# finishing session processing for this request, call the update()
|
|
|
|
# method.
|
|
|
|
#
|
|
|
|
# == Storing session state
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# The caller can specify what form of storage to use for the session's
|
2003-08-28 23:52:24 -04:00
|
|
|
# data with the +database_manager+ option to CGI::Session::new. The
|
|
|
|
# following storage classes are provided as part of the standard library:
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# CGI::Session::FileStore:: stores data as plain text in a flat file. Only
|
|
|
|
# works with String data. This is the default
|
2003-08-28 23:52:24 -04:00
|
|
|
# storage type.
|
2009-03-05 22:56:38 -05:00
|
|
|
# CGI::Session::MemoryStore:: stores data in an in-memory hash. The data
|
2013-07-18 09:50:32 -04:00
|
|
|
# only persists for as long as the current Ruby
|
2003-08-28 23:52:24 -04:00
|
|
|
# interpreter instance does.
|
|
|
|
# CGI::Session::PStore:: stores data in Marshalled format. Provided by
|
2009-03-05 22:56:38 -05:00
|
|
|
# cgi/session/pstore.rb. Supports data of any type,
|
2003-08-28 23:52:24 -04:00
|
|
|
# and provides file-locking and transaction support.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# Custom storage types can also be created by defining a class with
|
2003-08-28 23:52:24 -04:00
|
|
|
# the following methods:
|
|
|
|
#
|
|
|
|
# new(session, options)
|
|
|
|
# restore # returns hash of session data.
|
|
|
|
# update
|
|
|
|
# close
|
|
|
|
# delete
|
|
|
|
#
|
|
|
|
# Changing storage type mid-session does not work. Note in particular
|
|
|
|
# that by default the FileStore and PStore session data files have the
|
|
|
|
# same name. If your application switches from one to the other without
|
|
|
|
# making sure that filenames will be different
|
|
|
|
# and clients still have old sessions lying around in cookies, then
|
|
|
|
# things will break nastily!
|
|
|
|
#
|
|
|
|
# == Maintaining the session id.
|
|
|
|
#
|
|
|
|
# Most session state is maintained on the server. However, a session
|
|
|
|
# id must be passed backwards and forwards between client and server
|
|
|
|
# to maintain a reference to this session state.
|
|
|
|
#
|
|
|
|
# The simplest way to do this is via cookies. The CGI::Session class
|
|
|
|
# provides transparent support for session id communication via cookies
|
|
|
|
# if the client has cookies enabled.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-28 23:52:24 -04:00
|
|
|
# If the client has cookies disabled, the session id must be included
|
|
|
|
# as a parameter of all requests sent by the client to the server. The
|
|
|
|
# CGI::Session class in conjunction with the CGI class will transparently
|
|
|
|
# add the session id as a hidden input field to all forms generated
|
|
|
|
# using the CGI#form() HTML generation method. No built-in support is
|
|
|
|
# provided for other mechanisms, such as URL re-writing. The caller is
|
2009-03-05 22:56:38 -05:00
|
|
|
# responsible for extracting the session id from the session_id
|
2003-08-28 23:52:24 -04:00
|
|
|
# attribute and manually encoding it in URLs and adding it as a hidden
|
|
|
|
# input to HTML forms created by other mechanisms. Also, session expiry
|
|
|
|
# is not automatically handled.
|
|
|
|
#
|
|
|
|
# == Examples of use
|
|
|
|
#
|
|
|
|
# === Setting the user's name
|
|
|
|
#
|
|
|
|
# require 'cgi'
|
|
|
|
# require 'cgi/session'
|
|
|
|
# require 'cgi/session/pstore' # provides CGI::Session::PStore
|
|
|
|
#
|
|
|
|
# cgi = CGI.new("html4")
|
|
|
|
#
|
|
|
|
# session = CGI::Session.new(cgi,
|
|
|
|
# 'database_manager' => CGI::Session::PStore, # use PStore
|
|
|
|
# 'session_key' => '_rb_sess_id', # custom session key
|
2009-03-05 22:56:38 -05:00
|
|
|
# 'session_expires' => Time.now + 30 * 60, # 30 minute timeout
|
2003-08-28 23:52:24 -04:00
|
|
|
# 'prefix' => 'pstore_sid_') # PStore option
|
|
|
|
# if cgi.has_key?('user_name') and cgi['user_name'] != ''
|
2009-03-05 22:56:38 -05:00
|
|
|
# # coerce to String: cgi[] returns the
|
2003-08-28 23:52:24 -04:00
|
|
|
# # string-like CGI::QueryExtension::Value
|
|
|
|
# session['user_name'] = cgi['user_name'].to_s
|
|
|
|
# elsif !session['user_name']
|
|
|
|
# session['user_name'] = "guest"
|
|
|
|
# end
|
|
|
|
# session.close
|
|
|
|
#
|
|
|
|
# === Creating a new session safely
|
|
|
|
#
|
|
|
|
# require 'cgi'
|
|
|
|
# require 'cgi/session'
|
|
|
|
#
|
|
|
|
# cgi = CGI.new("html4")
|
|
|
|
#
|
|
|
|
# # We make sure to delete an old session if one exists,
|
2009-03-05 22:56:38 -05:00
|
|
|
# # not just to free resources, but to prevent the session
|
2003-08-28 23:52:24 -04:00
|
|
|
# # from being maliciously hijacked later on.
|
|
|
|
# begin
|
2009-03-05 22:56:38 -05:00
|
|
|
# session = CGI::Session.new(cgi, 'new_session' => false)
|
|
|
|
# session.delete
|
2003-08-28 23:52:24 -04:00
|
|
|
# rescue ArgumentError # if no old session
|
|
|
|
# end
|
|
|
|
# session = CGI::Session.new(cgi, 'new_session' => true)
|
|
|
|
# session.close
|
|
|
|
#
|
1999-11-25 04:03:08 -05:00
|
|
|
class Session
|
|
|
|
|
2005-03-06 21:05:08 -05:00
|
|
|
class NoSession < RuntimeError #:nodoc:
|
|
|
|
end
|
2004-12-15 01:35:55 -05:00
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# The id of this session.
|
2004-11-19 11:59:11 -05:00
|
|
|
attr_reader :session_id, :new_session
|
1999-11-25 04:03:08 -05:00
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
def Session::callback(dbman) #:nodoc:
|
2004-11-15 23:55:14 -05:00
|
|
|
Proc.new{
|
2008-11-08 07:36:50 -05:00
|
|
|
dbman[0].close unless dbman.empty?
|
1999-11-25 04:03:08 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Create a new session id.
|
|
|
|
#
|
2015-09-03 08:12:14 -04:00
|
|
|
# The session id is a secure random number by SecureRandom
|
|
|
|
# if possible, otherwise an SHA512 hash based upon the time,
|
|
|
|
# a random number, and a constant string. This routine is
|
|
|
|
# used internally for automatically generated session ids.
|
2004-11-19 11:59:11 -05:00
|
|
|
def create_new_id
|
2007-06-09 21:42:51 -04:00
|
|
|
require 'securerandom'
|
2007-06-08 01:39:13 -04:00
|
|
|
begin
|
2015-09-03 08:12:14 -04:00
|
|
|
# by OpenSSL, or system provided entropy pool
|
2007-06-09 21:42:51 -04:00
|
|
|
session_id = SecureRandom.hex(16)
|
2007-06-08 01:39:13 -04:00
|
|
|
rescue NotImplementedError
|
2015-09-03 08:12:14 -04:00
|
|
|
# never happens on modern systems
|
|
|
|
require 'digest'
|
|
|
|
d = Digest('SHA512').new
|
2007-06-08 01:39:13 -04:00
|
|
|
now = Time::now
|
2015-09-03 08:12:14 -04:00
|
|
|
d.update(now.to_s)
|
|
|
|
d.update(String(now.usec))
|
|
|
|
d.update(String(rand(0)))
|
|
|
|
d.update(String($$))
|
|
|
|
d.update('foobar')
|
|
|
|
session_id = d.hexdigest[0, 32]
|
2007-06-08 01:39:13 -04:00
|
|
|
end
|
|
|
|
session_id
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
2004-11-19 11:59:11 -05:00
|
|
|
private :create_new_id
|
1999-11-25 04:03:08 -05:00
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Create a new CGI::Session object for +request+.
|
|
|
|
#
|
|
|
|
# +request+ is an instance of the +CGI+ class (see cgi.rb).
|
|
|
|
# +option+ is a hash of options for initialising this
|
|
|
|
# CGI::Session instance. The following options are
|
|
|
|
# recognised:
|
|
|
|
#
|
|
|
|
# session_key:: the parameter name used for the session id.
|
|
|
|
# Defaults to '_session_id'.
|
|
|
|
# session_id:: the session id to use. If not provided, then
|
|
|
|
# it is retrieved from the +session_key+ parameter
|
|
|
|
# of the request, or automatically generated for
|
|
|
|
# a new session.
|
2009-03-05 22:56:38 -05:00
|
|
|
# new_session:: if true, force creation of a new session. If not set,
|
2003-08-28 23:52:24 -04:00
|
|
|
# a new session is only created if none currently
|
|
|
|
# exists. If false, a new session is never created,
|
|
|
|
# and if none currently exists and the +session_id+
|
|
|
|
# option is not set, an ArgumentError is raised.
|
|
|
|
# database_manager:: the name of the class providing storage facilities
|
|
|
|
# for session state persistence. Built-in support
|
|
|
|
# is provided for +FileStore+ (the default),
|
|
|
|
# +MemoryStore+, and +PStore+ (from
|
|
|
|
# cgi/session/pstore.rb). See the documentation for
|
|
|
|
# these classes for more details.
|
|
|
|
#
|
|
|
|
# The following options are also recognised, but only apply if the
|
|
|
|
# session id is stored in a cookie.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# session_expires:: the time the current session expires, as a
|
2004-01-02 11:21:26 -05:00
|
|
|
# +Time+ object. If not set, the session will terminate
|
|
|
|
# when the user's browser is closed.
|
2003-08-28 23:52:24 -04:00
|
|
|
# session_domain:: the hostname domain for which this session is valid.
|
|
|
|
# If not set, defaults to the hostname of the server.
|
|
|
|
# session_secure:: if +true+, this session will only work over HTTPS.
|
|
|
|
# session_path:: the path for which this session applies. Defaults
|
|
|
|
# to the directory of the CGI script.
|
|
|
|
#
|
2008-06-04 05:37:38 -04:00
|
|
|
# +option+ is also passed on to the session storage class initializer; see
|
2003-08-28 23:52:24 -04:00
|
|
|
# the documentation for each session storage class for the options
|
|
|
|
# they support.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-28 23:52:24 -04:00
|
|
|
# The retrieved or created session is automatically added to +request+
|
|
|
|
# as a cookie, and also to its +output_hidden+ table, which is used
|
2009-03-05 22:56:38 -05:00
|
|
|
# to add hidden input elements to forms.
|
2003-08-28 23:52:24 -04:00
|
|
|
#
|
|
|
|
# *WARNING* the +output_hidden+
|
|
|
|
# fields are surrounded by a <fieldset> tag in HTML 4 generation, which
|
|
|
|
# is _not_ invisible on many browsers; you may wish to disable the
|
|
|
|
# use of fieldsets with code similar to the following
|
|
|
|
# (see http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/37805)
|
|
|
|
#
|
|
|
|
# cgi = CGI.new("html4")
|
|
|
|
# class << cgi
|
|
|
|
# undef_method :fieldset
|
|
|
|
# end
|
|
|
|
#
|
1999-11-25 04:03:08 -05:00
|
|
|
def initialize(request, option={})
|
2004-11-19 11:59:11 -05:00
|
|
|
@new_session = false
|
1999-11-25 04:03:08 -05:00
|
|
|
session_key = option['session_key'] || '_session_id'
|
2004-12-15 01:35:55 -05:00
|
|
|
session_id = option['session_id']
|
|
|
|
unless session_id
|
2008-11-08 07:36:50 -05:00
|
|
|
if option['new_session']
|
|
|
|
session_id = create_new_id
|
2008-12-18 07:20:13 -05:00
|
|
|
@new_session = true
|
2008-11-08 07:36:50 -05:00
|
|
|
end
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
2004-12-15 01:35:55 -05:00
|
|
|
unless session_id
|
2008-11-08 07:36:50 -05:00
|
|
|
if request.key?(session_key)
|
|
|
|
session_id = request[session_key]
|
|
|
|
session_id = session_id.read if session_id.respond_to?(:read)
|
|
|
|
end
|
|
|
|
unless session_id
|
|
|
|
session_id, = request.cookies[session_key]
|
|
|
|
end
|
|
|
|
unless session_id
|
|
|
|
unless option.fetch('new_session', true)
|
|
|
|
raise ArgumentError, "session_key `%s' should be supplied"%session_key
|
|
|
|
end
|
|
|
|
session_id = create_new_id
|
2008-12-18 07:20:13 -05:00
|
|
|
@new_session = true
|
2008-11-08 07:36:50 -05:00
|
|
|
end
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
2004-12-15 01:35:55 -05:00
|
|
|
@session_id = session_id
|
1999-11-25 04:03:08 -05:00
|
|
|
dbman = option['database_manager'] || FileStore
|
2004-12-15 01:35:55 -05:00
|
|
|
begin
|
|
|
|
@dbman = dbman::new(self, option)
|
|
|
|
rescue NoSession
|
2004-12-16 23:34:39 -05:00
|
|
|
unless option.fetch('new_session', true)
|
2004-12-15 01:35:55 -05:00
|
|
|
raise ArgumentError, "invalid session_id `%s'"%session_id
|
|
|
|
end
|
2008-12-18 07:20:13 -05:00
|
|
|
session_id = @session_id = create_new_id unless session_id
|
|
|
|
@new_session=true
|
2004-12-15 01:35:55 -05:00
|
|
|
retry
|
|
|
|
end
|
1999-11-25 04:03:08 -05:00
|
|
|
request.instance_eval do
|
2008-11-08 07:36:50 -05:00
|
|
|
@output_hidden = {session_key => session_id} unless option['no_hidden']
|
|
|
|
@output_cookies = [
|
2000-10-17 14:14:05 -04:00
|
|
|
Cookie::new("name" => session_key,
|
2008-11-08 07:36:50 -05:00
|
|
|
"value" => session_id,
|
|
|
|
"expires" => option['session_expires'],
|
|
|
|
"domain" => option['session_domain'],
|
|
|
|
"secure" => option['session_secure'],
|
2009-03-05 22:56:38 -05:00
|
|
|
"path" =>
|
2008-11-08 07:36:50 -05:00
|
|
|
if option['session_path']
|
|
|
|
option['session_path']
|
|
|
|
elsif ENV["SCRIPT_NAME"]
|
|
|
|
File::dirname(ENV["SCRIPT_NAME"])
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end)
|
2004-12-16 23:34:39 -05:00
|
|
|
] unless option['no_cookies']
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
2001-08-05 23:05:23 -04:00
|
|
|
@dbprot = [@dbman]
|
|
|
|
ObjectSpace::define_finalizer(self, Session::callback(@dbprot))
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Retrieve the session data for key +key+.
|
1999-11-25 04:03:08 -05:00
|
|
|
def [](key)
|
2006-07-30 10:06:50 -04:00
|
|
|
@data ||= @dbman.restore
|
1999-11-25 04:03:08 -05:00
|
|
|
@data[key]
|
|
|
|
end
|
|
|
|
|
2013-05-05 09:27:26 -04:00
|
|
|
# Set the session data for key +key+.
|
1999-11-25 04:03:08 -05:00
|
|
|
def []=(key, val)
|
2006-07-30 10:06:50 -04:00
|
|
|
@write_lock ||= true
|
|
|
|
@data ||= @dbman.restore
|
2001-11-13 03:14:27 -05:00
|
|
|
@data[key] = val
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Store session data on the server. For some session storage types,
|
|
|
|
# this is a no-op.
|
2009-03-05 22:56:38 -05:00
|
|
|
def update
|
1999-11-25 04:03:08 -05:00
|
|
|
@dbman.update
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# Store session data on the server and close the session storage.
|
2003-08-28 23:52:24 -04:00
|
|
|
# For some session storage types, this is a no-op.
|
2000-10-17 14:14:05 -04:00
|
|
|
def close
|
|
|
|
@dbman.close
|
2001-08-05 23:05:23 -04:00
|
|
|
@dbprot.clear
|
2000-10-17 14:14:05 -04:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Delete the session from storage. Also closes the storage.
|
|
|
|
#
|
|
|
|
# Note that the session's data is _not_ automatically deleted
|
|
|
|
# upon the session expiring.
|
1999-11-25 04:03:08 -05:00
|
|
|
def delete
|
|
|
|
@dbman.delete
|
2001-08-05 23:05:23 -04:00
|
|
|
@dbprot.clear
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# File-based session storage class.
|
|
|
|
#
|
|
|
|
# Implements session storage as a flat file of 'key=value' values.
|
|
|
|
# This storage type only works directly with String values; the
|
|
|
|
# user is responsible for converting other types to Strings when
|
|
|
|
# storing and from Strings when retrieving.
|
1999-11-25 04:03:08 -05:00
|
|
|
class FileStore
|
2003-08-28 23:52:24 -04:00
|
|
|
# Create a new FileStore instance.
|
|
|
|
#
|
|
|
|
# This constructor is used internally by CGI::Session. The
|
|
|
|
# user does not generally need to call it directly.
|
|
|
|
#
|
|
|
|
# +session+ is the session for which this instance is being
|
|
|
|
# created. The session id must only contain alphanumeric
|
|
|
|
# characters; automatically generated session ids observe
|
|
|
|
# this requirement.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2008-06-04 05:37:38 -04:00
|
|
|
# +option+ is a hash of options for the initializer. The
|
2003-08-28 23:52:24 -04:00
|
|
|
# following options are recognised:
|
|
|
|
#
|
|
|
|
# tmpdir:: the directory to use for storing the FileStore
|
|
|
|
# file. Defaults to Dir::tmpdir (generally "/tmp"
|
|
|
|
# on Unix systems).
|
|
|
|
# prefix:: the prefix to add to the session id when generating
|
|
|
|
# the filename for this session's FileStore file.
|
2004-11-15 23:55:14 -05:00
|
|
|
# Defaults to "cgi_sid_".
|
|
|
|
# suffix:: the prefix to add to the session id when generating
|
|
|
|
# the filename for this session's FileStore file.
|
2003-08-28 23:52:24 -04:00
|
|
|
# Defaults to the empty string.
|
|
|
|
#
|
|
|
|
# This session's FileStore file will be created if it does
|
|
|
|
# not exist, or opened if it does.
|
1999-11-25 04:03:08 -05:00
|
|
|
def initialize(session, option={})
|
2008-11-08 07:36:50 -05:00
|
|
|
dir = option['tmpdir'] || Dir::tmpdir
|
|
|
|
prefix = option['prefix'] || 'cgi_sid_'
|
|
|
|
suffix = option['suffix'] || ''
|
|
|
|
id = session.session_id
|
2004-08-24 03:59:50 -04:00
|
|
|
require 'digest/md5'
|
|
|
|
md5 = Digest::MD5.hexdigest(id)[0,16]
|
2008-11-08 07:36:50 -05:00
|
|
|
@path = dir+"/"+prefix+md5+suffix
|
|
|
|
if File::exist? @path
|
|
|
|
@hash = nil
|
|
|
|
else
|
2004-11-19 11:59:11 -05:00
|
|
|
unless session.new_session
|
2004-12-15 01:35:55 -05:00
|
|
|
raise CGI::Session::NoSession, "uninitialized session"
|
2004-11-19 11:59:11 -05:00
|
|
|
end
|
2008-11-08 07:36:50 -05:00
|
|
|
@hash = {}
|
|
|
|
end
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Restore session state from the session's FileStore file.
|
|
|
|
#
|
|
|
|
# Returns the session state as a hash.
|
1999-11-25 04:03:08 -05:00
|
|
|
def restore
|
2008-11-08 07:36:50 -05:00
|
|
|
unless @hash
|
|
|
|
@hash = {}
|
2004-05-20 00:34:14 -04:00
|
|
|
begin
|
2007-11-14 22:20:52 -05:00
|
|
|
lockf = File.open(@path+".lock", "r")
|
|
|
|
lockf.flock File::LOCK_SH
|
2008-11-08 07:36:50 -05:00
|
|
|
f = File.open(@path, 'r')
|
|
|
|
for line in f
|
|
|
|
line.chomp!
|
|
|
|
k, v = line.split('=',2)
|
2008-11-08 09:52:59 -05:00
|
|
|
@hash[CGI::unescape(k)] = Marshal.restore(CGI::unescape(v))
|
2008-11-08 07:36:50 -05:00
|
|
|
end
|
2004-05-20 00:34:14 -04:00
|
|
|
ensure
|
2008-11-08 07:36:50 -05:00
|
|
|
f.close unless f.nil?
|
2007-11-14 22:20:52 -05:00
|
|
|
lockf.close if lockf
|
2004-05-20 00:34:14 -04:00
|
|
|
end
|
2008-11-08 07:36:50 -05:00
|
|
|
end
|
|
|
|
@hash
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Save session state to the session's FileStore file.
|
1999-11-25 04:03:08 -05:00
|
|
|
def update
|
2008-11-08 07:36:50 -05:00
|
|
|
return unless @hash
|
2004-05-20 00:34:14 -04:00
|
|
|
begin
|
2007-11-14 22:20:52 -05:00
|
|
|
lockf = File.open(@path+".lock", File::CREAT|File::RDWR, 0600)
|
2008-11-08 07:36:50 -05:00
|
|
|
lockf.flock File::LOCK_EX
|
2007-11-14 22:20:52 -05:00
|
|
|
f = File.open(@path+".new", File::CREAT|File::TRUNC|File::WRONLY, 0600)
|
2008-11-08 07:36:50 -05:00
|
|
|
for k,v in @hash
|
2008-11-08 09:52:59 -05:00
|
|
|
f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(Marshal.dump(v)))
|
2008-11-08 07:36:50 -05:00
|
|
|
end
|
2007-11-14 22:20:52 -05:00
|
|
|
f.close
|
|
|
|
File.rename @path+".new", @path
|
2004-05-20 00:34:14 -04:00
|
|
|
ensure
|
2016-11-21 18:05:41 -05:00
|
|
|
f.close if f
|
2007-11-14 22:20:52 -05:00
|
|
|
lockf.close if lockf
|
2004-05-20 00:34:14 -04:00
|
|
|
end
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Update and close the session's FileStore file.
|
1999-11-25 04:03:08 -05:00
|
|
|
def close
|
2008-11-08 07:36:50 -05:00
|
|
|
update
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Close and delete the session's FileStore file.
|
1999-11-25 04:03:08 -05:00
|
|
|
def delete
|
2013-04-19 02:17:01 -04:00
|
|
|
File::unlink @path+".lock" rescue nil
|
|
|
|
File::unlink @path+".new" rescue nil
|
|
|
|
File::unlink @path rescue nil
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# In-memory session storage class.
|
|
|
|
#
|
|
|
|
# Implements session storage as a global in-memory hash. Session
|
2013-07-18 09:50:32 -04:00
|
|
|
# data will only persist for as long as the Ruby interpreter
|
2003-08-28 23:52:24 -04:00
|
|
|
# instance does.
|
1999-11-25 04:03:08 -05:00
|
|
|
class MemoryStore
|
2003-08-28 23:52:24 -04:00
|
|
|
GLOBAL_HASH_TABLE = {} #:nodoc:
|
1999-11-25 04:03:08 -05:00
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Create a new MemoryStore instance.
|
|
|
|
#
|
|
|
|
# +session+ is the session this instance is associated with.
|
|
|
|
# +option+ is a list of initialisation options. None are
|
2014-04-28 11:20:28 -04:00
|
|
|
# currently recognized.
|
2001-05-06 11:06:00 -04:00
|
|
|
def initialize(session, option=nil)
|
2008-11-08 07:36:50 -05:00
|
|
|
@session_id = session.session_id
|
2004-11-19 11:59:11 -05:00
|
|
|
unless GLOBAL_HASH_TABLE.key?(@session_id)
|
|
|
|
unless session.new_session
|
2004-12-15 01:35:55 -05:00
|
|
|
raise CGI::Session::NoSession, "uninitialized session"
|
2004-11-19 11:59:11 -05:00
|
|
|
end
|
|
|
|
GLOBAL_HASH_TABLE[@session_id] = {}
|
|
|
|
end
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Restore session state.
|
|
|
|
#
|
|
|
|
# Returns session data as a hash.
|
1999-11-25 04:03:08 -05:00
|
|
|
def restore
|
2008-11-08 07:36:50 -05:00
|
|
|
GLOBAL_HASH_TABLE[@session_id]
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Update session state.
|
|
|
|
#
|
|
|
|
# A no-op.
|
1999-11-25 04:03:08 -05:00
|
|
|
def update
|
2008-11-08 07:36:50 -05:00
|
|
|
# don't need to update; hash is shared
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Close session storage.
|
|
|
|
#
|
|
|
|
# A no-op.
|
1999-11-25 04:03:08 -05:00
|
|
|
def close
|
2008-11-08 07:36:50 -05:00
|
|
|
# don't need to close
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
|
2003-08-28 23:52:24 -04:00
|
|
|
# Delete the session state.
|
1999-11-25 04:03:08 -05:00
|
|
|
def delete
|
2008-11-08 07:36:50 -05:00
|
|
|
GLOBAL_HASH_TABLE.delete(@session_id)
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
end
|
2004-09-17 05:24:13 -04:00
|
|
|
|
|
|
|
# Dummy session storage class.
|
|
|
|
#
|
|
|
|
# Implements session storage place holder. No actual storage
|
|
|
|
# will be done.
|
|
|
|
class NullStore
|
|
|
|
# Create a new NullStore instance.
|
|
|
|
#
|
|
|
|
# +session+ is the session this instance is associated with.
|
|
|
|
# +option+ is a list of initialisation options. None are
|
|
|
|
# currently recognised.
|
|
|
|
def initialize(session, option=nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Restore (empty) session state.
|
|
|
|
def restore
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update session state.
|
|
|
|
#
|
|
|
|
# A no-op.
|
|
|
|
def update
|
|
|
|
end
|
|
|
|
|
|
|
|
# Close session storage.
|
|
|
|
#
|
|
|
|
# A no-op.
|
|
|
|
def close
|
|
|
|
end
|
|
|
|
|
|
|
|
# Delete the session state.
|
|
|
|
#
|
|
|
|
# A no-op.
|
|
|
|
def delete
|
|
|
|
end
|
|
|
|
end
|
1999-11-25 04:03:08 -05:00
|
|
|
end
|
|
|
|
end
|