diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /lib/gitlab/testing/robots_blocker_middleware.rb | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'lib/gitlab/testing/robots_blocker_middleware.rb')
-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 |