<commit_before>//! ----------------------------------------------------------------------------
//! Copyright Verizon.
//!
//! \file:    TODO
//! \details: TODO
//!
//! Licensed under the terms of the Apache 2.0 open source license.
//! Please refer to the LICENSE file in the project root for the terms.
//! ----------------------------------------------------------------------------
//! ----------------------------------------------------------------------------
//! includes
//! ----------------------------------------------------------------------------
#include "waflz/def.h"
#include "waflz/config_parser.h"
#include "waflz/engine.h"
#include "waflz/geoip2_mmdb.h"
#include "waflz/string_util.h"
#include "support/ndebug.h"
#include "op/regex.h"
#include "op/ac.h"
#include "op/nms.h"
#include "op/byte_range.h"
#include "core/macro.h"
#include "core/tx.h"
#include "core/var.h"
#include "core/op.h"
#include <libxml/parser.h>
namespace ns_waflz {
//! ----------------------------------------------------------------------------
//! \details TODO
//! \return  TODO
//! \param   TODO
//! ----------------------------------------------------------------------------
_compiled_config::~_compiled_config()
{
        // -------------------------------------------------
        // destruct m_regex_list
        // -------------------------------------------------
        for(regex_list_t::iterator i_p = m_regex_list.begin();
            i_p != m_regex_list.end();
            ++i_p)
        {
                if(*i_p) { delete *i_p; *i_p = NULL;}
        }
        // -------------------------------------------------
        // destruct m_nms_list
        // -------------------------------------------------
        for(nms_list_t::iterator i_n = m_nms_list.begin();
            i_n != m_nms_list.end();
            ++i_n)
        {
                if(*i_n) { delete *i_n; *i_n = NULL;}
        }
        // -------------------------------------------------
        // destruct m_ac_list
        // -------------------------------------------------
        for(ac_list_t::iterator i_p = m_ac_list.begin();
            i_p != m_ac_list.end();
            ++i_p)
        {
                if(*i_p) { delete *i_p; *i_p = NULL;}
        }
        // -------------------------------------------------
        // destruct m_byte_range_list
        // -------------------------------------------------
        for(byte_range_list_t::iterator i_p = m_byte_range_list.begin();
            i_p != m_byte_range_list.end();
            ++i_p)
        {
                if(*i_p) { delete *i_p; *i_p = NULL;}
        }
}
//! ----------------------------------------------------------------------------
//! \details TODO
//! \return  TODO
//! \param   TODO
//! ----------------------------------------------------------------------------
engine::engine():
        m_macro(NULL),
        m_config_list(),
        m_compiled_config_map(),
        m_ctype_parser_map(),
        m_ruleset_root_dir("/oc/local/waf/ruleset/"),
        m_geoip2_mmdb(),
        m_geoip2_db(),
        m_geoip2_isp_db(),
        m_err_msg()
{
}
//! ----------------------------------------------------------------------------
//! \details TODO
//! \return  TODO
//! \param   TODO
//! ----------------------------------------------------------------------------
engine::~engine()
{
        // -------------------------------------------------
        // destruct m_config_list
        // -------------------------------------------------
        for(config_list_t::iterator i_cfg = m_config_list.begin();
            i_cfg != m_config_list.end();
            ++i_cfg)
        {
                if(*i_cfg) { delete *i_cfg; *i_cfg = NULL;}
        }
        // -------------------------------------------------
        // destruct m_compiled_config_map
        // -------------------------------------------------
        for(compiled_config_map_t::iterator i_cfg = m_compiled_config_map.begin();
            i_cfg != m_compiled_config_map.end();
            ++i_cfg)
        {
                if(i_cfg->second) { delete i_cfg->second; i_cfg->second = NULL;}
        }
        // -------------------------------------------------
        // *************************************************
        //             xml initialization
        // *************************************************
        // -------------------------------------------------
        xmlCleanupParser();
        if(m_macro)
        {
                delete m_macro;
                m_macro = NULL;
        }
        // -------------------------------------------------
        // *************************************************
        //                  geoip2 dbs
        // *************************************************
        // -------------------------------------------------
        if(m_geoip2_mmdb)
        {
                delete m_geoip2_mmdb;
                m_geoip2_mmdb = NULL;
        }
}
//! ----------------------------------------------------------------------------
//! \details TODO
//! \return  TODO
//! \param   TODO
//! ----------------------------------------------------------------------------
int32_t engine::init()
{
        int32_t l_s;
        // -------------------------------------------------
        // 
        // -------------------------------------------------

        // -------------------------------------------------
        // macro...
        // -------------------------------------------------
        if(m_macro)
        {
                delete m_macro;
                m_macro = NULL;
        }
        m_macro = new macro();
        l_s = m_macro->init();
        if(l_s != WAFLZ_STATUS_OK)
        {
                return WAFLZ_STATUS_ERROR;
        }
        // -------------------------------------------------
        // init...
        // -------------------------------------------------
        init_tx_cb_vector();
        init_var_cb_vector();
        init_op_cb_vector();
        // -------------------------------------------------
        // *************************************************
        //         Content-Type --> parser map
        // *************************************************
        // -------------------------------------------------
        m_ctype_parser_map["application/x-www-form-urlencoded"] = PARSER_URL_ENCODED;
        m_ctype_parser_map["text/xml"]                          = PARSER_XML;
        m_ctype_parser_map["application/xml"]                   = PARSER_XML;
        m_ctype_parser_map["application/json"]                  = PARSER_JSON;
        m_ctype_parser_map["multipart/form-data"]               = PARSER_MULTIPART;
        // -------------------------------------------------
        // *************************************************
        //             xml initialization
        // *************************************************
        // -------------------------------------------------
        xmlInitParser();<commit_msg>setting libxml2 generic erorr function to do nothing to disable error output. (#215)<commit_after>//! ----------------------------------------------------------------------------
//! Copyright Verizon.
//!
//! \file:    TODO
//! \details: TODO
//!
//! Licensed under the terms of the Apache 2.0 open source license.
//! Please refer to the LICENSE file in the project root for the terms.
//! ----------------------------------------------------------------------------
//! ----------------------------------------------------------------------------
//! includes
//! ----------------------------------------------------------------------------
#include "waflz/def.h"
#include "waflz/config_parser.h"
#include "waflz/engine.h"
#include "waflz/geoip2_mmdb.h"
#include "waflz/string_util.h"
#include "support/ndebug.h"
#include "op/regex.h"
#include "op/ac.h"
#include "op/nms.h"
#include "op/byte_range.h"
#include "core/macro.h"
#include "core/tx.h"
#include "core/var.h"
#include "core/op.h"
#include <libxml/parser.h>
namespace ns_waflz {
//! ----------------------------------------------------------------------------
//! statics
//! ----------------------------------------------------------------------------
//! ----------------------------------------------------------------------------
//! \details define error function for libxml2 to "| > /dev/null" errors
//!          defined w/ xmlGenericErrorFunc function sig
//! \return  NA
//! ----------------------------------------------------------------------------
static void xml_error_func(void *ctx, const char *msg, ...)
{
}
//! ----------------------------------------------------------------------------
//! \details TODO
//! \return  TODO
//! \param   TODO
//! ----------------------------------------------------------------------------
_compiled_config::~_compiled_config()
{
        // -------------------------------------------------
        // destruct m_regex_list
        // -------------------------------------------------
        for(regex_list_t::iterator i_p = m_regex_list.begin();
            i_p != m_regex_list.end();
            ++i_p)
        {
                if(*i_p) { delete *i_p; *i_p = NULL;}
        }
        // -------------------------------------------------
        // destruct m_nms_list
        // -------------------------------------------------
        for(nms_list_t::iterator i_n = m_nms_list.begin();
            i_n != m_nms_list.end();
            ++i_n)
        {
                if(*i_n) { delete *i_n; *i_n = NULL;}
        }
        // -------------------------------------------------
        // destruct m_ac_list
        // -------------------------------------------------
        for(ac_list_t::iterator i_p = m_ac_list.begin();
            i_p != m_ac_list.end();
            ++i_p)
        {
                if(*i_p) { delete *i_p; *i_p = NULL;}
        }
        // -------------------------------------------------
        // destruct m_byte_range_list
        // -------------------------------------------------
        for(byte_range_list_t::iterator i_p = m_byte_range_list.begin();
            i_p != m_byte_range_list.end();
            ++i_p)
        {
                if(*i_p) { delete *i_p; *i_p = NULL;}
        }
}
//! ----------------------------------------------------------------------------
//! \details TODO
//! \return  TODO
//! \param   TODO
//! ----------------------------------------------------------------------------
engine::engine():
        m_macro(NULL),
        m_config_list(),
        m_compiled_config_map(),
        m_ctype_parser_map(),
        m_ruleset_root_dir("/oc/local/waf/ruleset/"),
        m_geoip2_mmdb(),
        m_geoip2_db(),
        m_geoip2_isp_db(),
        m_err_msg()
{
}
//! ----------------------------------------------------------------------------
//! \details TODO
//! \return  TODO
//! \param   TODO
//! ----------------------------------------------------------------------------
engine::~engine()
{
        // -------------------------------------------------
        // destruct m_config_list
        // -------------------------------------------------
        for(config_list_t::iterator i_cfg = m_config_list.begin();
            i_cfg != m_config_list.end();
            ++i_cfg)
        {
                if(*i_cfg) { delete *i_cfg; *i_cfg = NULL;}
        }
        // -------------------------------------------------
        // destruct m_compiled_config_map
        // -------------------------------------------------
        for(compiled_config_map_t::iterator i_cfg = m_compiled_config_map.begin();
            i_cfg != m_compiled_config_map.end();
            ++i_cfg)
        {
                if(i_cfg->second) { delete i_cfg->second; i_cfg->second = NULL;}
        }
        // -------------------------------------------------
        // *************************************************
        //             xml initialization
        // *************************************************
        // -------------------------------------------------
        xmlCleanupParser();
        if(m_macro)
        {
                delete m_macro;
                m_macro = NULL;
        }
        // -------------------------------------------------
        // *************************************************
        //                  geoip2 dbs
        // *************************************************
        // -------------------------------------------------
        if(m_geoip2_mmdb)
        {
                delete m_geoip2_mmdb;
                m_geoip2_mmdb = NULL;
        }
}
//! ----------------------------------------------------------------------------
//! \details TODO
//! \return  TODO
//! \param   TODO
//! ----------------------------------------------------------------------------
int32_t engine::init()
{
        int32_t l_s;
        // -------------------------------------------------
        // 
        // -------------------------------------------------

        // -------------------------------------------------
        // macro...
        // -------------------------------------------------
        if(m_macro)
        {
                delete m_macro;
                m_macro = NULL;
        }
        m_macro = new macro();
        l_s = m_macro->init();
        if(l_s != WAFLZ_STATUS_OK)
        {
                return WAFLZ_STATUS_ERROR;
        }
        // -------------------------------------------------
        // init...
        // -------------------------------------------------
        init_tx_cb_vector();
        init_var_cb_vector();
        init_op_cb_vector();
        // -------------------------------------------------
        // *************************************************
        //         Content-Type --> parser map
        // *************************************************
        // -------------------------------------------------
        m_ctype_parser_map["application/x-www-form-urlencoded"] = PARSER_URL_ENCODED;
        m_ctype_parser_map["text/xml"]                          = PARSER_XML;
        m_ctype_parser_map["application/xml"]                   = PARSER_XML;
        m_ctype_parser_map["application/json"]                  = PARSER_JSON;
        m_ctype_parser_map["multipart/form-data"]               = PARSER_MULTIPART;
        // -------------------------------------------------
        // *************************************************
        //             xml initialization
        // *************************************************
        // -------------------------------------------------
        xmlGenericErrorFunc l_err_h = (xmlGenericErrorFunc)xml_error_func;
        initGenericErrorDefaultFunc(&l_err_h);
        xmlInitParser();
{'fraction_non_alphanumeric': 0.45890086060852475, 'fraction_numerical': 0.002236226875381175, 'mean_word_length': 0.6998387468325271, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 21}, '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': '6393562', 'n_tokens_mistral': 3405, 'n_tokens_neox': 3239, 'n_words': 503}