<commit_before>require 'net/protocol'
require 'uri'

module ICAP
  class Client
    attr_reader :address, :port

    # Number of seconds to wait for the connection to open. The default
    # value is +nil+.
    attr_accessor :open_timeout

    # Creates a new ICAP::Client object for the specified server address,
    # without opening the TCP connection or initializing the ICAP session.
    # The +address+ should be a DNS hostname or IP address.
    def initialize(address, port = nil)
      @address = address
      @port = (port || ICAP.default_port)

      @open_timeout = nil
      @started = false
      @socket = nil
      @started = false
    end

    def inspect
      "#<#{self.class} #{@address}:#{@port} open=#{started?}>"
    end

    def started?
      @started
    end

    # Opens a TCP connection and ICAP session.
    #
    # When this method is called with a block, it passes the ICAP::Client
    # object to the block, and closes the TCP connection and ICAP session
    # after the block has been executed.
    #
    # When called with a block, it returns the return value of the
    # block; otherwise, it returns self.
    #
    def start  # :yield: http
      raise IOError, 'ICAP session already opened' if @started
      if block_given?
        begin
          do_start
          return yield(self)
        ensure
          do_finish
        end
      end
      do_start
      self
    end

    def do_start
      connect
      @started = true
    end
    private :do_start

    def connect
      s = Timeout.timeout(@open_timeout, ::Net::OpenTimeout) {
        begin
          TCPSocket.open(address, port)
        rescue => e
          raise e, "Failed to open TCP connection to " +
            "#{address}:#{port} (#{e.message})"
        end
      }
      @socket = ::Net::BufferedIO.new(s)
    end
    private :connect

    # Finishes the ICAP session and closes the TCP connection.
    # Raises IOError if the session has not been started.
    def finish
      raise IOError, 'ICAP session not yet started' unless started?
      do_finish
    end

    def do_finish
      @started = false
      @socket.close if @socket and not @socket.closed?
      @socket = nil<commit_msg>Building socket needs to be ruby 1.9.3 friendly<commit_after>require 'net/protocol'
require 'uri'

module ICAP
  class Client
    attr_reader :address, :port

    # Number of seconds to wait for the connection to open. The default
    # value is +nil+.
    attr_accessor :open_timeout

    # Creates a new ICAP::Client object for the specified server address,
    # without opening the TCP connection or initializing the ICAP session.
    # The +address+ should be a DNS hostname or IP address.
    def initialize(address, port = nil)
      @address = address
      @port = (port || ICAP.default_port)

      @open_timeout = nil
      @started = false
      @socket = nil
      @started = false
    end

    def inspect
      "#<#{self.class} #{@address}:#{@port} open=#{started?}>"
    end

    def started?
      @started
    end

    # Opens a TCP connection and ICAP session.
    #
    # When this method is called with a block, it passes the ICAP::Client
    # object to the block, and closes the TCP connection and ICAP session
    # after the block has been executed.
    #
    # When called with a block, it returns the return value of the
    # block; otherwise, it returns self.
    #
    def start  # :yield: http
      raise IOError, 'ICAP session already opened' if @started
      if block_given?
        begin
          do_start
          return yield(self)
        ensure
          do_finish
        end
      end
      do_start
      self
    end

    def do_start
      connect
      @started = true
    end
    private :do_start

    def connect
      s = timeout(@open_timeout) { TCPSocket.open(address, port) }
      @socket = ::Net::BufferedIO.new(s)
    end
    private :connect

    # Finishes the ICAP session and closes the TCP connection.
    # Raises IOError if the session has not been started.
    def finish
      raise IOError, 'ICAP session not yet started' unless started?
      do_finish
    end

    def do_finish
      @started = false
      @socket.close if @socket and not @socket.closed?
      @socket = nil
{'fraction_non_alphanumeric': 0.0745356219139431, 'fraction_numerical': 0.0007053844345168116, 'mean_word_length': 2.2227272727272727, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6466991', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1198, 'n_words': 475}