Add convenience functions for common tasks with regular expressions

Several filters use a match-or-ignore logic with two regular
expressions when filtering queries. This commit adds a convenience
function for this task. Also adds a convenience function for reading
several regular expression parameters at once, compiling them and
saving the code while checking for errors.

Also, use the new functions in QLA and CCR filters.
This commit is contained in:
Esa Korhonen
2017-06-20 17:00:35 +03:00
parent 5fd690eb1f
commit 14bb6cf99b
6 changed files with 187 additions and 111 deletions

View File

@ -369,6 +369,28 @@ pcre2_code* config_get_compiled_regex(const MXS_CONFIG_PARAMETER *params,
const char *key, uint32_t options,
uint32_t* output_ovec_size);
/**
* Get multiple compiled regular expressions and the maximum ovector size of
* the patterns. The returned regex codes should be freed by the caller.
*
* @param params List of configuration parameters
* @param keys An array of parameter names. If an element is not found in @c params,
* the corresponding spot in @c out_codes is set to NULL.
* @param keys_size Size of both @c keys and @c out_arr
* @param options PCRE2 compilation options
* @param out_ovec_size If not NULL, the maximum ovector size of successfully
* compiled patterns is written here.
* @param out_codes An array of handles to compiled codes. The referenced pointers
* will be set to point to the compiled codes. The array size must be equal to
* @c keys array size and it must contain valid pointers.
*
* @return True, if all patterns given by @c keys were successfully compiled.
* False otherwise.
*/
bool config_get_compiled_regexes(const MXS_CONFIG_PARAMETER *params,
const char* keys[], int keys_size,
uint32_t options, uint32_t* out_ovec_size,
pcre2_code** out_codes[]);
/**
* Parse a list of server names and write the results in an array of strings
* with one server name in each. The output array and its elements should be

View File

@ -55,4 +55,26 @@ mxs_pcre2_result_t mxs_pcre2_simple_match(const char* pattern, const char* subje
void mxs_pcre2_print_error(int errorcode, const char *module_name, const char *filename,
int line_num, const char* func_name);
/**
* Check that @c subject is valid. A valid subject matches @c re_match yet does
* not match @c re_exclude. If an error occurs, an error code is written to
* @c match_error_out.
*
* @param re_match If not NULL, the subject must match this to be valid. If NULL,
* all inputs are considered valid.
* @param re_exclude If not NULL, will invalidate a matching subject. Even subjects
* validated by @c re_match can be invalidated. If NULL, invalidates nothing.
* @param md PCRE2 match data block
* @param subject Subject string. Should NOT be an empty string.
* @param length Length of subject. Can be zero for 0-terminated strings.
* @param calling_module Which module the function was called from. Can be NULL.
* Used for log messages.
*
* @return True, if subject is considered valid. False if subject is not valid or
* an error occurred.
*/
bool mxs_pcre2_check_match_exclude(pcre2_code* re_match, pcre2_code* re_exclude,
pcre2_match_data* md, const char* subject,
int length, const char* calling_module);
MXS_END_DECLS