2020-05-20 05:06:13 +00:00
|
|
|
/*
|
|
|
|
* mod_vhostdb - virtual hosts mapping from backend database
|
|
|
|
*
|
|
|
|
* Copyright(c) 2017 Glenn Strauss gstrauss()gluelogic.com All rights reserved
|
|
|
|
* License: BSD 3-clause (same as lighttpd)
|
|
|
|
*/
|
2017-01-18 05:36:49 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2021-05-22 21:14:22 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "mod_vhostdb_api.h"
|
2018-03-25 07:45:05 +00:00
|
|
|
#include "base.h"
|
2017-01-18 05:36:49 +00:00
|
|
|
#include "plugin.h"
|
2020-10-17 14:25:11 +00:00
|
|
|
#include "plugin_config.h"
|
2017-01-18 05:36:49 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "stat_cache.h"
|
2020-09-05 03:47:19 +00:00
|
|
|
#include "algo_splaytree.h"
|
2017-01-18 05:36:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vhostdb framework
|
|
|
|
*/
|
|
|
|
|
2020-07-13 04:57:54 +00:00
|
|
|
typedef struct {
|
|
|
|
splay_tree *sptree; /* data in nodes of tree are (vhostdb_cache_entry *) */
|
|
|
|
time_t max_age;
|
|
|
|
} vhostdb_cache;
|
|
|
|
|
2017-01-18 05:36:49 +00:00
|
|
|
typedef struct {
|
|
|
|
const http_vhostdb_backend_t *vhostdb_backend;
|
2020-07-13 04:57:54 +00:00
|
|
|
vhostdb_cache *vhostdb_cache;
|
2017-01-18 05:36:49 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
2019-11-03 05:02:14 +00:00
|
|
|
plugin_config defaults;
|
2017-01-18 05:36:49 +00:00
|
|
|
plugin_config conf;
|
|
|
|
} plugin_data;
|
|
|
|
|
2020-07-13 04:57:54 +00:00
|
|
|
typedef struct {
|
|
|
|
char *server_name;
|
|
|
|
char *document_root;
|
|
|
|
uint32_t slen;
|
|
|
|
uint32_t dlen;
|
2021-07-12 18:46:49 +00:00
|
|
|
unix_time64_t ctime;
|
2020-07-13 04:57:54 +00:00
|
|
|
} vhostdb_cache_entry;
|
|
|
|
|
|
|
|
static vhostdb_cache_entry *
|
|
|
|
vhostdb_cache_entry_init (const buffer * const server_name, const buffer * const docroot)
|
|
|
|
{
|
[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
2021-06-09 02:57:36 +00:00
|
|
|
const uint32_t slen = buffer_clen(server_name);
|
|
|
|
const uint32_t dlen = buffer_clen(docroot);
|
2020-07-13 04:57:54 +00:00
|
|
|
vhostdb_cache_entry * const ve =
|
|
|
|
malloc(sizeof(vhostdb_cache_entry) + slen + dlen);
|
2021-07-13 05:21:26 +00:00
|
|
|
force_assert(ve);
|
2021-03-11 11:32:28 +00:00
|
|
|
ve->ctime = log_monotonic_secs;
|
2020-07-13 04:57:54 +00:00
|
|
|
ve->slen = slen;
|
|
|
|
ve->dlen = dlen;
|
|
|
|
ve->server_name = (char *)(ve + 1);
|
|
|
|
ve->document_root = ve->server_name + slen;
|
|
|
|
memcpy(ve->server_name, server_name->ptr, slen);
|
|
|
|
memcpy(ve->document_root, docroot->ptr, dlen);
|
|
|
|
return ve;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vhostdb_cache_entry_free (vhostdb_cache_entry *ve)
|
|
|
|
{
|
|
|
|
free(ve);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vhostdb_cache_free (vhostdb_cache *vc)
|
|
|
|
{
|
|
|
|
splay_tree *sptree = vc->sptree;
|
|
|
|
while (sptree) {
|
|
|
|
vhostdb_cache_entry_free(sptree->data);
|
|
|
|
sptree = splaytree_delete(sptree, sptree->key);
|
|
|
|
}
|
|
|
|
free(vc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static vhostdb_cache *
|
|
|
|
vhostdb_cache_init (const array *opts)
|
|
|
|
{
|
|
|
|
vhostdb_cache *vc = malloc(sizeof(vhostdb_cache));
|
|
|
|
force_assert(vc);
|
|
|
|
vc->sptree = NULL;
|
|
|
|
vc->max_age = 600; /* 10 mins */
|
|
|
|
for (uint32_t i = 0, used = opts->used; i < used; ++i) {
|
2020-10-17 14:25:11 +00:00
|
|
|
data_unset *du = opts->data[i];
|
|
|
|
if (buffer_is_equal_string(&du->key, CONST_STR_LEN("max-age")))
|
|
|
|
vc->max_age = (time_t)config_plugin_value_to_int32(du, vc->max_age);
|
2020-07-13 04:57:54 +00:00
|
|
|
}
|
|
|
|
return vc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static vhostdb_cache_entry *
|
|
|
|
mod_vhostdb_cache_query (request_st * const r, plugin_data * const p)
|
|
|
|
{
|
[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
2021-06-09 02:57:36 +00:00
|
|
|
const int ndx = splaytree_djbhash(BUF_PTR_LEN(&r->uri.authority));
|
2020-07-13 04:57:54 +00:00
|
|
|
splay_tree ** const sptree = &p->conf.vhostdb_cache->sptree;
|
|
|
|
*sptree = splaytree_splay(*sptree, ndx);
|
|
|
|
vhostdb_cache_entry * const ve =
|
|
|
|
(*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL;
|
|
|
|
|
|
|
|
return ve
|
|
|
|
&& buffer_is_equal_string(&r->uri.authority, ve->server_name, ve->slen)
|
|
|
|
? ve
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mod_vhostdb_cache_insert (request_st * const r, plugin_data * const p, vhostdb_cache_entry * const ve)
|
|
|
|
{
|
[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
2021-06-09 02:57:36 +00:00
|
|
|
const int ndx = splaytree_djbhash(BUF_PTR_LEN(&r->uri.authority));
|
2020-07-13 04:57:54 +00:00
|
|
|
splay_tree ** const sptree = &p->conf.vhostdb_cache->sptree;
|
|
|
|
/*(not necessary to re-splay (with current usage) since single-threaded
|
|
|
|
* and splaytree has not been modified since mod_vhostdb_cache_query())*/
|
|
|
|
/* *sptree = splaytree_splay(*sptree, ndx); */
|
|
|
|
if (NULL == *sptree || (*sptree)->key != ndx)
|
|
|
|
*sptree = splaytree_insert(*sptree, ndx, ve);
|
|
|
|
else { /* collision; replace old entry */
|
|
|
|
vhostdb_cache_entry_free((*sptree)->data);
|
|
|
|
(*sptree)->data = ve;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 05:36:49 +00:00
|
|
|
INIT_FUNC(mod_vhostdb_init) {
|
2019-11-03 05:02:14 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FREE_FUNC(mod_vhostdb_free) {
|
|
|
|
plugin_data *p = p_d;
|
2020-07-13 04:57:54 +00:00
|
|
|
|
|
|
|
if (NULL == p->cvlist) return;
|
|
|
|
/* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
|
|
|
|
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) {
|
|
|
|
if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
|
|
|
|
switch (cpv->k_id) {
|
|
|
|
case 1: /* vhostdb.cache */
|
|
|
|
vhostdb_cache_free(cpv->v.v);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 20:32:00 +00:00
|
|
|
|
|
|
|
http_vhostdb_dumbdata_reset();
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
static void mod_vhostdb_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: /* vhostdb.backend */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
pconf->vhostdb_backend = cpv->v.v;
|
|
|
|
break;
|
2020-07-13 04:57:54 +00:00
|
|
|
case 1: /* vhostdb.cache */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
pconf->vhostdb_cache = cpv->v.v;
|
|
|
|
break;
|
2019-11-03 05:02:14 +00:00
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_vhostdb_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
2019-10-17 05:27:52 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_vhostdb_patch_config(request_st * const r, plugin_data * const p) {
|
2019-11-03 05:02:14 +00:00
|
|
|
memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
|
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
2019-11-03 05:02:14 +00:00
|
|
|
mod_vhostdb_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("vhostdb.backend"),
|
|
|
|
T_CONFIG_STRING,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
2020-07-13 04:57:54 +00:00
|
|
|
,{ CONST_STR_LEN("vhostdb.cache"),
|
|
|
|
T_CONFIG_ARRAY,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
2019-11-03 05:02:14 +00:00
|
|
|
,{ NULL, 0,
|
|
|
|
T_CONFIG_UNSET,
|
|
|
|
T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
plugin_data * const p = p_d;
|
|
|
|
if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* process and validate config directives
|
|
|
|
* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
|
|
|
|
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) {
|
|
|
|
switch (cpv->k_id) {
|
|
|
|
case 0: /* vhostdb.backend */
|
[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
2021-06-09 02:57:36 +00:00
|
|
|
if (!buffer_is_blank(cpv->v.b)) {
|
2019-11-03 05:02:14 +00:00
|
|
|
const buffer * const b = cpv->v.b;
|
|
|
|
*(const void **)&cpv->v.v = http_vhostdb_backend_get(b);
|
|
|
|
if (NULL == cpv->v.v) {
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__,
|
|
|
|
"vhostdb.backend not supported: %s", b->ptr);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL;
|
|
|
|
}
|
|
|
|
break;
|
2020-07-13 04:57:54 +00:00
|
|
|
case 1: /* vhostdb.cache */
|
|
|
|
cpv->v.v = vhostdb_cache_init(cpv->v.a);
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL;
|
|
|
|
break;
|
2019-11-03 05:02:14 +00:00
|
|
|
default:/* should not happen */
|
|
|
|
break;
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
/* 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_vhostdb_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 12:23:58 +00:00
|
|
|
REQUEST_FUNC(mod_vhostdb_handle_request_reset) {
|
2017-01-18 05:36:49 +00:00
|
|
|
plugin_data *p = p_d;
|
2020-07-13 04:57:54 +00:00
|
|
|
vhostdb_cache_entry *ve;
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if ((ve = r->plugin_ctx[p->id])) {
|
|
|
|
r->plugin_ctx[p->id] = NULL;
|
2020-07-13 04:57:54 +00:00
|
|
|
vhostdb_cache_entry_free(ve);
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static handler_t mod_vhostdb_error_500 (request_st * const r)
|
2017-01-18 05:36:49 +00:00
|
|
|
{
|
2020-01-13 02:51:12 +00:00
|
|
|
r->http_status = 500; /* Internal Server Error */
|
|
|
|
r->handler_module = NULL;
|
2017-01-18 05:36:49 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
2020-07-13 04:57:54 +00:00
|
|
|
static handler_t mod_vhostdb_found (request_st * const r, vhostdb_cache_entry * const ve)
|
2017-01-18 05:36:49 +00:00
|
|
|
{
|
|
|
|
/* fix virtual server and docroot */
|
[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
2021-06-09 02:57:36 +00:00
|
|
|
if (ve->slen) {
|
|
|
|
r->server_name = &r->server_name_buf;
|
|
|
|
buffer_copy_string_len(&r->server_name_buf, ve->server_name, ve->slen);
|
|
|
|
}
|
2020-07-13 04:57:54 +00:00
|
|
|
buffer_copy_string_len(&r->physical.doc_root, ve->document_root, ve->dlen);
|
2017-01-18 05:36:49 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
REQUEST_FUNC(mod_vhostdb_handle_docroot) {
|
2017-01-18 05:36:49 +00:00
|
|
|
plugin_data *p = p_d;
|
2020-07-13 04:57:54 +00:00
|
|
|
vhostdb_cache_entry *ve;
|
2017-01-18 05:36:49 +00:00
|
|
|
|
|
|
|
/* no host specified? */
|
[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
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_blank(&r->uri.authority)) return HANDLER_GO_ON;
|
2017-01-18 05:36:49 +00:00
|
|
|
|
|
|
|
/* check if cached this connection */
|
2020-01-13 02:51:12 +00:00
|
|
|
ve = r->plugin_ctx[p->id];
|
2020-07-13 04:57:54 +00:00
|
|
|
if (ve
|
|
|
|
&& buffer_is_equal_string(&r->uri.authority, ve->server_name, ve->slen))
|
2020-01-13 02:51:12 +00:00
|
|
|
return mod_vhostdb_found(r, ve); /* HANDLER_GO_ON */
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_vhostdb_patch_config(r, p);
|
2017-01-18 05:36:49 +00:00
|
|
|
if (!p->conf.vhostdb_backend) return HANDLER_GO_ON;
|
|
|
|
|
2020-07-13 04:57:54 +00:00
|
|
|
if (p->conf.vhostdb_cache && (ve = mod_vhostdb_cache_query(r, p)))
|
|
|
|
return mod_vhostdb_found(r, ve); /* HANDLER_GO_ON */
|
|
|
|
|
2021-09-13 03:05:01 +00:00
|
|
|
buffer * const b = r->tmp_buf; /*(cleared before use in backend->query())*/
|
|
|
|
const http_vhostdb_backend_t * const backend = p->conf.vhostdb_backend;
|
2020-01-13 02:51:12 +00:00
|
|
|
if (0 != backend->query(r, backend->p_d, b)) {
|
|
|
|
return mod_vhostdb_error_500(r); /* HANDLER_FINISHED */
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
|
[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
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_blank(b)) {
|
2017-01-18 05:36:49 +00:00
|
|
|
/* no such virtual host */
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sanity check that really is a directory */
|
|
|
|
buffer_append_slash(b);
|
2020-10-09 17:33:54 +00:00
|
|
|
if (!stat_cache_path_isdir(b)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__, "%s", b->ptr);
|
|
|
|
return mod_vhostdb_error_500(r); /* HANDLER_FINISHED */
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 04:57:54 +00:00
|
|
|
if (ve && !p->conf.vhostdb_cache)
|
|
|
|
vhostdb_cache_entry_free(ve);
|
|
|
|
|
|
|
|
ve = vhostdb_cache_entry_init(&r->uri.authority, b);
|
|
|
|
|
|
|
|
if (!p->conf.vhostdb_cache)
|
|
|
|
r->plugin_ctx[p->id] = ve;
|
|
|
|
else
|
|
|
|
mod_vhostdb_cache_insert(r, p, ve);
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
return mod_vhostdb_found(r, ve); /* HANDLER_GO_ON */
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 04:57:54 +00:00
|
|
|
/* walk though cache, collect expired ids, and remove them in a second loop */
|
|
|
|
static void
|
2021-07-12 18:46:49 +00:00
|
|
|
mod_vhostdb_tag_old_entries (splay_tree * const t, int * const keys, int * const ndx, const time_t max_age, const unix_time64_t cur_ts)
|
2020-07-13 04:57:54 +00:00
|
|
|
{
|
|
|
|
if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
|
|
|
|
if (t->left)
|
|
|
|
mod_vhostdb_tag_old_entries(t->left, keys, ndx, max_age, cur_ts);
|
|
|
|
if (t->right)
|
|
|
|
mod_vhostdb_tag_old_entries(t->right, keys, ndx, max_age, cur_ts);
|
|
|
|
if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
|
|
|
|
|
|
|
|
const vhostdb_cache_entry * const ve = t->data;
|
|
|
|
if (cur_ts - ve->ctime > max_age)
|
|
|
|
keys[(*ndx)++] = t->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute_noinline__
|
|
|
|
static void
|
2021-07-12 18:46:49 +00:00
|
|
|
mod_vhostdb_periodic_cleanup(splay_tree **sptree_ptr, const time_t max_age, const unix_time64_t cur_ts)
|
2020-07-13 04:57:54 +00:00
|
|
|
{
|
|
|
|
splay_tree *sptree = *sptree_ptr;
|
|
|
|
int max_ndx, i;
|
|
|
|
int keys[8192]; /* 32k size on stack */
|
|
|
|
do {
|
|
|
|
if (!sptree) break;
|
|
|
|
max_ndx = 0;
|
|
|
|
mod_vhostdb_tag_old_entries(sptree, keys, &max_ndx, max_age, cur_ts);
|
|
|
|
for (i = 0; i < max_ndx; ++i) {
|
|
|
|
int ndx = keys[i];
|
|
|
|
sptree = splaytree_splay(sptree, ndx);
|
|
|
|
if (sptree && sptree->key == ndx) {
|
|
|
|
vhostdb_cache_entry_free(sptree->data);
|
|
|
|
sptree = splaytree_delete(sptree, ndx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (max_ndx == sizeof(keys)/sizeof(int));
|
|
|
|
*sptree_ptr = sptree;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRIGGER_FUNC(mod_vhostdb_periodic)
|
|
|
|
{
|
|
|
|
const plugin_data * const p = p_d;
|
2021-07-12 18:46:49 +00:00
|
|
|
const unix_time64_t cur_ts = log_monotonic_secs;
|
2020-07-13 04:57:54 +00:00
|
|
|
if (cur_ts & 0x7) return HANDLER_GO_ON; /*(continue once each 8 sec)*/
|
|
|
|
UNUSED(srv);
|
|
|
|
|
|
|
|
/* future: might construct array of (vhostdb_cache *) at startup
|
|
|
|
* to avoid the need to search for them here */
|
2021-01-22 07:42:16 +00:00
|
|
|
/* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
if (NULL == p->cvlist) return HANDLER_GO_ON;
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
|
2020-07-13 04:57:54 +00:00
|
|
|
const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
for (; cpv->k_id != -1; ++cpv) {
|
|
|
|
if (cpv->k_id != 1) continue; /* k_id == 1 for vhostdb.cache */
|
|
|
|
if (cpv->vtype != T_CONFIG_LOCAL) continue;
|
|
|
|
vhostdb_cache *vc = cpv->v.v;
|
|
|
|
mod_vhostdb_periodic_cleanup(&vc->sptree, vc->max_age, cur_ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-18 05:36:49 +00:00
|
|
|
int mod_vhostdb_plugin_init(plugin *p);
|
|
|
|
int mod_vhostdb_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
2019-10-19 04:30:54 +00:00
|
|
|
p->name = "vhostdb";
|
2017-01-18 05:36:49 +00:00
|
|
|
p->init = mod_vhostdb_init;
|
|
|
|
p->cleanup = mod_vhostdb_free;
|
|
|
|
p->set_defaults = mod_vhostdb_set_defaults;
|
2020-07-13 04:57:54 +00:00
|
|
|
p->handle_trigger = mod_vhostdb_periodic;
|
2017-01-18 05:36:49 +00:00
|
|
|
p->handle_docroot = mod_vhostdb_handle_docroot;
|
2020-07-25 12:23:58 +00:00
|
|
|
p->handle_request_reset = mod_vhostdb_handle_request_reset;
|
2017-01-18 05:36:49 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|