2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. 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 />
89 <section><title>Implementing a content encoding filter</title>
90 <p>Note: this gzip example is just for the purposes of illustration.
91 Please refer to <module>mod_deflate</module> for a practical
95 # mod_ext_filter directive to define the external filter<br />
96 ExtFilterDefine gzip mode=output cmd=/bin/gzip<br />
98 <Location /gzipped><br />
100 # core directive to cause the gzip filter to be<br />
101 # run on output<br />
102 SetOutputFilter gzip<br />
104 # mod_header directive to add<br />
105 # "Content-Encoding: gzip" header field<br />
106 Header set Content-Encoding gzip<br />
112 <section><title>Slowing down the server</title>
114 # mod_ext_filter directive to define a filter<br />
115 # which runs everything through cat; cat doesn't<br />
116 # modify anything; it just introduces extra pathlength<br />
117 # and consumes more resources<br />
118 ExtFilterDefine slowdown mode=output cmd=/bin/cat \<br />
120 preservescontentlength<br />
123 <Location /><br />
125 # core directive to cause the slowdown filter to<br />
126 # be run several times on output<br />
128 SetOutputFilter slowdown;slowdown;slowdown<br />
134 <section><title>Using sed to replace text in the response</title>
136 # mod_ext_filter directive to define a filter which<br />
137 # replaces text in the response<br />
139 ExtFilterDefine fixtext mode=output intype=text/html \<br />
141 cmd="/bin/sed s/verdana/arial/g"<br />
144 <Location /><br />
146 # core directive to cause the fixtext filter to<br />
147 # be run on output<br />
148 SetOutputFilter fixtext<br />
154 <section><title>Tracing another filter</title>
156 # Trace the data read and written by mod_deflate<br />
157 # for a particular client (IP 192.168.1.31)<br />
158 # experiencing compression problems.<br />
159 # This filter will trace what goes into mod_deflate.<br />
160 ExtFilterDefine tracebefore \<br />
162 cmd="/bin/tracefilter.pl /tmp/tracebefore" \<br />
163 EnableEnv=trace_this_client<br />
166 # This filter will trace what goes after mod_deflate.<br />
167 # Note that without the ftype parameter, the default<br />
168 # filter type of AP_FTYPE_RESOURCE would cause the<br />
169 # filter to be placed *before* mod_deflate in the filter<br />
170 # chain. Giving it a numeric value slightly higher than<br />
171 # AP_FTYPE_CONTENT_SET will ensure that it is placed<br />
172 # after mod_deflate.<br />
173 ExtFilterDefine traceafter \<br />
175 cmd="/bin/tracefilter.pl /tmp/traceafter" \<br />
176 EnableEnv=trace_this_client ftype=21<br />
179 <Directory /usr/local/docs><br />
181 SetEnvIf Remote_Addr 192.168.1.31 trace_this_client<br />
182 SetOutputFilter tracebefore;deflate;traceafter<br />
187 <example><title>Here is the filter which traces the data:</title>
188 #!/usr/local/bin/perl -w<br />
191 open(SAVE, ">$ARGV[0]")<br />
193 or die "can't open $ARGV[0]: $?";<br />
196 while (<STDIN>) {<br />
206 </section> <!-- /Examples -->
209 <name>ExtFilterDefine</name>
210 <description>Define an external filter</description>
211 <syntax>ExtFilterDefine <var>filtername</var> <var>parameters</var></syntax>
212 <contextlist><context>server config</context></contextlist>
215 <p>The <directive>ExtFilterDefine</directive> directive defines the
216 characteristics of an external filter, including the program to
217 run and its arguments.</p>
219 <p><var>filtername</var> specifies the name of the filter being
220 defined. This name can then be used in <directive module="core"
221 >SetOutputFilter</directive>
222 directives. It must be unique among all registered filters.
223 <em>At the present time, no error is reported by the
224 register-filter API, so a problem with duplicate names isn't
225 reported to the user.</em></p>
227 <p>Subsequent parameters can appear in any order and define the
228 external command to run and certain other characteristics. The
229 only required parameter is <code>cmd=</code>. These parameters
233 <dt><code>cmd=<var>cmdline</var></code></dt>
235 <dd>The <code>cmd=</code> keyword allows you to specify the
236 external command to run. If there are arguments after the
237 program name, the command line should be surrounded in
238 quotation marks (<em>e.g.</em>, <code>cmd="<var>/bin/mypgm</var>
239 <var>arg1</var> <var>arg2</var>"</code>.) Normal shell quoting is
240 not necessary since the program is run directly, bypassing the shell.
241 Program arguments are blank-delimited. A backslash can be used to
242 escape blanks which should be part of a program argument. Any
243 backslashes which are part of the argument must be escaped with
244 backslash themselves. In addition to the standard CGI environment
245 variables, DOCUMENT_URI, DOCUMENT_PATH_INFO, and
246 QUERY_STRING_UNESCAPED will also be set for the program.</dd>
248 <dt><code>mode=<var>mode</var></code></dt>
250 <dd>Use <code>mode=output</code> (the default) for filters which
251 process the response. Use <code>mode=input</code> for filters
252 which process the request. <code>mode=input</code> is available
253 in Apache 2.1 and later.</dd>
255 <dt><code>intype=<var>imt</var></code></dt>
257 <dd>This parameter specifies the internet media type (<em>i.e.</em>,
258 MIME type) of documents which should be filtered. By default,
259 all documents are filtered. If <code>intype=</code> is
260 specified, the filter will be disabled for documents of other
263 <dt><code>outtype=<var>imt</var></code></dt>
265 <dd>This parameter specifies the internet media type (<em>i.e.</em>,
266 MIME type) of filtered documents. It is useful when the
267 filter changes the internet media type as part of the
268 filtering operation. By default, the internet media type is
271 <dt><code>PreservesContentLength</code></dt>
273 <dd>The <code>PreservesContentLength</code> keyword specifies
274 that the filter preserves the content length. This is not the
275 default, as most filters change the content length. In the
276 event that the filter doesn't modify the length, this keyword
277 should be specified.</dd>
279 <dt><code>ftype=<var>filtertype</var></code></dt>
281 <dd>This parameter specifies the numeric value for filter type
282 that the filter should be registered as. The default value,
283 AP_FTYPE_RESOURCE, is sufficient in most cases. If the filter
284 needs to operate at a different point in the filter chain than
285 resource filters, then this parameter will be necessary. See
286 the AP_FTYPE_foo definitions in util_filter.h for appropriate
289 <dt><code>disableenv=<var>env</var></code></dt>
291 <dd>This parameter specifies the name of an environment variable
292 which, if set, will disable the filter.</dd>
294 <dt><code>enableenv=<var>env</var></code></dt>
296 <dd>This parameter specifies the name of an environment variable
297 which must be set, or the filter will be disabled.</dd>
303 <name>ExtFilterOptions</name>
304 <description>Configure <module>mod_ext_filter</module> options</description>
305 <syntax>ExtFilterOptions <var>option</var> [<var>option</var>] ...</syntax>
306 <default>ExtFilterOptions NoLogStderr</default>
307 <contextlist><context>directory</context></contextlist>
310 <p>The <directive>ExtFilterOptions</directive> directive specifies
311 special processing options for <module>mod_ext_filter</module>.
312 <var>Option</var> can be one of</p>
315 <dt><code>LogStderr | NoLogStderr</code></dt>
317 <dd>The <code>LogStderr</code> keyword specifies that
318 messages written to standard error by the external filter
319 program will be saved in the Apache error log.
320 <code>NoLogStderr</code> disables this feature.</dd>
322 <dt><code>Onfail=[abort|remove]</code></dt>
323 <dd>Determines how to proceed if the external filter program
324 cannot be started. With <code>abort</code> (the default value)
325 the request will be aborted. With <code>remove</code>, the
326 filter is removed and the request continues without it.</dd>
329 <example><title>Example</title>
330 ExtFilterOptions LogStderr
333 <p>Messages written to the filter's standard error will be stored
334 in the Apache error log.</p>