<commit_before>/*
*  Part of regex_ban plugin: is the abstract class that banjax is inherited from
*
*  Vmon: May 2013: Initial version.
*/
#ifndef BANJAX_FILTER_H
#define BANJAX_FILTER_H

#include <assert.h>
#include <yaml-cpp/yaml.h>
#include <functional>

#include "transaction_muncher.h"
#include "filter_list.h"
#include "ip_db.h"

class FilterResponse;

using ResponseGenerator = std::function<std::string(const TransactionParts&, const FilterResponse&)>;

/** this is mainly created due to disablibity of yaml
    to merge nodes of the same name but also to manage
    priority
  */
class FilterConfig {
public:
  std::list<YAML::const_iterator> config_node_list;
  int priority;

  FilterConfig() : priority(0) {}
};

/**
   this is the standard extended response that the event handler expect to
   process.
 */
class FilterExtendedResponse
{
public:
  ResponseGenerator response_generator;
  char* content_type_;
  bool banned_ip;
  std::string set_cookie_header;
  int response_code;

  // Returns the set content type. Transfers
  // ownership to the caller.
  char* get_and_release_content_type() {
    char * ct = content_type_;
    content_type_ = nullptr;
    return ct;
  }

  void set_content_type(const char* x) {
    if (content_type_) {
      TSfree(content_type_);
      content_type_ = nullptr;
    }
    if (x) {
      content_type_ = TSstrdup(x);
    }
  }

  /**
     A constructor that optionally set the response_generator on creation
   */
  FilterExtendedResponse(ResponseGenerator requested_response_generator = nullptr) :
    response_generator(requested_response_generator),
    content_type_(nullptr),
    banned_ip(false),
    response_code(403)
  {}

  virtual ~FilterExtendedResponse() {}
};

/** we want to keep the FilterResponse struture as light as possible.
    because lots of filters response with only GO_AHEAD_NO_COMMENT and
    we want to have that transmitted as fast as possible.

    if a filter needs more space for its communication they can define
    a class and put the pointer to its object into response_data.
*/
class FilterResponse final
{
public:
   enum ResponseType {
        GO_AHEAD_NO_COMMENT,
        I_RESPOND,
        SERVE_IMMIDIATELY_DONT_CACHE,
        SERVE_FRESH,
   };

  ResponseType response_type;
  std::shared_ptr<FilterExtendedResponse> response_data;

  FilterResponse(ResponseType cur_response_type = GO_AHEAD_NO_COMMENT, FilterExtendedResponse* cur_response_data = nullptr) :
    response_type(cur_response_type),
    response_data(cur_response_data)
  {}<commit_msg>Make sure content_type_ is freed<commit_after>/*
*  Part of regex_ban plugin: is the abstract class that banjax is inherited from
*
*  Vmon: May 2013: Initial version.
*/
#ifndef BANJAX_FILTER_H
#define BANJAX_FILTER_H

#include <assert.h>
#include <yaml-cpp/yaml.h>
#include <functional>

#include "transaction_muncher.h"
#include "filter_list.h"
#include "ip_db.h"

class FilterResponse;

using ResponseGenerator = std::function<std::string(const TransactionParts&, const FilterResponse&)>;

/** this is mainly created due to disablibity of yaml
    to merge nodes of the same name but also to manage
    priority
  */
class FilterConfig {
public:
  std::list<YAML::const_iterator> config_node_list;
  int priority;

  FilterConfig() : priority(0) {}
};

/**
   this is the standard extended response that the event handler expect to
   process.
 */
class FilterExtendedResponse
{
public:
  ResponseGenerator response_generator;
  char* content_type_;
  bool banned_ip;
  std::string set_cookie_header;
  int response_code;

  // Returns the set content type. Transfers
  // ownership to the caller.
  char* get_and_release_content_type() {
    char * ct = content_type_;
    content_type_ = nullptr;
    return ct;
  }

  void set_content_type(const char* x) {
    if (content_type_) {
      TSfree(content_type_);
      content_type_ = nullptr;
    }
    if (x) {
      content_type_ = TSstrdup(x);
    }
  }

  /**
     A constructor that optionally set the response_generator on creation
   */
  FilterExtendedResponse(ResponseGenerator requested_response_generator = nullptr) :
    response_generator(requested_response_generator),
    content_type_(nullptr),
    banned_ip(false),
    response_code(403)
  {}

  virtual ~FilterExtendedResponse() {
    if (content_type_) {
      // This shouldn't happen as content_type_ should have been released by
      // the time this destructor is run, but let's just make sure.
      TSfree(content_type_);
    }
  }
};

/** we want to keep the FilterResponse struture as light as possible.
    because lots of filters response with only GO_AHEAD_NO_COMMENT and
    we want to have that transmitted as fast as possible.

    if a filter needs more space for its communication they can define
    a class and put the pointer to its object into response_data.
*/
class FilterResponse final
{
public:
   enum ResponseType {
        GO_AHEAD_NO_COMMENT,
        I_RESPOND,
        SERVE_IMMIDIATELY_DONT_CACHE,
        SERVE_FRESH,
   };

  ResponseType response_type;
  std::shared_ptr<FilterExtendedResponse> response_data;

  FilterResponse(ResponseType cur_response_type = GO_AHEAD_NO_COMMENT, FilterExtendedResponse* cur_response_data = nullptr) :
    response_type(cur_response_type),
    response_data(cur_response_data)
  {}
{'fraction_non_alphanumeric': 0.07793427230046948, 'fraction_numerical': 0.003004694835680751, 'mean_word_length': 3.9360518999073215, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '1833283', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1592, 'n_words': 476}