* lib/cgi/session.rb: fix indentation.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20154 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
xibbar 2008-11-08 12:36:50 +00:00
parent 16285e768e
commit b4890b598b
2 changed files with 70 additions and 64 deletions

View File

@ -1,3 +1,7 @@
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> Sat Nov 8 18:11:14 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/webrick/httpproxy.rb (WEBrick::HTTPProxyServer#do_CONNECT): * lib/webrick/httpproxy.rb (WEBrick::HTTPProxyServer#do_CONNECT):

View File

@ -163,7 +163,7 @@ class CGI
def Session::callback(dbman) #:nodoc: def Session::callback(dbman) #:nodoc:
Proc.new{ Proc.new{
dbman[0].close unless dbman.empty? dbman[0].close unless dbman.empty?
} }
end end
@ -254,24 +254,24 @@ class CGI
session_key = option['session_key'] || '_session_id' session_key = option['session_key'] || '_session_id'
session_id = option['session_id'] session_id = option['session_id']
unless session_id unless session_id
if option['new_session'] if option['new_session']
session_id = create_new_id session_id = create_new_id
end end
end end
unless session_id unless session_id
if request.key?(session_key) if request.key?(session_key)
session_id = request[session_key] session_id = request[session_key]
session_id = session_id.read if session_id.respond_to?(:read) session_id = session_id.read if session_id.respond_to?(:read)
end end
unless session_id unless session_id
session_id, = request.cookies[session_key] session_id, = request.cookies[session_key]
end end
unless session_id unless session_id
unless option.fetch('new_session', true) unless option.fetch('new_session', true)
raise ArgumentError, "session_key `%s' should be supplied"%session_key raise ArgumentError, "session_key `%s' should be supplied"%session_key
end end
session_id = create_new_id session_id = create_new_id
end end
end end
@session_id = session_id @session_id = session_id
dbman = option['database_manager'] || FileStore dbman = option['database_manager'] || FileStore
@ -285,20 +285,21 @@ class CGI
retry retry
end end
request.instance_eval do request.instance_eval do
@output_hidden = {session_key => session_id} unless option['no_hidden'] @output_hidden = {session_key => session_id} unless option['no_hidden']
@output_cookies = [ @output_cookies = [
Cookie::new("name" => session_key, Cookie::new("name" => session_key,
"value" => session_id, "value" => session_id,
"expires" => option['session_expires'], "expires" => option['session_expires'],
"domain" => option['session_domain'], "domain" => option['session_domain'],
"secure" => option['session_secure'], "secure" => option['session_secure'],
"path" => if option['session_path'] then "path" =>
option['session_path'] if option['session_path']
elsif ENV["SCRIPT_NAME"] then option['session_path']
File::dirname(ENV["SCRIPT_NAME"]) elsif ENV["SCRIPT_NAME"]
else File::dirname(ENV["SCRIPT_NAME"])
"" else
end) ""
end)
] unless option['no_cookies'] ] unless option['no_cookies']
end end
@dbprot = [@dbman] @dbprot = [@dbman]
@ -373,56 +374,58 @@ class CGI
# This session's FileStore file will be created if it does # This session's FileStore file will be created if it does
# not exist, or opened if it does. # not exist, or opened if it does.
def initialize(session, option={}) def initialize(session, option={})
dir = option['tmpdir'] || Dir::tmpdir dir = option['tmpdir'] || Dir::tmpdir
prefix = option['prefix'] || 'cgi_sid_' prefix = option['prefix'] || 'cgi_sid_'
suffix = option['suffix'] || '' suffix = option['suffix'] || ''
id = session.session_id id = session.session_id
require 'digest/md5' require 'digest/md5'
md5 = Digest::MD5.hexdigest(id)[0,16] md5 = Digest::MD5.hexdigest(id)[0,16]
@path = dir+"/"+prefix+md5+suffix @path = dir+"/"+prefix+md5+suffix
if File::exist? @path p @path
@hash = nil p id
else if File::exist? @path
@hash = nil
else
unless session.new_session unless session.new_session
raise CGI::Session::NoSession, "uninitialized session" raise CGI::Session::NoSession, "uninitialized session"
end end
@hash = {} @hash = {}
end end
end end
# Restore session state from the session's FileStore file. # Restore session state from the session's FileStore file.
# #
# Returns the session state as a hash. # Returns the session state as a hash.
def restore def restore
unless @hash unless @hash
@hash = {} @hash = {}
begin begin
lockf = File.open(@path+".lock", "r") lockf = File.open(@path+".lock", "r")
lockf.flock File::LOCK_SH lockf.flock File::LOCK_SH
f = File.open(@path, 'r') f = File.open(@path, 'r')
for line in f for line in f
line.chomp! line.chomp!
k, v = line.split('=',2) k, v = line.split('=',2)
@hash[CGI::unescape(k)] = CGI::unescape(v) @hash[CGI::unescape(k)] = CGI::unescape(v)
end end
ensure ensure
f.close unless f.nil? f.close unless f.nil?
lockf.close if lockf lockf.close if lockf
end end
end end
@hash @hash
end end
# Save session state to the session's FileStore file. # Save session state to the session's FileStore file.
def update def update
return unless @hash return unless @hash
begin begin
lockf = File.open(@path+".lock", File::CREAT|File::RDWR, 0600) 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) f = File.open(@path+".new", File::CREAT|File::TRUNC|File::WRONLY, 0600)
for k,v in @hash for k,v in @hash
f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(v)) f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(v))
end end
f.close f.close
File.rename @path+".new", @path File.rename @path+".new", @path
ensure ensure
@ -433,15 +436,14 @@ class CGI
# Update and close the session's FileStore file. # Update and close the session's FileStore file.
def close def close
update update
end end
# Close and delete the session's FileStore file. # Close and delete the session's FileStore file.
def delete def delete
File::unlink @path+".lock" rescue nil File::unlink @path+".lock" rescue nil
File::unlink @path+".new" rescue nil File::unlink @path+".new" rescue nil
File::unlink @path File::unlink @path rescue Errno::ENOENT
rescue Errno::ENOENT
end end
end end
@ -459,7 +461,7 @@ class CGI
# +option+ is a list of initialisation options. None are # +option+ is a list of initialisation options. None are
# currently recognised. # currently recognised.
def initialize(session, option=nil) def initialize(session, option=nil)
@session_id = session.session_id @session_id = session.session_id
unless GLOBAL_HASH_TABLE.key?(@session_id) unless GLOBAL_HASH_TABLE.key?(@session_id)
unless session.new_session unless session.new_session
raise CGI::Session::NoSession, "uninitialized session" raise CGI::Session::NoSession, "uninitialized session"
@ -472,26 +474,26 @@ class CGI
# #
# Returns session data as a hash. # Returns session data as a hash.
def restore def restore
GLOBAL_HASH_TABLE[@session_id] GLOBAL_HASH_TABLE[@session_id]
end end
# Update session state. # Update session state.
# #
# A no-op. # A no-op.
def update def update
# don't need to update; hash is shared # don't need to update; hash is shared
end end
# Close session storage. # Close session storage.
# #
# A no-op. # A no-op.
def close def close
# don't need to close # don't need to close
end end
# Delete the session state. # Delete the session state.
def delete def delete
GLOBAL_HASH_TABLE.delete(@session_id) GLOBAL_HASH_TABLE.delete(@session_id)
end end
end end