Permalink
Newer
Older
100644 148 lines (120 sloc) 3.38 KB
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
*
@alobbs
Jan 14, 2011
8
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
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.
24
25
#include "common-internal.h"
26
#include "rule_or.h"
27
#include "plugin_loader.h"
28
#include "virtual_server.h"
29
30
#define ENTRIES "rule,or"
31
32
PLUGIN_INFO_RULE_EASIEST_INIT(or);
33
34
static ret_t
35
match (cherokee_rule_t *rule,
36
cherokee_connection_t *conn,
37
cherokee_config_entry_t *ret_conf)
38
{
39
ret_t ret;
40
41
/* It only needs one of the sides to match.
42
*/
43
ret = cherokee_rule_match (RULE_OR(rule)->left, conn, ret_conf);
45
return ret;
46
47
/* It didn't match, it is time for the right side
48
*/
49
return cherokee_rule_match (RULE_OR(rule)->right, conn, ret_conf);
52
static ret_t
53
configure_branch (cherokee_rule_or_t *rule,
54
cherokee_config_node_t *conf,
55
cherokee_virtual_server_t *vsrv,
56
const char *branch,
57
cherokee_rule_t **branch_rule)
58
{
59
ret_t ret;
60
cherokee_config_node_t *subconf = NULL;
62
/* Get the configuration sub-tree
63
*/
64
ret = cherokee_config_node_get (conf, branch, &subconf);
65
if (ret != ret_ok)
66
return ret;
67
68
/* Instance the sub-rule match
69
*/
70
ret = cherokee_virtual_server_new_rule (vsrv, subconf,
71
RULE(rule)->priority,
72
branch_rule);
73
if (ret != ret_ok)
74
return ret;
75
76
if ((branch_rule == NULL) || (*branch_rule) == NULL)
77
return ret_error;
78
79
/* Let the child rule know about its parent
80
*/
81
(*branch_rule)->parent_rule = RULE(rule);
82
86
static ret_t
87
configure (cherokee_rule_or_t *rule,
88
cherokee_config_node_t *conf,
89
cherokee_virtual_server_t *vsrv)
90
{
91
ret_t ret;
93
ret = configure_branch (rule, conf, vsrv, "right", &rule->right);
94
if (ret != ret_ok)
95
return ret;
96
97
ret = configure_branch (rule, conf, vsrv, "left", &rule->left);
98
if (ret != ret_ok)
99
return ret;
100
101
return ret_ok;
102
}
103
104
static ret_t
105
_free (void *p)
106
{
107
ret_t ret;
108
cherokee_boolean_t error = false;
109
cherokee_rule_or_t *rule = RULE_OR(p);
110
111
if (rule->left) {
112
ret = cherokee_rule_free (rule->left);
113
if (ret != ret_ok) error = true;
114
}
115
116
if (rule->right) {
117
ret = cherokee_rule_free (rule->right);
118
if (ret != ret_ok) error = true;
121
return (error)? ret_error : ret_ok;
124
ret_t
125
cherokee_rule_or_new (cherokee_rule_t **rule)
126
{
127
CHEROKEE_NEW_STRUCT (n, rule_or);
128
129
/* Parent class constructor
130
*/
131
cherokee_rule_init_base (RULE(n), PLUGIN_INFO_PTR(or));
132
133
/* Virtual methods
134
*/
135
RULE(n)->match = (rule_func_match_t) match;
136
RULE(n)->configure = (rule_func_configure_t) configure;
137
MODULE(n)->free = (module_func_free_t) _free;
138
139
/* Properties
140
*/
141
n->left = NULL;
142
n->right = NULL;
143
144
*rule = RULE(n);
145
return ret_ok;
146
}
147