©
Ce document utiliseManuel du site Web PHP chinoisLibérer
| 说明 | 根据上下文实际情况对输出过滤器进行动态配置 |
|---|---|
| 状态 | 基本(B) |
| 模块名 | filter_module |
| 源文件 | mod_filter.c |
| 兼容性 | Version 2.1 及以后的版本中可用 |
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_filterworks 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 tomod_filter; no change to existing filter modules is required (although it may be possible to simplify them).
In the traditional filtering model, filters are inserted unconditionally usingAddOutputFilterand 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_filterby contrast gives server administrators a great deal of flexibility in configuring the filter chain. In fact, filters can be inserted based on any Request Header, Response Header or Environment Variable. This generalises the limited flexibility offered byAddOutputFilterByType, and fixes it to work correctly with dynamic content, regardless of the content generator. The ability to dispatch based on Environment Variables offers the full flexibility of configuration withmod_rewriteto anyone who needs it.
![[This image displays the traditional filter model]](http://m.sbmmt.com/fr/manual/images/mod_filter_old.gif)
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.
![[This image shows the mod_filter model]](http://m.sbmmt.com/fr/manual/images/mod_filter_new.gif)
Figure 2:Themod_filtermodel
mod_filterworks 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 tomod_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.
There are three stages to configuring a filter chain withmod_filter. For details of the directives, see below.
FilterDeclaredirective declares a filter, assigning it a name and filter type. Required only if the filter is not the default type AP_FTYPE_RESOURCE.
FilterProviderdirective registers a provider with a filter. The filter may have been declared with
FilterDeclare; if not, FilterProvider will implicitly declare it with the default type AP_FTYPE_RESOURCE. The provider must have been registered with
ap_register_output_filterby some module. The remaining arguments to
FilterProviderare a dispatch criterion and a match string. The former may be an HTTP request or response header, an environment variable, or the Handler used by this request. The latter is matched to it for each request, to determine whether this provider will be used to implement the filter for this request.
FilterChaindirective 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_filterin place of
AddOutputFilterByType
FilterDeclare SSI
FilterProvider SSI INCLUDES resp=Content-Type $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 $image
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-transformheader from the backend.mod_filteraims to offer generic handling of these details of filter implementation, reducing the complexity required of content filter modules. This is work-in-progress; theFilterProtocolimplements some of this functionality for back-compatibility with Apache 2.0 modules. For httpd 2.1 and later, theap_register_output_filter_protocol和ap_filter_protocolAPI enables filter modules to declare their own behaviour.
At the same time,mod_filtershould not interfere with a filter that wants to handle all aspects of the protocol. By default (i.e. in the absence of anyFilterProtocoldirectives),mod_filterwill leave the headers untouched.
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.
| 说明 | Configure the filter chain |
|---|---|
| 语法 | FilterChain [+=-@!]filter-name... |
| 作用域 | server config, virtual host, directory, .htaccess |
| 覆盖项 | Options |
| 状态 | 基本(B) |
| 模块 | mod_filter |
This configures an actual filter chain, from declared filters.FilterChaintakes 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
| 说明 | Declare a smart filter |
|---|---|
| 语法 | FilterDeclarefilter-name[type] |
| 作用域 | server config, virtual host, directory, .htaccess |
| 覆盖项 | Options |
| 状态 | 基本(B) |
| 模块 | mod_filter |
This directive declares an output filter together with a header or environment variable that will determine runtime configuration. The first argument is afilter-namefor use inFilterProvider,FilterChain和FilterProtocoldirectives.
The final (optional) argument is the type of filter, and takes values ofap_filter_type- namelyRESOURCE(the default),CONTENT_SET,PROTOCOL,TRANSCODE,CONNECTION或NETWORK.
| 说明 | Deal with correct HTTP protocol handling |
|---|---|
| 语法 | FilterProtocolfilter-name[provider-name]proto-flags |
| 作用域 | server config, virtual host, directory, .htaccess |
| 覆盖项 | Options |
| 状态 | 基本(B) |
| 模块 | mod_filter |
This directsmod_filterto 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.
There are two forms of this directive. With three arguments, it applies specifically to afilter-nameand aprovider-namefor that filter. With two arguments it applies to afilter-namewhenever the filter runsanyprovider.
proto-flagsis one or more of
change=yes
change=1:1
byteranges=no
proxy=no
proxy=transform
Cache-Control: no-transformheader.
cache=no
| 说明 | Register a content filter |
|---|---|
| 语法 | FilterProviderfilter-nameprovider-name[req|resp|env]=dispatchmatch |
| 作用域 | server config, virtual host, directory, .htaccess |
| 覆盖项 | Options |
| 状态 | 基本(B) |
| 模块 | mod_filter |
This directive registers aproviderfor the smart filter. The provider will be called if and only if thematchdeclared here matches the value of the header or environment variable declared asdispatch.
provider-namemust have been registered by loading a module that registers the name withap_register_output_filter.
dispatchargument is a string with optionalreq=,resp=或env=prefix causing it to dispatch on (respectively) the request header, response header, or environment variable named. In the absence of a prefix, it defaults to a response header. A special case is the wordhandler, which causesmod_filterto dispatch on the content handler.
matchargument specifies a match that will be applied to the filter'sdispatchcriterion. Thematchmay be a string match (exact match or substring), a regex, an integer (greater, lessthan or equals), or unconditional. The first characters of thematchargument determines this:
First, if the first character is an exclamation mark (!), this reverses the rule, so the provider will be used if and only if the matchfails.
Second, it interprets the first character excluding any leading!as follows:
| Character | Description |
|---|---|
| (none) | exact match |
$ |
substring match |
/ |
regex match (delimited by a second/) |
= |
integer equality |
< |
integer less-than |
<= |
integer less-than or equal |
> |
integer greater-than |
>= |
integer greater-than or equal |
* |
Unconditional match |
| 说明 | Get debug/diagnostic information frommod_filter |
|---|---|
| 语法 | FilterTracefilter-namelevel |
| 作用域 | server config, virtual host, directory |
| 状态 | 基本(B) |
| 模块 | mod_filter |
This directive generates debug information frommod_filter. It is designed to help test and debug providers (filter modules), although it may also help withmod_filteritself.
The debug output depends on thelevelset:
0(default)
1
mod_filterwill 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 mod_diagnostics.
2(not yet implemented)