/
handler_admin.c
299 lines (239 loc) · 7.84 KB
/
handler_admin.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
/* -*- 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 "handler_admin.h"
#include "request.h"
#include "request-protected.h"
#include "server.h"
#include "server-protected.h"
#include "admin_server.h"
#include "util.h"
#define ENTRIES "handler,admin"
/* Plug-in initialization
*/
PLUGIN_INFO_HANDLER_EASIEST_INIT (admin, http_get | http_post | http_purge);
/* Methods implementation
*/
ret_t
cherokee_handler_admin_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
{
UNUSED(conf);
UNUSED(srv);
UNUSED(_props);
return ret_ok;
}
ret_t
cherokee_handler_admin_new (cherokee_handler_t **hdl, void *cnt, cherokee_module_props_t *props)
{
CHEROKEE_NEW_STRUCT (n, handler_admin);
/* Init the base class object
*/
cherokee_handler_init_base (HANDLER(n), cnt, HANDLER_PROPS(props), PLUGIN_INFO_HANDLER_PTR(admin));
MODULE(n)->init = (module_func_init_t) cherokee_handler_admin_init;
MODULE(n)->free = (module_func_free_t) cherokee_handler_admin_free;
HANDLER(n)->add_headers = (handler_func_add_headers_t) cherokee_handler_admin_add_headers;
HANDLER(n)->read_post = (handler_func_read_post_t) cherokee_handler_admin_read_post;
HANDLER(n)->step = (handler_func_step_t) cherokee_handler_admin_step;
/* Supported features
*/
HANDLER(n)->support = hsupport_nothing;
cherokee_buffer_init (&n->reply);
/* Configure the data writer object
*/
cherokee_dwriter_init (&n->dwriter, THREAD_TMP_BUF1(REQ_THREAD(cnt)));
cherokee_dwriter_set_buffer (&n->dwriter, &n->reply);
/* Return the object
*/
*hdl = HANDLER(n);
return ret_ok;
}
ret_t
cherokee_handler_admin_free (cherokee_handler_admin_t *hdl)
{
cherokee_buffer_mrproper (&hdl->reply);
cherokee_dwriter_mrproper (&hdl->dwriter);
return ret_ok;
}
static ret_t
process_request_line (cherokee_handler_admin_t *hdl, cherokee_buffer_t *line)
{
#define COMP(str,sub) (strncmp(str, sub, sizeof(sub)-1) == 0)
if (COMP (line->buf, "get server.ports")) {
return cherokee_admin_server_reply_get_ports (HANDLER(hdl), &hdl->dwriter);
} else if (COMP (line->buf, "get server.traffic")) {
return cherokee_admin_server_reply_get_traffic (HANDLER(hdl), &hdl->dwriter);
} else if (COMP (line->buf, "get server.thread_num")) {
return cherokee_admin_server_reply_get_thread_num (HANDLER(hdl), &hdl->dwriter);
} else if (COMP (line->buf, "get server.trace")) {
return cherokee_admin_server_reply_get_trace (HANDLER(hdl), &hdl->dwriter);
} else if (COMP (line->buf, "set server.trace")) {
return cherokee_admin_server_reply_set_trace (HANDLER(hdl), &hdl->dwriter, line);
} else if (COMP (line->buf, "get server.sources")) {
return cherokee_admin_server_reply_get_sources (HANDLER(hdl), &hdl->dwriter);
} else if (COMP (line->buf, "kill server.source")) {
return cherokee_admin_server_reply_kill_source (HANDLER(hdl), &hdl->dwriter, line);
} else if (COMP (line->buf, "set server.backup_mode")) {
return cherokee_admin_server_reply_set_backup_mode (HANDLER(hdl), &hdl->dwriter, line);
} else if (COMP (line->buf, "get server.connections")) {
return cherokee_admin_server_reply_get_conns (HANDLER(hdl), &hdl->dwriter);
} else if (COMP (line->buf, "close server.connection")) {
return cherokee_admin_server_reply_close_conn (HANDLER(hdl), &hdl->dwriter, line);
}
SHOULDNT_HAPPEN;
return ret_error;
}
static ret_t
front_line_cache_purge (cherokee_handler_admin_t *hdl)
{
ret_t ret;
cherokee_request_t *conn = HANDLER_REQ(hdl);
cherokee_virtual_server_t *vsrv = HANDLER_VSRV(hdl);
/* FLCache not active in the Virtual Server
*/
if (vsrv->flcache == NULL) {
conn->error_code = http_not_found;
return ret_error;
}
/* Remove the cache objects
*/
ret = cherokee_flcache_purge_path (vsrv->flcache, &conn->request);
switch (ret) {
case ret_ok:
cherokee_dwriter_cstring (&hdl->dwriter, "ok");
return ret_ok;
case ret_not_found:
cherokee_dwriter_cstring (&hdl->dwriter, "not found");
conn->error_code = http_not_found;
return ret_error;
default:
cherokee_dwriter_cstring (&hdl->dwriter, "error");
conn->error_code = http_internal_error;
return ret_error;
}
SHOULDNT_HAPPEN;
return ret_error;
}
ret_t
cherokee_handler_admin_init (cherokee_handler_admin_t *hdl)
{
cherokee_request_t *conn = HANDLER_REQ(hdl);
#define finishes_by(s) ((conn->request.len > sizeof(s)-1) && \
(!strncmp (conn->request.buf + conn->request.len - (sizeof(s)-1), s, sizeof(s)-1)))
if (finishes_by ("/py")) {
hdl->dwriter.lang = dwriter_python;
} else if (finishes_by ("/js")) {
hdl->dwriter.lang = dwriter_json;
} else if (finishes_by ("/php")) {
hdl->dwriter.lang = dwriter_php;
} else if (finishes_by ("/ruby")) {
hdl->dwriter.lang = dwriter_ruby;
}
/* Front-Line Cache's PURGE
*/
if (conn->header.method == http_purge) {
return front_line_cache_purge (hdl);
}
#undef finishes_by
return ret_ok;
}
ret_t
cherokee_handler_admin_read_post (cherokee_handler_admin_t *hdl)
{
int re;
ret_t ret;
char *tmp;
cherokee_buffer_t post = CHEROKEE_BUF_INIT;
cherokee_buffer_t line = CHEROKEE_BUF_INIT;
cherokee_request_t *conn = HANDLER_REQ(hdl);
/* Check for the post info
*/
if (! conn->post.has_info) {
conn->error_code = http_bad_request;
return ret_error;
}
/* Process line per line
*/
ret = cherokee_post_read (&conn->post, &conn->socket, &post);
switch (ret) {
case ret_ok:
case ret_eagain:
break;
default:
conn->error_code = http_bad_request;
return ret_error;
}
/* Parse
*/
TRACE (ENTRIES, "Post contains: '%s'\n", post.buf);
cherokee_dwriter_list_open (&hdl->dwriter);
for (tmp = post.buf;;) {
char *end1 = strchr (tmp, CHR_LF);
char *end2 = strchr (tmp, CHR_CR);
char *end = cherokee_min_str (end1, end2);
if (end == NULL) break;
if (end - tmp < 2) break;
/* Copy current line and go to the next one
*/
cherokee_buffer_add (&line, tmp, end - tmp);
while ((*end == CHR_CR) || (*end == CHR_LF)) end++;
tmp = end;
/* Process current line
*/
ret = process_request_line (hdl, &line);
if (ret == ret_error) {
conn->error_code = http_bad_request;
ret = ret_error;
goto exit2;
}
/* Clean up for the next iteration
*/
cherokee_buffer_clean (&line);
}
cherokee_dwriter_list_close (&hdl->dwriter);
/* There might be more POST to read
*/
re = cherokee_post_read_finished (&conn->post);
ret = re ? ret_ok : ret_eagain;
exit2:
cherokee_buffer_mrproper (&post);
cherokee_buffer_mrproper (&line);
return ret;
}
ret_t
cherokee_handler_admin_add_headers (cherokee_handler_admin_t *hdl, cherokee_buffer_t *buffer)
{
cherokee_request_t *conn = HANDLER_REQ(hdl);
/* Regular request
*/
if (cherokee_request_should_include_length(conn)) {
HANDLER(hdl)->support = hsupport_length;
cherokee_buffer_add_va (buffer, "Content-Length: %lu" CRLF, hdl->reply.len);
}
return ret_ok;
}
ret_t
cherokee_handler_admin_step (cherokee_handler_admin_t *hdl, cherokee_buffer_t *buffer)
{
cherokee_buffer_add_buffer (buffer, &hdl->reply);
return ret_eof_have_data;
}