diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cgi/session.rb | 43 | ||||
-rw-r--r-- | lib/cgi/session/pstore.rb | 2 | ||||
-rw-r--r-- | lib/set.rb | 14 |
3 files changed, 35 insertions, 24 deletions
diff --git a/lib/cgi/session.rb b/lib/cgi/session.rb index 1bddaf3260..8384ed9beb 100644 --- a/lib/cgi/session.rb +++ b/lib/cgi/session.rb @@ -155,6 +155,9 @@ class CGI # class Session + #:nodoc: + class NoSession < RuntimeError; end + # The id of this session. attr_reader :session_id, :new_session @@ -243,35 +246,43 @@ class CGI def initialize(request, option={}) @new_session = false session_key = option['session_key'] || '_session_id' - id = option['session_id'] - unless id + session_id = option['session_id'] + unless session_id if option['new_session'] - id = create_new_id + session_id = create_new_id end end - unless id + unless session_id if request.key?(session_key) - id = request[session_key] - id = id.read if id.respond_to?(:read) + session_id = request[session_key] + session_id = session_id.read if session_id.respond_to?(:read) end - unless id - id, = request.cookies[session_key] + unless session_id + session_id, = request.cookies[session_key] end - unless id + unless session_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 + session_id = create_new_id end end - @session_id = id + @session_id = session_id dbman = option['database_manager'] || FileStore - @dbman = dbman::new(self, option) + begin + @dbman = dbman::new(self, option) + rescue NoSession + if option.key?('new_session') and not option['new_session'] + raise ArgumentError, "invalid session_id `%s'"%session_id + end + session_id = @session_id = create_new_id + retry + end request.instance_eval do - @output_hidden = {session_key => id} + @output_hidden = {session_key => session_id} @output_cookies = [ Cookie::new("name" => session_key, - "value" => id, + "value" => session_id, "expires" => option['session_expires'], "domain" => option['session_domain'], "secure" => option['session_secure'], @@ -371,7 +382,7 @@ class CGI @path = dir+"/"+prefix+md5+suffix unless File::exist? @path unless session.new_session - raise RuntimeError, "uninitialized session" + raise CGI::Session::NoSession, "uninitialized session" end @hash = {} end @@ -441,7 +452,7 @@ class CGI @session_id = session.session_id unless GLOBAL_HASH_TABLE.key?(@session_id) unless session.new_session - raise RuntimeError, "uninitialized session" + raise CGI::Session::NoSession, "uninitialized session" end GLOBAL_HASH_TABLE[@session_id] = {} end diff --git a/lib/cgi/session/pstore.rb b/lib/cgi/session/pstore.rb index 40d2214c20..a4ef0cbdcb 100644 --- a/lib/cgi/session/pstore.rb +++ b/lib/cgi/session/pstore.rb @@ -63,7 +63,7 @@ class CGI path.untaint unless File::exist?(path) unless session.new_session - raise RuntimeError, "uninitialized session" + raise CGI::Session::NoSession, "uninitialized session" end @hash = {} end diff --git a/lib/set.rb b/lib/set.rb index 23d7b847ef..8364d4a072 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -193,7 +193,7 @@ class Set # Adds the given object to the set and returns self. Use +merge+ to # add several elements at once. def add(o) - @hash[o] = true + @hash[o] = o self end alias << add @@ -313,7 +313,7 @@ class Set set.is_a?(Set) && size == set.size or return false - set.all? { |o| include?(o) } + set.all? { |o| @hash.value?(o) } end def hash # :nodoc: @@ -466,7 +466,7 @@ class SortedSet < Set def add(o) @keys = nil - @hash[o] = true + @hash[o] = o self end alias << add @@ -551,7 +551,7 @@ end # if @proc.arity == 2 # instance_eval %{ # def add(o) -# @hash[o] = true if @proc.call(self, o) +# @hash[o] = o if @proc.call(self, o) # self # end # alias << add @@ -560,7 +560,7 @@ end # if include?(o) || !@proc.call(self, o) # nil # else -# @hash[o] = true +# @hash[o] = o # self # end # end @@ -583,7 +583,7 @@ end # else # instance_eval %{ # def add(o) -# @hash[o] = true if @proc.call(o) +# @hash[o] = o if @proc.call(o) # self # end # alias << add @@ -592,7 +592,7 @@ end # if include?(o) || !@proc.call(o) # nil # else -# @hash[o] = true +# @hash[o] = o # self # end # end |