2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-26 17:03:20 +01:00
|
|
|
module WildcardUrlChecker
|
2019-12-13 13:12:12 +11:00
|
|
|
VALID_PROTOCOLS = %w(http https discourse).freeze
|
2019-02-26 17:03:20 +01:00
|
|
|
|
|
|
|
def self.check_url(url, url_to_check)
|
2019-12-13 13:12:12 +11:00
|
|
|
return nil if !valid_url?(url_to_check)
|
|
|
|
|
2019-02-26 17:03:20 +01:00
|
|
|
escaped_url = Regexp.escape(url).sub("\\*", '\S*')
|
2019-12-13 13:12:12 +11:00
|
|
|
url_regex = Regexp.new("\\A#{escaped_url}\\z", 'i')
|
2019-02-26 17:03:20 +01:00
|
|
|
|
|
|
|
url_to_check.match(url_regex)
|
|
|
|
end
|
|
|
|
|
2019-12-13 13:12:12 +11:00
|
|
|
private
|
|
|
|
|
|
|
|
def self.valid_url?(url)
|
|
|
|
uri = URI.parse(url)
|
|
|
|
VALID_PROTOCOLS.include?(uri&.scheme) && uri&.host.present?
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
false
|
|
|
|
end
|
2019-02-26 17:03:20 +01:00
|
|
|
end
|