router.rb 2.03 KB
Newer Older
1
2
3
4
module Gitlab
  module EtagCaching
    class Router
      Route = Struct.new(:regexp, :name)
5
6
7
8
9
      # We enable an ETag for every request matching the regex.
      # To match a regex the path needs to match the following:
      #   - Don't contain a reserved word (expect for the words used in the
      #     regex itself)
      #   - Ending in `noteable/issue/<id>/notes` for the `issue_notes` route
10
11
      #   - Ending in `issues/id`/realtime_changes` for the `issue_title` route
      USED_IN_ROUTES = %w[noteable issue notes issues realtime_changes
12
                          commit pipelines merge_requests new].freeze
13
      RESERVED_WORDS = Gitlab::Regex::ILLEGAL_PROJECT_PATH_WORDS - USED_IN_ROUTES
14
      RESERVED_WORDS_REGEX = Regexp.union(*RESERVED_WORDS)
15
16
      ROUTES = [
        Gitlab::EtagCaching::Router::Route.new(
17
          %r(^(?!.*(#{RESERVED_WORDS_REGEX})).*/noteable/issue/\d+/notes\z),
18
19
20
          'issue_notes'
        ),
        Gitlab::EtagCaching::Router::Route.new(
21
          %r(^(?!.*(#{RESERVED_WORDS_REGEX})).*/issues/\d+/realtime_changes\z),
22
23
24
          'issue_title'
        ),
        Gitlab::EtagCaching::Router::Route.new(
25
          %r(^(?!.*(#{RESERVED_WORDS_REGEX})).*/commit/\S+/pipelines\.json\z),
26
27
28
          'commit_pipelines'
        ),
        Gitlab::EtagCaching::Router::Route.new(
29
          %r(^(?!.*(#{RESERVED_WORDS_REGEX})).*/merge_requests/new\.json\z),
30
31
32
          'new_merge_request_pipelines'
        ),
        Gitlab::EtagCaching::Router::Route.new(
33
          %r(^(?!.*(#{RESERVED_WORDS_REGEX})).*/merge_requests/\d+/pipelines\.json\z),
34
35
36
          'merge_request_pipelines'
        ),
        Gitlab::EtagCaching::Router::Route.new(
37
          %r(^(?!.*(#{RESERVED_WORDS_REGEX})).*/pipelines\.json\z),
38
          'project_pipelines'
39
40
41
42
        ),
        Gitlab::EtagCaching::Router::Route.new(
          %r(^(?!.*(#{RESERVED_WORDS})).*/pipelines/\d+\.json\z),
          'project_pipeline'
43
        )
44
45
46
47
48
49
50
51
      ].freeze

      def self.match(env)
        ROUTES.find { |route| route.regexp.match(env['PATH_INFO']) }
      end
    end
  end
end