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 Expression parser
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 virtual host */
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 /** The current recursion level */
135 } ap_expr_eval_ctx_t;
138 * Evaluate a parse tree, full featured version
139 * @param ctx The evaluation context with all data filled in
140 * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
141 * @note *ctx->err will be set to NULL on success, or to an error message on
143 * @note request headers used during evaluation will be added to the Vary:
144 * response header if ctx->vary_this is set.
146 AP_DECLARE(int) ap_expr_exec_ctx(ap_expr_eval_ctx_t *ctx);
149 * Evaluate a parse tree of a string valued expression
150 * @param r The current request
151 * @param expr The expression to be evaluated
152 * @param err Where an error message should be stored
153 * @return The result string, NULL on error
154 * @note err will be set to NULL on success, or to an error message on error
155 * @note request headers used during evaluation will be added to the Vary:
156 * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
158 AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r,
159 const ap_expr_info_t *expr,
163 * Evaluate a parse tree of a string valued expression
164 * @param r The current request
165 * @param expr The expression to be evaluated
166 * @param nmatch size of the regex match vector pmatch
167 * @param pmatch information about regex matches
168 * @param source the string that pmatch applies to
169 * @param err Where an error message should be stored
170 * @return The result string, NULL on error
171 * @note err will be set to NULL on success, or to an error message on error
172 * @note nmatch/pmatch/source can be used both to make previous matches
173 * available to ap_expr_exec_re and to use ap_expr_exec_re's matches
175 * @note request headers used during evaluation will be added to the Vary:
176 * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
178 AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
179 const ap_expr_info_t *expr,
181 ap_regmatch_t *pmatch,
187 * The parser can be extended with variable lookup, functions, and
190 * During parsing, the parser calls the lookup function to resolve a
191 * name into a function pointer and an opaque context for the function.
192 * If the argument to a function or operator is constant, the lookup function
193 * may also parse that argument and store the parsed data in the context.
195 * The default lookup function is the hook ::ap_expr_lookup_default which just
196 * calls ap_run_expr_lookup. Modules can use it to make functions and
197 * variables generally available.
199 * An ap_expr consumer can also provide its own custom lookup function to
200 * modify the set of variables and functions that are available. The custom
201 * lookup function can in turn call 'ap_run_expr_lookup'.
204 /** Unary operator, takes one string argument and returns a bool value.
205 * The name must have the form '-z' (one letter only).
206 * @param ctx The evaluation context
207 * @param data An opaque context provided by the lookup hook function
208 * @param arg The (right) operand
211 typedef int ap_expr_op_unary_t(ap_expr_eval_ctx_t *ctx, const void *data,
214 /** Binary operator, takes two string arguments and returns a bool value.
215 * The name must have the form '-cmp' (at least two letters).
216 * @param ctx The evaluation context
217 * @param data An opaque context provided by the lookup hook function
218 * @param arg1 The left operand
219 * @param arg2 The right operand
222 typedef int ap_expr_op_binary_t(ap_expr_eval_ctx_t *ctx, const void *data,
223 const char *arg1, const char *arg2);
225 /** String valued function, takes a string argument and returns a string
226 * @param ctx The evaluation context
227 * @param data An opaque context provided by the lookup hook function
228 * @param arg The argument
229 * @return The functions result string, may be NULL for 'empty string'
231 typedef const char *(ap_expr_string_func_t)(ap_expr_eval_ctx_t *ctx,
235 /** String valued function, takes a list argument and returns a string
236 * @param ctx The evaluation context
237 * @param data An opaque context provided by the lookup hook function
238 * @param args The list of string arguments
239 * @return The functions result string, may be NULL for 'empty string'
241 typedef const char *(ap_expr_string_list_func_t)(ap_expr_eval_ctx_t *ctx,
243 const apr_array_header_t *args);
245 /** List valued function, takes a string argument and returns a list of strings
246 * Can currently only be called following the builtin '-in' operator.
247 * @param ctx The evaluation context
248 * @param data An opaque context provided by the lookup hook function
249 * @param arg The argument
250 * @return The functions result list of strings, may be NULL for 'empty array'
252 typedef apr_array_header_t *(ap_expr_list_func_t)(ap_expr_eval_ctx_t *ctx,
256 /** Variable lookup function, takes no argument and returns a string
257 * @param ctx The evaluation context
258 * @param data An opaque context provided by the lookup hook function
259 * @return The expanded variable
261 typedef const char *(ap_expr_var_func_t)(ap_expr_eval_ctx_t *ctx,
264 /** parameter struct passed to the lookup hook functions */
266 /** type of the looked up object */
268 #define AP_EXPR_FUNC_VAR 0
269 #define AP_EXPR_FUNC_STRING 1
270 #define AP_EXPR_FUNC_LIST 2
271 #define AP_EXPR_FUNC_OP_UNARY 3
272 #define AP_EXPR_FUNC_OP_BINARY 4
273 /** name of the looked up object */
281 /** where to store the function pointer */
283 /** where to store the function's context */
285 /** where to store the error message (if any) */
288 /** arg for pre-parsing (only if a simple string).
289 * For binary ops, this is the right argument.
290 * For functions with more arguments, this is the first string
293 } ap_expr_lookup_parms;
295 /** Function for looking up the provider function for a variable, operator
296 * or function in an expression.
297 * @param parms The parameter struct, also determins where the result is
299 * @return OK on success,
301 * DECLINED if the requested name is not handled by this function
303 typedef int (ap_expr_lookup_fn_t)(ap_expr_lookup_parms *parms);
305 /** Default lookup function which just calls ap_run_expr_lookup().
306 * ap_run_expr_lookup cannot be used directly because it has the wrong
307 * calling convention under Windows.
309 AP_DECLARE_NONSTD(int) ap_expr_lookup_default(ap_expr_lookup_parms *parms);
311 AP_DECLARE_HOOK(int, expr_lookup, (ap_expr_lookup_parms *parms))
314 * Parse an expression into a parse tree
316 * @param ptemp temp pool
317 * @param info The ap_expr_info_t struct (with values filled in)
318 * @param expr The expression string to parse
319 * @param lookup_fn The lookup function to use, NULL for default
320 * @return NULL on success, error message on error.
321 * A pointer to the resulting parse tree will be stored in
324 AP_DECLARE(const char *) ap_expr_parse(apr_pool_t *pool, apr_pool_t *ptemp,
325 ap_expr_info_t *info, const char *expr,
326 ap_expr_lookup_fn_t *lookup_fn);
329 * High level interface to ap_expr_parse that also creates ap_expr_info_t and
330 * uses info from cmd_parms to fill in most of it.
331 * @param cmd The cmd_parms struct
332 * @param expr The expression string to parse
333 * @param flags The flags to use, see AP_EXPR_FLAG_*
334 * @param err Set to NULL on success, error message on error
335 * @param lookup_fn The lookup function used to lookup vars, functions, and
337 * @param module_index The module_index to set for the expression
338 * @return The parsed expression
339 * @note Usually ap_expr_parse_cmd() should be used
341 AP_DECLARE(ap_expr_info_t *) ap_expr_parse_cmd_mi(const cmd_parms *cmd,
345 ap_expr_lookup_fn_t *lookup_fn,
349 * Convenience wrapper for ap_expr_parse_cmd_mi() that sets
350 * module_index = APLOG_MODULE_INDEX
352 #define ap_expr_parse_cmd(cmd, expr, flags, err, lookup_fn) \
353 ap_expr_parse_cmd_mi(cmd, expr, flags, err, lookup_fn, APLOG_MODULE_INDEX)
356 * Internal initialisation of ap_expr (for httpd internal use)
358 void ap_expr_init(apr_pool_t *pool);
364 #endif /* AP_EXPR_H */