Class: CGI::Session::FileStore
- Inherits:
-
Object
- Object
- CGI::Session::FileStore
- Defined in:
- lib/cgi/session.rb
Overview
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.
Instance Method Summary collapse
-
#close ⇒ Object
Update and close the session’s FileStore file.
-
#delete ⇒ Object
Close and delete the session’s FileStore file.
-
#initialize(session, option = {}) ⇒ FileStore
constructor
Create a new FileStore instance.
-
#restore ⇒ Object
Restore session state from the session’s FileStore file.
-
#update ⇒ Object
Save session state to the session’s FileStore file.
Constructor Details
#initialize(session, option = {}) ⇒ FileStore
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.
option is a hash of options for the initialiser. The 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. Defaults to the empty string.
- suffix
-
the prefix to add to the session id when generating the filename for this session’s FileStore file. Defaults to the empty string.
This session’s FileStore file will be created if it does not exist, or opened if it does.
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/cgi/session.rb', line 369 def initialize(session, option={}) dir = option['tmpdir'] || Dir::tmpdir prefix = option['prefix'] || '' suffix = option['suffix'] || '' id = session.session_id require 'digest/md5' md5 = Digest::MD5.hexdigest(id)[0,16] @path = dir+"/"+prefix+md5+suffix if File::exist? @path @hash = nil else unless session.new_session raise CGI::Session::NoSession, "uninitialized session" end @hash = {} end end |
Instance Method Details
#close ⇒ Object
Update and close the session’s FileStore file.
423 424 425 |
# File 'lib/cgi/session.rb', line 423 def close update end |
#delete ⇒ Object
Close and delete the session’s FileStore file.
428 429 430 431 |
# File 'lib/cgi/session.rb', line 428 def delete File::unlink @path rescue Errno::ENOENT end |
#restore ⇒ Object
Restore session state from the session’s FileStore file.
Returns the session state as a hash.
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/cgi/session.rb', line 390 def restore unless @hash @hash = {} begin f = File.open(@path, 'r') f.flock File::LOCK_SH for line in f line.chomp! k, v = line.split('=',2) @hash[CGI::unescape(k)] = CGI::unescape(v) end ensure f.close unless f.nil? end end @hash end |
#update ⇒ Object
Save session state to the session’s FileStore file.
409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/cgi/session.rb', line 409 def update return unless @hash begin f = File.open(@path, File::CREAT|File::TRUNC|File::RDWR, 0600) f.flock File::LOCK_EX for k,v in @hash f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(v)) end ensure f.close unless f.nil? end end |