mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Factor out unique id generator. Expose cgi to session store.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6174 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
9e4c8b8858
commit
772e02dd62
1 changed files with 25 additions and 13 deletions
|
@ -3,28 +3,40 @@
|
||||||
# when serving requests from a long-lived process.
|
# when serving requests from a long-lived process.
|
||||||
#
|
#
|
||||||
# http://railsexpress.de/blog/articles/2005/11/22/speeding-up-the-creation-of-new-sessions
|
# http://railsexpress.de/blog/articles/2005/11/22/speeding-up-the-creation-of-new-sessions
|
||||||
|
#
|
||||||
|
# Also expose the CGI instance to session stores.
|
||||||
require 'cgi/session'
|
require 'cgi/session'
|
||||||
require 'digest/md5'
|
require 'digest/md5'
|
||||||
|
|
||||||
class CGI
|
class CGI
|
||||||
class Session #:nodoc:
|
class Session #:nodoc:
|
||||||
|
# Generate an MD5 hash including the time, a random number, the process id,
|
||||||
|
# and a constant string. This is used to generate session ids but may be
|
||||||
|
# reused elsewhere.
|
||||||
|
def self.generate_unique_id(constant = 'foobar')
|
||||||
|
md5 = Digest::MD5.new
|
||||||
|
now = Time.now
|
||||||
|
md5 << now.to_s
|
||||||
|
md5 << String(now.usec)
|
||||||
|
md5 << String(rand(0))
|
||||||
|
md5 << String($$)
|
||||||
|
md5 << constant
|
||||||
|
md5.hexdigest
|
||||||
|
end
|
||||||
|
|
||||||
|
# Make the CGI instance available to session stores.
|
||||||
|
attr_reader :cgi
|
||||||
|
alias_method :initialize_without_cgi_reader, :initialize
|
||||||
|
def initialize(cgi, options = {})
|
||||||
|
@cgi = cgi
|
||||||
|
initialize_without_cgi_reader(cgi, options)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
# Create a new session id.
|
# Create a new session id.
|
||||||
#
|
|
||||||
# The session id is an MD5 hash based upon the time,
|
|
||||||
# a random number, and a constant string. This routine
|
|
||||||
# is used internally for automatically generated
|
|
||||||
# session ids.
|
|
||||||
def create_new_id
|
def create_new_id
|
||||||
md5 = Digest::MD5::new
|
|
||||||
now = Time::now
|
|
||||||
md5.update(now.to_s)
|
|
||||||
md5.update(String(now.usec))
|
|
||||||
md5.update(String(rand(0)))
|
|
||||||
md5.update(String($$))
|
|
||||||
md5.update('foobar')
|
|
||||||
@new_session = true
|
@new_session = true
|
||||||
md5.hexdigest
|
self.class.generate_unique_id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue