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 module="core">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 (&& / ||)
123 <dt>Configure the Chain</dt>
124 <dd>The above directives build components of a smart filter chain,
125 but do not configure it to run. The <directive module="mod_filter"
126 >FilterChain</directive> directive builds a filter chain from smart
127 filters declared, offering the flexibility to insert filters at the
128 beginning or end of the chain, remove a filter, or clear the chain.</dd>
131 <section id="errordocs"><title>Filtering and Response Status</title>
132 <p>mod_filter normally only runs filters on responses with
133 HTTP status 200 (OK). If you want to filter documents with
134 other response statuses, you can set the <var>filter-errordocs</var>
135 environment variable, and it will work on all responses
136 regardless of status. To refine this further, you can use
137 expression conditions with <directive>FilterProvider</directive>.</p>
139 <section id="upgrade"><title>Upgrading from Apache HTTP Server 2.2 Configuration</title>
140 <p>The <directive module="mod_filter">FilterProvider</directive>
141 directive has changed from httpd 2.2: the <var>match</var> and
142 <var>dispatch</var> arguments are replaced with a single but
143 more versatile <var>expression</var>. In general, you can convert
144 a match/dispatch pair to the two sides of an expression, using
146 <example>"dispatch = match"</example>
147 <p>The Request headers, Response headers and Environment variables
148 are now interpreted from syntax <var>$req{foo}</var>,
149 <var>$resp{foo}</var> and <var>$env{foo}</var> respectively.
150 The variables <var>$handler</var> and <var>$Content-Type</var>
151 are also supported.</p>
152 <p>Note that the match no longer supports integer comparisons
153 or substring matches. The latter can be replaced by regular
154 expression matches.</p>
157 <section id="examples"><title>Examples</title>
159 <dt>Server side Includes (SSI)</dt>
160 <dd>A simple case of using <module>mod_filter</module> in place of
161 <directive module="core">AddOutputFilterByType</directive>
163 FilterDeclare SSI<br/>
164 FilterProvider SSI INCLUDES "$resp{Content-Type} = /^text\/html/"<br/>
169 <dt>Server side Includes (SSI)</dt>
170 <dd>The same as the above but dispatching on handler (classic
171 SSI behaviour; .shtml files get processed).
173 FilterProvider SSI INCLUDES "Handler = server-parsed"<br/>
178 <dt>Emulating mod_gzip with mod_deflate</dt>
179 <dd>Insert INFLATE filter only if "gzip" is NOT in the
180 Accept-Encoding header. This filter runs with ftype CONTENT_SET.
182 FilterDeclare gzip CONTENT_SET<br/>
183 FilterProvider gzip inflate "$req{Accept-Encoding} != /gzip/"<br/>
188 <dt>Image Downsampling</dt>
189 <dd>Suppose we want to downsample all web images, and have filters
190 for GIF, JPEG and PNG.
192 FilterProvider unpack jpeg_unpack "$resp{Content-Type} = image/jpeg"<br/>
193 FilterProvider unpack gif_unpack "$resp{Content-Type} = image/gif"<br/>
194 FilterProvider unpack png_unpack "$resp{Content-Type} = image/png"<br/>
196 FilterProvider downsample downsample_filter "$resp{Content-Type} = /image\/(jpeg|gif|png)/"<br/>
197 FilterProtocol downsample "change=yes"<br/>
199 FilterProvider repack jpeg_pack "$resp{Content-Type} = image/jpeg"<br/>
200 FilterProvider repack gif_pack "$resp{Content-Type} = image/gif"<br/>
201 FilterProvider repack png_pack "$resp{Content-Type} = image/png"<br/>
202 <Location /image-filter><br/>
204 FilterChain unpack downsample repack<br/>
212 <section id="protocol"><title>Protocol Handling</title>
213 <p>Historically, each filter is responsible for ensuring that whatever
214 changes it makes are correctly represented in the HTTP response headers,
215 and that it does not run when it would make an illegal change. This
216 imposes a burden on filter authors to re-implement some common
217 functionality in every filter:</p>
220 <li>Many filters will change the content, invalidating existing content
221 tags, checksums, hashes, and lengths.</li>
223 <li>Filters that require an entire, unbroken response in input need to
224 ensure they don't get byteranges from a backend.</li>
226 <li>Filters that transform output in a filter need to ensure they don't
227 violate a <code>Cache-Control: no-transform</code> header from the
230 <li>Filters may make responses uncacheable.</li>
233 <p><module>mod_filter</module> aims to offer generic handling of these
234 details of filter implementation, reducing the complexity required of
235 content filter modules. This is work-in-progress; the
236 <directive module="mod_filter">FilterProtocol</directive> implements
237 some of this functionality for back-compatibility with Apache 2.0
238 modules. For httpd 2.1 and later, the
239 <code>ap_register_output_filter_protocol</code> and
240 <code>ap_filter_protocol</code> API enables filter modules to
241 declare their own behaviour.</p>
243 <p>At the same time, <module>mod_filter</module> should not interfere
244 with a filter that wants to handle all aspects of the protocol. By
245 default (i.e. in the absence of any <directive module="mod_filter"
246 >FilterProtocol</directive> directives), <module>mod_filter</module>
247 will leave the headers untouched.</p>
249 <p>At the time of writing, this feature is largely untested,
250 as modules in common use are designed to work with 2.0.
251 Modules using it should test it carefully.</p>
255 <name>FilterDeclare</name>
256 <description>Declare a smart filter</description>
257 <syntax>FilterDeclare <var>filter-name</var> <var>[type]</var></syntax>
258 <contextlist><context>server config</context><context>virtual host</context>
259 <context>directory</context><context>.htaccess</context></contextlist>
260 <override>Options</override>
263 <p>This directive declares an output filter together with a
264 header or environment variable that will determine runtime
265 configuration. The first argument is a <var>filter-name</var>
266 for use in <directive module="mod_filter">FilterProvider</directive>,
267 <directive module="mod_filter">FilterChain</directive> and
268 <directive module="mod_filter">FilterProtocol</directive> directives.</p>
270 <p>The final (optional) argument
271 is the type of filter, and takes values of <code>ap_filter_type</code>
272 - namely <code>RESOURCE</code> (the default), <code>CONTENT_SET</code>,
273 <code>PROTOCOL</code>, <code>TRANSCODE</code>, <code>CONNECTION</code>
274 or <code>NETWORK</code>.</p>
279 <name>FilterProvider</name>
280 <description>Register a content filter</description>
281 <syntax>FilterProvider <var>filter-name</var> <var>provider-name</var>
282 <var>expression</var></syntax>
283 <contextlist><context>server config</context><context>virtual host</context>
284 <context>directory</context><context>.htaccess</context></contextlist>
285 <override>Options</override>
288 <p>This directive registers a <em>provider</em> for the smart filter.
289 The provider will be called if and only if the <var>expression</var>
290 declared evaluates to true when the harness is first called.</p>
293 <var>provider-name</var> must have been registered by loading
294 a module that registers the name with
295 <code>ap_register_output_filter</code>.
298 <p><var>expression</var> can be any of the following:</p>
300 <dt><code><var>string</var></code></dt>
301 <dd>true if <var>string</var> is not empty</dd>
303 <dt><code><var>string1</var> = <var>string2</var><br />
304 <var>string1</var> == <var>string2</var><br />
305 <var>string1</var> != <var>string2</var></code></dt>
307 <dd><p>Compare <var>string1</var> with <var>string2</var>. If
308 <var>string2</var> has the form <code>/<var>string2</var>/</code>
309 then it is treated as a regular expression. Regular expressions are
310 implemented by the <a href="http://www.pcre.org">PCRE</a> engine and
311 have the same syntax as those in <a href="http://www.perl.com">perl
312 5</a>. Note that <code>==</code> is just an alias for <code>=</code>
313 and behaves exactly the same way.</p>
316 <dt><code><var>string1</var> < <var>string2</var><br />
317 <var>string1</var> <= <var>string2</var><br />
318 <var>string1</var> > <var>string2</var><br />
319 <var>string1</var> >= <var>string2</var></code></dt>
321 <dd>Compare <var>string1</var> with <var>string2</var>. Note, that
322 strings are compared <em>literally</em> (using
323 <code>strcmp(3)</code>). Therefore the string "100" is less than
326 <dt><code>( <var>expression</var> )</code></dt>
327 <dd>true if <var>expression</var> is true</dd>
329 <dt><code>! <var>expression</var></code></dt>
330 <dd>true if <var>expression</var> is false</dd>
332 <dt><code><var>expression1</var> &&
333 <var>expression2</var></code></dt>
334 <dd>true if both <var>expression1</var> and
335 <var>expression2</var> are true</dd>
337 <dt><code><var>expression1</var> ||
338 <var>expression2</var></code></dt>
339 <dd>true if either <var>expression1</var> or
340 <var>expression2</var> is true</dd>
347 <name>FilterChain</name>
348 <description>Configure the filter chain</description>
349 <syntax>FilterChain [+=-@!]<var>filter-name</var> <var>...</var></syntax>
350 <contextlist><context>server config</context><context>virtual host</context>
351 <context>directory</context><context>.htaccess</context></contextlist>
352 <override>Options</override>
355 <p>This configures an actual filter chain, from declared filters.
356 <directive>FilterChain</directive> takes any number of arguments,
357 each optionally preceded with a single-character control that
358 determines what to do:</p>
361 <dt><code>+<var>filter-name</var></code></dt>
362 <dd>Add <var>filter-name</var> to the end of the filter chain</dd>
364 <dt><code>@<var>filter-name</var></code></dt>
365 <dd>Insert <var>filter-name</var> at the start of the filter chain</dd>
367 <dt><code>-<var>filter-name</var></code></dt>
368 <dd>Remove <var>filter-name</var> from the filter chain</dd>
370 <dt><code>=<var>filter-name</var></code></dt>
371 <dd>Empty the filter chain and insert <var>filter-name</var></dd>
373 <dt><code>!</code></dt>
374 <dd>Empty the filter chain</dd>
376 <dt><code><var>filter-name</var></code></dt>
377 <dd>Equivalent to <code>+<var>filter-name</var></code></dd>
383 <name>FilterProtocol</name>
384 <description>Deal with correct HTTP protocol handling</description>
385 <syntax>FilterProtocol <var>filter-name</var> [<var>provider-name</var>]
386 <var>proto-flags</var></syntax>
387 <contextlist><context>server config</context><context>virtual host</context>
388 <context>directory</context><context>.htaccess</context></contextlist>
389 <override>Options</override>
392 <p>This directs <module>mod_filter</module> to deal with ensuring the
393 filter doesn't run when it shouldn't, and that the HTTP response
394 headers are correctly set taking into account the effects of the
397 <p>There are two forms of this directive. With three arguments, it
398 applies specifically to a <var>filter-name</var> and a
399 <var>provider-name</var> for that filter.
400 With two arguments it applies to a <var>filter-name</var> whenever the
401 filter runs <em>any</em> provider.</p>
403 <p><var>proto-flags</var> is one or more of</p>
406 <dt><code>change=yes</code></dt>
407 <dd>The filter changes the content, including possibly the content
410 <dt><code>change=1:1</code></dt>
411 <dd>The filter changes the content, but will not change the content
414 <dt><code>byteranges=no</code></dt>
415 <dd>The filter cannot work on byteranges and requires complete input</dd>
417 <dt><code>proxy=no</code></dt>
418 <dd>The filter should not run in a proxy context</dd>
420 <dt><code>proxy=transform</code></dt>
421 <dd>The filter transforms the response in a manner incompatible with
422 the HTTP <code>Cache-Control: no-transform</code> header.</dd>
424 <dt><code>cache=no</code></dt>
425 <dd>The filter renders the output uncacheable (eg by introducing randomised
426 content changes)</dd>
432 <name>FilterTrace</name>
433 <description>Get debug/diagnostic information from
434 <module>mod_filter</module></description>
435 <syntax>FilterTrace <var>filter-name</var> <var>level</var></syntax>
436 <contextlist><context>server config</context><context>virtual host</context>
437 <context>directory</context></contextlist>
440 <p>This directive generates debug information from
441 <module>mod_filter</module>.
442 It is designed to help test and debug providers (filter modules), although
443 it may also help with <module>mod_filter</module> itself.</p>
445 <p>The debug output depends on the <var>level</var> set:</p>
447 <dt><code>0</code> (default)</dt>
448 <dd>No debug information is generated.</dd>
450 <dt><code>1</code></dt>
451 <dd><module>mod_filter</module> will record buckets and brigades
452 passing through the filter to the error log, before the provider has
453 processed them. This is similar to the information generated by
454 <a href="http://apache.webthing.com/mod_diagnostics/">mod_diagnostics</a>.
457 <dt><code>2</code> (not yet implemented)</dt>
458 <dd>Will dump the full data passing through to a tempfile before the
459 provider. <strong>For single-user debug only</strong>; this will not
460 support concurrent hits.</dd>