1 #include "first.h" 2 3 #include "array.h" 4 #include "buffer.h" 5 #include "log.h" 6 #include "http_header.h" 7 #include "plugin.h" 8 #include "request.h" 9 10 #include <stdlib.h> 11 #include <string.h> 12 13 typedef struct { 14 const array *request_header; 15 const array *set_request_header; 16 const array *response_header; 17 const array *set_response_header; 18 const array *environment; 19 const array *set_environment; 20 } plugin_config; 21 22 typedef struct { 23 PLUGIN_DATA; 24 plugin_config defaults; 25 plugin_config conf; 26 } plugin_data; 27 28 typedef struct { 29 int handled; /* make sure that we only apply the headers once */ 30 plugin_config conf; 31 } handler_ctx; 32 33 static handler_ctx * handler_ctx_init(void) { 34 return ck_calloc(1, sizeof(handler_ctx)); 35 } 36 37 static void handler_ctx_free(handler_ctx *hctx) { 38 free(hctx); 39 } 40 41 INIT_FUNC(mod_setenv_init) { 42 return ck_calloc(1, sizeof(plugin_data)); 43 } 44 45 static void mod_setenv_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { 46 switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 47 case 0: /* setenv.add-request-header */ 48 pconf->request_header = cpv->v.a; 49 break; 50 case 1: /* setenv.add-response-header */ 51 pconf->response_header = cpv->v.a; 52 break; 53 case 2: /* setenv.add-environment */ 54 pconf->environment = cpv->v.a; 55 break; 56 case 3: /* setenv.set-request-header */ 57 pconf->set_request_header = cpv->v.a; 58 break; 59 case 4: /* setenv.set-response-header */ 60 pconf->set_response_header = cpv->v.a; 61 break; 62 case 5: /* setenv.set-environment */ 63 pconf->set_environment = cpv->v.a; 64 break; 65 default:/* should not happen */ 66 return; 67 } 68 } 69 70 static void mod_setenv_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { 71 do { 72 mod_setenv_merge_config_cpv(pconf, cpv); 73 } while ((++cpv)->k_id != -1); 74 } 75 76 static void mod_setenv_patch_config(request_st * const r, plugin_data * const p, plugin_config * const pconf) { 77 memcpy(pconf, &p->defaults, sizeof(plugin_config)); 78 for (int i = 1, used = p->nconfig; i < used; ++i) { 79 if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 80 mod_setenv_merge_config(pconf, p->cvlist + p->cvlist[i].v.u2[0]); 81 } 82 } 83 84 static void mod_setenv_prep_ext (const array * const ac) { 85 array *a; 86 *(const array **)&a = ac; 87 for (uint32_t i = 0; i < a->used; ++i) { 88 data_string * const ds = (data_string *)a->data[i]; 89 ds->ext = http_header_hkey_get(BUF_PTR_LEN(&ds->key)); 90 /*(convenience: assume list, remove line ends, if line ends after ',')*/ 91 for (char *s = ds->value.ptr; (s = strchr(s, ',')); ++s) { 92 if (s[1] == '\r') *++s = ' '; 93 if (s[1] == '\n') *++s = ' '; 94 } 95 /*(strip trailing and leading whitespace)*/ 96 const char *s = ds->value.ptr; 97 uint32_t n = buffer_clen(&ds->value); 98 while (n-- && (s[n]==' ' || s[n]=='\t' || s[n]=='\r' || s[n]=='\n')) ; 99 buffer_truncate(&ds->value, ++n); 100 s = ds->value.ptr; 101 while (*s==' ' || *s=='\t' || *s=='\r' || *s=='\n') ++s; 102 if (s != ds->value.ptr) { 103 n -= (uint32_t)(s - ds->value.ptr); 104 memmove(ds->value.ptr, s, n); 105 buffer_truncate(&ds->value, n); 106 } 107 /*(warning if value contains '\r' or '\n'; 108 * invalid in HTTP/2 and in updated HTTP/1.1 specs)*/ 109 s = ds->value.ptr; 110 if (NULL != strchr(s, '\r') || NULL != strchr(s, '\n')) { 111 log_error(NULL, __FILE__, __LINE__, 112 "WARNING: setenv.*-header contains CR and/or NL (invalid): " 113 "%s: %s", ds->key.ptr, s); 114 log_error(NULL, __FILE__, __LINE__, 115 "Use mod_magnet for finer control of request, response headers."); 116 } 117 } 118 } 119 120 SETDEFAULTS_FUNC(mod_setenv_set_defaults) { 121 static const config_plugin_keys_t cpk[] = { 122 { CONST_STR_LEN("setenv.add-request-header"), 123 T_CONFIG_ARRAY_KVSTRING, 124 T_CONFIG_SCOPE_CONNECTION } 125 ,{ CONST_STR_LEN("setenv.add-response-header"), 126 T_CONFIG_ARRAY_KVSTRING, 127 T_CONFIG_SCOPE_CONNECTION } 128 ,{ CONST_STR_LEN("setenv.add-environment"), 129 T_CONFIG_ARRAY_KVSTRING, 130 T_CONFIG_SCOPE_CONNECTION } 131 ,{ CONST_STR_LEN("setenv.set-request-header"), 132 T_CONFIG_ARRAY_KVSTRING, 133 T_CONFIG_SCOPE_CONNECTION } 134 ,{ CONST_STR_LEN("setenv.set-response-header"), 135 T_CONFIG_ARRAY_KVSTRING, 136 T_CONFIG_SCOPE_CONNECTION } 137 ,{ CONST_STR_LEN("setenv.set-environment"), 138 T_CONFIG_ARRAY_KVSTRING, 139 T_CONFIG_SCOPE_CONNECTION } 140 ,{ NULL, 0, 141 T_CONFIG_UNSET, 142 T_CONFIG_SCOPE_UNSET } 143 }; 144 145 plugin_data * const p = p_d; 146 if (!config_plugin_values_init(srv, p, cpk, "mod_setenv")) 147 return HANDLER_ERROR; 148 149 /* process and validate config directives 150 * (init i to 0 if global context; to 1 to skip empty global context) */ 151 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 152 const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 153 for (; -1 != cpv->k_id; ++cpv) { 154 switch (cpv->k_id) { 155 case 0: /* setenv.add-request-header */ 156 case 1: /* setenv.add-response-header */ 157 mod_setenv_prep_ext(cpv->v.a); 158 break; 159 case 2: /* setenv.add-environment */ 160 break; 161 case 3: /* setenv.set-request-header */ 162 case 4: /* setenv.set-response-header */ 163 mod_setenv_prep_ext(cpv->v.a); 164 break; 165 case 5: /* setenv.set-environment */ 166 break; 167 default:/* should not happen */ 168 break; 169 } 170 } 171 } 172 173 /* initialize p->defaults from global config context */ 174 if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 175 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 176 if (-1 != cpv->k_id) 177 mod_setenv_merge_config(&p->defaults, cpv); 178 } 179 180 return HANDLER_GO_ON; 181 } 182 183 URIHANDLER_FUNC(mod_setenv_uri_handler) { 184 plugin_data *p = p_d; 185 handler_ctx *hctx = r->plugin_ctx[p->id]; 186 if (!hctx) 187 r->plugin_ctx[p->id] = hctx = handler_ctx_init(); 188 else if (hctx->handled) 189 return HANDLER_GO_ON; 190 hctx->handled = 1; 191 192 mod_setenv_patch_config(r, p, &hctx->conf); 193 194 const array * const aa = hctx->conf.request_header; 195 const array * const as = hctx->conf.set_request_header; 196 197 if (aa) { 198 for (uint32_t k = 0; k < aa->used; ++k) { 199 const data_string * const ds = (const data_string *)aa->data[k]; 200 http_header_request_append(r, ds->ext, BUF_PTR_LEN(&ds->key), 201 BUF_PTR_LEN(&ds->value)); 202 } 203 } 204 205 if (as) { 206 for (uint32_t k = 0; k < as->used; ++k) { 207 const data_string * const ds = (const data_string *)as->data[k]; 208 !buffer_is_blank(&ds->value) 209 ? http_header_request_set(r, ds->ext, BUF_PTR_LEN(&ds->key), 210 BUF_PTR_LEN(&ds->value)) 211 : http_header_request_unset(r, ds->ext, BUF_PTR_LEN(&ds->key)); 212 } 213 } 214 215 return HANDLER_GO_ON; 216 } 217 218 REQUEST_FUNC(mod_setenv_handle_request_env) { 219 plugin_data *p = p_d; 220 handler_ctx *hctx = r->plugin_ctx[p->id]; 221 if (NULL == hctx) return HANDLER_GO_ON; 222 if (hctx->handled > 1) return HANDLER_GO_ON; 223 hctx->handled = 2; 224 225 const array * const aa = hctx->conf.environment; 226 const array * const as = hctx->conf.set_environment; 227 228 if (aa) { 229 for (uint32_t k = 0; k < hctx->conf.environment->used; ++k) { 230 const data_string * const ds = (const data_string *)aa->data[k]; 231 http_header_env_append(r, BUF_PTR_LEN(&ds->key), 232 BUF_PTR_LEN(&ds->value)); 233 } 234 } 235 236 if (as) { 237 for (uint32_t k = 0; k < as->used; ++k) { 238 const data_string * const ds = (const data_string *)as->data[k]; 239 http_header_env_set(r, BUF_PTR_LEN(&ds->key), 240 BUF_PTR_LEN(&ds->value)); 241 } 242 } 243 244 return HANDLER_GO_ON; 245 } 246 247 REQUEST_FUNC(mod_setenv_handle_response_start) { 248 plugin_data *p = p_d; 249 handler_ctx *hctx = r->plugin_ctx[p->id]; 250 if (NULL == hctx) return HANDLER_GO_ON; 251 252 const array * const aa = hctx->conf.response_header; 253 const array * const as = hctx->conf.set_response_header; 254 255 if (aa) { 256 for (uint32_t k = 0; k < aa->used; ++k) { 257 const data_string * const ds = (const data_string *)aa->data[k]; 258 http_header_response_insert(r, ds->ext, BUF_PTR_LEN(&ds->key), 259 BUF_PTR_LEN(&ds->value)); 260 } 261 } 262 263 if (as) { 264 for (uint32_t k = 0; k < as->used; ++k) { 265 const data_string * const ds = (const data_string *)as->data[k]; 266 !buffer_is_blank(&ds->value) 267 ? http_header_response_set(r, ds->ext, BUF_PTR_LEN(&ds->key), 268 BUF_PTR_LEN(&ds->value)) 269 : http_header_response_unset(r, ds->ext, BUF_PTR_LEN(&ds->key)); 270 } 271 } 272 273 return HANDLER_GO_ON; 274 } 275 276 REQUEST_FUNC(mod_setenv_handle_request_reset) { 277 void ** const hctx = r->plugin_ctx+((plugin_data_base *)p_d)->id; 278 if (*hctx) { handler_ctx_free(*hctx); *hctx = NULL; } 279 return HANDLER_GO_ON; 280 } 281 282 283 __attribute_cold__ 284 int mod_setenv_plugin_init(plugin *p); 285 int mod_setenv_plugin_init(plugin *p) { 286 p->version = LIGHTTPD_VERSION_ID; 287 p->name = "setenv"; 288 289 p->init = mod_setenv_init; 290 p->set_defaults= mod_setenv_set_defaults; 291 p->handle_uri_clean = mod_setenv_uri_handler; 292 p->handle_request_env = mod_setenv_handle_request_env; 293 p->handle_response_start = mod_setenv_handle_response_start; 294 p->handle_request_reset = mod_setenv_handle_request_reset; 295 296 297 return 0; 298 } 299