1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @brief Expression parser
21 * @defgroup AP_EXPR ap_expr
22 * @ingroup APACHE_CORE
30 #include "http_config.h"
37 /** A node in the expression parse tree */
38 typedef struct ap_expr_node ap_expr_t;
40 /** Struct describing a parsed expression */
42 /** The root of the actual expression parse tree */
44 /** The filename where the expression has been defined (for logging).
48 /** The line number where the expression has been defined (for logging). */
49 unsigned int line_number;
50 /** Flags relevant for the expression, see AP_EXPR_FLAG_* */
52 /** The module that is used for loglevel configuration */
56 /** Use ssl_expr compatibility mode (changes the meaning of the comparison
59 #define AP_EXPR_FLAG_SSL_EXPR_COMPAT 1
60 /** Don't add siginificant request headers to the Vary response header */
61 #define AP_EXPR_FLAG_DONT_VARY 2
62 /** Don't allow functions/vars that bypass the current request's access
63 * restrictions or would otherwise leak confidential information.
64 * Used by e.g. mod_include.
66 #define AP_EXPR_FLAG_RESTRICTED 4
67 /** Expression evaluates to a string, not to a bool */
68 #define AP_EXPR_FLAG_STRING_RESULT 8
72 * Evaluate a parse tree, simple interface
73 * @param r The current request
74 * @param expr The expression to be evaluated
75 * @param err Where an error message should be stored
76 * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
77 * @note err will be set to NULL on success, or to an error message on error
78 * @note request headers used during evaluation will be added to the Vary:
79 * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
81 AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *expr,
85 * Evaluate a parse tree, with access to regexp backreference
86 * @param r The current request
87 * @param expr The expression to be evaluated
88 * @param nmatch size of the regex match vector pmatch
89 * @param pmatch information about regex matches
90 * @param source the string that pmatch applies to
91 * @param err Where an error message should be stored
92 * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
93 * @note err will be set to NULL on success, or to an error message on error
94 * @note nmatch/pmatch/source can be used both to make previous matches
95 * available to ap_expr_exec_re and to use ap_expr_exec_re's matches
97 * @note request headers used during evaluation will be added to the Vary:
98 * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
100 AP_DECLARE(int) ap_expr_exec_re(request_rec *r, const ap_expr_info_t *expr,
101 apr_size_t nmatch, ap_regmatch_t *pmatch,
102 const char **source, const char **err);
104 /** Context used during evaluation of a parse tree, created by ap_expr_exec */
106 /** the current request */
108 /** the current connection */
110 /** the current connection */
112 /** the pool to use */
114 /** where to store the error string */
116 /** ap_expr_info_t for the expression */
117 const ap_expr_info_t *info;
118 /** regex match information for back references */
119 ap_regmatch_t *re_pmatch;
120 /** size of the vector pointed to by re_pmatch */
121 apr_size_t re_nmatch;
122 /** the string corresponding to the re_pmatch */
123 const char **re_source;
124 /** A string where the comma separated names of headers are stored
125 * to be later added to the Vary: header. If NULL, the caller is not
126 * interested in this information.
128 const char **vary_this;
129 /** where to store the result string */
130 const char **result_string;
131 /** Arbitrary context data provided by the caller for custom functions */
133 } ap_expr_eval_ctx_t;
136 * Evaluate a parse tree, full featured version
137 * @param ctx The evaluation context with all data filled in
138 * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
139 * @note *ctx->err will be set to NULL on success, or to an error message on
141 * @note request headers used during evaluation will be added to the Vary:
142 * response header if ctx->vary_this is set.
144 AP_DECLARE(int) ap_expr_exec_ctx(ap_expr_eval_ctx_t *ctx);
147 * Evaluate a parse tree of a string valued expression
148 * @param r The current request
149 * @param expr The expression to be evaluated
150 * @param err Where an error message should be stored
151 * @return The result string, NULL on error
152 * @note err will be set to NULL on success, or to an error message on error
153 * @note request headers used during evaluation will be added to the Vary:
154 * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
156 AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r,
157 const ap_expr_info_t *expr,
161 * Evaluate a parse tree of a string valued expression
162 * @param r The current request
163 * @param expr The expression to be evaluated
164 * @param nmatch size of the regex match vector pmatch
165 * @param pmatch information about regex matches
166 * @param source the string that pmatch applies to
167 * @param err Where an error message should be stored
168 * @return The result string, NULL on error
169 * @note err will be set to NULL on success, or to an error message on error
170 * @note nmatch/pmatch/source can be used both to make previous matches
171 * available to ap_expr_exec_re and to use ap_expr_exec_re's matches
173 * @note request headers used during evaluation will be added to the Vary:
174 * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
176 AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
177 const ap_expr_info_t *expr,
179 ap_regmatch_t *pmatch,
185 * The parser can be extended with variable lookup, functions, and
188 * During parsing, the parser calls the lookup function to resolve a
189 * name into a function pointer and an opaque context for the function.
190 * If the argument to a function or operator is constant, the lookup function
191 * may also parse that argument and store the parsed data in the context.
193 * The default lookup function is the hook ::ap_expr_lookup_default which just
194 * calls ap_run_expr_lookup. Modules can use it to make functions and
195 * variables generally available.
197 * An ap_expr consumer can also provide its own custom lookup function to
198 * modify the set of variables and functions that are available. The custom
199 * lookup function can in turn call 'ap_run_expr_lookup'.
202 /** Unary operator, takes one string argument and returns a bool value.
203 * The name must have the form '-z' (one letter only).
204 * @param ctx The evaluation context
205 * @param data An opaque context provided by the lookup hook function
206 * @param arg The (right) operand
209 typedef int ap_expr_op_unary_t(ap_expr_eval_ctx_t *ctx, const void *data,
212 /** Binary operator, takes two string arguments and returns a bool value.
213 * The name must have the form '-cmp' (at least two letters).
214 * @param ctx The evaluation context
215 * @param data An opaque context provided by the lookup hook function
216 * @param arg1 The left operand
217 * @param arg2 The right operand
220 typedef int ap_expr_op_binary_t(ap_expr_eval_ctx_t *ctx, const void *data,
221 const char *arg1, const char *arg2);
223 /** String valued function, takes a string argument and returns a string
224 * @param ctx The evaluation context
225 * @param data An opaque context provided by the lookup hook function
226 * @param arg The argument
227 * @return The functions result string, may be NULL for 'empty string'
229 typedef const char *(ap_expr_string_func_t)(ap_expr_eval_ctx_t *ctx,
233 /** List valued function, takes a string argument and returns a list of strings
234 * Can currently only be called following the builtin '-in' operator.
235 * @param ctx The evaluation context
236 * @param data An opaque context provided by the lookup hook function
237 * @param arg The argument
238 * @return The functions result list of strings, may be NULL for 'empty array'
240 typedef apr_array_header_t *(ap_expr_list_func_t)(ap_expr_eval_ctx_t *ctx,
244 /** Variable lookup function, takes no argument and returns a string
245 * @param ctx The evaluation context
246 * @param data An opaque context provided by the lookup hook function
247 * @return The expanded variable
249 typedef const char *(ap_expr_var_func_t)(ap_expr_eval_ctx_t *ctx,
252 /** parameter struct passed to the lookup hook functions */
254 /** type of the looked up object */
256 #define AP_EXPR_FUNC_VAR 0
257 #define AP_EXPR_FUNC_STRING 1
258 #define AP_EXPR_FUNC_LIST 2
259 #define AP_EXPR_FUNC_OP_UNARY 3
260 #define AP_EXPR_FUNC_OP_BINARY 4
261 /** name of the looked up object */
269 /** where to store the function pointer */
271 /** where to store the function's context */
273 /** where to store the error message (if any) */
276 /** arg for pre-parsing (only if a simple string).
277 * For binary ops, this is the right argument. */
279 } ap_expr_lookup_parms;
281 /** Function for looking up the provider function for a variable, operator
282 * or function in an expression.
283 * @param parms The parameter struct, also determins where the result is
285 * @return OK on success,
287 * DECLINED if the requested name is not handled by this function
289 typedef int (ap_expr_lookup_fn_t)(ap_expr_lookup_parms *parms);
291 /** Default lookup function which just calls ap_run_expr_lookup().
292 * ap_run_expr_lookup cannot be used directly because it has the wrong
293 * calling convention under Windows.
295 AP_DECLARE_NONSTD(int) ap_expr_lookup_default(ap_expr_lookup_parms *parms);
297 AP_DECLARE_HOOK(int, expr_lookup, (ap_expr_lookup_parms *parms))
300 * Parse an expression into a parse tree
302 * @param ptemp temp pool
303 * @param info The ap_expr_info_t struct (with values filled in)
304 * @param expr The expression string to parse
305 * @param lookup_fn The lookup function to use, NULL for default
306 * @return NULL on success, error message on error.
307 * A pointer to the resulting parse tree will be stored in
310 AP_DECLARE(const char *) ap_expr_parse(apr_pool_t *pool, apr_pool_t *ptemp,
311 ap_expr_info_t *info, const char *expr,
312 ap_expr_lookup_fn_t *lookup_fn);
315 * High level interface to ap_expr_parse that also creates ap_expr_info_t and
316 * uses info from cmd_parms to fill in most of it.
317 * @param cmd The cmd_parms struct
318 * @param expr The expression string to parse
319 * @param flags The flags to use, see AP_EXPR_FLAG_*
320 * @param err Set to NULL on success, error message on error
321 * @param lookup_fn The lookup function used to lookup vars, functions, and
323 * @param module_index The module_index to set for the expression
324 * @return The parsed expression
325 * @note Usually ap_expr_parse_cmd() should be used
327 AP_DECLARE(ap_expr_info_t *) ap_expr_parse_cmd_mi(const cmd_parms *cmd,
331 ap_expr_lookup_fn_t *lookup_fn,
335 * Convenience wrapper for ap_expr_parse_cmd_mi() that sets
336 * module_index = APLOG_MODULE_INDEX
338 #define ap_expr_parse_cmd(cmd, expr, flags, err, lookup_fn) \
339 ap_expr_parse_cmd_mi(cmd, expr, flags, err, lookup_fn, APLOG_MODULE_INDEX)
342 * Internal initialisation of ap_expr (for httpd internal use)
344 void ap_expr_init(apr_pool_t *pool);
350 #endif /* AP_EXPR_H */