Skip to content
This repository
Newer
Older
100644 207 lines (164 sloc) 4.438 kb
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Cherokee
4 *
5 * Authors:
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 *
920e20bc »
2011-01-14 Happy new year!
8 * Copyright (C) 2001-2011 Alvaro Lopez Ortega
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
23 */
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
24
25 #include "common-internal.h"
73eb6823 »
2009-11-24 Renames the URL parameter matching module (http_arg to url_arg), and
26 #include "rule_url_arg.h"
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
27
28 #include "plugin_loader.h"
29 #include "server-protected.h"
30 #include "connection-protected.h"
31 #include "util.h"
32 #include "pcre/pcre.h"
33
73eb6823 »
2009-11-24 Renames the URL parameter matching module (http_arg to url_arg), and
34 #define ENTRIES "rule,url_arg"
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
35
73eb6823 »
2009-11-24 Renames the URL parameter matching module (http_arg to url_arg), and
36 PLUGIN_INFO_RULE_EASIEST_INIT(url_arg);
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
37
38
39 static ret_t
73eb6823 »
2009-11-24 Renames the URL parameter matching module (http_arg to url_arg), and
40 check_argument (cherokee_rule_url_arg_t *rule,
41 cherokee_buffer_t *value)
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
42 {
43 int re;
44
45 /* Check whether it matches
46 */
47 re = pcre_exec (rule->pcre, NULL,
48 value->buf, value->len,
49 0, 0, NULL, 0);
50
51 if (re < 0) {
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
52 TRACE (ENTRIES, "Parameter value '%s' didn't match with '%s'\n",
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
53 value->buf, rule->match.buf);
54 return ret_not_found;
55 }
56
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
57 TRACE (ENTRIES, "Parameter value '%s' matched with '%s'\n",
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
58 value->buf, rule->match.buf);
59
60 return ret_ok;
61 }
62
63
64 static ret_t
09ab2bac »
2009-11-24 conn->arguments stores cherokee_buffer_t objects instead of char *
65 match_avl_func (cherokee_buffer_t *key, void *val, void *param)
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
66 {
73eb6823 »
2009-11-24 Renames the URL parameter matching module (http_arg to url_arg), and
67 ret_t ret;
68 cherokee_buffer_t *value = BUF(val);
69 cherokee_rule_url_arg_t *rule = RULE_URL_ARG(param);
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
70
71 UNUSED(key);
72
09ab2bac »
2009-11-24 conn->arguments stores cherokee_buffer_t objects instead of char *
73 /* Skip key-only entries (with no value)
74 */
75 if (value == NULL) {
76 return ret_ok;
77 }
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
78
09ab2bac »
2009-11-24 conn->arguments stores cherokee_buffer_t objects instead of char *
79 /* Check the value
80 */
81 ret = check_argument (rule, value);
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
82 if (ret == ret_not_found) {
83 return ret_ok;
84 }
85
86 /* Found */
87 return ret_eof;
88 }
89
90
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
91 static ret_t
92 match (cherokee_rule_url_arg_t *rule,
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
93 cherokee_connection_t *conn,
94 cherokee_config_entry_t *ret_conf)
95 {
09ab2bac »
2009-11-24 conn->arguments stores cherokee_buffer_t objects instead of char *
96 ret_t ret;
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
97 cherokee_buffer_t *value;
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
98
99 UNUSED(ret_conf);
100
101 /* Parse HTTP arguments
102 */
103 ret = cherokee_connection_parse_args (conn);
104 if (ret != ret_ok) {
105 return ret_not_found;
106 }
107
108 if (conn->arguments == NULL) {
109 return ret_not_found;
110 }
111
112 /* Retrieve the right one
113 */
09ab2bac »
2009-11-24 conn->arguments stores cherokee_buffer_t objects instead of char *
114 if (! cherokee_buffer_is_empty (&rule->arg))
115 {
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
116 ret = cherokee_avl_get (conn->arguments, &rule->arg, (void **)&value);
117 if (ret != ret_ok) {
118 return ret_not_found;
119 }
120
09ab2bac »
2009-11-24 conn->arguments stores cherokee_buffer_t objects instead of char *
121 return check_argument (rule, value);
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
122 }
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
123
124 /* Check all arguments
125 */
126 else {
127 ret = cherokee_avl_while (conn->arguments, match_avl_func, rule, NULL, NULL);
128 if (ret == ret_eof) {
129 return ret_ok;
130 }
131 }
132
133 return ret_not_found;
134 }
135
136
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
137 static ret_t
138 configure (cherokee_rule_url_arg_t *rule,
139 cherokee_config_node_t *conf,
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
140 cherokee_virtual_server_t *vsrv)
141 {
142 ret_t ret;
143 cherokee_regex_table_t *regexs = VSERVER_SRV(vsrv)->regexs;
144
145 /* Read the matching reg-ex
146 */
147 ret = cherokee_config_node_copy (conf, "match", &rule->match);
148 if (ret != ret_ok) {
149 LOG_ERROR (CHEROKEE_ERROR_RULE_NO_PROPERTY,
150 RULE(rule)->priority, "match");
151 return ret_error;
152 }
153
154 /* Read the optional argument
155 */
156 cherokee_config_node_copy (conf, "arg", &rule->arg);
157
158 /* Compile the regular expression
159 */
160 ret = cherokee_regex_table_add (regexs, rule->match.buf);
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
161 if (ret != ret_ok)
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
162 return ret;
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
163
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
164 ret = cherokee_regex_table_get (regexs, rule->match.buf, &rule->pcre);
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
165 if (ret != ret_ok)
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
166 return ret;
167
168 return ret_ok;
169 }
170
171
172 static ret_t
173 _free (void *p)
174 {
73eb6823 »
2009-11-24 Renames the URL parameter matching module (http_arg to url_arg), and
175 cherokee_rule_url_arg_t *rule = RULE_URL_ARG(p);
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
176
177 cherokee_buffer_mrproper (&rule->arg);
178 cherokee_buffer_mrproper (&rule->match);
179 return ret_ok;
180 }
181
182
183 ret_t
73eb6823 »
2009-11-24 Renames the URL parameter matching module (http_arg to url_arg), and
184 cherokee_rule_url_arg_new (cherokee_rule_url_arg_t **rule)
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
185 {
73eb6823 »
2009-11-24 Renames the URL parameter matching module (http_arg to url_arg), and
186 CHEROKEE_NEW_STRUCT (n, rule_url_arg);
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
187
188 /* Parent class constructor
189 */
73eb6823 »
2009-11-24 Renames the URL parameter matching module (http_arg to url_arg), and
190 cherokee_rule_init_base (RULE(n), PLUGIN_INFO_PTR(url_arg));
72f64441 »
2009-12-13 Clean up: Trims trailing spaces.
191
992ee812 »
2009-11-24 Adds a new rule type. It evaluates regular expressions against the
192 /* Virtual methods
193 */
194 RULE(n)->match = (rule_func_match_t) match;
195 RULE(n)->configure = (rule_func_configure_t) configure;
196 MODULE(n)->free = (module_func_free_t) _free;
197
198 /* Properties
199 */
200 n->pcre = NULL;
201 cherokee_buffer_init (&n->arg);
202 cherokee_buffer_init (&n->match);
203
204 *rule = n;
205 return ret_ok;
206 }
Something went wrong with that request. Please try again.