debian-mirror-gitlab/lib/gitlab/routing.rb

70 lines
1.9 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
module Gitlab
module Routing
2017-08-17 22:00:37 +05:30
extend ActiveSupport::Concern
2020-06-23 00:09:42 +05:30
class LegacyRedirector
# @params path_type [symbol] type of path to do "-" redirection
# https://gitlab.com/gitlab-org/gitlab/-/issues/16854
def initialize(path_type)
@path_type = path_type
end
def call(_params, request)
ensure_valid_uri!(request)
# Only replace the last occurrence of `path`.
#
# `request.fullpath` includes the querystring
new_path = request.path.sub(%r{/#{@path_type}(/*)(?!.*#{@path_type})}, "/-/#{@path_type}\\1")
new_path = "#{new_path}?#{request.query_string}" if request.query_string.present?
new_path
end
private
def ensure_valid_uri!(request)
URI.parse(request.path)
rescue URI::InvalidURIError => e
# If url is invalid, raise custom error,
# which can be ignored by monitoring tools.
2021-06-08 01:23:25 +05:30
raise ActionController::RoutingError, e.message
2020-06-23 00:09:42 +05:30
end
end
2017-09-10 17:25:29 +05:30
mattr_accessor :_includers
self._includers = []
2017-08-17 22:00:37 +05:30
included do
2017-09-10 17:25:29 +05:30
Gitlab::Routing.includes_helpers(self)
2017-08-17 22:00:37 +05:30
include Gitlab::Routing.url_helpers
end
2017-09-10 17:25:29 +05:30
def self.includes_helpers(klass)
self._includers << klass
end
2016-06-02 11:05:42 +05:30
# Returns the URL helpers Module.
#
# This method caches the output as Rails' "url_helpers" method creates an
# anonymous module every time it's called.
#
# Returns a Module.
def self.url_helpers
@url_helpers ||= Gitlab::Application.routes.url_helpers
end
2018-03-17 18:26:18 +05:30
def self.redirect_legacy_paths(router, *paths)
paths.each do |path|
router.match "/#{path}(/*rest)",
via: [:get, :post, :patch, :delete],
2020-06-23 00:09:42 +05:30
to: router.redirect(LegacyRedirector.new(path)),
2018-03-17 18:26:18 +05:30
as: "legacy_#{path}_redirect"
end
end
2016-06-02 11:05:42 +05:30
end
end