Last active
August 29, 2015 13:56
-
-
Save kaznum/9319698 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 <[email protected]> | |
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