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="upgrade"><title>Upgrading from HTTPD 2.2 Configuration</title>
132 <p>The <directive module="mod_filter">FilterProvider</directive>
133 directive has changed from HTTPD 2.2: the <var>match</var> and
134 <var>dispatch</var> arguments are replaced with a single but
135 more versatile <var>expression</var>. In general, you can convert
136 a match/dispatch pair to the two sides of an expression, using
138 <example>"dispatch = match"</example>
139 <p>The Request headers, Response headers and Environment variables
140 are now interpreted from syntax <var>$req{foo}</var>,
141 <var>$resp{foo}</var> and <var>$env{foo}</var> respectively.
142 The variables <var>$handler</var> and <var>$Content-Type</var>
143 are also supported.</p>
144 <p>Note that the match no longer supports integer comparisons
145 or substring matches. The latter can be replaced by regular
146 expression matches.</p>
149 <section id="examples"><title>Examples</title>
151 <dt>Server side Includes (SSI)</dt>
152 <dd>A simple case of using <module>mod_filter</module> in place of
153 <directive module="core">AddOutputFilterByType</directive>
155 FilterDeclare SSI<br/>
156 FilterProvider SSI INCLUDES "$resp{Content-Type} = /^text\/html/"<br/>
161 <dt>Server side Includes (SSI)</dt>
162 <dd>The same as the above but dispatching on handler (classic
163 SSI behaviour; .shtml files get processed).
165 FilterProvider SSI INCLUDES "Handler = server-parsed"<br/>
170 <dt>Emulating mod_gzip with mod_deflate</dt>
171 <dd>Insert INFLATE filter only if "gzip" is NOT in the
172 Accept-Encoding header. This filter runs with ftype CONTENT_SET.
174 FilterDeclare gzip CONTENT_SET<br/>
175 FilterProvider gzip inflate "$req{Accept-Encoding} != /gzip/"<br/>
180 <dt>Image Downsampling</dt>
181 <dd>Suppose we want to downsample all web images, and have filters
182 for GIF, JPEG and PNG.
184 FilterProvider unpack jpeg_unpack "$resp{Content-Type} = image/jpeg"<br/>
185 FilterProvider unpack gif_unpack "$resp{Content-Type} = image/gif"<br/>
186 FilterProvider unpack png_unpack "$resp{Content-Type} = image/png"<br/>
188 FilterProvider downsample downsample_filter "$resp{Content-Type} = /image\/(jpeg|gif|png)/"<br/>
189 FilterProtocol downsample "change=yes"<br/>
191 FilterProvider repack jpeg_pack "$resp{Content-Type} = image/jpeg"<br/>
192 FilterProvider repack gif_pack "$resp{Content-Type} = image/gif"<br/>
193 FilterProvider repack png_pack "$resp{Content-Type} = image/png"<br/>
194 <Location /image-filter><br/>
196 FilterChain unpack downsample repack<br/>
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>FilterDeclare</name>
248 <description>Declare a smart filter</description>
249 <syntax>FilterDeclare <var>filter-name</var> <var>[type]</var></syntax>
250 <contextlist><context>server config</context><context>virtual host</context>
251 <context>directory</context><context>.htaccess</context></contextlist>
252 <override>Options</override>
255 <p>This directive declares an output filter together with a
256 header or environment variable that will determine runtime
257 configuration. The first argument is a <var>filter-name</var>
258 for use in <directive module="mod_filter">FilterProvider</directive>,
259 <directive module="mod_filter">FilterChain</directive> and
260 <directive module="mod_filter">FilterProtocol</directive> directives.</p>
262 <p>The final (optional) argument
263 is the type of filter, and takes values of <code>ap_filter_type</code>
264 - namely <code>RESOURCE</code> (the default), <code>CONTENT_SET</code>,
265 <code>PROTOCOL</code>, <code>TRANSCODE</code>, <code>CONNECTION</code>
266 or <code>NETWORK</code>.</p>
271 <name>FilterProvider</name>
272 <description>Register a content filter</description>
273 <syntax>FilterProvider <var>filter-name</var> <var>provider-name</var>
274 <var>expression</var></syntax>
275 <contextlist><context>server config</context><context>virtual host</context>
276 <context>directory</context><context>.htaccess</context></contextlist>
277 <override>Options</override>
280 <p>This directive registers a <em>provider</em> for the smart filter.
281 The provider will be called if and only if the <var>expression</var>
282 declared evaluates to true when the harness is first called.</p>
285 <var>provider-name</var> must have been registered by loading
286 a module that registers the name with
287 <code>ap_register_output_filter</code>.
290 <p><var>expression</var> can be any of the following:</p>
292 <dt><code><var>string</var></code></dt>
293 <dd>true if <var>string</var> is not empty</dd>
295 <dt><code><var>string1</var> = <var>string2</var><br />
296 <var>string1</var> == <var>string2</var><br />
297 <var>string1</var> != <var>string2</var></code></dt>
299 <dd><p>Compare <var>string1</var> with <var>string2</var>. If
300 <var>string2</var> has the form <code>/<var>string2</var>/</code>
301 then it is treated as a regular expression. Regular expressions are
302 implemented by the <a href="http://www.pcre.org">PCRE</a> engine and
303 have the same syntax as those in <a href="http://www.perl.com">perl
304 5</a>. Note that <code>==</code> is just an alias for <code>=</code>
305 and behaves exactly the same way.</p>
308 <dt><code><var>string1</var> < <var>string2</var><br />
309 <var>string1</var> <= <var>string2</var><br />
310 <var>string1</var> > <var>string2</var><br />
311 <var>string1</var> >= <var>string2</var></code></dt>
313 <dd>Compare <var>string1</var> with <var>string2</var>. Note, that
314 strings are compared <em>literally</em> (using
315 <code>strcmp(3)</code>). Therefore the string "100" is less than
318 <dt><code>( <var>expression</var> )</code></dt>
319 <dd>true if <var>expression</var> is true</dd>
321 <dt><code>! <var>expression</var></code></dt>
322 <dd>true if <var>expression</var> is false</dd>
324 <dt><code><var>expression1</var> &&
325 <var>expression2</var></code></dt>
326 <dd>true if both <var>expression1</var> and
327 <var>expression2</var> are true</dd>
329 <dt><code><var>expression1</var> ||
330 <var>expression2</var></code></dt>
331 <dd>true if either <var>expression1</var> or
332 <var>expression2</var> is true</dd>
339 <name>FilterChain</name>
340 <description>Configure the filter chain</description>
341 <syntax>FilterChain [+=-@!]<var>filter-name</var> <var>...</var></syntax>
342 <contextlist><context>server config</context><context>virtual host</context>
343 <context>directory</context><context>.htaccess</context></contextlist>
344 <override>Options</override>
347 <p>This configures an actual filter chain, from declared filters.
348 <directive>FilterChain</directive> takes any number of arguments,
349 each optionally preceded with a single-character control that
350 determines what to do:</p>
353 <dt><code>+<var>filter-name</var></code></dt>
354 <dd>Add <var>filter-name</var> to the end of the filter chain</dd>
356 <dt><code>@<var>filter-name</var></code></dt>
357 <dd>Insert <var>filter-name</var> at the start of the filter chain</dd>
359 <dt><code>-<var>filter-name</var></code></dt>
360 <dd>Remove <var>filter-name</var> from the filter chain</dd>
362 <dt><code>=<var>filter-name</var></code></dt>
363 <dd>Empty the filter chain and insert <var>filter-name</var></dd>
365 <dt><code>!</code></dt>
366 <dd>Empty the filter chain</dd>
368 <dt><code><var>filter-name</var></code></dt>
369 <dd>Equivalent to <code>+<var>filter-name</var></code></dd>
375 <name>FilterProtocol</name>
376 <description>Deal with correct HTTP protocol handling</description>
377 <syntax>FilterProtocol <var>filter-name</var> [<var>provider-name</var>]
378 <var>proto-flags</var></syntax>
379 <contextlist><context>server config</context><context>virtual host</context>
380 <context>directory</context><context>.htaccess</context></contextlist>
381 <override>Options</override>
384 <p>This directs <module>mod_filter</module> to deal with ensuring the
385 filter doesn't run when it shouldn't, and that the HTTP response
386 headers are correctly set taking into account the effects of the
389 <p>There are two forms of this directive. With three arguments, it
390 applies specifically to a <var>filter-name</var> and a
391 <var>provider-name</var> for that filter.
392 With two arguments it applies to a <var>filter-name</var> whenever the
393 filter runs <em>any</em> provider.</p>
395 <p><var>proto-flags</var> is one or more of</p>
398 <dt><code>change=yes</code></dt>
399 <dd>The filter changes the content, including possibly the content
402 <dt><code>change=1:1</code></dt>
403 <dd>The filter changes the content, but will not change the content
406 <dt><code>byteranges=no</code></dt>
407 <dd>The filter cannot work on byteranges and requires complete input</dd>
409 <dt><code>proxy=no</code></dt>
410 <dd>The filter should not run in a proxy context</dd>
412 <dt><code>proxy=transform</code></dt>
413 <dd>The filter transforms the response in a manner incompatible with
414 the HTTP <code>Cache-Control: no-transform</code> header.</dd>
416 <dt><code>cache=no</code></dt>
417 <dd>The filter renders the output uncacheable (eg by introducing randomised
418 content changes)</dd>
424 <name>FilterTrace</name>
425 <description>Get debug/diagnostic information from
426 <module>mod_filter</module></description>
427 <syntax>FilterTrace <var>filter-name</var> <var>level</var></syntax>
428 <contextlist><context>server config</context><context>virtual host</context>
429 <context>directory</context></contextlist>
432 <p>This directive generates debug information from
433 <module>mod_filter</module>.
434 It is designed to help test and debug providers (filter modules), although
435 it may also help with <module>mod_filter</module> itself.</p>
437 <p>The debug output depends on the <var>level</var> set:</p>
439 <dt><code>0</code> (default)</dt>
440 <dd>No debug information is generated.</dd>
442 <dt><code>1</code></dt>
443 <dd><module>mod_filter</module> will record buckets and brigades
444 passing through the filter to the error log, before the provider has
445 processed them. This is similar to the information generated by
446 <a href="http://apache.webthing.com/mod_diagnostics/">mod_diagnostics</a>.
449 <dt><code>2</code> (not yet implemented)</dt>
450 <dd>Will dump the full data passing through to a tempfile before the
451 provider. <strong>For single-user debug only</strong>; this will not
452 support concurrent hits.</dd>