<commit_before>require 'net/https'

module Logglier
  module Client
    class HTTP

      # Used to wrap and setup Net::HTTP as we need it
      class NetHTTPProxy

        # @param [URI] input_uri URI to deliver messages to
        # @param [Hash] opts Option hash
        # @option [Integer] read_timeout Read timeout for the http session. defaults to 2
        # @option [Integer] open_timeout Open timeout for the http session. defaults to 2
        # @option [Integer] verify_mode OpenSSL::SSL::VERIFY_* constant
        # @option [String] ca_file Path to the ca file
        def initialize(input_uri, opts={})
          @input_uri = input_uri

          @http = Net::HTTP.new(@input_uri.host, @input_uri.port)

          if @input_uri.scheme == 'https'
            @http.use_ssl = true
            @http.verify_mode = opts[:verify_mode] || OpenSSL::SSL::VERIFY_PEER
            @http.ca_file = opts[:ca_file] if opts[:ca_file]
          end

          # We prefer persistent HTTP connections, so workaround http://redmine.ruby-lang.org/issues/4522
          @http.start

          @http.read_timeout = opts[:read_timeout] || 2
          @http.open_timeout = opts[:open_timeout] || 2
        end

        # Delivers the message via HTTP, handling errors
        #
        # @param [String] message The message to deliver
        def deliver(message)
          retried = false
          begin
            @http.request_post(@input_uri.path, message)
          # We're using persistent connections, so connection can be closed by the other side
          # after a timeout. Don't consider it an error, just retry once.
          rescue Errno::ECONNRESET
            unless retried
              retried = true
              retry
            else
              $stderr.puts "WARNING: connection was reset while posting message: #{message}"
            end
          rescue TimeoutError, OpenSSL::SSL::SSLError, EOFError, Errno::ECONNRESET => e
            $stderr.puts "WARNING: #{e.class} posting message: #{message}"
          end
        end
      end

    end
  end
end<commit_msg>refactor http connection stuff<commit_after>require 'net/https'

module Logglier
  module Client
    class HTTP

      # Used to wrap and setup Net::HTTP as we need it
      class NetHTTPProxy

        # @param [URI] input_uri URI to deliver messages to
        # @param [Hash] opts Option hash
        # @option [Integer] read_timeout Read timeout for the http session. defaults to 2
        # @option [Integer] open_timeout Open timeout for the http session. defaults to 2
        # @option [Integer] verify_mode OpenSSL::SSL::VERIFY_* constant
        # @option [String] ca_file Path to the ca file
        def initialize(input_uri, opts={})
          @input_uri = input_uri
          @verify_mode = opts[:verify_mode] || OpenSSL::SSL::VERIFY_PEER
          @ca_file = opts[:ca_file]
          @read_timeout = opts[:read_timeout] || 5
          @open_timeout = opts[:open_timeout] || 5

          connect!
        end

        # Delivers the message via HTTP, handling errors
        #
        # @param [String] message The message to deliver
        def deliver(message)
          retried = false
          begin
            @http.request_post(@input_uri.path, message)
          # We're using persistent connections, so connection can be closed by the other side
          # after a timeout. Don't consider it an error, just retry once.
          rescue Errno::ECONNRESET
            unless retried
              retried = true
              connect!
              retry
            else
              $stderr.puts "WARNING: connection was reset while posting message: #{message}"
            end
          rescue TimeoutError, OpenSSL::SSL::SSLError, EOFError, Errno::ECONNRESET => e
            $stderr.puts "WARNING: #{e.class} posting message: #{message}"
          end
        end

        private

        def connect!
          @http = Net::HTTP.new(@input_uri.host, @input_uri.port)

          if @input_uri.scheme == 'https'
            @http.use_ssl = true
            @http.verify_mode = @verify_mode
            @http.ca_file = @ca_file
          end

          # We prefer persistent HTTP connections, so workaround http://redmine.ruby-lang.org/issues/4522
          @http.start

          @http.read_timeout = @read_timeout
          @http.open_timeout = @open_timeout

        end
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.08376726284808694, 'fraction_numerical': 0.003622368123160516, 'mean_word_length': 2.036426116838488, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7566972', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1184, 'n_words': 390}