Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 272 lines (221 sloc) 6.211 kB
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
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 *
920e20b @alobbs Happy new year!
alobbs authored
8 * Copyright (C) 2001-2011 Alvaro Lopez Ortega
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
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
2c39092 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2660 5dc97367-9…
alobbs authored
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
23 */
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
24
25 #include "common-internal.h"
26 #include "rule_extensions.h"
27 #include "plugin_loader.h"
28 #include "connection-protected.h"
4aa95fd @alobbs Adds a new option to the Extensions rule matching plug-in. It allows
alobbs authored
29 #include "server-protected.h"
30 #include "thread.h"
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
31 #include "util.h"
32
33 #define ENTRIES "rule,extensions"
34 #define MAGIC 0xFABADA
35
36 PLUGIN_INFO_RULE_EASIEST_INIT(extensions);
37
38
39 static ret_t
40 parse_value (cherokee_buffer_t *value, cherokee_avl_t *extensions)
41 {
42 char *val;
700f901 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1451 5dc97367-9…
alobbs authored
43 char *tmpp;
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
44 cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
45
46 TRACE(ENTRIES, "Adding extensions: '%s'\n", value->buf);
47 cherokee_buffer_add_buffer (&tmp, value);
48
700f901 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1451 5dc97367-9…
alobbs authored
49 tmpp = tmp.buf;
50 while ((val = strsep(&tmpp, ",")) != NULL) {
51 TRACE(ENTRIES, "Adding extension: '%s'\n", val);
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
52 cherokee_avl_add_ptr (extensions, val, (void *)MAGIC);
53 }
54
55 cherokee_buffer_mrproper (&tmp);
56 return ret_ok;
57 }
58
fd30a37 @skarcha git-svn-id: svn://cherokee-project.com/cherokee/trunk@2393 5dc97367-9…
skarcha authored
59 static ret_t
60 configure (cherokee_rule_extensions_t *rule,
61 cherokee_config_node_t *conf,
b0dd10b @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1398 5dc97367-9…
alobbs authored
62 cherokee_virtual_server_t *vsrv)
63 {
64 ret_t ret;
65 cherokee_buffer_t *tmp = NULL;
66
651edbd @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1439 5dc97367-9…
alobbs authored
67 UNUSED(vsrv);
68
b0dd10b @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1398 5dc97367-9…
alobbs authored
69 ret = cherokee_config_node_read (conf, "extensions", &tmp);
70 if (ret != ret_ok) {
165f0ad @alobbs Merges the 'newerrors' branch (r3613:HEAD). It implements a new
alobbs authored
71 LOG_CRITICAL (CHEROKEE_ERROR_RULE_NO_PROPERTY,
72 RULE(rule)->priority, "extensions");
b0dd10b @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1398 5dc97367-9…
alobbs authored
73 return ret_error;
74 }
75
4aa95fd @alobbs Adds a new option to the Extensions rule matching plug-in. It allows
alobbs authored
76 cherokee_config_node_read_bool (conf, "check_local_file", &rule->check_local_file);
77 cherokee_config_node_read_bool (conf, "iocache", &rule->use_iocache);
78
b0dd10b @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1398 5dc97367-9…
alobbs authored
79 return parse_value (tmp, &rule->extensions);
80 }
81
9bf446d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1444 5dc97367-9…
alobbs authored
82 static ret_t
83 _free (void *p)
84 {
85 cherokee_rule_extensions_t *rule = RULE_EXTENSIONS(p);
86
120f7d3 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1447 5dc97367-9…
alobbs authored
87 cherokee_avl_mrproper (&rule->extensions, NULL);
9bf446d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1444 5dc97367-9…
alobbs authored
88 return ret_ok;
89 }
90
fd30a37 @skarcha git-svn-id: svn://cherokee-project.com/cherokee/trunk@2393 5dc97367-9…
skarcha authored
91 static ret_t
4aa95fd @alobbs Adds a new option to the Extensions rule matching plug-in. It allows
alobbs authored
92 local_file_exists (cherokee_rule_extensions_t *rule,
93 cherokee_connection_t *conn,
94 cherokee_config_entry_t *ret_conf)
95 {
96 ret_t ret;
97 struct stat *info;
98 struct stat nocache_info;
99 cherokee_boolean_t is_file;
100 cherokee_iocache_entry_t *io_entry = NULL;
101 cherokee_server_t *srv = CONN_SRV(conn);
102 cherokee_buffer_t *tmp = THREAD_TMP_BUF1(CONN_THREAD(conn));
103
104 UNUSED(rule);
105
106 /* Build the full path
107 */
108 cherokee_buffer_clean (tmp);
109
110 if (ret_conf->document_root != NULL) {
111 /* A previous non-final rule set a custom document root */
112 cherokee_buffer_add_buffer (tmp, ret_conf->document_root);
113 } else {
114 cherokee_buffer_add_buffer (tmp, &conn->local_directory);
115 }
116
268a01a @alobbs Fixes the Extensions Rule so the QA 251 passes successfully. There was
alobbs authored
117 cherokee_buffer_add_str (tmp, "/");
118
119 if (! cherokee_buffer_is_empty (&conn->web_directory)) {
120 cherokee_buffer_add (tmp,
121 conn->request.buf + conn->web_directory.len,
122 conn->request.len - conn->web_directory.len);
123 } else {
124 cherokee_buffer_add_buffer (tmp, &conn->request);
125 }
4aa95fd @alobbs Adds a new option to the Extensions rule matching plug-in. It allows
alobbs authored
126
127 /* Check the local file
128 */
129 ret = cherokee_io_stat (srv->iocache, tmp, rule->use_iocache,
130 &nocache_info, &io_entry, &info);
131
8814b7c @alobbs Fixes a memory management issue. The 'extensions' rule match could
alobbs authored
132 if (ret == ret_ok) {
133 is_file = S_ISREG(info->st_mode);
134 }
4aa95fd @alobbs Adds a new option to the Extensions rule matching plug-in. It allows
alobbs authored
135
136 if (io_entry) {
137 cherokee_iocache_entry_unref (&io_entry);
138 }
139
140 /* Report and return
141 */
142 if (ret != ret_ok) {
143 TRACE(ENTRIES, "Rule extensions: almost matched '%s', but file does not exist\n", tmp->buf);
144 return ret_not_found;
145 }
146
147 if (! is_file) {
148 TRACE(ENTRIES, "Rule extensions: almost matched '%s', but it is not a file\n", tmp->buf);
149 return ret_not_found;
150 }
151
152 return ret_ok;
153 }
154
155 static ret_t
4071d6a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3288 5dc97367-9…
alobbs authored
156 match (cherokee_rule_extensions_t *rule,
157 cherokee_connection_t *conn,
158 cherokee_config_entry_t *ret_conf)
700f901 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1451 5dc97367-9…
alobbs authored
159 {
160 ret_t ret;
161 char *dot;
0a2e178 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2761 5dc97367-9…
alobbs authored
162 char *slash;
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
163 char *end;
164 char *p;
700f901 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1451 5dc97367-9…
alobbs authored
165 void *foo;
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
166 char *dot_prev = NULL;
700f901 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1451 5dc97367-9…
alobbs authored
167
4071d6a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3288 5dc97367-9…
alobbs authored
168 UNUSED(ret_conf);
169
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
170 end = conn->request.buf + conn->request.len;
171 p = end - 1;
700f901 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1451 5dc97367-9…
alobbs authored
172
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
173 /* For each '.' */
174 while (p > conn->request.buf) {
175 if (*p != '.') {
176 p--;
177 continue;
178 }
fd30a37 @skarcha git-svn-id: svn://cherokee-project.com/cherokee/trunk@2393 5dc97367-9…
skarcha authored
179
6b6630a @skarcha git-svn-id: svn://cherokee-project.com/cherokee/trunk@3035 5dc97367-9…
skarcha authored
180 if (p[1] == '\0') {
181 p--;
182 continue;
183 }
184
185 if (p[1] == '/') {
186 p--;
187 continue;
188 }
189
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
190 dot = p;
191 slash = NULL;
192
193 /* Find a slash after the dot */
194 while (p < end) {
195 if (*p == '/') {
196 slash = p;
197 *p = '\0';
198 break;
199 }
200
201 p++;
202
203 if ((dot_prev != NULL) && (p >= dot_prev)) {
204 break;
205 }
6908568 @skarcha git-svn-id: svn://cherokee-project.com/cherokee/trunk@2921 5dc97367-9…
skarcha authored
206 }
0a2e178 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2761 5dc97367-9…
alobbs authored
207
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
208 /* Check it out */
209 ret = cherokee_avl_get_ptr (&rule->extensions, dot+1, &foo);
210 switch (ret) {
211 case ret_ok:
4aa95fd @alobbs Adds a new option to the Extensions rule matching plug-in. It allows
alobbs authored
212 if (rule->check_local_file) {
213 ret = local_file_exists (rule, conn, ret_conf);
214 if (ret != ret_ok) {
215 break;
216 }
217 }
218
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
219 TRACE(ENTRIES, "Match extension: '%s'\n", dot+1);
220 if (slash != NULL) {
221 *slash = '/';
222 }
223 return ret_ok;
224 case ret_not_found:
225 TRACE(ENTRIES, "Rule extension: did not match '%s'\n", dot+1);
226 break;
227 default:
228 conn->error_code = http_internal_error;
229 return ret_error;
230 }
231
232 /* Revert pathinfo match char
233 */
234 if (slash != NULL) {
0a2e178 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2761 5dc97367-9…
alobbs authored
235 *slash = '/';
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
236 }
237
238 dot_prev = dot;
239 p = dot - 1;
700f901 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1451 5dc97367-9…
alobbs authored
240 }
241
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
242 TRACE(ENTRIES, "Rule extension: nothing more to test '%s'\n", conn->request.buf);
243 return ret_not_found;
700f901 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1451 5dc97367-9…
alobbs authored
244 }
9bf446d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1444 5dc97367-9…
alobbs authored
245
2d8fa7d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3033 5dc97367-9…
alobbs authored
246
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
247 ret_t
b0dd10b @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1398 5dc97367-9…
alobbs authored
248 cherokee_rule_extensions_new (cherokee_rule_extensions_t **rule)
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
249 {
250 CHEROKEE_NEW_STRUCT (n, rule_extensions);
251
252 /* Parent class constructor
253 */
254 cherokee_rule_init_base (RULE(n), PLUGIN_INFO_PTR(extensions));
fd30a37 @skarcha git-svn-id: svn://cherokee-project.com/cherokee/trunk@2393 5dc97367-9…
skarcha authored
255
00dbf15 @alobbs Fixes a typo: s/methos/methods/g
alobbs authored
256 /* Virtual methods
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
257 */
b0dd10b @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1398 5dc97367-9…
alobbs authored
258 RULE(n)->match = (rule_func_match_t) match;
259 RULE(n)->configure = (rule_func_configure_t) configure;
9bf446d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1444 5dc97367-9…
alobbs authored
260 MODULE(n)->free = (module_func_free_t) _free;
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
261
262 /* Properties
263 */
4aa95fd @alobbs Adds a new option to the Extensions rule matching plug-in. It allows
alobbs authored
264 n->check_local_file = false;
265 n->use_iocache = true;
266
337832a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1375 5dc97367-9…
alobbs authored
267 cherokee_avl_init (&n->extensions);
268
269 *rule = n;
270 return ret_ok;
271 }
Something went wrong with that request. Please try again.