xref: /lighttpd1.4/src/plugin.c (revision 3bc7866f)
1 #include "first.h"
2 
3 #include "plugins.h"
4 #include "plugin.h"
5 #include "base.h"
6 #include "array.h"
7 #include "log.h"
8 
9 #include <string.h>
10 #include <stdlib.h>
11 
12 #ifdef HAVE_VALGRIND_VALGRIND_H
13 # include <valgrind/valgrind.h>
14 #endif
15 
16 #ifndef LIGHTTPD_STATIC
17 #ifdef HAVE_DLFCN_H
18 #include <dlfcn.h>
19 #endif
20 #endif
21 /*
22  *
23  * if you change this enum to add a new callback, be sure
24  * - that PLUGIN_FUNC_SIZEOF is the last entry
25  * - that you add:
26  *   1. PLUGIN_CALL_... as callback-dispatcher
27  *   2. count and assignment in plugins_call_init()
28  *
29  */
30 
31 typedef enum {
32 	PLUGIN_FUNC_HANDLE_URI_CLEAN,
33 	PLUGIN_FUNC_HANDLE_DOCROOT,
34 	PLUGIN_FUNC_HANDLE_PHYSICAL,
35 	PLUGIN_FUNC_HANDLE_SUBREQUEST_START,
36 	/* PLUGIN_FUNC_HANDLE_SUBREQUEST, *//* max one handler_module per req */
37 	PLUGIN_FUNC_HANDLE_RESPONSE_START,
38 	PLUGIN_FUNC_HANDLE_REQUEST_DONE,
39 	PLUGIN_FUNC_HANDLE_REQUEST_RESET,
40 	PLUGIN_FUNC_HANDLE_REQUEST_ENV,
41 	PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT,
42 	PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR,
43 	PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE,
44 	PLUGIN_FUNC_HANDLE_TRIGGER,
45 	PLUGIN_FUNC_HANDLE_WAITPID,
46 	PLUGIN_FUNC_HANDLE_SIGHUP,
47 	/* PLUGIN_FUNC_INIT, *//* handled here in plugin.c */
48 	/* PLUGIN_FUNC_CLEANUP, *//* handled here in plugin.c */
49 	PLUGIN_FUNC_SET_DEFAULTS,
50 	PLUGIN_FUNC_WORKER_INIT,
51 
52 	PLUGIN_FUNC_SIZEOF
53 } plugin_t;
54 
55 __attribute_malloc__
56 __attribute_returns_nonnull__
57 static plugin *plugin_init(void) {
58 	plugin *p;
59 
60 	p = calloc(1, sizeof(*p));
61 	force_assert(NULL != p);
62 
63 	return p;
64 }
65 
66 static void plugin_free(plugin *p) {
67     if (NULL == p) return; /*(should not happen w/ current usage)*/
68   #if !defined(LIGHTTPD_STATIC)
69     if (p->lib) {
70      #if defined(HAVE_VALGRIND_VALGRIND_H)
71      /*if (!RUNNING_ON_VALGRIND) */
72      #endif
73       #ifdef _WIN32
74         FreeLibrary(p->lib);
75       #else
76         dlclose(p->lib);
77       #endif
78     }
79   #endif
80 
81     free(p);
82 }
83 
84 static void plugins_register(server *srv, plugin *p) {
85 	plugin **ps;
86 	if (srv->plugins.used == srv->plugins.size) {
87 		srv->plugins.size += 4;
88 		srv->plugins.ptr   = realloc(srv->plugins.ptr, srv->plugins.size * sizeof(*ps));
89 		force_assert(NULL != srv->plugins.ptr);
90 	}
91 
92 	ps = srv->plugins.ptr;
93 	ps[srv->plugins.used++] = p;
94 }
95 
96 #ifdef _WIN32
97 __attribute_cold__
98 static void
99 log_w32_syserror_2 (log_error_st *const errh, const char *file, const int line, const char * const str1, const char * const str2)
100 {
101     TCHAR lpMsgBuf[1024];
102     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
103                   0, /* MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) */
104                   (LPTSTR)lpMsgBuf, sizeof(lpMsgBuf)/sizeof(TCHAR), NULL);
105     log_error(errh, file, line, "%s for %s: %s", str1, str2, (char *)lpMsgBuf);
106 }
107 #endif
108 
109 /**
110  *
111  *
112  *
113  */
114 
115 #if defined(LIGHTTPD_STATIC)
116 
117 /* pre-declare functions, as there is no header for them */
118 #define PLUGIN_INIT(x)\
119 	int x ## _plugin_init(plugin *p);
120 
121 #include "plugin-static.h"
122 
123 #undef PLUGIN_INIT
124 
125 /* build NULL-terminated table of name + init-function */
126 
127 typedef struct {
128 	const char* name;
129 	int (*plugin_init)(plugin *p);
130 } plugin_load_functions;
131 
132 static const plugin_load_functions load_functions[] = {
133 #define PLUGIN_INIT(x) \
134 	{ #x, &x ## _plugin_init },
135 
136 #include "plugin-static.h"
137 
138 	{ NULL, NULL }
139 #undef PLUGIN_INIT
140 };
141 
142 int plugins_load(server *srv) {
143 	for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) {
144 		data_string *ds = (data_string *)srv->srvconf.modules->data[i];
145 		char *module = ds->value.ptr;
146 
147 		uint32_t j;
148 		for (j = 0; load_functions[j].name; ++j) {
149 			if (0 == strcmp(load_functions[j].name, module)) {
150 				plugin * const p = plugin_init();
151 				if ((*load_functions[j].plugin_init)(p)) {
152 					log_error(srv->errh, __FILE__, __LINE__, "%s plugin init failed", module);
153 					plugin_free(p);
154 					return -1;
155 				}
156 				plugins_register(srv, p);
157 				break;
158 			}
159 		}
160 		if (!load_functions[j].name) {
161 			log_error(srv->errh, __FILE__, __LINE__, "%s plugin not found", module);
162 			if (srv->srvconf.compat_module_load) {
163 				if (buffer_eq_slen(&ds->value, CONST_STR_LEN("mod_deflate")))
164 					continue;
165 			}
166 			return -1;
167 		}
168 	}
169 
170 	return 0;
171 }
172 
173 #else /* defined(LIGHTTPD_STATIC) */
174 
175 int plugins_load(server *srv) {
176 	buffer * const tb = srv->tmp_buf;
177 	int (*init)(plugin *pl);
178 
179 	for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) {
180 		const buffer * const module = &((data_string *)srv->srvconf.modules->data[i])->value;
181 		void *lib = NULL;
182 
183 		/* check if module is built-in to main executable */
184 		buffer_clear(tb);
185 		buffer_append_str2(tb, BUF_PTR_LEN(module),
186 		                       CONST_STR_LEN("_plugin_init"));
187 	  #ifdef _WIN32
188 		init = (int(WINAPI *)(plugin *))(intptr_t)
189 		  GetProcAddress(GetModuleHandle(NULL), tb->ptr);
190 	  #else
191 		init = (int (*)(plugin *))(intptr_t)dlsym(RTLD_DEFAULT, tb->ptr);
192 	  #endif
193 
194 	  if (NULL == init) {
195 		buffer_copy_string(tb, srv->srvconf.modules_dir);
196 		buffer_append_path_len(tb, BUF_PTR_LEN(module));
197 
198 	  #ifdef _WIN32
199 		buffer_append_string_len(tb, CONST_STR_LEN(".dll"));
200 		if (NULL == (lib = LoadLibrary(tb->ptr))) {
201 			log_w32_syserror_2(srv->errh, __FILE__, __LINE__,
202 			                   "LoadLibrary()", tb->ptr);
203 			if (srv->srvconf.compat_module_load) {
204 				if (buffer_eq_slen(module, CONST_STR_LEN("mod_deflate")))
205 					continue;
206 			}
207 			return -1;
208 		}
209 		buffer_copy_buffer(tb, module);
210 		buffer_append_string_len(tb, CONST_STR_LEN("_plugin_init"));
211 		init = (int(WINAPI *)(plugin *))(intptr_t)GetProcAddress(lib, tb->ptr);
212 		if (init == NULL) {
213 			log_w32_syserror_2(srv->errh, __FILE__, __LINE__,
214 			                   "GetProcAddress()", tb->ptr);
215 		        FreeLibrary(lib);
216 			return -1;
217 		}
218 	  #else
219 	   #if defined(__CYGWIN__)
220 		buffer_append_string_len(tb, CONST_STR_LEN(".dll"));
221 	   #else
222 		buffer_append_string_len(tb, CONST_STR_LEN(".so"));
223 	   #endif
224 		if (NULL == (lib = dlopen(tb->ptr, RTLD_NOW|RTLD_GLOBAL))) {
225 			log_error(srv->errh, __FILE__, __LINE__,
226 			  "dlopen() failed for: %s %s", tb->ptr, dlerror());
227 			if (srv->srvconf.compat_module_load) {
228 				if (buffer_eq_slen(module, CONST_STR_LEN("mod_deflate")))
229 					continue;
230 			}
231 			return -1;
232 		}
233 		buffer_clear(tb);
234 		buffer_append_str2(tb, BUF_PTR_LEN(module),
235                                        CONST_STR_LEN("_plugin_init"));
236 		init = (int (*)(plugin *))(intptr_t)dlsym(lib, tb->ptr);
237 		if (NULL == init) {
238 			const char *error = dlerror();
239 			if (error != NULL) {
240 				log_error(srv->errh, __FILE__, __LINE__, "dlsym: %s", error);
241 			} else {
242 				log_error(srv->errh, __FILE__, __LINE__, "dlsym symbol not found: %s", tb->ptr);
243 			}
244 		        dlclose(lib);
245 			return -1;
246 		}
247 	  #endif
248 	  }
249 
250 		plugin *p = plugin_init();
251 		p->lib = lib;
252 		if ((*init)(p)) {
253 			log_error(srv->errh, __FILE__, __LINE__, "%s plugin init failed", module->ptr);
254 			plugin_free(p);
255 			return -1;
256 		}
257 		plugins_register(srv, p);
258 	}
259 
260 	return 0;
261 }
262 
263 #endif /* defined(LIGHTTPD_STATIC) */
264 
265 typedef struct {
266   handler_t(*fn)();
267   plugin_data_base *data;
268 } plugin_fn_data;
269 
270 __attribute_hot__
271 static handler_t plugins_call_fn_req_data(request_st * const r, const int e) {
272     const void * const plugin_slots = r->con->plugin_slots;
273     const uint32_t offset = ((const uint16_t *)plugin_slots)[e];
274     if (0 == offset) return HANDLER_GO_ON;
275     const plugin_fn_data *plfd = (const plugin_fn_data *)
276       (((uintptr_t)plugin_slots) + offset);
277     handler_t rc = HANDLER_GO_ON;
278     while (plfd->fn && (rc = plfd->fn(r, plfd->data)) == HANDLER_GO_ON)
279         ++plfd;
280     return rc;
281 }
282 
283 __attribute_hot__
284 static handler_t plugins_call_fn_con_data(connection * const con, const int e) {
285     const void * const plugin_slots = con->plugin_slots;
286     const uint32_t offset = ((const uint16_t *)plugin_slots)[e];
287     if (0 == offset) return HANDLER_GO_ON;
288     const plugin_fn_data *plfd = (const plugin_fn_data *)
289       (((uintptr_t)plugin_slots) + offset);
290     handler_t rc = HANDLER_GO_ON;
291     while (plfd->fn && (rc = plfd->fn(con, plfd->data)) == HANDLER_GO_ON)
292         ++plfd;
293     return rc;
294 }
295 
296 static handler_t plugins_call_fn_srv_data(server * const srv, const int e) {
297     const uint32_t offset = ((const uint16_t *)srv->plugin_slots)[e];
298     if (0 == offset) return HANDLER_GO_ON;
299     const plugin_fn_data *plfd = (const plugin_fn_data *)
300       (((uintptr_t)srv->plugin_slots) + offset);
301     handler_t rc = HANDLER_GO_ON;
302     while (plfd->fn && (rc = plfd->fn(srv,plfd->data)) == HANDLER_GO_ON)
303         ++plfd;
304     return rc;
305 }
306 
307 static void plugins_call_fn_srv_data_all(server * const srv, const int e) {
308     const uint32_t offset = ((const uint16_t *)srv->plugin_slots)[e];
309     if (0 == offset) return;
310     const plugin_fn_data *plfd = (const plugin_fn_data *)
311       (((uintptr_t)srv->plugin_slots) + offset);
312     for (; plfd->fn; ++plfd)
313         plfd->fn(srv, plfd->data);
314 }
315 
316 /**
317  * plugins that use
318  *
319  * - request_st *r
320  * - void *p_d (plugin_data *)
321  */
322 
323 #define PLUGIN_CALL_FN_REQ_DATA(x, y) \
324     handler_t plugins_call_##y(request_st * const r) {\
325         return plugins_call_fn_req_data(r, x); \
326     }
327 
328 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_URI_CLEAN, handle_uri_clean)
329 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_DOCROOT, handle_docroot)
330 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_PHYSICAL, handle_physical)
331 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_SUBREQUEST_START, handle_subrequest_start)
332 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_RESPONSE_START, handle_response_start)
333 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_REQUEST_DONE, handle_request_done)
334 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_REQUEST_RESET, handle_request_reset)
335 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_REQUEST_ENV, handle_request_env)
336 
337 /**
338  * plugins that use
339  *
340  * - connection *con
341  * - void *p_d (plugin_data *)
342  */
343 
344 #define PLUGIN_CALL_FN_CON_DATA(x, y) \
345     handler_t plugins_call_##y(connection *con) {\
346         return plugins_call_fn_con_data(con, x); \
347     }
348 
349 PLUGIN_CALL_FN_CON_DATA(PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT, handle_connection_accept)
350 PLUGIN_CALL_FN_CON_DATA(PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR, handle_connection_shut_wr)
351 PLUGIN_CALL_FN_CON_DATA(PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE, handle_connection_close)
352 
353 #undef PLUGIN_CALL_FN_SRV_CON_DATA
354 
355 /**
356  * plugins that use
357  *
358  * - server *srv
359  * - void *p_d (plugin_data *)
360  */
361 
362 handler_t plugins_call_set_defaults(server *srv) {
363     return plugins_call_fn_srv_data(srv, PLUGIN_FUNC_SET_DEFAULTS);
364 }
365 
366 handler_t plugins_call_worker_init(server *srv) {
367     return plugins_call_fn_srv_data(srv, PLUGIN_FUNC_WORKER_INIT);
368 }
369 
370 void plugins_call_handle_trigger(server *srv) {
371     plugins_call_fn_srv_data_all(srv, PLUGIN_FUNC_HANDLE_TRIGGER);
372 }
373 
374 void plugins_call_handle_sighup(server *srv) {
375     plugins_call_fn_srv_data_all(srv, PLUGIN_FUNC_HANDLE_SIGHUP);
376 }
377 
378 handler_t plugins_call_handle_waitpid(server *srv, pid_t pid, int status) {
379     const uint32_t offset =
380       ((const uint16_t *)srv->plugin_slots)[PLUGIN_FUNC_HANDLE_WAITPID];
381     if (0 == offset) return HANDLER_GO_ON;
382     const plugin_fn_data *plfd = (const plugin_fn_data *)
383       (((uintptr_t)srv->plugin_slots) + offset);
384     handler_t rc = HANDLER_GO_ON;
385     while (plfd->fn&&(rc=plfd->fn(srv,plfd->data,pid,status))==HANDLER_GO_ON)
386         ++plfd;
387     return rc;
388 }
389 
390 static void plugins_call_cleanup(server * const srv) {
391     plugin ** const ps = srv->plugins.ptr;
392     for (uint32_t i = 0; i < srv->plugins.used; ++i) {
393         plugin *p = ps[i];
394         if (NULL == p) continue;
395         if (NULL != p->data) {
396             plugin_data_base *pd = p->data;
397             if (p->cleanup)
398                 p->cleanup(p->data);
399             free(pd->cvlist);
400             free(pd);
401             p->data = NULL;
402         }
403     }
404 }
405 
406 __attribute_cold__
407 static void plugins_call_init_reverse(server *srv, const uint32_t offset) {
408     if (0 == offset) return;
409     plugin_fn_data *a = (plugin_fn_data *)
410       (((uintptr_t)srv->plugin_slots) + offset);
411     plugin_fn_data *b = a;
412     while (b->fn) ++b;
413     for (; a < --b; ++a) { /* swap to reverse list */
414         plugin_fn_data tmp = *a;
415         *a = *b;
416         *b = tmp;
417     }
418 }
419 
420 __attribute_cold__
421 static void plugins_call_init_slot(server *srv, handler_t(*fn)(), void *data, const uint32_t offset) {
422     if (fn) {
423         plugin_fn_data *plfd = (plugin_fn_data *)
424           (((uintptr_t)srv->plugin_slots) + offset);
425         while (plfd->fn) ++plfd;
426         plfd->fn = fn;
427         plfd->data = data;
428     }
429 }
430 
431 handler_t plugins_call_init(server *srv) {
432 	plugin ** const ps = srv->plugins.ptr;
433 	uint16_t offsets[PLUGIN_FUNC_SIZEOF];
434 	memset(offsets, 0, sizeof(offsets));
435 
436 	for (uint32_t i = 0; i < srv->plugins.used; ++i) {
437 		/* check which calls are supported */
438 
439 		plugin *p = ps[i];
440 
441 		if (p->init) {
442 			if (NULL == (p->data = p->init())) {
443 				log_error(srv->errh, __FILE__, __LINE__,
444 				  "plugin-init failed for module %s", p->name);
445 				return HANDLER_ERROR;
446 			}
447 
448 			((plugin_data_base *)(p->data))->self = p;
449 			((plugin_data_base *)(p->data))->id = i + 1;
450 
451 			if (p->version != LIGHTTPD_VERSION_ID) {
452 				log_error(srv->errh, __FILE__, __LINE__,
453 				  "plugin-version doesn't match lighttpd-version for %s", p->name);
454 				return HANDLER_ERROR;
455 			}
456 		}
457 
458 		if (p->priv_defaults && HANDLER_ERROR==p->priv_defaults(srv, p->data)) {
459 			return HANDLER_ERROR;
460 		}
461 
462 		if (p->handle_uri_clean)
463 			++offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN];
464 		if (p->handle_uri_raw && !p->handle_uri_clean)
465 			++offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN]; /*(same as above)*/
466 		if (p->handle_request_env)
467 			++offsets[PLUGIN_FUNC_HANDLE_REQUEST_ENV];
468 		if (p->handle_request_done)
469 			++offsets[PLUGIN_FUNC_HANDLE_REQUEST_DONE];
470 		if (p->handle_connection_accept)
471 			++offsets[PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT];
472 		if (p->handle_connection_shut_wr)
473 			++offsets[PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR];
474 		if (p->handle_connection_close)
475 			++offsets[PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE];
476 		if (p->handle_trigger)
477 			++offsets[PLUGIN_FUNC_HANDLE_TRIGGER];
478 		if (p->handle_sighup)
479 			++offsets[PLUGIN_FUNC_HANDLE_SIGHUP];
480 		if (p->handle_waitpid)
481 			++offsets[PLUGIN_FUNC_HANDLE_WAITPID];
482 		if (p->handle_subrequest_start)
483 			++offsets[PLUGIN_FUNC_HANDLE_SUBREQUEST_START];
484 		if (p->handle_response_start)
485 			++offsets[PLUGIN_FUNC_HANDLE_RESPONSE_START];
486 		if (p->handle_docroot)
487 			++offsets[PLUGIN_FUNC_HANDLE_DOCROOT];
488 		if (p->handle_physical)
489 			++offsets[PLUGIN_FUNC_HANDLE_PHYSICAL];
490 		if (p->handle_request_reset)
491 			++offsets[PLUGIN_FUNC_HANDLE_REQUEST_RESET];
492 		if (p->set_defaults)
493 			++offsets[PLUGIN_FUNC_SET_DEFAULTS];
494 		if (p->worker_init)
495 			++offsets[PLUGIN_FUNC_WORKER_INIT];
496 	}
497 
498 	uint32_t nslots =
499 	  (sizeof(offsets)+sizeof(plugin_fn_data)-1) / sizeof(plugin_fn_data);
500 	for (uint32_t i = 0; i < PLUGIN_FUNC_SIZEOF; ++i) {
501 		if (offsets[i]) {
502 			uint32_t offset = nslots;
503 			nslots += offsets[i]+1; /* +1 to mark end of each list */
504 			force_assert(offset * sizeof(plugin_fn_data) <= USHRT_MAX);
505 			offsets[i] = (uint16_t)(offset * sizeof(plugin_fn_data));
506 		}
507 	}
508 
509 	/* allocate and fill slots of two dimensional array */
510 	srv->plugin_slots = calloc(nslots, sizeof(plugin_fn_data));
511 	force_assert(NULL != srv->plugin_slots);
512 	memcpy(srv->plugin_slots, offsets, sizeof(offsets));
513 
514 	/* add handle_uri_raw before handle_uri_clean, but in same slot */
515 	for (uint32_t i = 0; i < srv->plugins.used; ++i) {
516 		plugin * const p = ps[i];
517 		plugins_call_init_slot(srv, p->handle_uri_raw, p->data,
518 					offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN]);
519 	}
520 
521 	for (uint32_t i = 0; i < srv->plugins.used; ++i) {
522 		plugin * const p = ps[i];
523 
524 		if (!p->handle_uri_raw)
525 			plugins_call_init_slot(srv, p->handle_uri_clean, p->data,
526 						offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN]);
527 		plugins_call_init_slot(srv, p->handle_request_env, p->data,
528 					offsets[PLUGIN_FUNC_HANDLE_REQUEST_ENV]);
529 		plugins_call_init_slot(srv, p->handle_request_done, p->data,
530 					offsets[PLUGIN_FUNC_HANDLE_REQUEST_DONE]);
531 		plugins_call_init_slot(srv, p->handle_connection_accept, p->data,
532 					offsets[PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT]);
533 		plugins_call_init_slot(srv, p->handle_connection_shut_wr, p->data,
534 					offsets[PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR]);
535 		plugins_call_init_slot(srv, p->handle_connection_close, p->data,
536 					offsets[PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE]);
537 		plugins_call_init_slot(srv, p->handle_trigger, p->data,
538 					offsets[PLUGIN_FUNC_HANDLE_TRIGGER]);
539 		plugins_call_init_slot(srv, p->handle_sighup, p->data,
540 					offsets[PLUGIN_FUNC_HANDLE_SIGHUP]);
541 		plugins_call_init_slot(srv, p->handle_waitpid, p->data,
542 					offsets[PLUGIN_FUNC_HANDLE_WAITPID]);
543 		plugins_call_init_slot(srv, p->handle_subrequest_start, p->data,
544 					offsets[PLUGIN_FUNC_HANDLE_SUBREQUEST_START]);
545 		plugins_call_init_slot(srv, p->handle_response_start, p->data,
546 					offsets[PLUGIN_FUNC_HANDLE_RESPONSE_START]);
547 		plugins_call_init_slot(srv, p->handle_docroot, p->data,
548 					offsets[PLUGIN_FUNC_HANDLE_DOCROOT]);
549 		plugins_call_init_slot(srv, p->handle_physical, p->data,
550 					offsets[PLUGIN_FUNC_HANDLE_PHYSICAL]);
551 		plugins_call_init_slot(srv, p->handle_request_reset, p->data,
552 					offsets[PLUGIN_FUNC_HANDLE_REQUEST_RESET]);
553 		plugins_call_init_slot(srv, p->set_defaults, p->data,
554 					offsets[PLUGIN_FUNC_SET_DEFAULTS]);
555 		plugins_call_init_slot(srv, p->worker_init, p->data,
556 					offsets[PLUGIN_FUNC_WORKER_INIT]);
557 	}
558 
559 	/* reverse cleanup lists to balance ctor/dtor-like plugin behaviors */
560 	plugins_call_init_reverse(srv,offsets[PLUGIN_FUNC_HANDLE_REQUEST_RESET]);
561 	plugins_call_init_reverse(srv,offsets[PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE]);
562 
563 	return HANDLER_GO_ON;
564 }
565 
566 void plugins_free(server *srv) {
567 	if (srv->plugin_slots) {
568 		plugins_call_cleanup(srv);
569 		free(srv->plugin_slots);
570 		srv->plugin_slots = NULL;
571 	}
572 
573 	for (uint32_t i = 0; i < srv->plugins.used; ++i) {
574 		plugin_free(((plugin **)srv->plugins.ptr)[i]);
575 	}
576 	free(srv->plugins.ptr);
577 	srv->plugins.ptr = NULL;
578 	srv->plugins.used = 0;
579 	srv->plugins.size = 0;
580 	array_free_data(&plugin_stats);
581 }
582