Skip to content

Instantly share code, notes, and snippets.

@kaznum
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaznum/9319698 to your computer and use it in GitHub Desktop.
Save kaznum/9319698 to your computer and use it in GitHub Desktop.
# This module offers the way to get session_id from HTTP_X_SID
# in the request headers for Rack application.
#
# This is only work memcache and dalli
# (Rack::Session::Cookie implements 'extract_session_id' by itself.)
# Put this file in config/initializers when used with Rails.
#
# I don't know why this module exists because the default way is
# using cookies, but cookie is even put in HTTP Request and response
# headers too.
#
# License : MIT License
# Kazuya NUMATA <numata@gmail.com>
require 'rack/session/abstract/id'
module SessionHeader
module Rack
module Session
module ID
ENV_SESSION_KEY = 'rack.session'.freeze
def self.included(base)
base.class_eval do
include ::SessionHeader::Rack::Session::ID::InstanceMethods
alias_method_chain :extract_session_id, :session_id_header
alias_method_chain :commit_session, :session_id_header
end
end
module InstanceMethods
private
def extract_session_id_with_session_id_header(env)
sid = env["HTTP_X_SID"]
sid ||= extract_session_id_without_session_id_header(env)
sid
end
def commit_session_with_session_id_header(env, status, headers, body)
status, headers, body = commit_session_without_session_id_header(env, status, headers, body)
session = env[ENV_SESSION_KEY]
headers["X-Sid"] = session.id
[status, headers, body]
end
end
end
end
end
end
::Rack::Session::Abstract::ID.class_eval do
include ::SessionHeader::Rack::Session::ID
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment