2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "base.h"
|
2019-10-25 07:36:52 +00:00
|
|
|
#include "array.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "buffer.h"
|
2019-10-25 07:36:52 +00:00
|
|
|
#include "log.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
typedef struct {
|
2019-10-25 07:36:52 +00:00
|
|
|
const array *request_header;
|
|
|
|
const array *set_request_header;
|
|
|
|
const array *response_header;
|
|
|
|
const array *set_response_header;
|
|
|
|
const array *environment;
|
|
|
|
const array *set_environment;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-25 07:36:52 +00:00
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
2016-12-22 17:32:22 +00:00
|
|
|
typedef struct {
|
2019-10-25 07:36:52 +00:00
|
|
|
int handled; /* make sure that we only apply the headers once */
|
|
|
|
plugin_config conf;
|
2016-12-22 17:32:22 +00:00
|
|
|
} handler_ctx;
|
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static handler_ctx * handler_ctx_init(void) {
|
2019-10-25 07:36:52 +00:00
|
|
|
handler_ctx * const hctx = calloc(1, sizeof(handler_ctx));
|
|
|
|
force_assert(hctx);
|
|
|
|
return hctx;
|
2006-01-14 18:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handler_ctx_free(handler_ctx *hctx) {
|
2019-10-25 07:36:52 +00:00
|
|
|
free(hctx);
|
2006-01-14 18:36:20 +00:00
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
INIT_FUNC(mod_setenv_init) {
|
2019-10-25 07:36:52 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 07:36:52 +00:00
|
|
|
static void mod_setenv_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: /* setenv.add-request-header */
|
|
|
|
pconf->request_header = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 1: /* setenv.add-response-header */
|
|
|
|
pconf->response_header = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 2: /* setenv.add-environment */
|
|
|
|
pconf->environment = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 3: /* setenv.set-request-header */
|
|
|
|
pconf->set_request_header = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 4: /* setenv.set-response-header */
|
|
|
|
pconf->set_response_header = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 5: /* setenv.set-environment */
|
|
|
|
pconf->set_environment = cpv->v.a;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2019-10-25 07:36:52 +00:00
|
|
|
static void mod_setenv_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_setenv_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_setenv_patch_config(request_st * const r, plugin_data * const p, plugin_config * const pconf) {
|
2019-10-25 07:36:52 +00:00
|
|
|
memcpy(pconf, &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-10-25 07:36:52 +00:00
|
|
|
mod_setenv_merge_config(pconf, p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-25 07:36:52 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_setenv_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("setenv.add-request-header"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-10-25 07:36:52 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("setenv.add-response-header"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-10-25 07:36:52 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("setenv.add-environment"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-10-25 07:36:52 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("setenv.set-request-header"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-10-25 07:36:52 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("setenv.set-response-header"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-10-25 07:36:52 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("setenv.set-environment"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-10-25 07:36:52 +00:00
|
|
|
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_setenv"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* future: might create custom data structures here
|
|
|
|
* then look up and store http_header_e at config time
|
|
|
|
* enum http_header_e id = http_header_hkey_get(CONST_BUF_LEN(&ds->key));
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
const 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: /* setenv.add-request-header */
|
|
|
|
case 1: /* setenv.add-response-header */
|
|
|
|
case 2: /* setenv.add-environment */
|
|
|
|
case 3: /* setenv.set-request-header */
|
|
|
|
case 4: /* setenv.set-response-header */
|
|
|
|
case 5: /* setenv.set-environment */
|
|
|
|
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_setenv_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
URIHANDLER_FUNC(mod_setenv_uri_handler) {
|
2019-10-25 07:36:52 +00:00
|
|
|
plugin_data *p = p_d;
|
2020-01-13 02:51:12 +00:00
|
|
|
handler_ctx *hctx = r->plugin_ctx[p->id];
|
2019-10-25 07:36:52 +00:00
|
|
|
if (!hctx)
|
2020-01-13 02:51:12 +00:00
|
|
|
r->plugin_ctx[p->id] = hctx = handler_ctx_init();
|
2019-10-25 07:36:52 +00:00
|
|
|
else if (hctx->handled)
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
hctx->handled = 1;
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_setenv_patch_config(r, p, &hctx->conf);
|
2019-10-25 07:36:52 +00:00
|
|
|
|
|
|
|
const array * const aa = hctx->conf.request_header;
|
|
|
|
const array * const as = hctx->conf.set_request_header;
|
|
|
|
|
|
|
|
if (aa) {
|
|
|
|
for (uint32_t k = 0; k < aa->used; ++k) {
|
|
|
|
const data_string * const ds = (const data_string *)aa->data[k];
|
|
|
|
const enum http_header_e id =
|
|
|
|
http_header_hkey_get(CONST_BUF_LEN(&ds->key));
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_request_append(r, id, CONST_BUF_LEN(&ds->key),
|
2019-10-25 07:36:52 +00:00
|
|
|
CONST_BUF_LEN(&ds->value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (as) {
|
|
|
|
for (uint32_t k = 0; k < as->used; ++k) {
|
|
|
|
const data_string * const ds = (const data_string *)as->data[k];
|
|
|
|
const enum http_header_e id =
|
|
|
|
http_header_hkey_get(CONST_BUF_LEN(&ds->key));
|
|
|
|
!buffer_string_is_empty(&ds->value)
|
2020-01-13 02:51:12 +00:00
|
|
|
? http_header_request_set(r, id, CONST_BUF_LEN(&ds->key),
|
|
|
|
CONST_BUF_LEN(&ds->value))
|
|
|
|
: http_header_request_unset(r, id, CONST_BUF_LEN(&ds->key));
|
2019-10-25 07:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2016-12-22 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
REQUEST_FUNC(mod_setenv_handle_request_env) {
|
2019-10-25 07:36:52 +00:00
|
|
|
plugin_data *p = p_d;
|
2020-01-13 02:51:12 +00:00
|
|
|
handler_ctx *hctx = r->plugin_ctx[p->id];
|
2019-10-25 07:36:52 +00:00
|
|
|
if (NULL == hctx) return HANDLER_GO_ON;
|
|
|
|
if (hctx->handled > 1) return HANDLER_GO_ON;
|
|
|
|
hctx->handled = 2;
|
|
|
|
|
|
|
|
const array * const aa = hctx->conf.environment;
|
|
|
|
const array * const as = hctx->conf.set_environment;
|
|
|
|
|
|
|
|
if (aa) {
|
|
|
|
for (uint32_t k = 0; k < hctx->conf.environment->used; ++k) {
|
|
|
|
const data_string * const ds = (const data_string *)aa->data[k];
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_env_append(r, CONST_BUF_LEN(&ds->key),
|
|
|
|
CONST_BUF_LEN(&ds->value));
|
2019-10-25 07:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (as) {
|
|
|
|
for (uint32_t k = 0; k < as->used; ++k) {
|
|
|
|
const data_string * const ds = (const data_string *)as->data[k];
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_env_set(r, CONST_BUF_LEN(&ds->key),
|
|
|
|
CONST_BUF_LEN(&ds->value));
|
2019-10-25 07:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2016-12-22 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
REQUEST_FUNC(mod_setenv_handle_response_start) {
|
2019-10-25 07:36:52 +00:00
|
|
|
plugin_data *p = p_d;
|
2020-01-13 02:51:12 +00:00
|
|
|
handler_ctx *hctx = r->plugin_ctx[p->id];
|
2019-10-25 07:36:52 +00:00
|
|
|
if (NULL == hctx) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
const array * const aa = hctx->conf.response_header;
|
|
|
|
const array * const as = hctx->conf.set_response_header;
|
|
|
|
|
|
|
|
if (aa) {
|
|
|
|
for (uint32_t k = 0; k < aa->used; ++k) {
|
|
|
|
const data_string * const ds = (const data_string *)aa->data[k];
|
|
|
|
const enum http_header_e id =
|
|
|
|
http_header_hkey_get(CONST_BUF_LEN(&ds->key));
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_insert(r, id, CONST_BUF_LEN(&ds->key),
|
|
|
|
CONST_BUF_LEN(&ds->value));
|
2019-10-25 07:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (as) {
|
|
|
|
for (uint32_t k = 0; k < as->used; ++k) {
|
|
|
|
const data_string * const ds = (const data_string *)as->data[k];
|
|
|
|
const enum http_header_e id =
|
|
|
|
http_header_hkey_get(CONST_BUF_LEN(&ds->key));
|
|
|
|
!buffer_string_is_empty(&ds->value)
|
2020-01-13 02:51:12 +00:00
|
|
|
? http_header_response_set(r, id, CONST_BUF_LEN(&ds->key),
|
|
|
|
CONST_BUF_LEN(&ds->value))
|
|
|
|
: http_header_response_unset(r, id, CONST_BUF_LEN(&ds->key));
|
2019-10-25 07:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
REQUEST_FUNC(mod_setenv_reset) {
|
|
|
|
void ** const hctx = r->plugin_ctx+((plugin_data_base *)p_d)->id;
|
2019-10-25 07:36:52 +00:00
|
|
|
if (*hctx) { handler_ctx_free(*hctx); *hctx = NULL; }
|
|
|
|
return HANDLER_GO_ON;
|
2006-01-14 18:36:20 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
int mod_setenv_plugin_init(plugin *p);
|
2005-02-20 14:27:00 +00:00
|
|
|
int mod_setenv_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
2019-10-19 04:30:54 +00:00
|
|
|
p->name = "setenv";
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->init = mod_setenv_init;
|
|
|
|
p->handle_uri_clean = mod_setenv_uri_handler;
|
2016-12-22 17:32:22 +00:00
|
|
|
p->handle_request_env = mod_setenv_handle_request_env;
|
|
|
|
p->handle_response_start = mod_setenv_handle_response_start;
|
2005-02-20 14:27:00 +00:00
|
|
|
p->set_defaults = mod_setenv_set_defaults;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-07-14 13:15:38 +00:00
|
|
|
p->connection_reset = mod_setenv_reset;
|
2006-01-14 18:36:20 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|