.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "libapache2-mod-perl2-2.0.11::docs::api::Apache2::Filter 3pm" .TH libapache2-mod-perl2-2.0.11::docs::api::Apache2::Filter 3pm "2021-05-21" "perl v5.32.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Apache2::Filter \- Perl API for Apache 2.0 Filtering .SH "Synopsis" .IX Header "Synopsis" .Vb 1 \& use Apache2::Filter (); \& \& # filter attributes \& my $c = $f\->c; \& my $r = $f\->r; \& my $frec = $f\->frec(); \& my $next_f = $f\->next; \& \& my $ctx = $f\->ctx; \& $f\->ctx($ctx); \& \& # bucket brigade filtering API \& $rc = $f\->next\->get_brigade($bb, $mode, $block, $readbytes); \& $rc = $f\->next\->pass_brigade($bb); \& $rc = $f\->fflush($bb); \& \& # streaming filtering API \& while ($filter\->read(my $buffer, $wanted)) { \& # transform $buffer here \& $filter\->print($buffer); \& } \& if ($f\->seen_eos) { \& $filter\->print("filter signature"); \& } \& \& # filter manipulations \& $r\->add_input_filter(\e&callback); \& $c\->add_input_filter(\e&callback); \& $r\->add_output_filter(\e&callback); \& $c\->add_output_filter(\e&callback); \& $f\->remove; .Ve .SH "Description" .IX Header "Description" \&\f(CW\*(C`Apache2::Filter\*(C'\fR provides Perl \s-1API\s0 for Apache 2.0 filtering framework. .PP Make sure to read \f(CW\*(C`the Filtering tutorial|docs::2.0::user::handlers::filters\*(C'\fR. .SH "Common Filter API" .IX Header "Common Filter API" The following methods can be called from any filter handler: .ie n .SS """c""" .el .SS "\f(CWc\fP" .IX Subsection "c" Get the current connection object from a connection or a request filter: .PP .Vb 1 \& $c = $f\->c; .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" .PD 0 .ie n .IP "ret: $c ( ""Apache2::Connection object"" )" 4 .el .IP "ret: \f(CW$c\fR ( \f(CWApache2::Connection object\fR )" 4 .IX Item "ret: $c ( Apache2::Connection object )" .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PD .ie n .SS """ctx""" .el .SS "\f(CWctx\fP" .IX Subsection "ctx" Get/set the filter context data. .PP .Vb 2 \& $ctx = $f\->ctx; \& $f\->ctx($ctx); .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" .PD 0 .ie n .IP "opt arg2: $ctx ( \s-1SCALAR\s0 )" 4 .el .IP "opt arg2: \f(CW$ctx\fR ( \s-1SCALAR\s0 )" 4 .IX Item "opt arg2: $ctx ( SCALAR )" .PD next context .ie n .IP "ret: $ctx ( \s-1SCALAR\s0 )" 4 .el .IP "ret: \f(CW$ctx\fR ( \s-1SCALAR\s0 )" 4 .IX Item "ret: $ctx ( SCALAR )" current context .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PP A filter context is created before the filter is called for the first time and it's destroyed at the end of the request. The context is preserved between filter invocations of the same request. So if a filter needs to store some data between invocations it should use the filter context for that. The filter context is initialized with the \&\f(CW\*(C`undef\*(C'\fR value. .PP The \f(CW\*(C`ctx\*(C'\fR method accepts a single \s-1SCALAR\s0 argument. Therefore if you want to store any other perl datastructure you should use a reference to it. .PP For example you can store a hash reference: .PP .Vb 1 \& $f\->ctx({ foo => \*(Aqbar\*(Aq }); .Ve .PP and then access it: .PP .Vb 1 \& $foo = $f\->ctx\->{foo}; .Ve .PP if you access the context more than once it's more efficient to copy it's value before using it: .PP .Vb 2 \& my $ctx = $f\->ctx; \& $foo = $ctx\->{foo}; .Ve .PP to avoid redundant method calls. As of this writing \f(CW$ctx\fR is not a tied variable, so if you modify it need to store it at the end: .PP .Vb 1 \& $f\->ctx($ctx); .Ve .PP \&\s-1META:\s0 later we might make it a TIEd-variable interface, so it'll be stored automatically. .PP Besides its primary purpose of storing context data across multiple filter invocations, this method is also useful when used as a flag. For example here is how to ensure that something happens only once during the filter's life: .PP .Vb 4 \& unless ($f\->ctx) { \& do_something_once(); \& $f\->ctx(1); \& } .Ve .ie n .SS """frec""" .el .SS "\f(CWfrec\fP" .IX Subsection "frec" Get/set the \f(CW\*(C`Apache2::FilterRec\*(C'\fR (filter record) object. .PP .Vb 1 \& $frec = $f\->frec(); .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" .PD 0 .ie n .IP "ret: $frec ( ""Apache2::FilterRec object"" )" 4 .el .IP "ret: \f(CW$frec\fR ( \f(CWApache2::FilterRec object\fR )" 4 .IX Item "ret: $frec ( Apache2::FilterRec object )" .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PD .PP For example you can call \&\f(CW\*(C`$frec\->name\*(C'\fR to get filter's name. .ie n .SS """next""" .el .SS "\f(CWnext\fP" .IX Subsection "next" Return the \f(CW\*(C`Apache2::Filter\*(C'\fR object of the next filter in chain. .PP .Vb 1 \& $next_f = $f\->next; .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" The current filter object .ie n .IP "ret: $next_f ( ""Apache2::Filter object"" )" 4 .el .IP "ret: \f(CW$next_f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "ret: $next_f ( Apache2::Filter object )" The next filter object in chain .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PP Since Apache inserts several core filters at the end of each chain, normally this method always returns an object. However if it's not a mod_perl filter handler, you can call only the following methods on it: \f(CW\*(C`get_brigade\*(C'\fR, \&\f(CW\*(C`pass_brigade\*(C'\fR, \f(CW\*(C`c\*(C'\fR, \f(CW\*(C`r\*(C'\fR, \&\f(CW\*(C`frec\*(C'\fR and \f(CW\*(C`next\*(C'\fR. If you call other methods the behavior is undefined. .PP The next filter can be a mod_perl one or not, it's easy to tell which one is that by calling \&\f(CW\*(C`$f\->frec\->name\*(C'\fR. .ie n .SS """r""" .el .SS "\f(CWr\fP" .IX Subsection "r" Inside an \s-1HTTP\s0 request filter retrieve the current request object: .PP .Vb 1 \& $r = $f\->r; .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" .PD 0 .ie n .IP "ret: $r ( ""Apache2::RequestRec object"" )" 4 .el .IP "ret: \f(CW$r\fR ( \f(CWApache2::RequestRec object\fR )" 4 .IX Item "ret: $r ( Apache2::RequestRec object )" .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PD .PP If a sub-request adds filters, then that sub-request object is associated with the filter. .ie n .SS """remove""" .el .SS "\f(CWremove\fP" .IX Subsection "remove" Remove the current filter from the filter chain (for the current request or connection). .PP .Vb 1 \& $f\->remove; .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" .PD 0 .IP "ret: no return value" 4 .IX Item "ret: no return value" .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PD .PP Notice that you should either complete the current filter invocation normally (by calling \f(CW\*(C`get_brigade\*(C'\fR or \&\f(CW\*(C`pass_brigade\*(C'\fR depending on the filter kind) or if nothing was done, return \f(CW\*(C`Apache2::Const::DECLINED\*(C'\fR and mod_perl will take care of passing the current bucket brigade through unmodified to the next filter in chain. .PP Note: calling \fBremove()\fR on the very top connection filter doesn't affect the filter chain due to a bug in Apache 2.0 (which may be fixed in 2.1). So don't use it with connection filters, till it gets fixed in Apache and then make sure to require the minimum Apache version if you rely on. .PP Remember that if the connection is \&\f(CW\*(C`$c\->keepalive\*(C'\fR ) and the connection filter is removed, it won't be added until the connection is closed. Which may happen after many \s-1HTTP\s0 requests. You may want to keep the filter in place and pass the data through unmodified, by returning \f(CW\*(C`Apache2::Const::DECLINED\*(C'\fR. If you need to reset the whole or parts of the filter context between requests, use the technique based on \f(CW\*(C`$c\->keepalives\*(C'\fR counting. .PP This method works for native Apache (non\-mod_perl) filters too. .SH "Bucket Brigade Filter API" .IX Header "Bucket Brigade Filter API" The following methods can be called from any filter, directly manipulating bucket brigades: .ie n .SS """fflush""" .el .SS "\f(CWfflush\fP" .IX Subsection "fflush" Flush a bucket brigade down the filter stack. .PP .Vb 1 \& $rc = $f\->fflush($bb); .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" The current filter .ie n .IP "arg1: $bb ( ""Apache2::Brigade object"" )" 4 .el .IP "arg1: \f(CW$bb\fR ( \f(CWApache2::Brigade object\fR )" 4 .IX Item "arg1: $bb ( Apache2::Brigade object )" The brigade to flush .ie n .IP "ret: $rc ( ""APR::Const status constant"" )" 4 .el .IP "ret: \f(CW$rc\fR ( \f(CWAPR::Const status constant\fR )" 4 .IX Item "ret: $rc ( APR::Const status constant )" Refer to the \f(CW\*(C`pass_brigade()\*(C'\fR entry. .ie n .IP "excpt: ""APR::Error""" 4 .el .IP "excpt: \f(CWAPR::Error\fR" 4 .IX Item "excpt: APR::Error" Exceptions are thrown only when this function is called in the \s-1VOID\s0 context. Refer to the \f(CW\*(C`get_brigade()\*(C'\fR entry for details. .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PP \&\f(CW\*(C`fflush\*(C'\fR is a shortcut method. So instead of doing: .PP .Vb 3 \& my $b = APR::Bucket::flush_create($f\->c\->bucket_alloc); \& $bb\->insert_tail($b); \& $f\->pass_brigade($bb); .Ve .PP one can just write: .PP .Vb 1 \& $f\->fflush($bb); .Ve .ie n .SS """get_brigade""" .el .SS "\f(CWget_brigade\fP" .IX Subsection "get_brigade" This is a method to use in bucket brigade input filters. It acquires a bucket brigade from the upstream input filter. .PP .Vb 4 \& $rc = $next_f\->get_brigade($bb, $mode, $block, $readbytes); \& $rc = $next_f\->get_brigade($bb, $mode, $block); \& $rc = $next_f\->get_brigade($bb, $mode) \& $rc = $next_f\->get_brigade($bb); .Ve .ie n .IP "obj: $next_f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$next_f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $next_f ( Apache2::Filter object )" The next filter in the filter chain. .Sp Inside filter handlers it's usually \f(CW\*(C`$f\->next\*(C'\fR. Inside protocol handlers: \&\f(CW\*(C`$c\->input_filters\*(C'\fR. .ie n .IP "arg1: $bb ( ""APR::Brigade object"" )" 4 .el .IP "arg1: \f(CW$bb\fR ( \f(CWAPR::Brigade object\fR )" 4 .IX Item "arg1: $bb ( APR::Brigade object )" The original bucket brigade passed to \f(CW\*(C`get_brigade()\*(C'\fR, which must be empty. .Sp Inside input filter handlers it's usually the second argument to the filter handler. .Sp Otherwise it should be created: .Sp .Vb 1 \& my $bb = APR::Brigade\->new($c\->pool, $c\->bucket_alloc); .Ve .Sp On return it gets populated with the next bucket brigade. That brigade may contain nothing if there was no more data to read. The return status tells the outcome. .ie n .IP "opt arg2: $mode ( ""Apache2::Const :input_mode constant"" )" 4 .el .IP "opt arg2: \f(CW$mode\fR ( \f(CWApache2::Const :input_mode constant\fR )" 4 .IX Item "opt arg2: $mode ( Apache2::Const :input_mode constant )" The filter mode in which the data should be read. .Sp If inside the filter handler, you should normally pass the same mode that was passed to the filter handler (the third argument). .Sp At the end of this section the available modes are presented. .Sp If the argument \f(CW$mode\fR is not passed, \&\f(CW\*(C`Apache2::Const::MODE_READBYTES\*(C'\fR is used as a default value. .ie n .IP "opt arg3: $block ( ""APR::Const :read_type constant"" )" 4 .el .IP "opt arg3: \f(CW$block\fR ( \f(CWAPR::Const :read_type constant\fR )" 4 .IX Item "opt arg3: $block ( APR::Const :read_type constant )" You may ask the reading operation to be blocking: \&\f(CW\*(C`APR::Const::BLOCK_READ\*(C'\fR, or nonblocking: \&\f(CW\*(C`APR::Const::NONBLOCK_READ\*(C'\fR. .Sp If inside the filter handler, you should normally pass the same blocking mode argument that was passed to the filter handler (the forth argument). .Sp If the argument \f(CW$block\fR is not passed, \&\f(CW\*(C`APR::Const::BLOCK_READ\*(C'\fR is used as a default value. .ie n .IP "opt arg4: $readbytes ( integer )" 4 .el .IP "opt arg4: \f(CW$readbytes\fR ( integer )" 4 .IX Item "opt arg4: $readbytes ( integer )" How many bytes to read from the next filter. .Sp If inside the filter handler, you may want the same number of bytes, as the upstream filter, i.e. the argument that was passed to the filter handler (the fifth argument). .Sp If the argument \f(CW$block\fR is not passed, 8192 is used as a default value. .ie n .IP "ret: $rc ( ""APR::Const status constant"" )" 4 .el .IP "ret: \f(CW$rc\fR ( \f(CWAPR::Const status constant\fR )" 4 .IX Item "ret: $rc ( APR::Const status constant )" On success, \&\f(CW\*(C`APR::Const::SUCCESS\*(C'\fR is returned and \f(CW$bb\fR is populated (see the \f(CW$bb\fR entry). .Sp In case of a failure \*(-- a failure code is returned, in which case normally it should be returned to the caller. .Sp If the bottom-most filter doesn't read from the network, then \&\f(CW\*(C`Apache2::NOBODY_READ\*(C'\fR is returned (\s-1META:\s0 need to add this constant). .Sp Inside protocol handlers the return code can also be \f(CW\*(C`APR::Const::EOF\*(C'\fR, which is success as well. .ie n .IP "excpt: ""APR::Error""" 4 .el .IP "excpt: \f(CWAPR::Error\fR" 4 .IX Item "excpt: APR::Error" You don't have to ask for the return value. If this function is called in the \s-1VOID\s0 context, e.g.: .Sp .Vb 1 \& $f\->next\->get_brigade($bb, $mode, $block, $readbytes); .Ve .Sp mod_perl will do the error checking on your behalf, and if the return code is not \&\f(CW\*(C`APR::Const::SUCCESS\*(C'\fR, an \&\f(CW\*(C`APR::Error exception\*(C'\fR will be thrown. The only time you want to do the error checking yourself, is when return codes besides \&\f(CW\*(C`APR::Const::SUCCESS\*(C'\fR are considered as successful and you want to manage them by yourself. .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PP Available input filter modes (the optional second argument \f(CW$mode\fR) are: .IP "\(bu" 4 \&\f(CW\*(C`Apache2::Const::MODE_READBYTES\*(C'\fR .Sp The filter should return at most readbytes data .IP "\(bu" 4 \&\f(CW\*(C`Apache2::Const::MODE_GETLINE\*(C'\fR .Sp The filter should return at most one line of \s-1CRLF\s0 data. (If a potential line is too long or no \s-1CRLF\s0 is found, the filter may return partial data). .IP "\(bu" 4 \&\f(CW\*(C`Apache2::Const::MODE_EATCRLF\*(C'\fR .Sp The filter should implicitly eat any \s-1CRLF\s0 pairs that it sees. .IP "\(bu" 4 \&\f(CW\*(C`Apache2::Const::MODE_SPECULATIVE\*(C'\fR .Sp The filter read should be treated as speculative and any returned data should be stored for later retrieval in another mode. .IP "\(bu" 4 \&\f(CW\*(C`Apache2::Const::MODE_EXHAUSTIVE\*(C'\fR .Sp The filter read should be exhaustive and read until it can not read any more. Use this mode with extreme caution. .IP "\(bu" 4 \&\f(CW\*(C`Apache2::Const::MODE_INIT\*(C'\fR .Sp The filter should initialize the connection if needed, \s-1NNTP\s0 or \s-1FTP\s0 over \s-1SSL\s0 for example. .PP Either compile all these constants with: .PP .Vb 1 \& use Apache2::Const \-compile => qw(:input_mode); .Ve .PP But it's a bit more efficient to compile only those constants that you need. .PP Example: .PP Here is a fragment of a filter handler, that receives a bucket brigade from the upstream filter: .PP .Vb 5 \& use Apache2::Filter (); \& use APR::Const \-compile => qw(SUCCESS); \& use Apache2::Const \-compile => qw(OK); \& sub filter { \& my ($f, $bb, $mode, $block, $readbytes) = @_; \& \& my $rc = $f\->next\->get_brigade($bb, $mode, $block, $readbytes); \& return $rc unless $rc == APR::Const::SUCCESS; \& \& # ... process $bb \& \& return Apache2::Const::OK; \& } .Ve .PP Usually arguments \f(CW$mode\fR, \f(CW$block\fR, \f(CW$readbytes\fR are the same as passed to the filter itself. .PP You can see that in case of a failure, the handler returns immediately with that failure code, which gets propagated to the downstream filter. .PP If you decide not check the return code, you can write it as: .PP .Vb 2 \& sub filter { \& my ($f, $bb, $mode, $block, $readbytes) = @_; \& \& $f\->next\->get_brigade($bb, $mode, $block, $readbytes); \& \& # ... process $bb \& \& return Apache2::Const::OK; \& } .Ve .PP and the error checking will be done on your behalf. .PP You will find many more examples in \f(CW\*(C`the filter handlers|docs::2.0::user::handlers::filters\*(C'\fR and \&\f(CW\*(C`the protocol handlers|docs::2.0::user::handlers::protocols\*(C'\fR tutorials. .ie n .SS """pass_brigade""" .el .SS "\f(CWpass_brigade\fP" .IX Subsection "pass_brigade" This is a method to use in bucket brigade output filters. It passes the current bucket brigade to the downstream output filter. .PP .Vb 1 \& $rc = $next_f\->pass_brigade($bb); .Ve .ie n .IP "obj: $next_f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$next_f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $next_f ( Apache2::Filter object )" The next filter in the filter chain. .Sp Inside output filter handlers it's usually \f(CW\*(C`$f\->next\*(C'\fR. Inside protocol handlers: \&\f(CW\*(C`$c\->output_filters\*(C'\fR. .ie n .IP "arg1: $bb ( ""APR::Brigade object"" )" 4 .el .IP "arg1: \f(CW$bb\fR ( \f(CWAPR::Brigade object\fR )" 4 .IX Item "arg1: $bb ( APR::Brigade object )" The bucket brigade to pass. .Sp Inside output filter handlers it's usually the second argument to the filter handler (after potential manipulations). .ie n .IP "ret: $rc ( ""APR::Const status constant"" )" 4 .el .IP "ret: \f(CW$rc\fR ( \f(CWAPR::Const status constant\fR )" 4 .IX Item "ret: $rc ( APR::Const status constant )" On success, \&\f(CW\*(C`APR::Const::SUCCESS\*(C'\fR is returned. .Sp In case of a failure \*(-- a failure code is returned, in which case normally it should be returned to the caller. .Sp If the bottom-most filter doesn't write to the network, then \&\f(CW\*(C`Apache2::NOBODY_WROTE\*(C'\fR is returned (\s-1META:\s0 need to add this constant). .Sp Also refer to the \f(CW\*(C`get_brigade()\*(C'\fR entry to see how to avoid checking the errors explicitly. .ie n .IP "excpt: ""APR::Error""" 4 .el .IP "excpt: \f(CWAPR::Error\fR" 4 .IX Item "excpt: APR::Error" Exceptions are thrown only when this function is called in the \s-1VOID\s0 context. Refer to the \f(CW\*(C`get_brigade()\*(C'\fR entry for details. .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PP The caller relinquishes ownership of the brigade (i.e. it may get destroyed/overwritten/etc. by the callee). .PP Example: .PP Here is a fragment of a filter handler, that passes a bucket brigade to the downstream filter (after some potential processing of the buckets in the bucket brigade): .PP .Vb 5 \& use Apache2::Filter (); \& use APR::Const \-compile => qw(SUCCESS); \& use Apache2::Const \-compile => qw(OK); \& sub filter { \& my ($f, $bb) = @_; \& \& # ... process $bb \& \& my $rc = $f\->next\->pass_brigade($bb); \& return $rc unless $rc == APR::Const::SUCCESS; \& \& return Apache2::Const::OK; \& } .Ve .SH "Streaming Filter API" .IX Header "Streaming Filter API" The following methods can be called from any filter, which uses the simplified streaming functionality: .ie n .SS """print""" .el .SS "\f(CWprint\fP" .IX Subsection "print" Send the contents of \f(CW$buffer\fR to the next filter in chain (via internal buffer). .PP .Vb 1 \& $sent = $f\->print($buffer); .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" .PD 0 .ie n .IP "arg1: $buffer ( string )" 4 .el .IP "arg1: \f(CW$buffer\fR ( string )" 4 .IX Item "arg1: $buffer ( string )" .PD The data to send. .ie n .IP "ret: $sent ( integer )" 4 .el .IP "ret: \f(CW$sent\fR ( integer )" 4 .IX Item "ret: $sent ( integer )" How many characters were sent. There is no need to check, since all should go through and if something goes work an exception will be thrown. .ie n .IP "excpt: ""APR::Error""" 4 .el .IP "excpt: \f(CWAPR::Error\fR" 4 .IX Item "excpt: APR::Error" .PD 0 .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PD .PP This method should be used only in streaming filters. .ie n .SS """read""" .el .SS "\f(CWread\fP" .IX Subsection "read" Read data from the filter .PP .Vb 1 \& $read = $f\->read($buffer, $wanted); .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" .PD 0 .ie n .IP "arg1: $buffer ( \s-1SCALAR\s0 )" 4 .el .IP "arg1: \f(CW$buffer\fR ( \s-1SCALAR\s0 )" 4 .IX Item "arg1: $buffer ( SCALAR )" .PD The buffer to fill. All previous data will be lost. .ie n .IP "opt arg2: $wanted ( integer )" 4 .el .IP "opt arg2: \f(CW$wanted\fR ( integer )" 4 .IX Item "opt arg2: $wanted ( integer )" How many bytes to attempt to read. .Sp If this optional argument is not specified \*(-- the default 8192 will be used. .ie n .IP "ret: $read ( integer )" 4 .el .IP "ret: \f(CW$read\fR ( integer )" 4 .IX Item "ret: $read ( integer )" How many bytes were actually read. .Sp \&\f(CW$buffer\fR gets populated with the string that is read. It will contain an empty string if there was nothing to read. .ie n .IP "excpt: ""APR::Error""" 4 .el .IP "excpt: \f(CWAPR::Error\fR" 4 .IX Item "excpt: APR::Error" .PD 0 .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PD .PP Reads at most \f(CW$wanted\fR characters into \f(CW$buffer\fR. The returned value \f(CW$read\fR tells exactly how many were read, making it easy to use it in a while loop: .PP .Vb 4 \& while ($filter\->read(my $buffer, $wanted)) { \& # transform $buffer here \& $filter\->print($buffer); \& } .Ve .PP This is a streaming filter method, which acquires a single bucket brigade behind the scenes and reads data from all its buckets. Therefore it can only read from one bucket brigade per filter invocation. .PP If the \s-1EOS\s0 bucket is read, the \f(CW\*(C`seen_eos\*(C'\fR method will return a true value. .ie n .SS """seen_eos""" .el .SS "\f(CWseen_eos\fP" .IX Subsection "seen_eos" This methods returns a true value when the \s-1EOS\s0 bucket is seen by the \&\f(CW\*(C`read\*(C'\fR method. .PP .Vb 1 \& $ok = $f\->seen_eos; .Ve .ie n .IP "obj: $f ( ""Apache2::Filter object"" )" 4 .el .IP "obj: \f(CW$f\fR ( \f(CWApache2::Filter object\fR )" 4 .IX Item "obj: $f ( Apache2::Filter object )" The filter to remove .ie n .IP "ret: $ok ( boolean )" 4 .el .IP "ret: \f(CW$ok\fR ( boolean )" 4 .IX Item "ret: $ok ( boolean )" a true value if \s-1EOS\s0 has been seen, otherwise a false value .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PP This method only works in streaming filters which exhaustively \&\f(CW\*(C`$f\->read\*(C'\fR all the incoming data in a while loop, like so: .PP .Vb 6 \& while ($f\->read(my $buffer, $wanted)) { \& # do something with $buffer \& } \& if ($f\->seen_eos) { \& # do something \& } .Ve .PP The technique in this example is useful when a streaming filter wants to append something to the very end of data, or do something at the end of the last filter invocation. After the \s-1EOS\s0 bucket is read, the filter should expect not to be invoked again. .PP If an input streaming filter doesn't consume all data in the bucket brigade (or even in several bucket brigades), it has to generate the \&\s-1EOS\s0 event by itself. So when the filter is done it has to set the \s-1EOS\s0 flag: .PP .Vb 1 \& $f\->seen_eos(1); .Ve .PP when the filter handler returns, internally mod_perl will take care of creating and sending the \s-1EOS\s0 bucket to the upstream input filter. .PP A similar logic may apply for output filters. .PP In most other cases you shouldn't set this flag. When this flag is prematurely set (before the real \s-1EOS\s0 bucket has arrived) in the current filter invocation, instead of invoking the filter again, mod_perl will create and send the \s-1EOS\s0 bucket to the next filter, ignoring any other bucket brigades that may have left to consume. As mentioned earlier this special behavior is useful in writing special tests that test abnormal situations. .SH "Other Filter-related API" .IX Header "Other Filter-related API" Other methods which affect filters, but called on non\-\f(CW\*(C`Apache2::Filter\*(C'\fR objects: .ie n .SS """add_input_filter""" .el .SS "\f(CWadd_input_filter\fP" .IX Subsection "add_input_filter" Add \f(CW&callback\fR filter handler to input request filter chain. .PP .Vb 1 \& $r\->add_input_filter(\e&callback); .Ve .PP Add \f(CW&callback\fR filter handler to input connection filter chain. .PP .Vb 1 \& $c\->add_input_filter(\e&callback); .Ve .ie n .IP "obj: $c ( ""Apache2::Connection object"" ) or $r ( ""Apache2::RequestRec object"" )" 4 .el .IP "obj: \f(CW$c\fR ( \f(CWApache2::Connection object\fR ) or \f(CW$r\fR ( \f(CWApache2::RequestRec object\fR )" 4 .IX Item "obj: $c ( Apache2::Connection object ) or $r ( Apache2::RequestRec object )" .PD 0 .ie n .IP "arg1: &callback (\s-1CODE\s0 ref)" 4 .el .IP "arg1: \f(CW&callback\fR (\s-1CODE\s0 ref)" 4 .IX Item "arg1: &callback (CODE ref)" .IP "ret: no return value" 4 .IX Item "ret: no return value" .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PD .PP [\s-1META:\s0 It seems that you can't add a filter when another filter is called. I've tried to add an output connection filter from the input connection filter when it was called for the first time. It didn't have any affect for the first request (over keepalive connection). The only way I succeeded to do that is from that input connection filter's filter_init handler. In fact it does work if there is any filter additional filter of the same kind configured from httpd.conf or via filter_init. It looks like there is a bug in httpd, where it doesn't prepare the chain of 3rd party filter if none were inserted before the first filter was called.] .ie n .SS """add_output_filter""" .el .SS "\f(CWadd_output_filter\fP" .IX Subsection "add_output_filter" Add \f(CW&callback\fR filter handler to output request filter chain. .PP .Vb 1 \& $r\->add_output_filter(\e&callback); .Ve .PP Add \f(CW&callback\fR filter handler to output connection filter chain. .PP .Vb 1 \& $c\->add_output_filter(\e&callback); .Ve .ie n .IP "obj: $c ( ""Apache2::Connection object"" ) or $r ( ""Apache2::RequestRec object"" )" 4 .el .IP "obj: \f(CW$c\fR ( \f(CWApache2::Connection object\fR ) or \f(CW$r\fR ( \f(CWApache2::RequestRec object\fR )" 4 .IX Item "obj: $c ( Apache2::Connection object ) or $r ( Apache2::RequestRec object )" .PD 0 .ie n .IP "arg1: &callback (\s-1CODE\s0 ref)" 4 .el .IP "arg1: \f(CW&callback\fR (\s-1CODE\s0 ref)" 4 .IX Item "arg1: &callback (CODE ref)" .IP "ret: no return value" 4 .IX Item "ret: no return value" .IP "since: 2.0.00" 4 .IX Item "since: 2.0.00" .PD .SH "Filter Handler Attributes" .IX Header "Filter Handler Attributes" Packages using filter attributes have to subclass \f(CW\*(C`Apache2::Filter\*(C'\fR: .PP .Vb 2 \& package MyApache2::FilterCool; \& use base qw(Apache2::Filter); .Ve .PP Attributes are parsed during the code compilation, by the function \&\f(CW\*(C`MODIFY_CODE_ATTRIBUTES\*(C'\fR, inherited from the \f(CW\*(C`Apache2::Filter\*(C'\fR package. .ie n .SS """FilterRequestHandler""" .el .SS "\f(CWFilterRequestHandler\fP" .IX Subsection "FilterRequestHandler" The \f(CW\*(C`FilterRequestHandler\*(C'\fR attribute tells mod_perl to insert the filter into an \s-1HTTP\s0 request filter chain. .PP For example, to configure an output request filter handler, use the \&\f(CW\*(C`FilterRequestHandler\*(C'\fR attribute in the handler subroutine's declaration: .PP .Vb 2 \& package MyApache2::FilterOutputReq; \& sub handler : FilterRequestHandler { ... } .Ve .PP and add the configuration entry: .PP .Vb 1 \& PerlOutputFilterHandler MyApache2::FilterOutputReq .Ve .PP This is the default mode. So if you are writing an \s-1HTTP\s0 request filter, you don't have to specify this attribute. .PP The section \s-1HTTP\s0 Request vs. Connection Filters delves into more details. .ie n .SS """FilterConnectionHandler""" .el .SS "\f(CWFilterConnectionHandler\fP" .IX Subsection "FilterConnectionHandler" The \f(CW\*(C`FilterConnectionHandler\*(C'\fR attribute tells mod_perl to insert this filter into a connection filter chain. .PP For example, to configure an output connection filter handler, use the \&\f(CW\*(C`FilterConnectionHandler\*(C'\fR attribute in the handler subroutine's declaration: .PP .Vb 2 \& package MyApache2::FilterOutputCon; \& sub handler : FilterConnectionHandler { ... } .Ve .PP and add the configuration entry: .PP .Vb 1 \& PerlOutputFilterHandler MyApache2::FilterOutputCon .Ve .PP The section \s-1HTTP\s0 Request vs. Connection Filters delves into more details. .ie n .SS """FilterInitHandler""" .el .SS "\f(CWFilterInitHandler\fP" .IX Subsection "FilterInitHandler" The attribute \f(CW\*(C`FilterInitHandler\*(C'\fR marks the function suitable to be used as a filter initialization callback, which is called immediately after a filter is inserted to the filter chain and before it's actually called. .PP .Vb 5 \& sub init : FilterInitHandler { \& my $f = shift; \& #... \& return Apache2::Const::OK; \& } .Ve .PP In order to hook this filter callback, the real filter has to assign this callback using the \&\f(CW\*(C`FilterHasInitHandler\*(C'\fR which accepts a reference to the callback function. .PP For further discussion and examples refer to the Filter Initialization Phase tutorial section. .ie n .SS """FilterHasInitHandler""" .el .SS "\f(CWFilterHasInitHandler\fP" .IX Subsection "FilterHasInitHandler" If a filter wants to run an initialization callback it can register such using the \f(CW\*(C`FilterHasInitHandler\*(C'\fR attribute. Similar to \&\f(CW\*(C`push_handlers\*(C'\fR the callback reference is expected, rather than a callback name. The used callback function has to have the \&\f(CW\*(C`FilterInitHandler\*(C'\fR attribute. For example: .PP .Vb 8 \& package MyApache2::FilterBar; \& use base qw(Apache2::Filter); \& sub init : FilterInitHandler { ... } \& sub filter : FilterRequestHandler FilterHasInitHandler(\e&init) { \& my ($f, $bb) = @_; \& # ... \& return Apache2::Const::OK; \& } .Ve .PP For further discussion and examples refer to the Filter Initialization Phase tutorial section. .SH "Configuration" .IX Header "Configuration" mod_perl 2.0 filters configuration is explained in the filter handlers tutorial. .ie n .SS """PerlInputFilterHandler""" .el .SS "\f(CWPerlInputFilterHandler\fP" .IX Subsection "PerlInputFilterHandler" See \&\f(CW\*(C`PerlInputFilterHandler\*(C'\fR. .ie n .SS """PerlOutputFilterHandler""" .el .SS "\f(CWPerlOutputFilterHandler\fP" .IX Subsection "PerlOutputFilterHandler" See \&\f(CW\*(C`PerlOutputFilterHandler\*(C'\fR. .ie n .SS """PerlSetInputFilter""" .el .SS "\f(CWPerlSetInputFilter\fP" .IX Subsection "PerlSetInputFilter" See \&\f(CW\*(C`PerlSetInputFilter\*(C'\fR. .ie n .SS """PerlSetOutputFilter""" .el .SS "\f(CWPerlSetOutputFilter\fP" .IX Subsection "PerlSetOutputFilter" See \&\f(CW\*(C`PerlSetInputFilter\*(C'\fR. .SH "TIE Interface" .IX Header "TIE Interface" \&\f(CW\*(C`Apache2::Filter\*(C'\fR also implements a tied interface, so you can work with the \f(CW$f\fR object as a hash reference. .PP The \s-1TIE\s0 interface is mostly unimplemented and might be implemented post 2.0 release. .ie n .SS """TIEHANDLE""" .el .SS "\f(CWTIEHANDLE\fP" .IX Subsection "TIEHANDLE" .Vb 1 \& $ret = TIEHANDLE($stashsv, $sv); .Ve .ie n .IP "obj: $stashsv ( \s-1SCALAR\s0 )" 4 .el .IP "obj: \f(CW$stashsv\fR ( \s-1SCALAR\s0 )" 4 .IX Item "obj: $stashsv ( SCALAR )" .PD 0 .ie n .IP "arg1: $sv ( \s-1SCALAR\s0 )" 4 .el .IP "arg1: \f(CW$sv\fR ( \s-1SCALAR\s0 )" 4 .IX Item "arg1: $sv ( SCALAR )" .ie n .IP "ret: $ret ( \s-1SCALAR\s0 )" 4 .el .IP "ret: \f(CW$ret\fR ( \s-1SCALAR\s0 )" 4 .IX Item "ret: $ret ( SCALAR )" .IP "since: subject to change" 4 .IX Item "since: subject to change" .PD .ie n .SS """PRINT""" .el .SS "\f(CWPRINT\fP" .IX Subsection "PRINT" .Vb 1 \& $ret = PRINT(...); .Ve .ie n .IP "obj: ""..."" (\s-1XXX\s0)" 4 .el .IP "obj: \f(CW...\fR (\s-1XXX\s0)" 4 .IX Item "obj: ... (XXX)" .PD 0 .ie n .IP "ret: $ret ( integer )" 4 .el .IP "ret: \f(CW$ret\fR ( integer )" 4 .IX Item "ret: $ret ( integer )" .IP "since: subject to change" 4 .IX Item "since: subject to change" .PD .SH "See Also" .IX Header "See Also" mod_perl 2.0 documentation. .SH "Copyright" .IX Header "Copyright" mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0. .SH "Authors" .IX Header "Authors" The mod_perl development team and numerous contributors.