<commit_before>/*****************************************************************************
 * Licensed to Qualys, Inc. (QUALYS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * QUALYS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include <ironbee/core.h>
#include <ironbee/cfgmap.h>
#include <ironbee/debug.h>
#include <ironbee/engine.h>
#include <ironbee/lock.h>
#include <ironbee/module.h>
#include <ironbee/provider.h>
#include <ironbee/util.h>
#include <ironbee/list.h>
#include <ironbee/config.h>
#include <ironbee/mpool.h>
#include <ironbee/rule_engine.h>
#include <ironbee/operator.h>
#include <ironbee/action.h>

#include "rules_lua.h"
#include "lua/ironbee.h"

#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <ctype.h>

#include <lua.h>

/* Define the module name as well as a string version of it. */
#define MODULE_NAME        rules
#define MODULE_NAME_STR    IB_XSTRINGIFY(MODULE_NAME)

/* Declare the public module symbol. */
IB_MODULE_DECLARE();

/**
 * Phase lookup table.
 */
typedef struct {
    const char       *str;
    ib_rule_phase_t   phase;
} phase_lookup_t;
static phase_lookup_t phase_lookup_table[] = {
    { "REQUEST_HEADER",  PHASE_REQUEST_HEADER },
    { "REQUEST",         PHASE_REQUEST_BODY },
    { "RESPONSE_HEADER", PHASE_RESPONSE_HEADER },
    { "RESPONSE",        PHASE_RESPONSE_BODY },
    { "POSTPROCESS",     PHASE_POSTPROCESS },
    /* Shortcuts */
    { "REQHDR",          PHASE_REQUEST_HEADER },
    { "RSPHDR",          PHASE_RESPONSE_HEADER },
    /* List terminator */
    { "NONE",            PHASE_NONE },
    { NULL,              PHASE_INVALID },
};

/**
 * Stream lookup table.
 */
typedef struct {
    const char       *str;
    ib_rule_stream_t  stream;
} stream_lookup_t;
static stream_lookup_t stream_lookup_table[] = {
    { "REQUEST_HEADER_STREAM",  STREAM_REQUEST_HEADER },
    { "REQUEST_BODY_STREAM",    STREAM_REQUEST_BODY },
    { "RESPONSE_HEADER_STREAM", STREAM_RESPONSE_HEADER },
    { "RESPONSE_BODY_STREAM",   STREAM_RESPONSE_BODY },
    /* Shortcuts */
    { "STRREQHDR",              STREAM_REQUEST_HEADER },
    { "STRREQBODY",             STREAM_REQUEST_BODY },
    { "STRRSPHDR",              STREAM_RESPONSE_HEADER },
    { "STRRSPBODY",             STREAM_RESPONSE_BODY }, 
    /* List terminator */
    { NULL,                     STREAM_INVALID },
};

/**
 * Ironbee's root rule state.
 */
static lua_State *g_ironbee_rules_lua;

/**
 * @brief Semaphore ID used to protect Lua thread creation and destruction.
 */
static ib_lock_t g_lua_lock;


/**
 * Lookup a phase name in the phase name table.
 * @internal
 *
 * @param[in] str Phase name string to lookup
 * @param[out] phase Phase number
 *
 * @returns Status code
 */
static ib_status_t lookup_phase(const char *str, ib_rule_phase_t *phase)
{
    IB_FTRACE_INIT();<commit_msg>rules.c: Remove trailing whitespace.<commit_after>/*****************************************************************************
 * Licensed to Qualys, Inc. (QUALYS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * QUALYS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include <ironbee/core.h>
#include <ironbee/cfgmap.h>
#include <ironbee/debug.h>
#include <ironbee/engine.h>
#include <ironbee/lock.h>
#include <ironbee/module.h>
#include <ironbee/provider.h>
#include <ironbee/util.h>
#include <ironbee/list.h>
#include <ironbee/config.h>
#include <ironbee/mpool.h>
#include <ironbee/rule_engine.h>
#include <ironbee/operator.h>
#include <ironbee/action.h>

#include "rules_lua.h"
#include "lua/ironbee.h"

#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <ctype.h>

#include <lua.h>

/* Define the module name as well as a string version of it. */
#define MODULE_NAME        rules
#define MODULE_NAME_STR    IB_XSTRINGIFY(MODULE_NAME)

/* Declare the public module symbol. */
IB_MODULE_DECLARE();

/**
 * Phase lookup table.
 */
typedef struct {
    const char       *str;
    ib_rule_phase_t   phase;
} phase_lookup_t;
static phase_lookup_t phase_lookup_table[] = {
    { "REQUEST_HEADER",  PHASE_REQUEST_HEADER },
    { "REQUEST",         PHASE_REQUEST_BODY },
    { "RESPONSE_HEADER", PHASE_RESPONSE_HEADER },
    { "RESPONSE",        PHASE_RESPONSE_BODY },
    { "POSTPROCESS",     PHASE_POSTPROCESS },
    /* Shortcuts */
    { "REQHDR",          PHASE_REQUEST_HEADER },
    { "RSPHDR",          PHASE_RESPONSE_HEADER },
    /* List terminator */
    { "NONE",            PHASE_NONE },
    { NULL,              PHASE_INVALID },
};

/**
 * Stream lookup table.
 */
typedef struct {
    const char       *str;
    ib_rule_stream_t  stream;
} stream_lookup_t;
static stream_lookup_t stream_lookup_table[] = {
    { "REQUEST_HEADER_STREAM",  STREAM_REQUEST_HEADER },
    { "REQUEST_BODY_STREAM",    STREAM_REQUEST_BODY },
    { "RESPONSE_HEADER_STREAM", STREAM_RESPONSE_HEADER },
    { "RESPONSE_BODY_STREAM",   STREAM_RESPONSE_BODY },
    /* Shortcuts */
    { "STRREQHDR",              STREAM_REQUEST_HEADER },
    { "STRREQBODY",             STREAM_REQUEST_BODY },
    { "STRRSPHDR",              STREAM_RESPONSE_HEADER },
    { "STRRSPBODY",             STREAM_RESPONSE_BODY },
    /* List terminator */
    { NULL,                     STREAM_INVALID },
};

/**
 * Ironbee's root rule state.
 */
static lua_State *g_ironbee_rules_lua;

/**
 * @brief Semaphore ID used to protect Lua thread creation and destruction.
 */
static ib_lock_t g_lua_lock;


/**
 * Lookup a phase name in the phase name table.
 * @internal
 *
 * @param[in] str Phase name string to lookup
 * @param[out] phase Phase number
 *
 * @returns Status code
 */
static ib_status_t lookup_phase(const char *str, ib_rule_phase_t *phase)
{
    IB_FTRACE_INIT();
{'fraction_non_alphanumeric': 0.150767987065481, 'fraction_numerical': 0.0010778765831312314, 'mean_word_length': 3.5069823922282937, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4284497', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2329, 'n_words': 628}