[前][次][番号順一覧][スレッド一覧]

ruby-changes:70217

From: Nobuyoshi <ko1@a...>
Date: Wed, 15 Dec 2021 08:24:50 +0900 (JST)
Subject: [ruby-changes:70217] 6eb500e2df (master): [ruby/cgi] Extract CGI::Session#new_store_file

https://git.ruby-lang.org/ruby.git/commit/?id=6eb500e2df

From 6eb500e2df17475a557de536ce24a4d878bf1607 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sat, 27 Nov 2021 18:55:42 +0900
Subject: [ruby/cgi] Extract CGI::Session#new_store_file

https://github.com/ruby/cgi/commit/b3e2ff9164
---
 lib/cgi/session.rb        | 58 +++++++++++++++++++++++++++++++++++------------
 lib/cgi/session/pstore.rb | 16 ++-----------
 2 files changed, 45 insertions(+), 29 deletions(-)

diff --git a/lib/cgi/session.rb b/lib/cgi/session.rb
index 29e7b3ece3b..70c7ebca426 100644
--- a/lib/cgi/session.rb
+++ b/lib/cgi/session.rb
@@ -189,6 +189,47 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/session.rb#L189
     end
     private :create_new_id
 
+
+    # Create a new file to store the session data.
+    #
+    # This file will be created if it does not exist, or opened if it
+    # does.
+    #
+    # This path is generated under _tmpdir_ from _prefix_, the
+    # digested session id, and _suffix_.
+    #
+    # +option+ is a hash of options for the initializer.  The
+    # following options are recognised:
+    #
+    # tmpdir:: the directory to use for storing the FileStore
+    #          file.  Defaults to Dir::tmpdir (generally "/tmp"
+    #          on Unix systems).
+    # prefix:: the prefix to add to the session id when generating
+    #          the filename for this session's FileStore file.
+    #          Defaults to "cgi_sid_".
+    # suffix:: the prefix to add to the session id when generating
+    #          the filename for this session's FileStore file.
+    #          Defaults to the empty string.
+    def new_store_file(option={}) # :nodoc:
+      dir = option['tmpdir'] || Dir::tmpdir
+      prefix = option['prefix']
+      suffix = option['suffix']
+      require 'digest/md5'
+      md5 = Digest::MD5.hexdigest(session_id)[0,16]
+      path = dir+"/"
+      path << prefix if prefix
+      path << md5
+      path << suffix if suffix
+      if File::exist? path
+        hash = nil
+      elsif new_session
+        hash = {}
+      else
+        raise NoSession, "uninitialized session"
+      end
+      return path, hash
+    end
+
     # Create a new CGI::Session object for +request+.
     #
     # +request+ is an instance of the +CGI+ class (see cgi.rb).
@@ -373,21 +414,8 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/session.rb#L414
       # 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
-        require 'digest/md5'
-        md5 = Digest::MD5.hexdigest(id)[0,16]
-        @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
+        option = {'prefix' => 'cgi_sid_'}.update(option)
+        @path, @hash = session.new_store_file(option)
       end
 
       # Restore session state from the session's FileStore file.
diff --git a/lib/cgi/session/pstore.rb b/lib/cgi/session/pstore.rb
index cc3006400fa..45d0d8ae2cb 100644
--- a/lib/cgi/session/pstore.rb
+++ b/lib/cgi/session/pstore.rb
@@ -44,20 +44,8 @@ class CGI https://github.com/ruby/ruby/blob/trunk/lib/cgi/session/pstore.rb#L44
       # 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
-        require 'digest/md5'
-        md5 = Digest::MD5.hexdigest(id)[0,16]
-        path = dir+"/"+prefix+md5
-        if File::exist?(path)
-          @hash = nil
-        else
-          unless session.new_session
-            raise CGI::Session::NoSession, "uninitialized session"
-          end
-          @hash = {}
-        end
+        option = {'suffix'=>''}.update(option)
+        path, @hash = session.new_store_file(option)
         @p = ::PStore.new(path)
         @p.transaction do |p|
           File.chmod(0600, p.path)
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]