Sha256: 80a8e340355ce2c4edab4f2d9e5220965e066e661ace45dce3da7754cbb77312

Contents?: true

Size: 1.53 KB

Versions: 22

Compression:

Stored size: 1.53 KB

Contents

module Typhoeus
  class Hydra
    class NetConnectNotAllowedError < StandardError; end

    module ConnectOptions
      def self.included(base)
        base.extend(ClassMethods)
      end

      # This method checks to see if we should raise an error on
      # a request.
      #
      # @raises NetConnectNotAllowedError
      def check_allow_net_connect!(request)
        return if Typhoeus::Hydra.allow_net_connect?
        return if Typhoeus::Hydra.ignore_hosts.include?(request.host_domain)

        raise NetConnectNotAllowedError, "Real HTTP requests are not allowed. Unregistered request: #{request.inspect}"
      end
      private :check_allow_net_connect!

      module ClassMethods
        def self.extended(base)
          class << base
            attr_accessor :allow_net_connect
            attr_accessor :ignore_localhost
          end
          base.allow_net_connect = true
          base.ignore_localhost = false
        end

        # Returns whether we allow external HTTP connections.
        # Useful for mocking/tests.
        #
        # @return [boolean] true/false
        def allow_net_connect?
          allow_net_connect
        end

        def ignore_localhost?
          ignore_localhost
        end

        def ignore_hosts
          @ignore_hosts ||= []

          if ignore_localhost?
            @ignore_hosts + Typhoeus::Request::LOCALHOST_ALIASES
          else
            @ignore_hosts
          end
        end

        def ignore_hosts=(hosts)
          @ignore_hosts = hosts
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 6 rubygems

Version Path
dcu-typhoeus-0.4.3 lib/typhoeus/hydra/connect_options.rb
dcu-typhoeus-0.4.2 lib/typhoeus/hydra/connect_options.rb
hhry-typhoeus-0.4.0 lib/typhoeus/hydra/connect_options.rb
typhoeus-0.4.2 lib/typhoeus/hydra/connect_options.rb
typhoeus-0.4.1 lib/typhoeus/hydra/connect_options.rb
typhoeus-0.4.0 lib/typhoeus/hydra/connect_options.rb
dcu-typhoeus-0.4.1 lib/typhoeus/hydra/connect_options.rb
dcu-typhoeus-0.4.0 lib/typhoeus/hydra/connect_options.rb
marnen-typhoeus-0.3.7 lib/typhoeus/hydra/connect_options.rb
marnen-typhoeus-0.3.6 lib/typhoeus/hydra/connect_options.rb
marnen-typhoeus-0.3.5 lib/typhoeus/hydra/connect_options.rb
marnen-typhoeus-0.3.4 lib/typhoeus/hydra/connect_options.rb
typhoeus-0.3.3 lib/typhoeus/hydra/connect_options.rb
typhoeus-0.3.2 lib/typhoeus/hydra/connect_options.rb
xenda-typhoeus-0.2.4 lib/typhoeus/hydra/connect_options.rb
typhoeus-0.2.4 lib/typhoeus/hydra/connect_options.rb
typhoeus-0.2.3 lib/typhoeus/hydra/connect_options.rb
arachni-typhoeus-0.2.0.2 lib/typhoeus/hydra/connect_options.rb
typhoeus-0.2.2 lib/typhoeus/hydra/connect_options.rb
arachni-typhoeus-0.2.0.1 lib/typhoeus/hydra/connect_options.rb