/
handler_common.c
356 lines (277 loc) · 10.1 KB
/
handler_common.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2014 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 "handler_common.h"
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include "util.h"
#include "connection.h"
#include "connection-protected.h"
#include "server.h"
#include "server-protected.h"
#include "module.h"
#include "connection.h"
#define ENTRIES "handler,common"
#define DEFAULT_ALLOW_PATHINFO false
#define DEFAULT_ALLOW_DIRLIST true
ret_t
cherokee_handler_common_props_free (cherokee_handler_common_props_t *props)
{
if (props->props_file != NULL) {
cherokee_handler_file_props_free (props->props_file);
props->props_file = NULL;
}
if (props->props_dirlist != NULL) {
cherokee_handler_dirlist_props_free (props->props_dirlist);
props->props_dirlist = NULL;
}
return cherokee_handler_props_free_base (HANDLER_PROPS(props));
}
ret_t
cherokee_handler_common_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
{
ret_t ret;
cherokee_config_node_t *subconf;
cherokee_handler_common_props_t *props;
if (*_props == NULL) {
CHEROKEE_NEW_STRUCT (n, handler_common_props);
cherokee_handler_props_init_base (HANDLER_PROPS(n),
MODULE_PROPS_FREE(cherokee_handler_common_props_free));
n->props_file = NULL;
n->props_dirlist = NULL;
n->allow_pathinfo = DEFAULT_ALLOW_PATHINFO;
n->allow_dirlist = DEFAULT_ALLOW_DIRLIST;
*_props = MODULE_PROPS(n);
}
props = PROP_COMMON(*_props);
/* Parse 'common' parameters
*/
ret = cherokee_config_node_get (conf, "allow_pathinfo", &subconf);
if (ret == ret_ok) {
ret = cherokee_atob (subconf->val.buf, &props->allow_pathinfo);
if (ret != ret_ok) return ret;
}
ret = cherokee_config_node_get (conf, "allow_dirlist", &subconf);
if (ret == ret_ok) {
ret = cherokee_atob (subconf->val.buf, &props->allow_dirlist);
if (ret != ret_ok) return ret;
}
/* Parse 'file' parameters
*/
ret = cherokee_handler_file_configure (conf, srv, (cherokee_module_props_t **)&props->props_file);
if ((ret != ret_ok) && (ret != ret_deny)) return ret;
/* Parse 'dirlist' parameters
*/
return cherokee_handler_dirlist_configure (conf, srv, (cherokee_module_props_t **)&props->props_dirlist);
}
ret_t
cherokee_handler_common_new (cherokee_handler_t **hdl, void *cnt, cherokee_module_props_t *props)
{
ret_t ret;
int exists;
struct stat nocache_info;
struct stat *info;
cherokee_iocache_entry_t *io_entry = NULL;
cherokee_iocache_t *iocache = NULL;
cherokee_boolean_t use_iocache = true;
cherokee_connection_t *conn = CONN(cnt);
TRACE_CONN(conn);
/* Check some properties
*/
if (PROP_COMMON(props)->props_file != NULL) {
use_iocache = PROP_COMMON(props)->props_file->use_cache;
}
use_iocache &= (CONN_SRV(cnt)->iocache != NULL);
/* Check the request
*/
cherokee_buffer_add_buffer (&conn->local_directory, &conn->request);
if (use_iocache)
iocache = CONN_SRV(conn)->iocache;
ret = cherokee_io_stat (iocache, &conn->local_directory, use_iocache, &nocache_info, &io_entry, &info);
exists = (ret == ret_ok);
TRACE (ENTRIES, "request: '%s', local: '%s', exists %d\n",
conn->request.buf, conn->local_directory.buf, exists);
if (!exists) {
ret_t ret;
char *pathinfo;
int pathinfo_len;
int begin;
/* If PathInfo is not allowed just return 'Not Found'
*/
if (! PROP_COMMON(props)->allow_pathinfo) {
TRACE(ENTRIES, "Returns conn->error_code: %s\n", "http_not_found");
cherokee_iocache_entry_unref (&io_entry);
conn->error_code = http_not_found;
return ret_error;
}
/* Maybe it could stat() the file because the request contains
* a PathInfo string at the end..
*/
begin = conn->local_directory.len - conn->request.len;
ret = cherokee_split_pathinfo (&conn->local_directory, begin, true, &pathinfo, &pathinfo_len);
if ((ret == ret_not_found) || (pathinfo_len <= 0)) {
TRACE(ENTRIES, "Returns conn->error_code: %s\n", "http_not_found");
cherokee_iocache_entry_unref (&io_entry);
conn->error_code = http_not_found;
return ret_error;
}
/* Copy the PathInfo and clean the request
*/
if (cherokee_buffer_is_empty (&conn->request_original)) {
cherokee_buffer_add_buffer (&conn->request_original, &conn->request);
cherokee_buffer_add_buffer (&conn->query_string_original, &conn->query_string);
}
cherokee_buffer_add (&conn->pathinfo, pathinfo, pathinfo_len);
cherokee_buffer_drop_ending (&conn->request, pathinfo_len);
/* Clean the local_directory, this connection is going
* to restart the connection setup phase
*/
cherokee_buffer_clean (&conn->local_directory);
cherokee_iocache_entry_unref (&io_entry);
TRACE_CONN(conn);
return ret_eagain;
}
cherokee_buffer_drop_ending (&conn->local_directory, conn->request.len);
/* Is it a file?
*/
if (S_ISREG(info->st_mode)) {
TRACE (ENTRIES, "going for %s\n", "handler_file");
cherokee_iocache_entry_unref (&io_entry);
return cherokee_handler_file_new (hdl, cnt, MODULE_PROPS(PROP_COMMON(props)->props_file));
}
/* Is it a directory
*/
if (S_ISDIR(info->st_mode)) {
cherokee_thread_t *thread = CONN_THREAD(conn);
cherokee_list_t *i;
cherokee_iocache_entry_unref (&io_entry);
/* Maybe it has to be redirected
*/
if (cherokee_buffer_end_char (&conn->request) != '/') {
TRACE (ENTRIES, "going for %s\n", "handler_dir");
return cherokee_handler_dirlist_new (hdl, cnt, MODULE_PROPS(PROP_COMMON(props)->props_dirlist));
}
/* Add the request
*/
cherokee_buffer_add_buffer (&conn->local_directory, &conn->request);
/* Have an index file inside?
*/
list_for_each (i, &CONN_VSRV(conn)->index_list) {
int is_dir;
cherokee_buffer_t *index = BUF(LIST_ITEM_INFO(i));
/* Check if the index is fullpath
*/
if (index->buf[0] == '/') {
cherokee_buffer_t *new_local_dir = THREAD_TMP_BUF1(thread);
/* This means there is a configuration entry like:
* 'DirectoryIndex index.php, /index_default.php'
* and it's proceesing '/index_default.php'.
*/
/* Build the secondary path
*/
cherokee_buffer_add_buffer (&conn->effective_directory, &conn->local_directory);
/* Lets reconstruct the local directory
*/
cherokee_buffer_clean (new_local_dir);
cherokee_buffer_add_buffer (new_local_dir, &CONN_VSRV(conn)->root);
cherokee_buffer_add_buffer (new_local_dir, index);
ret = cherokee_io_stat (iocache, new_local_dir, use_iocache, &nocache_info, &io_entry, &info);
exists = (ret == ret_ok);
cherokee_iocache_entry_unref (&io_entry);
if (!exists)
continue;
/* Build the new request before respin
*/
cherokee_buffer_clean (&conn->local_directory);
if (cherokee_buffer_is_empty (&conn->request_original)) {
cherokee_buffer_add_buffer (&conn->request_original, &conn->request);
cherokee_buffer_add_buffer (&conn->query_string_original, &conn->query_string);
}
cherokee_buffer_clean (&conn->request);
cherokee_buffer_add_buffer (&conn->request, index);
TRACE (ENTRIES, "top level index matched %s\n", index->buf);
BIT_SET (conn->options, conn_op_root_index);
TRACE_CONN(conn);
return ret_eagain;
}
/* stat() the possible new path
*/
cherokee_buffer_add_buffer (&conn->local_directory, index);
ret = cherokee_io_stat (iocache, &conn->local_directory, use_iocache, &nocache_info, &io_entry, &info);
exists = (ret == ret_ok);
is_dir = ((ret == ret_ok) && S_ISDIR(info->st_mode));
cherokee_iocache_entry_unref (&io_entry);
cherokee_buffer_drop_ending (&conn->local_directory, index->len);
TRACE (ENTRIES, "trying index '%s', exists %d\n", index->buf, exists);
/* If the file doesn't exist or it is a directory, try with the next one
*/
if ((!exists) || (is_dir))
continue;
/* Add the index file to the request and clean up
*/
cherokee_buffer_drop_ending (&conn->local_directory, conn->request.len);
if (cherokee_buffer_is_empty (&conn->request_original)) {
cherokee_buffer_add_buffer (&conn->request_original, &conn->request);
cherokee_buffer_add_buffer (&conn->query_string_original, &conn->query_string);
}
cherokee_buffer_add_buffer (&conn->request, index);
TRACE_CONN(conn);
return ret_eagain;
}
/* If the dir hasn't a index file, it uses dirlist
*/
cherokee_buffer_drop_ending (&conn->local_directory, conn->request.len);
if (PROP_COMMON(props)->allow_dirlist) {
return cherokee_handler_dirlist_new (hdl, cnt,
MODULE_PROPS(PROP_COMMON(props)->props_dirlist));
}
conn->error_code = http_access_denied;
return ret_error;
}
/* Unknown request type
*/
TRACE(ENTRIES, "Returns conn->error_code: %s\n", "http_unsupported_media_type");
conn->error_code = http_unsupported_media_type;
return ret_error;
}
/* Library init function
*/
static cherokee_boolean_t _common_is_init = false;
void
PLUGIN_INIT_NAME(common) (cherokee_plugin_loader_t *loader)
{
if (_common_is_init) return;
_common_is_init = true;
/* Load the dependences
*/
cherokee_plugin_loader_load (loader, "file");
cherokee_plugin_loader_load (loader, "dirlist");
}
PLUGIN_INFO_HANDLER_EASY_INIT (common, http_all_methods);