Skip to content

Commit b3e2ff9

Browse files
committed
Extract CGI::Session#new_store_file
1 parent 6f86b80 commit b3e2ff9

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

lib/cgi/session.rb

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,47 @@ def create_new_id
189189
end
190190
private :create_new_id
191191

192+
193+
# Create a new file to store the session data.
194+
#
195+
# This file will be created if it does not exist, or opened if it
196+
# does.
197+
#
198+
# This path is generated under _tmpdir_ from _prefix_, the
199+
# digested session id, and _suffix_.
200+
#
201+
# +option+ is a hash of options for the initializer. The
202+
# following options are recognised:
203+
#
204+
# tmpdir:: the directory to use for storing the FileStore
205+
# file. Defaults to Dir::tmpdir (generally "/tmp"
206+
# on Unix systems).
207+
# prefix:: the prefix to add to the session id when generating
208+
# the filename for this session's FileStore file.
209+
# Defaults to "cgi_sid_".
210+
# suffix:: the prefix to add to the session id when generating
211+
# the filename for this session's FileStore file.
212+
# Defaults to the empty string.
213+
def new_store_file(option={}) # :nodoc:
214+
dir = option['tmpdir'] || Dir::tmpdir
215+
prefix = option['prefix']
216+
suffix = option['suffix']
217+
require 'digest/md5'
218+
md5 = Digest::MD5.hexdigest(session_id)[0,16]
219+
path = dir+"/"
220+
path << prefix if prefix
221+
path << md5
222+
path << suffix if suffix
223+
if File::exist? path
224+
hash = nil
225+
elsif new_session
226+
hash = {}
227+
else
228+
raise NoSession, "uninitialized session"
229+
end
230+
return path, hash
231+
end
232+
192233
# Create a new CGI::Session object for +request+.
193234
#
194235
# +request+ is an instance of the +CGI+ class (see cgi.rb).
@@ -373,21 +414,8 @@ class FileStore
373414
# This session's FileStore file will be created if it does
374415
# not exist, or opened if it does.
375416
def initialize(session, option={})
376-
dir = option['tmpdir'] || Dir::tmpdir
377-
prefix = option['prefix'] || 'cgi_sid_'
378-
suffix = option['suffix'] || ''
379-
id = session.session_id
380-
require 'digest/md5'
381-
md5 = Digest::MD5.hexdigest(id)[0,16]
382-
@path = dir+"/"+prefix+md5+suffix
383-
if File::exist? @path
384-
@hash = nil
385-
else
386-
unless session.new_session
387-
raise CGI::Session::NoSession, "uninitialized session"
388-
end
389-
@hash = {}
390-
end
417+
option = {'prefix' => 'cgi_sid_'}.update(option)
418+
@path, @hash = session.new_store_file(option)
391419
end
392420

393421
# Restore session state from the session's FileStore file.

lib/cgi/session/pstore.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,8 @@ class PStore
4444
# This session's PStore file will be created if it does
4545
# not exist, or opened if it does.
4646
def initialize(session, option={})
47-
dir = option['tmpdir'] || Dir::tmpdir
48-
prefix = option['prefix'] || ''
49-
id = session.session_id
50-
require 'digest/md5'
51-
md5 = Digest::MD5.hexdigest(id)[0,16]
52-
path = dir+"/"+prefix+md5
53-
if File::exist?(path)
54-
@hash = nil
55-
else
56-
unless session.new_session
57-
raise CGI::Session::NoSession, "uninitialized session"
58-
end
59-
@hash = {}
60-
end
47+
option = {'suffix'=>''}.update(option)
48+
path, @hash = session.new_store_file(option)
6149
@p = ::PStore.new(path)
6250
@p.transaction do |p|
6351
File.chmod(0600, p.path)

0 commit comments

Comments
 (0)