Security Plugin: set http status for URL rule match
[MonkeyD.git] / src / include / plugin.h
blobea9dcbd2dd75104bd699529106424582b75d123b
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "monkey.h"
23 #include "config.h"
24 #include "request.h"
25 #include "memory.h"
26 #include "iov.h"
27 #include "socket.h"
28 #include "epoll.h"
29 #include "http_status.h"
30 #include "utils.h"
32 #ifndef MK_PLUGIN_H
33 #define MK_PLUGIN_H
35 #define MK_PLUGIN_LOAD "plugins.load"
37 #define MK_PLUGIN_ERROR -1 /* plugin execution error */
38 #define MK_PLUGIN_
40 /* Plugin: Core types */
41 #define MK_PLUGIN_CORE_PRCTX (1)
42 #define MK_PLUGIN_CORE_THCTX (2)
44 /* Plugin: Stages */
45 #define MK_PLUGIN_STAGE_10 (4) /* Connection just accept()ed */
46 #define MK_PLUGIN_STAGE_20 (8) /* HTTP Request arrived */
47 #define MK_PLUGIN_STAGE_30 (16) /* Object handler */
48 #define MK_PLUGIN_STAGE_40 (32) /* Content served */
49 #define MK_PLUGIN_STAGE_50 (64) /* Conection ended */
51 /* Plugin: Network type */
52 #define MK_PLUGIN_NETWORK_IO (128)
53 #define MK_PLUGIN_NETWORK_IP (256)
55 /* Return values */
56 #define MK_PLUGIN_RET_NOT_ME -1
57 #define MK_PLUGIN_RET_CONTINUE 100
58 #define MK_PLUGIN_RET_END 200
59 #define MK_PLUGIN_RET_CLOSE_CONX 300
61 /* Event return values */
62 #define MK_PLUGIN_RET_EVENT_NOT_ME -300
64 /* NEW PROPOSAL */
65 struct plugin_core
67 int (*prctx) ();
68 int (*thctx) ();
71 struct plugin_stage
73 int (*s10) (int, struct sched_connection *);
74 int (*s20) (struct client_request *, struct request *);
75 int (*s30) (struct plugin *, struct client_request *, struct request *);
76 int (*s40) (struct client_request *, struct request *);
77 int (*s50) (int);
80 struct plugin_network_io
82 int (*accept) (int, struct sockaddr_in);
83 int (*read) (int, void *, int);
84 int (*write) (int, const void *, size_t);
85 int (*writev) (int, struct mk_iov *);
86 int (*close) (int);
87 int (*connect) (int, char *, int);
88 int (*send_file) (int, int, off_t *, size_t);
89 int (*create_socket) (int, int, int);
90 int (*bind) (int, const struct sockaddr *addr, socklen_t, int);
91 int (*server) (int, char *);
94 struct plugin_network_ip
96 int (*addr) (int);
97 int (*maxlen) ();
100 struct plugin
102 char *shortname;
103 char *name;
104 char *version;
105 char *path;
106 void *handler;
107 int *hooks;
109 /* Mandatory calls */
110 int (*init) (void *, char *);
111 int (*exit) ();
113 /* Hook functions by type */
114 struct plugin_core core;
115 struct plugin_stage stage;
116 struct plugin_network_io net_io;
117 struct plugin_network_ip net_ip;
119 /* Epoll Events */
120 int (*event_read) (int);
121 int (*event_write) (int);
122 int (*event_error) (int);
123 int (*event_close) (int);
124 int (*event_timeout) (int);
126 /* Each plugin has a thread key for it's global data */
127 pthread_key_t *thread_key;
129 /* Next! */
130 struct plugin *next;
134 /* Multiple plugins can work on multiple stages, we don't want
135 * Monkey be comparing each plugin looking for a specific stage,
136 * so we create a Map of direct stage calls
138 struct plugin_stagem
140 struct plugin *p;
141 struct plugin_stagem *next;
144 struct plugin_stagemap
146 struct plugin_stagem *stage_10;
147 struct plugin_stagem *stage_20;
148 struct plugin_stagem *stage_30;
149 struct plugin_stagem *stage_40;
150 struct plugin_stagem *stage_50;
153 struct plugin_stagemap *plg_stagemap;
155 /* Network map calls */
156 struct plugin_network_io *plg_netiomap;
157 struct plugin_network_ip *plg_netipmap;
159 /* API functions exported to plugins */
160 struct plugin_api
162 struct server_config *config;
163 struct plugin *plugins;
164 struct sched_list_node **sched_list;
166 /* HTTP request function */
167 int *(*http_request_end) (int);
169 /* memory functions */
170 void *(*mem_alloc) (int);
171 void *(*mem_alloc_z) (int);
172 void (*mem_free) (void *);
173 void (*pointer_set) (mk_pointer *, char *);
174 void (*pointer_print) (mk_pointer);
176 /* string functions */
177 char *(*str_build) (char **, unsigned long *, const char *, ...);
178 char *(*str_dup) (const char *);
179 int (*str_search) (char *, char *);
180 int (*str_search_n) (char *, char *, int);
181 char *(*str_copy_substr) (const char *, int, int);
182 int (*str_itop) (int, mk_pointer *);
183 struct mk_string_line *(*str_split_line) (const char *);
185 /* file functions */
186 char *(*file_to_buffer) (char *);
187 struct file_info *(*file_get_info) (char *);
188 int (*header_send) (int,
189 struct client_request *, struct request *);
190 void (*header_set_http_status) (struct request *, int);
192 /* iov functions */
193 struct mk_iov *(*iov_create) (int, int);
194 void (*iov_free) (struct mk_iov *);
195 int (*iov_add_entry) (struct mk_iov *, char *, int, mk_pointer, int);
196 int (*iov_set_entry) (struct mk_iov *, char *, int, int, int);
197 ssize_t (*iov_send) (int, struct mk_iov *, int);
198 void (*iov_print) (struct mk_iov *);
200 /* plugin functions */
201 void *(*plugin_load_symbol) (void *, char *);
203 /* epoll functions */
204 int (*epoll_create) (int);
205 void *(*epoll_init) (int, mk_epoll_handlers *, int);
206 int (*epoll_add) (int, int, int, int);
207 int (*epoll_del) (int, int);
208 int (*epoll_change_mode) (int, int, int);
210 /* socket functions */
211 int (*socket_cork_flag) (int, int);
212 int (*socket_reset) (int);
213 int (*socket_set_tcp_nodelay) (int);
214 int (*socket_connect) (int, char *, int);
215 int (*socket_set_nonblocking) (int);
216 int (*socket_create) ();
217 int (*socket_close) (int);
218 int (*socket_sendv) (int, struct mk_iov *, int);
219 int (*socket_send) (int, const void *, size_t);
220 int (*socket_read) (int, void *, int);
221 int (*socket_send_file) (int, int, off_t, size_t);
223 /* configuration reader functions */
224 struct mk_config *(*config_create) (char *);
225 void (*config_free) (struct mk_config *);
226 struct mk_config_section *(*config_section_get) (struct mk_config *,
227 char *);
228 void *(*config_section_getval) (struct mk_config_section *, char *, int);
229 struct sched_connection *(*sched_get_connection) (struct sched_list_node *,
230 int);
232 /* worker's functions */
233 int (*worker_spawn) (void (*func) (void *));
235 /* event's functions */
236 int (*event_add) (int, int, struct plugin *, struct client_request *,
237 struct request *);
238 int (*event_del) (int);
240 int (*event_socket_change_mode) (int, int);
242 /* system specific functions */
243 int (*sys_get_somaxconn)();
245 /* Time utils functions */
246 int (*time_unix)();
247 mk_pointer *(*time_human)();
249 #ifdef TRACE
250 void (*trace)();
251 #endif
255 typedef char mk_plugin_data_t[];
256 typedef int mk_plugin_hook_t;
257 typedef pthread_key_t mk_plugin_key_t;
259 /* Plugin events thread key */
260 pthread_key_t mk_plugin_event_k;
262 struct plugin_event {
263 int socket;
265 struct plugin *handler;
266 struct client_request *cr;
267 struct request *sr;
269 struct plugin_event *next;
272 void mk_plugin_init();
273 int mk_plugin_stage_run(mk_plugin_hook_t stage,
274 unsigned int socket,
275 struct sched_connection *conx,
276 struct client_request *cr, struct request *sr);
278 void mk_plugin_core_process();
279 void mk_plugin_core_thread();
281 void mk_plugin_request_handler_add(struct request *sr, struct plugin *p);
282 void mk_plugin_request_handler_del(struct request *sr, struct plugin *p);
284 void mk_plugin_preworker_calls();
286 /* Plugins events interface */
287 int mk_plugin_event_add(int socket, int mode,
288 struct plugin *handler,
289 struct client_request *cr,
290 struct request *sr);
291 int mk_plugin_event_del(int socket);
293 int mk_plugin_event_set_list(struct plugin_event *event);
294 struct plugin_event *mk_plugin_event_get_list();
295 int mk_plugin_event_socket_change_mode(int socket, int mode);
297 /* Plugins event handlers */
298 int mk_plugin_event_read(int socket);
299 int mk_plugin_event_write(int socket);
300 int mk_plugin_event_error(int socket);
301 int mk_plugin_event_close(int socket);
302 int mk_plugin_event_timeout(int socket);
304 void mk_plugin_register_to(struct plugin **st, struct plugin *p);
305 void *mk_plugin_load_symbol(void *handler, const char *symbol);
306 int mk_plugin_http_request_end(int socket);
308 /* Register functions */
309 struct plugin *mk_plugin_register(struct plugin *p);
310 void mk_plugin_unregister(struct plugin *p);
312 struct plugin *mk_plugin_alloc(void *handler, char *path);
313 void mk_plugin_free(struct plugin *p);
315 int mk_plugin_time_now_unix();
316 mk_pointer *mk_plugin_time_now_human();
318 #endif