summaryrefslogtreecommitdiff
path: root/lib/gitlab/testing/robots_blocker_middleware.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/testing/robots_blocker_middleware.rb')
-rw-r--r--lib/gitlab/testing/robots_blocker_middleware.rb45
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