1docs::api::Apache2::FilUtseerr(3C)ontributed Perl Documednotcast:i:oanpi::Apache2::Filter(3)
2
3
4
6 Apache2::Filter - Perl API for Apache 2.0 Filtering
7
9 use Apache2::Filter ();
10
11 # filter attributes
12 my $c = $f->c;
13 my $r = $f->r;
14 my $frec = $f->frec();
15 my $next_f = $f->next;
16
17 my $ctx = $f->ctx;
18 $f->ctx($ctx);
19
20 # bucket brigade filtering API
21 $rc = $f->next->get_brigade($bb, $mode, $block, $readbytes);
22 $rc = $f->next->pass_brigade($bb);
23 $rc = $f->fflush($bb);
24
25 # streaming filtering API
26 while ($filter->read(my $buffer, $wanted)) {
27 # transform $buffer here
28 $filter->print($buffer);
29 }
30 if ($f->seen_eos) {
31 $filter->print("filter signature");
32 }
33
34 # filter manipulations
35 $r->add_input_filter(\&callback);
36 $c->add_input_filter(\&callback);
37 $r->add_output_filter(\&callback);
38 $c->add_output_filter(\&callback);
39 $f->remove;
40
42 "Apache2::Filter" provides Perl API for Apache 2.0 filtering framework.
43
44 Make sure to read "the Filtering tutorial⎪docs::2.0::user::han‐
45 dlers::filters".
46
48 The following methods can be called from any filter handler:
49
50 "c"
51
52 Get the current connection object from a connection or a request fil‐
53 ter:
54
55 $c = $f->c;
56
57 obj: $f ( "Apache2::Filter object" )
58 ret: $c ( "Apache2::Connection object" )
59 since: 2.0.00
60
61 "ctx"
62
63 Get/set the filter context data.
64
65 $ctx = $f->ctx;
66 $f->ctx($ctx);
67
68 obj: $f ( "Apache2::Filter object" )
69 opt arg2: $ctx ( SCALAR )
70 next context
71
72 ret: $ctx ( SCALAR )
73 current context
74
75 since: 2.0.00
76
77 A filter context is created before the filter is called for the first
78 time and it's destroyed at the end of the request. The context is pre‐
79 served between filter invocations of the same request. So if a filter
80 needs to store some data between invocations it should use the filter
81 context for that. The filter context is initialized with the "undef"
82 value.
83
84 The "ctx" method accepts a single SCALAR argument. Therefore if you
85 want to store any other perl datastructure you should use a reference
86 to it.
87
88 For example you can store a hash reference:
89
90 $f->ctx({ foo => 'bar' });
91
92 and then access it:
93
94 $foo = $f->ctx->{foo};
95
96 if you access the context more than once it's more efficient to copy
97 it's value before using it:
98
99 my $ctx = $f->ctx;
100 $foo = $ctx->{foo};
101
102 to avoid redundant method calls. As of this writing $ctx is not a tied
103 variable, so if you modify it need to store it at the end:
104
105 $f->ctx($ctx);
106
107 META: later we might make it a TIEd-variable interface, so it'll be
108 stored automatically.
109
110 Besides its primary purpose of storing context data across multiple
111 filter invocations, this method is also useful when used as a flag. For
112 example here is how to ensure that something happens only once during
113 the filter's life:
114
115 unless ($f->ctx) {
116 do_something_once();
117 $f->ctx(1);
118 }
119
120 "frec"
121
122 Get/set the "Apache2::FilterRec" (filter record) object.
123
124 $frec = $f->frec();
125
126 obj: $f ( "Apache2::Filter object" )
127 ret: $frec ( "Apache2::FilterRec object" )
128 since: 2.0.00
129
130 For example you can call "$frec->name" to get filter's name.
131
132 "next"
133
134 Return the "Apache2::Filter" object of the next filter in chain.
135
136 $next_f = $f->next;
137
138 obj: $f ( "Apache2::Filter object" )
139 The current filter object
140
141 ret: $next_f ( "Apache2::Filter object" )
142 The next filter object in chain
143
144 since: 2.0.00
145
146 Since Apache inserts several core filters at the end of each chain,
147 normally this method always returns an object. However if it's not a
148 mod_perl filter handler, you can call only the following methods on it:
149 "get_brigade", "pass_brigade", "c", "r", "frec" and "next". If you call
150 other methods the behavior is undefined.
151
152 The next filter can be a mod_perl one or not, it's easy to tell which
153 one is that by calling "$f->frec->name".
154
155 "r"
156
157 Inside an HTTP request filter retrieve the current request object:
158
159 $r = $f->r;
160
161 obj: $f ( "Apache2::Filter object" )
162 ret: $r ( "Apache2::RequestRec object" )
163 since: 2.0.00
164
165 If a sub-request adds filters, then that sub-request object is associ‐
166 ated with the filter.
167
168 "remove"
169
170 Remove the current filter from the filter chain (for the current
171 request or connection).
172
173 $f->remove;
174
175 obj: $f ( "Apache2::Filter object" )
176 ret: no return value
177 since: 2.0.00
178
179 Notice that you should either complete the current filter invocation
180 normally (by calling "get_brigade" or "pass_brigade" depending on the
181 filter kind) or if nothing was done, return "Apache2::Const::DECLINED"
182 and mod_perl will take care of passing the current bucket brigade
183 through unmodified to the next filter in chain.
184
185 Note: calling remove() on the very top connection filter doesn't affect
186 the filter chain due to a bug in Apache 2.0 (which may be fixed in
187 2.1). So don't use it with connection filters, till it gets fixed in
188 Apache and then make sure to require the minimum Apache version if you
189 rely on.
190
191 Remember that if the connection is "$c->keepalive" ) and the connection
192 filter is removed, it won't be added until the connection is closed.
193 Which may happen after many HTTP requests. You may want to keep the
194 filter in place and pass the data through unmodified, by returning
195 "Apache2::Const::DECLINED". If you need to reset the whole or parts of
196 the filter context between requests, use the technique based on
197 "$c->keepalives" counting.
198
199 This method works for native Apache (non-mod_perl) filters too.
200
202 The following methods can be called from any filter, directly manipu‐
203 lating bucket brigades:
204
205 "fflush"
206
207 Flush a bucket brigade down the filter stack.
208
209 $rc = $f->fflush($bb);
210
211 obj: $f ( "Apache2::Filter object" )
212 The current filter
213
214 arg1: $bb ( "Apache2::Brigade object" )
215 The brigade to flush
216
217 ret: $rc ( "APR::Const status constant" )
218 Refer to the "pass_brigade()" entry.
219
220 excpt: "APR::Error"
221 Exceptions are thrown only when this function is called in the VOID
222 context. Refer to the "get_brigade()" entry for details.
223
224 since: 2.0.00
225
226 "fflush" is a shortcut method. So instead of doing:
227
228 my $b = APR::Bucket::flush_create($f->c->bucket_alloc);
229 $bb->insert_tail($b);
230 $f->pass_brigade($bb);
231
232 one can just write:
233
234 $f->fflush($bb);
235
236 "get_brigade"
237
238 This is a method to use in bucket brigade input filters. It acquires a
239 bucket brigade from the upstream input filter.
240
241 $rc = $next_f->get_brigade($bb, $mode, $block, $readbytes);
242 $rc = $next_f->get_brigade($bb, $mode, $block);
243 $rc = $next_f->get_brigade($bb, $mode)
244 $rc = $next_f->get_brigade($bb);
245
246 obj: $next_f ( "Apache2::Filter object" )
247 The next filter in the filter chain.
248
249 Inside filter handlers it's usually "$f->next". Inside protocol
250 handlers: "$c->input_filters".
251
252 arg1: $bb ( "APR::Brigade object" )
253 The original bucket brigade passed to "get_brigade()", which must
254 be empty.
255
256 Inside input filter handlers it's usually the second argument to
257 the filter handler.
258
259 Otherwise it should be created:
260
261 my $bb = APR::Brigade->new($c->pool, $c->bucket_alloc);
262
263 On return it gets populated with the next bucket brigade. That
264 brigade may contain nothing if there was no more data to read. The
265 return status tells the outcome.
266
267 opt arg2: $mode ( "Apache2::Const :input_mode constant" )
268 The filter mode in which the data should be read.
269
270 If inside the filter handler, you should normally pass the same
271 mode that was passed to the filter handler (the third argument).
272
273 At the end of this section the available modes are presented.
274
275 If the argument $mode is not passed, "Apache2::Const::MODE_READ‐
276 BYTES" is used as a default value.
277
278 opt arg3: $block ( "APR::Const :read_type constant" )
279 You may ask the reading operation to be blocking:
280 "APR::Const::BLOCK_READ", or nonblocking: "APR::Const::NON‐
281 BLOCK_READ".
282
283 If inside the filter handler, you should normally pass the same
284 blocking mode argument that was passed to the filter handler (the
285 forth argument).
286
287 If the argument $block is not passed, "APR::Const::BLOCK_READ" is
288 used as a default value.
289
290 opt arg4: $readbytes ( integer )
291 How many bytes to read from the next filter.
292
293 If inside the filter handler, you may want the same number of
294 bytes, as the upstream filter, i.e. the argument that was passed to
295 the filter handler (the fifth argument).
296
297 If the argument $block is not passed, 8192 is used as a default
298 value.
299
300 ret: $rc ( "APR::Const status constant" )
301 On success, "APR::Const::SUCCESS" is returned and $bb is populated
302 (see the $bb entry).
303
304 In case of a failure -- a failure code is returned, in which case
305 normally it should be returned to the caller.
306
307 If the bottom-most filter doesn't read from the network, then
308 "Apache2::NOBODY_READ" is returned (META: need to add this con‐
309 stant).
310
311 Inside protocol handlers the return code can also be
312 "APR::Const::EOF", which is success as well.
313
314 excpt: "APR::Error"
315 You don't have to ask for the return value. If this function is
316 called in the VOID context, e.g.:
317
318 $f->next->get_brigade($bb, $mode, $block, $readbytes);
319
320 mod_perl will do the error checking on your behalf, and if the
321 return code is not "APR::Const::SUCCESS", an "APR::Error exception"
322 will be thrown. The only time you want to do the error checking
323 yourself, is when return codes besides "APR::Const::SUCCESS" are
324 considered as successful and you want to manage them by yourself.
325
326 since: 2.0.00
327
328 Available input filter modes (the optional second argument $mode) are:
329
330 * "Apache2::Const::MODE_READBYTES"
331 The filter should return at most readbytes data
332
333 * "Apache2::Const::MODE_GETLINE"
334 The filter should return at most one line of CRLF data. (If a
335 potential line is too long or no CRLF is found, the filter may
336 return partial data).
337
338 * "Apache2::Const::MODE_EATCRLF"
339 The filter should implicitly eat any CRLF pairs that it sees.
340
341 * "Apache2::Const::MODE_SPECULATIVE"
342 The filter read should be treated as speculative and any returned
343 data should be stored for later retrieval in another mode.
344
345 * "Apache2::Const::MODE_EXHAUSTIVE"
346 The filter read should be exhaustive and read until it can not read
347 any more. Use this mode with extreme caution.
348
349 * "Apache2::Const::MODE_INIT"
350 The filter should initialize the connection if needed, NNTP or FTP
351 over SSL for example.
352
353 Either compile all these constants with:
354
355 use Apache2::Const -compile => qw(:input_mode);
356
357 But it's a bit more efficient to compile only those constants that you
358 need.
359
360 Example:
361
362 Here is a fragment of a filter handler, that receives a bucket brigade
363 from the upstream filter:
364
365 use Apache2::Filter ();
366 use APR::Const -compile => qw(SUCCESS);
367 use Apache2::Const -compile => qw(OK);
368 sub filter {
369 my ($f, $bb, $mode, $block, $readbytes) = @_;
370
371 my $rc = $f->next->get_brigade($bb, $mode, $block, $readbytes);
372 return $rc unless $rc == APR::Const::SUCCESS;
373
374 # ... process $bb
375
376 return Apache2::Const::OK;
377 }
378
379 Usually arguments $mode, $block, $readbytes are the same as passed to
380 the filter itself.
381
382 You can see that in case of a failure, the handler returns immediately
383 with that failure code, which gets propagated to the downstream filter.
384
385 If you decide not check the return code, you can write it as:
386
387 sub filter {
388 my ($f, $bb, $mode, $block, $readbytes) = @_;
389
390 $f->next->get_brigade($bb, $mode, $block, $readbytes);
391
392 # ... process $bb
393
394 return Apache2::Const::OK;
395 }
396
397 and the error checking will be done on your behalf.
398
399 You will find many more examples in "the filter han‐
400 dlers⎪docs::2.0::user::handlers::filters" and "the protocol han‐
401 dlers⎪docs::2.0::user::handlers::protocols" tutorials.
402
403 "pass_brigade"
404
405 This is a method to use in bucket brigade output filters. It passes
406 the current bucket brigade to the downstream output filter.
407
408 $rc = $next_f->pass_brigade($bb);
409
410 obj: $next_f ( "Apache2::Filter object" )
411 The next filter in the filter chain.
412
413 Inside output filter handlers it's usually "$f->next". Inside pro‐
414 tocol handlers: "$c->output_filters".
415
416 arg1: $bb ( "APR::Brigade object" )
417 The bucket brigade to pass.
418
419 Inside output filter handlers it's usually the second argument to
420 the filter handler (after potential manipulations).
421
422 ret: $rc ( "APR::Const status constant" )
423 On success, "APR::Const::SUCCESS" is returned.
424
425 In case of a failure -- a failure code is returned, in which case
426 normally it should be returned to the caller.
427
428 If the bottom-most filter doesn't write to the network, then
429 "Apache2::NOBODY_WROTE" is returned (META: need to add this con‐
430 stant).
431
432 Also refer to the "get_brigade()" entry to see how to avoid check‐
433 ing the errors explicitly.
434
435 excpt: "APR::Error"
436 Exceptions are thrown only when this function is called in the VOID
437 context. Refer to the "get_brigade()" entry for details.
438
439 since: 2.0.00
440
441 The caller relinquishes ownership of the brigade (i.e. it may get
442 destroyed/overwritten/etc. by the callee).
443
444 Example:
445
446 Here is a fragment of a filter handler, that passes a bucket brigade to
447 the downstream filter (after some potential processing of the buckets
448 in the bucket brigade):
449
450 use Apache2::Filter ();
451 use APR::Const -compile => qw(SUCCESS);
452 use Apache2::Const -compile => qw(OK);
453 sub filter {
454 my ($f, $bb) = @_;
455
456 # ... process $bb
457
458 my $rc = $f->next->pass_brigade($bb);
459 return $rc unless $rc == APR::Const::SUCCESS;
460
461 return Apache2::Const::OK;
462 }
463
465 The following methods can be called from any filter, which uses the
466 simplified streaming functionality:
467
468 "print"
469
470 Send the contents of $buffer to the next filter in chain (via internal
471 buffer).
472
473 $sent = $f->print($buffer);
474
475 obj: $f ( "Apache2::Filter object" )
476 arg1: $buffer ( string )
477 The data to send.
478
479 ret: $sent ( integer )
480 How many characters were sent. There is no need to check, since all
481 should go through and if something goes work an exception will be
482 thrown.
483
484 excpt: "APR::Error"
485 since: 2.0.00
486
487 This method should be used only in streaming filters.
488
489 "read"
490
491 Read data from the filter
492
493 $read = $f->read($buffer, $wanted);
494
495 obj: $f ( "Apache2::Filter object" )
496 arg1: $buffer ( SCALAR )
497 The buffer to fill. All previous data will be lost.
498
499 opt arg2: $wanted ( integer )
500 How many bytes to attempt to read.
501
502 If this optional argument is not specified -- the default 8192 will
503 be used.
504
505 ret: $read ( integer )
506 How many bytes were actually read.
507
508 $buffer gets populated with the string that is read. It will con‐
509 tain an empty string if there was nothing to read.
510
511 excpt: "APR::Error"
512 since: 2.0.00
513
514 Reads at most $wanted characters into $buffer. The returned value $read
515 tells exactly how many were read, making it easy to use it in a while
516 loop:
517
518 while ($filter->read(my $buffer, $wanted)) {
519 # transform $buffer here
520 $filter->print($buffer);
521 }
522
523 This is a streaming filter method, which acquires a single bucket
524 brigade behind the scenes and reads data from all its buckets. There‐
525 fore it can only read from one bucket brigade per filter invocation.
526
527 If the EOS bucket is read, the "seen_eos" method will return a true
528 value.
529
530 "seen_eos"
531
532 This methods returns a true value when the EOS bucket is seen by the
533 "read" method.
534
535 $ok = $f->seen_eos;
536
537 obj: $f ( "Apache2::Filter object" )
538 The filter to remove
539
540 ret: $ok ( boolean )
541 a true value if EOS has been seen, otherwise a false value
542
543 since: 2.0.00
544
545 This method only works in streaming filters which exhaustively
546 "$f->read" all the incoming data in a while loop, like so:
547
548 while ($f->read(my $buffer, $wanted)) {
549 # do something with $buffer
550 }
551 if ($f->seen_eos) {
552 # do something
553 }
554
555 The technique in this example is useful when a streaming filter wants
556 to append something to the very end of data, or do something at the end
557 of the last filter invocation. After the EOS bucket is read, the filter
558 should expect not to be invoked again.
559
560 If an input streaming filter doesn't consume all data in the bucket
561 brigade (or even in several bucket brigades), it has to generate the
562 EOS event by itself. So when the filter is done it has to set the EOS
563 flag:
564
565 $f->seen_eos(1);
566
567 when the filter handler returns, internally mod_perl will take care of
568 creating and sending the EOS bucket to the upstream input filter.
569
570 A similar logic may apply for output filters.
571
572 In most other cases you shouldn't set this flag. When this flag is
573 prematurely set (before the real EOS bucket has arrived) in the current
574 filter invocation, instead of invoking the filter again, mod_perl will
575 create and send the EOS bucket to the next filter, ignoring any other
576 bucket brigades that may have left to consume. As mentioned earlier
577 this special behavior is useful in writing special tests that test
578 abnormal situations.
579
581 Other methods which affect filters, but called on non-"Apache2::Filter"
582 objects:
583
584 "add_input_filter"
585
586 Add &callback filter handler to input request filter chain.
587
588 $r->add_input_filter(\&callback);
589
590 Add &callback filter handler to input connection filter chain.
591
592 $c->add_input_filter(\&callback);
593
594 obj: $c ( "Apache2::Connection object" ) or $r ( "Apache2::RequestRec
595 object" )
596 arg1: &callback (CODE ref)
597 ret: no return value
598 since: 2.0.00
599
600 [META: It seems that you can't add a filter when another filter is
601 called. I've tried to add an output connection filter from the input
602 connection filter when it was called for the first time. It didn't have
603 any affect for the first request (over keepalive connection). The only
604 way I succeeded to do that is from that input connection filter's fil‐
605 ter_init handler. In fact it does work if there is any filter addi‐
606 tional filter of the same kind configured from httpd.conf or via fil‐
607 ter_init. It looks like there is a bug in httpd, where it doesn't pre‐
608 pare the chain of 3rd party filter if none were inserted before the
609 first filter was called.]
610
611 "add_output_filter"
612
613 Add &callback filter handler to output request filter chain.
614
615 $r->add_output_filter(\&callback);
616
617 Add &callback filter handler to output connection filter chain.
618
619 $c->add_output_filter(\&callback);
620
621 obj: $c ( "Apache2::Connection object" ) or $r ( "Apache2::RequestRec
622 object" )
623 arg1: &callback (CODE ref)
624 ret: no return value
625 since: 2.0.00
626
628 Packages using filter attributes have to subclass "Apache2::Filter":
629
630 package MyApache2::FilterCool;
631 use base qw(Apache2::Filter);
632
633 Attributes are parsed during the code compilation, by the function
634 "MODIFY_CODE_ATTRIBUTES", inherited from the "Apache2::Filter" package.
635
636 "FilterRequestHandler"
637
638 The "FilterRequestHandler" attribute tells mod_perl to insert the fil‐
639 ter into an HTTP request filter chain.
640
641 For example, to configure an output request filter handler, use the
642 "FilterRequestHandler" attribute in the handler subroutine's declara‐
643 tion:
644
645 package MyApache2::FilterOutputReq;
646 sub handler : FilterRequestHandler { ... }
647
648 and add the configuration entry:
649
650 PerlOutputFilterHandler MyApache2::FilterOutputReq
651
652 This is the default mode. So if you are writing an HTTP request filter,
653 you don't have to specify this attribute.
654
655 The section HTTP Request vs. Connection Filters delves into more
656 details.
657
658 "FilterConnectionHandler"
659
660 The "FilterConnectionHandler" attribute tells mod_perl to insert this
661 filter into a connection filter chain.
662
663 For example, to configure an output connection filter handler, use the
664 "FilterConnectionHandler" attribute in the handler subroutine's decla‐
665 ration:
666
667 package MyApache2::FilterOutputCon;
668 sub handler : FilterConnectionHandler { ... }
669
670 and add the configuration entry:
671
672 PerlOutputFilterHandler MyApache2::FilterOutputCon
673
674 The section HTTP Request vs. Connection Filters delves into more
675 details.
676
677 "FilterInitHandler"
678
679 The attribute "FilterInitHandler" marks the function suitable to be
680 used as a filter initialization callback, which is called immediately
681 after a filter is inserted to the filter chain and before it's actually
682 called.
683
684 sub init : FilterInitHandler {
685 my $f = shift;
686 #...
687 return Apache2::Const::OK;
688 }
689
690 In order to hook this filter callback, the real filter has to assign
691 this callback using the "FilterHasInitHandler" which accepts a refer‐
692 ence to the callback function.
693
694 For further discussion and examples refer to the Filter Initialization
695 Phase tutorial section.
696
697 "FilterHasInitHandler"
698
699 If a filter wants to run an initialization callback it can register
700 such using the "FilterHasInitHandler" attribute. Similar to "push_han‐
701 dlers" the callback reference is expected, rather than a callback name.
702 The used callback function has to have the "FilterInitHandler"
703 attribute. For example:
704
705 package MyApache2::FilterBar;
706 use base qw(Apache2::Filter);
707 sub init : FilterInitHandler { ... }
708 sub filter : FilterRequestHandler FilterHasInitHandler(\&init) {
709 my ($f, $bb) = @_;
710 # ...
711 return Apache2::Const::OK;
712 }
713
714 For further discussion and examples refer to the Filter Initialization
715 Phase tutorial section.
716
718 mod_perl 2.0 filters configuration is explained in the filter handlers
719 tutorial.
720
721 "PerlInputFilterHandler"
722
723 See "PerlInputFilterHandler".
724
725 "PerlOutputFilterHandler"
726
727 See "PerlOutputFilterHandler".
728
729 "PerlSetInputFilter"
730
731 See "PerlSetInputFilter".
732
733 "PerlSetOutputFilter"
734
735 See "PerlSetInputFilter".
736
738 "Apache2::Filter" also implements a tied interface, so you can work
739 with the $f object as a hash reference.
740
741 The TIE interface is mostly unimplemented and might be implemented post
742 2.0 release.
743
744 "TIEHANDLE"
745
746 $ret = TIEHANDLE($stashsv, $sv);
747
748 obj: $stashsv ( SCALAR )
749 arg1: $sv ( SCALAR )
750 ret: $ret ( SCALAR )
751 since: subject to change
752
753 "PRINT"
754
755 $ret = PRINT(...);
756
757 obj: "..." (XXX)
758 ret: $ret ( integer )
759 since: subject to change
760
762 mod_perl 2.0 documentation.
763
765 mod_perl 2.0 and its core modules are copyrighted under The Apache
766 Software License, Version 2.0.
767
769 The mod_perl development team and numerous contributors.
770
771
772
773perl v5.8.8 2006-11-19 docs::api::Apache2::Filter(3)