Filters
This document describes the use of filters in Apache.
Filtering in Apache 2
The Filter Chain is available in Apache 2.0 and higher,and enables applications to process incoming and outgoing datain a highly flexible and configurable manner, regardless ofwhere the data comes from. We can pre-process incoming data,and post-process outgoing data, at will. This is basicallyindependent of the traditional request processing phases.
Some examples of filtering in the standard Apache distribution are:
Apache also uses a number of filters internally to performfunctions like chunking and byte-range handling.
A wider range of applications are implemented by third-party filtermodules available from modules.apache.org andelsewhere. A few of these are:
- HTML and XML processing and rewriting
- XSLT transforms and XIncludes
- XML Namespace support
- File Upload handling and decoding of HTML Forms
- Image processing
- Protection of vulnerable applications such as PHP scripts
- Text search-and-replace editing
Smart Filtering
mod_filter
, included in Apache 2.1 and later,enables the filter chain to be configured dynamically at run time.So for example you can set up a proxy to rewriteHTML with an HTML filter and JPEG images with a completelyseparate filter, despite the proxy having no prior informationabout what the origin server will send. This works by using afilter harness, that dispatches to different providers accordingto the actual contents at runtime. Any filter may be eitherinserted directly in the chain and run unconditionally, orused as a provider and inserted dynamically. For example,
- an HTML processing filter will only run if the content istext/html or application/xhtml+xml
- A compression filter will only run if the input is acompressible type and not already compressed
- A charset conversion filter will be inserted if a textdocument is not already in the desired charset
Using Filters
There are two ways to use filtering: Simple and Dynamic.In general, you should use one or the other; mixing them canhave unexpected consequences (although simple Input filteringcan be mixed freely with either simple or dynamic Output filtering).
The Simple Way is the only way to configure input filters, and issufficient for output filters where you need a static filter chain.Relevant directives are SetInputFilter
, SetOutputFilter
, AddInputFilter
, AddOutputFilter
, RemoveInputFilter
, and RemoveOutputFilter
.
The Dynamic Way enables both static and flexible, dynamic configurationof output filters, as discussed in the mod_filter
page.Relevant directives are FilterChain
, FilterDeclare
, and FilterProvider
.
One further directive AddOutputFilterByType
is still supported,but may be problematic and is now deprecated. Use dynamicconfiguration instead.