1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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
26 #include "handler_example.h"
27 #include <cherokee/cherokee.h>
31 #include "connection.h"
32 #include "connection-protected.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
55 props_free (cherokee_handler_example_props_t
*props
)
57 return cherokee_module_props_free_base (MODULE_PROPS(props
));
62 cherokee_handler_example_configure (cherokee_config_node_t
*conf
, cherokee_server_t
*srv
, cherokee_module_props_t
**_props
)
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
);
89 PRINT_MSG ("ERROR: Handler file: Unknown key: '%s'\n", subconf
->key
.buf
);
98 cherokee_handler_example_new (cherokee_handler_t
**hdl
, cherokee_connection_t
*cnt
, cherokee_module_props_t
*props
)
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
;
116 ret
= cherokee_buffer_init (&n
->buffer
);
117 if (unlikely(ret
!= ret_ok
))
120 ret
= cherokee_buffer_ensure_size (&n
->buffer
, 4*1024);
121 if (unlikely(ret
!= ret_ok
))
130 cherokee_handler_example_free (cherokee_handler_example_t
*hdl
)
132 cherokee_buffer_mrproper (&hdl
->buffer
);
137 example_build_page (cherokee_handler_example_t
*hdl
)
140 cherokee_server_t
*srv
;
141 cherokee_buffer_t
*buf
;
146 srv
= HANDLER_SRV(hdl
);
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
, "!!!");
160 cherokee_handler_example_init (cherokee_handler_example_t
*hdl
)
164 cint_t web_interface
= 1;
169 example_build_page (hdl
);
172 hdl
->action
= send_page
;
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
;
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
) {
194 cherokee_buffer_add_str (buffer
, "Content-Type: text/html"CRLF
);