Text of httpd.apache.org › docs › trunk › developer › output-filters.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Guide to writing output filters - 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 > Developer Documentation Guide to writing output filters Available Languages: en There are a number of common pitfalls encountered when writing output filters; this page aims to document best practice for authors of new or existing filters. This document is applicable to both version 2.0 and version 2.2 of the Apache HTTP Server; it specifically targets RESOURCE -level or CONTENT_SET -level filters though some advice is generic to all types of filter.

Filters and bucket brigades Filter invocation Brigade structure Processing buckets Filtering brigades Maintaining state Buffering buckets Non-blocking bucket reads Ten rules for output filters Use case: buffering in mod_ratelimit Filters and bucket brigades ¶ Each time a filter is invoked, it is passed a bucket brigade , containing a sequence of buckets which represent both data content and metadata. Every bucket has a bucket type ; a number of bucket types are defined and used by the httpd core modules (and the apr-util library which provides the bucket brigade interface), but modules are free to define their own types.

Output filters must be prepared to process buckets of non-standard types; with a few exceptions, a filter need not care about the types of buckets being filtered. A filter can tell whether a bucket represents either data or metadata using the APR_BUCKET_IS_METADATA macro. Generally, all metadata buckets should be passed down the filter chain by an output filter. Filters may transform, delete, and insert data buckets as appropriate.

There are two metadata bucket types which all filters must pay attention to: the EOS bucket type, and the FLUSH bucket type. An EOS bucket indicates that the end of the response has been reached and no further buckets need be processed. A FLUSH bucket indicates that the filter should flush any buffered buckets (if applicable) down the filter chain immediately. FLUSH buckets are sent when the content generator (or an upstream filter) knows that there may be a delay before more content can be sent.

By passing FLUSH buckets down the filter chain immediately, filters ensure that the client is not kept waiting for pending data longer than necessary. Filters can create FLUSH buckets and pass these down the filter chain if desired. Generating FLUSH buckets unnecessarily, or too frequently, can harm network utilisation since it may force large numbers of small packets to be sent, rather than a small number of larger packets.

The section on Non-blocking bucket reads covers a case where filters are encouraged to generate FLUSH buckets. Example bucket brigade HEAP FLUSH FILE EOS This shows a bucket brigade which may be passed to a filter; it contains two metadata buckets ( FLUSH and EOS ), and two data buckets ( HEAP and FILE ). Filter invocation ¶ For any given request, an output filter might be invoked only once and be given a single brigade representing the entire response.

It is also possible that the number of times a filter is invoked for a single response is proportional to the size of the content being filtered, with the filter being passed a brigade containing a single bucket each time. Filters must operate correctly in either case. An output filter which allocates long-lived memory every time it is invoked may consume memory proportional to response size. Output filters which need to allocate memory should do so once per response; see Maintaining state below.

An output filter can distinguish the final invocation for a given response by the presence of an EOS bucket in the brigade. Any buckets in the brigade after an EOS should be ignored. An output filter should never pass an empty brigade down the filter chain. To be defensive, filters should be prepared to accept an empty brigade, and should return success without passing this brigade on down the filter chain. The handling of an empty brigade should have no side effects (such as changing any state private to the filter).

How to handle an empty brigade apr_status_t dummy_filter(ap_filter_t *f, apr_bucket_brigade *bb) { if (APR_BRIGADE_EMPTY(bb)) { return APR_SUCCESS; } ... Brigade structure ¶ A bucket brigade is a doubly-linked list of buckets. The list is terminated (at both ends) by a sentinel which can be distinguished from a normal bucket by comparing it with the pointer returned by APR_BRIGADE_SENTINEL . The list sentinel is in fact not a valid bucket structure; any attempt to call normal bucket functions (such as apr_bucket_read ) on the sentinel will have undefined behavior (i.e. will crash the process).

There are a variety of functions and macros for traversing and manipulating bucket brigades; see the apr_buckets.h header for complete coverage. Commonly used macros include: APR_BRIGADE_FIRST(bb) returns the first bucket in brigade bb APR_BRIGADE_LAST(bb) returns the last bucket in brigade bb APR_BUCKET_NEXT(e) gives the next bucket after bucket e APR_BUCKET_PREV(e) gives the bucket before bucket e The apr_bucket_brigade structure itself is allocated out of a pool, so if a filter creates a new brigade, it must ensure that memory use is correctly bounded.

A filter which allocates a new brigade out of the request pool ( r->pool ) on every invocation, for example, will fall foul of the warning above concerning memory use. Such a filter should instead create a brigade on the first invocation per request, and store that brigade in its state structure . It is generally never advisable to use apr_brigade_destroy to "destroy" a brigade unless you know for certain that the brigade will never be used again, even then, it should be used rarely.

The memory used by the brigade structure will not be released by calling this function (since it comes from a pool), but the associated pool cleanup is unregistered. Using apr_brigade_destroy can in fact cause memory leaks; if a "destroyed" brigade contains buckets when its containing pool is destroyed, those buckets will not be immediately destroyed. In general, filters should use apr_brigade_cleanup in preference to apr_brigade_destroy .

Processing buckets ¶ When dealing with non-metadata buckets, it is important to understand that the " apr_bucket * " object is an abstract representation of data: The amount of data represented by the bucket may or may not have a determinate length; for a bucket which represents data of indeterminate length, the ->length field is set to the value (apr_size_t)-1 . For example, buckets of the PIPE bucket type have an indeterminate length; they represent the output from a pipe.

The data represented by a bucket may or may not be mapped into memory. The FILE bucket type, for example, represents data stored in a file on disk. Filters read the data from a bucket using the apr_bucket_read function. When this function is invoked, the bucket may morph into a different bucket type, and may also insert a new bucket into the bucket brigade. This must happen for buckets which represent data not mapped into memory.

To give an example; consider a bucket brigade containing a single FILE bucket representing an entire file, 24 kilobytes in size: FILE(0K-24K) When this bucket is read, it will read a block of data from the file, morph into a HEAP bucket to represent that data, and return the data to the caller. It also inserts a new FILE bucket representing the remainder of the file; after the apr_bucket_read call, the brigade looks like: HEAP(8K) FILE(8K-24K) Filtering brigades ¶ The basic function of any output filter will be to iterate through the passed-in brigade and transform (or simply examine) the content in some manner.

The implementation of the iteration loop is critical to producing a well-behaved output filter. Taking an example which loops through the entire brigade as follows: Bad output filter -- do not imitate! apr_bucket *e = APR_BRIGADE_FIRST(bb); const char *data; apr_size_t length; while

The rest of this page is on the live site.