Sha256: 7b5c24545746cc3847449b7b8e51f5742b6c5a798054344d507189db3366a00c

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require 'ipaddress'

module Yawast
  module Shared
    class Uri
      def self.extract_uri(url)
        # this might be buggy - actually, I know it is...
        url = 'http://' + url unless url.include?('http://') || url.include?('https://')

        # make sure the path is at least a slash
        uri = URI.parse(url)
        uri.path = '/' if uri.path == ''

        # this is buggy, but we don't handle files anyhow...
        # if the path doesn't end in a slash, add one.
        uri.path.concat '/' if uri.path[-1, 1] != '/'

        # see if we can resolve the host
        # we don't really need it, it just serves as validation
        begin
          dns = Resolv::DNS.new
          dns.getaddress(uri.host)
        rescue => e # rubocop:disable Style/RescueStandardError
          if uri.host == 'localhost'
            # do nothing, in this case, we just don't care.
          elsif IPAddress.valid? uri.host
            # in this case the host name is actually a IP, let it go through.
          else
            # we've passed all the exceptions, if we are here, it's a problem
            raise ArgumentError, "Invalid URL (#{e.message})"
          end
        end

        uri
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yawast-0.7.2 lib/shared/uri.rb
yawast-0.7.1 lib/shared/uri.rb
yawast-0.7.0 lib/shared/uri.rb
yawast-0.7.0.beta3 lib/shared/uri.rb
yawast-0.7.0.beta2 lib/shared/uri.rb