Welcome to mirror list, hosted at ThFree Co, Russian Federation.

robots_blocker_middleware.rb « testing « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 034492122df38d3ab7e36cdc9fbc2044674ab69c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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