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_filter.xml.meta">
25 <name>mod_filter</name>
26 <description>Context-sensitive smart filter configuration module</description>
28 <sourcefile>mod_filter.c</sourcefile>
29 <identifier>filter_module</identifier>
30 <compatibility>Version 2.1 and later</compatibility>
33 <p>This module enables smart, context-sensitive configuration of
34 output content filters. For example, apache can be configured to
35 process different content-types through different filters, even
36 when the content-type is not known in advance (e.g. in a proxy).</p>
38 <p><module>mod_filter</module> works by introducing indirection into
39 the filter chain. Instead of inserting filters in the chain, we insert
40 a filter harness which in turn dispatches conditionally
41 to a filter provider. Any content filter may be used as a provider
42 to <module>mod_filter</module>; no change to existing filter modules is
43 required (although it may be possible to simplify them).</p>
46 <section id="smart"><title>Smart Filtering</title>
47 <p>In the traditional filtering model, filters are inserted unconditionally
48 using <directive module="mod_mime">AddOutputFilter</directive> and family.
49 Each filter then needs to determine whether to run, and there is little
50 flexibility available for server admins to allow the chain to be
51 configured dynamically.</p>
53 <p><module>mod_filter</module> by contrast gives server administrators a
54 great deal of flexibility in configuring the filter chain. In fact,
55 filters can be inserted based on any Request Header, Response Header
56 or Environment Variable. This generalises the limited flexibility offered
57 by <directive>AddOutputFilterByType</directive>, and fixes
58 it to work correctly with dynamic content, regardless of the
59 content generator. The ability to dispatch based on Environment
60 Variables offers the full flexibility of configuration with
61 <module>mod_rewrite</module> to anyone who needs it.</p>
64 <section id="terms"><title>Filter Declarations, Providers and Chains</title>
66 <img src="../images/mod_filter_old.gif" width="160" height="310"
67 alt="[This image displays the traditional filter model]"/><br />
68 <dfn>Figure 1:</dfn> The traditional filter model</p>
70 <p>In the traditional model, output filters are a simple chain
71 from the content generator (handler) to the client. This works well
72 provided the filter chain can be correctly configured, but presents
73 problems when the filters need to be configured dynamically based on
74 the outcome of the handler.</p>
77 <img src="../images/mod_filter_new.gif" width="423" height="331"
78 alt="[This image shows the mod_filter model]"/><br />
79 <dfn>Figure 2:</dfn> The <module>mod_filter</module> model</p>
81 <p><module>mod_filter</module> works by introducing indirection into
82 the filter chain. Instead of inserting filters in the chain, we insert
83 a filter harness which in turn dispatches conditionally
84 to a filter provider. Any content filter may be used as a provider
85 to <module>mod_filter</module>; no change to existing filter modules
86 is required (although it may be possible to simplify them). There can be
87 multiple providers for one filter, but no more than one provider will
88 run for any single request.</p>
90 <p>A filter chain comprises any number of instances of the filter
91 harness, each of which may have any number of providers. A special
92 case is that of a single provider with unconditional dispatch: this
93 is equivalent to inserting the provider filter directly into the chain.</p>
96 <section id="config"><title>Configuring the Chain</title>
97 <p>There are three stages to configuring a filter chain with
98 <module>mod_filter</module>. For details of the directives, see below.</p>
101 <dt>Declare Filters</dt>
102 <dd>The <directive module="mod_filter">FilterDeclare</directive> directive
103 declares a filter, assigning it a name and filter type. Required
104 only if the filter is not the default type AP_FTYPE_RESOURCE.</dd>
106 <dt>Register Providers</dt>
107 <dd>The <directive module="mod_filter">FilterProvider</directive>
108 directive registers a provider with a filter. The filter may have
109 been declared with <directive module="mod_filter"
110 >FilterDeclare</directive>; if not, FilterProvider will implicitly
111 declare it with the default type AP_FTYPE_RESOURCE. The provider
113 registered with <code>ap_register_output_filter</code> by some module.
114 The final argument to <directive module="mod_filter"
115 >FilterProvider</directive> is an expression: the provider will be
116 selected to run for a request if and only if the expression evaluates
117 to true. The expression may evaluate HTTP request or response
118 headers, environment variables, or the Handler used by this request.
119 Unlike earlier versions, mod_filter now supports complex expressions
120 involving multiple criteria with AND / OR logic (&& / ||)
121 and brackets. The details of the expression syntax are described in
122 the <a href="../expr.html">ap_expr documentation</a>.</dd>
124 <dt>Configure the Chain</dt>
125 <dd>The above directives build components of a smart filter chain,
126 but do not configure it to run. The <directive module="mod_filter"
127 >FilterChain</directive> directive builds a filter chain from smart
128 filters declared, offering the flexibility to insert filters at the
129 beginning or end of the chain, remove a filter, or clear the chain.</dd>
132 <section id="errordocs"><title>Filtering and Response Status</title>
133 <p>mod_filter normally only runs filters on responses with
134 HTTP status 200 (OK). If you want to filter documents with
135 other response statuses, you can set the <var>filter-errordocs</var>
136 environment variable, and it will work on all responses
137 regardless of status. To refine this further, you can use
138 expression conditions with <directive>FilterProvider</directive>.</p>
140 <section id="upgrade"><title>Upgrading from Apache HTTP Server 2.2 Configuration</title>
141 <p>The <directive module="mod_filter">FilterProvider</directive>
142 directive has changed from httpd 2.2: the <var>match</var> and
143 <var>dispatch</var> arguments are replaced with a single but
144 more versatile <var>expression</var>. In general, you can convert
145 a match/dispatch pair to the two sides of an expression, using
147 <example>"dispatch = 'match'"</example>
148 <p>The Request headers, Response headers and Environment variables
149 are now interpreted from syntax <var>%{req:foo}</var>,
150 <var>%{resp:foo}</var> and <var>%{env:foo}</var> respectively.
151 The variables <var>%{HANDLER}</var> and <var>%{CONTENT_TYPE}</var>
152 are also supported.</p>
153 <p>Note that the match no longer support substring matches. They can be
154 replaced by regular expression matches.</p>
157 <section id="examples"><title>Examples</title>
159 <dt>Server side Includes (SSI)</dt>
160 <dd>A simple case of replacing <directive>AddOutputFilterByType</directive>
162 FilterDeclare SSI<br/>
163 FilterProvider SSI INCLUDES "%{CONTENT_TYPE} =~ m|^text/html|"<br/>
168 <dt>Server side Includes (SSI)</dt>
169 <dd>The same as the above but dispatching on handler (classic
170 SSI behaviour; .shtml files get processed).
172 FilterProvider SSI INCLUDES "%{HANDLER} = 'server-parsed'"<br/>
177 <dt>Emulating mod_gzip with mod_deflate</dt>
178 <dd>Insert INFLATE filter only if "gzip" is NOT in the
179 Accept-Encoding header. This filter runs with ftype CONTENT_SET.
181 FilterDeclare gzip CONTENT_SET<br/>
182 FilterProvider gzip inflate "%{req:Accept-Encoding} !~ /gzip/"<br/>
187 <dt>Image Downsampling</dt>
188 <dd>Suppose we want to downsample all web images, and have filters
189 for GIF, JPEG and PNG.
191 FilterProvider unpack jpeg_unpack "%{CONTENT_TYPE} = 'image/jpeg'"<br/>
192 FilterProvider unpack gif_unpack "%{CONTENT_TYPE} = 'image/gif'"<br/>
193 FilterProvider unpack png_unpack "%{CONTENT_TYPE} = 'image/png'"<br/>
195 FilterProvider downsample downsample_filter "%{CONTENT_TYPE} = m|^image/(jpeg|gif|png)|"<br/>
196 FilterProtocol downsample "change=yes"<br/>
198 FilterProvider repack jpeg_pack "%{CONTENT_TYPE} = 'image/jpeg'"<br/>
199 FilterProvider repack gif_pack "%{CONTENT_TYPE} = 'image/gif'"<br/>
200 FilterProvider repack png_pack "%{CONTENT_TYPE} = 'image/png'"<br/>
201 <Location /image-filter><br/>
203 FilterChain unpack downsample repack<br/>
211 <section id="protocol"><title>Protocol Handling</title>
212 <p>Historically, each filter is responsible for ensuring that whatever
213 changes it makes are correctly represented in the HTTP response headers,
214 and that it does not run when it would make an illegal change. This
215 imposes a burden on filter authors to re-implement some common
216 functionality in every filter:</p>
219 <li>Many filters will change the content, invalidating existing content
220 tags, checksums, hashes, and lengths.</li>
222 <li>Filters that require an entire, unbroken response in input need to
223 ensure they don't get byteranges from a backend.</li>
225 <li>Filters that transform output in a filter need to ensure they don't
226 violate a <code>Cache-Control: no-transform</code> header from the
229 <li>Filters may make responses uncacheable.</li>
232 <p><module>mod_filter</module> aims to offer generic handling of these
233 details of filter implementation, reducing the complexity required of
234 content filter modules. This is work-in-progress; the
235 <directive module="mod_filter">FilterProtocol</directive> implements
236 some of this functionality for back-compatibility with Apache 2.0
237 modules. For httpd 2.1 and later, the
238 <code>ap_register_output_filter_protocol</code> and
239 <code>ap_filter_protocol</code> API enables filter modules to
240 declare their own behaviour.</p>
242 <p>At the same time, <module>mod_filter</module> should not interfere
243 with a filter that wants to handle all aspects of the protocol. By
244 default (i.e. in the absence of any <directive module="mod_filter"
245 >FilterProtocol</directive> directives), <module>mod_filter</module>
246 will leave the headers untouched.</p>
248 <p>At the time of writing, this feature is largely untested,
249 as modules in common use are designed to work with 2.0.
250 Modules using it should test it carefully.</p>
254 <name>AddOutputFilterByType</name>
255 <description>assigns an output filter to a particular media-type</description>
256 <syntax>AddOutputFilterByType <var>filter</var>[;<var>filter</var>...]
257 <var>media-type</var> [<var>media-type</var>] ...</syntax>
258 <contextlist><context>server config</context>
259 <context>virtual host</context><context>directory</context>
260 <context>.htaccess</context></contextlist>
261 <override>FileInfo</override>
262 <compatibility>Available in Apache httpd 2.0.33 and later; deprecated in Apache httpd 2.1 and later</compatibility>
265 <p>This directive activates a particular output <a
266 href="../filter.html">filter</a> for a request depending on the
267 response <glossary>media-type</glossary>. Because of certain
268 problems discussed below, this directive is deprecated. The same
269 functionality is available using <module>mod_filter</module>.</p>
271 <p>The following example uses the <code>DEFLATE</code> filter, which
272 is provided by <module>mod_deflate</module>. It will compress all
273 output (either static or dynamic) which is labeled as
274 <code>text/html</code> or <code>text/plain</code> before it is sent
278 AddOutputFilterByType DEFLATE text/html text/plain
281 <p>If you want the content to be processed by more than one filter, their
282 names have to be separated by semicolons. It's also possible to use one
283 <directive>AddOutputFilterByType</directive> directive for each of
286 <p>The configuration below causes all script output labeled as
287 <code>text/html</code> to be processed at first by the
288 <code>INCLUDES</code> filter and then by the <code>DEFLATE</code>
292 <Location /cgi-bin/><br />
294 Options Includes<br />
295 AddOutputFilterByType INCLUDES;DEFLATE text/html<br />
300 <note type="warning"><title>Note</title>
301 <p>Enabling filters with <directive>AddOutputFilterByType</directive>
302 may fail partially or completely in some cases. For example, no
303 filters are applied if the <glossary>media-type</glossary> could not
304 be determined. If you want to make sure that the filters will be
305 applied, assign the content type to a resource explicitly; for
306 example, with <directive module="mod_mime">AddType</directive> or
307 <directive module="core">ForceType</directive>. Setting the
308 content type within a (non-nph) CGI script is also safe.</p>
313 <seealso><directive module="mod_mime">AddOutputFilter</directive></seealso>
314 <seealso><directive module="core">SetOutputFilter</directive></seealso>
315 <seealso><a href="../filter.html">filters</a></seealso>
319 <name>FilterDeclare</name>
320 <description>Declare a smart filter</description>
321 <syntax>FilterDeclare <var>filter-name</var> <var>[type]</var></syntax>
322 <contextlist><context>server config</context><context>virtual host</context>
323 <context>directory</context><context>.htaccess</context></contextlist>
324 <override>Options</override>
327 <p>This directive declares an output filter together with a
328 header or environment variable that will determine runtime
329 configuration. The first argument is a <var>filter-name</var>
330 for use in <directive module="mod_filter">FilterProvider</directive>,
331 <directive module="mod_filter">FilterChain</directive> and
332 <directive module="mod_filter">FilterProtocol</directive> directives.</p>
334 <p>The final (optional) argument
335 is the type of filter, and takes values of <code>ap_filter_type</code>
336 - namely <code>RESOURCE</code> (the default), <code>CONTENT_SET</code>,
337 <code>PROTOCOL</code>, <code>TRANSCODE</code>, <code>CONNECTION</code>
338 or <code>NETWORK</code>.</p>
343 <name>FilterProvider</name>
344 <description>Register a content filter</description>
345 <syntax>FilterProvider <var>filter-name</var> <var>provider-name</var>
346 <var>expression</var></syntax>
347 <contextlist><context>server config</context><context>virtual host</context>
348 <context>directory</context><context>.htaccess</context></contextlist>
349 <override>Options</override>
352 <p>This directive registers a <em>provider</em> for the smart filter.
353 The provider will be called if and only if the <var>expression</var>
354 declared evaluates to true when the harness is first called.</p>
357 <var>provider-name</var> must have been registered by loading
358 a module that registers the name with
359 <code>ap_register_output_filter</code>.
362 <p><var>expression</var> is an
363 <a href="../expr.html">ap_expr documentation</a>.</p>
366 <seealso><a href="../expr.html">Expressions in Apache HTTP Server</a>,
367 for a complete reference and examples.</seealso>
368 <seealso><module>mod_include</module></seealso>
372 <name>FilterChain</name>
373 <description>Configure the filter chain</description>
374 <syntax>FilterChain [+=-@!]<var>filter-name</var> <var>...</var></syntax>
375 <contextlist><context>server config</context><context>virtual host</context>
376 <context>directory</context><context>.htaccess</context></contextlist>
377 <override>Options</override>
380 <p>This configures an actual filter chain, from declared filters.
381 <directive>FilterChain</directive> takes any number of arguments,
382 each optionally preceded with a single-character control that
383 determines what to do:</p>
386 <dt><code>+<var>filter-name</var></code></dt>
387 <dd>Add <var>filter-name</var> to the end of the filter chain</dd>
389 <dt><code>@<var>filter-name</var></code></dt>
390 <dd>Insert <var>filter-name</var> at the start of the filter chain</dd>
392 <dt><code>-<var>filter-name</var></code></dt>
393 <dd>Remove <var>filter-name</var> from the filter chain</dd>
395 <dt><code>=<var>filter-name</var></code></dt>
396 <dd>Empty the filter chain and insert <var>filter-name</var></dd>
398 <dt><code>!</code></dt>
399 <dd>Empty the filter chain</dd>
401 <dt><code><var>filter-name</var></code></dt>
402 <dd>Equivalent to <code>+<var>filter-name</var></code></dd>
408 <name>FilterProtocol</name>
409 <description>Deal with correct HTTP protocol handling</description>
410 <syntax>FilterProtocol <var>filter-name</var> [<var>provider-name</var>]
411 <var>proto-flags</var></syntax>
412 <contextlist><context>server config</context><context>virtual host</context>
413 <context>directory</context><context>.htaccess</context></contextlist>
414 <override>Options</override>
417 <p>This directs <module>mod_filter</module> to deal with ensuring the
418 filter doesn't run when it shouldn't, and that the HTTP response
419 headers are correctly set taking into account the effects of the
422 <p>There are two forms of this directive. With three arguments, it
423 applies specifically to a <var>filter-name</var> and a
424 <var>provider-name</var> for that filter.
425 With two arguments it applies to a <var>filter-name</var> whenever the
426 filter runs <em>any</em> provider.</p>
428 <p><var>proto-flags</var> is one or more of</p>
431 <dt><code>change=yes</code></dt>
432 <dd>The filter changes the content, including possibly the content
435 <dt><code>change=1:1</code></dt>
436 <dd>The filter changes the content, but will not change the content
439 <dt><code>byteranges=no</code></dt>
440 <dd>The filter cannot work on byteranges and requires complete input</dd>
442 <dt><code>proxy=no</code></dt>
443 <dd>The filter should not run in a proxy context</dd>
445 <dt><code>proxy=transform</code></dt>
446 <dd>The filter transforms the response in a manner incompatible with
447 the HTTP <code>Cache-Control: no-transform</code> header.</dd>
449 <dt><code>cache=no</code></dt>
450 <dd>The filter renders the output uncacheable (eg by introducing randomised
451 content changes)</dd>
457 <name>FilterTrace</name>
458 <description>Get debug/diagnostic information from
459 <module>mod_filter</module></description>
460 <syntax>FilterTrace <var>filter-name</var> <var>level</var></syntax>
461 <contextlist><context>server config</context><context>virtual host</context>
462 <context>directory</context></contextlist>
465 <p>This directive generates debug information from
466 <module>mod_filter</module>.
467 It is designed to help test and debug providers (filter modules), although
468 it may also help with <module>mod_filter</module> itself.</p>
470 <p>The debug output depends on the <var>level</var> set:</p>
472 <dt><code>0</code> (default)</dt>
473 <dd>No debug information is generated.</dd>
475 <dt><code>1</code></dt>
476 <dd><module>mod_filter</module> will record buckets and brigades
477 passing through the filter to the error log, before the provider has
478 processed them. This is similar to the information generated by
479 <a href="http://apache.webthing.com/mod_diagnostics/">mod_diagnostics</a>.
482 <dt><code>2</code> (not yet implemented)</dt>
483 <dd>Will dump the full data passing through to a tempfile before the
484 provider. <strong>For single-user debug only</strong>; this will not
485 support concurrent hits.</dd>