<commit_before># encoding: utf-8

class RawNetCapture < StringIO
  attr_reader :raw_traffic

  def initialize
    super
    reset
  end

  def received(data)
    @raw_traffic << [:received, data]
  end

  def sent(data)
    @raw_traffic << [:sent, data]
  end

  private

  def reset
    @raw_traffic = []
  end
end

class RawHTTPCapture < StringIO
  attr_reader :transactions

  def initialize
    super
    reset
  end

  def received(data)
    @raw_received << data
  end

  def sent(data)
    # when there are multiple requests on the same connection, E.g. redirection, we want the last one.
    if @raw_received.length > 0
      reset
    end

    @raw_sent << data
  end

  def transactions
    @transactions ||= []
  end

  private

  attr_reader :raw_sent, :raw_received

  def reset
    @raw_received = StringIO.new
    @raw_sent = StringIO.new
    transactions << Transaction.new(self)
  end

  class Transaction
    HEADER_BODY_SEPARATOR = "\r\n\r\n"
    attr_reader :response, :request

    def initialize(capture)
      @request = Request.new(capture.send(:raw_sent))
      @response = Response.new(capture.send(:raw_received))
    end

    class TransactionPart
      attr_reader :headers, :body, :raw

      def initialize(raw_io)
        @raw_io = raw_io
      end

      def body
        @body ||= unless calculated_parts?
          calculate_parts
          body
        end
      end

      def headers
        @headers ||= unless calculated_parts?
          calculate_parts
          headers
        end
      end

      def raw
        @raw ||= raw_io.string
      end

      private

      attr_reader :raw_io

      def calculate_parts
        @headers, _, @body = raw.partition(HEADER_BODY_SEPARATOR).map do |part|
          part.empty? ? nil : part
        end

        @calculated_parts = true
      end

      def calculated_parts?
        !!@calculated_parts
      end
    end

    Response = Request = TransactionPart
  end
end

module Net
  class BufferedIO
    private<commit_msg>Define classes explicitly rather than setting constant variables<commit_after># encoding: utf-8

class RawNetCapture < StringIO
  attr_reader :raw_traffic

  def initialize
    super
    reset
  end

  def received(data)
    @raw_traffic << [:received, data]
  end

  def sent(data)
    @raw_traffic << [:sent, data]
  end

  private

  def reset
    @raw_traffic = []
  end
end

class RawHTTPCapture < StringIO
  attr_reader :transactions

  def initialize
    super
    reset
  end

  def received(data)
    @raw_received << data
  end

  def sent(data)
    # when there are multiple requests on the same connection, E.g. redirection, we want the last one.
    if @raw_received.length > 0
      reset
    end

    @raw_sent << data
  end

  def transactions
    @transactions ||= []
  end

  private

  attr_reader :raw_sent, :raw_received

  def reset
    @raw_received = StringIO.new
    @raw_sent = StringIO.new
    transactions << Transaction.new(self)
  end

  class Transaction
    HEADER_BODY_SEPARATOR = "\r\n\r\n"
    attr_reader :response, :request

    def initialize(capture)
      @request = Request.new(capture.send(:raw_sent))
      @response = Response.new(capture.send(:raw_received))
    end

    class Part
      attr_reader :headers, :body, :raw

      def initialize(raw_io)
        @raw_io = raw_io
      end

      def body
        @body ||= unless calculated_parts?
          calculate_parts
          body
        end
      end

      def headers
        @headers ||= unless calculated_parts?
          calculate_parts
          headers
        end
      end

      def raw
        @raw ||= raw_io.string
      end

      private

      attr_reader :raw_io

      def calculate_parts
        @headers, _, @body = raw.partition(HEADER_BODY_SEPARATOR).map do |part|
          part.empty? ? nil : part
        end

        @calculated_parts = true
      end

      def calculated_parts?
        !!@calculated_parts
      end
    end

    class Response < Part; end
    class Request < Part; end
  end
end

module Net
  class BufferedIO
    private
{'fraction_non_alphanumeric': 0.07349338559529642, 'fraction_numerical': 0.0009799118079372856, 'mean_word_length': 2.187353629976581, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 5, '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': '5411413', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1238, 'n_words': 349}