Permalink
Browse files

Cookie based session management middleware.

darcs-hash:20070309234006-111e0-a40cda5895130b1ee3d5a8b2ea92aac2703787ca.gz
  • Loading branch information...
chneukirchen committed Mar 9, 2007
1 parent 4687521 commit 417ac6a3d6b394dc2a2d30d9e1235148170dec50
Showing with 74 additions and 0 deletions.
  1. +74 −0 lib/rack/session/cookie.rb
@@ -0,0 +1,74 @@
require 'base64'

module Rack

module Session

# Rack::Session::Cookie provides simple cookie based session management.
# The session is a Ruby Hash stored as base64 encoded marshalled data
# set to :key (default: rack.session).
#
# Example:
#
# use Rack::Session::Cookie, :key => 'rack.session',
# :domain => 'foo.com',
# :path => '/',
# :expire_after => 2592000
#
# All parameters are optional.

class Cookie

def initialize(app, options)
@app = app
@key = options[:key] || "rack.session"
@default_options = {:domain => nil,
:path => "/",
:expire_after => nil}.merge(options)
end

def call(env)
load_session(env)
status, headers, body = @app.call(env)
commit_session(env, status, headers, body)
end

private

def load_session(env)
request = Rack::Request.new(env)
session_data = request.cookies[@key]

if session_data.nil?
env["rack.session"] = Hash.new
else
session_data = Base64.decode64(session_data)
session_data = Marshal.load(session_data)
env["rack.session"] = session_data
end
env["rack.session.options"] = @default_options.dup
end

def commit_session(env, status, headers, body)
session_data = Marshal.dump(env["rack.session_hash"])
session_data = Base64.encode64(session_data)

if session_data.size > (4096 - @key.size)
env["rack.errors"].puts("Warning! Rack::Session::Cookie data size exceeds 4K. Content dropped.")
[status, headers, body]
else
options = env["rack.session.options"]
cookie = Hash.new
cookie[:value] = session_data
cookie[:expires] = Time.now + options[:expire_after] unless options[:expire_after].nil?
response = Rack::Response.new(body, status, headers)
response.set_cookie(@key, cookie.merge(options))
response.to_a
end
end

end

end

end

0 comments on commit 417ac6a

Please sign in to comment.