Skip to content

Commit

Permalink
Remove WhatDoTheyKnow namespace
Browse files Browse the repository at this point in the history
This class is used in all installs of Alaveteli
  • Loading branch information
garethrees committed Dec 11, 2015
1 parent 0c3c0e5 commit f65b26f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
4 changes: 2 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class Application < Rails::Application
ENV['RECAPTCHA_PRIVATE_KEY'] = ::AlaveteliConfiguration::recaptcha_private_key

# Insert a bit of middleware code to prevent uneeded cookie setting.
require "#{Rails.root}/lib/whatdotheyknow/strip_empty_sessions"
config.middleware.insert_before ::ActionDispatch::Cookies, WhatDoTheyKnow::StripEmptySessions, :key => '_wdtk_cookie_session', :path => "/", :httponly => true
require "#{Rails.root}/lib/strip_empty_sessions"
config.middleware.insert_before ::ActionDispatch::Cookies, StripEmptySessions, :key => '_wdtk_cookie_session', :path => "/", :httponly => true

# Strip non-UTF-8 request parameters
config.middleware.insert 0, Rack::UTF8Sanitizer
Expand Down
27 changes: 27 additions & 0 deletions lib/strip_empty_sessions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- encoding : utf-8 -*-
class StripEmptySessions
ENV_SESSION_KEY = "rack.session".freeze
HTTP_SET_COOKIE = "Set-Cookie".freeze
STRIPPABLE_KEYS = ['session_id', '_csrf_token', 'locale']

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
30 changes: 0 additions & 30 deletions lib/whatdotheyknow/strip_empty_sessions.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# -*- encoding : utf-8 -*-
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe WhatDoTheyKnow::StripEmptySessions do
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe StripEmptySessions do

def make_response(session_data, response_headers)
app = lambda do |env|
env['rack.session'] = session_data
return [200, response_headers, ['content']]
end
strip_empty_sessions = WhatDoTheyKnow::StripEmptySessions
strip_empty_sessions = StripEmptySessions
app = strip_empty_sessions.new(app, {:key => 'mykey', :path => '', :httponly => true})
response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/html')
end
Expand Down

0 comments on commit f65b26f

Please sign in to comment.