From 35952055e2bd7249320922afb3d2f1a32e68f429 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 29 Apr 2014 10:48:09 +1000 Subject: [PATCH] BUGFIX: web crawlers messing with anon caching --- lib/middleware/anonymous_cache.rb | 23 +++++++++++++++---- .../middleware/anonymous_cache_spec.rb | 16 +++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rb index 8d103b149e9..13c62adee3f 100644 --- a/lib/middleware/anonymous_cache.rb +++ b/lib/middleware/anonymous_cache.rb @@ -1,4 +1,5 @@ require_dependency "mobile_detection" +require_dependency "crawler_detection" module Middleware class AnonymousCache @@ -8,6 +9,9 @@ module Middleware end class Helper + USER_AGENT = "HTTP_USER_AGENT".freeze + RACK_SESSION = "rack.session".freeze + def initialize(env) @env = env end @@ -19,18 +23,29 @@ module Middleware def is_mobile? @is_mobile ||= begin - session = @env["rack.session"] + session = @env[RACK_SESSION] # don't initialize params until later otherwise # you get a broken params on the request params = {} - user_agent = @env["HTTP_USER_AGENT"] + user_agent = @env[USER_AGENT] MobileDetection.resolve_mobile_view!(user_agent,params,session) ? :true : :false - end == :true + end + + @is_mobile == :true + end + + def is_crawler? + @is_crawler ||= + begin + user_agent = @env[USER_AGENT] + CrawlerDetection.crawler?(user_agent) ? :true : :false + end + @is_crawler == :true end def cache_key - @cache_key ||= "ANON_CACHE_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}|m=#{is_mobile?}" + @cache_key ||= "ANON_CACHE_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}|m=#{is_mobile?}|c=#{is_crawler?}" end def cache_key_body diff --git a/spec/components/middleware/anonymous_cache_spec.rb b/spec/components/middleware/anonymous_cache_spec.rb index 5eb3b59547c..0c93dc4191f 100644 --- a/spec/components/middleware/anonymous_cache_spec.rb +++ b/spec/components/middleware/anonymous_cache_spec.rb @@ -30,15 +30,27 @@ describe Middleware::AnonymousCache::Helper do new_helper("ANON_CACHE_DURATION" => 10) end + let!(:crawler) do + new_helper("ANON_CACHE_DURATION" => 10, "HTTP_USER_AGENT" => "AdsBot-Google (+http://www.google.com/adsbot.html)") + end + after do helper.clear_cache + crawler.clear_cache end it "returns cached data for cached requests" do helper.is_mobile = true helper.cached.should be_nil - helper.cache([200, {"HELLO" => "WORLD"}, ["hello ", "world"]]) - helper.cached.should == [200, {"HELLO" => "WORLD"}, ["hello world"]] + helper.cache([200, {"HELLO" => "WORLD"}, ["hello ", "my world"]]) + + helper = new_helper("ANON_CACHE_DURATION" => 10) + helper.is_mobile = true + helper.cached.should == [200, {"HELLO" => "WORLD"}, ["hello my world"]] + + crawler.cached.should be_nil + crawler.cache([200, {"HELLO" => "WORLD"}, ["hello ", "world"]]) + crawler.cached.should == [200, {"HELLO" => "WORLD"}, ["hello world"]] end end