summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-11-25 09:03:08 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-11-25 09:03:08 +0000
commitebab487fcd7633038b9272ddbe31c268cda15723 (patch)
treee7880ae217a2a58dbe35bcf0f94744bc03805e08 /lib
parent8e48dc16e97a783a69f0972b4882ad2faae561ea (diff)
19991125
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@570 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/cgi/session.rb157
-rw-r--r--lib/pstore.rb42
-rw-r--r--lib/singleton.rb7
3 files changed, 180 insertions, 26 deletions
diff --git a/lib/cgi/session.rb b/lib/cgi/session.rb
new file mode 100644
index 0000000000..c584fdb49b
--- /dev/null
+++ b/lib/cgi/session.rb
@@ -0,0 +1,157 @@
+
+require 'CGI'
+require 'final'
+
+class CGI
+ class Session
+
+ attr_reader :session_id
+
+ def Session::callback(dbman)
+ lambda{
+ dbman.close
+ }
+ end
+
+ def create_new_id
+ require 'md5'
+ md5 = MD5::new
+ md5.update(String(Time::now))
+ md5.update(String(rand(0)))
+ md5.update(String($$))
+ md5.update('foobar')
+ @session_id = md5.hexdigest[0,16]
+ end
+ private :create_new_id
+
+ def initialize(request, option={})
+ session_key = option['session_key'] || '_session_id'
+ id, = option['session_id']
+ unless id
+ if option['new_session']
+ id = create_new_id
+ end
+ end
+ unless id
+ id, = request[session_key]
+ unless id
+ id, = request.cookies[session_key]
+ end
+ unless id
+ if option.key?('new_session') and not option['new_session']
+ raise ArgumentError, "session_key `%s' should be supplied"%session_key
+ end
+ id = create_new_id
+ end
+ end
+ @session_id = id
+ dbman = option['database_manager'] || FileStore
+ @dbman = dbman::new(self, option)
+ request.instance_eval do
+ @output_hidden = {session_key => id}
+ @output_cookies = [Cookie::new(session_key,id)]
+ end
+ ObjectSpace::define_finalizer(self, Session::callback(@dbman))
+ end
+
+ def [](key)
+ unless @data
+ @data = @dbman.restore
+ end
+ @data[key]
+ end
+
+ def []=(key, val)
+ unless @write_lock
+ @write_lock = true
+ end
+ unless @data
+ @data = @dbman.restore
+ end
+ @data[key] = String(val)
+ end
+
+ def update
+ @dbman.update
+ end
+
+ def delete
+ @dbman.delete
+ end
+
+ class FileStore
+ def initialize(session, option={})
+ dir = option['tmpdir'] || ENV['TMP'] || '/tmp'
+ prefix = option['prefix'] || ''
+ path = dir+"/"+prefix+session.session_id
+ path.untaint
+ unless File::exist? path
+ @hash = {}
+ end
+ begin
+ @f = open(path, "r+")
+ rescue Errno::ENOENT
+ @f = open(path, "w+")
+ end
+ end
+
+ def restore
+ unless @hash
+ @hash = {}
+ @f.flock File::LOCK_EX
+ @f.rewind
+ for line in @f
+ line.chomp!
+ k, v = line.split('=',2)
+ @hash[CGI::unescape(k)] = CGI::unescape(v)
+ end
+ end
+ @hash
+ end
+
+ def update
+ @f.rewind
+ for k,v in @hash
+ @f.printf "%s=%s\n", CGI::escape(k), CGI::escape(v)
+ end
+ @f.truncate @f.tell
+ end
+
+ def close
+ update
+ @f.close
+ end
+
+ def delete
+ path = @f.path
+ @f.close
+ File::unlink path
+ end
+ end
+
+ class MemoryStore
+ GLOBAL_HASH_TABLE = {}
+
+ def initialize(session, option={})
+ @session_id = session.session_id
+ GLOBAL_HASH_TABLE[@session_id] = {}
+ end
+
+ def restore
+ GLOBAL_HASH_TABLE[@session_id]
+ end
+
+ def update
+ # don't need to update; hash is shared
+ end
+
+ def close
+ # don't need to close
+ end
+
+ def delete
+ GLOBAL_HASH_TABLE[@session_id] = nil
+ end
+ end
+ end
+end
diff --git a/lib/pstore.rb b/lib/pstore.rb
index cc90207aa3..566de8d8f9 100644
--- a/lib/pstore.rb
+++ b/lib/pstore.rb
@@ -13,6 +13,7 @@
# end
require "marshal"
+require "ftools"
class PStore
class Error < StandardError
@@ -77,22 +78,19 @@ class PStore
raise PStore::Error, "nested transaction" if @transaction
begin
@transaction = true
- value = file = nil
- lock = @filename + ".lock"
- loop do
- begin
- File::symlink("pstore::#$$", lock)
- break
- rescue Errno::EEXIST
- rescue
- sleep 1
- end
+ value = nil
+ backup = @filename+"~"
+ if File::exist?(@filename)
+ file = File::open(@filename, "r+")
+ orig = true
+ else
+ file = File::open(@filename, "w+")
end
- begin
- File::open(@filename, "r") do |file|
- @table = Marshal.load(file)
- end
- rescue Errno::ENOENT
+ file.flock(File::LOCK_EX)
+ if orig
+ File::copy @filename, backup
+ @table = Marshal::load(file)
+ else
@table = {}
end
begin
@@ -105,16 +103,10 @@ class PStore
ensure
unless @abort
begin
- File::rename @filename, @filename+"~"
- rescue Errno::ENOENT
- no_orig = true
- end
- begin
- File::open(@filename, "w") do |file|
- Marshal::dump(@table, file)
- end
+ file.rewind
+ Marshal::dump(@table, file)
rescue
- File::rename @filename+"~", @filename unless no_orig
+ File::rename backup, @filename if File::exist?(backup)
end
end
@abort = false
@@ -122,7 +114,7 @@ class PStore
ensure
@table = nil
@transaction = false
- File::unlink(lock)
+ file.close
end
value
end
diff --git a/lib/singleton.rb b/lib/singleton.rb
index 4aea574e7a..2785c77134 100644
--- a/lib/singleton.rb
+++ b/lib/singleton.rb
@@ -17,7 +17,12 @@ module Singleton
@__instance__ = nil
def instance
unless @__instance__
- @__instance__ = new
+ Thread.critical = true
+ begin
+ @__instance__ = new
+ ensure
+ Thread.critical = false
+ end
end
return @__instance__
end