GCS Amplitude
GCS Amplitude

Apache Module mod_filter

![[This image shows the modfilter model]](../../images/modfilter_new.gif)

directive declares a filter, assigning it a name and filter type. Required only if the filter is not the default type APFTYPERESOURCE.FilterProvider

; if not, FilterProvider will implicitly declare it with the default type APFTYPERESOURCE. The provider must have been registered with apregisteroutput_filter

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 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.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

FilterProvider unpack jpegunpack "%{CONTENTTYPE} = 'image/jpeg'" FilterProvider unpack gifunpack "%{CONTENTTYPE} = 'image/gif'" FilterProvider unpack pngunpack "%{CONTENTTYPE} = 'image/png'" FilterProvider downsample downsamplefilter "%{CONTENTTYPE} = m|^image/(jpeg|gif|png)|" FilterProtocol downsample "change=yes" FilterProvider repack jpegpack "%{CONTENTTYPE} = 'image/jpeg'" FilterProvider repack gifpack "%{CONTENTTYPE} = 'image/gif'" FilterProvider repack pngpack "%{CONTENTTYPE} = 'image/png'" FilterChain unpack downsample repack

will record buckets and brigades passing through the filter to the error log, before the provider has processed them. This is similar to the information generated by 2

Apache HTTP Server Version 2.4

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).

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).mod_filter

In the traditional filtering model, filters are inserted unconditionally using 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.AddOutputFilter

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 mod_filterexpressions This generalises the limited flexibility offered by AddOutputFilterByType.


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 modelmod_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.mod_filter

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.

There are three stages to configuring a filter chain with . For details of the directives, see below.mod_filter

FilterDeclare
FilterProvider
FilterDeclare
ap_register_output_filter
FilterProvider
FilterChain

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.

The directive has changed from httpd 2.2: the FilterProvidermatch 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.

AddOutputFilterByType
FilterDeclare SSI FilterProvider SSI INCLUDES "%{CONTENT_TYPE} =~ m|^text/html|" FilterChain SSI
FilterProvider SSI INCLUDES "%{HANDLER} = 'server-parsed'" FilterChain SSI
FilterDeclare gzip CONTENT_SET FilterProvider gzip inflate "%{req:Accept-Encoding} !~ /gzip/" FilterChain gzip
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'" FilterChain unpack downsample repack

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:

Cache-Control: no-transform

aims to offer generic handling of these details of filter implementation, reducing the complexity required of content filter modules. This is work-in-progress; the mod_filter implements some of this functionality for back-compatibility with Apache 2.0 modules. For httpd 2.1 and later, the FilterProtocolap_register_output_filter_protocol and ap_filter_protocol API enables filter modules to declare their own behaviour.

At the same time, should not interfere with a filter that wants to handle all aspects of the protocol. By default (i.e. in the absence of any mod_filter directives), FilterProtocol will leave the headers untouched.mod_filter

At the time of writing, this feature is largely untested, as modules in common use are designed to work with 2.0. Modules using it should test it carefully.

AddOutputFilterByType filter[;filter...]
media-type [media-type] ...
mod_filter

This directive activates a particular output filter for a request depending on the response media-type.

The following example uses the DEFLATE filter, which is provided by . It will compress all output (either static or dynamic) which is labeled as mod_deflatetext/html or text/plain before it is sent to the client.

AddOutputFilterByType DEFLATE text/html text/plain

If you want the content to be processed by more than one filter, their names have to be separated by semicolons. It's also possible to use one AddOutputFilterByType directive for each of these filters.

The configuration below causes all script output labeled as text/html to be processed at first by the INCLUDES filter and then by the DEFLATE filter.

Options Includes AddOutputFilterByType INCLUDES;DEFLATE text/html
FilterChain [+=-@!]filter-name ...

This configures an actual filter chain, from declared filters. FilterChain takes any number of arguments, each optionally preceded with a single-character control that determines what to do:

+filter-name
@filter-name
-filter-name
=filter-name
!
filter-name
+filter-name
FilterDeclare filter-name [type]

This directive declares an output filter together with a header or environment variable that will determine runtime configuration. The first argument is a filter-name for use in , FilterProvider and FilterChain directives.FilterProtocol

The final (optional) argument is the type of filter, and takes values of ap_filter_type - namely RESOURCE (the default), CONTENT_SET, PROTOCOL, TRANSCODE, CONNECTION or NETWORK.

FilterProtocol filter-name [provider-name]
 proto-flags

This directs to deal with ensuring the filter doesn't run when it shouldn't, and that the HTTP response headers are correctly set taking into account the effects of the filter.mod_filter

There are two forms of this directive. With three arguments, it applies specifically to a filter-name and a provider-name for that filter. With two arguments it applies to a filter-name whenever the filter runs any provider.

Flags specified with this directive are merged with the flags that underlying providers may have registerd with . For example, a filter may internally specify the equivalent of mod_filterchange=yes, but a particular configuration of the module can override with change=no.

proto-flags is one or more of

change=yes|no
change=1:1
byteranges=no
proxy=no
proxy=transform
Cache-Control: no-transform
cache=no
FilterProvider filter-name provider-name
 expression

This directive registers a provider for the smart filter. The provider will be called if and only if the expression declared evaluates to true when the harness is first called.

provider-name must have been registered by loading a module that registers the name with ap_register_output_filter.

expression is an ap_expr.

mod_include
mod_filter
FilterTrace filter-name level

This directive generates debug information from . It is designed to help test and debug providers (filter modules), although it may also help with mod_filter itself.mod_filter

The debug output depends on the level set:

0
1
mod_filter
2

Apache Module mod_filter

Available Languages: en | fr

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.

Filter Declarations, Providers and Chains

Figure 1: The traditional filter model

Configuring the Chain

There are three stages to configuring a filter chain with mod_filter. For details of the directives, see below.

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:

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 filte

AddOutputFilterByType Directive

This directive activates a particular output filter for a request depending on the response media-type.

FilterChain Directive

This configures an actual filter chain, from declared filters. FilterChain takes any number of arguments, each optionally preceded with a single-character control that determines what to do:

FilterDeclare Directive

This directive declares an output filter together with a header or environment variable that will determine runtime configuration. The first argument is a filter-name for use in FilterProvider, FilterChain and FilterProtocol directives.

FilterProtocol Directive

This directs mod_filter to deal with ensuring the filter doesn't run when it shouldn't, and that the HTTP response headers are correctly set taking into account the effects of the filter.

FilterProvider Directive

This directive registers a provider for the smart filter. The provider will be called if and only if the expression declared evaluates to true when the harness is first called.

FilterTrace Directive

This directive generates debug information from mod_filter. It is designed to help test and debug providers (filter modules), although it may also help with mod_filter itself.