Apache HTTP Server 版本 2.4

| 描述 | 在将响应主体传递给客户端之前,通过外部程序传递它。 |
|---|---|
| 状态 | 扩展 |
| 模块标识符 | ext_filter_module |
| 源文件 | mod_ext_filter.c |
mod_ext_filter 为 过滤器 提供了一个简单且熟悉的编程模型。使用此模块,从 stdin 读取并写入 stdout 的程序(即 Unix 风格的过滤器命令)可以成为 Apache 的过滤器。这种过滤机制比使用专门为 Apache API 编写的并在 Apache 服务器进程内部运行的过滤器要慢得多,但它确实具有以下优点
即使性能特征不适合生产使用,mod_ext_filter 也可以用作过滤器的原型环境。
# mod_ext_filter directive to define a filter
# to HTML-ize text/c files using the external
# program /usr/bin/enscript, with the type of
# the result set to text/html
ExtFilterDefine c-to-html mode=output \
intype=text/c outtype=text/html \
cmd="/usr/bin/enscript --color -w html -Ec -o -"
<Directory "/export/home/trawick/apacheinst/htdocs/c">
# core directive to cause the new filter to
# be run on output
SetOutputFilter c-to-html
# mod_mime directive to set the type of .c
# files to text/c
AddType text/c .c
</Directory>
注意:此 gzip 示例仅用于说明目的。有关实际实现,请参阅 mod_deflate。
# mod_ext_filter directive to define the external filter
ExtFilterDefine gzip mode=output cmd=/bin/gzip
<Location "/gzipped">
# core directive to cause the gzip filter to be
# run on output
SetOutputFilter gzip
# mod_headers directive to add
# "Content-Encoding: gzip" header field
Header set Content-Encoding gzip
</Location>
# mod_ext_filter directive to define a filter
# which runs everything through cat; cat doesn't
# modify anything; it just introduces extra pathlength
# and consumes more resources
ExtFilterDefine slowdown mode=output cmd=/bin/cat \
preservescontentlength
<Location "/">
# core directive to cause the slowdown filter to
# be run several times on output
#
SetOutputFilter slowdown;slowdown;slowdown
</Location>
# mod_ext_filter directive to define a filter which
# replaces text in the response
#
ExtFilterDefine fixtext mode=output intype=text/html \
cmd="/bin/sed s/verdana/arial/g"
<Location "/">
# core directive to cause the fixtext filter to
# be run on output
SetOutputFilter fixtext
</Location>
您可以使用 mod_substitute 做同样的事情,而无需调用外部进程。
# Trace the data read and written by mod_deflate
# for a particular client (IP 192.168.1.31)
# experiencing compression problems.
# This filter will trace what goes into mod_deflate.
ExtFilterDefine tracebefore \
cmd="/bin/tracefilter.pl /tmp/tracebefore" \
EnableEnv=trace_this_client
# This filter will trace what goes after mod_deflate.
# Note that without the ftype parameter, the default
# filter type of AP_FTYPE_RESOURCE would cause the
# filter to be placed *before* mod_deflate in the filter
# chain. Giving it a numeric value slightly higher than
# AP_FTYPE_CONTENT_SET will ensure that it is placed
# after mod_deflate.
ExtFilterDefine traceafter \
cmd="/bin/tracefilter.pl /tmp/traceafter" \
EnableEnv=trace_this_client ftype=21
<Directory "/usr/local/docs">
SetEnvIf Remote_Addr 192.168.1.31 trace_this_client
SetOutputFilter tracebefore;deflate;traceafter
</Directory>
#!/usr/local/bin/perl -w
use strict;
open(SAVE, ">$ARGV[0]")
or die "can't open $ARGV[0]: $?";
while (<STDIN>) {
print SAVE $_;
print $_;
}
close(SAVE);
| 描述 | 定义外部过滤器 |
|---|---|
| 语法 | ExtFilterDefine filtername parameters |
| 上下文 | 服务器配置 |
| 状态 | 扩展 |
| 模块 | mod_ext_filter |
ExtFilterDefine 指令定义了外部过滤器的特性,包括要运行的程序及其参数。
filtername 指定要定义的过滤器的名称。此名称随后可以在 SetOutputFilter 指令中使用。它必须在所有已注册的过滤器中是唯一的。目前,注册过滤器 API 不会报告任何错误,因此重复名称的问题不会报告给用户。
后续参数可以以任何顺序出现,并定义要运行的外部命令以及某些其他特性。唯一必需的参数是 cmd=。这些参数是
cmd=cmdlinecmd= 关键字允许您指定要运行的外部命令。如果程序名称后面有参数,则命令行应放在引号中(例如,cmd="/bin/mypgm arg1 arg2")。由于程序直接运行,绕过了 shell,因此不需要正常的 shell 引号。程序参数以空格分隔。可以使用反斜杠转义应作为程序参数一部分的空格。任何作为参数一部分的反斜杠必须用反斜杠本身转义。除了标准 CGI 环境变量外,DOCUMENT_URI、DOCUMENT_PATH_INFO 和 QUERY_STRING_UNESCAPED 也将为程序设置。mode=modemode=output(默认值)。对于处理请求的过滤器,使用 mode=input。mode=input 在 Apache 2.1 及更高版本中可用。intype=imtintype=,则过滤器将对其他类型的文档禁用。outtype=imtPreservesContentLengthPreservesContentLength 关键字指定过滤器保留内容长度。这不是默认值,因为大多数过滤器都会更改内容长度。如果过滤器没有修改长度,则应指定此关键字。ftype=filtertypedisableenv=envenableenv=env| 描述 | 配置 mod_ext_filter 选项 |
|---|---|
| 语法 | ExtFilterOptions option [option] ... |
| 默认 | ExtFilterOptions NoLogStderr |
| 上下文 | 目录 |
| 状态 | 扩展 |
| 模块 | mod_ext_filter |
ExtFilterOptions 指令指定 mod_ext_filter 的特殊处理选项。 Option 可以是以下之一
LogStderr | NoLogStderrLogStderr 关键字指定将外部过滤器程序写入标准错误的消息保存到 Apache 错误日志中。NoLogStderr 禁用此功能。Onfail=[abort|remove]abort(默认值)将中止请求。使用 remove,将删除过滤器,请求将继续执行,但没有过滤器。ExtFilterOptions LogStderr
写入过滤器标准错误的消息将存储在 Apache 错误日志中。