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 new smart filter, assigning it a name and optional filter
100 <dt>Register Providers</dt>
101 <dd>The <directive module="mod_filter">FilterProvider</directive>
102 directive registers a provider with a filter. The filter may have
103 been declared with <directive module="mod_filter"
104 >FilterDeclare</directive>; if not, FilterProvider will implicitly
105 declare it. The provider must have been
106 registered with <code>ap_register_output_filter</code> by some module.
107 The final argument to <directive module="mod_filter"
108 >FilterProvider</directive> is an expression: the provider will be
109 selected to run for a request if and only if the expression evaluates
110 to true. The expression may evaluate HTTP request or response
111 headers, environment variables, or the Handler used by this request.
112 Unlike earlier versions, mod_filter now supports complex expressions
113 involving multiple criteria with AND / OR logic (&& / ||)
114 and brackets. The details of the expression syntax are described in
115 the <a href="../expr.html">ap_expr documentation</a>.</dd>
117 <dt>Configure the Chain</dt>
118 <dd>The above directives build components of a smart filter chain,
119 but do not configure it to run. The <directive module="mod_filter"
120 >FilterChain</directive> directive builds a filter chain from smart
121 filters declared, offering the flexibility to insert filters at the
122 beginning or end of the chain, remove a filter, or clear the chain.</dd>
125 <section id="errordocs"><title>Filtering and Response Status</title>
126 <p>mod_filter normally only runs filters on responses with
127 HTTP status 200 (OK). If you want to filter documents with
128 other response statuses, you can set the <var>filter-errordocs</var>
129 environment variable, and it will work on all responses
130 regardless of status. To refine this further, you can use
131 expression conditions with <directive>FilterProvider</directive>.</p>
133 <section id="upgrade"><title>Upgrading from Apache HTTP Server 2.2 Configuration</title>
134 <p>The <directive module="mod_filter">FilterProvider</directive>
135 directive has changed from httpd 2.2: the <var>match</var> and
136 <var>dispatch</var> arguments are replaced with a single but
137 more versatile <var>expression</var>. In general, you can convert
138 a match/dispatch pair to the two sides of an expression, using
140 <example>"dispatch = 'match'"</example>
141 <p>The Request headers, Response headers and Environment variables
142 are now interpreted from syntax <var>%{req:foo}</var>,
143 <var>%{resp:foo}</var> and <var>%{env:foo}</var> respectively.
144 The variables <var>%{HANDLER}</var> and <var>%{CONTENT_TYPE}</var>
145 are also supported.</p>
146 <p>Note that the match no longer support substring matches. They can be
147 replaced by regular expression matches.</p>
150 <section id="examples"><title>Examples</title>
152 <dt>Server side Includes (SSI)</dt>
153 <dd>A simple case replacing <directive>AddOutputFilterByType</directive>.
154 This example creates a new smart filter named "SSI" that conditionally leverates
155 the "INCLUDES" filter from <module>mod_include</module> as a provider.
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>This example demonstrates the dynamic properties granted to traditional
174 filters when a smart filter is constructed around them. A new smart filter
175 named "gzip" is created that dynamically inserts <module>mod_deflate</module>'s
176 INFLATE filter only if "gzip" is NOT in the Accept-Encoding header.
177 The gzip smart filter runs with type CONTENT_SET.
178 <highlight language="config">
179 FilterDeclare gzip CONTENT_SET
180 FilterProvider gzip INFLATE "%{req:Accept-Encoding} !~ /gzip/"
185 <dt>Image Downsampling</dt>
186 <dd>This example demonstrates further abstractions that the smart filtering.
187 Suppose we want to downsample all web images, and have different
188 filter providers for manipulating GIF, JPEG and PNG
189 The configuration defines smart filters "unpack" and "repack" via
190 the harness that invokes the right underlying filter providers based on
191 the content type at runtime.
192 <highlight language="config">
193 FilterProvider unpack jpeg_unpack "%{CONTENT_TYPE} = 'image/jpeg'"
194 FilterProvider unpack gif_unpack "%{CONTENT_TYPE} = 'image/gif'"
195 FilterProvider unpack png_unpack "%{CONTENT_TYPE} = 'image/png'"
197 FilterProvider downsample downsample_filter "%{CONTENT_TYPE} = m|^image/(jpeg|gif|png)|"
198 FilterProtocol downsample "change=yes"
200 FilterProvider repack jpeg_pack "%{CONTENT_TYPE} = 'image/jpeg'"
201 FilterProvider repack gif_pack "%{CONTENT_TYPE} = 'image/gif'"
202 FilterProvider repack png_pack "%{CONTENT_TYPE} = 'image/png'"
203 <Location "/image-filter">
204 FilterChain unpack downsample repack
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>Had severe limitations before
263 being moved to <module>mod_filter</module> in version 2.3.7</compatibility>
266 <p>This directive activates a particular output <a
267 href="../filter.html">filter</a> for a request depending on the
268 response <glossary>media-type</glossary>.</p>
270 <p>The following example uses the <code>DEFLATE</code> filter, which
271 is provided by <module>mod_deflate</module>. It will compress all
272 output (either static or dynamic) which is labeled as
273 <code>text/html</code> or <code>text/plain</code> before it is sent
276 <highlight language="config">
277 AddOutputFilterByType DEFLATE text/html text/plain
280 <p>If you want the content to be processed by more than one filter, their
281 names have to be separated by semicolons. It's also possible to use one
282 <directive>AddOutputFilterByType</directive> directive for each of
285 <p>The configuration below causes all script output labeled as
286 <code>text/html</code> to be processed at first by the
287 <code>INCLUDES</code> filter and then by the <code>DEFLATE</code>
290 <highlight language="config">
291 <Location "/cgi-bin/">
293 AddOutputFilterByType INCLUDES;DEFLATE text/html
299 <seealso><directive module="mod_mime">AddOutputFilter</directive></seealso>
300 <seealso><directive module="core">SetOutputFilter</directive></seealso>
301 <seealso><a href="../filter.html">filters</a></seealso>
305 <name>FilterDeclare</name>
306 <description>Declare a smart filter</description>
307 <syntax>FilterDeclare <var>smart-filter-name</var> <var>[type]</var></syntax>
308 <contextlist><context>server config</context><context>virtual host</context>
309 <context>directory</context><context>.htaccess</context></contextlist>
310 <override>Options</override>
313 <p>This directive declares an output filter together with a
314 header or environment variable that will determine runtime
315 configuration. The first argument is a <var>smart-filter-name</var>
316 for use in <directive module="mod_filter">FilterProvider</directive>,
317 <directive module="mod_filter">FilterChain</directive> and
318 <directive module="mod_filter">FilterProtocol</directive> directives.</p>
320 <p>The final (optional) argument
321 is the type of filter, and takes values of <code>ap_filter_type</code>
322 - namely <code>RESOURCE</code> (the default), <code>CONTENT_SET</code>,
323 <code>PROTOCOL</code>, <code>TRANSCODE</code>, <code>CONNECTION</code>
324 or <code>NETWORK</code>.</p>
329 <name>FilterProvider</name>
330 <description>Register a content filter</description>
331 <syntax>FilterProvider <var>smart-filter-name</var> <var>provider-name</var>
332 <var>expression</var></syntax>
333 <contextlist><context>server config</context><context>virtual host</context>
334 <context>directory</context><context>.htaccess</context></contextlist>
335 <override>Options</override>
338 <p>This directive registers a <em>provider</em> for the smart filter.
339 The provider will be called if and only if the <var>expression</var>
340 declared evaluates to true when the harness is first called.</p>
343 <var>provider-name</var> must have been registered by loading
344 a module that registers the name with
345 <code>ap_register_output_filter</code>.
348 <p><var>expression</var> is an
349 <a href="../expr.html">ap_expr</a>.</p>
352 <seealso><a href="../expr.html">Expressions in Apache HTTP Server</a>,
353 for a complete reference and examples.</seealso>
354 <seealso><module>mod_include</module></seealso>
358 <name>FilterChain</name>
359 <description>Configure the filter chain</description>
360 <syntax>FilterChain [+=-@!]<var>smart-filter-name</var> <var>...</var></syntax>
361 <contextlist><context>server config</context><context>virtual host</context>
362 <context>directory</context><context>.htaccess</context></contextlist>
363 <override>Options</override>
366 <p>This configures an actual filter chain, from declared filters.
367 <directive>FilterChain</directive> takes any number of arguments,
368 each optionally preceded with a single-character control that
369 determines what to do:</p>
372 <dt><code>+<var>smart-filter-name</var></code></dt>
373 <dd>Add <var>filter-name</var> to the end of the filter chain</dd>
375 <dt><code>@<var>smart-filter-name</var></code></dt>
376 <dd>Insert <var>smart-filter-name</var> at the start of the filter chain</dd>
378 <dt><code>-<var>smart-filter-name</var></code></dt>
379 <dd>Remove <var>smart-filter-name</var> from the filter chain</dd>
381 <dt><code>=<var>smart-filter-name</var></code></dt>
382 <dd>Empty the filter chain and insert <var>smart-filter-name</var></dd>
384 <dt><code>!</code></dt>
385 <dd>Empty the filter chain</dd>
387 <dt><code><var>smart-filter-name</var></code></dt>
388 <dd>Equivalent to <code>+<var>smart-filter-name</var></code></dd>
394 <name>FilterProtocol</name>
395 <description>Deal with correct HTTP protocol handling</description>
396 <syntax>FilterProtocol <var>smart-filter-name</var> [<var>provider-name</var>]
397 <var>proto-flags</var></syntax>
398 <contextlist><context>server config</context><context>virtual host</context>
399 <context>directory</context><context>.htaccess</context></contextlist>
400 <override>Options</override>
403 <p>This directs <module>mod_filter</module> to deal with ensuring the
404 filter doesn't run when it shouldn't, and that the HTTP response
405 headers are correctly set taking into account the effects of the
408 <p>There are two forms of this directive. With three arguments, it
409 applies specifically to a <var>smart-filter-name</var> and a
410 <var>provider-name</var> for that filter.
411 With two arguments it applies to a <var>smart-filter-name</var> whenever the
412 filter runs <em>any</em> provider.</p>
414 <p>Flags specified with this directive are merged with the flags
415 that underlying providers may have registerd with
416 <module>mod_filter</module>. For example, a filter may internally specify
417 the equivalent of <code>change=yes</code>, but a particular
418 configuration of the module can override with <code>change=no</code>.
421 <p><var>proto-flags</var> is one or more of</p>
424 <dt><code>change=yes|no</code></dt>
425 <dd>Specifies whether the filter changes the content, including possibly
426 the content length. The "no" argument is supported in 2.4.7 and later.</dd>
428 <dt><code>change=1:1</code></dt>
429 <dd>The filter changes the content, but will not change the content
432 <dt><code>byteranges=no</code></dt>
433 <dd>The filter cannot work on byteranges and requires complete input</dd>
435 <dt><code>proxy=no</code></dt>
436 <dd>The filter should not run in a proxy context</dd>
438 <dt><code>proxy=transform</code></dt>
439 <dd>The filter transforms the response in a manner incompatible with
440 the HTTP <code>Cache-Control: no-transform</code> header.</dd>
442 <dt><code>cache=no</code></dt>
443 <dd>The filter renders the output uncacheable (eg by introducing randomised
444 content changes)</dd>
450 <name>FilterTrace</name>
451 <description>Get debug/diagnostic information from
452 <module>mod_filter</module></description>
453 <syntax>FilterTrace <var>smart-filter-name</var> <var>level</var></syntax>
454 <contextlist><context>server config</context><context>virtual host</context>
455 <context>directory</context></contextlist>
458 <p>This directive generates debug information from
459 <module>mod_filter</module>.
460 It is designed to help test and debug providers (filter modules), although
461 it may also help with <module>mod_filter</module> itself.</p>
463 <p>The debug output depends on the <var>level</var> set:</p>
465 <dt><code>0</code> (default)</dt>
466 <dd>No debug information is generated.</dd>
468 <dt><code>1</code></dt>
469 <dd><module>mod_filter</module> will record buckets and brigades
470 passing through the filter to the error log, before the provider has
471 processed them. This is similar to the information generated by
472 <a href="http://apache.webthing.com/mod_diagnostics/">mod_diagnostics</a>.
475 <dt><code>2</code> (not yet implemented)</dt>
476 <dd>Will dump the full data passing through to a tempfile before the
477 provider. <strong>For single-user debug only</strong>; this will not
478 support concurrent hits.</dd>