diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-16 03:09:37 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-16 03:09:37 +0300 |
commit | fe890c45c21e32bc7d06d093fe2c37f319c62e3e (patch) | |
tree | 4c1801723d3e028e1510d9b52490a726560f46f7 /lib/gitlab/testing | |
parent | 7dc8bd3c16a6f8367fdee691711d3313e2efc3c6 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/testing')
-rw-r--r-- | lib/gitlab/testing/robots_blocker_middleware.rb | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/gitlab/testing/robots_blocker_middleware.rb b/lib/gitlab/testing/robots_blocker_middleware.rb new file mode 100644 index 00000000000..034492122df --- /dev/null +++ b/lib/gitlab/testing/robots_blocker_middleware.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +# rubocop:disable Style/ClassVars +module Gitlab + module Testing + class RobotsBlockerMiddleware + @@block_requests = Concurrent::AtomicBoolean.new(false) + + # Block requests according to robots.txt. + # Any new requests disallowed by robots.txt will return an HTTP 503 status. + def self.block_requests! + @@block_requests.value = true + end + + # Allows the server to accept requests again. + def self.allow_requests! + @@block_requests.value = false + end + + def initialize(app) + @app = app + end + + def call(env) + request = Rack::Request.new(env) + + if block_requests? && Gitlab::RobotsTxt.disallowed?(request.path_info) + block_request(env) + else + @app.call(env) + end + end + + private + + def block_requests? + @@block_requests.true? + end + + def block_request(env) + [503, {}, []] + end + end + end +end |