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

virtual_host_finder.rb « pages « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d5e2159fb523bba47068d69d2927ee8b44e92344 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

module Gitlab
  module Pages
    class VirtualHostFinder
      def initialize(host)
        @host = host&.downcase
      end

      def execute
        return if host.blank?

        gitlab_host = ::Gitlab.config.pages.host.downcase.prepend(".")

        if host.ends_with?(gitlab_host)
          name = host.delete_suffix(gitlab_host)

          by_unique_domain(name) || by_namespace_domain(name)
        else
          by_custom_domain(host)
        end
      end

      private

      attr_reader :host

      def by_unique_domain(name)
        project = Project.by_pages_enabled_unique_domain(name)

        return unless Feature.enabled?(:pages_unique_domain, project)
        return unless project&.pages_deployed?

        ::Pages::VirtualDomain.new(projects: [project])
      end

      def by_namespace_domain(name)
        namespace = Namespace.top_most.by_path(name)

        return if namespace.blank?

        cache = if Feature.enabled?(:cache_pages_domain_api, namespace)
                  ::Gitlab::Pages::CacheControl.for_namespace(namespace.id)
                end

        ::Pages::VirtualDomain.new(
          trim_prefix: namespace.full_path,
          projects: namespace.all_projects_with_pages,
          cache: cache
        )
      end

      def by_custom_domain(host)
        domain = PagesDomain.find_by_domain_case_insensitive(host)

        return unless domain&.pages_deployed?

        cache = if Feature.enabled?(:cache_pages_domain_api, domain.project.root_namespace)
                  ::Gitlab::Pages::CacheControl.for_domain(domain.id)
                end

        ::Pages::VirtualDomain.new(
          projects: [domain.project],
          domain: domain,
          cache: cache
        )
      end
    end
  end
end