Blame include/ap_expr.h

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * @file ap_expr.h
Packit 90a5c9
 * @brief Expression parser
Packit 90a5c9
 *
Packit 90a5c9
 * @defgroup AP_EXPR Expression parser
Packit 90a5c9
 * @ingroup  APACHE_CORE
Packit 90a5c9
 * @{
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#ifndef AP_EXPR_H
Packit 90a5c9
#define AP_EXPR_H
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "ap_regex.h"
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
extern "C" {
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
/** A node in the expression parse tree */
Packit 90a5c9
typedef struct ap_expr_node ap_expr_t;
Packit 90a5c9
Packit 90a5c9
/** Struct describing a parsed expression */
Packit 90a5c9
typedef struct {
Packit 90a5c9
    /** The root of the actual expression parse tree */
Packit 90a5c9
    ap_expr_t *root_node;
Packit 90a5c9
    /** The filename where the expression has been defined (for logging).
Packit 90a5c9
     *  May be NULL
Packit 90a5c9
     */
Packit 90a5c9
    const char *filename;
Packit 90a5c9
    /** The line number where the expression has been defined (for logging). */
Packit 90a5c9
    unsigned int line_number;
Packit 90a5c9
    /** Flags relevant for the expression, see AP_EXPR_FLAG_* */
Packit 90a5c9
    unsigned int flags;
Packit 90a5c9
    /** The module that is used for loglevel configuration */
Packit 90a5c9
    int module_index;
Packit 90a5c9
} ap_expr_info_t;
Packit 90a5c9
Packit 90a5c9
/** Use ssl_expr compatibility mode (changes the meaning of the comparison
Packit 90a5c9
 * operators)
Packit 90a5c9
 */
Packit 90a5c9
#define AP_EXPR_FLAG_SSL_EXPR_COMPAT       1
Packit 90a5c9
/** Don't add siginificant request headers to the Vary response header */
Packit 90a5c9
#define AP_EXPR_FLAG_DONT_VARY             2
Packit 90a5c9
/** Don't allow functions/vars that bypass the current request's access
Packit 90a5c9
 *  restrictions or would otherwise leak confidential information.
Packit 90a5c9
 *  Used by e.g. mod_include.
Packit 90a5c9
 */
Packit 90a5c9
#define AP_EXPR_FLAG_RESTRICTED            4
Packit 90a5c9
/** Expression evaluates to a string, not to a bool */
Packit 90a5c9
#define AP_EXPR_FLAG_STRING_RESULT         8
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Evaluate a parse tree, simple interface
Packit 90a5c9
 * @param r The current request
Packit 90a5c9
 * @param expr The expression to be evaluated
Packit 90a5c9
 * @param err Where an error message should be stored
Packit 90a5c9
 * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
Packit 90a5c9
 * @note err will be set to NULL on success, or to an error message on error
Packit 90a5c9
 * @note request headers used during evaluation will be added to the Vary:
Packit 90a5c9
 *       response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *expr,
Packit 90a5c9
                             const char **err);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Evaluate a parse tree, with access to regexp backreference
Packit 90a5c9
 * @param r The current request
Packit 90a5c9
 * @param expr The expression to be evaluated
Packit 90a5c9
 * @param nmatch size of the regex match vector pmatch
Packit 90a5c9
 * @param pmatch information about regex matches
Packit 90a5c9
 * @param source the string that pmatch applies to
Packit 90a5c9
 * @param err Where an error message should be stored
Packit 90a5c9
 * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
Packit 90a5c9
 * @note err will be set to NULL on success, or to an error message on error
Packit 90a5c9
 * @note nmatch/pmatch/source can be used both to make previous matches
Packit 90a5c9
 *       available to ap_expr_exec_re and to use ap_expr_exec_re's matches
Packit 90a5c9
 *       later on.
Packit 90a5c9
 * @note request headers used during evaluation will be added to the Vary:
Packit 90a5c9
 *       response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_expr_exec_re(request_rec *r, const ap_expr_info_t *expr,
Packit 90a5c9
                                apr_size_t nmatch, ap_regmatch_t *pmatch,
Packit 90a5c9
                                const char **source, const char **err);
Packit 90a5c9
Packit 90a5c9
/** Context used during evaluation of a parse tree, created by ap_expr_exec */
Packit 90a5c9
typedef struct {
Packit 90a5c9
    /** the current request */
Packit 90a5c9
    request_rec *r;
Packit 90a5c9
    /** the current connection */
Packit 90a5c9
    conn_rec *c;
Packit 90a5c9
    /** the current virtual host */
Packit 90a5c9
    server_rec *s;
Packit 90a5c9
    /** the pool to use */
Packit 90a5c9
    apr_pool_t *p;
Packit 90a5c9
    /** where to store the error string */
Packit 90a5c9
    const char **err;
Packit 90a5c9
    /** ap_expr_info_t for the expression */
Packit 90a5c9
    const ap_expr_info_t *info;
Packit 90a5c9
    /** regex match information for back references */
Packit 90a5c9
    ap_regmatch_t *re_pmatch;
Packit 90a5c9
    /** size of the vector pointed to by re_pmatch */
Packit 90a5c9
    apr_size_t re_nmatch;
Packit 90a5c9
    /** the string corresponding to the re_pmatch */
Packit 90a5c9
    const char **re_source;
Packit 90a5c9
    /** A string where the comma separated names of headers are stored
Packit 90a5c9
     * to be later added to the Vary: header. If NULL, the caller is not
Packit 90a5c9
     * interested in this information.
Packit 90a5c9
     */
Packit 90a5c9
    const char **vary_this;
Packit 90a5c9
    /** where to store the result string */
Packit 90a5c9
    const char **result_string;
Packit 90a5c9
    /** Arbitrary context data provided by the caller for custom functions */
Packit 90a5c9
    void *data;
Packit 90a5c9
    /** The current recursion level */
Packit 90a5c9
    int reclvl;
Packit 90a5c9
} ap_expr_eval_ctx_t;
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Evaluate a parse tree, full featured version
Packit 90a5c9
 * @param ctx The evaluation context with all data filled in
Packit 90a5c9
 * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
Packit 90a5c9
 * @note *ctx->err will be set to NULL on success, or to an error message on
Packit 90a5c9
 *       error
Packit 90a5c9
 * @note request headers used during evaluation will be added to the Vary:
Packit 90a5c9
 *       response header if ctx->vary_this is set.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_expr_exec_ctx(ap_expr_eval_ctx_t *ctx);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Evaluate a parse tree of a string valued expression
Packit 90a5c9
 * @param r The current request
Packit 90a5c9
 * @param expr The expression to be evaluated
Packit 90a5c9
 * @param err Where an error message should be stored
Packit 90a5c9
 * @return The result string, NULL on error
Packit 90a5c9
 * @note err will be set to NULL on success, or to an error message on error
Packit 90a5c9
 * @note request headers used during evaluation will be added to the Vary:
Packit 90a5c9
 *       response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r,
Packit 90a5c9
                                          const ap_expr_info_t *expr,
Packit 90a5c9
                                          const char **err);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Evaluate a parse tree of a string valued expression
Packit 90a5c9
 * @param r The current request
Packit 90a5c9
 * @param expr The expression to be evaluated
Packit 90a5c9
 * @param nmatch size of the regex match vector pmatch
Packit 90a5c9
 * @param pmatch information about regex matches
Packit 90a5c9
 * @param source the string that pmatch applies to
Packit 90a5c9
 * @param err Where an error message should be stored
Packit 90a5c9
 * @return The result string, NULL on error
Packit 90a5c9
 * @note err will be set to NULL on success, or to an error message on error
Packit 90a5c9
 * @note nmatch/pmatch/source can be used both to make previous matches
Packit 90a5c9
 *       available to ap_expr_exec_re and to use ap_expr_exec_re's matches
Packit 90a5c9
 *       later on.
Packit 90a5c9
 * @note request headers used during evaluation will be added to the Vary:
Packit 90a5c9
 *       response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
Packit 90a5c9
                                             const ap_expr_info_t *expr,
Packit 90a5c9
                                             apr_size_t nmatch,
Packit 90a5c9
                                             ap_regmatch_t *pmatch,
Packit 90a5c9
                                             const char **source,
Packit 90a5c9
                                             const char **err);
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * The parser can be extended with variable lookup, functions, and
Packit 90a5c9
 * and operators.
Packit 90a5c9
 *
Packit 90a5c9
 * During parsing, the parser calls the lookup function to resolve a
Packit 90a5c9
 * name into a function pointer and an opaque context for the function.
Packit 90a5c9
 * If the argument to a function or operator is constant, the lookup function
Packit 90a5c9
 * may also parse that argument and store the parsed data in the context.
Packit 90a5c9
 *
Packit 90a5c9
 * The default lookup function is the hook ::ap_expr_lookup_default which just
Packit 90a5c9
 * calls ap_run_expr_lookup. Modules can use it to make functions and
Packit 90a5c9
 * variables generally available.
Packit 90a5c9
 *
Packit 90a5c9
 * An ap_expr consumer can also provide its own custom lookup function to
Packit 90a5c9
 * modify the set of variables and functions that are available. The custom
Packit 90a5c9
 * lookup function can in turn call 'ap_run_expr_lookup'.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/** Unary operator, takes one string argument and returns a bool value.
Packit 90a5c9
 * The name must have the form '-z' (one letter only).
Packit 90a5c9
 * @param ctx The evaluation context
Packit 90a5c9
 * @param data An opaque context provided by the lookup hook function
Packit 90a5c9
 * @param arg The (right) operand
Packit 90a5c9
 * @return 0 or 1
Packit 90a5c9
 */
Packit 90a5c9
typedef int ap_expr_op_unary_t(ap_expr_eval_ctx_t *ctx, const void *data,
Packit 90a5c9
                               const char *arg);
Packit 90a5c9
Packit 90a5c9
/** Binary operator, takes two string arguments and returns a bool value.
Packit 90a5c9
 * The name must have the form '-cmp' (at least two letters).
Packit 90a5c9
 * @param ctx The evaluation context
Packit 90a5c9
 * @param data An opaque context provided by the lookup hook function
Packit 90a5c9
 * @param arg1 The left operand
Packit 90a5c9
 * @param arg2 The right operand
Packit 90a5c9
 * @return 0 or 1
Packit 90a5c9
 */
Packit 90a5c9
typedef int ap_expr_op_binary_t(ap_expr_eval_ctx_t *ctx, const void *data,
Packit 90a5c9
                                const char *arg1, const char *arg2);
Packit 90a5c9
Packit 90a5c9
/** String valued function, takes a string argument and returns a string
Packit 90a5c9
 * @param ctx The evaluation context
Packit 90a5c9
 * @param data An opaque context provided by the lookup hook function
Packit 90a5c9
 * @param arg The argument
Packit 90a5c9
 * @return The functions result string, may be NULL for 'empty string'
Packit 90a5c9
 */
Packit 90a5c9
typedef const char *(ap_expr_string_func_t)(ap_expr_eval_ctx_t *ctx,
Packit 90a5c9
                                            const void *data,
Packit 90a5c9
                                            const char *arg);
Packit 90a5c9
Packit 90a5c9
/** List valued function, takes a string argument and returns a list of strings
Packit 90a5c9
 * Can currently only be called following the builtin '-in' operator.
Packit 90a5c9
 * @param ctx The evaluation context
Packit 90a5c9
 * @param data An opaque context provided by the lookup hook function
Packit 90a5c9
 * @param arg The argument
Packit 90a5c9
 * @return The functions result list of strings, may be NULL for 'empty array'
Packit 90a5c9
 */
Packit 90a5c9
typedef apr_array_header_t *(ap_expr_list_func_t)(ap_expr_eval_ctx_t *ctx,
Packit 90a5c9
                                                  const void *data,
Packit 90a5c9
                                                  const char *arg);
Packit 90a5c9
Packit 90a5c9
/** Variable lookup function, takes no argument and returns a string
Packit 90a5c9
 * @param ctx The evaluation context
Packit 90a5c9
 * @param data An opaque context provided by the lookup hook function
Packit 90a5c9
 * @return The expanded variable
Packit 90a5c9
 */
Packit 90a5c9
typedef const char *(ap_expr_var_func_t)(ap_expr_eval_ctx_t *ctx,
Packit 90a5c9
                                         const void *data);
Packit 90a5c9
Packit 90a5c9
/** parameter struct passed to the lookup hook functions */
Packit 90a5c9
typedef struct {
Packit 90a5c9
    /** type of the looked up object */
Packit 90a5c9
    int type;
Packit 90a5c9
#define AP_EXPR_FUNC_VAR        0
Packit 90a5c9
#define AP_EXPR_FUNC_STRING     1
Packit 90a5c9
#define AP_EXPR_FUNC_LIST       2
Packit 90a5c9
#define AP_EXPR_FUNC_OP_UNARY   3
Packit 90a5c9
#define AP_EXPR_FUNC_OP_BINARY  4
Packit 90a5c9
    /** name of the looked up object */
Packit 90a5c9
    const char *name;
Packit 90a5c9
Packit 90a5c9
    int flags;
Packit 90a5c9
Packit 90a5c9
    apr_pool_t *pool;
Packit 90a5c9
    apr_pool_t *ptemp;
Packit 90a5c9
Packit 90a5c9
    /** where to store the function pointer */
Packit 90a5c9
    const void **func;
Packit 90a5c9
    /** where to store the function's context */
Packit 90a5c9
    const void **data;
Packit 90a5c9
    /** where to store the error message (if any) */
Packit 90a5c9
    const char **err;
Packit 90a5c9
Packit 90a5c9
    /** arg for pre-parsing (only if a simple string).
Packit 90a5c9
     *  For binary ops, this is the right argument. */
Packit 90a5c9
    const char *arg;
Packit 90a5c9
} ap_expr_lookup_parms;
Packit 90a5c9
Packit 90a5c9
/** Function for looking up the provider function for a variable, operator
Packit 90a5c9
 *  or function in an expression.
Packit 90a5c9
 *  @param parms The parameter struct, also determins where the result is
Packit 90a5c9
 *               stored.
Packit 90a5c9
 *  @return OK on success,
Packit 90a5c9
 *          !OK on failure,
Packit 90a5c9
 *          DECLINED if the requested name is not handled by this function
Packit 90a5c9
 */
Packit 90a5c9
typedef int (ap_expr_lookup_fn_t)(ap_expr_lookup_parms *parms);
Packit 90a5c9
Packit 90a5c9
/** Default lookup function which just calls ap_run_expr_lookup().
Packit 90a5c9
 *  ap_run_expr_lookup cannot be used directly because it has the wrong
Packit 90a5c9
 *  calling convention under Windows.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE_NONSTD(int) ap_expr_lookup_default(ap_expr_lookup_parms *parms);
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_HOOK(int, expr_lookup, (ap_expr_lookup_parms *parms))
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Parse an expression into a parse tree
Packit 90a5c9
 * @param pool Pool
Packit 90a5c9
 * @param ptemp temp pool
Packit 90a5c9
 * @param info The ap_expr_info_t struct (with values filled in)
Packit 90a5c9
 * @param expr The expression string to parse
Packit 90a5c9
 * @param lookup_fn The lookup function to use, NULL for default
Packit 90a5c9
 * @return NULL on success, error message on error.
Packit 90a5c9
 *         A pointer to the resulting parse tree will be stored in
Packit 90a5c9
 *         info->root_node.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(const char *) ap_expr_parse(apr_pool_t *pool, apr_pool_t *ptemp,
Packit 90a5c9
                                       ap_expr_info_t *info, const char *expr,
Packit 90a5c9
                                       ap_expr_lookup_fn_t *lookup_fn);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * High level interface to ap_expr_parse that also creates ap_expr_info_t and
Packit 90a5c9
 * uses info from cmd_parms to fill in most of it.
Packit 90a5c9
 * @param cmd The cmd_parms struct
Packit 90a5c9
 * @param expr The expression string to parse
Packit 90a5c9
 * @param flags The flags to use, see AP_EXPR_FLAG_*
Packit 90a5c9
 * @param err Set to NULL on success, error message on error
Packit 90a5c9
 * @param lookup_fn The lookup function used to lookup vars, functions, and
Packit 90a5c9
 *        operators
Packit 90a5c9
 * @param module_index The module_index to set for the expression
Packit 90a5c9
 * @return The parsed expression
Packit 90a5c9
 * @note Usually ap_expr_parse_cmd() should be used
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(ap_expr_info_t *) ap_expr_parse_cmd_mi(const cmd_parms *cmd,
Packit 90a5c9
                                                  const char *expr,
Packit 90a5c9
                                                  unsigned int flags,
Packit 90a5c9
                                                  const char **err,
Packit 90a5c9
                                                  ap_expr_lookup_fn_t *lookup_fn,
Packit 90a5c9
                                                  int module_index);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Convenience wrapper for ap_expr_parse_cmd_mi() that sets
Packit 90a5c9
 * module_index = APLOG_MODULE_INDEX
Packit 90a5c9
 */
Packit 90a5c9
#define ap_expr_parse_cmd(cmd, expr, flags, err, lookup_fn) \
Packit 90a5c9
        ap_expr_parse_cmd_mi(cmd, expr, flags, err, lookup_fn, APLOG_MODULE_INDEX)
Packit 90a5c9
Packit 90a5c9
 /**
Packit 90a5c9
  * Internal initialisation of ap_expr (for httpd internal use)
Packit 90a5c9
  */
Packit 90a5c9
void ap_expr_init(apr_pool_t *pool);
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
}
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#endif /* AP_EXPR_H */
Packit 90a5c9
/** @} */