<commit_before>module Lacquer
  class Varnish
    def stats
      send_command('stats').collect do |stats|
        stats = stats.split("\n")
        stats.shift
        stats = stats.collect do |stat|
          stat = stat.strip.match(/(\d+)\s+(.+)$/)
          { :key => stat[2], :value => stat[1] } if stat
        end
      end
    end

    # Sends the command 'url.purge *path*'
    def purge(*paths)
      paths.all? do |path|
        ActiveSupport::Notifications.instrument('purge.lacquer', path: path) do |payload|
          send_command(Lacquer.configuration.purge_command + " " + path.gsub('\\', '\\\\\\')).all? do |result|
            payload[:result] = result
            result =~ /200/
          end
        end
      end
    end

    # Sends commands over telnet to varnish servers listed in the config.
    def send_command(command)
      Lacquer.configuration.varnish_servers.collect do |server|
        retries = 0
        response = nil
        begin
          retries += 1
          connection = Net::Telnet.new(
            'Host' => server[:host],
            'Port' => server[:port],
            'Timeout' => server[:timeout] || 5)

          if(server[:secret])
            connection.waitfor("Match" => /^107/) do |authentication_request|
              salt = authentication_request.split("\n")[1][0..31]
              if(salt.empty?)
                raise VarnishError, "Bad authentication request"
              end

              digest = OpenSSL::Digest::Digest.new('sha256')
              digest << salt
              digest << "\n"
              digest << server[:secret]
              digest << salt
              digest << "\n"

              connection.cmd("String" => "auth #{digest.to_s}", "Match" => /\d{3}/) do |auth_response|
                if(!(/^200/ =~ auth_response))
                  raise AuthenticationError, "Could not authenticate"
                end
              end
            end<commit_msg>revert the regexp -> split change, not strictly required<commit_after>module Lacquer
  class Varnish
    def stats
      send_command('stats').collect do |stats|
        stats = stats.split("\n")
        stats.shift
        stats = stats.collect do |stat|
          stat = stat.strip.match(/(\d+)\s+(.+)$/)
          { :key => stat[2], :value => stat[1] } if stat
        end
      end
    end

    # Sends the command 'url.purge *path*'
    def purge(*paths)
      paths.all? do |path|
        ActiveSupport::Notifications.instrument('purge.lacquer', path: path) do |payload|
          send_command(Lacquer.configuration.purge_command + " " + path.gsub('\\', '\\\\\\')).all? do |result|
            payload[:result] = result
            result =~ /200/
          end
        end
      end
    end

    # Sends commands over telnet to varnish servers listed in the config.
    def send_command(command)
      Lacquer.configuration.varnish_servers.collect do |server|
        retries = 0
        response = nil
        begin
          retries += 1
          connection = Net::Telnet.new(
            'Host' => server[:host],
            'Port' => server[:port],
            'Timeout' => server[:timeout] || 5)

          if(server[:secret])
            connection.waitfor("Match" => /^107/) do |authentication_request|
              matchdata = /^107 \d{2}\s*(.{32}).*$/m.match(authentication_request) # Might be a bit ugly regex, but it works great!
              salt = matchdata[1]
              if(salt.empty?)
                raise VarnishError, "Bad authentication request"
              end

              digest = OpenSSL::Digest::Digest.new('sha256')
              digest << salt
              digest << "\n"
              digest << server[:secret]
              digest << salt
              digest << "\n"

              connection.cmd("String" => "auth #{digest.to_s}", "Match" => /\d{3}/) do |auth_response|
                if(!(/^200/ =~ auth_response))
                  raise AuthenticationError, "Could not authenticate"
                end
              end
            end
{'fraction_non_alphanumeric': 0.13382899628252787, 'fraction_numerical': 0.011648079306071871, 'mean_word_length': 1.6978609625668448, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 20, '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': '1839933', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1114, 'n_words': 279}