aboutsummaryrefslogtreecommitdiffstats
path: root/lib/whatdotheyknow/strip_empty_sessions.rb
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@dracos.co.uk>2011-07-22 12:49:00 +0100
committerRobin Houston <robin.houston@gmail.com>2011-07-26 16:29:53 +0100
commit30bba84b72bf8c40c149dea8f8895b182ede16ae (patch)
tree0f137b5d721a40ecd02d1a8be37c1fde1ee00444 /lib/whatdotheyknow/strip_empty_sessions.rb
parent15a0c6e8561c2540b51e5466f8f317d386d16ee3 (diff)
Add FixMyTransport's strip_empty_sessions to not send a cookie if there's nothing in the session.
Diffstat (limited to 'lib/whatdotheyknow/strip_empty_sessions.rb')
-rw-r--r--lib/whatdotheyknow/strip_empty_sessions.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/whatdotheyknow/strip_empty_sessions.rb b/lib/whatdotheyknow/strip_empty_sessions.rb
new file mode 100644
index 000000000..9c87a4bbc
--- /dev/null
+++ b/lib/whatdotheyknow/strip_empty_sessions.rb
@@ -0,0 +1,29 @@
+module WhatDoTheyKnow
+
+ class StripEmptySessions
+ ENV_SESSION_KEY = "rack.session".freeze
+ HTTP_SET_COOKIE = "Set-Cookie".freeze
+ STRIPPABLE_KEYS = [:session_id, :_csrf_token]
+
+ def initialize(app, options = {})
+ @app = app
+ @options = options
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ session_data = env[ENV_SESSION_KEY]
+ set_cookie = headers[HTTP_SET_COOKIE]
+ if session_data
+ if (session_data.keys - STRIPPABLE_KEYS).empty?
+ if set_cookie.is_a? Array
+ set_cookie.reject! {|c| c.match(/^\n?#{@options[:key]}=/)}
+ elsif set_cookie.is_a? String
+ headers[HTTP_SET_COOKIE].gsub!( /(^|\n)#{@options[:key]}=.*?(\n|$)/, "" )
+ end
+ end
+ end
+ [status, headers, body]
+ end
+ end
+end