]> granicus.if.org Git - apache/blob - include/ap_expr.h
Create wrapper API for apr_random;
[apache] / include / ap_expr.h
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file ap_expr.h
19  * @brief Expression parser
20  *
21  * @defgroup AP_EXPR ap_expr
22  * @ingroup  APACHE_CORE
23  * @{
24  */
25
26 #ifndef AP_EXPR_H
27 #define AP_EXPR_H
28
29 #include "httpd.h"
30 #include "http_config.h"
31 #include "ap_regex.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /** A node in the expression parse tree */
38 typedef struct ap_expr_node ap_expr_t;
39
40 /** Struct describing a parsed expression */
41 typedef struct {
42     /** The root of the actual expression parse tree */
43     ap_expr_t *root_node;
44     /** The filename where the expression has been defined (for logging).
45      *  May be NULL
46      */
47     const char *filename;
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_* */
51     unsigned int flags;
52     /** The module that is used for loglevel configuration */
53     int module_index;
54 } ap_expr_info_t;
55
56 /** Use ssl_expr compatibility mode (changes the meaning of the comparison
57  * operators)
58  */
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.
65  */
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
69
70
71 /**
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.
80  */
81 AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *expr,
82                              const char **err);
83
84 /**
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
96  *       later on.
97  * @note request headers used during evaluation will be added to the Vary:
98  *       response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
99  */
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);
103
104 /** Context used during evaluation of a parse tree, created by ap_expr_exec */
105 typedef struct {
106     /** the current request */
107     request_rec *r;
108     /** the current connection */
109     conn_rec *c;
110     /** the current connection */
111     server_rec *s;
112     /** the pool to use */
113     apr_pool_t *p;
114     /** where to store the error string */
115     const char **err;
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.
127      */
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 */
132     void *data;
133 } ap_expr_eval_ctx_t;
134
135 /**
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
140  *       error
141  * @note request headers used during evaluation will be added to the Vary:
142  *       response header if ctx->vary_this is set.
143  */
144 AP_DECLARE(int) ap_expr_exec_ctx(ap_expr_eval_ctx_t *ctx);
145
146 /**
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.
155  */
156 AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r,
157                                           const ap_expr_info_t *expr,
158                                           const char **err);
159
160 /**
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
172  *       later on.
173  * @note request headers used during evaluation will be added to the Vary:
174  *       response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
175  */
176 AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
177                                              const ap_expr_info_t *expr,
178                                              apr_size_t nmatch,
179                                              ap_regmatch_t *pmatch,
180                                              const char **source,
181                                              const char **err);
182
183
184 /**
185  * The parser can be extended with variable lookup, functions, and
186  * and operators.
187  *
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.
192  *
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.
196  *
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'.
200  */
201
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
207  * @return 0 or 1
208  */
209 typedef int ap_expr_op_unary_t(ap_expr_eval_ctx_t *ctx, const void *data,
210                                const char *arg);
211
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
218  * @return 0 or 1
219  */
220 typedef int ap_expr_op_binary_t(ap_expr_eval_ctx_t *ctx, const void *data,
221                                 const char *arg1, const char *arg2);
222
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'
228  */
229 typedef const char *(ap_expr_string_func_t)(ap_expr_eval_ctx_t *ctx,
230                                             const void *data,
231                                             const char *arg);
232
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'
239  */
240 typedef apr_array_header_t *(ap_expr_list_func_t)(ap_expr_eval_ctx_t *ctx,
241                                                   const void *data,
242                                                   const char *arg);
243
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
248  */
249 typedef const char *(ap_expr_var_func_t)(ap_expr_eval_ctx_t *ctx,
250                                          const void *data);
251
252 /** parameter struct passed to the lookup hook functions */
253 typedef struct {
254     /** type of the looked up object */
255     int type;
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 */
262     const char *name;
263
264     int flags;
265
266     apr_pool_t *pool;
267     apr_pool_t *ptemp;
268
269     /** where to store the function pointer */
270     const void **func;
271     /** where to store the function's context */
272     const void **data;
273     /** where to store the error message (if any) */
274     const char **err;
275
276     /** arg for pre-parsing (only if a simple string).
277      *  For binary ops, this is the right argument. */
278     const char *arg;
279 } ap_expr_lookup_parms;
280
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
284  *               stored.
285  *  @return OK on success,
286  *          !OK on failure,
287  *          DECLINED if the requested name is not handled by this function
288  */
289 typedef int (ap_expr_lookup_fn_t)(ap_expr_lookup_parms *parms);
290
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.
294  */
295 AP_DECLARE_NONSTD(int) ap_expr_lookup_default(ap_expr_lookup_parms *parms);
296
297 AP_DECLARE_HOOK(int, expr_lookup, (ap_expr_lookup_parms *parms))
298
299 /**
300  * Parse an expression into a parse tree
301  * @param pool Pool
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
308  *         info->root_node.
309  */
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);
313
314 /**
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
322  *        operators
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
326  */
327 AP_DECLARE(ap_expr_info_t *) ap_expr_parse_cmd_mi(const cmd_parms *cmd,
328                                                   const char *expr,
329                                                   unsigned int flags,
330                                                   const char **err,
331                                                   ap_expr_lookup_fn_t *lookup_fn,
332                                                   int module_index);
333
334 /**
335  * Convenience wrapper for ap_expr_parse_cmd_mi() that sets
336  * module_index = APLOG_MODULE_INDEX
337  */
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)
340
341  /**
342   * Internal initialisation of ap_expr (for httpd internal use)
343   */
344 void ap_expr_init(apr_pool_t *pool);
345
346 #ifdef __cplusplus
347 }
348 #endif
349
350 #endif /* AP_EXPR_H */
351 /** @} */