<commit_before>
require 'sinatra/base'

module Bixby
class Server < Sinatra::Base

  SHELL_EXEC = "shell_exec"

  DEFAULT_PORT = 18000

  class << self
    attr_accessor :agent
  end

  def initialize
    super
    @log = Logging.logger[self]
  end

  def agent
    self.class.agent
  end

  get '/*' do
    @log.debug { "Disposing of GET request: #{request.path}" }
    status 405
    return "GET requests are not allowed\n"
  end

  post '/*' do
    res = handle_request().to_json
    return encrypt(res)
  end

  # Encrypt the response
  #
  # @param [String] json      json response
  #
  # @return [String] base64 encrypted response when crypto is enabled,
  #                  otherwise, plain json
  def encrypt(json)
    agent.crypto_enabled? ? agent.encrypt_for_server(json) : json
  end

  def handle_request
    req = extract_valid_request()
    if req.kind_of? JsonResponse then
      @log.debug { "request extraction failed" }
      return req
    end

    return handle_exec(req)
  end

  def extract_valid_request
    body = request.body.read.strip
    if body.nil? or body.empty? then
      return JsonResponse.invalid_request
    end

    if agent.crypto_enabled? then
      body = agent.decrypt_from_server(body)
    end

    begin
      req = JsonRequest.from_json(body)
    rescue Exception
      return JsonResponse.invalid_request
    end

    if SHELL_EXEC != req.operation then
      return JsonResponse.invalid_request("unsupported operation: #{req.operation}")
    end

    return req
  end

  # Handle the exec request and return the response
  #
  # @return [JsonResponse]
  def handle_exec(req)
    begin
      cmd_res = agent.shell_exec(req.params)
      @log.debug { cmd_res.to_s + "\n---\n\n\n" }
      return cmd_res.to_json_response

    rescue Exception => ex
      if ex.kind_of? BundleNotFound then
        return JsonResponse.bundle_not_found(ex.message)
      elsif ex.kind_of? CommandNotFound then
        return JsonResponse.command_not_found(ex.message)
      end
      @log.error(ex)
      return JsonResponse.new("fail", ex.message, nil, 500)
    end
  end

end # Server
end # Bixby<commit_msg>log validation errors<commit_after>
require 'sinatra/base'

module Bixby
class Server < Sinatra::Base

  SHELL_EXEC = "shell_exec"

  DEFAULT_PORT = 18000

  class << self
    attr_accessor :agent
  end

  def initialize
    super
    @log = Logging.logger[self]
  end

  def agent
    self.class.agent
  end

  get '/*' do
    @log.debug { "Disposing of GET request: #{request.path}" }
    status 405
    return "GET requests are not allowed\n"
  end

  post '/*' do
    res = handle_request().to_json
    return encrypt(res)
  end

  # Encrypt the response
  #
  # @param [String] json      json response
  #
  # @return [String] base64 encrypted response when crypto is enabled,
  #                  otherwise, plain json
  def encrypt(json)
    agent.crypto_enabled? ? agent.encrypt_for_server(json) : json
  end

  def handle_request
    req = extract_valid_request()
    if req.kind_of? JsonResponse then
      @log.debug { "request extraction failed" }
      return req
    end

    return handle_exec(req)
  end

  def extract_valid_request
    body = request.body.read.strip
    if body.nil? or body.empty? then
      return JsonResponse.invalid_request
    end

    if agent.crypto_enabled? then
      body = agent.decrypt_from_server(body)
    end

    begin
      req = JsonRequest.from_json(body)
    rescue Exception
      return JsonResponse.invalid_request
    end

    if SHELL_EXEC != req.operation then
      return JsonResponse.invalid_request("unsupported operation: #{req.operation}")
    end

    return req
  end

  # Handle the exec request and return the response
  #
  # @return [JsonResponse]
  def handle_exec(req)
    begin
      cmd_res = agent.shell_exec(req.params)
      @log.debug { cmd_res.to_s + "\n---\n\n\n" }
      return cmd_res.to_json_response

    rescue Exception => ex
      if ex.kind_of? BundleNotFound then
        @log.debug(ex)
        return JsonResponse.bundle_not_found(ex.message)
      elsif ex.kind_of? CommandNotFound then
        @log.debug(ex)
        return JsonResponse.command_not_found(ex.message)
      end
      @log.error(ex)
      return JsonResponse.new("fail", ex.message, nil, 500)
    end
  end

end # Server
end # Bixby
{'fraction_non_alphanumeric': 0.08406466512702078, 'fraction_numerical': 0.006004618937644342, 'mean_word_length': 2.661031276415892, 'pattern_counts': {'":': 0, '<': 9, '<?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': '3363099', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1352, 'n_words': 365}