2017-02-18 05:52:16 +00:00
|
|
|
# frozen_string_literal: true
|
2003-08-29 13:27:20 +00:00
|
|
|
#
|
|
|
|
# cgi/session/pstore.rb - persistent storage of marshalled session data
|
|
|
|
#
|
|
|
|
# Documentation: William Webber (william@williamwebber.com)
|
2009-03-06 03:56:38 +00:00
|
|
|
#
|
2003-08-29 13:27:20 +00:00
|
|
|
# == Overview
|
|
|
|
#
|
|
|
|
# This file provides the CGI::Session::PStore class, which builds
|
|
|
|
# persistent of session data on top of the pstore library. See
|
|
|
|
# cgi/session.rb for more details on session storage managers.
|
|
|
|
|
2018-11-02 17:52:33 +00:00
|
|
|
require_relative '../session'
|
2003-07-15 07:35:14 +00:00
|
|
|
require 'pstore'
|
|
|
|
|
|
|
|
class CGI
|
|
|
|
class Session
|
2003-08-29 13:27:20 +00:00
|
|
|
# PStore-based session storage class.
|
|
|
|
#
|
|
|
|
# This builds upon the top-level PStore class provided by the
|
|
|
|
# library file pstore.rb. Session data is marshalled and stored
|
|
|
|
# in a file. File locking and transaction services are provided.
|
2003-07-15 07:35:14 +00:00
|
|
|
class PStore
|
2003-08-29 13:27:20 +00:00
|
|
|
# Create a new CGI::Session::PStore 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-06 03:56:38 +00:00
|
|
|
#
|
2008-06-04 09:37:38 +00:00
|
|
|
# +option+ is a hash of options for the initializer. The
|
2003-08-29 13:27:20 +00:00
|
|
|
# following options are recognised:
|
|
|
|
#
|
|
|
|
# tmpdir:: the directory to use for storing the PStore
|
|
|
|
# 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 PStore file.
|
|
|
|
# Defaults to the empty string.
|
|
|
|
#
|
|
|
|
# This session's PStore file will be created if it does
|
|
|
|
# not exist, or opened if it does.
|
2006-07-30 14:06:50 +00:00
|
|
|
def initialize(session, option={})
|
2021-11-27 18:55:42 +09:00
|
|
|
option = {'suffix'=>''}.update(option)
|
|
|
|
path, @hash = session.new_store_file(option)
|
2008-11-08 15:03:42 +00:00
|
|
|
@p = ::PStore.new(path)
|
|
|
|
@p.transaction do |p|
|
|
|
|
File.chmod(0600, p.path)
|
|
|
|
end
|
2003-07-15 07:35:14 +00:00
|
|
|
end
|
|
|
|
|
2003-08-29 13:27:20 +00:00
|
|
|
# Restore session state from the session's PStore file.
|
|
|
|
#
|
|
|
|
# Returns the session state as a hash.
|
2003-07-15 07:35:14 +00:00
|
|
|
def restore
|
2008-11-08 15:03:42 +00:00
|
|
|
unless @hash
|
|
|
|
@p.transaction do
|
2004-10-19 10:25:23 +00:00
|
|
|
@hash = @p['hash'] || {}
|
2008-11-08 15:03:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@hash
|
2003-07-15 07:35:14 +00:00
|
|
|
end
|
|
|
|
|
2003-08-29 13:27:20 +00:00
|
|
|
# Save session state to the session's PStore file.
|
2009-03-06 03:56:38 +00:00
|
|
|
def update
|
2008-11-08 15:03:42 +00:00
|
|
|
@p.transaction do
|
|
|
|
@p['hash'] = @hash
|
|
|
|
end
|
2003-07-15 07:35:14 +00:00
|
|
|
end
|
|
|
|
|
2003-08-29 13:27:20 +00:00
|
|
|
# Update and close the session's PStore file.
|
2003-07-15 07:35:14 +00:00
|
|
|
def close
|
2008-11-08 15:03:42 +00:00
|
|
|
update
|
2003-07-15 07:35:14 +00:00
|
|
|
end
|
|
|
|
|
2003-08-29 13:27:20 +00:00
|
|
|
# Close and delete the session's PStore file.
|
2003-07-15 07:35:14 +00:00
|
|
|
def delete
|
2008-11-08 15:03:42 +00:00
|
|
|
path = @p.path
|
|
|
|
File::unlink path
|
2003-07-15 07:35:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-08-10 01:34:50 +00:00
|
|
|
# :enddoc:
|