|
|
|
@ -1,18 +1,23 @@
|
|
|
|
|
#include "first.h" |
|
|
|
|
|
|
|
|
|
#include "plugin.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(HAVE_PCRE_H) /* do nothing if PCRE not available */ |
|
|
|
|
#if defined(HAVE_GDBM_H) || defined(USE_MEMCACHED) /* at least one required */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "base.h" |
|
|
|
|
#include "fdevent.h" |
|
|
|
|
#include "log.h" |
|
|
|
|
#include "buffer.h" |
|
|
|
|
#include "http_header.h" |
|
|
|
|
|
|
|
|
|
#include "plugin.h" |
|
|
|
|
|
|
|
|
|
#include <sys/stat.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
#include "fdevent.h" |
|
|
|
|
# include <gdbm.h> |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
@ -29,307 +34,341 @@
|
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
/* plugin config for all request/connections */ |
|
|
|
|
|
|
|
|
|
typedef struct { |
|
|
|
|
buffer *db_filename; |
|
|
|
|
|
|
|
|
|
buffer *trigger_url; |
|
|
|
|
buffer *download_url; |
|
|
|
|
buffer *deny_url; |
|
|
|
|
|
|
|
|
|
array *mc_hosts; |
|
|
|
|
buffer *mc_namespace; |
|
|
|
|
#if defined(HAVE_PCRE_H) |
|
|
|
|
pcre *trigger_regex; |
|
|
|
|
pcre *download_regex; |
|
|
|
|
#endif |
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
GDBM_FILE db; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
memcached_st *memc; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
unsigned short trigger_timeout; |
|
|
|
|
unsigned short debug; |
|
|
|
|
const buffer *deny_url; |
|
|
|
|
pcre *trigger_regex; |
|
|
|
|
pcre *download_regex; |
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
GDBM_FILE db; |
|
|
|
|
#endif |
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
memcached_st *memc; |
|
|
|
|
const buffer *mc_namespace; |
|
|
|
|
#endif |
|
|
|
|
unsigned short trigger_timeout; |
|
|
|
|
unsigned short debug; |
|
|
|
|
} plugin_config; |
|
|
|
|
|
|
|
|
|
typedef struct { |
|
|
|
|
PLUGIN_DATA; |
|
|
|
|
|
|
|
|
|
buffer *tmp_buf; |
|
|
|
|
|
|
|
|
|
plugin_config **config_storage; |
|
|
|
|
|
|
|
|
|
plugin_config conf; |
|
|
|
|
PLUGIN_DATA; |
|
|
|
|
plugin_config defaults; |
|
|
|
|
plugin_config conf; |
|
|
|
|
} plugin_data; |
|
|
|
|
|
|
|
|
|
/* init the plugin data */ |
|
|
|
|
INIT_FUNC(mod_trigger_b4_dl_init) { |
|
|
|
|
plugin_data *p; |
|
|
|
|
|
|
|
|
|
p = calloc(1, sizeof(*p)); |
|
|
|
|
|
|
|
|
|
p->tmp_buf = buffer_init(); |
|
|
|
|
|
|
|
|
|
return p; |
|
|
|
|
return calloc(1, sizeof(plugin_data)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* detroy the plugin data */ |
|
|
|
|
FREE_FUNC(mod_trigger_b4_dl_free) { |
|
|
|
|
plugin_data *p = p_d; |
|
|
|
|
|
|
|
|
|
UNUSED(srv); |
|
|
|
|
|
|
|
|
|
if (!p) return HANDLER_GO_ON; |
|
|
|
|
|
|
|
|
|
if (p->config_storage) { |
|
|
|
|
size_t i; |
|
|
|
|
for (i = 0; i < srv->config_context->used; i++) { |
|
|
|
|
plugin_config *s = p->config_storage[i]; |
|
|
|
|
|
|
|
|
|
if (NULL == s) continue; |
|
|
|
|
|
|
|
|
|
buffer_free(s->db_filename); |
|
|
|
|
buffer_free(s->download_url); |
|
|
|
|
buffer_free(s->trigger_url); |
|
|
|
|
buffer_free(s->deny_url); |
|
|
|
|
|
|
|
|
|
buffer_free(s->mc_namespace); |
|
|
|
|
array_free(s->mc_hosts); |
|
|
|
|
|
|
|
|
|
#if defined(HAVE_PCRE_H) |
|
|
|
|
if (s->trigger_regex) pcre_free(s->trigger_regex); |
|
|
|
|
if (s->download_regex) pcre_free(s->download_regex); |
|
|
|
|
#endif |
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
if (s->db) gdbm_close(s->db); |
|
|
|
|
#endif |
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
if (s->memc) memcached_free(s->memc); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
free(s); |
|
|
|
|
} |
|
|
|
|
free(p->config_storage); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
buffer_free(p->tmp_buf); |
|
|
|
|
|
|
|
|
|
free(p); |
|
|
|
|
|
|
|
|
|
return HANDLER_GO_ON; |
|
|
|
|
static void mod_trigger_b4_dl_free_config(plugin_data * const p) { |
|
|
|
|
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) { |
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
case 0: /* trigger-before-download.gdbm-filename */ |
|
|
|
|
gdbm_close(cpv->v.v); |
|
|
|
|
break; |
|
|
|
|
#endif |
|
|
|
|
case 1: /* trigger-before-download.trigger-url */ |
|
|
|
|
pcre_free(cpv->v.v); |
|
|
|
|
break; |
|
|
|
|
case 2: /* trigger-before-download.download-url */ |
|
|
|
|
pcre_free(cpv->v.v); |
|
|
|
|
break; |
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
case 5: /* trigger-before-download.memcache-hosts */ |
|
|
|
|
memcached_free(cpv->v.v); |
|
|
|
|
break; |
|
|
|
|
#endif |
|
|
|
|
default: |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* handle plugin config and check values */ |
|
|
|
|
static int mod_trigger_b4_dl_init_gdbm(server * const srv, config_plugin_value_t * const cpv) { |
|
|
|
|
if (buffer_string_is_empty(cpv->v.b)) { |
|
|
|
|
cpv->v.v = NULL; |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
SETDEFAULTS_FUNC(mod_trigger_b4_dl_set_defaults) { |
|
|
|
|
plugin_data *p = p_d; |
|
|
|
|
size_t i = 0; |
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
|
|
|
|
|
GDBM_FILE db = gdbm_open(cpv->v.b->ptr, 4096, GDBM_WRCREAT | GDBM_NOLOCK, |
|
|
|
|
S_IRUSR | S_IWUSR, 0); |
|
|
|
|
|
|
|
|
|
config_values_t cv[] = { |
|
|
|
|
{ "trigger-before-download.gdbm-filename", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */ |
|
|
|
|
{ "trigger-before-download.trigger-url", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */ |
|
|
|
|
{ "trigger-before-download.download-url", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */ |
|
|
|
|
{ "trigger-before-download.deny-url", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */ |
|
|
|
|
{ "trigger-before-download.trigger-timeout", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 4 */ |
|
|
|
|
{ "trigger-before-download.memcache-hosts", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 5 */ |
|
|
|
|
{ "trigger-before-download.memcache-namespace", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 6 */ |
|
|
|
|
{ "trigger-before-download.debug", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 7 */ |
|
|
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } |
|
|
|
|
}; |
|
|
|
|
if (db) { |
|
|
|
|
cpv->v.v = db; |
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL; |
|
|
|
|
fdevent_setfd_cloexec(gdbm_fdesc(db)); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__, |
|
|
|
|
"gdbm-open failed %s", cpv->v.b->ptr); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!p) return HANDLER_ERROR; |
|
|
|
|
#else |
|
|
|
|
|
|
|
|
|
p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *)); |
|
|
|
|
UNUSED(srv); |
|
|
|
|
return 1; |
|
|
|
|
|
|
|
|
|
for (i = 0; i < srv->config_context->used; i++) { |
|
|
|
|
data_config const* config = (data_config const*)srv->config_context->data[i]; |
|
|
|
|
plugin_config *s; |
|
|
|
|
#if defined(HAVE_PCRE_H) |
|
|
|
|
const char *errptr; |
|
|
|
|
int erroff; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
s = calloc(1, sizeof(plugin_config)); |
|
|
|
|
s->db_filename = buffer_init(); |
|
|
|
|
s->download_url = buffer_init(); |
|
|
|
|
s->trigger_url = buffer_init(); |
|
|
|
|
s->deny_url = buffer_init(); |
|
|
|
|
s->mc_hosts = array_init(); |
|
|
|
|
s->mc_namespace = buffer_init(); |
|
|
|
|
|
|
|
|
|
cv[0].destination = s->db_filename; |
|
|
|
|
cv[1].destination = s->trigger_url; |
|
|
|
|
cv[2].destination = s->download_url; |
|
|
|
|
cv[3].destination = s->deny_url; |
|
|
|
|
cv[4].destination = &(s->trigger_timeout); |
|
|
|
|
cv[5].destination = s->mc_hosts; |
|
|
|
|
cv[6].destination = s->mc_namespace; |
|
|
|
|
cv[7].destination = &(s->debug); |
|
|
|
|
|
|
|
|
|
p->config_storage[i] = s; |
|
|
|
|
|
|
|
|
|
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
} |
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
if (!buffer_string_is_empty(s->db_filename)) { |
|
|
|
|
if (NULL == (s->db = gdbm_open(s->db_filename->ptr, 4096, GDBM_WRCREAT | GDBM_NOLOCK, S_IRUSR | S_IWUSR, 0))) { |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", |
|
|
|
|
"gdbm-open failed"); |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
} |
|
|
|
|
fdevent_setfd_cloexec(gdbm_fdesc(s->db)); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
#if defined(HAVE_PCRE_H) |
|
|
|
|
if (!buffer_string_is_empty(s->download_url)) { |
|
|
|
|
if (NULL == (s->download_regex = pcre_compile(s->download_url->ptr, |
|
|
|
|
0, &errptr, &erroff, NULL))) { |
|
|
|
|
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbss", |
|
|
|
|
"compiling regex for download-url failed:", |
|
|
|
|
s->download_url, "pos:", erroff); |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!buffer_string_is_empty(s->trigger_url)) { |
|
|
|
|
if (NULL == (s->trigger_regex = pcre_compile(s->trigger_url->ptr, |
|
|
|
|
0, &errptr, &erroff, NULL))) { |
|
|
|
|
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbss", |
|
|
|
|
"compiling regex for trigger-url failed:", |
|
|
|
|
s->trigger_url, "pos:", erroff); |
|
|
|
|
|
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
if (!array_is_vlist(s->mc_hosts)) { |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", |
|
|
|
|
"unexpected value for trigger-before-download.memcache-hosts; expected list of \"host\""); |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (s->mc_hosts->used) { |
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
buffer *option_string = buffer_init(); |
|
|
|
|
size_t k; |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
data_string *ds = (data_string *)s->mc_hosts->data[0]; |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
buffer_append_string_len(option_string, CONST_STR_LEN("--SERVER=")); |
|
|
|
|
buffer_append_string_buffer(option_string, &ds->value); |
|
|
|
|
} |
|
|
|
|
static int mod_trigger_b4_dl_init_memcached(server * const srv, config_plugin_value_t * const cpv) { |
|
|
|
|
const array * const mc_hosts = cpv->v.a; |
|
|
|
|
if (0 == mc_hosts->used) { |
|
|
|
|
cpv->v.v = NULL; |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
|
|
|
|
|
buffer * const opts = srv->tmp_buf; |
|
|
|
|
buffer_clear(opts); |
|
|
|
|
for (uint32_t k = 0; k < mc_hosts->used; ++k) { |
|
|
|
|
const data_string * const ds = (const data_string *)mc_hosts->data[k]; |
|
|
|
|
buffer_append_string_len(opts, CONST_STR_LEN(" --SERVER=")); |
|
|
|
|
buffer_append_string_buffer(opts, &ds->value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cpv->v.v = memcached(opts->ptr+1, buffer_string_length(opts)-1); |
|
|
|
|
|
|
|
|
|
if (cpv->v.v) { |
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL; |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__, |
|
|
|
|
"configuring memcached failed for option string: %s", opts->ptr); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__, |
|
|
|
|
"memcache support is not compiled in but " |
|
|
|
|
"trigger-before-download.memcache-hosts is set; aborting"); |
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (k = 1; k < s->mc_hosts->used; k++) { |
|
|
|
|
data_string *ds = (data_string *)s->mc_hosts->data[k]; |
|
|
|
|
static int mod_trigger_b4_dl_init_regex(server * const srv, config_plugin_value_t * const cpv, const char * const str) { |
|
|
|
|
const buffer * const b = cpv->v.b; |
|
|
|
|
if (buffer_string_is_empty(b)) { |
|
|
|
|
cpv->v.v = NULL; |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const char *errptr; |
|
|
|
|
int erroff; |
|
|
|
|
cpv->v.v = pcre_compile(b->ptr, 0, &errptr, &erroff, NULL); |
|
|
|
|
|
|
|
|
|
if (cpv->v.v) { |
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL; |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__, |
|
|
|
|
"compiling regex for %s failed: %s pos: %d", |
|
|
|
|
str, b->ptr, erroff); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
buffer_append_string_len(option_string, CONST_STR_LEN(" --SERVER=")); |
|
|
|
|
buffer_append_string_buffer(option_string, &ds->value); |
|
|
|
|
} |
|
|
|
|
FREE_FUNC(mod_trigger_b4_dl_free) { |
|
|
|
|
plugin_data *p = p_d; |
|
|
|
|
if (!p) return HANDLER_GO_ON; |
|
|
|
|
UNUSED(srv); |
|
|
|
|
|
|
|
|
|
s->memc = memcached(CONST_BUF_LEN(option_string)); |
|
|
|
|
mod_trigger_b4_dl_free_config(p); |
|
|
|
|
|
|
|
|
|
if (NULL == s->memc) { |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", |
|
|
|
|
"configuring memcached failed for option string:", |
|
|
|
|
option_string); |
|
|
|
|
} |
|
|
|
|
buffer_free(option_string); |
|
|
|
|
free(p->cvlist); |
|
|
|
|
free(p); |
|
|
|
|
|
|
|
|
|
if (NULL == s->memc) return HANDLER_ERROR; |
|
|
|
|
#else |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", |
|
|
|
|
"memcache support is not compiled in but trigger-before-download.memcache-hosts is set, aborting"); |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return HANDLER_GO_ON; |
|
|
|
|
return HANDLER_GO_ON; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#if defined(HAVE_PCRE_H) |
|
|
|
|
|
|
|
|
|
#define PATCH(x) \ |
|
|
|
|
p->conf.x = s->x; |
|
|
|
|
static int mod_trigger_b4_dl_patch_connection(server *srv, connection *con, plugin_data *p) { |
|
|
|
|
size_t i, j; |
|
|
|
|
plugin_config *s = p->config_storage[0]; |
|
|
|
|
|
|
|
|
|
#if defined(HAVE_GDBM) |
|
|
|
|
PATCH(db); |
|
|
|
|
#endif |
|
|
|
|
#if defined(HAVE_PCRE_H) |
|
|
|
|
PATCH(download_regex); |
|
|
|
|
PATCH(trigger_regex); |
|
|
|
|
#endif |
|
|
|
|
PATCH(trigger_timeout); |
|
|
|
|
PATCH(deny_url); |
|
|
|
|
PATCH(mc_namespace); |
|
|
|
|
PATCH(debug); |
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
PATCH(memc); |
|
|
|
|
#endif |
|
|
|
|
static void mod_trigger_b4_dl_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: /* trigger-before-download.gdbm-filename */ |
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
if (cpv->vtype != T_CONFIG_LOCAL) break; |
|
|
|
|
pconf->db = cpv->v.v; |
|
|
|
|
#endif |
|
|
|
|
break; |
|
|
|
|
case 1: /* trigger-before-download.trigger-url */ |
|
|
|
|
if (cpv->vtype != T_CONFIG_LOCAL) break; |
|
|
|
|
pconf->trigger_regex = cpv->v.v; |
|
|
|
|
break; |
|
|
|
|
case 2: /* trigger-before-download.download-url */ |
|
|
|
|
if (cpv->vtype != T_CONFIG_LOCAL) break; |
|
|
|
|
pconf->download_regex = cpv->v.v; |
|
|
|
|
break; |
|
|
|
|
case 3: /* trigger-before-download.deny-url */ |
|
|
|
|
pconf->deny_url = cpv->v.b; |
|
|
|
|
break; |
|
|
|
|
case 4: /* trigger-before-download.trigger-timeout */ |
|
|
|
|
pconf->trigger_timeout = cpv->v.shrt; |
|
|
|
|
break; |
|
|
|
|
case 5: /* trigger-before-download.memcache-hosts */ |
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
if (cpv->vtype != T_CONFIG_LOCAL) break; |
|
|
|
|
pconf->memc = cpv->v.v; |
|
|
|
|
#endif |
|
|
|
|
break; |
|
|
|
|
case 6: /* trigger-before-download.memcache-namespace */ |
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
pconf->mc_namespace = cpv->v.b; |
|
|
|
|
#endif |
|
|
|
|
break; |
|
|
|
|
case 7: /* trigger-before-download.debug */ |
|
|
|
|
pconf->debug = cpv->v.u; |
|
|
|
|
break; |
|
|
|
|
default:/* should not happen */ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* skip the first, the global context */ |
|
|
|
|
for (i = 1; i < srv->config_context->used; i++) { |
|
|
|
|
if (!config_check_cond(con, i)) continue; /* condition not matched */ |
|
|
|
|
static void mod_trigger_b4_dl_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { |
|
|
|
|
do { |
|
|
|
|
mod_trigger_b4_dl_merge_config_cpv(pconf, cpv); |
|
|
|
|
} while ((++cpv)->k_id != -1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
data_config *dc = (data_config *)srv->config_context->data[i]; |
|
|
|
|
s = p->config_storage[i]; |
|
|
|
|
static void mod_trigger_b4_dl_patch_config(connection * const con, plugin_data * const p) { |
|
|
|
|
memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); |
|
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) { |
|
|
|
|
if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) |
|
|
|
|
mod_trigger_b4_dl_merge_config(&p->conf, |
|
|
|
|
p->cvlist + p->cvlist[i].v.u2[0]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* merge config */ |
|
|
|
|
for (j = 0; j < dc->value->used; j++) { |
|
|
|
|
data_unset *du = dc->value->data[j]; |
|
|
|
|
SETDEFAULTS_FUNC(mod_trigger_b4_dl_set_defaults) { |
|
|
|
|
static const config_plugin_keys_t cpk[] = { |
|
|
|
|
{ CONST_STR_LEN("trigger-before-download.gdbm-filename"), |
|
|
|
|
T_CONFIG_STRING, |
|
|
|
|
T_CONFIG_SCOPE_CONNECTION } |
|
|
|
|
,{ CONST_STR_LEN("trigger-before-download.trigger-url"), |
|
|
|
|
T_CONFIG_STRING, |
|
|
|
|
T_CONFIG_SCOPE_CONNECTION } |
|
|
|
|
,{ CONST_STR_LEN("trigger-before-download.download-url"), |
|
|
|
|
T_CONFIG_STRING, |
|
|
|
|
T_CONFIG_SCOPE_CONNECTION } |
|
|
|
|
,{ CONST_STR_LEN("trigger-before-download.deny-url"), |
|
|
|
|
T_CONFIG_STRING, |
|
|
|
|
T_CONFIG_SCOPE_CONNECTION } |
|
|
|
|
,{ CONST_STR_LEN("trigger-before-download.trigger-timeout"), |
|
|
|
|
T_CONFIG_SHORT, |
|
|
|
|
T_CONFIG_SCOPE_CONNECTION } |
|
|
|
|
,{ CONST_STR_LEN("trigger-before-download.memcache-hosts"), |
|
|
|
|
T_CONFIG_ARRAY, |
|
|
|
|
T_CONFIG_SCOPE_CONNECTION } |
|
|
|
|
,{ CONST_STR_LEN("trigger-before-download.memcache-namespace"), |
|
|
|
|
T_CONFIG_STRING, |
|
|
|
|
T_CONFIG_SCOPE_CONNECTION } |
|
|
|
|
,{ CONST_STR_LEN("trigger-before-download.debug"), |
|
|
|
|
T_CONFIG_SHORT, |
|
|
|
|
T_CONFIG_SCOPE_CONNECTION } |
|
|
|
|
,{ NULL, 0, |
|
|
|
|
T_CONFIG_UNSET, |
|
|
|
|
T_CONFIG_SCOPE_UNSET } |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
plugin_data * const p = p_d; |
|
|
|
|
if (!config_plugin_values_init(srv, p, cpk, "mod_trigger_b4_dl")) |
|
|
|
|
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: /* trigger-before-download.gdbm-filename */ |
|
|
|
|
if (!mod_trigger_b4_dl_init_gdbm(srv, cpv)) |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
break; |
|
|
|
|
case 1: /* trigger-before-download.trigger-url */ |
|
|
|
|
if (!mod_trigger_b4_dl_init_regex(srv, cpv, "trigger-url")) |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
break; |
|
|
|
|
case 2: /* trigger-before-download.download-url */ |
|
|
|
|
if (!mod_trigger_b4_dl_init_regex(srv, cpv, "download-url")) |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
break; |
|
|
|
|
case 3: /* trigger-before-download.deny-url */ |
|
|
|
|
case 4: /* trigger-before-download.trigger-timeout */ |
|
|
|
|
break; |
|
|
|
|
case 5: /* trigger-before-download.memcache-hosts */ |
|
|
|
|
if (!array_is_vlist(cpv->v.a)) { |
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__, |
|
|
|
|
"unexpected value for %s; " |
|
|
|
|
"expected list of \"host\"", cpk[cpv->k_id].k); |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
} |
|
|
|
|
if (!mod_trigger_b4_dl_init_memcached(srv, cpv)) |
|
|
|
|
return HANDLER_ERROR; |
|
|
|
|
break; |
|
|
|
|
case 6: /* trigger-before-download.memcache-namespace */ |
|
|
|
|
case 7: /* trigger-before-download.debug */ |
|
|
|
|
break; |
|
|
|
|
default:/* should not happen */ |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* 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_trigger_b4_dl_merge_config(&p->defaults, cpv); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return HANDLER_GO_ON; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (buffer_is_equal_string(&du->key, CONST_STR_LEN("trigger-before-download.download-url"))) { |
|
|
|
|
#if defined(HAVE_PCRE_H) |
|
|
|
|
PATCH(download_regex); |
|
|
|
|
#endif |
|
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("trigger-before-download.trigger-url"))) { |
|
|
|
|
# if defined(HAVE_PCRE_H) |
|
|
|
|
PATCH(trigger_regex); |
|
|
|
|
# endif |
|
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("trigger-before-download.gdbm-filename"))) { |
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
PATCH(db); |
|
|
|
|
#endif |
|
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("trigger-before-download.trigger-timeout"))) { |
|
|
|
|
PATCH(trigger_timeout); |
|
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("trigger-before-download.debug"))) { |
|
|
|
|
PATCH(debug); |
|
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("trigger-before-download.deny-url"))) { |
|
|
|
|
PATCH(deny_url); |
|
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("trigger-before-download.memcache-namespace"))) { |
|
|
|
|
PATCH(mc_namespace); |
|
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("trigger-before-download.memcache-hosts"))) { |
|
|
|
|
#if defined(USE_MEMCACHED) |
|
|
|
|
PATCH(memc); |
|
|
|
|
static void mod_trigger_b4_dl_memcached_key(buffer * const b, const plugin_data * const p, const buffer * const remote_ip) { |
|
|
|
|
buffer_clear(b); |
|
|
|
|
if (p->conf.mc_namespace) |
|
|
|
|
buffer_copy_buffer(b, p->conf.mc_namespace); |
|
|
|
|
buffer_append_string_buffer(b, remote_ip); |
|
|
|
|
|
|
|
|
|
/* memcached can't handle spaces */ |
|
|
|
|
for (size_t i = 0, len = buffer_string_length(b); i < len; ++i) { |
|
|
|
|
if (b->ptr[i] == ' ') b->ptr[i] = '-'; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
static handler_t mod_trigger_b4_dl_deny(connection * const con, const plugin_data * const p) { |
|
|
|
|
if (p->conf.deny_url) { |
|
|
|
|
http_header_response_set(con, HTTP_HEADER_LOCATION, |
|
|
|
|
CONST_STR_LEN("Location"), |
|
|
|
|
CONST_BUF_LEN(p->conf.deny_url)); |
|
|
|
|
con->http_status = 307; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
log_error(con->errh, __FILE__, __LINE__, |
|
|
|
|
"trigger-before-download.deny-url not configured"); |
|
|
|
|
con->http_status = 500; |
|
|
|
|
} |
|
|
|
|
con->file_finished = 1; |
|
|
|
|
return HANDLER_FINISHED; |
|
|
|
|
} |
|
|
|
|
#undef PATCH |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) { |
|
|
|
|
#if defined(HAVE_PCRE_H) |
|
|
|
|
plugin_data *p = p_d; |
|
|
|
|
const char *remote_ip; |
|
|
|
|
const buffer *vb; |
|
|
|
|
|
|
|
|
|
int n; |
|
|
|
|
# define N 10 |
|
|
|
@ -339,7 +378,7 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
|
|
|
|
|
if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON; |
|
|
|
|
|
|
|
|
|
mod_trigger_b4_dl_patch_connection(srv, con, p); |
|
|
|
|
mod_trigger_b4_dl_patch_config(con, p); |
|
|
|
|
|
|
|
|
|
if (!p->conf.trigger_regex || !p->conf.download_regex) return HANDLER_GO_ON; |
|
|
|
|
|
|
|
|
@ -358,14 +397,12 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
if (!p->conf.memc) return HANDLER_GO_ON; |
|
|
|
|
# endif |
|
|
|
|
|
|
|
|
|
if (NULL != (vb = http_header_request_get(con, HTTP_HEADER_X_FORWARDED_FOR, CONST_STR_LEN("X-Forwarded-For")))) { |
|
|
|
|
/* X-Forwarded-For contains the ip behind the proxy */ |
|
|
|
|
|
|
|
|
|
remote_ip = vb->ptr; |
|
|
|
|
|
|
|
|
|
/* memcache can't handle spaces */ |
|
|
|
|
} else { |
|
|
|
|
remote_ip = con->dst_addr_buf->ptr; |
|
|
|
|
/* X-Forwarded-For contains the ip behind the proxy */ |
|
|
|
|
const buffer *remote_ip = |
|
|
|
|
http_header_request_get(con, HTTP_HEADER_X_FORWARDED_FOR, |
|
|
|
|
CONST_STR_LEN("X-Forwarded-For")); |
|
|
|
|
if (NULL == remote_ip) { |
|
|
|
|
remote_ip = con->dst_addr_buf; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (p->conf.debug) { |
|
|
|
@ -386,8 +423,8 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
/* the trigger matched */ |
|
|
|
|
datum key, val; |
|
|
|
|
|
|
|
|
|
key.dptr = (char *)remote_ip; |
|
|
|
|
key.dsize = strlen(remote_ip); |
|
|
|
|
*(const char **)&key.dptr = remote_ip->ptr; |
|
|
|
|
key.dsize = buffer_string_length(remote_ip); |
|
|
|
|
|
|
|
|
|
val.dptr = (char *)&(srv->cur_ts); |
|
|
|
|
val.dsize = sizeof(srv->cur_ts); |
|
|
|
@ -400,21 +437,15 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
# endif |
|
|
|
|
# if defined(USE_MEMCACHED) |
|
|
|
|
if (p->conf.memc) { |
|
|
|
|
size_t i, len; |
|
|
|
|
buffer_copy_buffer(p->tmp_buf, p->conf.mc_namespace); |
|
|
|
|
buffer_append_string(p->tmp_buf, remote_ip); |
|
|
|
|
|
|
|
|
|
len = buffer_string_length(p->tmp_buf); |
|
|
|
|
for (i = 0; i < len; i++) { |
|
|
|
|
if (p->tmp_buf->ptr[i] == ' ') p->tmp_buf->ptr[i] = '-'; |
|
|
|
|
} |
|
|
|
|
buffer * const b = srv->tmp_buf; |
|
|
|
|
mod_trigger_b4_dl_memcached_key(b, p, remote_ip); |
|
|
|
|
|
|
|
|
|
if (p->conf.debug) { |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "(debug) triggered IP:", p->tmp_buf); |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "(debug) triggered IP:", b); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (MEMCACHED_SUCCESS != memcached_set(p->conf.memc, |
|
|
|
|
CONST_BUF_LEN(p->tmp_buf), |
|
|
|
|
CONST_BUF_LEN(b), |
|
|
|
|
(const char *)&(srv->cur_ts), sizeof(srv->cur_ts), |
|
|
|
|
p->conf.trigger_timeout, 0)) { |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", |
|
|
|
@ -438,19 +469,14 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
datum key, val; |
|
|
|
|
time_t last_hit; |
|
|
|
|
|
|
|
|
|
key.dptr = (char *)remote_ip; |
|
|
|
|
key.dsize = strlen(remote_ip); |
|
|
|
|
*(const char **)&key.dptr = remote_ip->ptr; |
|
|
|
|
key.dsize = buffer_string_length(remote_ip); |
|
|
|
|
|
|
|
|
|
val = gdbm_fetch(p->conf.db, key); |
|
|
|
|
|
|
|
|
|
if (val.dptr == NULL) { |
|
|
|
|
/* not found, redirect */ |
|
|
|
|
|
|
|
|
|
http_header_response_set(con, HTTP_HEADER_LOCATION, CONST_STR_LEN("Location"), CONST_BUF_LEN(p->conf.deny_url)); |
|
|
|
|
con->http_status = 307; |
|
|
|
|
con->file_finished = 1; |
|
|
|
|
|
|
|
|
|
return HANDLER_FINISHED; |
|
|
|
|
return mod_trigger_b4_dl_deny(con, p); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
memcpy(&last_hit, val.dptr, sizeof(time_t)); |
|
|
|
@ -460,10 +486,6 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
if (srv->cur_ts - last_hit > p->conf.trigger_timeout) { |
|
|
|
|
/* found, but timeout, redirect */ |
|
|
|
|
|
|
|
|
|
http_header_response_set(con, HTTP_HEADER_LOCATION, CONST_STR_LEN("Location"), CONST_BUF_LEN(p->conf.deny_url)); |
|
|
|
|
con->http_status = 307; |
|
|
|
|
con->file_finished = 1; |
|
|
|
|
|
|
|
|
|
if (p->conf.db) { |
|
|
|
|
if (0 != gdbm_delete(p->conf.db, key)) { |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", |
|
|
|
@ -471,7 +493,7 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return HANDLER_FINISHED; |
|
|
|
|
return mod_trigger_b4_dl_deny(con, p); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val.dptr = (char *)&(srv->cur_ts); |
|
|
|
@ -483,21 +505,13 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
# endif |
|
|
|
|
|
|
|
|
|
# if defined(USE_MEMCACHED) |
|
|
|
|
if (p->conf.memc) { |
|
|
|
|
size_t i, len; |
|
|
|
|
|
|
|
|
|
buffer_copy_buffer(p->tmp_buf, p->conf.mc_namespace); |
|
|
|
|
buffer_append_string(p->tmp_buf, remote_ip); |
|
|
|
|
|
|
|
|
|
len = buffer_string_length(p->tmp_buf); |
|
|
|
|
for (i = 0; i < len; i++) { |
|
|
|
|
if (p->tmp_buf->ptr[i] == ' ') p->tmp_buf->ptr[i] = '-'; |
|
|
|
|
} |
|
|
|
|
buffer * const b = srv->tmp_buf; |
|
|
|
|
mod_trigger_b4_dl_memcached_key(b, p, remote_ip); |
|
|
|
|
|
|
|
|
|
if (p->conf.debug) { |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "(debug) checking IP:", p->tmp_buf); |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "(debug) checking IP:", b); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -506,18 +520,13 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
* and the timestamp is updated |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
if (MEMCACHED_SUCCESS != memcached_exist(p->conf.memc, CONST_BUF_LEN(p->tmp_buf))) { |
|
|
|
|
http_header_response_set(con, HTTP_HEADER_LOCATION, CONST_STR_LEN("Location"), CONST_BUF_LEN(p->conf.deny_url)); |
|
|
|
|
|
|
|
|
|
con->http_status = 307; |
|
|
|
|
con->file_finished = 1; |
|
|
|
|
|
|
|
|
|
return HANDLER_FINISHED; |
|
|
|
|
if (MEMCACHED_SUCCESS != memcached_exist(p->conf.memc, CONST_BUF_LEN(b))) { |
|
|
|
|
return mod_trigger_b4_dl_deny(con, p); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* set a new timeout */ |
|
|
|
|
if (MEMCACHED_SUCCESS != memcached_set(p->conf.memc, |
|
|
|
|
CONST_BUF_LEN(p->tmp_buf), |
|
|
|
|
CONST_BUF_LEN(b), |
|
|
|
|
(const char *)&(srv->cur_ts), sizeof(srv->cur_ts), |
|
|
|
|
p->conf.trigger_timeout, 0)) { |
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", |
|
|
|
@ -527,51 +536,33 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
|
|
|
|
|
# endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#else |
|
|
|
|
UNUSED(srv); |
|
|
|
|
UNUSED(con); |
|
|
|
|
UNUSED(p_d); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
return HANDLER_GO_ON; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#if defined(HAVE_GDBM_H) |
|
|
|
|
TRIGGER_FUNC(mod_trigger_b4_dl_handle_trigger) { |
|
|
|
|
plugin_data *p = p_d; |
|
|
|
|
size_t i; |
|
|
|
|
|
|
|
|
|
/* check DB each minute */ |
|
|
|
|
if (srv->cur_ts % 60 != 0) return HANDLER_GO_ON; |
|
|
|
|
|
|
|
|
|
/* cleanup */ |
|
|
|
|
for (i = 0; i < srv->config_context->used; i++) { |
|
|
|
|
plugin_config *s = p->config_storage[i]; |
|
|
|
|
static void mod_trigger_b4_dl_trigger_gdbm(GDBM_FILE db, const time_t cur_ts, const int trigger_timeout) { |
|
|
|
|
datum key, val, okey; |
|
|
|
|
|
|
|
|
|
if (!s->db) continue; |
|
|
|
|
|
|
|
|
|
okey.dptr = NULL; |
|
|
|
|
|
|
|
|
|
/* according to the manual this loop + delete does delete all entries on its way
|
|
|
|
|
* |
|
|
|
|
* we don't care as the next round will remove them. We don't have to perfect here. |
|
|
|
|
*/ |
|
|
|
|
for (key = gdbm_firstkey(s->db); key.dptr; key = gdbm_nextkey(s->db, okey)) { |
|
|
|
|
for (key = gdbm_firstkey(db); key.dptr; key = gdbm_nextkey(db, okey)) { |
|
|
|
|
time_t last_hit; |
|
|
|
|
if (okey.dptr) { |
|
|
|
|
free(okey.dptr); |
|
|
|
|
okey.dptr = NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val = gdbm_fetch(s->db, key); |
|
|
|
|
val = gdbm_fetch(db, key); |
|
|
|
|
|
|
|
|
|
memcpy(&last_hit, val.dptr, sizeof(time_t)); |
|
|
|
|
|
|
|
|
|
free(val.dptr); |
|
|
|
|
|
|
|
|
|
if (srv->cur_ts - last_hit > s->trigger_timeout) { |
|
|
|
|
gdbm_delete(s->db, key); |
|
|
|
|
if (cur_ts - last_hit > trigger_timeout) { |
|
|
|
|
gdbm_delete(db, key); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
okey = key; |
|
|
|
@ -579,19 +570,55 @@ TRIGGER_FUNC(mod_trigger_b4_dl_handle_trigger) {
|
|
|
|
|
if (okey.dptr) free(okey.dptr); |
|
|
|
|
|
|
|
|
|
/* reorg once a day */ |
|
|
|
|
if ((srv->cur_ts % (60 * 60 * 24) != 0)) gdbm_reorganize(s->db); |
|
|
|
|
} |
|
|
|
|
return HANDLER_GO_ON; |
|
|
|
|
if ((cur_ts % (60 * 60 * 24) != 0)) gdbm_reorganize(db); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TRIGGER_FUNC(mod_trigger_b4_dl_handle_trigger) { |
|
|
|
|
/* check DB each minute */ |
|
|
|
|
const time_t cur_ts = srv->cur_ts; |
|
|
|
|
if (cur_ts % 60 != 0) return HANDLER_GO_ON; |
|
|
|
|
|
|
|
|
|
plugin_data * const p = p_d; |
|
|
|
|
|
|
|
|
|
/* (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]; |
|
|
|
|
void *db = NULL; |
|
|
|
|
int timeout = (int)p->defaults.trigger_timeout; |
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) { |
|
|
|
|
switch (cpv->k_id) { |
|
|
|
|
case 0: /* trigger-before-download.gdbm-filename */ |
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL && NULL != cpv->v.v) |
|
|
|
|
db = cpv->v.v; |
|
|
|
|
break; |
|
|
|
|
case 4: /* trigger-before-download.trigger-timeout */ |
|
|
|
|
timeout = (int)cpv->v.shrt; |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (db) |
|
|
|
|
mod_trigger_b4_dl_trigger_gdbm(db, cur_ts, timeout); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return HANDLER_GO_ON; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
/* this function is called at dlopen() time and inits the callbacks */ |
|
|
|
|
|
|
|
|
|
#endif /* defined(HAVE_PCRE_H) */ |
|
|
|
|
#endif /* defined(HAVE_GDBM_H) || defined(USE_MEMCACHED) */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int mod_trigger_b4_dl_plugin_init(plugin *p); |
|
|
|
|
int mod_trigger_b4_dl_plugin_init(plugin *p) { |
|
|
|
|
p->version = LIGHTTPD_VERSION_ID; |
|
|
|
|
p->name = "trigger_b4_dl"; |
|
|
|
|
|
|
|
|
|
#if defined(HAVE_PCRE_H) /* do nothing if PCRE not available */ |
|
|
|
|
#if defined(HAVE_GDBM_H) || defined(USE_MEMCACHED) /* at least one required */ |
|
|
|
|
|
|
|
|
|
p->init = mod_trigger_b4_dl_init; |
|
|
|
|
p->handle_uri_clean = mod_trigger_b4_dl_uri_handler; |
|
|
|
|
p->set_defaults = mod_trigger_b4_dl_set_defaults; |
|
|
|
@ -600,5 +627,8 @@ int mod_trigger_b4_dl_plugin_init(plugin *p) {
|
|
|
|
|
#endif |
|
|
|
|
p->cleanup = mod_trigger_b4_dl_free; |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|