lib/rack/iframe.rb in rack-iframe-0.0.2 vs lib/rack/iframe.rb in rack-iframe-0.0.3

- old
+ new

@@ -5,24 +5,27 @@ module Rack class Iframe DEFAULT_P3P = %(CP="ALL DSP COR CURa ADMa DEVa OUR IND COM NAV").freeze DEFAULT_IFRAME_SESSION_PATH = '/iframe_session'.freeze + DEFAULT_ENV_SESSION_KEY = 'rack.session'.freeze def initialize(app, options = {}) @app, @options = app, options @options[:p3p] ||= DEFAULT_P3P @options[:iframe_session_path] ||= DEFAULT_IFRAME_SESSION_PATH + @options[:env_session_key] ||= DEFAULT_ENV_SESSION_KEY end def call(env) # 1) If P3P: Set a random Etag (If-None-Match) to trick backend to not send cached response (304). set_invalid_etag!(env) if set_p3p_header?(env) # 2) Request if iframe_session_path?(env) - @status, @headers, @body = iframe_session_response + @app.call(env) # ...still call app as we want same ENV. + @status, @headers, @body = iframe_session_response(env) else @status, @headers, @body = @app.call(env) end # 3) If P3P: Attach P3P header. @@ -33,11 +36,11 @@ end protected def user_agent(env) - env['HTTP_USER_AGENT'] + env['HTTP_USER_AGENT'] || [] end def set_invalid_etag!(env) env['HTTP_IF_NONE_MATCH'] = Digest::MD5.hexdigest(Time.now.to_s) end @@ -75,10 +78,17 @@ def iframe_session_path?(env) env['PATH_INFO'] == @options[:iframe_session_path] end - def iframe_session_response + def iframe_session_response(env) + begin + # Write a value into the session to ensure we get a session (cookie). + session_key = @options[:env_session_key] + env[session_key][:iframe_session] = true + rescue => e + env['rack.errors'].puts "[rack-iframe]: env[#{@options[:env_session_key]}] = #{env[@options[:env_session_key]]}" + end [200, {}, [""]] end end end