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