[Ruby-session-devel-cvs] CVS: ruby-session/src/apache session.rb,1.3,1.4
Status: Alpha
Brought to you by:
thetitan
|
From: Sean C. <the...@us...> - 2002-06-20 13:46:54
|
Update of /cvsroot/ruby-session/ruby-session/src/apache
In directory usw-pr-cvs1:/tmp/cvs-serv14258
Modified Files:
session.rb
Log Message:
*) Updated apache/session.rb to become actually usable as a sessioning
module. Currently it only strips session data from the URI but
will quickly snab data out of cookies and rewrite pages.
Index: session.rb
===================================================================
RCS file: /cvsroot/ruby-session/ruby-session/src/apache/session.rb,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- session.rb 19 Nov 2001 00:16:15 -0000 1.3
+++ session.rb 20 Jun 2002 13:46:50 -0000 1.4
@@ -1,179 +1,52 @@
#!/usr/local/bin/ruby -w
-# ruby-session: Got persistence?
-
-# Copyright 2001
-# Sean Chittenden <se...@ch...>. All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-# 1. Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the above copyright
-# notice, this list of conditions and the following disclaimer in the
-# documentation and/or other materials provided with the distribution.
-# 3. All advertising materials mentioning features or use of this software
-# must display the following acknowledgment:
-#
-# This product includes software developed by Sean Chittenden
-# <se...@ch...> and ruby-session's contributors.
-#
-# 4. Neither the name of the software nor the names of its contributors
-# may be used to endorse or promote products derived from this software
-# without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-# SUCH DAMAGE.
-#
-# Please see the LICENSE file for more information regarding the use
-# and distribution of this software.
+# See the LICENSE file for copyright and distribution information.
#
# $Id$
+require 'session'
+
module Apache
class Session
- attr_accessor :cookies, :data, :expired, :session_dir, :session_id
- attr_reader :session_id_length, :start_time, :type
-
- #
- # Class methods
- #
-
- # RubyAccessHandler Apache::Session
- def Session.check_access (r)
- if ENV.has_key?('SESSION')
- if ENV['SESSION'] == 'require'
- # Test to see if the request has a session
-
- # if the request doesn't have a session, then generate a
- # session using Session.generate(), append it to the URL,
- # and send a 302 response
+ @@html_session_re_a = nil
+ @@html_session_re_form = nil
+ @@html_session_re_img = nil
+ @@uri_session_ri = nil
+
+ def Session.cleanup(r)
+ s = nil
+ end # def Session.cleanup()
+
+
+ def Session.init(r)
+ s = Session.new()
+
+ # One time init junk
+ @@html_session_re_a ||= Regexp.new('(<\s*[Aa].*?[Hh][Rr][Ee][Ff]\s*=\s*)(([\'\"])((?:[^#]|[Mm][Aa][Ii][Ll][Tt][Oo]).*?[^\?]*?){0,1}(\?.*?){0,1}\3)(.*?>)')
+ @@html_session_re_form ||= Regexp.new('(<\s*[Ff][Oo][Rr][Mm].*?[Aa][Cc][Tt][Ii][Oo][Nn]\s*=\s*)(([\'"])([^\?]*?){0,1}(\?.*?){0,1}\3)(.*?>)')
+ @@html_session_re_img ||= Regexp.new('(<\s*[Ii][Mm][Gg].*?[Ss][Rr][Cc]\s*=\s*)(([\'"])([^\?]*?){0,1}(\?.*?){0,1}\3)(.*?>)')
+ @@uri_session_re ||= Regexp.new('/sess=([A-Fa-f0-9]{32})/')
+ end # def Session.init()
+
+
+ def Session.translate_uri(r)
+ md = @@uri_session_re.match(r.uri)
+ if !md.nil?
+ $session.session_id = md[1]
+ r.uri = uri.sub(md[0], '/')
- end
+ $session.generate() unless $session.load()
+ r.notes['session_id'] = $session.session_id
else
+ # No Session or invalid session found in the URI
+ #
+ # XXX Need to incorporate checking for cookies here
+ end # if md.nil?
+
+ # Let another handler map this request to a file
+ return(Apache::DECLINED)
+ end # def Session.translate_uri()
- # if ruby-session can't handle this request, then pass the
- # request to the next access handler
-
- return(Apache::DECLINED)
- end # if ENV.has_key?()
- end # def Session.check_access
-
-
- def Session.exist? (location)
- return(Session._exist?(location))
- end # def Session.exist?
-
-
- def Session.generate ()
- require 'digest/md5'
- md5digest = Digest::MD5.new
- md5digest.update(Time.now.to_f.to_s + rand.to_s + $$.to_s)
- return(md5digest.hexdigest)
- end # def Session.generate
-
-
- def Session.make_location (*args)
- begin
- return(Session._make_location(args))
- rescue NameError
- raise RuntimeError, "No Session persistence layers available"
- end
- end # def Session.make_location
-
-
- public # methods
-
-
- def [] (key)
- return(@data[key])
- end # def []
-
-
- def []= (key, value)
- return(@data[key] = value)
- end # def []=
-
-
- def delete ()
- self._delete
- end # def delete
-
-
- def exist? (session_id)
- return(self._exist?)
- end # def exist?
-
-
- def location ()
- @location = self.make_location unless (@location)
- return(@location)
- end # def location
-
-
- def persist ()
- @data['__ruby_session_expired__'] = (@expired ? 'y' : 'n')
- @data['__ruby_session_start_time__'] = @start_time
-
- self._persist
-
- @data.delete('__ruby_session_expired__')
- @data.delete('__ruby_session_start_time__')
- end # def persist
-
-
- def restore ()
- self._restore
-
- @start_time = @data.delete('__ruby_session_start_time__')
- @expired = (@data.delete('__ruby_session_expired__') == 'y' ? true : false)
- end # def restore
-
-
- def type= (type)
- @type = type.downcase
- begin
- require File.join('apache', 'session', @type)
- rescue LoadError
- raise LoadError, "Invalid Session Type '#{type}'"
- end
-
- return(@type)
- end # def type=
-
-
- private
-
-
- def initialize (opts = {})
- @cookies = (opts.has_key?('cookies') ? opts['cookies'] : false)
- @data = (opts.has_key?('session_data') ? opts['session_data'] : {})
- @expired = false
- @location = nil
- @session_dir = (opts.has_key?('session_dir') ? opts['session_dir'] : '/tmp')
- @session_id = nil
- @start_time = Time.now
- @type = self.type = (opts.has_key?('type') ? opts['type'] : 'file')
-
- if opts.has_key?('session_id')
- @session_id = opts['session_id']
- self.restore
- else
- @session_id = Session.generate
- end # if opts.has_key?('session_id')
-
- end # def initialize ()
end # class Session
-
end # module Apache
|