[Ruby-session-devel-cvs] CVS: ruby-session/src/apache cookie.rb,NONE,1.1
Status: Alpha
Brought to you by:
thetitan
|
From: Sean C. <the...@us...> - 2001-11-19 08:42:27
|
Update of /cvsroot/ruby-session/ruby-session/src/apache
In directory usw-pr-cvs1:/tmp/cvs-serv29304
Added Files:
cookie.rb
Log Message:
* Adding Apache::Cookie. Module isn't fully functional, but it does
go a long way towards providing a consistent mod_ruby interface for
cookies.
* Can create cookies, add cookies to CookieDough and bake the
CookieDough to get a valid Cookie.
* Need to add ability to set more meta-data about cookie values
* Need to add ability to read cookies from incoming HTTP headers
--- NEW FILE: cookie.rb ---
# 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.
#
# $Id: cookie.rb,v 1.1 2001/11/19 08:42:24 thetitan Exp $
module Apache
class Cookie
attr_accessor :secure
attr_reader
public
def Cookie.uri_escape (str_arg)
str = str_arg.dup
str.gsub!(/#{Regexp.escape('%')}/o, '%25')
str.gsub!(/#{Regexp.escape('$')}/o, '%24')
str.gsub!(/#{Regexp.escape('&')}/o, '%26')
str.gsub!(/#{Regexp.escape('"')}/o, '%22')
str.gsub!(/#{Regexp.escape('\'')}/o, '%27')
str.gsub!(/#{Regexp.escape(',')}/o, '%2C')
str.gsub!(/#{Regexp.escape('=')}/o, '%3D')
str.gsub!(/#{Regexp.escape(';')}/o, '%3B')
str.gsub!(/#{Regexp.escape(':')}/o, '%3A')
str.gsub!(/#{Regexp.escape(" ")}/o, '%20')
str.gsub!(/#{Regexp.escape("\n")}/o, '%0A')
str.gsub!(/#{Regexp.escape("\t")}/o, '%09')
str.gsub!(/#{Regexp.escape("\r")}/o, '%0D')
str.gsub(/([^a-zA-Z0-9._-]+)/o) do
'%' + $1.unpack('H2' * $1.size).join('%').upcase
end.tr(' ', '+')
return(str)
end # def uri_escape
def name
return(Apache.unescape_url(@name))
end # def name
# Sets the cookie name. Cookie name's can't start with a dollar sign ($)
# as specified by RFC 2109. All $'s are being escaped by uri_escape().
def name= (name_arg)
name = name_arg.dup
@name = Cookie.uri_escape(name)
end # def name=
def to_s
return(self.to_str)
end # def to_s
def to_str
return(@name.to_s + '=' + @value.to_s)
end # def to_str
def value
return(Apache.unescape_url(@value))
end # def value
# Sets the value of the cookie. This must be ASCII printable, but
# I'm not checking for that now. Instead I'm just going to borrow
# some code from cgi.rb and will URI encode the value. XXX
def value= (value_arg)
value = value_arg.dup
@value = Cookie.uri_escape(value)
end # def value=
private
def initialize
@name = '' # Cookie name
@value = '' # Cookie value (must be ASCII printable)
@secure = false
end # def initialize
end # class Cookie
class CookieDough
public
def add (*args)
args.each do |arg|
@jar[arg.name] = arg
end # args.each
end # def add
def bake
cookies = []
@jar.keys.sort.each do |cookie_name|
cookies.push(@jar.fetch(cookie_name).to_str)
end # @jar.keys.sort.each
return('Set-Cookie: ' + cookies.join(','))
end # def bake
private
def initialize ( *args )
@jar = {}
if args.length == 0
@r = Apache.request
else
if args[0].type.to_s == "Apache::Request"
@r = args[0]
else
raise TypeError, "Need a request handler to operate on"
end # if args[0]
end # def initialize
end # def initialize
end # class CookieDough
end # module Apache
|