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>
32 <p>This module enables smart, context-sensitive configuration of
33 output content filters. For example, apache can be configured to
34 process different content-types through different filters, even
35 when the content-type is not known in advance (e.g. in a proxy).</p>
37 <p><module>mod_filter</module> works by introducing indirection into
38 the filter chain. Instead of inserting filters in the chain, we insert
39 a filter harness which in turn dispatches conditionally
40 to a filter provider. Any content filter may be used as a provider
41 to <module>mod_filter</module>; no change to existing filter modules is
42 required (although it may be possible to simplify them).</p>
45 <section id="smart"><title>Smart Filtering</title>
46 <p>In the traditional filtering model, filters are inserted unconditionally
47 using <directive module="mod_mime">AddOutputFilter</directive> and family.
48 Each filter then needs to determine whether to run, and there is little
49 flexibility available for server admins to allow the chain to be
50 configured dynamically.</p>
52 <p><module>mod_filter</module> by contrast gives server administrators a
53 great deal of flexibility in configuring the filter chain. In fact,
54 filters can be inserted based on complex boolean
55 <a href="../expr.html">expressions</a> This generalises the limited
56 flexibility offered by <directive>AddOutputFilterByType</directive>.</p>
59 <section id="terms"><title>Filter Declarations, Providers and Chains</title>
61 <img src="../images/mod_filter_old.gif" width="160" height="310"
62 alt="[This image displays the traditional filter model]"/><br />
63 <dfn>Figure 1:</dfn> The traditional filter model</p>
65 <p>In the traditional model, output filters are a simple chain
66 from the content generator (handler) to the client. This works well
67 provided the filter chain can be correctly configured, but presents
68 problems when the filters need to be configured dynamically based on
69 the outcome of the handler.</p>
72 <img src="../images/mod_filter_new.gif" width="423" height="331"
73 alt="[This image shows the mod_filter model]"/><br />
74 <dfn>Figure 2:</dfn> The <module>mod_filter</module> model</p>
76 <p><module>mod_filter</module> works by introducing indirection into
77 the filter chain. Instead of inserting filters in the chain, we insert
78 a filter harness which in turn dispatches conditionally
79 to a filter provider. Any content filter may be used as a provider
80 to <module>mod_filter</module>; no change to existing filter modules
81 is required (although it may be possible to simplify them). There can be
82 multiple providers for one filter, but no more than one provider will
83 run for any single request.</p>
85 <p>A filter chain comprises any number of instances of the filter
86 harness, each of which may have any number of providers. A special
87 case is that of a single provider with unconditional dispatch: this
88 is equivalent to inserting the provider filter directly into the chain.</p>
91 <section id="config"><title>Configuring the Chain</title>
92 <p>There are three stages to configuring a filter chain with
93 <module>mod_filter</module>. For details of the directives, see below.</p>
96 <dt>Declare Filters</dt>
97 <dd>The <directive module="mod_filter">FilterDeclare</directive> directive
98 declares a filter, assigning it a name and filter type. Required
99 only if the filter is not the default type AP_FTYPE_RESOURCE.</dd>
101 <dt>Register Providers</dt>
102 <dd>The <directive module="mod_filter">FilterProvider</directive>
103 directive registers a provider with a filter. The filter may have
104 been declared with <directive module="mod_filter"
105 >FilterDeclare</directive>; if not, FilterProvider will implicitly
106 declare it with the default type AP_FTYPE_RESOURCE. The provider
108 registered with <code>ap_register_output_filter</code> by some module.
109 The final argument to <directive module="mod_filter"
110 >FilterProvider</directive> is an expression: the provider will be
111 selected to run for a request if and only if the expression evaluates
112 to true. The expression may evaluate HTTP request or response
113 headers, environment variables, or the Handler used by this request.
114 Unlike earlier versions, mod_filter now supports complex expressions
115 involving multiple criteria with AND / OR logic (&& / ||)
116 and brackets. The details of the expression syntax are described in
117 the <a href="../expr.html">ap_expr documentation</a>.</dd>
119 <dt>Configure the Chain</dt>
120 <dd>The above directives build components of a smart filter chain,
121 but do not configure it to run. The <directive module="mod_filter"
122 >FilterChain</directive> directive builds a filter chain from smart
123 filters declared, offering the flexibility to insert filters at the
124 beginning or end of the chain, remove a filter, or clear the chain.</dd>
127 <section id="errordocs"><title>Filtering and Response Status</title>
128 <p>mod_filter normally only runs filters on responses with
129 HTTP status 200 (OK). If you want to filter documents with
130 other response statuses, you can set the <var>filter-errordocs</var>
131 environment variable, and it will work on all responses
132 regardless of status. To refine this further, you can use
133 expression conditions with <directive>FilterProvider</directive>.</p>
135 <section id="upgrade"><title>Upgrading from Apache HTTP Server 2.2 Configuration</title>
136 <p>The <directive module="mod_filter">FilterProvider</directive>
137 directive has changed from httpd 2.2: the <var>match</var> and
138 <var>dispatch</var> arguments are replaced with a single but
139 more versatile <var>expression</var>. In general, you can convert
140 a match/dispatch pair to the two sides of an expression, using
142 <example>"dispatch = 'match'"</example>
143 <p>The Request headers, Response headers and Environment variables
144 are now interpreted from syntax <var>%{req:foo}</var>,
145 <var>%{resp:foo}</var> and <var>%{env:foo}</var> respectively.
146 The variables <var>%{HANDLER}</var> and <var>%{CONTENT_TYPE}</var>
147 are also supported.</p>
148 <p>Note that the match no longer support substring matches. They can be
149 replaced by regular expression matches.</p>
152 <section id="examples"><title>Examples</title>
154 <dt>Server side Includes (SSI)</dt>
155 <dd>A simple case of replacing <directive>AddOutputFilterByType</directive>
156 <highlight language="config">
158 FilterProvider SSI INCLUDES "%{CONTENT_TYPE} =~ m|^text/html|"
163 <dt>Server side Includes (SSI)</dt>
164 <dd>The same as the above but dispatching on handler (classic
165 SSI behaviour; .shtml files get processed).
166 <highlight language="config">
167 FilterProvider SSI INCLUDES "%{HANDLER} = 'server-parsed'"
172 <dt>Emulating mod_gzip with mod_deflate</dt>
173 <dd>Insert INFLATE filter only if "gzip" is NOT in the
174 Accept-Encoding header. This filter runs with ftype CONTENT_SET.
175 <highlight language="config">
176 FilterDeclare gzip CONTENT_SET
177 FilterProvider gzip inflate "%{req:Accept-Encoding} !~ /gzip/"
182 <dt>Image Downsampling</dt>
183 <dd>Suppose we want to downsample all web images, and have filters
184 for GIF, JPEG and PNG.
185 <highlight language="config">
186 FilterProvider unpack jpeg_unpack "%{CONTENT_TYPE} = 'image/jpeg'"
187 FilterProvider unpack gif_unpack "%{CONTENT_TYPE} = 'image/gif'"
188 FilterProvider unpack png_unpack "%{CONTENT_TYPE} = 'image/png'"
190 FilterProvider downsample downsample_filter "%{CONTENT_TYPE} = m|^image/(jpeg|gif|png)|"
191 FilterProtocol downsample "change=yes"
193 FilterProvider repack jpeg_pack "%{CONTENT_TYPE} = 'image/jpeg'"
194 FilterProvider repack gif_pack "%{CONTENT_TYPE} = 'image/gif'"
195 FilterProvider repack png_pack "%{CONTENT_TYPE} = 'image/png'"
196 <Location /image-filter>
197 FilterChain unpack downsample repack
204 <section id="protocol"><title>Protocol Handling</title>
205 <p>Historically, each filter is responsible for ensuring that whatever
206 changes it makes are correctly represented in the HTTP response headers,
207 and that it does not run when it would make an illegal change. This
208 imposes a burden on filter authors to re-implement some common
209 functionality in every filter:</p>
212 <li>Many filters will change the content, invalidating existing content
213 tags, checksums, hashes, and lengths.</li>
215 <li>Filters that require an entire, unbroken response in input need to
216 ensure they don't get byteranges from a backend.</li>
218 <li>Filters that transform output in a filter need to ensure they don't
219 violate a <code>Cache-Control: no-transform</code> header from the
222 <li>Filters may make responses uncacheable.</li>
225 <p><module>mod_filter</module> aims to offer generic handling of these
226 details of filter implementation, reducing the complexity required of
227 content filter modules. This is work-in-progress; the
228 <directive module="mod_filter">FilterProtocol</directive> implements
229 some of this functionality for back-compatibility with Apache 2.0
230 modules. For httpd 2.1 and later, the
231 <code>ap_register_output_filter_protocol</code> and
232 <code>ap_filter_protocol</code> API enables filter modules to
233 declare their own behaviour.</p>
235 <p>At the same time, <module>mod_filter</module> should not interfere
236 with a filter that wants to handle all aspects of the protocol. By
237 default (i.e. in the absence of any <directive module="mod_filter"
238 >FilterProtocol</directive> directives), <module>mod_filter</module>
239 will leave the headers untouched.</p>
241 <p>At the time of writing, this feature is largely untested,
242 as modules in common use are designed to work with 2.0.
243 Modules using it should test it carefully.</p>
247 <name>AddOutputFilterByType</name>
248 <description>assigns an output filter to a particular media-type</description>
249 <syntax>AddOutputFilterByType <var>filter</var>[;<var>filter</var>...]
250 <var>media-type</var> [<var>media-type</var>] ...</syntax>
251 <contextlist><context>server config</context>
252 <context>virtual host</context><context>directory</context>
253 <context>.htaccess</context></contextlist>
254 <override>FileInfo</override>
255 <compatibility>Had severe limitations before
256 being moved to <module>mod_filter</module> in version 2.3.7</compatibility>
259 <p>This directive activates a particular output <a
260 href="../filter.html">filter</a> for a request depending on the
261 response <glossary>media-type</glossary>.</p>
263 <p>The following example uses the <code>DEFLATE</code> filter, which
264 is provided by <module>mod_deflate</module>. It will compress all
265 output (either static or dynamic) which is labeled as
266 <code>text/html</code> or <code>text/plain</code> before it is sent
269 <highlight language="config">
270 AddOutputFilterByType DEFLATE text/html text/plain
273 <p>If you want the content to be processed by more than one filter, their
274 names have to be separated by semicolons. It's also possible to use one
275 <directive>AddOutputFilterByType</directive> directive for each of
278 <p>The configuration below causes all script output labeled as
279 <code>text/html</code> to be processed at first by the
280 <code>INCLUDES</code> filter and then by the <code>DEFLATE</code>
283 <highlight language="config">
284 <Location /cgi-bin/>
286 AddOutputFilterByType INCLUDES;DEFLATE text/html
292 <seealso><directive module="mod_mime">AddOutputFilter</directive></seealso>
293 <seealso><directive module="core">SetOutputFilter</directive></seealso>
294 <seealso><a href="../filter.html">filters</a></seealso>
298 <name>FilterDeclare</name>
299 <description>Declare a smart filter</description>
300 <syntax>FilterDeclare <var>filter-name</var> <var>[type]</var></syntax>
301 <contextlist><context>server config</context><context>virtual host</context>
302 <context>directory</context><context>.htaccess</context></contextlist>
303 <override>Options</override>
306 <p>This directive declares an output filter together with a
307 header or environment variable that will determine runtime
308 configuration. The first argument is a <var>filter-name</var>
309 for use in <directive module="mod_filter">FilterProvider</directive>,
310 <directive module="mod_filter">FilterChain</directive> and
311 <directive module="mod_filter">FilterProtocol</directive> directives.</p>
313 <p>The final (optional) argument
314 is the type of filter, and takes values of <code>ap_filter_type</code>
315 - namely <code>RESOURCE</code> (the default), <code>CONTENT_SET</code>,
316 <code>PROTOCOL</code>, <code>TRANSCODE</code>, <code>CONNECTION</code>
317 or <code>NETWORK</code>.</p>
322 <name>FilterProvider</name>
323 <description>Register a content filter</description>
324 <syntax>FilterProvider <var>filter-name</var> <var>provider-name</var>
325 <var>expression</var></syntax>
326 <contextlist><context>server config</context><context>virtual host</context>
327 <context>directory</context><context>.htaccess</context></contextlist>
328 <override>Options</override>
331 <p>This directive registers a <em>provider</em> for the smart filter.
332 The provider will be called if and only if the <var>expression</var>
333 declared evaluates to true when the harness is first called.</p>
336 <var>provider-name</var> must have been registered by loading
337 a module that registers the name with
338 <code>ap_register_output_filter</code>.
341 <p><var>expression</var> is an
342 <a href="../expr.html">ap_expr</a>.</p>
345 <seealso><a href="../expr.html">Expressions in Apache HTTP Server</a>,
346 for a complete reference and examples.</seealso>
347 <seealso><module>mod_include</module></seealso>
351 <name>FilterChain</name>
352 <description>Configure the filter chain</description>
353 <syntax>FilterChain [+=-@!]<var>filter-name</var> <var>...</var></syntax>
354 <contextlist><context>server config</context><context>virtual host</context>
355 <context>directory</context><context>.htaccess</context></contextlist>
356 <override>Options</override>
359 <p>This configures an actual filter chain, from declared filters.
360 <directive>FilterChain</directive> takes any number of arguments,
361 each optionally preceded with a single-character control that
362 determines what to do:</p>
365 <dt><code>+<var>filter-name</var></code></dt>
366 <dd>Add <var>filter-name</var> to the end of the filter chain</dd>
368 <dt><code>@<var>filter-name</var></code></dt>
369 <dd>Insert <var>filter-name</var> at the start of the filter chain</dd>
371 <dt><code>-<var>filter-name</var></code></dt>
372 <dd>Remove <var>filter-name</var> from the filter chain</dd>
374 <dt><code>=<var>filter-name</var></code></dt>
375 <dd>Empty the filter chain and insert <var>filter-name</var></dd>
377 <dt><code>!</code></dt>
378 <dd>Empty the filter chain</dd>
380 <dt><code><var>filter-name</var></code></dt>
381 <dd>Equivalent to <code>+<var>filter-name</var></code></dd>
387 <name>FilterProtocol</name>
388 <description>Deal with correct HTTP protocol handling</description>
389 <syntax>FilterProtocol <var>filter-name</var> [<var>provider-name</var>]
390 <var>proto-flags</var></syntax>
391 <contextlist><context>server config</context><context>virtual host</context>
392 <context>directory</context><context>.htaccess</context></contextlist>
393 <override>Options</override>
396 <p>This directs <module>mod_filter</module> to deal with ensuring the
397 filter doesn't run when it shouldn't, and that the HTTP response
398 headers are correctly set taking into account the effects of the
401 <p>There are two forms of this directive. With three arguments, it
402 applies specifically to a <var>filter-name</var> and a
403 <var>provider-name</var> for that filter.
404 With two arguments it applies to a <var>filter-name</var> whenever the
405 filter runs <em>any</em> provider.</p>
407 <p><var>proto-flags</var> is one or more of</p>
410 <dt><code>change=yes</code></dt>
411 <dd>The filter changes the content, including possibly the content
414 <dt><code>change=1:1</code></dt>
415 <dd>The filter changes the content, but will not change the content
418 <dt><code>byteranges=no</code></dt>
419 <dd>The filter cannot work on byteranges and requires complete input</dd>
421 <dt><code>proxy=no</code></dt>
422 <dd>The filter should not run in a proxy context</dd>
424 <dt><code>proxy=transform</code></dt>
425 <dd>The filter transforms the response in a manner incompatible with
426 the HTTP <code>Cache-Control: no-transform</code> header.</dd>
428 <dt><code>cache=no</code></dt>
429 <dd>The filter renders the output uncacheable (eg by introducing randomised
430 content changes)</dd>
436 <name>FilterTrace</name>
437 <description>Get debug/diagnostic information from
438 <module>mod_filter</module></description>
439 <syntax>FilterTrace <var>filter-name</var> <var>level</var></syntax>
440 <contextlist><context>server config</context><context>virtual host</context>
441 <context>directory</context></contextlist>
444 <p>This directive generates debug information from
445 <module>mod_filter</module>.
446 It is designed to help test and debug providers (filter modules), although
447 it may also help with <module>mod_filter</module> itself.</p>
449 <p>The debug output depends on the <var>level</var> set:</p>
451 <dt><code>0</code> (default)</dt>
452 <dd>No debug information is generated.</dd>
454 <dt><code>1</code></dt>
455 <dd><module>mod_filter</module> will record buckets and brigades
456 passing through the filter to the error log, before the provider has
457 processed them. This is similar to the information generated by
458 <a href="http://apache.webthing.com/mod_diagnostics/">mod_diagnostics</a>.
461 <dt><code>2</code> (not yet implemented)</dt>
462 <dd>Will dump the full data passing through to a tempfile before the
463 provider. <strong>For single-user debug only</strong>; this will not
464 support concurrent hits.</dd>