]> granicus.if.org Git - apache/blob - include/ap_expr.h
make bill happy (if possible!)
[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 Expression parser
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 virtual host */
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     /** The current recursion level */
134     int reclvl;
135 } ap_expr_eval_ctx_t;
136
137 /**
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
142  *       error
143  * @note request headers used during evaluation will be added to the Vary:
144  *       response header if ctx->vary_this is set.
145  */
146 AP_DECLARE(int) ap_expr_exec_ctx(ap_expr_eval_ctx_t *ctx);
147
148 /**
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.
157  */
158 AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r,
159                                           const ap_expr_info_t *expr,
160                                           const char **err);
161
162 /**
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
174  *       later on.
175  * @note request headers used during evaluation will be added to the Vary:
176  *       response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
177  */
178 AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
179                                              const ap_expr_info_t *expr,
180                                              apr_size_t nmatch,
181                                              ap_regmatch_t *pmatch,
182                                              const char **source,
183                                              const char **err);
184
185
186 /**
187  * The parser can be extended with variable lookup, functions, and
188  * and operators.
189  *
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.
194  *
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.
198  *
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'.
202  */
203
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
209  * @return 0 or 1
210  */
211 typedef int ap_expr_op_unary_t(ap_expr_eval_ctx_t *ctx, const void *data,
212                                const char *arg);
213
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
220  * @return 0 or 1
221  */
222 typedef int ap_expr_op_binary_t(ap_expr_eval_ctx_t *ctx, const void *data,
223                                 const char *arg1, const char *arg2);
224
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'
230  */
231 typedef const char *(ap_expr_string_func_t)(ap_expr_eval_ctx_t *ctx,
232                                             const void *data,
233                                             const char *arg);
234
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'
240  */
241 typedef const char *(ap_expr_string_list_func_t)(ap_expr_eval_ctx_t *ctx,
242                                             const void *data,
243                                             const apr_array_header_t *args);
244
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'
251  */
252 typedef apr_array_header_t *(ap_expr_list_func_t)(ap_expr_eval_ctx_t *ctx,
253                                                   const void *data,
254                                                   const char *arg);
255
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
260  */
261 typedef const char *(ap_expr_var_func_t)(ap_expr_eval_ctx_t *ctx,
262                                          const void *data);
263
264 /** parameter struct passed to the lookup hook functions */
265 typedef struct {
266     /** type of the looked up object */
267     int type;
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 */
274     const char *name;
275
276     int flags;
277
278     apr_pool_t *pool;
279     apr_pool_t *ptemp;
280
281     /** where to store the function pointer */
282     const void **func;
283     /** where to store the function's context */
284     const void **data;
285     /** where to store the error message (if any) */
286     const char **err;
287
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
291      *  argument. */
292     const char *arg;
293 } ap_expr_lookup_parms;
294
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
298  *               stored.
299  *  @return OK on success,
300  *          !OK on failure,
301  *          DECLINED if the requested name is not handled by this function
302  */
303 typedef int (ap_expr_lookup_fn_t)(ap_expr_lookup_parms *parms);
304
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.
308  */
309 AP_DECLARE_NONSTD(int) ap_expr_lookup_default(ap_expr_lookup_parms *parms);
310
311 AP_DECLARE_HOOK(int, expr_lookup, (ap_expr_lookup_parms *parms))
312
313 /**
314  * Parse an expression into a parse tree
315  * @param pool Pool
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
322  *         info->root_node.
323  */
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);
327
328 /**
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
336  *        operators
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
340  */
341 AP_DECLARE(ap_expr_info_t *) ap_expr_parse_cmd_mi(const cmd_parms *cmd,
342                                                   const char *expr,
343                                                   unsigned int flags,
344                                                   const char **err,
345                                                   ap_expr_lookup_fn_t *lookup_fn,
346                                                   int module_index);
347
348 /**
349  * Convenience wrapper for ap_expr_parse_cmd_mi() that sets
350  * module_index = APLOG_MODULE_INDEX
351  */
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)
354
355  /**
356   * Internal initialisation of ap_expr (for httpd internal use)
357   */
358 void ap_expr_init(apr_pool_t *pool);
359
360 #ifdef __cplusplus
361 }
362 #endif
363
364 #endif /* AP_EXPR_H */
365 /** @} */