📄 mod_headers.c
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * mod_headers.c: Add/append/remove HTTP response headers * Written by Paul Sutton, paul@ukweb.com, 1 Oct 1996 * * The Header directive can be used to add/replace/remove HTTP headers * within the response message. The RequestHeader directive can be used * to add/replace/remove HTTP headers before a request message is processed. * Valid in both per-server and per-dir configurations. * * Syntax is: * * Header action header value * RequestHeader action header value * * Where action is one of: * set - set this header, replacing any old value * add - add this header, possible resulting in two or more * headers with the same name * append - append this text onto any existing header of this same * merge - merge this text onto any existing header of this same, * avoiding duplicate values * unset - remove this header * edit - transform the header value according to a regexp * * Where action is unset, the third argument (value) should not be given. * The header name can include the colon, or not. * * The Header and RequestHeader directives can only be used where allowed * by the FileInfo override. * * When the request is processed, the header directives are processed in * this order: firstly, the main server, then the virtual server handling * this request (if any), then any <Directory> sections (working downwards * from the root dir), then an <Location> sections (working down from * shortest URL component), the any <File> sections. This order is * important if any 'set' or 'unset' actions are used. For example, * the following two directives have different effect if applied in * the reverse order: * * Header append Author "John P. Doe" * Header unset Author * * Examples: * * To set the "Author" header, use * Header add Author "John P. Doe" * * To remove a header: * Header unset Author * */#include "apr.h"#include "apr_lib.h"#include "apr_strings.h"#include "apr_buckets.h"#include "apr_hash.h"#define APR_WANT_STRFUNC#include "apr_want.h"#include "httpd.h"#include "http_config.h"#include "http_request.h"#include "http_log.h"#include "util_filter.h"#include "http_protocol.h"#include "mod_ssl.h" /* for the ssl_var_lookup optional function defn *//* format_tag_hash is initialized during pre-config */static apr_hash_t *format_tag_hash;typedef enum { hdr_add = 'a', /* add header (could mean multiple hdrs) */ hdr_set = 's', /* set (replace old value) */ hdr_append = 'm', /* append (merge into any old value) */ hdr_merge = 'g', /* merge (merge, but avoid duplicates) */ hdr_unset = 'u', /* unset header */ hdr_echo = 'e', /* echo headers from request to response */ hdr_edit = 'r' /* change value by regexp */} hdr_actions;/* * magic cmd->info values */static char hdr_in = '0'; /* RequestHeader */static char hdr_out = '1'; /* Header onsuccess */static char hdr_err = '2'; /* Header always *//* * There is an array of struct format_tag per Header/RequestHeader * config directive */typedef struct { const char* (*func)(request_rec *r,char *arg); char *arg;} format_tag;/* 'Magic' condition_var value to run action in post_read_request */static const char* condition_early = "early";/* * There is one "header_entry" per Header/RequestHeader config directive */typedef struct { hdr_actions action; const char *header; apr_array_header_t *ta; /* Array of format_tag structs */ ap_regex_t *regex; const char *condition_var; const char *subs;} header_entry;/* echo_do is used for Header echo to iterate through the request headers*/typedef struct { request_rec *r; header_entry *hdr;} echo_do;/* edit_do is used for Header edit to iterate through the request headers */typedef struct { apr_pool_t *p; header_entry *hdr; apr_table_t *t;} edit_do;/* * headers_conf is our per-module configuration. This is used as both * a per-dir and per-server config */typedef struct { apr_array_header_t *fixup_in; apr_array_header_t *fixup_out; apr_array_header_t *fixup_err;} headers_conf;module AP_MODULE_DECLARE_DATA headers_module;/* Pointer to ssl_var_lookup, if available. */static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *header_ssl_lookup = NULL;/* * Tag formatting functions */static const char *constant_item(request_rec *r, char *stuff){ return stuff;}static const char *header_request_duration(request_rec *r, char *a){ return apr_psprintf(r->pool, "D=%" APR_TIME_T_FMT, (apr_time_now() - r->request_time));}static const char *header_request_time(request_rec *r, char *a){ return apr_psprintf(r->pool, "t=%" APR_TIME_T_FMT, r->request_time);}/* unwrap_header returns HDR with any newlines converted into * whitespace if necessary. */static const char *unwrap_header(apr_pool_t *p, const char *hdr){ if (ap_strchr_c(hdr, APR_ASCII_LF) || ap_strchr_c(hdr, APR_ASCII_CR)) { char *ptr; hdr = ptr = apr_pstrdup(p, hdr); do { if (*ptr == APR_ASCII_LF || *ptr == APR_ASCII_CR) *ptr = APR_ASCII_BLANK; } while (*ptr++); } return hdr;}static const char *header_request_env_var(request_rec *r, char *a){ const char *s = apr_table_get(r->subprocess_env,a); if (s) return unwrap_header(r->pool, s); else return "(null)";}static const char *header_request_ssl_var(request_rec *r, char *name){ if (header_ssl_lookup) { const char *val = header_ssl_lookup(r->pool, r->server, r->connection, r, name); if (val && val[0]) return unwrap_header(r->pool, val); else return "(null)"; } else { return "(null)"; }}/* * Config routines */static void *create_headers_dir_config(apr_pool_t *p, char *d){ headers_conf *conf = apr_pcalloc(p, sizeof(*conf)); conf->fixup_in = apr_array_make(p, 2, sizeof(header_entry)); conf->fixup_out = apr_array_make(p, 2, sizeof(header_entry)); conf->fixup_err = apr_array_make(p, 2, sizeof(header_entry)); return conf;}static void *merge_headers_config(apr_pool_t *p, void *basev, void *overridesv){ headers_conf *newconf = apr_pcalloc(p, sizeof(*newconf)); headers_conf *base = basev; headers_conf *overrides = overridesv; newconf->fixup_in = apr_array_append(p, base->fixup_in, overrides->fixup_in); newconf->fixup_out = apr_array_append(p, base->fixup_out, overrides->fixup_out); newconf->fixup_err = apr_array_append(p, base->fixup_err, overrides->fixup_err); return newconf;}static char *parse_misc_string(apr_pool_t *p, format_tag *tag, const char **sa){ const char *s; char *d; tag->func = constant_item; s = *sa; while (*s && *s != '%') { s++; } /* * This might allocate a few chars extra if there's a backslash * escape in the format string. */ tag->arg = apr_palloc(p, s - *sa + 1); d = tag->arg; s = *sa; while (*s && *s != '%') { if (*s != '\\') { *d++ = *s++; } else { s++; switch (*s) { case '\\': *d++ = '\\'; s++; break; case 'r': *d++ = '\r'; s++; break; case 'n': *d++ = '\n'; s++; break; case 't': *d++ = '\t'; s++; break; default: /* copy verbatim */ *d++ = '\\'; /* * Allow the loop to deal with this *s in the normal * fashion so that it handles end of string etc. * properly. */ break; } } } *d = '\0'; *sa = s; return NULL;}static char *parse_format_tag(apr_pool_t *p, format_tag *tag, const char **sa){ const char *s = *sa; const char * (*tag_handler)(request_rec *,char *); /* Handle string literal/conditionals */ if (*s != '%') { return parse_misc_string(p, tag, sa); } s++; /* skip the % */ /* Pass through %% or % at end of string as % */ if ((*s == '%') || (*s == '\0')) { tag->func = constant_item; tag->arg = "%"; if (*s) s++; *sa = s; return NULL; } tag->arg = '\0'; /* grab the argument if there is one */ if (*s == '{') { ++s; tag->arg = ap_getword(p,&s,'}'); } tag_handler = (const char * (*)(request_rec *,char *))apr_hash_get(format_tag_hash, s++, 1); if (!tag_handler) { char dummy[2]; dummy[0] = s[-1]; dummy[1] = '\0'; return apr_pstrcat(p, "Unrecognized header format %", dummy, NULL); } tag->func = tag_handler; *sa = s; return NULL;}/* * A format string consists of white space, text and optional format * tags in any order. E.g., * * Header add MyHeader "Free form text %D %t more text" * * Decompose the format string into its tags. Each tag (struct format_tag) * contains a pointer to the function used to format the tag. Then save each * tag in the tag array anchored in the header_entry. */static char *parse_format_string(apr_pool_t *p, header_entry *hdr, const char *s){ char *res; /* No string to parse with unset and echo commands */ if (hdr->action == hdr_unset || hdr->action == hdr_edit || hdr->action == hdr_echo) { return NULL; } hdr->ta = apr_array_make(p, 10, sizeof(format_tag)); while (*s) { if ((res = parse_format_tag(p, (format_tag *) apr_array_push(hdr->ta), &s))) { return res; } } return NULL;}/* handle RequestHeader and Header directive */static APR_INLINE const char *header_inout_cmd(cmd_parms *cmd, void *indirconf, const char *action, const char *hdr, const char *value, const char *subs, const char *envclause){ headers_conf *dirconf = indirconf; const char *condition_var = NULL; const char *colon; header_entry *new; apr_array_header_t *fixup = (cmd->info == &hdr_in) ? dirconf->fixup_in : (cmd->info == &hdr_err) ? dirconf->fixup_err : dirconf->fixup_out; new = (header_entry *) apr_array_push(fixup); if (!strcasecmp(action, "set")) new->action = hdr_set; else if (!strcasecmp(action, "add")) new->action = hdr_add; else if (!strcasecmp(action, "append")) new->action = hdr_append; else if (!strcasecmp(action, "merge")) new->action = hdr_merge; else if (!strcasecmp(action, "unset")) new->action = hdr_unset; else if (!strcasecmp(action, "echo")) new->action = hdr_echo; else if (!strcasecmp(action, "edit")) new->action = hdr_edit; else return "first argument must be 'add', 'set', 'append', 'merge', " "'unset', 'echo', or 'edit'."; if (new->action == hdr_edit) { if (subs == NULL) { return "Header edit requires a match and a substitution"; } new->regex = ap_pregcomp(cmd->pool, value, AP_REG_EXTENDED); if (new->regex == NULL) { return "Header edit regex could not be compiled"; } new->subs = subs; } else { /* there's no subs, so envclause is really that argument */ if (envclause != NULL) { return "Too many arguments to directive"; } envclause = subs; } if (new->action == hdr_unset) { if (value) { if (envclause) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -