Sha256: 8b9772e6ea40c16e39b6d75f200cd81a9b2e324d75d6e2217f0f218514580243

Contents?: true

Size: 1.33 KB

Versions: 77

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require 'ipaddr'

module Aws
  module Endpoints

    # @api private
    class URL
      def initialize(url)
        uri = URI(url)
        @scheme = uri.scheme
        # only support http and https schemes
        raise ArgumentError unless %w[https http].include?(@scheme)

        # do not support query
        raise ArgumentError if uri.query

        @authority = _authority(url, uri)
        @path = uri.path
        @normalized_path = uri.path + (uri.path[-1] == '/' ? '' : '/')
        @is_ip = _is_ip(uri.host)
      end

      attr_reader :scheme
      attr_reader :authority
      attr_reader :path
      attr_reader :normalized_path
      attr_reader :is_ip

      def as_json(_options = {})
        {
          'scheme' => scheme,
          'authority' => authority,
          'path' => path,
          'normalizedPath' => normalized_path,
          'isIp' => is_ip
        }
      end

      private

      def _authority(url, uri)
        # don't include port if it's default and not parsed originally
        if uri.default_port == uri.port && !url.include?(":#{uri.port}")
          uri.host
        else
          "#{uri.host}:#{uri.port}"
        end
      end

      def _is_ip(authority)
        IPAddr.new(authority)
        true
      rescue IPAddr::InvalidAddressError
        false
      end
    end
  end
end

Version data entries

77 entries across 77 versions & 1 rubygems

Version Path
aws-sdk-core-3.214.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.213.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.212.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.211.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.210.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.209.1 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.209.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.208.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.207.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.206.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.205.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.204.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.203.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.202.2 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.202.1 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.202.0 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.201.5 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.201.4 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.201.3 lib/aws-sdk-core/endpoints/url.rb
aws-sdk-core-3.201.2 lib/aws-sdk-core/endpoints/url.rb