mirror of
https://github.com/discourse/discourse.git
synced 2025-02-14 23:35:00 +00:00
BUGFIX: web crawlers messing with anon caching
This commit is contained in:
parent
ba683bc611
commit
35952055e2
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user