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 服务器进程内部运行的过滤器相比,此过滤机制要慢得多,但是它确实具有以下好处:

即使性能 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=。这些参数是:

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指定特殊的处理选项。选项可以是以下之一

ExtFilterOptions LogStderr

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

首页