Class CGI::Session::FileStore
In: lib/cgi/session.rb
Parent: Object

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.

Methods

close   delete   new   restore   update  

Public Class methods

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.

[Source]

     # File lib/cgi/session.rb, line 375
375:       def initialize(session, option={})
376:         dir = option['tmpdir'] || Dir::tmpdir
377:        prefix = option['prefix'] || ''
378:         suffix = option['suffix'] || ''
379:         id = session.session_id
380:         require 'digest/md5'
381:         md5 = Digest::MD5.hexdigest(id)[0,16]
382:         @path = dir+"/"+prefix+md5+suffix
383:         unless File::exist? @path
384:           unless session.new_session
385:             raise CGI::Session::NoSession, "uninitialized session"
386:           end
387:           @hash = {}
388:         end
389:       end

Public Instance methods

Update and close the session’s FileStore file.

[Source]

     # File lib/cgi/session.rb, line 427
427:       def close
428:         update
429:       end

Close and delete the session’s FileStore file.

[Source]

     # File lib/cgi/session.rb, line 432
432:       def delete
433:         File::unlink @path
434:       rescue Errno::ENOENT
435:       end

Restore session state from the session’s FileStore file.

Returns the session state as a hash.

[Source]

     # File lib/cgi/session.rb, line 394
394:       def restore
395:         unless @hash
396:           @hash = {}
397:           begin
398:             f = File.open(@path, 'r')
399:             f.flock File::LOCK_SH
400:             for line in f
401:               line.chomp!
402:               k, v = line.split('=',2)
403:               @hash[CGI::unescape(k)] = CGI::unescape(v)
404:             end
405:           ensure
406:             f.close unless f.nil?
407:           end
408:         end
409:         @hash
410:       end

Save session state to the session’s FileStore file.

[Source]

     # File lib/cgi/session.rb, line 413
413:       def update
414:         return unless @hash
415:         begin
416:           f = File.open(@path, File::CREAT|File::TRUNC|File::RDWR, 0600)
417:           f.flock File::LOCK_EX
418:           for k,v in @hash
419:             f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(v))
420:           end
421:         ensure
422:           f.close unless f.nil?
423:         end
424:       end

[Validate]