summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-11 11:01:41 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-11 11:01:41 +0000
commit90c250920b187c4ccef84bddd1562ae7fe62b92b (patch)
treebd26f498dd95a10d877d07060077976ebd1af89f
parentb3952151a12709b15411d86232dd98991d8cb232 (diff)
merges r20154-20158, r20161 from trunk into ruby_1_9_1.
* lib/cgi/session/pstore.rb: fix indentation. * lib/cgi/session.rb (FileStore): use marshalized data. * test/cgi/session_dir: add a session directory in test. * test/cgi/test_cgi_session.rb: add a test. * test/cgi/test_cgi_multipart.rb: 1.9 support. * test/cgi/test_cgi_session.rb: ditto. * test/cgi/test_cgi_tag_helper.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@20200 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog20
-rw-r--r--lib/cgi/session.rb128
-rw-r--r--lib/cgi/session/pstore.rb50
-rwxr-xr-xtest/cgi/test_cgi_multipart.rb6
-rwxr-xr-xtest/cgi/test_cgi_session.rb98
-rwxr-xr-xtest/cgi/test_cgi_tag_helper.rb2
6 files changed, 213 insertions, 91 deletions
diff --git a/ChangeLog b/ChangeLog
index 2cde827727..9952a05e33 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+Sun Nov 9 00:02:01 2008 Takeyuki FUJIOKA <xibbar@ruby-lang.org>
+
+ * lib/cgi/session/pstore.rb: fix indentation.
+
+Sat Nov 8 23:47:45 2008 Takeyuki FUJIOKA <xibbar@ruby-lang.org>
+
+ * lib/cgi/session.rb (FileStore): use marshalized data.
+
+ * test/cgi/session_dir: add a session directory in test.
+
+ * test/cgi/test_cgi_session.rb: add a test.
+
+Sat Nov 8 21:57:03 2008 Takeyuki FUJIOKA <xibbar@ruby-lang.org>
+
+ * lib/cgi/session.rb: remove debug code.
+
+Sat Nov 8 21:33:53 2008 Takeyuki FUJIOKA <xibbar@ruby-lang.org>
+
+ * lib/cgi/session.rb: fix indentation.
+
Sat Nov 8 18:11:14 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/webrick/httpproxy.rb (WEBrick::HTTPProxyServer#do_CONNECT):
diff --git a/lib/cgi/session.rb b/lib/cgi/session.rb
index a9cfedd56b..063d910856 100644
--- a/lib/cgi/session.rb
+++ b/lib/cgi/session.rb
@@ -163,7 +163,7 @@ class CGI
def Session::callback(dbman) #:nodoc:
Proc.new{
- dbman[0].close unless dbman.empty?
+ dbman[0].close unless dbman.empty?
}
end
@@ -254,24 +254,24 @@ class CGI
session_key = option['session_key'] || '_session_id'
session_id = option['session_id']
unless session_id
- if option['new_session']
- session_id = create_new_id
- end
+ if option['new_session']
+ session_id = create_new_id
+ end
end
unless session_id
- if request.key?(session_key)
- session_id = request[session_key]
- session_id = session_id.read if session_id.respond_to?(:read)
- end
- unless session_id
- session_id, = request.cookies[session_key]
- end
- unless session_id
- unless option.fetch('new_session', true)
- raise ArgumentError, "session_key `%s' should be supplied"%session_key
- end
- session_id = create_new_id
- end
+ if request.key?(session_key)
+ session_id = request[session_key]
+ session_id = session_id.read if session_id.respond_to?(:read)
+ end
+ unless session_id
+ session_id, = request.cookies[session_key]
+ end
+ unless session_id
+ unless option.fetch('new_session', true)
+ raise ArgumentError, "session_key `%s' should be supplied"%session_key
+ end
+ session_id = create_new_id
+ end
end
@session_id = session_id
dbman = option['database_manager'] || FileStore
@@ -285,20 +285,21 @@ class CGI
retry
end
request.instance_eval do
- @output_hidden = {session_key => session_id} unless option['no_hidden']
- @output_cookies = [
+ @output_hidden = {session_key => session_id} unless option['no_hidden']
+ @output_cookies = [
Cookie::new("name" => session_key,
- "value" => session_id,
- "expires" => option['session_expires'],
- "domain" => option['session_domain'],
- "secure" => option['session_secure'],
- "path" => if option['session_path'] then
- option['session_path']
- elsif ENV["SCRIPT_NAME"] then
- File::dirname(ENV["SCRIPT_NAME"])
- else
- ""
- end)
+ "value" => session_id,
+ "expires" => option['session_expires'],
+ "domain" => option['session_domain'],
+ "secure" => option['session_secure'],
+ "path" =>
+ if option['session_path']
+ option['session_path']
+ elsif ENV["SCRIPT_NAME"]
+ File::dirname(ENV["SCRIPT_NAME"])
+ else
+ ""
+ end)
] unless option['no_cookies']
end
@dbprot = [@dbman]
@@ -373,56 +374,56 @@ class CGI
# This session's FileStore file will be created if it does
# not exist, or opened if it does.
def initialize(session, option={})
- dir = option['tmpdir'] || Dir::tmpdir
- prefix = option['prefix'] || 'cgi_sid_'
- suffix = option['suffix'] || ''
- id = session.session_id
+ dir = option['tmpdir'] || Dir::tmpdir
+ prefix = option['prefix'] || 'cgi_sid_'
+ suffix = option['suffix'] || ''
+ id = session.session_id
require 'digest/md5'
md5 = Digest::MD5.hexdigest(id)[0,16]
- @path = dir+"/"+prefix+md5+suffix
- if File::exist? @path
- @hash = nil
- else
+ @path = dir+"/"+prefix+md5+suffix
+ if File::exist? @path
+ @hash = nil
+ else
unless session.new_session
raise CGI::Session::NoSession, "uninitialized session"
end
- @hash = {}
- end
+ @hash = {}
+ end
end
# Restore session state from the session's FileStore file.
#
# Returns the session state as a hash.
def restore
- unless @hash
- @hash = {}
+ unless @hash
+ @hash = {}
begin
lockf = File.open(@path+".lock", "r")
lockf.flock File::LOCK_SH
- f = File.open(@path, 'r')
- for line in f
- line.chomp!
- k, v = line.split('=',2)
- @hash[CGI::unescape(k)] = CGI::unescape(v)
- end
+ f = File.open(@path, 'r')
+ for line in f
+ line.chomp!
+ k, v = line.split('=',2)
+ @hash[CGI::unescape(k)] = Marshal.restore(CGI::unescape(v))
+ end
ensure
- f.close unless f.nil?
+ f.close unless f.nil?
lockf.close if lockf
end
- end
- @hash
+ end
+ @hash
end
# Save session state to the session's FileStore file.
def update
- return unless @hash
+ return unless @hash
begin
lockf = File.open(@path+".lock", File::CREAT|File::RDWR, 0600)
- lockf.flock File::LOCK_EX
+ lockf.flock File::LOCK_EX
f = File.open(@path+".new", File::CREAT|File::TRUNC|File::WRONLY, 0600)
- for k,v in @hash
- f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(v))
- end
+ for k,v in @hash
+ f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(Marshal.dump(v)))
+ end
f.close
File.rename @path+".new", @path
ensure
@@ -433,15 +434,14 @@ class CGI
# Update and close the session's FileStore file.
def close
- update
+ update
end
# Close and delete the session's FileStore file.
def delete
File::unlink @path+".lock" rescue nil
File::unlink @path+".new" rescue nil
- File::unlink @path
- rescue Errno::ENOENT
+ File::unlink @path rescue Errno::ENOENT
end
end
@@ -459,7 +459,7 @@ class CGI
# +option+ is a list of initialisation options. None are
# currently recognised.
def initialize(session, option=nil)
- @session_id = session.session_id
+ @session_id = session.session_id
unless GLOBAL_HASH_TABLE.key?(@session_id)
unless session.new_session
raise CGI::Session::NoSession, "uninitialized session"
@@ -472,26 +472,26 @@ class CGI
#
# Returns session data as a hash.
def restore
- GLOBAL_HASH_TABLE[@session_id]
+ GLOBAL_HASH_TABLE[@session_id]
end
# Update session state.
#
# A no-op.
def update
- # don't need to update; hash is shared
+ # don't need to update; hash is shared
end
# Close session storage.
#
# A no-op.
def close
- # don't need to close
+ # don't need to close
end
# Delete the session state.
def delete
- GLOBAL_HASH_TABLE.delete(@session_id)
+ GLOBAL_HASH_TABLE.delete(@session_id)
end
end
diff --git a/lib/cgi/session/pstore.rb b/lib/cgi/session/pstore.rb
index 6039183363..3cd3e46000 100644
--- a/lib/cgi/session/pstore.rb
+++ b/lib/cgi/session/pstore.rb
@@ -43,55 +43,55 @@ class CGI
# This session's PStore file will be created if it does
# not exist, or opened if it does.
def initialize(session, option={})
- dir = option['tmpdir'] || Dir::tmpdir
- prefix = option['prefix'] || ''
- id = session.session_id
+ dir = option['tmpdir'] || Dir::tmpdir
+ prefix = option['prefix'] || ''
+ id = session.session_id
require 'digest/md5'
md5 = Digest::MD5.hexdigest(id)[0,16]
- path = dir+"/"+prefix+md5
- path.untaint
- if File::exist?(path)
- @hash = nil
- else
+ path = dir+"/"+prefix+md5
+ path.untaint
+ if File::exist?(path)
+ @hash = nil
+ else
unless session.new_session
raise CGI::Session::NoSession, "uninitialized session"
end
- @hash = {}
- end
- @p = ::PStore.new(path)
- @p.transaction do |p|
- File.chmod(0600, p.path)
- end
+ @hash = {}
+ end
+ @p = ::PStore.new(path)
+ @p.transaction do |p|
+ File.chmod(0600, p.path)
+ end
end
# Restore session state from the session's PStore file.
#
# Returns the session state as a hash.
def restore
- unless @hash
- @p.transaction do
+ unless @hash
+ @p.transaction do
@hash = @p['hash'] || {}
- end
- end
- @hash
+ end
+ end
+ @hash
end
# Save session state to the session's PStore file.
def update
- @p.transaction do
- @p['hash'] = @hash
- end
+ @p.transaction do
+ @p['hash'] = @hash
+ end
end
# Update and close the session's PStore file.
def close
- update
+ update
end
# Close and delete the session's PStore file.
def delete
- path = @p.path
- File::unlink path
+ path = @p.path
+ File::unlink path
end
end
diff --git a/test/cgi/test_cgi_multipart.rb b/test/cgi/test_cgi_multipart.rb
index 34f077e9c5..47a41f6488 100755
--- a/test/cgi/test_cgi_multipart.rb
+++ b/test/cgi/test_cgi_multipart.rb
@@ -135,7 +135,11 @@ class CGIMultipartTest < Test::Unit::TestCase
ENV['CONTENT_LENGTH'] = input.length.to_s
ENV['REQUEST_METHOD'] = 'POST'
## set $stdin
- tmpfile = Tempfile.new(self.name, :binmode => true)
+ tmpfile = if RUBY_VERSION >="1.9"
+ Tempfile.new(self.name, :binmode => true)
+ else
+ Tempfile.new(self.name)
+ end
tmpfile << input
tmpfile.rewind()
$stdin = tmpfile
diff --git a/test/cgi/test_cgi_session.rb b/test/cgi/test_cgi_session.rb
new file mode 100755
index 0000000000..c041b9cf59
--- /dev/null
+++ b/test/cgi/test_cgi_session.rb
@@ -0,0 +1,98 @@
+require 'test/unit'
+require 'cgi'
+require 'cgi/session'
+require 'cgi/session/pstore'
+require 'stringio'
+
+class CGISessionTest < Test::Unit::TestCase
+
+
+ def setup
+ FileUtils.rm(Dir::glob(File.dirname(__FILE__)+"/session_dir/*"))
+ end
+
+
+ def teardown
+ @environ.each do |key, val| ENV.delete(key) end
+ $stdout = STDOUT
+# FileUtils.rm(Dir::glob(File.dirname(__FILE__)+"/session_dir/*"))
+ end
+
+ def test_cgi_session_filestore
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
+ # 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ value1="value1"
+ value2="\x8F\xBC\x8D]"
+ value2.force_encoding("SJIS") if RUBY_VERSION>="1.9"
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>File.dirname(__FILE__)+"/session_dir")
+ session["key1"]=value1
+ session["key2"]=value2
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session.close
+ $stdout = StringIO.new
+ cgi.out{""}
+
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'HTTP_COOKIE' => "_session_id=#{session_id}",
+ 'QUERY_STRING' => "_session_id=#{session.session_id}",
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>File.dirname(__FILE__)+"/session_dir")
+ $stdout = StringIO.new
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session.close
+
+ end
+ def test_cgi_session_pstore
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
+ # 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ value1="value1"
+ value2="\x8F\xBC\x8D]"
+ value2.force_encoding("SJIS") if RUBY_VERSION>="1.9"
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>File.dirname(__FILE__)+"/session_dir","database_manager"=>CGI::Session::PStore)
+ session["key1"]=value1
+ session["key2"]=value2
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session.close
+ $stdout = StringIO.new
+ cgi.out{""}
+
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'HTTP_COOKIE' => "_session_id=#{session_id}",
+ 'QUERY_STRING' => "_session_id=#{session.session_id}",
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>File.dirname(__FILE__)+"/session_dir","database_manager"=>CGI::Session::PStore)
+ $stdout = StringIO.new
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session.close
+
+
+ end
+end
diff --git a/test/cgi/test_cgi_tag_helper.rb b/test/cgi/test_cgi_tag_helper.rb
index f28146a026..6e726b93a3 100755
--- a/test/cgi/test_cgi_tag_helper.rb
+++ b/test/cgi/test_cgi_tag_helper.rb
@@ -318,7 +318,7 @@ class CGITagHelperTest < Test::Unit::TestCase
assert_match(/^<INPUT .*TYPE="checkbox".*>bb<INPUT .*TYPE="checkbox".*>dd$/,str)
assert_match(/^<INPUT .*NAME="foo".*>bb<INPUT .*NAME="foo".*>dd$/,str)
assert_match(/^<INPUT .*>bb<INPUT .*CHECKED.*>dd$/,str)
- assert_match(/<INPUT .*TYPE="text".*>/,cgi.text_field(:name=>"name",:value=>"value"))
+ assert_match(/<INPUT .*TYPE="text".*>/,cgi.text_field(:name=>"name",:value=>"value")) if RUBY_VERSION>="1.9"
if RUBY_VERSION>="1.9"
str=cgi.radio_group("foo",["aa","bb"],["cc","dd",false])
assert_match(/^<INPUT .*VALUE="aa".*>bb<INPUT .*VALUE="cc".*>dd$/,str)