mirror of
https://github.com/discourse/discourse.git
synced 2024-12-18 21:46:29 +08:00
24 lines
525 B
Ruby
24 lines
525 B
Ruby
# frozen_string_literal: true
|
|
|
|
module WildcardUrlChecker
|
|
VALID_PROTOCOLS = %w(http https discourse).freeze
|
|
|
|
def self.check_url(url, url_to_check)
|
|
return nil if !valid_url?(url_to_check)
|
|
|
|
escaped_url = Regexp.escape(url).sub("\\*", '\S*')
|
|
url_regex = Regexp.new("\\A#{escaped_url}\\z", 'i')
|
|
|
|
url_to_check.match(url_regex)
|
|
end
|
|
|
|
private
|
|
|
|
def self.valid_url?(url)
|
|
uri = URI.parse(url)
|
|
VALID_PROTOCOLS.include?(uri&.scheme) && uri&.host.present?
|
|
rescue URI::InvalidURIError
|
|
false
|
|
end
|
|
end
|