1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * mod_headers.c: Add/append/remove HTTP response headers
19 * Written by Paul Sutton, paul@ukweb.com, 1 Oct 1996
21 * The Header directive can be used to add/replace/remove HTTP headers
22 * within the response message. The RequestHeader directive can be used
23 * to add/replace/remove HTTP headers before a request message is processed.
24 * Valid in both per-server and per-dir configurations.
28 * Header action header value
29 * RequestHeader action header value
31 * Where action is one of:
32 * set - set this header, replacing any old value
33 * add - add this header, possible resulting in two or more
34 * headers with the same name
35 * append - append this text onto any existing header of this same
36 * unset - remove this header
38 * Where action is unset, the third argument (value) should not be given.
39 * The header name can include the colon, or not.
41 * The Header and RequestHeader directives can only be used where allowed
42 * by the FileInfo override.
44 * When the request is processed, the header directives are processed in
45 * this order: firstly, the main server, then the virtual server handling
46 * this request (if any), then any <Directory> sections (working downwards
47 * from the root dir), then an <Location> sections (working down from
48 * shortest URL component), the any <File> sections. This order is
49 * important if any 'set' or 'unset' actions are used. For example,
50 * the following two directives have different effect if applied in
53 * Header append Author "John P. Doe"
58 * To set the "Author" header, use
59 * Header add Author "John P. Doe"
68 #include "apr_strings.h"
69 #include "apr_buckets.h"
72 #define APR_WANT_STRFUNC
76 #include "http_config.h"
77 #include "http_request.h"
79 #include "util_filter.h"
80 #include "http_protocol.h" /* ap_hook_insert_error_filter */
82 /* format_tag_hash is initialized during pre-config */
83 static apr_hash_t *format_tag_hash;
86 hdr_add = 'a', /* add header (could mean multiple hdrs) */
87 hdr_set = 's', /* set (replace old value) */
88 hdr_append = 'm', /* append (merge into any old value) */
89 hdr_unset = 'u', /* unset header */
90 hdr_echo = 'e' /* echo headers from request to response */
94 * magic cmd->info values
96 static char hdr_in = '0'; /* RequestHeader */
97 static char hdr_out = '1'; /* Header onsuccess */
98 static char hdr_err = '2'; /* Header always */
101 * There is an array of struct format_tag per Header/RequestHeader
105 const char* (*func)(request_rec *r,char *arg);
110 * There is one "header_entry" per Header/RequestHeader config directive
115 apr_array_header_t *ta; /* Array of format_tag structs */
117 const char *condition_var;
120 /* echo_do is used for Header echo to iterate through the request headers*/
127 * headers_conf is our per-module configuration. This is used as both
128 * a per-dir and per-server config
131 apr_array_header_t *fixup_in;
132 apr_array_header_t *fixup_out;
133 apr_array_header_t *fixup_err;
136 module AP_MODULE_DECLARE_DATA headers_module;
139 * Tag formatting functions
141 static const char *constant_item(request_rec *r, char *stuff)
145 static const char *header_request_duration(request_rec *r, char *a)
147 return apr_psprintf(r->pool, "D=%" APR_TIME_T_FMT,
148 (apr_time_now() - r->request_time));
150 static const char *header_request_time(request_rec *r, char *a)
152 return apr_psprintf(r->pool, "t=%" APR_TIME_T_FMT, r->request_time);
154 static const char *header_request_env_var(request_rec *r, char *a)
156 const char *s = apr_table_get(r->subprocess_env,a);
166 static void *create_headers_config(apr_pool_t *p, char *dummy)
168 headers_conf *conf = apr_palloc(p, sizeof(*conf));
170 conf->fixup_in = apr_array_make(p, 2, sizeof(header_entry));
171 conf->fixup_out = apr_array_make(p, 2, sizeof(header_entry));
172 conf->fixup_err = apr_array_make(p, 2, sizeof(header_entry));
177 static void *merge_headers_config(apr_pool_t *p, void *basev, void *overridesv)
179 headers_conf *newconf = apr_palloc(p, sizeof(*newconf));
180 headers_conf *base = basev;
181 headers_conf *overrides = overridesv;
183 newconf->fixup_in = apr_array_append(p, base->fixup_in, overrides->fixup_in);
184 newconf->fixup_out = apr_array_append(p, base->fixup_out, overrides->fixup_out);
185 newconf->fixup_err = apr_array_append(p, base->fixup_err, overrides->fixup_err);
190 static char *parse_misc_string(apr_pool_t *p, format_tag *tag, const char **sa)
195 tag->func = constant_item;
198 while (*s && *s != '%') {
202 * This might allocate a few chars extra if there's a backslash
203 * escape in the format string.
205 tag->arg = apr_palloc(p, s - *sa + 1);
209 while (*s && *s != '%') {
236 * Allow the loop to deal with this *s in the normal
237 * fashion so that it handles end of string etc.
250 static char *parse_format_tag(apr_pool_t *p, format_tag *tag, const char **sa)
253 const char * (*tag_handler)(request_rec *,char *);
255 /* Handle string literal/conditionals */
257 return parse_misc_string(p, tag, sa);
259 s++; /* skip the % */
261 /* grab the argument if there is one */
264 tag->arg = ap_getword(p,&s,'}');
267 tag_handler = (const char * (*)(request_rec *,char *))apr_hash_get(format_tag_hash, s++, 1);
273 return apr_pstrcat(p, "Unrecognized Header or RequestHeader directive %",
276 tag->func = tag_handler;
283 * A format string consists of white space, text and optional format
284 * tags in any order. E.g.,
286 * Header add MyHeader "Free form text %D %t more text"
288 * Decompose the format string into its tags. Each tag (struct format_tag)
289 * contains a pointer to the function used to format the tag. Then save each
290 * tag in the tag array anchored in the header_entry.
292 static char *parse_format_string(apr_pool_t *p, header_entry *hdr, const char *s)
296 /* No string to parse with unset and copy commands */
297 if (hdr->action == hdr_unset ||
298 hdr->action == hdr_echo) {
302 hdr->ta = apr_array_make(p, 10, sizeof(format_tag));
305 if ((res = parse_format_tag(p, (format_tag *) apr_array_push(hdr->ta), &s))) {
312 /* handle RequestHeader and Header directive */
313 static const char *header_inout_cmd(cmd_parms *cmd, void *indirconf,
314 const char *action, const char *inhdr,
315 const char *value, const char* envclause)
317 headers_conf *dirconf = indirconf;
318 const char *condition_var = NULL;
320 char *hdr = apr_pstrdup(cmd->pool, inhdr);
322 apr_array_header_t *fixup = (cmd->info == &hdr_in)
323 ? dirconf->fixup_in : (cmd->info == &hdr_err)
325 : dirconf->fixup_out;
327 new = (header_entry *) apr_array_push(fixup);
329 if (!strcasecmp(action, "set"))
330 new->action = hdr_set;
331 else if (!strcasecmp(action, "add"))
332 new->action = hdr_add;
333 else if (!strcasecmp(action, "append"))
334 new->action = hdr_append;
335 else if (!strcasecmp(action, "unset"))
336 new->action = hdr_unset;
337 else if (!strcasecmp(action, "echo"))
338 new->action = hdr_echo;
340 return "first argument must be add, set, append, unset or echo.";
342 if (new->action == hdr_unset) {
344 return "header unset takes two arguments";
346 else if (new->action == hdr_echo) {
349 return "Header echo takes two arguments";
350 else if (cmd->info == &hdr_in)
351 return "Header echo only valid on Header directive";
353 regex = ap_pregcomp(cmd->pool, hdr, REG_EXTENDED | REG_NOSUB);
355 return "Header echo regex could not be compiled";
361 return "header requires three arguments";
363 /* Handle the envclause on Header */
364 if (envclause != NULL) {
365 if (strncasecmp(envclause, "env=", 4) != 0) {
366 return "error: envclause should be in the form env=envar";
368 if ((envclause[4] == '\0')
369 || ((envclause[4] == '!') && (envclause[5] == '\0'))) {
370 return "error: missing environment variable name. envclause should be in the form env=envar ";
372 condition_var = apr_pstrdup(cmd->pool, &envclause[4]);
375 if ((colon = strchr(hdr, ':')))
379 new->condition_var = condition_var;
381 return parse_format_string(cmd->pool, new, value);
384 /* Handle all (xxx)Header directives */
385 static const char *header_cmd(cmd_parms *cmd, void *indirconf,
392 const char *envclause;
394 s = apr_pstrdup(cmd->pool, args);
395 action = ap_getword_conf(cmd->pool, &s);
396 if (cmd->info == &hdr_out) {
397 if (!strcasecmp(action, "always")) {
398 cmd->info = &hdr_err;
399 action = ap_getword_conf(cmd->pool, &s);
401 else if (!strcasecmp(action, "onsuccess")) {
402 action = ap_getword_conf(cmd->pool, &s);
405 hdr = ap_getword_conf(cmd->pool, &s);
406 val = *s ? ap_getword_conf(cmd->pool, &s) : NULL;
407 envclause = *s ? ap_getword_conf(cmd->pool, &s) : NULL;
410 return apr_pstrcat(cmd->pool, cmd->cmd->name,
411 " has too many arguments", NULL);
414 return header_inout_cmd(cmd, indirconf, action, hdr, val, envclause);
418 * Process the tags in the format string. Tags may be format specifiers
419 * (%D, %t, etc.), whitespace or text strings. For each tag, run the handler
420 * (formatter) specific to the tag. Handlers return text strings.
421 * Concatenate the return from each handler into one string that is
422 * returned from this call.
424 static char* process_tags(header_entry *hdr, request_rec *r)
430 format_tag *tag = (format_tag*) hdr->ta->elts;
432 for (i = 0; i < hdr->ta->nelts; i++) {
433 s = tag[i].func(r, tag[i].arg);
435 str = apr_pstrdup(r->pool, s);
437 str = apr_pstrcat(r->pool, str, s, NULL);
439 return str ? str : "";
442 static int echo_header(echo_do *v, const char *key, const char *val)
444 /* If the input header (key) matches the regex, echo it intact to
447 if (!ap_regexec(v->hdr->regex, key, 0, NULL, 0)) {
448 apr_table_add(v->r->headers_out, key, val);
454 static void do_headers_fixup(request_rec *r, apr_table_t *headers,
455 apr_array_header_t *fixup)
459 for (i = 0; i < fixup->nelts; ++i) {
460 header_entry *hdr = &((header_entry *) (fixup->elts))[i];
462 /* Have any conditional envar-controlled Header processing to do? */
463 if (hdr->condition_var) {
464 const char *envar = hdr->condition_var;
466 if (apr_table_get(r->subprocess_env, envar) == NULL)
470 if (apr_table_get(r->subprocess_env, &envar[1]) != NULL)
475 switch (hdr->action) {
477 apr_table_addn(headers, hdr->header, process_tags(hdr, r));
480 apr_table_mergen(headers, hdr->header, process_tags(hdr, r));
483 apr_table_setn(headers, hdr->header, process_tags(hdr, r));
486 apr_table_unset(headers, hdr->header);
493 apr_table_do((int (*) (void *, const char *, const char *))
494 echo_header, (void *) &v, r->headers_in, NULL);
501 static void ap_headers_insert_output_filter(request_rec *r)
503 headers_conf *dirconf = ap_get_module_config(r->per_dir_config,
506 if (dirconf->fixup_out->nelts || dirconf->fixup_err->nelts) {
507 ap_add_output_filter("FIXUP_HEADERS_OUT", NULL, r, r->connection);
511 static void ap_headers_insert_error_filter(request_rec *r)
513 headers_conf *dirconf = ap_get_module_config(r->per_dir_config,
516 if (dirconf->fixup_err->nelts) {
517 ap_add_output_filter("FIXUP_HEADERS_ERR", NULL, r, r->connection);
521 static apr_status_t ap_headers_output_filter(ap_filter_t *f,
522 apr_bucket_brigade *in)
524 headers_conf *dirconf = ap_get_module_config(f->r->per_dir_config,
527 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, f->r->server,
528 "headers: ap_headers_output_filter()");
531 do_headers_fixup(f->r, f->r->err_headers_out, dirconf->fixup_err);
532 do_headers_fixup(f->r, f->r->headers_out, dirconf->fixup_out);
534 /* remove ourselves from the filter chain */
535 ap_remove_output_filter(f);
537 /* send the data up the stack */
538 return ap_pass_brigade(f->next,in);
541 static apr_status_t ap_headers_error_filter(ap_filter_t *f,
542 apr_bucket_brigade *in)
544 headers_conf *dirconf = ap_get_module_config(f->r->per_dir_config,
547 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, f->r->server,
548 "headers: ap_headers_error_filter()");
551 do_headers_fixup(f->r, f->r->err_headers_out, dirconf->fixup_err);
553 /* remove ourselves from the filter chain */
554 ap_remove_output_filter(f);
556 /* send the data up the stack */
557 return ap_pass_brigade(f->next,in);
560 static apr_status_t ap_headers_fixup(request_rec *r)
562 headers_conf *dirconf = ap_get_module_config(r->per_dir_config,
566 if (dirconf->fixup_in->nelts) {
567 do_headers_fixup(r, r->headers_in, dirconf->fixup_in);
573 static const command_rec headers_cmds[] =
575 AP_INIT_RAW_ARGS("Header", header_cmd, &hdr_out, OR_FILEINFO,
576 "an optional condition, an action, header and value "
577 "followed by optional env clause"),
578 AP_INIT_RAW_ARGS("RequestHeader", header_cmd, &hdr_in, OR_FILEINFO,
579 "an action, header and value"),
583 static void register_format_tag_handler(apr_pool_t *p, char *tag, void *tag_handler, int def)
585 const void *h = apr_palloc(p, sizeof(h));
587 apr_hash_set(format_tag_hash, tag, 1, h);
589 static int header_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
591 format_tag_hash = apr_hash_make(p);
592 register_format_tag_handler(p, "D", (void*) header_request_duration, 0);
593 register_format_tag_handler(p, "t", (void*) header_request_time, 0);
594 register_format_tag_handler(p, "e", (void*) header_request_env_var, 0);
599 static void register_hooks(apr_pool_t *p)
601 ap_hook_pre_config(header_pre_config,NULL,NULL,APR_HOOK_MIDDLE);
602 ap_hook_insert_filter(ap_headers_insert_output_filter, NULL, NULL, APR_HOOK_LAST);
603 ap_hook_insert_error_filter(ap_headers_insert_error_filter, NULL, NULL, APR_HOOK_LAST);
604 ap_hook_fixups(ap_headers_fixup, NULL, NULL, APR_HOOK_LAST);
605 ap_register_output_filter("FIXUP_HEADERS_OUT", ap_headers_output_filter,
606 NULL, AP_FTYPE_CONTENT_SET);
607 ap_register_output_filter("FIXUP_HEADERS_ERR", ap_headers_error_filter,
608 NULL, AP_FTYPE_CONTENT_SET);
611 module AP_MODULE_DECLARE_DATA headers_module =
613 STANDARD20_MODULE_STUFF,
614 create_headers_config, /* dir config creater */
615 merge_headers_config, /* dir merger --- default is to override */
616 NULL, /* server config */
617 NULL, /* merge server configs */
618 headers_cmds, /* command apr_table_t */
619 register_hooks /* register hooks */