summaryrefslogtreecommitdiff
path: root/include/ap_expr.h
blob: 2763ee23d700fb77a5e7e71833ae3f0c94152178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file ap_expr.h
 * @brief Expression parser
 */

#ifndef AP_EXPR_H
#define AP_EXPR_H

#include "httpd.h"
#include "ap_regex.h"

#ifdef __cplusplus
extern "C" {
#endif

/* conditional expression parser stuff */
typedef enum {
    TOKEN_STRING,
    TOKEN_RE,
    TOKEN_AND,
    TOKEN_OR,
    TOKEN_NOT,
    TOKEN_EQ,
    TOKEN_NE,
    TOKEN_RBRACE,
    TOKEN_LBRACE,
    TOKEN_GROUP,
    TOKEN_GE,
    TOKEN_LE,
    TOKEN_GT,
    TOKEN_LT,
    TOKEN_ACCESS
} token_type_t;

typedef struct {
    token_type_t  type;
    const char   *value;
#ifdef DEBUG_INCLUDE
    const char   *s;
#endif
} token_t;

typedef struct parse_node {
    struct parse_node *parent;
    struct parse_node *left;
    struct parse_node *right;
    token_t token;
    int value;
    int done;
#ifdef DEBUG_INCLUDE
    int dump_done;
#endif
} ap_parse_node_t;

typedef struct {
    const char *source;
    const char *rexp;
    apr_size_t  nsub;
    ap_regmatch_t match[AP_MAX_REG_MATCH];
} backref_t;

typedef const char *(*string_func_t)(request_rec*, const char*);
typedef int (*opt_func_t)(request_rec*, ap_parse_node_t*, string_func_t);

/**
 * Parse an expression into a parse tree
 * @param pool Pool
 * @param expr The expression to parse
 * @param was_error On return, set to zero if parse successful, nonzero on error
 * @return The parse tree
 */
AP_DECLARE(ap_parse_node_t*) ap_expr_parse(apr_pool_t *pool, const char *expr,
                                           int *was_error);
/**
 * Evaluate a parse tree
 * @param r The current request
 * @param root The root node of the parse tree
 * @param was_error On return, set to zero if parse successful, nonzero on error
 * @param reptr Regular expression memory for backreferencing if a regexp was parsed
 * @param string_func String parser function - perform variable substitutions
 *                    Use ap_expr_string where applicable
 * @param eval_func Option evaluation function (e.g. -A filename)
 * @return the value the expression parsed to
 */
AP_DECLARE(int) ap_expr_eval(request_rec *r, ap_parse_node_t *root,
                             int *was_error, backref_t **reptr,
                             string_func_t string_func, opt_func_t eval_func);
/**
 * Evaluate an expression.  This is functionally equivalent to
 * ap_expr_parse followed by ap_expr_eval, but faster and more efficient
 * when an expression only needs to be parsed once and discarded.
 * @param r The current request
 * @param expr The expression to parse
 * @param was_error On return, set to zero if parse successful, nonzero on error
 * @param reptr Regular expression memory for backreferencing if a regexp was parsed
 * @param string_func String parser function - perform variable substitutions
 *                    Use ap_expr_string where applicable
 * @param eval_func Option evaluation function (e.g. -A filename)
 * @return the value the expression parsed to
 */
AP_DECLARE(int) ap_expr_evalstring(request_rec *r, const char *expr,
                                   int *was_error, backref_t **reptr,
                                   string_func_t string_func,
                                   opt_func_t eval_func);

/**
 * Internal initialisation of ap_expr (for httpd)
 * @param pool Pool
 * @return APR_SUCCESS or error
 */
AP_DECLARE(apr_status_t) ap_expr_init(apr_pool_t *pool);

/**
 * Default string evaluation function for passing to ap_expr_eval and
 * ap_expr_evalstring.  Use this (and update as necessary) to offer
 * a consistent expression syntax across different modules.
 * Supports the following:
 *     $req{foo}     - request header "foo"
 *     $resp{foo}    - response header "foo"
 *     $env{foo}     - environment variable "foo"
 *     $handler      - r->handler
 *     $content-type - r->content_type
 * Other strings are returned unmodified.
 * @param r The current request
 * @param str The string to evaluate
 * @return The evaluated string
 */
AP_DECLARE_NONSTD(const char*) ap_expr_string(request_rec *r, 
                                              const char *str);

#ifdef __cplusplus
}
#endif

#endif /* AP_EXPR_H */