<commit_before>#include "plugin.h"
#include "log.h"

#include <string.h>
#include <stdlib.h>

#include <stdio.h>

#ifdef HAVE_VALGRIND_VALGRIND_H
# include <valgrind/valgrind.h>
#endif

#ifndef __WIN32
# include <dlfcn.h>
#endif
/*
 *
 * if you change this enum to add a new callback, be sure
 * - that PLUGIN_FUNC_SIZEOF is the last entry
 * - that you add PLUGIN_TO_SLOT twice:
 *   1. as callback-dispatcher
 *   2. in plugins_call_init()
 *
 */

typedef struct {
	PLUGIN_DATA;
} plugin_data;

typedef enum {
	PLUGIN_FUNC_UNSET,
		PLUGIN_FUNC_HANDLE_URI_CLEAN,
		PLUGIN_FUNC_HANDLE_URI_RAW,
		PLUGIN_FUNC_HANDLE_REQUEST_DONE,
		PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE,
		PLUGIN_FUNC_HANDLE_TRIGGER,
		PLUGIN_FUNC_HANDLE_SIGHUP,
		PLUGIN_FUNC_HANDLE_SUBREQUEST,
		PLUGIN_FUNC_HANDLE_SUBREQUEST_START,
		PLUGIN_FUNC_HANDLE_JOBLIST,
		PLUGIN_FUNC_HANDLE_DOCROOT,
		PLUGIN_FUNC_HANDLE_PHYSICAL,
		PLUGIN_FUNC_CONNECTION_RESET,
		PLUGIN_FUNC_INIT,
		PLUGIN_FUNC_CLEANUP,
		PLUGIN_FUNC_SET_DEFAULTS,

		PLUGIN_FUNC_SIZEOF
} plugin_t;

static plugin *plugin_init(void) {
	plugin *p;

	p = calloc(1, sizeof(*p));

	return p;
}

static void plugin_free(plugin *p) {
	int use_dlclose = 1;
	if (p->name) buffer_free(p->name);
#ifdef HAVE_VALGRIND_VALGRIND_H
	/*if (RUNNING_ON_VALGRIND) use_dlclose = 0;*/
#endif

#ifndef LIGHTTPD_STATIC
	if (use_dlclose && p->lib) {
#ifdef __WIN32
		FreeLibrary(p->lib);
#else
		dlclose(p->lib);
#endif
	}
#endif

	free(p);
}

static int plugins_register(server *srv, plugin *p) {
	plugin **ps;
	if (0 == srv->plugins.size) {
		srv->plugins.size = 4;
		srv->plugins.ptr  = malloc(srv->plugins.size * sizeof(*ps));
		srv->plugins.used = 0;
	} else if (srv->plugins.used == srv->plugins.size) {
		srv->plugins.size += 4;
		srv->plugins.ptr   = realloc(srv->plugins.ptr, srv->plugins.size * sizeof(*ps));
	}

	ps = srv->plugins.ptr;
	ps[srv->plugins.used++] = p;

	return 0;
}

/**
 *
 *
 *
 */

#ifdef LIGHTTPD_STATIC
int plugins_load(server *srv) {
	plugin *p;
#define PLUGIN_INIT(x)\
	p = plugin_init(); \
	if (x ## _plugin_init(p)) { \
		log_error_write(srv, __FILE__, __LINE__, "ss", #x, "plugin init failed" ); \
		plugin_free(p); \
		return -1;\
	}\
	plugins_register(srv, p);

#include "plugin-static.h"

	return 0;
}
#else
int plugins_load(server *srv) {
	plugin *p;
	int (*init)(plugin *pl);
	const char *error;
	size_t i, j;

	for (i = 0; i < srv->srvconf.modules->used; i++) {
		data_string *d = (data_string *)srv->srvconf.modules->data[i];
		char *modules = d->value->ptr;

		for (j = 0; j < i; j++) {
			if (buffer_is_equal(d->value, ((data_string *) srv->srvconf.modules->data[j])->value)) {
				log_error_write(srv, __FILE__, __LINE__, "sbs", "Cannot load plugin", d->value, "more than once");
				return -1;
			}
		}

		buffer_copy_string_buffer(srv->tmp_buf, srv->srvconf.modules_dir);

		buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN("/"));
		buffer_append_string(srv->tmp_buf, modules);
#if defined(__WIN32) || defined(__CYGWIN__)
		buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN(".dll"));
#else
		buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN(".so"));
#endif

		p = plugin_init();
#ifdef __WIN32<commit_msg>Only print a warning for now if you try to load a module twice<commit_after>#include "plugin.h"
#include "log.h"

#include <string.h>
#include <stdlib.h>

#include <stdio.h>

#ifdef HAVE_VALGRIND_VALGRIND_H
# include <valgrind/valgrind.h>
#endif

#ifndef __WIN32
# include <dlfcn.h>
#endif
/*
 *
 * if you change this enum to add a new callback, be sure
 * - that PLUGIN_FUNC_SIZEOF is the last entry
 * - that you add PLUGIN_TO_SLOT twice:
 *   1. as callback-dispatcher
 *   2. in plugins_call_init()
 *
 */

typedef struct {
	PLUGIN_DATA;
} plugin_data;

typedef enum {
	PLUGIN_FUNC_UNSET,
		PLUGIN_FUNC_HANDLE_URI_CLEAN,
		PLUGIN_FUNC_HANDLE_URI_RAW,
		PLUGIN_FUNC_HANDLE_REQUEST_DONE,
		PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE,
		PLUGIN_FUNC_HANDLE_TRIGGER,
		PLUGIN_FUNC_HANDLE_SIGHUP,
		PLUGIN_FUNC_HANDLE_SUBREQUEST,
		PLUGIN_FUNC_HANDLE_SUBREQUEST_START,
		PLUGIN_FUNC_HANDLE_JOBLIST,
		PLUGIN_FUNC_HANDLE_DOCROOT,
		PLUGIN_FUNC_HANDLE_PHYSICAL,
		PLUGIN_FUNC_CONNECTION_RESET,
		PLUGIN_FUNC_INIT,
		PLUGIN_FUNC_CLEANUP,
		PLUGIN_FUNC_SET_DEFAULTS,

		PLUGIN_FUNC_SIZEOF
} plugin_t;

static plugin *plugin_init(void) {
	plugin *p;

	p = calloc(1, sizeof(*p));

	return p;
}

static void plugin_free(plugin *p) {
	int use_dlclose = 1;
	if (p->name) buffer_free(p->name);
#ifdef HAVE_VALGRIND_VALGRIND_H
	/*if (RUNNING_ON_VALGRIND) use_dlclose = 0;*/
#endif

#ifndef LIGHTTPD_STATIC
	if (use_dlclose && p->lib) {
#ifdef __WIN32
		FreeLibrary(p->lib);
#else
		dlclose(p->lib);
#endif
	}
#endif

	free(p);
}

static int plugins_register(server *srv, plugin *p) {
	plugin **ps;
	if (0 == srv->plugins.size) {
		srv->plugins.size = 4;
		srv->plugins.ptr  = malloc(srv->plugins.size * sizeof(*ps));
		srv->plugins.used = 0;
	} else if (srv->plugins.used == srv->plugins.size) {
		srv->plugins.size += 4;
		srv->plugins.ptr   = realloc(srv->plugins.ptr, srv->plugins.size * sizeof(*ps));
	}

	ps = srv->plugins.ptr;
	ps[srv->plugins.used++] = p;

	return 0;
}

/**
 *
 *
 *
 */

#ifdef LIGHTTPD_STATIC
int plugins_load(server *srv) {
	plugin *p;
#define PLUGIN_INIT(x)\
	p = plugin_init(); \
	if (x ## _plugin_init(p)) { \
		log_error_write(srv, __FILE__, __LINE__, "ss", #x, "plugin init failed" ); \
		plugin_free(p); \
		return -1;\
	}\
	plugins_register(srv, p);

#include "plugin-static.h"

	return 0;
}
#else
int plugins_load(server *srv) {
	plugin *p;
	int (*init)(plugin *pl);
	const char *error;
	size_t i, j;

	for (i = 0; i < srv->srvconf.modules->used; i++) {
		data_string *d = (data_string *)srv->srvconf.modules->data[i];
		char *modules = d->value->ptr;

		for (j = 0; j < i; j++) {
			if (buffer_is_equal(d->value, ((data_string *) srv->srvconf.modules->data[j])->value)) {
				log_error_write(srv, __FILE__, __LINE__, "sbs", "Cannot load plugin", d->value, "more than once, please fix your config (we may not accept such configs in future releases");
				continue;
			}
		}

		buffer_copy_string_buffer(srv->tmp_buf, srv->srvconf.modules_dir);

		buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN("/"));
		buffer_append_string(srv->tmp_buf, modules);
#if defined(__WIN32) || defined(__CYGWIN__)
		buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN(".dll"));
#else
		buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN(".so"));
#endif

		p = plugin_init();
#ifdef __WIN32
{'fraction_non_alphanumeric': 0.1466171233937142, 'fraction_numerical': 0.006967022758941012, 'mean_word_length': 4.5834053586862575, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '172346', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2661, 'n_words': 522}