2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Copyright 2002-2005 The Apache Software Foundation or its licensors, as
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <modulesynopsis metafile="mod_ext_filter.xml.meta">
25 <name>mod_ext_filter</name>
26 <description>Pass the response body through an external program before
27 delivery to the client</description>
28 <status>Extension</status>
29 <sourcefile>mod_ext_filter.c</sourcefile>
30 <identifier>ext_filter_module</identifier>
33 <p><module>mod_ext_filter</module> presents a simple and familiar
34 programming model for <a href="../filter.html">filters</a>. With
35 this module, a program which reads from stdin and writes to stdout
36 (i.e., a Unix-style filter command) can be a filter for
37 Apache. This filtering mechanism is much slower than using a
38 filter which is specially written for the Apache API and runs
39 inside of the Apache server process, but it does have the
40 following benefits:</p>
43 <li>the programming model is much simpler</li>
45 <li>any programming/scripting language can be used, provided
46 that it allows the program to read from standard input and
47 write to standard output</li>
49 <li>existing programs can be used unmodified as Apache
53 <p>Even when the performance characteristics are not suitable
54 for production use, <module>mod_ext_filter</module> can be used as
55 a prototype environment for filters.</p>
58 <seealso><a href="../filter.html">Filters</a></seealso>
60 <section id="examples"><title>Examples</title>
62 <section><title>Generating HTML from some other type of response</title>
64 # mod_ext_filter directive to define a filter<br />
65 # to HTML-ize text/c files using the external<br />
66 # program /usr/bin/enscript, with the type of<br />
67 # the result set to text/html<br />
68 ExtFilterDefine c-to-html mode=output \<br />
70 intype=text/c outtype=text/html \<br />
71 cmd="/usr/bin/enscript --color -W html -Ec -o - -"<br />
74 <Directory "/export/home/trawick/apacheinst/htdocs/c"><br />
76 # core directive to cause the new filter to<br />
77 # be run on output<br />
78 SetOutputFilter c-to-html<br />
80 # mod_mime directive to set the type of .c<br />
81 # files to text/c<br />
82 AddType text/c .c<br />
84 # mod_ext_filter directive to set the debug<br />
85 # level just high enough to see a log message<br />
86 # per request showing the configuration in force<br />
87 ExtFilterOptions DebugLevel=1<br />
93 <section><title>Implementing a content encoding filter</title>
94 <p>Note: this gzip example is just for the purposes of illustration.
95 Please refer to <module>mod_deflate</module> for a practical
99 # mod_ext_filter directive to define the external filter<br />
100 ExtFilterDefine gzip mode=output cmd=/bin/gzip<br />
102 <Location /gzipped><br />
104 # core directive to cause the gzip filter to be<br />
105 # run on output<br />
106 SetOutputFilter gzip<br />
108 # mod_header directive to add<br />
109 # "Content-Encoding: gzip" header field<br />
110 Header set Content-Encoding gzip<br />
116 <section><title>Slowing down the server</title>
118 # mod_ext_filter directive to define a filter<br />
119 # which runs everything through cat; cat doesn't<br />
120 # modify anything; it just introduces extra pathlength<br />
121 # and consumes more resources<br />
122 ExtFilterDefine slowdown mode=output cmd=/bin/cat \<br />
124 preservescontentlength<br />
127 <Location /><br />
129 # core directive to cause the slowdown filter to<br />
130 # be run several times on output<br />
132 SetOutputFilter slowdown;slowdown;slowdown<br />
138 <section><title>Using sed to replace text in the response</title>
140 # mod_ext_filter directive to define a filter which<br />
141 # replaces text in the response<br />
143 ExtFilterDefine fixtext mode=output intype=text/html \<br />
145 cmd="/bin/sed s/verdana/arial/g"<br />
148 <Location /><br />
150 # core directive to cause the fixtext filter to<br />
151 # be run on output<br />
152 SetOutputFilter fixtext<br />
158 <section><title>Tracing another filter</title>
160 # Trace the data read and written by mod_deflate<br />
161 # for a particular client (IP 192.168.1.31)<br />
162 # experiencing compression problems.<br />
163 # This filter will trace what goes into mod_deflate.<br />
164 ExtFilterDefine tracebefore \<br />
166 cmd="/bin/tracefilter.pl /tmp/tracebefore" \<br />
167 EnableEnv=trace_this_client<br />
170 # This filter will trace what goes after mod_deflate.<br />
171 # Note that without the ftype parameter, the default<br />
172 # filter type of AP_FTYPE_RESOURCE would cause the<br />
173 # filter to be placed *before* mod_deflate in the filter<br />
174 # chain. Giving it a numeric value slightly higher than<br />
175 # AP_FTYPE_CONTENT_SET will ensure that it is placed<br />
176 # after mod_deflate.<br />
177 ExtFilterDefine traceafter \<br />
179 cmd="/bin/tracefilter.pl /tmp/traceafter" \<br />
180 EnableEnv=trace_this_client ftype=21<br />
183 <Directory /usr/local/docs><br />
185 SetEnvIf Remote_Addr 192.168.1.31 trace_this_client<br />
186 SetOutputFilter tracebefore;deflate;traceafter<br />
191 <example><title>Here is the filter which traces the data:</title>
192 #!/usr/local/bin/perl -w<br />
195 open(SAVE, ">$ARGV[0]")<br />
197 or die "can't open $ARGV[0]: $?";<br />
200 while (<STDIN>) {<br />
210 </section> <!-- /Examples -->
213 <name>ExtFilterDefine</name>
214 <description>Define an external filter</description>
215 <syntax>ExtFilterDefine <var>filtername</var> <var>parameters</var></syntax>
216 <contextlist><context>server config</context></contextlist>
219 <p>The <directive>ExtFilterDefine</directive> directive defines the
220 characteristics of an external filter, including the program to
221 run and its arguments.</p>
223 <p><var>filtername</var> specifies the name of the filter being
224 defined. This name can then be used in <directive module="core"
225 >SetOutputFilter</directive>
226 directives. It must be unique among all registered filters.
227 <em>At the present time, no error is reported by the
228 register-filter API, so a problem with duplicate names isn't
229 reported to the user.</em></p>
231 <p>Subsequent parameters can appear in any order and define the
232 external command to run and certain other characteristics. The
233 only required parameter is <code>cmd=</code>. These parameters
237 <dt><code>cmd=<var>cmdline</var></code></dt>
239 <dd>The <code>cmd=</code> keyword allows you to specify the
240 external command to run. If there are arguments after the
241 program name, the command line should be surrounded in
242 quotation marks (<em>e.g.</em>, <code>cmd="<var>/bin/mypgm</var>
243 <var>arg1</var> <var>arg2</var>"</code>.) Normal shell quoting is
244 not necessary since the program is run directly, bypassing the shell.
245 Program arguments are blank-delimited. A backslash can be used to
246 escape blanks which should be part of a program argument. Any
247 backslashes which are part of the argument must be escaped with
248 backslash themselves. In addition to the standard CGI environment
249 variables, DOCUMENT_URI, DOCUMENT_PATH_INFO, and
250 QUERY_STRING_UNESCAPED will also be set for the program.</dd>
252 <dt><code>mode=<var>mode</var></code></dt>
254 <dd>Use <code>mode=output</code> (the default) for filters which
255 process the response. Use <code>mode=input</code> for filters
256 which process the request. <code>mode=input</code> is available
257 in Apache 2.1 and later.</dd>
259 <dt><code>intype=<var>imt</var></code></dt>
261 <dd>This parameter specifies the internet media type (<em>i.e.</em>,
262 MIME type) of documents which should be filtered. By default,
263 all documents are filtered. If <code>intype=</code> is
264 specified, the filter will be disabled for documents of other
267 <dt><code>outtype=<var>imt</var></code></dt>
269 <dd>This parameter specifies the internet media type (<em>i.e.</em>,
270 MIME type) of filtered documents. It is useful when the
271 filter changes the internet media type as part of the
272 filtering operation. By default, the internet media type is
275 <dt><code>PreservesContentLength</code></dt>
277 <dd>The <code>PreservesContentLength</code> keyword specifies
278 that the filter preserves the content length. This is not the
279 default, as most filters change the content length. In the
280 event that the filter doesn't modify the length, this keyword
281 should be specified.</dd>
283 <dt><code>ftype=<var>filtertype</var></code></dt>
285 <dd>This parameter specifies the numeric value for filter type
286 that the filter should be registered as. The default value,
287 AP_FTYPE_RESOURCE, is sufficient in most cases. If the filter
288 needs to operate at a different point in the filter chain than
289 resource filters, then this parameter will be necessary. See
290 the AP_FTYPE_foo definitions in util_filter.h for appropriate
293 <dt><code>disableenv=<var>env</var></code></dt>
295 <dd>This parameter specifies the name of an environment variable
296 which, if set, will disable the filter.</dd>
298 <dt><code>enableenv=<var>env</var></code></dt>
300 <dd>This parameter specifies the name of an environment variable
301 which must be set, or the filter will be disabled.</dd>
307 <name>ExtFilterOptions</name>
308 <description>Configure <module>mod_ext_filter</module> options</description>
309 <syntax>ExtFilterOptions <var>option</var> [<var>option</var>] ...</syntax>
310 <default>ExtFilterOptions DebugLevel=0 NoLogStderr</default>
311 <contextlist><context>directory</context></contextlist>
314 <p>The <directive>ExtFilterOptions</directive> directive specifies
315 special processing options for <module>mod_ext_filter</module>.
316 <var>Option</var> can be one of</p>
319 <dt><code>DebugLevel=<var>n</var></code></dt>
322 The <code>DebugLevel</code> keyword allows you to specify
323 the level of debug messages generated by
324 <module>mod_ext_filter</module>. By default, no debug messages
325 are generated. This is equivalent to
326 <code>DebugLevel=0</code>. With higher numbers, more debug
327 messages are generated, and server performance will be
328 degraded. The actual meanings of the numeric values are
329 described with the definitions of the DBGLVL_ constants
330 near the beginning of <code>mod_ext_filter.c</code>.
332 <p>Note: The core directive <directive module="core"
333 >LogLevel</directive> should be used to cause debug messages to
334 be stored in the Apache error log.</p>
337 <dt><code>LogStderr | NoLogStderr</code></dt>
339 <dd>The <code>LogStderr</code> keyword specifies that
340 messages written to standard error by the external filter
341 program will be saved in the Apache error log.
342 <code>NoLogStderr</code> disables this feature.</dd>
345 <example><title>Example</title>
346 ExtFilterOptions LogStderr DebugLevel=0
349 <p>Messages written to the filter's standard error will be stored
350 in the Apache error log. No debug messages will be generated by
351 <module>mod_ext_filter</module>. </p>