PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/helpers/katello/katello_url_helper.rb

https://github.com/daviddavis/katello
Ruby | 49 lines | 28 code | 7 blank | 14 comment | 0 complexity | 78d685693d355fa552beedda427e1321 MD5 | raw file
Possible License(s): GPL-2.0
  1. #
  2. # Copyright 2014 Red Hat, Inc.
  3. #
  4. # This software is licensed to you under the GNU General Public
  5. # License as published by the Free Software Foundation; either version
  6. # 2 of the License (GPLv2) or (at your option) any later version.
  7. # There is NO WARRANTY for this software, express or implied,
  8. # including the implied warranties of MERCHANTABILITY,
  9. # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
  10. # have received a copy of GPLv2 along with this software; if not, see
  11. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  12. module Katello
  13. module KatelloUrlHelper
  14. include Rails.application.routes.url_helpers
  15. unless defined? CONSTANTS_DEFINED
  16. PORT = /(([:]\d{1,5})?)/
  17. PROTOCOLS = %r{(https?|ftp)://}ix
  18. FILEPREFIX = %r{(^file://)|^/}ix # is this a file based url
  19. # validation of hostname according to RFC952 and RFC1123
  20. DOMAIN = /(?:(?:(?:(?:[a-z0-9][-a-z0-9]{0,61})?[a-z0-9])[.])*(?:[a-z][-a-z0-9]{0,61}[a-z0-9]|[a-z])[.]?)/
  21. IPV4 = /(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/
  22. #TODO: ipv6 support
  23. #IPV6 = /(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}/
  24. URLREG = /^#{PROTOCOLS}((localhost)|#{DOMAIN}|#{IPV4})#{PORT}(\/.*)?$/ix
  25. FILEREG = /#{FILEPREFIX}([\w-]*\/?)*/ix # match file based urls
  26. CONSTANTS_DEFINED = true
  27. end
  28. def kipv4?(url)
  29. !!IPV4.match(url)
  30. end
  31. def kprotocol?(url)
  32. regex = /^#{PROTOCOLS}/
  33. !!regex.match(url)
  34. end
  35. def kurl_valid?(url)
  36. !!(file_prefix?(url) ? FILEREG.match(url) : URLREG.match(url))
  37. end
  38. def file_prefix?(url)
  39. !!FILEPREFIX.match(url)
  40. end
  41. end
  42. end