/
rule_extensions.c
271 lines (221 loc) · 6.07 KB
/
rule_extensions.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "rule_extensions.h"
#include "plugin_loader.h"
#include "connection-protected.h"
#include "server-protected.h"
#include "thread.h"
#include "util.h"
#define ENTRIES "rule,extensions"
#define MAGIC 0xFABADA
PLUGIN_INFO_RULE_EASIEST_INIT(extensions);
static ret_t
parse_value (cherokee_buffer_t *value, cherokee_avl_t *extensions)
{
char *val;
char *tmpp;
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
TRACE(ENTRIES, "Adding extensions: '%s'\n", value->buf);
cherokee_buffer_add_buffer (&tmp, value);
tmpp = tmp.buf;
while ((val = strsep(&tmpp, ",")) != NULL) {
TRACE(ENTRIES, "Adding extension: '%s'\n", val);
cherokee_avl_add_ptr (extensions, val, (void *)MAGIC);
}
cherokee_buffer_mrproper (&tmp);
return ret_ok;
}
static ret_t
configure (cherokee_rule_extensions_t *rule,
cherokee_config_node_t *conf,
cherokee_virtual_server_t *vsrv)
{
ret_t ret;
cherokee_buffer_t *tmp = NULL;
UNUSED(vsrv);
ret = cherokee_config_node_read (conf, "extensions", &tmp);
if (ret != ret_ok) {
LOG_CRITICAL (CHEROKEE_ERROR_RULE_NO_PROPERTY,
RULE(rule)->priority, "extensions");
return ret_error;
}
cherokee_config_node_read_bool (conf, "check_local_file", &rule->check_local_file);
cherokee_config_node_read_bool (conf, "iocache", &rule->use_iocache);
return parse_value (tmp, &rule->extensions);
}
static ret_t
_free (void *p)
{
cherokee_rule_extensions_t *rule = RULE_EXTENSIONS(p);
cherokee_avl_mrproper (&rule->extensions, NULL);
return ret_ok;
}
static ret_t
local_file_exists (cherokee_rule_extensions_t *rule,
cherokee_connection_t *conn,
cherokee_config_entry_t *ret_conf)
{
ret_t ret;
struct stat *info;
struct stat nocache_info;
cherokee_boolean_t is_file;
cherokee_iocache_entry_t *io_entry = NULL;
cherokee_server_t *srv = CONN_SRV(conn);
cherokee_buffer_t *tmp = THREAD_TMP_BUF1(CONN_THREAD(conn));
UNUSED(rule);
/* Build the full path
*/
cherokee_buffer_clean (tmp);
if (ret_conf->document_root != NULL) {
/* A previous non-final rule set a custom document root */
cherokee_buffer_add_buffer (tmp, ret_conf->document_root);
} else {
cherokee_buffer_add_buffer (tmp, &conn->local_directory);
}
cherokee_buffer_add_str (tmp, "/");
if (! cherokee_buffer_is_empty (&conn->web_directory)) {
cherokee_buffer_add (tmp,
conn->request.buf + conn->web_directory.len,
conn->request.len - conn->web_directory.len);
} else {
cherokee_buffer_add_buffer (tmp, &conn->request);
}
/* Check the local file
*/
ret = cherokee_io_stat (srv->iocache, tmp, rule->use_iocache,
&nocache_info, &io_entry, &info);
if (ret == ret_ok) {
is_file = S_ISREG(info->st_mode);
}
if (io_entry) {
cherokee_iocache_entry_unref (&io_entry);
}
/* Report and return
*/
if (ret != ret_ok) {
TRACE(ENTRIES, "Rule extensions: almost matched '%s', but file does not exist\n", tmp->buf);
return ret_not_found;
}
if (! is_file) {
TRACE(ENTRIES, "Rule extensions: almost matched '%s', but it is not a file\n", tmp->buf);
return ret_not_found;
}
return ret_ok;
}
static ret_t
match (cherokee_rule_extensions_t *rule,
cherokee_connection_t *conn,
cherokee_config_entry_t *ret_conf)
{
ret_t ret;
char *dot;
char *slash;
char *end;
char *p;
void *foo;
char *dot_prev = NULL;
UNUSED(ret_conf);
end = conn->request.buf + conn->request.len;
p = end - 1;
/* For each '.' */
while (p > conn->request.buf) {
if (*p != '.') {
p--;
continue;
}
if (p[1] == '\0') {
p--;
continue;
}
if (p[1] == '/') {
p--;
continue;
}
dot = p;
slash = NULL;
/* Find a slash after the dot */
while (p < end) {
if (*p == '/') {
slash = p;
*p = '\0';
break;
}
p++;
if ((dot_prev != NULL) && (p >= dot_prev)) {
break;
}
}
/* Check it out */
ret = cherokee_avl_get_ptr (&rule->extensions, dot+1, &foo);
switch (ret) {
case ret_ok:
if (rule->check_local_file) {
ret = local_file_exists (rule, conn, ret_conf);
if (ret != ret_ok) {
break;
}
}
TRACE(ENTRIES, "Match extension: '%s'\n", dot+1);
if (slash != NULL) {
*slash = '/';
}
return ret_ok;
case ret_not_found:
TRACE(ENTRIES, "Rule extension: did not match '%s'\n", dot+1);
break;
default:
conn->error_code = http_internal_error;
return ret_error;
}
/* Revert pathinfo match char
*/
if (slash != NULL) {
*slash = '/';
}
dot_prev = dot;
p = dot - 1;
}
TRACE(ENTRIES, "Rule extension: nothing more to test '%s'\n", conn->request.buf);
return ret_not_found;
}
ret_t
cherokee_rule_extensions_new (cherokee_rule_extensions_t **rule)
{
CHEROKEE_NEW_STRUCT (n, rule_extensions);
/* Parent class constructor
*/
cherokee_rule_init_base (RULE(n), PLUGIN_INFO_PTR(extensions));
/* Virtual methods
*/
RULE(n)->match = (rule_func_match_t) match;
RULE(n)->configure = (rule_func_configure_t) configure;
MODULE(n)->free = (module_func_free_t) _free;
/* Properties
*/
n->check_local_file = false;
n->use_iocache = true;
cherokee_avl_init (&n->extensions);
*rule = n;
return ret_ok;
}