mirror of
https://github.com/discourse/discourse.git
synced 2025-03-23 04:15:41 +08:00
only applies to get html requests
This commit is contained in:
parent
ced7e9a691
commit
a84bb81ab5
@ -277,7 +277,10 @@ class Middleware::RequestTracker
|
|||||||
end
|
end
|
||||||
|
|
||||||
def block_crawler(request)
|
def block_crawler(request)
|
||||||
!request.path.ends_with?('robots.txt') &&
|
request.get? &&
|
||||||
|
!request.xhr? &&
|
||||||
|
request.env['HTTP_ACCEPT'] =~ /text\/html/ &&
|
||||||
|
!request.path.ends_with?('robots.txt') &&
|
||||||
CrawlerDetection.is_blocked_crawler?(request.env['HTTP_USER_AGENT'])
|
CrawlerDetection.is_blocked_crawler?(request.env['HTTP_USER_AGENT'])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ describe Middleware::RequestTracker do
|
|||||||
"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
|
"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
|
||||||
"REQUEST_URI" => "/path?bla=1",
|
"REQUEST_URI" => "/path?bla=1",
|
||||||
"REQUEST_METHOD" => "GET",
|
"REQUEST_METHOD" => "GET",
|
||||||
|
"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
|
||||||
"rack.input" => ""
|
"rack.input" => ""
|
||||||
}.merge(opts)
|
}.merge(opts)
|
||||||
end
|
end
|
||||||
@ -317,6 +318,14 @@ describe Middleware::RequestTracker do
|
|||||||
ApplicationRequest.write_cache!
|
ApplicationRequest.write_cache!
|
||||||
}.to_not change { ApplicationRequest.count }
|
}.to_not change { ApplicationRequest.count }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "allows json requests" do
|
||||||
|
SiteSetting.blacklisted_crawler_user_agents = 'Googlebot'
|
||||||
|
expect_success_response(*middleware.call(env(
|
||||||
|
'HTTP_USER_AGENT' => 'Googlebot/2.1 (+http://www.google.com/bot.html)',
|
||||||
|
'HTTP_ACCEPT' => 'application/json'
|
||||||
|
)))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user