Sha256: d56de40cc1bb57ccb4cdf0954f4a2f87e6fa65bb24937d67702fe580fbc1f600

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module HTTPX
  module Utils
    using URIExtensions

    module_function

    # The value of this field can be either an HTTP-date or a number of
    # seconds to delay after the response is received.
    def parse_retry_after(retry_after)
      # first: bet on it being an integer
      Integer(retry_after)
    rescue ArgumentError
      # Then it's a datetime
      time = Time.httpdate(retry_after)
      time - Time.now
    end

    if RUBY_VERSION < "2.3"

      def to_uri(uri)
        URI(uri)
      end

    else

      URIParser = URI::RFC2396_Parser.new

      def to_uri(uri)
        return Kernel.URI(uri) unless uri.is_a?(String) && !uri.ascii_only?

        uri = Kernel.URI(URIParser.escape(uri))

        non_ascii_hostname = URIParser.unescape(uri.host)

        non_ascii_hostname.force_encoding(Encoding::UTF_8)

        idna_hostname = Punycode.encode_hostname(non_ascii_hostname)

        uri.host = idna_hostname
        uri.non_ascii_hostname = non_ascii_hostname
        uri
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
httpx-0.16.1 lib/httpx/utils.rb
httpx-0.16.0 lib/httpx/utils.rb
httpx-0.15.4 lib/httpx/utils.rb
httpx-0.15.3 lib/httpx/utils.rb
httpx-0.15.2 lib/httpx/utils.rb
httpx-0.15.1 lib/httpx/utils.rb
httpx-0.15.0 lib/httpx/utils.rb