Apache 模块 mod_ext_filter

Description:在传递给 Client 端之前,将响应主体通过外部程序传递
Status:Extension
Module Identifier:ext_filter_module
Source File:mod_ext_filter.c

Summary

mod_ext_filterfilters提供了一个简单而熟悉的编程模型。使用此模块,从 stdin 读取并写入 stdout 的程序(即 Unix 样式的 filter 命令)可以成为 Apache 的过滤器。与使用专门为 Apache API 编写并在 Apache 服务器进程内部运行的过滤器相比,此过滤机制要慢得多,但是它确实具有以下好处:

  • 编程模型要简单得多

  • 可以使用任何编程/脚本语言,只要它允许程序从标准 Importing 读取并写入标准输出即可

  • 现有程序可以未经修改用作 Apache 过滤器

即使性能 Feature 不适合生产使用,mod_ext_filter也可以用作过滤器的原型环境。

Examples

从其他类型的响应生成 HTML

# 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>

使用 sed 替换响应中的文本

# 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>

Note

您可以使用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 Directive

Description:定义一个外部过滤器
Syntax:ExtFilterDefine filtername parameters
Context:server config
Status:Extension
Module:mod_ext_filter

ExtFilterDefine指令定义了外部过滤器的 Feature,包括要运行的程序及其参数。

filtername 指定要定义的过滤器的名称。然后可以在SetOutputFilter指令中使用该名称。在所有已注册的过滤器中,它必须是唯一的。 目前,寄存器过滤器 API 尚未报告任何错误,因此不会向用户报告名称重复的问题.

后续参数可以按任何 Sequences 出现,并定义要运行的外部命令和某些其他 Feature。唯一需要的参数是cmd=。这些参数是:

  • cmd=cmdline

    • cmd=关键字允许您指定要运行的外部命令。如果程序名称后面有参数,则命令行应用引号引起来(例如cmd="/bin/mypgm arg1 arg2")。由于程序是直接运行而绕过 Shell,因此不需要常规的 Shell 引号。程序参数以空格分隔。反斜杠可用于转义空格,空格应作为程序参数的一部分。作为参数一部分的任何反斜杠都必须自己使用反斜杠进行转义。除了标准的 CGI 环境变量外,还将为程序设置 DOCUMENT_URI,DOCUMENT_PATH_INFO 和 QUERY_STRING_UNESCAPED。
  • mode=mode

    • 对处理响应的过滤器使用mode=output(默认值)。将mode=input用于处理请求的过滤器。 mode=input在 Apache 2.1 和更高版本中可用。
  • intype=imt

    • 此参数指定应过滤的文档的 Internet 媒体类型(,即,MIME 类型)。默认情况下,将过滤所有文档。如果指定intype=,则将禁用其他类型文档的过滤器。
  • outtype=imt

    • 此参数指定已过滤文档的 Internet 媒体类型(,即,MIME 类型)。当筛选器更改 Internet 媒体类型作为筛选操作的一部分时,它很有用。默认情况下,Internet 媒体类型不变。
  • PreservesContentLength

    • PreservesContentLength关键字指定过滤器保留内容长度。这不是默认设置,因为大多数过滤器都会更改内容长度。如果过滤器未修改长度,则应指定此关键字。
  • ftype=filtertype

    • 此参数指定应将过滤器注册为的过滤器类型的数值。在大多数情况下,默认值 AP_FTYPE_RESOURCE 就足够了。如果过滤器需要在过滤器链中与资源过滤器不同的地方运行,那么此参数将是必需的。有关适当的值,请参见 util_filter.h 中的 AP_FTYPE_foo 定义。
  • disableenv=env

    • 此参数指定环境变量的名称,如果设置该名称,则将禁用过滤器。
  • enableenv=env

    • 此参数指定必须设置的环境变量的名称,否则将禁用过滤器。

ExtFilterOptions Directive

Description:配置mod_ext_filter选项
Syntax:ExtFilterOptions option [option] ...
Default:ExtFilterOptions NoLogStderr
Context:directory
Status:Extension
Module:mod_ext_filter

ExtFilterOptions指令为mod_ext_filter指定特殊的处理选项。选项可以是以下之一

  • LogStderr | NoLogStderr

    • LogStderr关键字指定由外部过滤器程序写入标准错误的消息将保存在 Apache 错误日志中。 NoLogStderr禁用此功能。
  • Onfail=[abort|remove]

    • 确定如果无法启动外部过滤器程序,如何进行。使用abort(默认值),请求将被中止。使用remove,将删除过滤器,并且在没有过滤器的情况下 continue 请求。
ExtFilterOptions LogStderr

写入过滤器标准错误的消息将存储在 Apache 错误日志中。