init.rb
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module NoosferoHttpCaching
def self.included(c)
c.send(:after_filter, :noosfero_set_cache)
c.send(:after_filter, :noosfero_session_check)
end
def noosfero_set_cache
return if logged_in?
n = nil
if profile
unless request.path =~ /^\/myprofile/
n = environment.profile_cache_in_minutes
end
else
if request.path == '/'
n = environment.home_cache_in_minutes
else
if params[:controller] != 'account' && !request.xhr? && request.path !~ /^\/admin/
n = environment.general_cache_in_minutes
end
end
end
if n
expires_in n.minutes, :private => false, :public => true
end
end
def noosfero_session_check
return if (params[:controller] == 'account' && params[:action] != 'user_data')
headers["X-Noosfero-Auth"] = (session[:user] != nil).to_s
end
class Middleware
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
if headers['X-Noosfero-Auth'] == 'false'
headers['Set-Cookie'] = remove_unwanted_cookies(headers['Set-Cookie'])
headers.delete('Set-Cookie') if headers['Set-Cookie'].blank?
end
headers.delete('X-Noosfero-Auth')
[status, headers, body]
end
protected
# filter off all cookies except for plugin-provided ones that are
# path-specific (i.e path != "/").
def remove_unwanted_cookies(cookie_list)
return nil if cookie_list.nil?
cookie_list.select do |c|
c =~ /^_noosfero_plugin_\w+=/ && c =~ /path=\/\w+/
end
end
end
end
unless Rails.env.development?
middleware = ActionController::Dispatcher.middleware
ActionController::Base.send(:include, NoosferoHttpCaching)
middleware.use NoosferoHttpCaching::Middleware
end