<commit_before>module BotFramework
  class Server
    def self.call(env)
      new.call(env)
    end

    def call(env)
      @request = Rack::Request.new env
      @response = Rack::Response.new
      if @request.post?
        if verify
          receive
        else
          p "Errors"
          p TokenValidator.new(headers).errors.join(",")
          raise InvalidToken
        end
      end
      @response.finish
    end

    # TODO: reply in separate thread t avoid timeout
    def receive
      # Thread.new {
      activity = Activity.new.build_from_hash JSON.parse(@request.body.read)
      Bot.receive(activity)
      # }
    end

    def headers
      env = @request.env
      Hash[*env.select { |k, _v| k.start_with? 'HTTP_' }
               .collect { |k, v| [k.sub(/^HTTP_/, ''), v] }
               .collect { |k, v| [k.split('_').collect(&:capitalize).join('-'), v] }
           .sort
           .flatten]
    end

    def verify
      TokenValidator.new(headers).valid?
    rescue JWT::DecodeError
      [401, { 'Content-Type' => 'text/plain' }, ['A token must be passed.']]
    rescue JWT::ExpiredSignature
      [403, { 'Content-Type' => 'text/plain' }, ['The token has expired.']]
    rescue JWT::InvalidIssuerError
      [403, { 'Content-Type' => 'text/plain' }, ['The token does not have a valid issuer.']]
    rescue JWT::InvalidIatError
      [403, { 'Content-Type' => 'text/plain' }, ['The token does not have a valid "issued at" time.']]
    end
  end
end<commit_msg>quick fix<commit_after>module BotFramework
  class Server
    def self.call(env)
      new.call(env)
    end

    def call(env)
      @request = Rack::Request.new env
      @response = Rack::Response.new
      if @request.post?
        if verify
          receive
        else
          raise InvalidToken
        end
      end
      @response.finish
    end

    # TODO: reply in separate thread t avoid timeout
    def receive
      # Thread.new {
      activity = Activity.new.build_from_hash JSON.parse(@request.body.read)
      Bot.receive(activity)
      # }
    end

    def headers
      env = @request.env
      Hash[*env.select { |k, _v| k.start_with? 'HTTP_' }
               .collect { |k, v| [k.sub(/^HTTP_/, ''), v] }
               .collect { |k, v| [k.split('_').collect(&:capitalize).join('-'), v] }
           .sort
           .flatten]
    end

    # Use logger instead of puts
    def verify
      validator = TokenValidator.new(headers)
      if validator.valid?
        return true
      else
        p "Errors: #{validator.errors}"
      end
    rescue JWT::DecodeError
      [401, { 'Content-Type' => 'text/plain' }, ['A token must be passed.']]
    rescue JWT::ExpiredSignature
      [403, { 'Content-Type' => 'text/plain' }, ['The token has expired.']]
    rescue JWT::InvalidIssuerError
      [403, { 'Content-Type' => 'text/plain' }, ['The token does not have a valid issuer.']]
    rescue JWT::InvalidIatError
      [403, { 'Content-Type' => 'text/plain' }, ['The token does not have a valid "issued at" time.']]
    end
  end
end
{'fraction_non_alphanumeric': 0.13584288052373159, 'fraction_numerical': 0.007855973813420621, 'mean_word_length': 2.089989888776542, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5295427', 'n_tokens_mistral': 987, 'n_tokens_neox': 954, 'n_words': 255}