This file is indexed.

/usr/share/doc/libapache2-mod-perl2-doc/docs/2.0/api/Apache2/Filter.html is in libapache2-mod-perl2-doc 2.0.8+httpd24-r1449661-6ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:root@localhost" />
</head>

<body style="background-color: white">



<ul id="index">
  <li><a href="#NAME">NAME</a></li>
  <li><a href="#Synopsis">Synopsis</a></li>
  <li><a href="#Description">Description</a></li>
  <li><a href="#Common-Filter-API">Common Filter API</a>
    <ul>
      <li><a href="#c">c</a></li>
      <li><a href="#ctx">ctx</a></li>
      <li><a href="#frec">frec</a></li>
      <li><a href="#next">next</a></li>
      <li><a href="#r">r</a></li>
      <li><a href="#remove">remove</a></li>
    </ul>
  </li>
  <li><a href="#Bucket-Brigade-Filter-API">Bucket Brigade Filter API</a>
    <ul>
      <li><a href="#fflush">fflush</a></li>
      <li><a href="#get_brigade">get_brigade</a></li>
      <li><a href="#pass_brigade">pass_brigade</a></li>
    </ul>
  </li>
  <li><a href="#Streaming-Filter-API">Streaming Filter API</a>
    <ul>
      <li><a href="#print">print</a></li>
      <li><a href="#read">read</a></li>
      <li><a href="#seen_eos">seen_eos</a></li>
    </ul>
  </li>
  <li><a href="#Other-Filter-related-API">Other Filter-related API</a>
    <ul>
      <li><a href="#add_input_filter">add_input_filter</a></li>
      <li><a href="#add_output_filter">add_output_filter</a></li>
    </ul>
  </li>
  <li><a href="#Filter-Handler-Attributes">Filter Handler Attributes</a>
    <ul>
      <li><a href="#FilterRequestHandler">FilterRequestHandler</a></li>
      <li><a href="#FilterConnectionHandler">FilterConnectionHandler</a></li>
      <li><a href="#FilterInitHandler">FilterInitHandler</a></li>
      <li><a href="#FilterHasInitHandler">FilterHasInitHandler</a></li>
    </ul>
  </li>
  <li><a href="#Configuration">Configuration</a>
    <ul>
      <li><a href="#PerlInputFilterHandler">PerlInputFilterHandler</a></li>
      <li><a href="#PerlOutputFilterHandler">PerlOutputFilterHandler</a></li>
      <li><a href="#PerlSetInputFilter">PerlSetInputFilter</a></li>
      <li><a href="#PerlSetOutputFilter">PerlSetOutputFilter</a></li>
    </ul>
  </li>
  <li><a href="#TIE-Interface">TIE Interface</a>
    <ul>
      <li><a href="#TIEHANDLE">TIEHANDLE</a></li>
      <li><a href="#PRINT">PRINT</a></li>
    </ul>
  </li>
  <li><a href="#See-Also">See Also</a></li>
  <li><a href="#Copyright">Copyright</a></li>
  <li><a href="#Authors">Authors</a></li>
</ul>

<h1 id="NAME">NAME</h1>

<p>Apache2::Filter - Perl API for Apache 2.0 Filtering</p>

<h1 id="Synopsis">Synopsis</h1>

<pre><code>  use Apache2::Filter ();
  
  # filter attributes
  my $c = $f-&gt;c;
  my $r = $f-&gt;r;
  my $frec = $f-&gt;frec();
  my $next_f = $f-&gt;next;
  
  my $ctx = $f-&gt;ctx;
  $f-&gt;ctx($ctx);
  
  # bucket brigade filtering API
  $rc = $f-&gt;next-&gt;get_brigade($bb, $mode, $block, $readbytes);
  $rc = $f-&gt;next-&gt;pass_brigade($bb);
  $rc = $f-&gt;fflush($bb);
  
  # streaming filtering API
  while ($filter-&gt;read(my $buffer, $wanted)) {
      # transform $buffer here
      $filter-&gt;print($buffer);
  }
  if ($f-&gt;seen_eos) {
      $filter-&gt;print(&quot;filter signature&quot;);
  }
  
  # filter manipulations
  $r-&gt;add_input_filter(\&amp;callback);
  $c-&gt;add_input_filter(\&amp;callback);
  $r-&gt;add_output_filter(\&amp;callback);
  $c-&gt;add_output_filter(\&amp;callback);
  $f-&gt;remove;</code></pre>

<h1 id="Description">Description</h1>

<p><code>Apache2::Filter</code> provides Perl API for Apache 2.0 filtering framework.</p>

<p>Make sure to read <code>the Filtering tutorial|docs::2.0::user::handlers::filters</code>.</p>

<h1 id="Common-Filter-API">Common Filter API</h1>

<p>The following methods can be called from any filter handler:</p>

<h2 id="c"><code>c</code></h2>

<p>Get the current connection object from a connection or a request filter:</p>

<pre><code>  $c = $f-&gt;c;</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

</dd>
<dt id="ret:-c-Apache2::Connection-object">ret: <code>$c</code> ( <code><a>Apache2::Connection object</a></code> )</dt>
<dd>

</dd>
<dt id="since:-2.0.00">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<h2 id="ctx"><code>ctx</code></h2>

<p>Get/set the filter context data.</p>

<pre><code>  $ctx = $f-&gt;ctx;
         $f-&gt;ctx($ctx);</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object1">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

</dd>
<dt id="opt-arg2:-ctx-SCALAR">opt arg2: <code>$ctx</code> ( SCALAR )</dt>
<dd>

<p>next context</p>

</dd>
<dt id="ret:-ctx-SCALAR">ret: <code>$ctx</code> ( SCALAR )</dt>
<dd>

<p>current context</p>

</dd>
<dt id="since:-2.0.001">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>A filter context is created before the filter is called for the first time and it&#39;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 <code>undef</code> value.</p>

<p>The <code>ctx</code> method accepts a single SCALAR argument. Therefore if you want to store any other perl datastructure you should use a reference to it.</p>

<p>For example you can store a hash reference:</p>

<pre><code>  $f-&gt;ctx({ foo =&gt; &#39;bar&#39; });</code></pre>

<p>and then access it:</p>

<pre><code>  $foo = $f-&gt;ctx-&gt;{foo};</code></pre>

<p>if you access the context more than once it&#39;s more efficient to copy it&#39;s value before using it:</p>

<pre><code>  my $ctx = $f-&gt;ctx;
  $foo = $ctx-&gt;{foo};</code></pre>

<p>to avoid redundant method calls. As of this writing <code>$ctx</code> is not a tied variable, so if you modify it need to store it at the end:</p>

<pre><code>  $f-&gt;ctx($ctx);</code></pre>

<p>META: later we might make it a TIEd-variable interface, so it&#39;ll be stored automatically.</p>

<p>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&#39;s life:</p>

<pre><code>  unless ($f-&gt;ctx) {
      do_something_once();
      $f-&gt;ctx(1);
  }</code></pre>

<h2 id="frec"><code>frec</code></h2>

<p>Get/set the <code><a>Apache2::FilterRec</a></code> (filter record) object.</p>

<pre><code>  $frec = $f-&gt;frec();</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object2">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

</dd>
<dt id="ret:-frec-Apache2::FilterRec-object">ret: <code>$frec</code> ( <code><a>Apache2::FilterRec object</a></code> )</dt>
<dd>

</dd>
<dt id="since:-2.0.002">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>For example you can call <code><a>$frec-&gt;name</a></code> to get filter&#39;s name.</p>

<h2 id="next"><code>next</code></h2>

<p>Return the <code>Apache2::Filter</code> object of the next filter in chain.</p>

<pre><code>  $next_f = $f-&gt;next;</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object3">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

<p>The current filter object</p>

</dd>
<dt id="ret:-next_f-Apache2::Filter-object">ret: <code>$next_f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

<p>The next filter object in chain</p>

</dd>
<dt id="since:-2.0.003">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>Since Apache inserts several core filters at the end of each chain, normally this method always returns an object. However if it&#39;s not a mod_perl filter handler, you can call only the following methods on it: <code><a href="#C_get_brigade_">get_brigade</a></code>, <code><a href="#C_pass_brigade_">pass_brigade</a></code>, <code><a href="#C_c_">c</a></code>, <code><a href="#C_r_">r</a></code>, <code><a href="#C_frec_">frec</a></code> and <code><a href="#C_next_">next</a></code>. If you call other methods the behavior is undefined.</p>

<p>The next filter can be a mod_perl one or not, it&#39;s easy to tell which one is that by calling <code><a>$f-&gt;frec-&gt;name</a></code>.</p>

<h2 id="r"><code>r</code></h2>

<p>Inside an HTTP request filter retrieve the current request object:</p>

<pre><code>  $r = $f-&gt;r;</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object4">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

</dd>
<dt id="ret:-r-Apache2::RequestRec-object">ret: <code>$r</code> ( <code><a>Apache2::RequestRec object</a></code> )</dt>
<dd>

</dd>
<dt id="since:-2.0.004">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>If a sub-request adds filters, then that sub-request object is associated with the filter.</p>

<h2 id="remove"><code>remove</code></h2>

<p>Remove the current filter from the filter chain (for the current request or connection).</p>

<pre><code>  $f-&gt;remove;</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object5">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

</dd>
<dt id="ret:-no-return-value">ret: no return value</dt>
<dd>

</dd>
<dt id="since:-2.0.005">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>Notice that you should either complete the current filter invocation normally (by calling <code><a href="#C_get_brigade_">get_brigade</a></code> or <code><a href="#C_pass_brigade_">pass_brigade</a></code> depending on the filter kind) or if nothing was done, return <code>Apache2::Const::DECLINED</code> and mod_perl will take care of passing the current bucket brigade through unmodified to the next filter in chain.</p>

<p>Note: calling remove() on the very top connection filter doesn&#39;t affect the filter chain due to a bug in Apache 2.0 (which may be fixed in 2.1). So don&#39;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.</p>

<p>Remember that if the connection is <code><a>$c-&gt;keepalive</a></code> ) and the connection filter is removed, it won&#39;t be added until the connection is closed. Which may happen after many HTTP requests. You may want to keep the filter in place and pass the data through unmodified, by returning <code>Apache2::Const::DECLINED</code>. If you need to reset the whole or parts of the filter context between requests, use the <a>technique based on <code>$c-&gt;keepalives</code> counting</a>.</p>

<p>This method works for native Apache (non-mod_perl) filters too.</p>

<h1 id="Bucket-Brigade-Filter-API">Bucket Brigade Filter API</h1>

<p>The following methods can be called from any filter, directly manipulating bucket brigades:</p>

<h2 id="fflush"><code>fflush</code></h2>

<p>Flush a bucket brigade down the filter stack.</p>

<pre><code>  $rc = $f-&gt;fflush($bb);</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object6">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

<p>The current filter</p>

</dd>
<dt id="arg1:-bb-Apache2::Brigade-object">arg1: <code>$bb</code> ( <code><a>Apache2::Brigade object</a></code> )</dt>
<dd>

<p>The brigade to flush</p>

</dd>
<dt id="ret:-rc-APR::Const-status-constant">ret: <code>$rc</code> ( <code><a>APR::Const status constant</a></code> )</dt>
<dd>

<p>Refer to the <code><a href="#C_pass_brigade_">pass_brigade()</a></code> entry.</p>

</dd>
<dt id="excpt:-APR::Error">excpt: <code><a>APR::Error</a></code></dt>
<dd>

<p>Exceptions are thrown only when this function is called in the VOID context. Refer to the <code><a href="#C_get_brigade_">get_brigade()</a></code> entry for details.</p>

</dd>
<dt id="since:-2.0.006">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p><code>fflush</code> is a shortcut method. So instead of doing:</p>

<pre><code>  my $b = APR::Bucket::flush_create($f-&gt;c-&gt;bucket_alloc);
  $bb-&gt;insert_tail($b);
  $f-&gt;pass_brigade($bb);</code></pre>

<p>one can just write:</p>

<pre><code>  $f-&gt;fflush($bb);</code></pre>

<h2 id="get_brigade"><code>get_brigade</code></h2>

<p>This is a method to use in bucket brigade input filters. It acquires a bucket brigade from the upstream input filter.</p>

<pre><code>  $rc = $next_f-&gt;get_brigade($bb, $mode, $block, $readbytes);
  $rc = $next_f-&gt;get_brigade($bb, $mode, $block);
  $rc = $next_f-&gt;get_brigade($bb, $mode)
  $rc = $next_f-&gt;get_brigade($bb);</code></pre>

<dl>

<dt id="obj:-next_f-Apache2::Filter-object">obj: <code>$next_f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

<p>The next filter in the filter chain.</p>

<p>Inside <a>filter handlers</a> it&#39;s usually <code><a href="#C_next_">$f-&gt;next</a></code>. Inside <a>protocol handlers</a>: <code><a>$c-&gt;input_filters</a></code>.</p>

</dd>
<dt id="arg1:-bb-APR::Brigade-object">arg1: <code>$bb</code> ( <code><a>APR::Brigade object</a></code> )</dt>
<dd>

<p>The original bucket brigade passed to <code>get_brigade()</code>, which must be empty.</p>

<p>Inside <a>input filter handlers</a> it&#39;s usually the second argument to the filter handler.</p>

<p>Otherwise it should be created:</p>

<pre><code>  my $bb = APR::Brigade-&gt;new($c-&gt;pool, $c-&gt;bucket_alloc);</code></pre>

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

</dd>
<dt id="opt-arg2:-mode-Apache2::Const-:input_mode-constant">opt arg2: <code>$mode</code> ( <code><a>Apache2::Const :input_mode constant</a></code> )</dt>
<dd>

<p>The filter mode in which the data should be read.</p>

<p>If inside the filter handler, you should normally pass the same mode that was passed to the filter handler (the third argument).</p>

<p>At the end of this section the available modes are presented.</p>

<p>If the argument <code>$mode</code> is not passed, <code><a>Apache2::Const::MODE_READBYTES</a></code> is used as a default value.</p>

</dd>
<dt id="opt-arg3:-block-APR::Const-:read_type-constant">opt arg3: <code>$block</code> ( <code><a>APR::Const :read_type constant</a></code> )</dt>
<dd>

<p>You may ask the reading operation to be blocking: <code><a>APR::Const::BLOCK_READ</a></code>, or nonblocking: <code><a>APR::Const::NONBLOCK_READ</a></code>.</p>

<p>If inside the filter handler, you should normally pass the same blocking mode argument that was passed to the filter handler (the forth argument).</p>

<p>If the argument <code>$block</code> is not passed, <code><a>APR::Const::BLOCK_READ</a></code> is used as a default value.</p>

</dd>
<dt id="opt-arg4:-readbytes-integer">opt arg4: <code>$readbytes</code> ( integer )</dt>
<dd>

<p>How many bytes to read from the next filter.</p>

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

<p>If the argument <code>$block</code> is not passed, 8192 is used as a default value.</p>

</dd>
<dt id="ret:-rc-APR::Const-status-constant1">ret: <code>$rc</code> ( <code><a>APR::Const status constant</a></code> )</dt>
<dd>

<p>On success, <code><a>APR::Const::SUCCESS</a></code> is returned and <code>$bb</code> is populated (see the <code>$bb</code> entry).</p>

<p>In case of a failure -- a failure code is returned, in which case normally it should be returned to the caller.</p>

<p>If the bottom-most filter doesn&#39;t read from the network, then <code>Apache2::NOBODY_READ</code> is returned (META: need to add this constant).</p>

<p>Inside <a>protocol handlers</a> the return code can also be <code>APR::Const::EOF</code>, which is success as well.</p>

</dd>
<dt id="excpt:-APR::Error1">excpt: <code><a>APR::Error</a></code></dt>
<dd>

<p>You don&#39;t have to ask for the return value. If this function is called in the VOID context, e.g.:</p>

<pre><code>  $f-&gt;next-&gt;get_brigade($bb, $mode, $block, $readbytes);</code></pre>

<p>mod_perl will do the error checking on your behalf, and if the return code is not <code><a>APR::Const::SUCCESS</a></code>, an <code><a>APR::Error exception</a></code> will be thrown. The only time you want to do the error checking yourself, is when return codes besides <code><a>APR::Const::SUCCESS</a></code> are considered as successful and you want to manage them by yourself.</p>

</dd>
<dt id="since:-2.0.007">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>Available input filter modes (the optional second argument <code>$mode</code>) are:</p>

<ul>

<li><p><code><a>Apache2::Const::MODE_READBYTES</a></code></p>

<p>The filter should return at most readbytes data</p>

</li>
<li><p><code><a>Apache2::Const::MODE_GETLINE</a></code></p>

<p>The filter should return at most one line of CRLF data. (If a potential line is too long or no CRLF is found, the filter may return partial data).</p>

</li>
<li><p><code><a>Apache2::Const::MODE_EATCRLF</a></code></p>

<p>The filter should implicitly eat any CRLF pairs that it sees.</p>

</li>
<li><p><code><a>Apache2::Const::MODE_SPECULATIVE</a></code></p>

<p>The filter read should be treated as speculative and any returned data should be stored for later retrieval in another mode.</p>

</li>
<li><p><code><a>Apache2::Const::MODE_EXHAUSTIVE</a></code></p>

<p>The filter read should be exhaustive and read until it can not read any more. Use this mode with extreme caution.</p>

</li>
<li><p><code><a>Apache2::Const::MODE_INIT</a></code></p>

<p>The filter should initialize the connection if needed, NNTP or FTP over SSL for example.</p>

</li>
</ul>

<p>Either compile all these constants with:</p>

<pre><code>  use Apache2::Const -compile =&gt; qw(:input_mode);</code></pre>

<p>But it&#39;s a bit more efficient to compile only those constants that you need.</p>

<p>Example:</p>

<p>Here is a fragment of a filter handler, that receives a bucket brigade from the upstream filter:</p>

<pre><code>  use Apache2::Filter ();
  use APR::Const    -compile =&gt; qw(SUCCESS);
  use Apache2::Const -compile =&gt; qw(OK);
  sub filter {
      my ($f, $bb, $mode, $block, $readbytes) = @_;
      
      my $rc = $f-&gt;next-&gt;get_brigade($bb, $mode, $block, $readbytes);
      return $rc unless $rc == APR::Const::SUCCESS;
      
      # ... process $bb
      
      return Apache2::Const::OK;
  }</code></pre>

<p>Usually arguments <code>$mode</code>, <code>$block</code>, <code>$readbytes</code> are the same as passed to the filter itself.</p>

<p>You can see that in case of a failure, the handler returns immediately with that failure code, which gets propagated to the downstream filter.</p>

<p>If you decide not check the return code, you can write it as:</p>

<pre><code>  sub filter {
      my ($f, $bb, $mode, $block, $readbytes) = @_;
      
      $f-&gt;next-&gt;get_brigade($bb, $mode, $block, $readbytes);
      
      # ... process $bb
      
      return Apache2::Const::OK;
  }</code></pre>

<p>and the error checking will be done on your behalf.</p>

<p>You will find many more examples in <code>the filter handlers|docs::2.0::user::handlers::filters</code> and <code>the protocol handlers|docs::2.0::user::handlers::protocols</code> tutorials.</p>

<h2 id="pass_brigade"><code>pass_brigade</code></h2>

<p>This is a method to use in bucket brigade output filters. It passes the current bucket brigade to the downstream output filter.</p>

<pre><code>  $rc = $next_f-&gt;pass_brigade($bb);</code></pre>

<dl>

<dt id="obj:-next_f-Apache2::Filter-object1">obj: <code>$next_f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

<p>The next filter in the filter chain.</p>

<p>Inside <a>output filter handlers</a> it&#39;s usually <code><a href="#C_next_">$f-&gt;next</a></code>. Inside <a>protocol handlers</a>: <code><a>$c-&gt;output_filters</a></code>.</p>

</dd>
<dt id="arg1:-bb-APR::Brigade-object1">arg1: <code>$bb</code> ( <code><a>APR::Brigade object</a></code> )</dt>
<dd>

<p>The bucket brigade to pass.</p>

<p>Inside <a>output filter handlers</a> it&#39;s usually the second argument to the filter handler (after potential manipulations).</p>

</dd>
<dt id="ret:-rc-APR::Const-status-constant2">ret: <code>$rc</code> ( <code><a>APR::Const status constant</a></code> )</dt>
<dd>

<p>On success, <code><a>APR::Const::SUCCESS</a></code> is returned.</p>

<p>In case of a failure -- a failure code is returned, in which case normally it should be returned to the caller.</p>

<p>If the bottom-most filter doesn&#39;t write to the network, then <code>Apache2::NOBODY_WROTE</code> is returned (META: need to add this constant).</p>

<p>Also refer to the <code><a href="#C_get_brigade_">get_brigade()</a></code> entry to see how to avoid checking the errors explicitly.</p>

</dd>
<dt id="excpt:-APR::Error2">excpt: <code><a>APR::Error</a></code></dt>
<dd>

<p>Exceptions are thrown only when this function is called in the VOID context. Refer to the <code><a href="#C_get_brigade_">get_brigade()</a></code> entry for details.</p>

</dd>
<dt id="since:-2.0.008">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>The caller relinquishes ownership of the brigade (i.e. it may get destroyed/overwritten/etc. by the callee).</p>

<p>Example:</p>

<p>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):</p>

<pre><code>  use Apache2::Filter ();
  use APR::Const    -compile =&gt; qw(SUCCESS);
  use Apache2::Const -compile =&gt; qw(OK);
  sub filter {
      my ($f, $bb) = @_;
  
      # ... process $bb
  
      my $rc = $f-&gt;next-&gt;pass_brigade($bb);
      return $rc unless $rc == APR::Const::SUCCESS;
  
      return Apache2::Const::OK;
  }</code></pre>

<h1 id="Streaming-Filter-API">Streaming Filter API</h1>

<p>The following methods can be called from any filter, which uses the simplified streaming functionality:</p>

<h2 id="print"><code>print</code></h2>

<p>Send the contents of <code>$buffer</code> to the next filter in chain (via internal buffer).</p>

<pre><code>  $sent = $f-&gt;print($buffer);</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object7">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

</dd>
<dt id="arg1:-buffer-string">arg1: <code>$buffer</code> ( string )</dt>
<dd>

<p>The data to send.</p>

</dd>
<dt id="ret:-sent-integer">ret: <code>$sent</code> ( integer )</dt>
<dd>

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

</dd>
<dt id="excpt:-APR::Error3">excpt: <code><a>APR::Error</a></code></dt>
<dd>

</dd>
<dt id="since:-2.0.009">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>This method should be used only in <a>streaming filters</a>.</p>

<h2 id="read"><code>read</code></h2>

<p>Read data from the filter</p>

<pre><code>  $read = $f-&gt;read($buffer, $wanted);</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object8">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

</dd>
<dt id="arg1:-buffer-SCALAR">arg1: <code>$buffer</code> ( SCALAR )</dt>
<dd>

<p>The buffer to fill. All previous data will be lost.</p>

</dd>
<dt id="opt-arg2:-wanted-integer">opt arg2: <code>$wanted</code> ( integer )</dt>
<dd>

<p>How many bytes to attempt to read.</p>

<p>If this optional argument is not specified -- the default 8192 will be used.</p>

</dd>
<dt id="ret:-read-integer">ret: <code>$read</code> ( integer )</dt>
<dd>

<p>How many bytes were actually read.</p>

<p><code>$buffer</code> gets populated with the string that is read. It will contain an empty string if there was nothing to read.</p>

</dd>
<dt id="excpt:-APR::Error4">excpt: <code><a>APR::Error</a></code></dt>
<dd>

</dd>
<dt id="since:-2.0.0010">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>Reads at most <code>$wanted</code> characters into <code>$buffer</code>. The returned value <code>$read</code> tells exactly how many were read, making it easy to use it in a while loop:</p>

<pre><code>  while ($filter-&gt;read(my $buffer, $wanted)) {
      # transform $buffer here
      $filter-&gt;print($buffer);
  }</code></pre>

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

<p>If the EOS bucket is read, the <code><a href="#C_seen_eos_">seen_eos</a></code> method will return a true value.</p>

<h2 id="seen_eos"><code>seen_eos</code></h2>

<p>This methods returns a true value when the EOS bucket is seen by the <code><a href="#C_read_">read</a></code> method.</p>

<pre><code>  $ok = $f-&gt;seen_eos;</code></pre>

<dl>

<dt id="obj:-f-Apache2::Filter-object9">obj: <code>$f</code> ( <code><a>Apache2::Filter object</a></code> )</dt>
<dd>

<p>The filter to remove</p>

</dd>
<dt id="ret:-ok-boolean">ret: <code>$ok</code> ( boolean )</dt>
<dd>

<p>a true value if EOS has been seen, otherwise a false value</p>

</dd>
<dt id="since:-2.0.0011">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>This method only works in streaming filters which exhaustively <code><a href="#C_read_">$f-&gt;read</a></code> all the incoming data in a while loop, like so:</p>

<pre><code>      while ($f-&gt;read(my $buffer, $wanted)) {
          # do something with $buffer
      }
      if ($f-&gt;seen_eos) {
          # do something
      }</code></pre>

<p>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 EOS bucket is read, the filter should expect not to be invoked again.</p>

<p>If an input streaming filter doesn&#39;t consume all data in the bucket brigade (or even in several bucket brigades), it has to generate the EOS event by itself. So when the filter is done it has to set the EOS flag:</p>

<pre><code>  $f-&gt;seen_eos(1);</code></pre>

<p>when the filter handler returns, internally mod_perl will take care of creating and sending the EOS bucket to the upstream input filter.</p>

<p>A similar logic may apply for output filters.</p>

<p>In most other cases you shouldn&#39;t set this flag. When this flag is prematurely set (before the real EOS bucket has arrived) in the current filter invocation, instead of invoking the filter again, mod_perl will create and send the EOS 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.</p>

<h1 id="Other-Filter-related-API">Other Filter-related API</h1>

<p>Other methods which affect filters, but called on non-<code>Apache2::Filter</code> objects:</p>

<h2 id="add_input_filter"><code>add_input_filter</code></h2>

<p>Add <code>&amp;callback</code> filter handler to input request filter chain.</p>

<pre><code>  $r-&gt;add_input_filter(\&amp;callback);</code></pre>

<p>Add <code>&amp;callback</code> filter handler to input connection filter chain.</p>

<pre><code>  $c-&gt;add_input_filter(\&amp;callback);</code></pre>

<dl>

<dt id="obj:-c-Apache2::Connection-object-or-r-Apache2::RequestRec-object">obj: <code>$c</code> ( <code><a>Apache2::Connection object</a></code> ) or <code>$r</code> ( <code><a>Apache2::RequestRec object</a></code> )</dt>
<dd>

</dd>
<dt id="arg1:-callback-CODE-ref">arg1: <code>&amp;callback</code> (CODE ref)</dt>
<dd>

</dd>
<dt id="ret:-no-return-value1">ret: no return value</dt>
<dd>

</dd>
<dt id="since:-2.0.0012">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<p>[META: It seems that you can&#39;t add a filter when another filter is called. I&#39;ve tried to add an output connection filter from the input connection filter when it was called for the first time. It didn&#39;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&#39;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&#39;t prepare the chain of 3rd party filter if none were inserted before the first filter was called.]</p>

<h2 id="add_output_filter"><code>add_output_filter</code></h2>

<p>Add <code>&amp;callback</code> filter handler to output request filter chain.</p>

<pre><code>  $r-&gt;add_output_filter(\&amp;callback);</code></pre>

<p>Add <code>&amp;callback</code> filter handler to output connection filter chain.</p>

<pre><code>  $c-&gt;add_output_filter(\&amp;callback);</code></pre>

<dl>

<dt id="obj:-c-Apache2::Connection-object-or-r-Apache2::RequestRec-object1">obj: <code>$c</code> ( <code><a>Apache2::Connection object</a></code> ) or <code>$r</code> ( <code><a>Apache2::RequestRec object</a></code> )</dt>
<dd>

</dd>
<dt id="arg1:-callback-CODE-ref1">arg1: <code>&amp;callback</code> (CODE ref)</dt>
<dd>

</dd>
<dt id="ret:-no-return-value2">ret: no return value</dt>
<dd>

</dd>
<dt id="since:-2.0.0013">since: 2.0.00</dt>
<dd>

</dd>
</dl>

<h1 id="Filter-Handler-Attributes">Filter Handler Attributes</h1>

<p>Packages using filter attributes have to subclass <code>Apache2::Filter</code>:</p>

<pre><code>  package MyApache2::FilterCool;
  use base qw(Apache2::Filter);</code></pre>

<p>Attributes are parsed during the code compilation, by the function <code>MODIFY_CODE_ATTRIBUTES</code>, inherited from the <code>Apache2::Filter</code> package.</p>

<h2 id="FilterRequestHandler"><code>FilterRequestHandler</code></h2>

<p>The <code>FilterRequestHandler</code> attribute tells mod_perl to insert the filter into an HTTP request filter chain.</p>

<p>For example, to configure an output request filter handler, use the <code>FilterRequestHandler</code> attribute in the handler subroutine&#39;s declaration:</p>

<pre><code>  package MyApache2::FilterOutputReq;
  sub handler : FilterRequestHandler { ... }</code></pre>

<p>and add the configuration entry:</p>

<pre><code>  PerlOutputFilterHandler MyApache2::FilterOutputReq</code></pre>

<p>This is the default mode. So if you are writing an HTTP request filter, you don&#39;t have to specify this attribute.</p>

<p>The section <a>HTTP Request vs. Connection Filters</a> delves into more details.</p>

<h2 id="FilterConnectionHandler"><code>FilterConnectionHandler</code></h2>

<p>The <code>FilterConnectionHandler</code> attribute tells mod_perl to insert this filter into a connection filter chain.</p>

<p>For example, to configure an output connection filter handler, use the <code>FilterConnectionHandler</code> attribute in the handler subroutine&#39;s declaration:</p>

<pre><code>  package MyApache2::FilterOutputCon;
  sub handler : FilterConnectionHandler { ... }</code></pre>

<p>and add the configuration entry:</p>

<pre><code>  PerlOutputFilterHandler MyApache2::FilterOutputCon</code></pre>

<p>The section <a>HTTP Request vs. Connection Filters</a> delves into more details.</p>

<h2 id="FilterInitHandler"><code>FilterInitHandler</code></h2>

<p>The attribute <code>FilterInitHandler</code> 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&#39;s actually called.</p>

<pre><code>  sub init : FilterInitHandler {
      my $f = shift;
      #...
      return Apache2::Const::OK;
  }</code></pre>

<p>In order to hook this filter callback, the real filter has to assign this callback using the <code><a href="#C_FilterHasInitHandler_">FilterHasInitHandler</a></code> which accepts a reference to the callback function.</p>

<p>For further discussion and examples refer to the <a>Filter Initialization Phase</a> tutorial section.</p>

<h2 id="FilterHasInitHandler"><code>FilterHasInitHandler</code></h2>

<p>If a filter wants to run an initialization callback it can register such using the <code>FilterHasInitHandler</code> attribute. Similar to <code>push_handlers</code> the callback reference is expected, rather than a callback name. The used callback function has to have the <code><a href="#C_FilterInitHandler_">FilterInitHandler</a></code> attribute. For example:</p>

<pre><code>  package MyApache2::FilterBar;
  use base qw(Apache2::Filter);
  sub init   : FilterInitHandler { ... }
  sub filter : FilterRequestHandler FilterHasInitHandler(\&amp;init) {
      my ($f, $bb) = @_;
      # ...
      return Apache2::Const::OK;
  }</code></pre>

<p>For further discussion and examples refer to the <a>Filter Initialization Phase</a> tutorial section.</p>

<h1 id="Configuration">Configuration</h1>

<p>mod_perl 2.0 filters configuration is explained in the <a>filter handlers tutorial</a>.</p>

<h2 id="PerlInputFilterHandler"><code>PerlInputFilterHandler</code></h2>

<p>See <code><a>PerlInputFilterHandler</a></code>.</p>

<h2 id="PerlOutputFilterHandler"><code>PerlOutputFilterHandler</code></h2>

<p>See <code><a>PerlOutputFilterHandler</a></code>.</p>

<h2 id="PerlSetInputFilter"><code>PerlSetInputFilter</code></h2>

<p>See <code><a>PerlSetInputFilter</a></code>.</p>

<h2 id="PerlSetOutputFilter"><code>PerlSetOutputFilter</code></h2>

<p>See <code><a>PerlSetInputFilter</a></code>.</p>

<h1 id="TIE-Interface">TIE Interface</h1>

<p><code>Apache2::Filter</code> also implements a tied interface, so you can work with the <code>$f</code> object as a hash reference.</p>

<p>The TIE interface is mostly unimplemented and might be implemented post 2.0 release.</p>

<h2 id="TIEHANDLE"><code>TIEHANDLE</code></h2>

<pre><code>  $ret = TIEHANDLE($stashsv, $sv);</code></pre>

<dl>

<dt id="obj:-stashsv-SCALAR">obj: <code>$stashsv</code> ( SCALAR )</dt>
<dd>

</dd>
<dt id="arg1:-sv-SCALAR">arg1: <code>$sv</code> ( SCALAR )</dt>
<dd>

</dd>
<dt id="ret:-ret-SCALAR">ret: <code>$ret</code> ( SCALAR )</dt>
<dd>

</dd>
<dt id="since:-subject-to-change">since: subject to change</dt>
<dd>

</dd>
</dl>

<h2 id="PRINT"><code>PRINT</code></h2>

<pre><code>  $ret = PRINT(...);</code></pre>

<dl>

<dt id="obj:-...-XXX">obj: <code>...</code> (XXX)</dt>
<dd>

</dd>
<dt id="ret:-ret-integer">ret: <code>$ret</code> ( integer )</dt>
<dd>

</dd>
<dt id="since:-subject-to-change1">since: subject to change</dt>
<dd>

</dd>
</dl>

<h1 id="See-Also">See Also</h1>

<p><a>mod_perl 2.0 documentation</a>.</p>

<h1 id="Copyright">Copyright</h1>

<p>mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.</p>

<h1 id="Authors">Authors</h1>

<p><a>The mod_perl development team and numerous contributors</a>.</p>


</body>

</html>