Sha256: 8cc83aa3f0e4e4d1a1bfeb19df47d6db3430e46ec23e96db194b31f8da30d9c5

Contents?: true

Size: 1.82 KB

Versions: 24

Compression:

Stored size: 1.82 KB

Contents

# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

require 'contrast/components/logger'

module Contrast
  module Utils
    # Util for information about an IO
    module IOUtil
      extend Contrast::Components::Logger::InstanceMethods

      class << self
        # We're only going to call rewind on things that we believe are safe to
        # call it on. This method allow lists those cases and returns false in
        # all others.
        def should_rewind? potential_io
          return true if potential_io.is_a?(StringIO)
          return false unless io?(potential_io)

          should_rewind_io?(potential_io)
        rescue StandardError => e
          logger.debug('Encountered an issue determining if rewindable', e, module: potential_io.cs__class.cs__name)
          false
        end

        # A class is IO if it is a decedent or delegate of IO
        def io? object
          return true if object.is_a?(IO)

          # DelegateClass, which is a Delegator, defines __getobj__ as a way to
          # get the object that the class wraps around (delegates to)
          return false unless object.is_a?(Delegator)

          object.__getobj__.is_a?(IO)
        end

        private

        # IO rewind cannot be used with streams such as pipes, ttys, and sockets or for ones which have been closed.
        #
        # @param io [IO] the input to check for the ability to rewind
        # @return [Boolean] if the given IO can be rewound
        def should_rewind_io? io
          return false if io.closed?
          return false if io.tty?

          status = io.stat
          return false unless status
          return false if status.pipe?
          return false if status.socket?

          true
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
contrast-agent-6.11.0 lib/contrast/utils/io_util.rb
contrast-agent-6.10.0 lib/contrast/utils/io_util.rb
contrast-agent-6.9.0 lib/contrast/utils/io_util.rb
contrast-agent-6.8.0 lib/contrast/utils/io_util.rb
contrast-agent-6.7.0 lib/contrast/utils/io_util.rb
contrast-agent-6.6.5 lib/contrast/utils/io_util.rb
contrast-agent-6.6.4 lib/contrast/utils/io_util.rb
contrast-agent-6.6.3 lib/contrast/utils/io_util.rb
contrast-agent-6.6.2 lib/contrast/utils/io_util.rb
contrast-agent-6.6.1 lib/contrast/utils/io_util.rb
contrast-agent-6.6.0 lib/contrast/utils/io_util.rb
contrast-agent-6.5.1 lib/contrast/utils/io_util.rb
contrast-agent-6.5.0 lib/contrast/utils/io_util.rb
contrast-agent-6.4.0 lib/contrast/utils/io_util.rb
contrast-agent-6.3.0 lib/contrast/utils/io_util.rb
contrast-agent-6.2.0 lib/contrast/utils/io_util.rb
contrast-agent-6.1.2 lib/contrast/utils/io_util.rb
contrast-agent-6.1.1 lib/contrast/utils/io_util.rb
contrast-agent-6.1.0 lib/contrast/utils/io_util.rb
contrast-agent-6.0.0 lib/contrast/utils/io_util.rb