mod_filter - Apache HTTP Server Version 2.5
Modules | Directives | FAQ | Glossary | Sitemap | Report a bug Apache HTTP Server Version 2.5 Apache > HTTP Server > Documentation > Version 2.5 > Modules Apache Module mod_filter Available Languages: en | fr Description: Context-sensitive smart filter configuration module Status: Base Module Identifier: filter_module Source File: mod_filter.c Summary This module enables smart, context-sensitive configuration of output content filters.
For example, apache can be configured to process different content-types through different filters, even when the content-type is not known in advance (e.g. in a proxy). mod_filter works by introducing indirection into the filter chain. Instead of inserting filters in the chain, we insert a filter harness which in turn dispatches conditionally to a filter provider. Any content filter may be used as a provider to mod_filter ; no change to existing filter modules is required (although it may be possible to simplify them).
Topics Smart Filtering Filter Declarations, Providers and Chains Configuring the Chain Filtering and Response Status Upgrading from Apache HTTP Server 2.2 Configuration Examples Protocol Handling Directives AddOutputFilterByType FilterChain FilterDeclare FilterProtocol FilterProvider FilterTrace Bugfix checklist httpd changelog Known issues Report a bug Smart Filtering ¶ In the traditional filtering model, filters are inserted unconditionally using AddOutputFilter and family.
Each filter then needs to determine whether to run, and there is little flexibility available for server admins to allow the chain to be configured dynamically. mod_filter by contrast gives server administrators a great deal of flexibility in configuring the filter chain. In fact, filters can be inserted based on complex boolean expressions This generalises the limited flexibility offered by AddOutputFilterByType . Filter Declarations, Providers and Chains ¶ Figure 1: The traditional filter model In the traditional model, output filters are a simple chain from the content generator (handler) to the client.
This works well provided the filter chain can be correctly configured, but presents problems when the filters need to be configured dynamically based on the outcome of the handler. Figure 2: The mod_filter model mod_filter works by introducing indirection into the filter chain. Instead of inserting filters in the chain, we insert a filter harness which in turn dispatches conditionally to a filter provider. Any content filter may be used as a provider to mod_filter ; no change to existing filter modules is required (although it may be possible to simplify them).
There can be multiple providers for one filter, but no more than one provider will run for any single request. A filter chain comprises any number of instances of the filter harness, each of which may have any number of providers. A special case is that of a single provider with unconditional dispatch: this is equivalent to inserting the provider filter directly into the chain. Configuring the Chain ¶ There are three stages to configuring a filter chain with mod_filter .
For details of the directives, see below. Declare Filters The FilterDeclare directive declares a new smart filter, assigning it a name and optional filter type. Register Providers The FilterProvider directive registers a provider with a filter. The filter may have been declared with FilterDeclare ; if not, FilterProvider will implicitly declare it. The provider must have been registered with ap_register_output_filter by some module.
The final argument to FilterProvider is an expression: the provider will be selected to run for a request if and only if the expression evaluates to true. The expression may evaluate HTTP request or response headers, environment variables, or the Handler used by this request. Unlike earlier versions, mod_filter now supports complex expressions involving multiple criteria with AND / OR logic (&& / ||) and brackets. The details of the expression syntax are described in the ap_expr documentation .
Configure the Chain The above directives build components of a smart filter chain, but do not configure it to run. The FilterChain directive builds a filter chain from smart filters declared, offering the flexibility to insert filters at the beginning or end of the chain, remove a filter, or clear the chain. Filtering and Response Status ¶ mod_filter normally only runs filters on responses with HTTP status 200 (OK). If you want to filter documents with other response statuses, you can set the filter-errordocs environment variable, and it will work on all responses regardless of status.
To refine this further, you can use expression conditions with FilterProvider . Upgrading from Apache HTTP Server 2.2 Configuration ¶ The FilterProvider directive has changed from httpd 2.2: the match and dispatch arguments are replaced with a single but more versatile expression . In general, you can convert a match/dispatch pair to the two sides of an expression, using something like: "dispatch = 'match'" The Request headers, Response headers and Environment variables are now interpreted from syntax %{req:foo} , %{resp:foo} and %{env:foo} respectively.
The variables %{HANDLER} and %{CONTENT_TYPE} are also supported. Note that the match no longer support substring matches. They can be replaced by regular expression matches. Examples ¶ Server side Includes (SSI) A simple case replacing AddOutputFilterByType . This example creates a new smart filter named "SSI" that conditionally leverages the "INCLUDES" filter from mod_include as a provider. FilterDeclare SSI FilterProvider SSI INCLUDES "%{CONTENT_TYPE} =~ m|^text/html|" FilterChain SSI Server side Includes (SSI) The same as the above but dispatching on handler (classic SSI behavior; .shtml files get processed).
FilterProvider SSI INCLUDES "%{HANDLER} = 'server-parsed'" FilterChain SSI Emulating mod_gzip with mod_deflate This example demonstrates the dynamic properties granted to traditional filters when a smart filter is constructed around them. A new smart filter named "gzip" is created that dynamically inserts mod_deflate 's INFLATE filter only if "gzip" is NOT in the Accept-Encoding header. The gzip smart filter runs with type CONTENT_SET.
FilterDeclare gzip CONTENT_SET FilterProvider gzip INFLATE "%{req:Accept-Encoding} !~ /gzip/" FilterChain gzip Image Downsampling This example demonstrates further abstractions that the smart filtering. Suppose we want to downsample all web images, and have different filter providers for manipulating GIF, JPEG and PNG The configuration defines smart filters "unpack" and "repack" via the harness that invokes the right underlying filter providers based on the content type at runtime.
FilterProvider unpack jpeg_unpack "%{CONTENT_TYPE} = 'image/jpeg'" FilterProvider unpack gif_unpack "%{CONTENT_TYPE} = 'image/gif'" FilterProvider unpack png_unpack "%{CONTENT_TYPE} = 'image/png'" FilterProvider downsample downsample_filter "%{CONTENT_TYPE} = m|^image/(jpeg|gif|png)|" FilterProtocol downsample "change=yes" FilterProvider repack jpeg_pack "%{CONTENT_TYPE} = 'image/jpeg'" FilterProvider repack gif_pack "%{CONTENT_TYPE} = 'image/gif'" FilterProvider repack png_pack "%{CONTENT_TYPE} = 'image/png'" <Location "/image-filter"> FilterChain unpack downsample repack </Location> Protocol Handling ¶ Historically, each filter is responsible for ensuring that whatever changes it makes are correctly represented in the HTTP response headers, and that it does not run when it would make an illegal change.
This imposes a burden on filter authors to re-implement some common functionality in every filter: Many filters will change the content, invalidating existing content tags, checksums, hashes, and lengths. Filters that require an entire, unbroken response in input need to ensure they don't get byteranges from a backend. Filters that transform output in a filter need to ensure they don't violate a Cache-Control: no-transform header
The rest of this page is on the live site.