Author: Braulio Bhavamitra <braulio@prout.io>
noosfero_http_caching: move to controller concerns
app/controllers/application_controller.rb | 3 +++ config/initializers/00_dependencies.rb | 8 ++++++-- | 5 -----
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e76ddb74a4a1cf0e15eb6ca38239915b046744a0..f93c2c5c158bda248b42d7ac1195eef9ef08f8cd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,10 @@ require 'noosfero/multi_tenancy' class ApplicationController < ActionController::Base + protect_from_forgery + + include NoosferoHttpCaching before_filter :detect_stuff_by_domain before_filter :init_noosfero_plugins diff --git a/app/controllers/concerns/noosfero_http_caching.rb b/app/controllers/concerns/noosfero_http_caching.rb new file mode 100644 index 0000000000000000000000000000000000000000..b43e3e631fad97f0235bb9e10c0327bbe3303fff --- /dev/null +++ b/app/controllers/concerns/noosfero_http_caching.rb @@ -0,0 +1,61 @@ +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 && response.status < 400 + expires_in n.minutes, :private => false, :public => true + end + end + + def noosfero_session_check + 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(set_cookie) + return nil if set_cookie.nil? + set_cookie.split(/\s*,\s*/).select do |c| + c =~ /^_noosfero_plugin_\w+=/ && c =~ /path=\/\w+/ + end.join(', ') + end + + end + +end + diff --git a/config/initializers/00_dependencies.rb b/config/initializers/00_dependencies.rb index 72a4e5223f7a610785fe37f148eaccf89898e0b4..767b91bf3be71ee94521785d42c28d248a38ab36 100644 --- a/config/initializers/00_dependencies.rb +++ b/config/initializers/00_dependencies.rb @@ -2,6 +2,10 @@ ## # Dependencies that need to load after application init # If don't depend on rails should be loaded on config/application.rb instead # -require 'noosfero_http_caching' -require 'noosfero/i18n' +unless Rails.env.development? + middleware = Noosfero::Application.config.middleware + middleware.insert_before ::ActionDispatch::Cookies, NoosferoHttpCaching::Middleware +end + +require 'noosfero/i18n' diff --git a/lib/noosfero_http_caching.rb b/lib/noosfero_http_caching.rb deleted file mode 100644 index bd3e6c97ccc10b8c1bffe964e502dd640999f963..0000000000000000000000000000000000000000 --- a/lib/noosfero_http_caching.rb +++ /dev/null @@ -1,66 +0,0 @@ -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 && response.status < 400 - expires_in n.minutes, :private => false, :public => true - end - end - - def noosfero_session_check - 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(set_cookie) - return nil if set_cookie.nil? - set_cookie.split(/\s*,\s*/).select do |c| - c =~ /^_noosfero_plugin_\w+=/ && c =~ /path=\/\w+/ - end.join(', ') - end - - end - -end - -unless Rails.env.development? - middleware = Noosfero::Application.config.middleware - ActionController::Base.send(:include, NoosferoHttpCaching) - middleware.insert_before ::ActionDispatch::Cookies, NoosferoHttpCaching::Middleware -end