Kernelcmdline, end for rrd
[handlervirt.git] / handler_example.c
blobc31366e22e834bd88f0a3b5f68561231a3d98072
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* Cherokee
5 * Authors:
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 * Stefan de Konink <stefan@konink.de>
9 * Copyright (C) 2001-2008 Alvaro Lopez Ortega
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
26 #include "handler_example.h"
27 #include <cherokee/cherokee.h>
30 #include "util.h"
31 #include "connection.h"
32 #include "connection-protected.h"
33 #include "server.h"
34 #include "server-protected.h"
35 #include "plugin_loader.h"
36 #include "connection_info.h"
39 /* Plug-in initialization
41 * In this function you can use any of these:
42 * http_delete | http_get | http_post | http_put
44 * For a full list: cherokee_http_method_t
46 * It is what your handler to be implements.
49 PLUGIN_INFO_HANDLER_EASIEST_INIT (example, http_get);
52 /* Methods implementation
54 static ret_t
55 props_free (cherokee_handler_example_props_t *props)
57 return cherokee_module_props_free_base (MODULE_PROPS(props));
61 ret_t
62 cherokee_handler_example_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
64 cherokee_list_t *i;
65 cherokee_handler_example_props_t *props;
67 if (*_props == NULL) {
68 CHEROKEE_NEW_STRUCT (n, handler_example_props);
70 cherokee_module_props_init_base (MODULE_PROPS(n),
71 MODULE_PROPS_FREE(props_free));
73 /* Look at handler_example.h
74 * This is an example of configuration.
76 n->example_config = false;
78 *_props = MODULE_PROPS(n);
81 props = PROP_EXAMPLE(*_props);
83 cherokee_config_node_foreach (i, conf) {
84 cherokee_config_node_t *subconf = CONFIG_NODE(i);
86 if (equal_buf_str (&subconf->key, "example_config")) {
87 props->example_config = atoi(subconf->val.buf);
88 } else {
89 PRINT_MSG ("ERROR: Handler file: Unknown key: '%s'\n", subconf->key.buf);
90 return ret_error;
94 return ret_ok;
97 ret_t
98 cherokee_handler_example_new (cherokee_handler_t **hdl, cherokee_connection_t *cnt, cherokee_module_props_t *props)
100 ret_t ret;
101 CHEROKEE_NEW_STRUCT (n, handler_example);
103 /* Init the base class object
105 cherokee_handler_init_base(HANDLER(n), cnt, HANDLER_PROPS(props), PLUGIN_INFO_HANDLER_PTR(example));
107 MODULE(n)->init = (handler_func_init_t) cherokee_handler_example_init;
108 MODULE(n)->free = (module_func_free_t) cherokee_handler_example_free;
109 HANDLER(n)->step = (handler_func_step_t) cherokee_handler_example_step;
110 HANDLER(n)->add_headers = (handler_func_add_headers_t) cherokee_handler_example_add_headers;
112 HANDLER(n)->support = hsupport_length | hsupport_range;
114 /* Init
116 ret = cherokee_buffer_init (&n->buffer);
117 if (unlikely(ret != ret_ok))
118 return ret;
120 ret = cherokee_buffer_ensure_size (&n->buffer, 4*1024);
121 if (unlikely(ret != ret_ok))
122 return ret;
124 *hdl = HANDLER(n);
125 return ret_ok;
129 ret_t
130 cherokee_handler_example_free (cherokee_handler_example_t *hdl)
132 cherokee_buffer_mrproper (&hdl->buffer);
133 return ret_ok;
136 static void
137 example_build_page (cherokee_handler_example_t *hdl)
139 ret_t ret;
140 cherokee_server_t *srv;
141 cherokee_buffer_t *buf;
143 /* Init
145 buf = &hdl->buffer;
146 srv = HANDLER_SRV(hdl);
148 /* Useful output
150 cherokee_buffer_add_str (buf, "Hello World");
152 /* But we did configure something!
154 if (HDL_EXAMPLE_PROPS(hdl)->example_config) {
155 cherokee_buffer_add_str (buf, "!!!");
159 ret_t
160 cherokee_handler_example_init (cherokee_handler_example_t *hdl)
162 ret_t ret;
163 void *param;
164 cint_t web_interface = 1;
166 /* Build the page
168 if (web_interface) {
169 example_build_page (hdl);
172 hdl->action = send_page;
174 return ret_ok;
178 ret_t
179 cherokee_handler_example_step (cherokee_handler_example_t *hdl, cherokee_buffer_t *buffer)
181 cherokee_buffer_add_buffer (buffer, &hdl->buffer);
182 return ret_eof_have_data;
186 ret_t
187 cherokee_handler_example_add_headers (cherokee_handler_example_t *hdl, cherokee_buffer_t *buffer)
189 cherokee_buffer_add_va (buffer, "Content-Length: %d"CRLF, hdl->buffer.len);
191 switch (hdl->action) {
192 case send_page:
193 default:
194 cherokee_buffer_add_str (buffer, "Content-Type: text/html"CRLF);
195 break;
198 return ret_ok;