lighttpd 1.4.x
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.6 KiB

#include "first.h"
#include <stdlib.h>
#include <string.h>
#include "plugin.h"
#include "log.h"
#include "buffer.h"
#include "array.h"
#include "request.h"
* this is a skeleton for a lighttpd plugin
* just replaces every occurrence of 'skeleton' by your plugin name
* e.g. in vim:
* :%s/skeleton/myhandler/
/* plugin config for all request/connections */
typedef struct {
const array *match;
} plugin_config;
typedef struct {
plugin_config defaults;
plugin_config conf;
} plugin_data;
#if 0 /* (needed if module keeps state for request) */
typedef struct {
size_t foo;
} handler_ctx;
static handler_ctx * handler_ctx_init() {
handler_ctx * hctx = calloc(1, sizeof(*hctx));
return hctx;
static void handler_ctx_free(handler_ctx *hctx) {
/* init the plugin data */
INIT_FUNC(mod_skeleton_init) {
return calloc(1, sizeof(plugin_data));
/* handle plugin config and check values */
static void mod_skeleton_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
case 0: /* skeleton.array */
pconf->match = cpv->v.a;
default:/* should not happen */
static void mod_skeleton_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
do {
mod_skeleton_merge_config_cpv(pconf, cpv);
} while ((++cpv)->k_id != -1);
static void mod_skeleton_patch_config(request_st * const r, plugin_data * const p) {
p->conf = p->defaults; /* copy small struct instead of memcpy() */
/*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
for (int i = 1, used = p->nconfig; i < used; ++i) {
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
mod_skeleton_merge_config(&p->conf, p->cvlist+p->cvlist[i].v.u2[0]);
SETDEFAULTS_FUNC(mod_skeleton_set_defaults) {
static const config_plugin_keys_t cpk[] = {
{ CONST_STR_LEN("skeleton.array"),
,{ NULL, 0,
plugin_data * const p = p_d;
if (!config_plugin_values_init(srv, p, cpk, "mod_skeleton"))
/* initialize p->defaults from global config context */
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
if (-1 != cpv->k_id)
mod_skeleton_merge_config(&p->defaults, cpv);
URIHANDLER_FUNC(mod_skeleton_uri_handler) {
plugin_data * const p = p_d;
/* determine whether or not module participates in request */
if (NULL != r->handler_module) return HANDLER_GO_ON;
[multiple] reduce redundant NULL buffer checks This commit is a large set of code changes and results in removal of hundreds, perhaps thousands, of CPU instructions, a portion of which are on hot code paths. Most (buffer *) used by lighttpd are not NULL, especially since buffers were inlined into numerous larger structs such as request_st and chunk. In the small number of instances where that is not the case, a NULL check is often performed earlier in a function where that buffer is later used with a buffer_* func. In the handful of cases that remained, a NULL check was added, e.g. with r->http_host and r->conf.server_tag. - check for empty strings at config time and set value to NULL if blank string will be ignored at runtime; at runtime, simple pointer check for NULL can be used to check for a value that has been set and is not blank ("") - use buffer_is_blank() instead of buffer_string_is_empty(), and use buffer_is_unset() instead of buffer_is_empty(), where buffer is known not to be NULL so that NULL check can be skipped - use buffer_clen() instead of buffer_string_length() when buffer is known not to be NULL (to avoid NULL check at runtime) - use buffer_truncate() instead of buffer_string_set_length() to truncate string, and use buffer_extend() to extend Examples where buffer known not to be NULL: - cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL (though we might set it to NULL if buffer_is_blank(cpv->v.b)) - address of buffer is arg (&foo) (compiler optimizer detects this in most, but not all, cases) - buffer is checked for NULL earlier in func - buffer is accessed in same scope without a NULL check (e.g. b->ptr) internal behavior change: callers must not pass a NULL buffer to some funcs. - buffer_init_buffer() requires non-null args - buffer_copy_buffer() requires non-null args - buffer_append_string_buffer() requires non-null args - buffer_string_space() requires non-null arg
1 year ago
if (buffer_is_blank(&r->uri.path)) return HANDLER_GO_ON;
/* get module config for request */
mod_skeleton_patch_config(r, p);
if (NULL == p->conf.match
|| NULL == array_match_value_suffix(p->conf.match, &r->uri.path)) {
/* module participates in request; business logic here */
r->http_status = 403; /* example: reject request with 403 Forbidden */
/* this function is called at dlopen() time and inits the callbacks */
int mod_skeleton_plugin_init(plugin *p);
int mod_skeleton_plugin_init(plugin *p) {
p->name = "skeleton";
p->init = mod_skeleton_init;
p->set_defaults= mod_skeleton_set_defaults;
p->handle_uri_clean = mod_skeleton_uri_handler;
return 0;