init.rb 1.77 KB
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