]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_filter.xml
Add upgrading section to mod_filter docs.
[apache] / docs / manual / mod / mod_filter.xml
1 <?xml version="1.0"?>
2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
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
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
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.
21 -->
22
23 <modulesynopsis metafile="mod_filter.xml.meta">
24
25 <name>mod_filter</name>
26 <description>Context-sensitive smart filter configuration module</description>
27 <status>Base</status>
28 <sourcefile>mod_filter.c</sourcefile>
29 <identifier>filter_module</identifier>
30 <compatibility>Version 2.1 and later</compatibility>
31
32 <summary>
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>
37
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>
44 </summary>
45
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>
52
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>
62 </section>
63
64 <section id="terms"><title>Filter Declarations, Providers and Chains</title>
65     <p class="figure">
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>
69
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>
75
76     <p class="figure">
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>
80
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>
89
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>
94 </section>
95
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>
99
100     <dl>
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>
105
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
112     must have been
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 (&amp;&amp; / ||)
121     and brackets.</dd>
122
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>
129 </dl>
130 </section>
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
137     something like:</p>
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>
147 </section>
148
149 <section id="examples"><title>Examples</title>
150     <dl>
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>
154     <example>
155       FilterDeclare SSI<br/>
156       FilterProvider SSI INCLUDES "$resp{Content-Type} = /^text\/html/"<br/>
157       FilterChain SSI
158     </example>
159     </dd>
160
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).
164     <example>
165       FilterProvider SSI INCLUDES "Handler = server-parsed"<br/>
166       FilterChain SSI
167     </example>
168     </dd>
169
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.
173     <example>
174       FilterDeclare gzip CONTENT_SET<br/>
175       FilterProvider gzip inflate "$req{Accept-Encoding} != /gzip/"<br/>
176       FilterChain gzip
177     </example>
178     </dd>
179
180     <dt>Image Downsampling</dt>
181     <dd>Suppose we want to downsample all web images, and have filters
182     for GIF, JPEG and PNG.
183     <example>
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/>
187       <br />
188       FilterProvider downsample downsample_filter "$resp{Content-Type} = /image\/(jpeg|gif|png)/"<br/>
189       FilterProtocol downsample "change=yes"<br/>
190       <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       &lt;Location /image-filter&gt;<br/>
195       <indent>
196         FilterChain unpack downsample repack<br/>
197       </indent>
198       &lt;/Location&gt;
199     </example>
200     </dd>
201     </dl>
202 </section>
203
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>
210
211     <ul>
212     <li>Many filters will change the content, invalidating existing content
213     tags, checksums, hashes, and lengths.</li>
214
215     <li>Filters that require an entire, unbroken response in input need to
216     ensure they don't get byteranges from a backend.</li>
217
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
220     backend.</li>
221
222     <li>Filters may make responses uncacheable.</li>
223     </ul>
224
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>
234
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>
240
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>
244 </section>
245
246 <directivesynopsis>
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>
253
254 <usage>
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>
261
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>
267 </usage>
268 </directivesynopsis>
269
270 <directivesynopsis>
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>
278
279 <usage>
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>
283
284     <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>.
288     </p>
289
290     <p><var>expression</var> can be any of the following:</p>
291     <dl>
292       <dt><code><var>string</var></code></dt>
293       <dd>true if <var>string</var> is not empty</dd>
294
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>
298
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>
306       </dd>
307
308       <dt><code><var>string1</var> &lt; <var>string2</var><br />
309        <var>string1</var> &lt;= <var>string2</var><br />
310        <var>string1</var> &gt; <var>string2</var><br />
311        <var>string1</var> &gt;= <var>string2</var></code></dt>
312
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
316       "20".</dd>
317
318       <dt><code>( <var>expression</var> )</code></dt>
319       <dd>true if <var>expression</var> is true</dd>
320
321       <dt><code>! <var>expression</var></code></dt>
322       <dd>true if <var>expression</var> is false</dd>
323
324       <dt><code><var>expression1</var> &amp;&amp;
325         <var>expression2</var></code></dt>
326       <dd>true if both <var>expression1</var> and
327       <var>expression2</var> are true</dd>
328
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>
333     </dl>
334
335 </usage>
336 </directivesynopsis>
337
338 <directivesynopsis>
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>
345
346 <usage>
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>
351
352     <dl>
353     <dt><code>+<var>filter-name</var></code></dt>
354     <dd>Add <var>filter-name</var> to the end of the filter chain</dd>
355
356     <dt><code>@<var>filter-name</var></code></dt>
357     <dd>Insert <var>filter-name</var> at the start of the filter chain</dd>
358
359     <dt><code>-<var>filter-name</var></code></dt>
360     <dd>Remove <var>filter-name</var> from the filter chain</dd>
361
362     <dt><code>=<var>filter-name</var></code></dt>
363     <dd>Empty the filter chain and insert <var>filter-name</var></dd>
364
365     <dt><code>!</code></dt>
366     <dd>Empty the filter chain</dd>
367
368     <dt><code><var>filter-name</var></code></dt>
369     <dd>Equivalent to <code>+<var>filter-name</var></code></dd>
370     </dl>
371 </usage>
372 </directivesynopsis>
373
374 <directivesynopsis>
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>
382
383 <usage>
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
387     filter.</p>
388
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>
394
395     <p><var>proto-flags</var> is one or more of</p>
396
397     <dl>
398     <dt><code>change=yes</code></dt>
399     <dd>The filter changes the content, including possibly the content
400     length</dd>
401
402     <dt><code>change=1:1</code></dt>
403     <dd>The filter changes the content, but will not change the content
404     length</dd>
405
406     <dt><code>byteranges=no</code></dt>
407     <dd>The filter cannot work on byteranges and requires complete input</dd>
408
409     <dt><code>proxy=no</code></dt>
410     <dd>The filter should not run in a proxy context</dd>
411
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>
415
416     <dt><code>cache=no</code></dt>
417     <dd>The filter renders the output uncacheable (eg by introducing randomised
418     content changes)</dd>
419     </dl>
420 </usage>
421 </directivesynopsis>
422
423 <directivesynopsis>
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>
430
431 <usage>
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>
436
437     <p>The debug output depends on the <var>level</var> set:</p>
438     <dl>
439     <dt><code>0</code> (default)</dt>
440     <dd>No debug information is generated.</dd>
441
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>.
447     </dd>
448
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>
453     </dl>
454 </usage>
455 </directivesynopsis>
456
457 </modulesynopsis>
458