File: filters.pod

package info (click to toggle)
libapache2-mod-perl2 2.0.13-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 12,016 kB
  • sloc: perl: 97,771; ansic: 14,493; makefile: 51; sh: 18
file content (2995 lines) | stat: -rw-r--r-- 98,582 bytes parent folder | download | duplicates (8)
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
=head1 NAME

Input and Output Filters




=head1 Description

This chapter discusses mod_perl's input and output filter handlers.

If all you need is to lookup the filtering API proceed directly to the
C<L<Apache2::Filter|docs::2.0::api::Apache2::Filter>> and
C<L<Apache2::FilterRec|docs::2.0::api::Apache2::FilterRec>> manpages.




=head1 Introducing Filters

You certainly already know how filters work, because you
encounter filters so often in real life. If you are unfortunate to
live in smog-filled cities like Saigon or Bangkok you are probably
used to wear a dust filter mask:

=for html
<img src="filter_life_mask.jpg" width="150" height="159"
 align="middle"  alt="dust mask"><br><br>

If you are smoker, chances are that you smoke cigarettes with filters:

=for html
<img src="filter_life_cigarrette.jpg" width="95" height="116"
 align="middle" alt="cigarrette filter"><br><br>

If you are a coffee gourmand, you have certainly tried a filter
coffee:

=for html
<img src="filter_life_coffee.jpg" width="179" height="190" 
 align="middle" alt="coffee machine"><br><br>

The shower that you use, may have a water filter:

=for html
<img src="filter_life_shower.jpg" width="180" height="168"
 align="middle" alt="shower filter"><br><br>

When the sun is too bright, you protect your eyes by wearing sun
goggles with UV filter:

=for html
<img src="filter_life_goggles.jpg" width="200" height="86" 
 align="middle" alt="sun goggles"><br><br>

If are a photographer you can't go a step without using filter lenses:

=for html
<img src="filter_life_camera.jpg" width="191" height="160"
 align="middle" alt="photo camera"><br><br>

If you love music, you might be unaware of it, but your super-modern
audio system is literally loaded with various electronic filters:

=for html
<img src="filter_life_player.jpg" width="277" height="150" 
 align="middle" alt="LP player"><br><br>

There are many more places in our lives where filters are used. The
purpose of all filters is to apply some transformation to what's
coming into the filter, letting something different out of the
filter. Certainly in some cases it's possible to modify the source
itself, but that makes things unflexible, and but most of the time we
have no control over the source. The advantage of using filters to
modify something is that they can be replaced when requirements change
Filters also can be stacked, which allows us to make each filter do
simple transformations. For example by combining several different
filters, we can apply multiple transformations. In certain situations
combining several filters of the same kind let's us achieve a better
quality output.

The mod_perl filters are not any different, they receive some data,
modify it and send it out. In the case of filtering the output of the
response handler, we could certainly change the response handler's
logic to do something different, since we control the response
handler. But this may make the code unnecessary complex. If we can
apply transformations to the response handler's output, it certainly
gives us more flexibility and simplifies things. For example if a
response needs to be compressed before sent out, it'd be very
inconvenient and inefficient to code in the response handler
itself. Using a filter for that purpose is a perfect
solution. Similarly, in certain cases, using an input filter to
transform the incoming request data is the most wise solution. Think
of the same example of having the incoming data coming compressed.

Just like with real life filters, you can pipe several filters to
modify each other's output. You can also customize a selection of
different filters at run time.

Without much further ado, let's write a simple but useful obfuscation
filter for our HTML documents.

We are going to use a very simple obfuscation -- turn an HTML document
into a one liner, which will make it harder to read its source without
a special processing. To accomplish that we are going to remove
characters \012 (C<\n>) and \015 (C<\r>), which depending on the
platform alone or as a combination represent the end of line and a
carriage return.

And here is the filter handler code:

  #file:MyApache2/FilterObfuscate.pm
  #--------------------------------
  package MyApache2::FilterObfuscate;
  
  use strict;
  use warnings;
  
  use Apache2::Filter ();
  use Apache2::RequestRec ();
  use APR::Table ();
  
  use Apache2::Const -compile => qw(OK);
  
  use constant BUFF_LEN => 1024;
  
  sub handler {
      my $f = shift;
  
      unless ($f->ctx) {
          $f->r->headers_out->unset('Content-Length');
          $f->ctx(1);
      }
  
      while ($f->read(my $buffer, BUFF_LEN)) {
          $buffer =~ s/[\r\n]//g;
          $f->print($buffer);
      }
  
      return Apache2::Const::OK;
  }
  1;

The directives below configure Apache to apply the
C<MyApache2::FilterObfuscate> filter to all requests that get mapped
to files with an I<".html"> extension:

  <Files ~ "\.html">
      PerlOutputFilterHandler MyApache2::FilterObfuscate
  </Files>

Filters are expected to return C<Apache2::Const::OK> or
C<Apache2::Const::DECLINED>.  But instead of receiving C<$r> (the
request object) as the first argument, they receive C<$f> (the filter
object). The filter object is described later in this chapter.

The filter starts by unsetting the C<Content-Length> response
header, because it modifies the length of the response body (shrinks
it). If the response handler sets the C<Content-Length> header and
the filter doesn't unset it, the client may have problems receiving the
response since it will expect more data than it was sent. I<Setting the
Content-Length Header> below describes how to set the Content-Length
header if you need to.

The core of this filter is a read-modify-print expression in a while
loop. The logic is very simple: read at most C<BUFF_LEN> characters of
data into C<$buffer>, apply the regex to remove any occurences of
C<\n> and C<\r> in it, and print the resulting data out. The input
data may come from a response handler, or from an upstream filter. The
output data goes to the next filter in the output chain. Even though
in this example we haven't configured any more filters, internally
Apache itself uses several core filters to manipulate the data and
send it out to the client.

As we are going to explain in detail in the following sections, the
same filter may be called many times during a single request, every
time receiving a subsequent chunk of data. For example if the POSTed
request data is 64k long, an input filter could be invoked 8 times,
each time receiving 8k of data. The same may happen during the
response phase, where an upstream filter may split 64k of output in 8,
8k chunks. The while loop that we just saw is going to read each of
these 8k in 8 calls, since it requests 1k on every C<read()> call.

Since it's enough to unset the C<Content-Length> header when the
filter is called the first time, we need to have some flag telling us
whether we have done the job. The method C<ctx()> provides this
functionality:

      unless ($f->ctx) {
          $f->r->headers_out->unset('Content-Length');
          $f->ctx(1);
      }

The C<unset()> call will be made only on the first filter call for
each request. You can store any kind of a Perl data
structure in C<L<$f-E<gt>ctx|docs::2.0::api::Apache2::Filter/C_ctx_>>
and retrieve it later in subsequent filter invocations of the same
request. There are several examples using this method in the
following sections.

To be truly useful, the C<MyApache2::FilterObfuscate> filter logic
should take into account situations where removing new line characters
will make the document render incorrectly in the browser.  As we
mentioned above, this is the case if there are multi-line
C<E<lt>preE<gt>>...C<E<lt>/preE<gt>> entries. Since this increases the
complexity of the filter, we will disregard this requirement for now.

A positive side-effect of this obfuscation algorithm is that it
reduces the amount of the data sent to the client. The
C<Apache::Clean> module, available from the CPAN, provides a
production-ready implementation of this technique which takes into
account the HTML markup specifics.

mod_perl I/O filtering follows the Perl principle of making simple
things easy and difficult things possible. You have seen that it's
trivial to write simple filters. As you read through this chapter you
will see that much more difficult things are possible, and that the
code is more elaborate.


=head1 I/O Filtering Concepts

Before introducing the APIs, mod_perl provides for Apache Filtering,
there are several important concepts to understand.

=head2 Two Methods for Manipulating Data

mod_perl provides two interfaces to filtering: a direct bucket
brigades manipulation interface and a simpler, stream-oriented
interface.  Apache 2.0 considers all incoming and outgoing data as
chunks of information, disregarding their kind and source or storage
methods. These data chunks are stored in I<buckets>, which form
L<bucket
brigades|docs::2.0::user::handlers::intro/Bucket_Brigades>. Input and
output filters massage the data in these I<bucket brigades>. Response
and protocol handlers also receive and send data using bucket
brigades, though in most cases this is hidden behind wrappers, such as
C<read()> and C<print()>.

mod_perl 2.0 filters can directly manipulate the bucket brigades or
use the simplified streaming interface where the filter object acts
similar to a filehandle, which can be read from and printed to.

Even though you don't use bucket brigades directly when you use the
streaming filter interface (which works on bucket brigades behind the
scenes), it's still important to understand bucket brigades. For
example you need to know that an output filter will be invoked as many
times as the number of bucket brigades sent from an upstream filter or
a content handler. Or you need to know that the end of stream
indicator (EOS) is sometimes sent in a separate bucket brigade, so it
shouldn't be a surprise that the filter was invoked even though no
real data went through. As we delve into the filter details you will
see that L<understanding bucket
brigades|docs::2.0::user::handlers::intro/Bucket_Brigades>, will help
to understand how filters work.

Moreover you will need to understand bucket brigades if you plan to
implement L<protocol modules|docs::2.0::user::handlers::protocols>.




=head2 HTTP Request Versus Connection Filters

HTTP request filters are applied when Apache serves an HTTP request.
HTTP request input filters get invoked on the body of the HTTP request
only if the body is consumed by the content handler.  HTTP request
headers are not passed through the HTTP request input filters.  HTTP
response output filters get invoked on the body of the HTTP response
if the content handler has generated one. HTTP response headers are
not passed through the HTTP response output filters.

It is also possible to apply filters at the connection level.  A
connection may be configured to serve one or more HTTP requests, or
handle other protocols. Connection filters see all the incoming and
outgoing data. If an HTTP request is served, connection filters can
modify the HTTP headers and the body of request and response.  If a
different protocol is served over the connection (e.g., IMAP), the
data could have a completely different pattern than the HTTP protocol
(headers + body). Thus, the only difference between connection filters
and request filters is that connection filters see everything from the
request, i.e., the headers and the body, whereas request filters see
only the body.


mod_perl 2.0 may
support several other Apache filter types in the future.

=head2 Multiple Invocations of Filter Handlers

Unlike other Apache handlers, filter handlers may get invoked more
than once during the same request. Filters get invoked as many times
as the number of bucket brigades sent from an upstream filter or a
content provider.

For example, a content generation handler may send a string, then
force a flush, and then send more data:

  # assuming buffered STDOUT ($|==0)
  $r->print("foo");
  $r->rflush;
  $r->print("bar");

In this case, Apache will generate one bucket brigade with two
buckets. There are several types of buckets which contain data; in
this example, the data type is I<transient>:

  bucket type       data
  ----------------------
  1st    transient   foo
  2nd    flush

Apache sends this bucket brigade to the filter chain.  Then, assuming
no more data is sent after C<print("bar")>, it will create a last
bucket brigade, with one bucket, containing data:

  bucket type       data
  ----------------------
  1st    transient   bar

and send it to the filter chain. Finally it will send yet another
bucket brigade with the EOS bucket indicating that there will be no
more data sent:

  bucket type       data
  ----------------------
  1st    eos

N<EOS buckets are valid for request filters. For connection filters,
you will get one only in the response filters and only at the end of
the connection. You can see a sample workaround for this situation in
the module C<Apache2::Filter::HTTPHeadersFixup> available on the
CPAN.>

Note that the EOS bucket may come attached to the last bucket brigade
with data, instead of coming in its own bucket brigade.  The location
depends on the other Apache modules manipulating the buckets and can
vary. Filters should never assume that the EOS bucket is arriving
alone in a bucket brigade.  Therefore the first output filter will be
invoked two or three times (three times if EOS is coming in its own
brigade), depending on the number of bucket brigades sent by the
response handler.

An upstream filter can modify the bucket brigades, by inserting extra
bucket brigades or even by collecting the data from multiple bucket
brigades and sending it along in just one brigade.  Therefore, when
coding a filter, never assume that the filter is always going to be
invoked once, or any fixed number of times. Neither can you assume how
the data is going to come in. To accommodate these situations, a
typical filter handler may need to split its logic in three parts.

To illustrate, below is some pseudo-code that represents all three
parts, i.e., initialization, processing, and finalization. This is a
typical stream-oriented filter handler.

  sub handler {
      my $f = shift;
  
      # runs on first invocation
      unless ($f->ctx) {
          init($f);
          $f->ctx(1);
      }
  
      # runs on all invocations
      process($f);
  
      # runs on the last invocation
      if ($f->seen_eos) {
          finalize($f);
      }
  
      return Apache2::Const::OK;
  }
  sub init     { ... }
  sub process  { ... }
  sub finalize { ... }

The following diagram depicts all three parts:

=for html
<img src="filter_logic.gif" width="620" height="356" 
 align="middle" alt="filter flow logic"><br><br>

Let's explain each part using this pseudo-filter.

=over

=item 1 Initialization

During initialization, the filter runs code that you want executed
only once, even if there are multiple invocations of the filter (this
is during a single request). The filter context ($f-E<gt>ctx) is used
as a flag to accomplish this task. For each new request the filter
context is created before the filter is called for the first time, and
it is destroyed at the end of the request.

      unless ($f->ctx) {
          init($f);
          $f->ctx(1);
      }

When the filter is invoked for the first time
C<L<$f-E<gt>ctx|docs::2.0::api::Apache2::Filter/C_ctx_>> returns
C<undef> and the custom function init() is called. This function
could, for example, retrieve some configuration data set in
I<httpd.conf> or initialize some data structure to a default value.

To make sure that init() won't be called on the following invocations,
we must set the filter context before the first invocation is
completed:

          $f->ctx(1);

In practice, the context is not just used as a flag, but to store real
data. You can use it to hold any data structure and pass it between
successive filter invocations. For example, the following filter
handler counts the number of times it was invoked during a single
request:

  sub handler {
      my $f = shift;
  
      my $ctx = $f->ctx;
      $ctx->{invoked}++;
      $f->ctx($ctx);
      warn "filter was invoked $ctx->{invoked} times\n";
  
      return Apache2::Const::DECLINED;
  }

Since this filter handler doesn't consume the data from the upstream
filter, it's important that this handler return
C<Apache2::Const::DECLINED>, in which case mod_perl passes the current
bucket brigade to the next filter. If this handler returns
C<Apache2::Const::OK>, the data will be lost, and if that data
included a special EOS token, this may cause problems.

Unsetting the C<Content-Length> header for filters that modify the
response body length is a good example of code to run in the
initialization phase:

  unless ($f->ctx) {
      $f->r->headers_out->unset('Content-Length');
      $f->ctx(1);
  }

We will see more initialization examples later in this chapter.

=item 2 Processing

The next part:

      process($f);

is unconditionally invoked on every filter invocation. That's where
the incoming data is read, modified and sent out to the next filter in
the filter chain. Here is an example that lowers the case of the
characters passing through:

  use constant READ_SIZE  => 1024;
  sub process {
      my $f = shift;
      while ($f->read(my $data, READ_SIZE)) {
          $f->print(lc $data);
      }
  }

Here the filter operates only on a single bucket brigade. Since it
manipulates every character separately the logic is simple.

In more complicated situations, a filter may need to buffer data
before the transformation can be applied. For example, if the filter
operates on HTML tokens (e.g., 'E<lt>img src="me.jpg"E<gt>'), it's
possible that one brigade will include the beginning of the token
('E<lt>img ') and the remainder of the token ('src="me.jpg"E<gt>')
will come in the next bucket brigade (on the next filter
invocation). To operate on the token as a whole, you would need to
capture each piece over several invocations. To do so, you can store
the unprocessed data in the filter context and then access it again on
the next invocation.

Another good example of the need to buffer data is a filter that
performs data compression, because compression is usually effective
only when applied to relatively big chunks of data. If a single bucket
brigade doesn't contain enough data, the filter may need to buffer the
data in the filter context until it collects enough to compress it.

=item 3 Finalization

Finally, some filters need to know when they are invoked for the last
time, in order to perform various cleanups and/or flush any remaining
data. As mentioned earlier, Apache indicates this event by a special
end of stream "token", represented by a bucket of type C<EOS>.  If the
filter is using the streaming interface, rather than manipulating the
bucket brigades directly, and it was calling C<read()> in a while
loop, it can check for the EOS token using the C<$f-E<gt>seen_eos>
method:

      if ($f->seen_eos) {
          finalize($f);
      }

This check should be done at the end of the filter handler because the
EOS token can come attached to the tail of some data or all alone such
that the last invocation gets only the EOS token.  If this test is
performed at the beginning of the handler and the EOS bucket was sent
in together with the data, the EOS event may be missed and the filter
won't function properly.

Filters that directly manipulate bucket brigades must manually look
for a bucket whose type is C<EOS>. There are examples of this method
later in the chapter.

=back

While not all filters need to perform all of these steps, this is a
good model to keep in mind while working on your filter handlers.
Since filters are called multiple times per request, you will likely
use these steps, with initialization, processing, and finishing, on
all but the simplest filters.


=head2 Blocking Calls

All filters (excluding the core filter that reads from the network and
the core filter that writes to it) block at least once when
invoked. Depending on whether this is an input or an output filter,
the blocking happens when the bucket brigade is requested from the
upstream filter or when the bucket brigade is passed to the downstream
filter.

Input and output filters differ in the ways they acquire the bucket
brigades, and thus in how blocking is handled. Each type is described
separately below. Although you can't see the difference when using the
streaming API, it's important to understand how things work
underneath. Therefore the examples below are transparent filters,
passing data through them unmodified.  Instead of reading the data in
and printing it out, the bucket brigades are passed as is. This makes
it easier to observe the blocking behavior.

The first example is a transparent input filter:

  #file:MyApache2/FilterTransparent.pm (first part)
  #-----------------------------------------------
  package MyApache2::FilterTransparent;
  
  use Apache2::Filter ();
  
  use Apache2::Const -compile => qw(OK);
  use APR::Const     -compile => ':common';
  
  sub in {
      my ($f, $bb, $mode, $block, $readbytes) = @_;
  
      my $rv = $f->next->get_brigade($bb, $mode, $block, $readbytes);
      return $rv unless $rv == APR::Const::SUCCESS;
  
      return Apache2::Const::OK;
  }

When the input filter I<in()> is invoked, it first asks the upstream
filter for the next bucket brigade (using the C<get_brigade()>
call). That upstream filter is in turn going to ask for the bucket
brigade from the next upstream filter and so on up the chain, until
the last filter (called C<core_in>), the one that reads from the
network, is reached. The C<core_in> filter reads, using a socket, a
portion of the incoming data from the network, processes it, and sends
it to its downstream filter. That filter processes the data and send
it to its downstream filter, etc., until it reaches the first filter
that requested the data. (In reality some other handler triggers the
request for the bucket brigade, such as an HTTP response handler or a
protocol module, but for this discussion it's sufficient to assume
that it's the first filter that issues the C<get_brigade()> call.)

The following diagram depicts a typical input filter data flow
in addition to the program control flow.

=for html
<img src="in_filter_stream.gif" width="659" height="275" 
 align="middle" alt="input filter data flow"><br><br>

The black- and white-headed arrows show when the control is passed
from one filter to another. In addition, the black-headed arrows show
the actual data flow. The diagram includes some pseudo-code, in Perl
for the mod_perl filters and in C for the internal Apache filters. You
don't have to understand C to understand this diagram. What's
important to understand is that when input filters are invoked, they
first call each other via the C<get_brigade()> call and then block
(notice the brick wall on the diagram), waiting for the call to
return. When this call returns, all upstream filters have already
completed their filtering task on the bucket brigade.

As mentioned earlier, the streaming interface hides the details,
but the first C<$f-E<gt>read()> call will block as the layer under it
performs the C<get_brigade()> call.

The diagram shows only part of the actual input filter chain for an
HTTP request. The C<...> indicates that there are more filters in
between the mod_perl filter and C<http_in>.

Now let's look at what happens in the output filters chain. Here the
first filter acquires the bucket brigades containing the response data
from the content handler (or another protocol handler if we aren't
talking HTTP). It may then make some modification and pass the data to
the next filter (using the C<pass_brigade()> call), which in turn
applies its modifications and sends the bucket brigade to the next
filter, etc. This continues all the way down to the last filter
(called C<core>) which writes the data to the network via the socket
the client is listening to.

Even though the output filters don't have to wait to acquire the
bucket brigade (since the upstream filter passes it to them as an
argument), they still block in a similar fashion to input filters,
since they have to wait for the C<pass_brigade()> call to return. In
this case, they are waiting to pass the data along rather than waiting
to receive it.

Here is an example of a transparent output filter:

  #file:MyApache2/FilterTransparent.pm (continued)
  #-----------------------------------------------
  sub out {
      my ($f, $bb) = @_;
  
      my $rv = $f->next->pass_brigade($bb);
      return $rv unless $rv == APR::Const::SUCCESS;
  
      return Apache2::Const::OK;
  }
  1;

The I<out()> filter passes C<$bb> to the downstream filter unmodified.
If you add print statements before and after the C<pass_brigade()>
call and configure the same filter twice, the print will show the
blocking call.

The following diagram depicts a typical output filter data flow in
addition to the program control flow:

=for html
<img src="out_filter_stream.gif" width="575" height="261" 
 align="middle" alt="output filter data flow"><br><br>

Similar to the input filters chain diagram, the arrows show the
program control flow and in addition the black-headed arrows show the
data flow. Again, it uses Perl pseudo-code for the mod_perl filter and
C pseudo-code for the Apache filters and the brick walls represent the
waiting. The diagram shows only part of the real HTTP response filters
chain, where C<...> stands for the omitted filters.



=head1 mod_perl Filters Declaration and Configuration

Now that we have laid out some basic concepts involved in filter use,
we can look at how mod_perl filters are declared and configured.

=head2 Filter Priority Types

When Apache filters are configured they are inserted into the filters
chain according to their priority type. In most cases when using one
or two filters things will just work, however if you find that the
order of filter invocation is wrong, you should consider the filter
priority type. Unfortunately this information is available only in the
Apache source code, unless it's documented in the module man
pages. Numerical definitions of priority types, such as
C<AP_FTYPE_CONTENT_SET> and C<AP_FTYPE_RESOURCE>, can be found in the
Apache source distribution in I<include/util_filter.h>.

As of this writing Apache comes with two core filters: C<DEFLATE> and
C<INCLUDES>. Regardless of your configuration directives, e.g.,:

  SetOutputFilter DEFLATE
  SetOutputFilter INCLUDES

the C<INCLUDES> filter will be inserted in the filters chain before
the C<DEFLATE> filter, even though it was configured after it. This is
because the C<DEFLATE> filter is of type C<AP_FTYPE_CONTENT_SET> (20),
whereas the C<INCLUDES> filter is of type C<AP_FTYPE_RESOURCE> (10).

As of this writing mod_perl provides two kind of filters with fixed
priority type (the type is defined by L<the filter handler's
attribute|/HTTP_Request_vs__Connection_Filters>):

  Handler's Attribute      Priority           Value
  -------------------------------------------------
  FilterRequestHandler     AP_FTYPE_RESOURCE    10
  FilterConnectionHandler  AP_FTYPE_PROTOCOL    30

Therefore C<FilterRequestHandler> filters (10) will always be invoked
before the C<DEFLATE> filter (20), whereas C<FilterConnectionHandler>
filters (30) will be invoked after it. When two filters have the same
priority (e.g., the C<INCLUDES> filter (10) has the same priority as
C<FilterRequestHandler> filters (10)), they are run in the order they
are configured. Therefore filters are inserted according to the
configuration order when
C<L<PerlSetOutputFilter|/PerlSetOutputFilter>> or
C<L<PerlSetInputFilter|/PerlSetInputFilter>> are used.


=head2 C<PerlInputFilterHandler>

The C<PerlInputFilterHandler> directive registers a filter, and
inserts it into the L<relevant|/HTTP_Request_vs__Connection_Filters>
input filters chain.

This handler is of type
C<L<VOID|docs::2.0::user::handlers::intro/item_VOID>>.

The handler's configuration scope is
C<L<DIR|docs::2.0::user::config::config/item_DIR>>.

B<Arguments>

C<PerlInputFilterHander> handlers are passed two arguments:
an C<L<Apache2::Filter>> object; and
an C<L<APR::Brigade>> object;

See the examples that follow in this chapter for further explanation.

B<Return>

Filters are expected to return C<L<Apache2::Const::OK|Apache2::Const>>
or C<L<Apache2::Const::DECLINED|Apache2::Const>>.
See the examples that follow in this chapter for further explanation.

B<Examples>

C<PerlInputFilterHandler> handlers are automatically
C<L<AutoLoad|docs::2.0::user::config::config/C_AutoLoad_>>ed, since
they need to be compiled before L<the filter
attributes|/HTTP_Request_vs__Connection_Filters> can be accessed.
Therefore if the filter handler subroutine is not called C<handler>,
you must preload the module containing the filter subroutine at server
startup. A filter handler can be configured not to be
C<L<AutoLoad|docs::2.0::user::config::config/C_AutoLoad_>>ed, using
the C<-> prefix. For example:

  PerlInputFilterHandler -MyApache2::FilterTest::lc

The following sections include several examples that use the
C<PerlInputFilterHandler> handler.





=head2 C<PerlOutputFilterHandler>

The C<PerlOutputFilterHandler> directive registers a filter, and
inserts it into the L<relevant|/HTTP_Request_vs__Connection_Filters>
output filters chain.

This handler is of type
C<L<VOID|docs::2.0::user::handlers::intro/item_VOID>>.

The handler's configuration scope is
C<L<DIR|docs::2.0::user::config::config/item_DIR>>.

B<Arguments>

C<PerlOutputFilterHander> handlers are passed five arguments:
an C<L<Apache2::Filter>> object;
an C<L<APR::Brigade>> object;
an  C<L<Apache2::Const :input_mode|docs::2.0::api::Apache2::Const/toc_C__input_mode_>> constant;
an C<L<APR::Const :read_type|docs::2.0::api::APR::Const/toc_C__read_type_>> constant; and
the number of bytes to read.

See the examples that follow in this chapter for further explanation.

B<Return>

B<Examples>

The following sections include several examples that use the
C<PerlOutputFilterHandler> handler.

Similar to C<L<PerlInputFilterHandler|/C_PerlInputFilterHandler_>>,
C<PerlOutputFilterHandler> handlers are automatically
C<L<AutoLoad|docs::2.0::user::config::config/C_AutoLoad_>>ed.






=head2 C<PerlSetInputFilter>

The C<SetInputFilter> directive, documented at
I<http://httpd.apache.org/docs-2.0/mod/core.html#setinputfilter>, sets
the filter or filters which will process client requests and POST
input when they are received by the server (in addition to any filters
configured earlier).

To mix mod_perl and non-mod_perl input filters of the L<same
priority|/Filter_Priority_Types> nothing special should be done. For
example if we have an imaginary Apache filter C<FILTER_FOO> and
mod_perl filter C<MyApache2::FilterInputFoo>, this configuration:

  SetInputFilter FILTER_FOO
  PerlInputFilterHandler MyApache2::FilterInputFoo

will add both filters. However the order of their invocation might
not be as you expect. To make the invocation order the
same as the insertion order, replace C<SetInputFilter> with
C<PerlSetInputFilter>, like so:

  PerlSetInputFilter FILTER_FOO
  PerlInputFilterHandler MyApache2::FilterInputFoo

Now the C<FILTER_FOO> filter will always be executed before the
C<MyApache2::FilterInputFoo> filter, since it was configured before
C<MyApache2::FilterInputFoo> (i.e., it'll apply its transformations on
the incoming data last). The diagram below shows the input filters
chain and the data flow from the network to the response handler for
the presented configuration:

       response handler
             /\
             ||
         FILTER_FOO
             /\
             ||
   MyApache2::FilterInputFoo
             /\
             ||
     core input filters
             /\
             ||
           network

As explained in the section L<Filter Priority
Types|/Filter_Priority_Types> this directive won't affect filters of
different priority. For example assuming that
C<MyApache2::FilterInputFoo> is a C<FilterRequestHandler> filter, the
configurations:

  PerlInputFilterHandler MyApache2::FilterInputFoo
  PerlSetInputFilter DEFLATE

and

  PerlSetInputFilter DEFLATE
  PerlInputFilterHandler MyApache2::FilterInputFoo

are equivalent, because mod_deflate's C<DEFLATE> filter has a higher
priority than C<MyApache2::FilterInputFoo>. Thefore, it will always be
inserted into the filter chain after C<MyApache2::FilterInputFoo>,
(i.e. the C<DEFLATE> filter will apply its transformations on the
incoming data first). The diagram below shows the input filters chain
and the data flow from the network to the response handler for the
presented configuration:

      response handler
             /\
             ||
   MyApache2::FilterInputFoo
             /\
             ||
          DEFLATE
             /\
             ||
     core input filters
             /\
             ||
           network

C<SetInputFilter>'s C<;> semantics are supported as well. For
example, in the following configuration:

  PerlInputFilterHandler MyApache2::FilterInputFoo
  PerlSetInputFilter FILTER_FOO;FILTER_BAR

C<MyApache2::FilterOutputFoo> will be executed first, followed by
C<FILTER_FOO> and finally by C<FILTER_BAR> (again, assuming that all
three filters have the same priority).


=head2 C<PerlSetOutputFilter>

The C<SetOutputFilter> directive, documented at
I<http://httpd.apache.org/docs-2.0/mod/core.html#setoutputfilter> sets
the filters which will process responses from the server before they
are sent to the client (in addition to any filters configured
earlier).

To mix mod_perl and non-mod_perl output filters of the L<same
priority|/Filter_Priority_Types> nothing special should
be done. This configuration:

  SetOutputFilter INCLUDES
  PerlOutputFilterHandler MyApache2::FilterOutputFoo

As with input filters, to
preserve the insertion order replace C<SetOutputFilter> with
C<PerlSetOutputFilter>, like so:

  PerlSetOutputFilter INCLUDES
  PerlOutputFilterHandler MyApache2::FilterOutputFoo

Now mod_include's C<INCLUDES> filter will always be executed before
the C<MyApache2::FilterOutputFoo> filter. The diagram below shows the
output filters chain and the data flow from the response handler to
the network for the presented configuration:

      response handler
             ||
             \/
          INCLUDES
             ||
             \/
   MyApache2::FilterOutputFoo
             ||
             \/
     core output filters
             ||
             \/
           network

C<SetOutputFilter>'s C<;> semantics are supported as well. For
example, in the following configuration:

  PerlOutputFilterHandler MyApache2::FilterOutputFoo
  PerlSetOutputFilter INCLUDES;FILTER_FOO

C<MyApache2::FilterOutputFoo> will be executed first, followed by
C<INCLUDES> and finally by C<FILTER_FOO> (again, assuming that all
three filters have the same priority).

As explained in the C<L<PerlSetInputFilter|/PerlSetInputFilter>>
section, if filters have different priorities, the insertion order
might be different. For example in the following configuration:

  PerlSetOutputFilter DEFLATE
  PerlSetOutputFilter INCLUDES
  PerlOutputFilterHandler MyApache2::FilterOutputFoo

mod_include's C<INCLUDES> filter will be always executed before the
C<MyApache2::FilterOutputFoo> filter. The latter will be followed by
mod_deflate's C<DEFLATE> filter, even though it was configured before
the other two filters. This is because it has a L<higher
priority|/Filter_Priority_Types>. And the corresponding diagram looks
like so:

      response handler
             ||
             \/
          INCLUDES
             ||
             \/
   MyApache2::FilterOutputFoo
             ||
             \/
           DEFLATE
             ||
             \/
     core output filters
             ||
             \/
           network







=head2 Adding OutputFilters Dynamically

If you have the need to add output filters dymically during the
request, mod_perl 2.0 offers you the possibility to push filter
callbacks at request time. For example here is how to add an output
filter during the Fixup phase:

  <Files *\.html >
    PerlFixupHandler MyApache2::AddFilterDyn
  </Files>

And the corresponding module is:

  #file:MyApache2/AddFilterDyn.pm
  #------------------------------
  package MyApache2::AddFilterDyn;
  
  use Apache2::Filter;
  use MyApache2::FilterObfuscate;
  
  use Apache2::Const -compile => qw(OK);
  
  sub handler {
      my $r = shift;
  
      $r->add_output_filter(\&MyApache2::FilterObfuscate::handler);
  
      return Apache2::Const::OK;
  }
  
  1;

You can also add connection filters dynamically. For more information
refer to the C<L<Apache2::Filter|docs::2.0::api::Apache2::Filter>>
manpages
C<L<add_input_filter|docs::2.0::api::Apache2::Filter/C_add_input_filter_>>
and
C<L<add_output_filter|docs::2.0::api::Apache2::Filter/C_add_output_filter_>>.





=head2 HTTP Request vs. Connection Filters

mod_perl 2.0 supports connection and HTTP request filtering. mod_perl
filter handlers specify the type of the filter using the method
attributes.

HTTP request filter handlers are declared using the
C<FilterRequestHandler> attribute. Consider the following request
input and output filters skeleton:

  package MyApache2::FilterRequestFoo;
  use base qw(Apache2::Filter);
  
  sub input  : FilterRequestHandler {
      my ($f, $bb, $mode, $block, $readbytes) = @_;
      #...
  }
  
  sub output : FilterRequestHandler {
      my ($f, $bb) = @_;
      #...
  }
  
  1;

If the attribute is not specified, the default C<FilterRequestHandler>
attribute is assumed.  Filters specifying subroutine attributes must
subclass C<Apache2::Filter>, others only need to:

  use Apache2::Filter ();

Request filters are usually configured in the
C<E<lt>LocationE<gt>> or equivalent sections:

  PerlModule MyApache2::FilterRequestFoo
  PerlModule MyApache2::NiceResponse
  <Location /filter_foo>
      SetHandler modperl
      PerlResponseHandler     MyApache2::NiceResponse
      PerlInputFilterHandler  MyApache2::FilterRequestFoo::input
      PerlOutputFilterHandler MyApache2::FilterRequestFoo::output
  </Location>

Now we have the request input and output filters configured.

The connection filter handler uses the C<FilterConnectionHandler>
attribute. Here is a similar example for the connection input and
output filters.

  package MyApache2::FilterConnectionBar;
  use base qw(Apache2::Filter);
  
  sub input  : FilterConnectionHandler {
      my ($f, $bb, $mode, $block, $readbytes) = @_;
      #...
  }
  
  sub output : FilterConnectionHandler {
      my ($f, $bb) = @_;
      #...
  }
  
  1;

With connection filters, unlike the request flters, the configuration
must be done outside the C<E<lt>LocationE<gt>> or equivalent sections,
usually within the C<E<lt>VirtualHostE<gt>> or the global server
configuration:

  Listen 8005
  <VirtualHost _default_:8005>
      PerlModule MyApache2::FilterConnectionBar
      PerlModule MyApache2::NiceResponse
   
      PerlInputFilterHandler  MyApache2::FilterConnectionBar::input
      PerlOutputFilterHandler MyApache2::FilterConnectionBar::output
      <Location />
          SetHandler modperl
          PerlResponseHandler MyApache2::NiceResponse
      </Location>
   
  </VirtualHost>

This configures the connection input and output
filters.

As can be seen from the above examples, the only difference between
connection filters and request filters is that that connection filters
see everything from the request, i.e., the headers and the body,
whereas request filters see only the body.








=head2 Filter Initialization Phase

There is one more callback in the filter framework. And that's
C<FilterInitHandler>. This I<init> callback runs immediately after the
filter handler is inserted into the filter chain, before it is
invoked for the first time. Here is a skeleton of an init handler:

  sub init : FilterInitHandler {
      my $f = shift;
      #...
      return Apache2::Const::OK;
  }

The attribute C<FilterInitHandler> marks the Perl function as suitable
to be used as a filter initialization callback.

For example you may decide to dynamically remove a filter before it
had a chance to run, if some condition is true:

  sub init : FilterInitHandler {
      my $f = shift;
      $f->remove() if should_remove_filter();
      return Apache2::Const::OK;
  }

Not all C<Apache2::Filter> methods can be used in the init handler,
because it's not a filter. Hence you can use methods that L<operate on
the filter itself|docs::2.0::api::Apache2::Filter/Common_Filter_API>,
such as C<L<remove()|docs::2.0::api::Apache2::Filter/C_remove_>> and
C<L<ctx()|docs::2.0::api::Apache2::Filter/C_ctx_>> or retrieve request
information, such as C<L<r()|docs::2.0::api::Apache2::Filter/C_r_>>
and C<L<c()|docs::2.0::api::Apache2::Filter/C_c_>>. You cannot use
methods that operate on data, such as
C<L<read()|docs::2.0::api::Apache2::Filter/C_read_>> and
C<L<print()|docs::2.0::api::Apache2::Filter/C_print_>>.

In order to hook an init filter handler, the real filter has to assign
this callback using the C<FilterHasInitHandler> function which accepts
a reference to the callback function, similar to
C<push_handlers()>. The callback function referred to must have the
C<FilterInitHandler> attribute. For example:

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

While attributes are parsed during compilation (it's really a sort of
source filter), the argument to the C<FilterHasInitHandler()>
attribute is compiled at a later stage once the module is compiled.

The argument to C<FilterHasInitHandler()> can be any Perl code which
when C<eval()>'ed returns a code reference. For example:

  package MyApache2::OtherFilter;
  use base qw(Apache2::Filter);
  sub init  : FilterInitHandler { ... }
  
  package MyApache2::FilterBar;
  use MyApache2::OtherFilter;
  use base qw(Apache2::Filter);
  sub get_pre_handler { \&MyApache2::OtherFilter::init }
  sub filter : FilterHasInitHandler(get_pre_handler()) { ... }

Here the C<MyApache2::FilterBar::filter> handler is configured to run
the C<MyApache2::OtherFilter::init> init handler.

Notice that the argument to C<FilterHasInitHandler()> is always
C<eval()>'ed in the package of the real filter handler (not the init
handler). So the above code leads to the following evaluation:

  $init_sub = eval "package MyApache2::FilterBar; get_pre_handler()";

This part is actually done in C, using the C<eval_pv()> C call.

Currently only one initialization callback can be registered per
filter handler.



=head1 All-in-One Filter

Before we delve into the details of how to write filters that do
something with the data, lets first write a simple filter that does
nothing but snooping on the data that goes through it. We are going to
develop the C<MyApache2::FilterSnoop> handler which can snoop on
request and connection filters, in input and output modes.

First we create a simple response handler that dumps
the request's I<args> and I<content> as strings:

  #file:MyApache2/Dump.pm
  #---------------------
  package MyApache2::Dump;
  
  use strict;
  use warnings;
  
  use Apache2::RequestRec ();
  use Apache2::RequestIO ();
  use Apache2::Filter ();
  use APR::Brigade ();
  use APR::Bucket ();
  
  use Apache2::Const -compile => qw(OK M_POST);
  
  sub handler {
      my $r = shift;
      $r->content_type('text/plain');
  
      $r->print("args:\n", $r->args, "\n");
  
      if ($r->method_number == Apache2::Const::M_POST) {
          my $data = content($r);
          $r->print("content:\n$data\n");
      }
  
      return Apache2::Const::OK;
  }
  
  use Apache2::Connection ();
  
  use Apache2::Const -compile => qw(MODE_READBYTES);
  use APR::Const     -compile => qw(SUCCESS BLOCK_READ);
  
  use constant IOBUFSIZE => 8192;
  
  sub content {
      my $r = shift;
  
      my $bb = APR::Brigade->new($r->pool, $r->connection->bucket_alloc);
  
      my $data = '';
      my $seen_eos = 0;
      do {
          $r->input_filters->get_brigade($bb, Apache2::Const::MODE_READBYTES,
                                         APR::Const::BLOCK_READ, IOBUFSIZE);
  
          for (my $b = $bb->first; $b; $b = $bb->next($b)) {
              if ($b->is_eos) {
                  $seen_eos++;
                  last;
              }
  
              if ($b->read(my $buf)) {
                  $data .= $buf;
              }
  
              $b->remove; # optimization to reuse memory
          }
      } while (!$seen_eos);
  
      $bb->destroy;
  
      return $data;
  }
  
 1;

which is configured as:

  PerlModule MyApache2::Dump
  <Location /dump>
      SetHandler modperl
      PerlResponseHandler MyApache2::Dump
  </Location>

If we issue the following request:

  % echo "mod_perl rules" | POST 'http://localhost:8002/dump?foo=1&bar=2'

the response will be:

  args:
  foo=1&bar=2
  content:
  mod_perl rules

As you can see it simply dumped the query string and the posted data.

Now let's write the snooping filter:

  #file:MyApache2/FilterSnoop.pm
  #----------------------------
  package MyApache2::FilterSnoop;
  
  use strict;
  use warnings;
  
  use base qw(Apache2::Filter);
  use Apache2::FilterRec ();
  use APR::Brigade ();
  use APR::Bucket ();
  use APR::BucketType ();
  
  use Apache2::Const -compile => qw(OK DECLINED);
  use APR::Const     -compile => ':common';
  
  sub connection : FilterConnectionHandler { snoop("connection", @_) }
  sub request    : FilterRequestHandler    { snoop("request",    @_) }
  
  sub snoop {
      my $type = shift;
      my ($f, $bb, $mode, $block, $readbytes) = @_; # filter args
  
      # $mode, $block, $readbytes are passed only for input filters
      my $stream = defined $mode ? "input" : "output";
  
      # read the data and pass-through the bucket brigades unchanged
      if (defined $mode) {
          # input filter
          my $rv = $f->next->get_brigade($bb, $mode, $block, $readbytes);
          return $rv unless $rv == APR::Const::SUCCESS;
          bb_dump($type, $stream, $bb);
      }
      else {
          # output filter
          bb_dump($type, $stream, $bb);
          my $rv = $f->next->pass_brigade($bb);
          return $rv unless $rv == APR::Const::SUCCESS;
      }
  
      return Apache2::Const::OK;
  }
  
  sub bb_dump {
      my ($type, $stream, $bb) = @_;
  
      my @data;
      for (my $b = $bb->first; $b; $b = $bb->next($b)) {
          $b->read(my $bdata);
          push @data, $b->type->name, $bdata;
      }
  
      # send the sniffed info to STDERR so not to interfere with normal
      # output
      my $direction = $stream eq 'output' ? ">>>" : "<<<";
      print STDERR "\n$direction $type $stream filter\n";
  
      my $c = 1;
      while (my ($btype, $data) = splice @data, 0, 2) {
          print STDERR "    o bucket $c: $btype\n";
          print STDERR "[$data]\n";
          $c++;
      }
  }
  1;

Recall that there are two types of two filter handlers, one for connection and
another for request filtering:

  sub connection : FilterConnectionHandler { snoop("connection", @_) }
  sub request    : FilterRequestHandler    { snoop("request",    @_) }

Both handlers forward their arguments to the C<snoop()> function,
which does the real work. These two subroutines are added in order to
assign the two different attributes. In addition, the functions pass
the filter type to C<snoop()> as the first argument, which gets
shifted off C<@_>. The rest of C<@_> are the arguments that were
originally passed to the filter handler.

It's easy to know whether a filter handler is running in the input or
the output mode. Although the arguments C<$f> and C<$bb> are always
passed, the arguments C<$mode>, C<$block>, and C<$readbytes>
are passed only to input filter handlers.

If we are in input mode, in the same call we retrieve the bucket
brigade from the previous filter on the input filters stack and
immediately link it to the C<$bb> variable which makes the bucket
brigade available to the next input filter when the filter handler
returns. If we forget to perform this linking our filter will become a
black hole into which data simply disappears. Next we call
C<bb_dump()> which dumps the type of the filter and the contents of
the bucket brigade to C<STDERR>, without influencing the normal data
flow.

If we are in output mode, the C<$bb> variable already points to the
current bucket brigade. Therefore we can read the contents of the
brigade right away, and then we pass the brigade to the next filter.

Let's snoop on connection and request filter levels in both directions
by applying the following configuration:


  Listen 8008
  <VirtualHost _default_:8008>
      PerlModule MyApache2::FilterSnoop
      PerlModule MyApache2::Dump
  
      # Connection filters
      PerlInputFilterHandler  MyApache2::FilterSnoop::connection
      PerlOutputFilterHandler MyApache2::FilterSnoop::connection
  
      <Location /dump>
          SetHandler modperl
          PerlResponseHandler MyApache2::Dump
          # Request filters
          PerlInputFilterHandler  MyApache2::FilterSnoop::request
          PerlOutputFilterHandler MyApache2::FilterSnoop::request
      </Location>
  
  </VirtualHost>

Notice that we use a virtual host because we want to install
connection filters.

If we issue the following request:

  % echo "mod_perl rules" | POST 'http://localhost:8008/dump?foo=1&bar=2'

we get the same response as before we installed
C<MyApache2::FilterSnoop> because our snooping filter didn't change
anything. The output didn't change, but there was some new information
printed to the I<error_log>. We present it all here, in order to
understand how filters work.

First we can see the connection input filter at work, as it processes
the HTTP headers. We can see that for this request each header is put
into a separate brigade with a single bucket. The data is conveniently
enclosed by C<[]> so you can see the new line characters as well.

  <<< connection input filter
      o bucket 1: HEAP
  [POST /dump?foo=1&bar=2 HTTP/1.1
  ]
  
  <<< connection input filter
      o bucket 1: HEAP
  [TE: deflate,gzip;q=0.3
  ]
  
  <<< connection input filter
      o bucket 1: HEAP
  [Connection: TE, close
  ]
  
  <<< connection input filter
      o bucket 1: HEAP
  [Host: localhost:8008
  ]
  
  <<< connection input filter
      o bucket 1: HEAP
  [User-Agent: lwp-request/2.01
  ]
  
  <<< connection input filter
      o bucket 1: HEAP
  [Content-Length: 14
  ]
  
  <<< connection input filter
      o bucket 1: HEAP
  [Content-Type: application/x-www-form-urlencoded
  ]
  
  <<< connection input filter
      o bucket 1: HEAP
  [
  ]

Here the HTTP header has been terminated by a double new line. So far
all the buckets were of the I<HEAP> type, meaning that they were
allocated from the heap memory. Notice that the HTTP request input
filters will never see the bucket brigades with HTTP headers because
they are consumed by the last core connection filter.

The following two entries are generated when
C<MyApache2::Dump::handler> reads the POSTed content:

  <<< connection input filter
      o bucket 1: HEAP
  [mod_perl rules]
  
  <<< request input filter
      o bucket 1: HEAP
  [mod_perl rules]
      o bucket 2: EOS
  []

As shown earlier, the connection input filter is run before the
request input filter. Since our connection input filter was passing
the data through unmodified and no other custom connection input
filter was configured, the request input filter sees the same
data. The last bucket in the brigade received by the request input
filter is of type I<EOS>, meaning that all the input data from the
current request has been received.

Next we can see that C<MyApache2::Dump::handler> has generated its
response. However we can see that only the request output filter gets
run at this point:

  >>> request output filter
      o bucket 1: TRANSIENT
  [args:
  foo=1&bar=2
  content:
  mod_perl rules
  ]

This happens because Apache hasn't yet sent the response HTTP headers
to the client. The request filter sees a bucket brigade with a single
bucket of type I<TRANSIENT> which is allocated from the stack memory.

The moment the first bucket brigade of the response body has entered
the connection output filters, Apache injects a bucket brigade with
the HTTP headers. Therefore we can see that the connection output
filter is filtering the brigade with HTTP headers (notice that the
request output filters don't see it):

  >>> connection output filter
      o bucket 1: HEAP
  [HTTP/1.1 200 OK
  Date: Tue, 07 Mar 2006 10:59:08 GMT
  Server: Apache/2.0.55 (Unix) mod_perl/2.000002
  Perl/v5.8.4 mod_ssl/2.0.55 OpenSSL/0.9.7c DAV/2
  Connection: close
  Transfer-Encoding: chunked
  Content-Type: text/plain; charset=ISO-8859-1
  
  ]

This is followed by the first response body's brigade:

  >>> connection output filter
      o bucket 1: TRANSIENT
  [2b
  ]
      o bucket 2: TRANSIENT
  [args:
  foo=1&bar=2
  content:
  mod_perl rules
  
  ]
      o bucket 3: IMMORTAL
  [
  ]

If the response is large, the request and connection filters will
filter chunks of the response one by one. These chunks are typically
8k in size, but this size can vary.

Finally, Apache sends a series of bucket brigades to finish off the
response, including the end of stream meta-bucket to tell filters that
they shouldn't expect any more data, and flush buckets to flush the
data, to make sure that any buffered output is sent to the client:

  >>> connection output filter
      o bucket 1: IMMORTAL
  [0
  
  ]
      o bucket 2: EOS
  []
  
  >>> connection output filter
      o bucket 1: FLUSH
  []
  
  >>> connection output filter
      o bucket 1: FLUSH
  []

This module helps to illustrate that each filter handler can be called
many times during each request and connection. It is called for each
bucket brigade. Also it is important to mention that HTTP request
input filters are invoked only if there is some POSTed data to read
and it's consumed by a content handler.







=head1 Input Filters

mod_perl supports L<Connection|/Connection_Input_Filters> and L<HTTP
Request|/HTTP_Request_Input_Filters> input filters. In the following
sections we will look at each of these in turn.

=head2 Connection Input Filters

Let's say that we want to test how our handlers behave when they are
requested as C<HEAD> requests, rather than C<GET> requests. We can
alter the request headers at the incoming connection level with the
alteration transparent to all handlers.

This example's filter handler looks for data like:

  GET /perl/test.pl HTTP/1.1

and turns it into:

  HEAD /perl/test.pl HTTP/1.1

The following input filter handler does that by directly manipulating
the bucket brigades:

  #file:MyApache2/InputFilterGET2HEAD.pm
  #-----------------------------------
  package MyApache2::InputFilterGET2HEAD;
  
  use strict;
  use warnings;
  
  use base qw(Apache2::Filter);
  
  use APR::Brigade ();
  use APR::Bucket ();
  
  use Apache2::Const -compile => qw(OK DECLINED);
  use APR::Const     -compile => ':common';
  
  sub handler : FilterConnectionHandler {
      my ($f, $bb, $mode, $block, $readbytes) = @_;
  
      return Apache2::Const::DECLINED if $f->ctx;
  
      my $rv = $f->next->get_brigade($bb, $mode, $block, $readbytes);
      return $rv unless $rv == APR::Const::SUCCESS;
  
      for (my $b = $bb->first; $b; $b = $bb->next($b)) {
          $b->read(my $data);
          warn("data: $data\n");
  
          if ($data and $data =~ s|^GET|HEAD|) {
              my $nb = APR::Bucket->new($bb->bucket_alloc, $data);
              $b->insert_after($nb);
              $b->remove; # no longer needed
              $f->ctx(1); # flag that that we have done the job
              last;
          }
      }
  
      return Apache2::Const::OK;
  }
  1;

The filter handler is called for each bucket brigade, which then
includes buckets with data. The gist of any input filter handler is to
request the bucket brigade from the upstream filter, and return it to
the downstream filter using the second argument C<$bb>. It's important
to remember that you can call methods on this argument, but you
shouldn't assign to this argument, or the chain will be broken.

There are two
techniques to choose from to retrieve-modify-return bucket brigades:

=over

=item 1

Create a new empty bucket brigade C<$ctx_bb>, pass it to the upstream
filter via C<get_brigade()> and wait for this call to return. When it
returns, C<$ctx_bb> will be populated with buckets. Now the filter
should move the bucket from C<$ctx_bb> to C<$bb>, on the way modifying
the buckets if needed. Once the buckets are moved, and the filter
returns, the downstream filter will receive the populated bucket
brigade.

=item 2

Pass C<$bb> to the upstream filter using C<get_brigade()> so it will be
populated with buckets. Once C<get_brigade()> returns, the filter can
go through the buckets and modify them in place, or it can do nothing
and just return (in which case, the downstream filter will receive the
bucket brigade unmodified).

=back

Both techniques allow addition and removal of buckets. Though the
second technique is more efficient since it doesn't have the overhead
of create the new brigade and moving the bucket from one brigade to
another. In this example we have chosen to use the second technique,
in the next example we will see the first technique.

Our filter has to perform the substitution of only one HTTP header
(which normally resides in one bucket), so we have to make sure that
no other data gets mangled (e.g. there could be POSTED data and it may
match C</^GET/> in one of the buckets). We use
C<L<$f-E<gt>ctx|docs::2.0::api::Apache2::Filter/C_ctx_>> as a flag
here. When it's undefined the filter knows that it hasn't done the
required substitution, though once it completes the job it sets the
context to 1.

Using the information stored in the context, the filter can
immediately return C<Apache2::Const::DECLINED> when it's invoked after
the substitution job has been done:

    return Apache2::Const::DECLINED if $f->ctx;

In that case mod_perl will call C<get_brigade()> internally which will
pass the bucket brigade to the downstream filter. Alternatively the
filter could do:

    my $rv = $f->next->get_brigade($bb, $mode, $block, $readbytes);
    return $rv unless $rv == APR::Const::SUCCESS;
    return Apache2::Const::OK if $f->ctx;

but this is a bit less efficient.

[META: the most efficient thing to do is to remove the filter itself
once the job is done, so it won't be even invoked after the job has
been done.

  if ($f->ctx) {
      $f->remove;
      return Apache2::Const::DECLINED;
  }

However, this can't be used with Apache 2.0.49 and lower, since it has
a bug when trying to remove the edge connection filter (it doesn't
remove it). Most likely that problem will be not fixed in the 2.0
series due to design flows. I don't know if it's going to be fixed in
2.1 series.]

If the job wasn't done yet, the filter calls C<get_brigade>, which
populates the C<$bb> bucket brigade. Next, the filter steps through
the buckets looking for the bucket that matches the regex:
C</^GET/>. If a match is found, a new bucket is created with the
modified data (C<s/^GET/HEAD/>. Now it has to be inserted in place of
the old bucket. In our example we insert the new bucket after the
bucket that we have just modified and immediately remove that bucket
that we don't need anymore:

            $b->insert_after($nb);
            $b->remove; # no longer needed

Finally we set the context to 1, so we know not to apply the
substitution on the following data, and break from the I<for> loop.

The handler returns C<Apache2::Const::OK> indicating that everything
was fine. The downstream filter will receive the bucket brigade with
one bucket modified.

Now let's check that the handler works properly. For example, consider
the following response handler:

  #file:MyApache2/RequestType.pm
  #---------------------------
  package MyApache2::RequestType;
  
  use strict;
  use warnings;
  
  use Apache2::RequestIO ();
  use Apache2::RequestRec ();
  use Apache2::Response ();
  
  use Apache2::Const -compile => 'OK';
  
  sub handler {
      my $r = shift;
  
      $r->content_type('text/plain');
      my $response = "the request type was " . $r->method;
      $r->set_content_length(length $response);
      $r->print($response);
  
      return Apache2::Const::OK;
  }
  
  1;

This handler returns to the client the request type it has issued. For
a C<HEAD> request Apache will discard the response body, but it will
still set the correct C<Content-Length> header, which will be 24 for a
C<GET> request and 25 for a C<HEAD> request. Therefore, if this
response handler is configured as:

  Listen 8005
  <VirtualHost _default_:8005>
      <Location />
          SetHandler modperl
          PerlResponseHandler +MyApache2::RequestType
      </Location>
  </VirtualHost>

and a C<GET> request is issued to I</>:

  panic% perl -MLWP::UserAgent -le \
  '$r = LWP::UserAgent->new()->get("http://localhost:8005/"); \
  print $r->headers->content_length . ": ".  $r->content'
  24: the request type was GET

the response body will be:

  the request type was GET

and the C<Content-Length> header will be set to 24. This is what we
would expect since the request was processed normally. However, if we
enable the C<MyApache2::InputFilterGET2HEAD> input connection filter:

  Listen 8005
  <VirtualHost _default_:8005>
      PerlInputFilterHandler +MyApache2::InputFilterGET2HEAD
  
      <Location />
          SetHandler modperl
          PerlResponseHandler +MyApache2::RequestType
      </Location>
  </VirtualHost>

and issue the same C<GET> request, we get only:

  25: 

This means the body was discarded by Apache, because our filter turned
the C<GET> request into a C<HEAD> request. If Apache wasn't discarding
the body on C<HEAD>, the response would be:

  the request type was HEAD

That's why the content length is reported as 25 and not 24 as in the
real GET request. So the content length of 25 and lack of body text in
the response confirm that our filter is acting as we expected.


=head2 HTTP Request Input Filters

Request filters are really non-different from connection filters,
other than that they are working on request and response bodies and
have an access to a request object.

=head2 Bucket Brigade-based Input Filters

As we have seen, filters can be either bucket brigade based, or stream
oriented. Here we look at a request input filter that lowercases the
request's body by directly manipulating the bucket brigade:
C<MyApache2::InputRequestFilterLC>.

  #file:MyApache2/InputRequestFilterLC.pm
  #-------------------------------------
  package MyApache2::InputRequestFilterLC;
  
  use strict;
  use warnings;
  
  use base qw(Apache2::Filter);
  
  use Apache2::Connection ();
  use APR::Brigade ();
  use APR::Bucket ();
  
  use Apache2::Const -compile => 'OK';
  use APR::Const     -compile => ':common';
  
  sub handler : FilterRequestHandler {
      my ($f, $bb, $mode, $block, $readbytes) = @_;
  
      my $c = $f->c;
      my $bb_ctx = APR::Brigade->new($c->pool, $c->bucket_alloc);
      my $rv = $f->next->get_brigade($bb_ctx, $mode, $block, $readbytes);
      return $rv unless $rv == APR::Const::SUCCESS;
  
      while (!$bb_ctx->is_empty) {
          my $b = $bb_ctx->first;
  
          if ($b->is_eos) {
              $bb->insert_tail($b);
              last;
          }
  
          my $len = $b->read(my $data);
          $b = APR::Bucket->new($bb->bucket_alloc, lc $data) if $len;
  
          $b->remove;
          $bb->insert_tail($b);
      }
  
      return Apache2::Const::OK;
  }
  
  1;

As promised, in this filter handler we have used the first technique
of bucket brigade modification. The handler creates a temporary bucket
brigade (C<ctx_bb>), populates it with data using C<get_brigade()>,
and then moves buckets from it to the bucket brigade C<$bb>. This
bucket brigade is then retrieved by the downstream filter when our
handler returns.

This filter doesn't need to know whether it was invoked for the first
time or whether it has already done something. It's a stateless
handler, since it has to lower case everything that passes through
it. Notice that this filter can't be used as the connection filter for
HTTP requests, since it will invalidate the incoming request headers.
For example the first header line:

  GET /perl/TEST.pl HTTP/1.1

becomes:

  get /perl/test.pl http/1.1

which invalidates the request method, the URL and the protocol.

To test, we can use the C<MyApache2::Dump> response handler, presented
earlier, which dumps the query string and the content body as a
response. Configure the server as follows:

  <Location /lc_input>
      SetHandler modperl
      PerlResponseHandler    +MyApache2::Dump
      PerlInputFilterHandler +MyApache2::InputRequestFilterLC
  </Location>

Now when issuing a POST request:

 % echo "mOd_pErl RuLeS" | POST 'http://localhost:8002/lc_input?FoO=1&BAR=2'

we get a response:

  args:
  FoO=1&BAR=2
  content:
  mod_perl rules

We can see that our filter has lowercased the POSTed body before the
content handler received it. And you can see that the query string
wasn't changed.

We have devoted so much attention to bucket brigade filters, even
though they are simple to manipulate, because it is important to
understand how the filters work underneath.  This understanding is
essential when you need to debug filters or to optimize them. There
are cases when a bucket brigade filter may be more efficient than the
stream-oriented version. For example if the filter applies a
transformation to selected buckets, certain buckets may contain open
filehandles or pipes, rather than real data. When you call C<read()>,
as shown above, the buckets will be forced to read that data in. But
if you didn't want to modify these buckets you could pass them as they
are and let Apache perform faster techniques for sending data from the
file handles or pipes.

N< The call to $b-E<gt>read(), or any other operation that internally
forces the bucket to read the information into the memory (like the
length() op), makes the data handling less efficient because it
creates more work.  Therefore care should be taken so not to read the
data in unless it's really necessary, and sometimes you can gain this
efficiency only by working with the bucket brigades.>


=head2 Stream-oriented Input Filters

Let's now look at the same filter implemented using the
stream-oriented API.

  #file:MyApache2/InputRequestFilterLC2.pm
  #-------------------------------------
  package MyApache2::InputRequestFilterLC2;
  
  use strict;
  use warnings;
  
  use base qw(Apache2::Filter);
  
  use Apache2::Const -compile => 'OK';
  
  use constant BUFF_LEN => 1024;
  
  sub handler : FilterRequestHandler {
      my $f = shift;
  
      while ($f->read(my $buffer, BUFF_LEN)) {
          $f->print(lc $buffer);
      }
  
      Apache2::Const::OK;
  }
  1;


The logic is very simple here. The filter reads in a loop and prints
the modified data, which at some point will be sent to the next
filter. The data transmission is triggered every time the internal
mod_perl buffer is filled or when the filter returns.

C<read()> populates C<$buffer> to a maximum of C<BUFF_LEN> characters
(1024 in our example). Assuming that the current bucket brigade
contains 2050 chars, C<read()> will get the first 1024 characters,
then 1024 characters more and finally the remaining 2 characters. Note
that even though the response handler may have sent more than 2050
characters, every filter invocation operates on a single bucket
brigade so you have to wait for the next invocation to get more
input. Earlier we showed that you can force the generation of several
bucket brigades in the content handler by using C<rflush()>. For
example:

  $r->print("string");
  $r->rflush();
  $r->print("another string");

It's only possible to get more than one bucket brigade from the same
filter handler invocation if the filter is not using the streaming
interface. In that case you can call C<get_brigade()> as many times as
needed or until EOS is received.

The configuration section is nearly identical for the two types of
filters:

 <Location /lc_input2>
      SetHandler modperl
      PerlResponseHandler    +MyApache2::Dump
      PerlInputFilterHandler +MyApache2::InputRequestFilterLC2
  </Location>

When issuing a POST request:

 % echo "mOd_pErl RuLeS" | POST 'http://localhost:8002/lc_input2?FoO=1&BAR=2'

we get the response:

  args:
  FoO=1&BAR=2
  content:
  mod_perl rules

As before, we see that our filter has lowercased the POSTed body
before the content handler received it and the query string wasn't
changed.

=head1 Output Filters


As discussed above in the section L<HTTP Request vs. Connection
Filters>, mod_perl supports L<Connection|/Connection_Output_Filters>
and L<HTTP Request|/HTTP_Request_Output_Filters> output filters. In
the following sections we will look at each of these in turn.


mod_perl supports L<Connection|/Connection_Output_Filters> and L<HTTP
Request|/HTTP_Request_Output_Filters> output filters. The differences
between connection filters and HTTP request filters are described
above in the section L<HTTP Request vs. Connection Filters>.

=head2 Connection Output Filters

Connection filters filter B<all> the data that is going through the
server. Therefore if the connection is of the HTTP request type,
connection output filters see the headers and the body of the
response, whereas request output filters see only the response body.

META: for now see the request output filter explanations and examples,
connection output filter examples will be added soon. Interesting
ideas for such filters are welcome (possible ideas: mangling output
headers for HTTP requests, pretty much anything for protocol modules).


=head2 HTTP Request Output Filters

As mentioned earlier, output filters can be written using the bucket
brigades manipulation or the simplified stream-oriented interface.
This section will show examples of both.

In order to generate output that can be manipulated by the two types
of output filters, we will first develop a response handler that sends
two lines of output: numerals 1234567890 and the English alphabet in a
single string:

  #file:MyApache2/SendAlphaNum.pm
  #-------------------------------
  package MyApache2::SendAlphaNum;
  
  use strict;
  use warnings;
  
  use Apache2::RequestRec ();
  use Apache2::RequestIO ();
  
  use Apache2::Const -compile => qw(OK);
  
  sub handler {
      my $r = shift;
  
      $r->content_type('text/plain');
  
      $r->print(1..9, "0\n");
      $r->print('a'..'z', "\n");
  
      return Apache2::Const::OK;
  }
  1;

In the examples below, we'll create a filter handler to reverse every
line of the response body, preserving the new line characters in their
places. Since we want to reverse characters only in the response body,
without breaking the HTTP headers, we will use the HTTP request output
filter rather than a connection output filter.


=head3 Stream-oriented Output Filters

The first filter implementation uses the stream-oriented filtering
API:

  #file:MyApache2/FilterReverse1.pm
  #----------------------------
  package MyApache2::FilterReverse1;
  
  use strict;
  use warnings;
  
  use base qw(Apache2::Filter);
  
  use Apache2::Const -compile => qw(OK);
  
  use constant BUFF_LEN => 1024;
  
  sub handler : FilterRequestHandler {
      my $f = shift;
  
      while ($f->read(my $buffer, BUFF_LEN)) {
          for (split "\n", $buffer) {
              $f->print(scalar reverse $_);
              $f->print("\n");
          }
      }
  
      return Apache2::Const::OK;
  }
  1;

Next, we add the following configuration to I<httpd.conf>:

  PerlModule MyApache2::FilterReverse1
  PerlModule MyApache2::SendAlphaNum
  <Location /reverse1>
      SetHandler modperl
      PerlResponseHandler     MyApache2::SendAlphaNum
      PerlOutputFilterHandler MyApache2::FilterReverse1
  </Location>

Now when a request to I</reverse1> is sent, the response handler
C<MyApache2::SendAlphaNum::handler()> sends:

  1234567890
  abcdefghijklmnopqrstuvwxyz

as a response. The output filter handler
C<MyApache2::FilterReverse1::handler> reverses the lines, so the client
gets:

  0987654321
  zyxwvutsrqponmlkjihgfedcba

The C<Apache2::Filter> module loads the C<read()> and C<print()>
methods which encapsulate the stream-oriented filtering interface.

The reversing filter is quite simple: in the loop it reads the data in
the I<readline()> mode in chunks up to the buffer length (1024 in our
example), and then prints each line reversed while preserving the new
line control characters at the end of each line.  Behind the scenes
C<$f-E<gt>read()> retrieves the incoming brigade and gets the data
from it, and C<$f-E<gt>print()> appends to the new brigade which is
then sent to the next filter in the stack. C<read()> breaks the
I<while> loop when the brigade is emptied or the end of stream is
received.

While this code is simple and easy to explain, there are cases it
won't handle correctly. For example, it will have problems if the
input lines are longer than 1,024 characters. It also doesn't account
for the different line terminators on different platforms (e.g., "\n",
"\r", or "\r\n").  Moreover a single line may be split across two or
even more bucket brigades, so we have to store the unprocessed string
in the filter context so it can be used on the following invocations.
Below is an example of a more complete handler, which takes care of
these issues:

  sub handler {
      my $f = shift;
  
      my $leftover = $f->ctx;
      while ($f->read(my $buffer, BUFF_LEN)) {
          $buffer = $leftover . $buffer if defined $leftover;
          $leftover = undef;
          while ($buffer =~ /([^\r\n]*)([\r\n]*)/g) {
              $leftover = $1, last unless $2;
              $f->print(scalar(reverse $1), $2);
          }
      }
  
      if ($f->seen_eos) {
          $f->print(scalar reverse $leftover) if defined $leftover;
      }
      else {
          $f->ctx($leftover) if defined $leftover;
      }
  
      return Apache2::Const::OK;
  }

The handler uses the C<$leftover> variable to store unprocessed data
as long as it fails to assemble a complete line or there is an
incomplete line following the new line token. On the next handler
invocation this data is then prepended to the next chunk that is
read. When the filter is invoked for the last time, signaled by the
C<$f-E<gt>seen_eos> method, it unconditionally reverses and sends the
data down the stream, which is then flushed down to the client.


=head3 Bucket Brigade-based Output Filters

The following filter implementation uses the bucket brigades API
to accomplish exactly the same task as the first filter.

  #file:MyApache2/FilterReverse2.pm
  #--------------------------------
  package MyApache2::FilterReverse2;
  
  use strict;
  use warnings;
  
  use base qw(Apache2::Filter);
  
  use APR::Brigade ();
  use APR::Bucket ();
  
  use Apache2::Const -compile => 'OK';
  use APR::Const     -compile => ':common';
  
  sub handler : FilterRequestHandler {
      my ($f, $bb) = @_;
  
      my $bb_ctx = APR::Brigade->new($f->c->pool, $f->c->bucket_alloc);
  
      while (!$bb->is_empty) {
          my $b = $bb->first;
  
          $b->remove;
  
          if ($b->is_eos) {
              $bb_ctx->insert_tail($b);
              last;
          }
  
          if ($b->read(my $data)) {
              $data = join "",
                  map {scalar(reverse $_), "\n"} split "\n", $data;
              $b = APR::Bucket->new($bb->bucket_alloc, $data);
          }
  
          $bb_ctx->insert_tail($b);
      }
  
      my $rv = $f->next->pass_brigade($bb_ctx);
      return $rv unless $rv == APR::Const::SUCCESS;
  
      return Apache2::Const::OK;
  }
  1;

Below is the corresponding configuration for I<httpd.conf>:

  PerlModule MyApache2::FilterReverse2
  PerlModule MyApache2::SendAlphaNum
  <Location /reverse2>
      SetHandler modperl
      PerlResponseHandler     MyApache2::SendAlphaNum
      PerlOutputFilterHandler MyApache2::FilterReverse2
  </Location>

Now when a request to I</reverse2> is made, the client gets:

  0987654321
  zyxwvutsrqponmlkjihgfedcba

as expected.

The bucket brigades output filter version is just a bit more
complicated than the stream-oriented one. The handler receives the
incoming bucket brigade C<$bb> as its second argument. When the
handler finishes it must pass a brigade to the next filter in the
stack, so we create a new bucket brigade into which we put the
modified buckets and which eventually we pass to the next filter.

The core of the handler removes buckets from the head of the bucket
brigade C<$bb>, while buckets are available, reads the data from the
buckets, then reverses and puts the data into a newly created bucket.
The new bucket is then inserted on the end of the new bucket brigade
using the C<insert_tail()> method. If we see a bucket which designates
the end of stream, we insert that bucket on the tail of the new bucket
brigade and break the loop. Finally we pass the created brigade with
modified data to the next filter and return.

Similar to the original version of
C<MyApache2::FilterReverse1::handler>, this filter is not smart enough
to handle incomplete lines. The filter could be made more foolproof by
building a better matching rule, and using the C<$leftover> buffer as
demonstrated in the previous section.  This left as an exercise for
the reader.





=head1 Filter Applications

The following sections provide various filter applications and their
implementation.

=head2 Handling Data Underruns

Sometimes filters need to read at least N bytes before they can apply
their transformation. It's quite possible that reading one bucket
brigade is not enough, but that two or more are needed. This situation
is sometimes referred to as an I<underrun>.

Let's take an input filter as an example.  When the filter realizes
that it doesn't have enough data in the current bucket brigade, it can
store the read data in the filter context, and wait for the next
invocation of itself, which may or may not satisfy its needs. While it
is gathering the data from the bucket brigades, it must return an
empty bucket brigade to the upstream input filter. However, this is
not the most efficient technique to resolve underruns.

Instead of returning an empty bucket brigade, the input filter can
request extra bucket brigades until the underrun condition gets
resolved. Note that this solution is transparent to any filters before
or after the current filter.

Consider this HTTP request:

  % perl -MLWP::UserAgent -le ' \
    $r = LWP::UserAgent->new()->post("http://localhost:8011/", \
         [content => "x" x (40 * 1024 + 7)]); \
    print $r->is_success ? $r->content : "failed: " . $r->code'
  read 40975 chars

This client POSTs just a little bit more than 40kb of data to the
server. Normally Apache splits incoming POSTed data into 8kb chunks,
putting each chunk into a separate bucket brigade. Therefore we expect
to get 5 brigades of 8kb, and one brigade with just a few bytes (a
total of 6 bucket brigades).

Now let's assume our example filter needs to have 1024*16 + 5 bytes to
have a complete token before it can start its processing. The extra 5
bytes are just so we don't perfectly fit into 8kb bucket brigades,
making the example closer to real situations. Having 40,975 bytes of
input and a token size of 16,389 bytes, we will have 2 full tokens and
a remainder of 8,197 bytes.

Before showing any code, let's look at the filter debug output to
better explain what we expect to happen:


  filter called
  asking for a bb
  asking for a bb
  asking for a bb
  storing the remainder: 7611 bytes
  
  filter called
  asking for a bb
  asking for a bb
  storing the remainder: 7222 bytes
 
  filter called
  asking for a bb
  seen eos, flushing the remaining: 8197 bytes

We can see that the filter was invoked three times. The first time it
has consumed three bucket brigades, collecting one full token of
16,389 bytes with a remainder of 7,611 bytes to be processed on the
next invocation. The second time it needed only two more bucket
brigades and this time, after completing the second token, 7,222 bytes
remained. Finally on the third invocation it consumed the last bucket
brigade for a total of six, just as we expected. However, it didn't
have enough for the third token and since EOS has been seen (no more
data expected), it has flushed the remaining 8,197 bytes as we
calculated earlier.

It is clear from the debugging output that the filter was invoked only
three times, instead of six times (there were six bucket
brigades). Notice that the upstream input filter, if there is one,
isn't aware that there were six bucket brigades, since it saw only
three. Our example filter didn't do much with those tokens, so it has
only repackaged data from 8kb per bucket brigade, to 16,389 bytes per
bucket brigade. But of course in a real implementation some
transformation would be applied on these tokens.

Now let's look at the implementation details.  First let's look at the
C<response()> handler, which is the first part of the module:

  #file:MyApache2/Underrun.pm
  #-------------------------
  package MyApache2::Underrun;
  
  use strict;
  use warnings;
  
  use constant IOBUFSIZE => 8192;
  
  use Apache2::Const -compile => qw(MODE_READBYTES OK M_POST);
  use APR::Const     -compile => qw(SUCCESS BLOCK_READ);
  
  sub response {
      my $r = shift;
  
      $r->content_type('text/plain');
  
      if ($r->method_number == Apache2::Const::M_POST) {
          my $data = read_post($r);
          #warn "HANDLER READ: $data\n";
          my $length = length $data;
          $r->print("read $length chars");
      }
  
      return Apache2::Const::OK;
  }
  
  sub read_post {
      my $r = shift;
  
      my $bb = APR::Brigade->new($r->pool, $r->connection->bucket_alloc);
  
      my $data = '';
      my $seen_eos = 0;
      do {
          $r->input_filters->get_brigade($bb, Apache2::Const::MODE_READBYTES,
                                         APR::Const::BLOCK_READ, IOBUFSIZE);
  
          for (my $b = $bb->first; $b; $b = $bb->next($b)) {
              if ($b->is_eos) {
                  $seen_eos++;
                  last;
              }
  
              if ($b->read(my $buf)) {
                  $data .= $buf;
              }
  
              $b->remove; # optimization to reuse memory
          }
      } while (!$seen_eos);
  
      $bb->destroy;
  
      return $data;
  }

The C<response()> handler is trivial -- it reads the POSTed data and
prints how many bytes it has read. C<read_post()> sucks in all POSTed
data without parsing it.

Now comes the filter (which lives in the same package):

  #file:MyApache2/Underrun.pm (continued)
  #-------------------------------------
  use Apache2::Filter ();
  
  use Apache2::Const -compile => qw(OK M_POST);
  
  use constant TOKEN_SIZE => 1024*16 + 5; # ~16k
  
  sub filter {
      my ($f, $bb, $mode, $block, $readbytes) = @_;
      my $ba = $f->r->connection->bucket_alloc;
      my $ctx = $f->ctx;
      my $buffer = defined $ctx ? $ctx : '';
      $ctx = '';  # reset
      my $seen_eos = 0;
      my $data;
      warn "\nfilter called\n";
  
      # fetch and consume bucket brigades until we have at least TOKEN_SIZE
      # bytes to work with
      do {
          my $tbb = APR::Brigade->new($f->r->pool, $ba);
          my $rv = $f->next->get_brigade($tbb, $mode, $block, $readbytes);
          warn "asking for a bb\n";
          ($data, $seen_eos) = flatten_bb($tbb);
          $tbb->destroy;
          $buffer .= $data;
      } while (!$seen_eos && length($buffer) < TOKEN_SIZE);
  
      # now create a bucket per chunk of TOKEN_SIZE size and put the remainder
      # in ctx
      for (split_buffer($buffer)) {
          if (length($_) == TOKEN_SIZE) {
              $bb->insert_tail(APR::Bucket->new($ba, $_));
          }
          else {
              $ctx .= $_;
          }
      }
  
      my $len = length($ctx);
      if ($seen_eos) {
          # flush the remainder
          $bb->insert_tail(APR::Bucket->new($ba, $ctx));
          $bb->insert_tail(APR::Bucket::eos_create($ba));
          warn "seen eos, flushing the remaining: $len bytes\n";
      }
      else {
          # will re-use the remainder on the next invocation
          $f->ctx($ctx);
          warn "storing the remainder: $len bytes\n";
      }
  
      return Apache2::Const::OK;
  }
  
  # split a string into tokens of TOKEN_SIZE bytes and a remainder
  sub split_buffer {
      my $buffer = shift;
      if ($] < 5.007) {
          my @tokens = $buffer =~ /(.{@{[TOKEN_SIZE]}}|.+)/g;
          return @tokens;
      }
      else {
          # available only since 5.7.x+
          return unpack "(A" . TOKEN_SIZE . ")*", $buffer;
      }
  }
  
  sub flatten_bb {
      my ($bb) = shift;
  
      my $seen_eos = 0;
  
      my @data;
      for (my $b = $bb->first; $b; $b = $bb->next($b)) {
          $seen_eos++, last if $b->is_eos;
          $b->read(my $bdata);
          push @data, $bdata;
      }
      return (join('', @data), $seen_eos);
  }
  
  1;

The filter calls C<get_brigade()> in a do-while loop until it reads
enough data or sees EOS. Notice that it may get underruns several
times, and then suddenly receive a lot of data at once, which will be
enough for more than one minimal size token, so we have to take this
into an account. Once the underrun condition is satisfied (we have at
least one complete token) the tokens are put into a bucket brigade and
returned to the upstream filter for processing, keeping any remainders
in the filter context for the next invocations or flushing all the
remaining data if EOS is seen.

Note that this example cannot be implemented with streaming filters
because each invocation gives the filter exactly one bucket brigade to
work with.  The streaming interface does not currently provide a
facility to fetch extra brigades.

Here is the Apache configuration for this example:

  PerlModule MyApache2::Underrun
  <Location />
    PerlInputFilterHandler MyApache2::Underrun::filter
    SetHandler modperl
    PerlResponseHandler MyApache2::Underrun::response
  </Location>




=head2 Setting the Content-Length Header in Request Output Filters

Earlier we have stated that a filter that modifies the content's
length must unset the Content-Length HTTP header. However sometimes
it's desirable to have this header set, for example when dealing with
proxies.

Since the headers are sent before the data, all the data must first be
buffered and processed. You cannot accomplish this task with the
streaming filter API since it passes FLUSH buckets through. As soon as
the FLUSH bucket is received by the core filter that sends the
headers, it generates the headers and sends those out. Therefore the
bucket brigade API must be used here to have a complete control over
what's going through. Here is a possible implementation:

  #file:MyApache2/FilterChangeLength.pm
  #-------------------------------------
  package MyApache2::FilterChangeLength;
  
  use strict;
  use warnings FATAL => 'all';
  
  use Apache2::RequestRec ();
  
  use APR::Table ();
  use APR::Bucket ();
  use APR::Brigade ();
  
  use base qw(Apache2::Filter);
  
  use Apache2::Const -compile => qw(OK);
  use APR::Const     -compile => ':common';
  
  sub handler {
      my ($filter, $bb) = @_;
  
      my $ctx = $filter->ctx;
  
      # no need to unset the C-L header, since this filter makes sure to
      # correct it before any headers go out.
      #unless ($ctx) {
      #    $filter->r->headers_out->unset('Content-Length');
      #}
  
      my $data = exists $ctx->{data} ? $ctx->{data} : '';
      $ctx->{invoked}++;
      my ($bdata, $seen_eos) = flatten_bb($bb);
      $bdata =~ s/-//g;
      $data .= $bdata if $bdata;
  
      if ($seen_eos) {
          my $len = length $data;
          $filter->r->headers_out->set('Content-Length', $len);
          $filter->print($data) if $data;
      }
      else {
          # store context for all but the last invocation
          $ctx->{data} = $data;
          $filter->ctx($ctx);
      }
  
      return Apache2::Const::OK;
  }
  
  sub flatten_bb {
      my ($bb) = shift;
  
      my $seen_eos = 0;
  
      my @data;
      for (my $b = $bb->first; $b; $b = $bb->next($b)) {
          $seen_eos++, last if $b->is_eos;
          $b->read(my $bdata);
          push @data, $bdata;
      }
      return (join('', @data), $seen_eos);
  }
  
  1;

In this module we use flatten_bb() to read the data from the buckets
and signal when the EOS is received. The filter simply collects the
data, storing it in the filter context. When it receives EOS it sets
the C<Content-Length> header and sends the data out.

The configuration is straightforward:

  PerlOutputFilterHandler MyApache2::FilterChangeLength




=head1 Filter Tips and Tricks

Various tips to use in filters.

=head2 Altering the Content-Type Response Header

Let's say that you want to modify the C<Content-Type> header in the
request output filter:

  sub handler : FilterRequestHandler {
      my $f = shift;
      ...
      $f->r->content_type("text/html; charset=$charset");
      ...

Request filters have an access to the request object, so we simply
modify it.



=head1 Writing Well-Behaving Filters

Filter writers must follow the following rules:

=head2 Connection Filters over KeepAlive Connections

Whenever a new HTTP request is processed, request filters get their
context (C<L<$f-E<gt>ctx|docs::2.0::api::Apache2::Filter/C_ctx_>>)
reset. This is also true for the connection filter context, as long as
the connection is not a
C<L<keepalive|docs::2.0::api::Apache2::Connection/C_keepalive_>>)
connection. When the connection is kept alive, there could be many
requests processed during a single connection and the same filter
context will persist through all of them, until the maximum number of
KeepAlive requests over the same connection is reached or until the
client breaks the connection.

Sometimes it's desirable to reset the whole context or parts of it
before a HTTP request is processed. For example
C<Apache2::Filter::HTTPHeadersFixup> needs to know when it should
start and stop processing HTTP headers. It keeps the state in the
filter's context. The problem is that whenever a new HTTP request is
coming in, it needs to be able to reset the state machine. If it
doesn't, it will process the HTTP headers of the first request and
miss the rest of the requests.

So let's say we have a hypothetical module
C<MyApache2::Filter::StateMachine> which implements an input
connection filter and it processes incoming data as long as the
I<state> flag is down. Once that flag goes up, the filter switches to
the pass-through-unmodified mode. Here is a skeleton of the module:

  #file:MyApache2/Filter/StateMachine.pm
  #------------------------------------
  package MyApache2::Filter::StateMachine;
  
  use base qw(Apache2::Filter);
  use Apache2::Connection ();
  
  use Apache2::Const -compile => qw(OK DECLINED CONN_KEEPALIVE);
  
  sub handler : FilterConnectionHandler {
      my ($f, $bb, $mode, $block, $readbytes) = @_;
  
      my $ctx = context($f);
  
      # pass through unmodified
      return Apache2::Const::DECLINED if $ctx->{state};
  
      # get data, do some processing, send it out
      process(); # your code comes here
  
      # change the state if some condition is reached
      $ctx->{state}++ if $done_condition;
  
      return Apache2::Const::OK;
  }
  
  sub context {
      my ($f) = shift;
  
      my $ctx = $f->ctx;
      unless ($ctx) {
          $ctx = {
              state => 0,
          };
  
          $f->ctx($ctx);
      }
  
      return $ctx;
  }
  1;

To make this module work properly over KeepAlive connections, we want
to reset the I<state> flag at the very beginning of the new
request. To accomplish this, all we need to do is to change the
C<context> wrapper to be:

  sub context {
      my ($f) = shift;
  
      my $ctx = $f->ctx;
      unless ($ctx) {
          $ctx = {
              state       => 0,
              keepalives  => $f->c->keepalives,
          };
  
          $f->ctx($ctx);
          return $ctx;
      }
  
      my $c = $f->c;
      if ($c->keepalive == Apache2::Const::CONN_KEEPALIVE &&
          $ctx->{state} && $c->keepalives > $ctx->{keepalives}) {
  
          $ctx->{state}      = 0;
          $ctx->{keepalives} = $c->keepalives;
      }
  
      return $ctx;
  }

The only difference from the previous implementation is that we
maintain one more state, which stores the number of requests served
over the current connection. When Apache reports more served requests
than we have in the context that means that we have a new request
coming in. So we reset the I<state> flag and store the new value of
the served connections.

For a more complete real-world implementation, see:
http://search.cpan.org/dist/Apache-Filter-HTTPHeadersFixup/





=head2 Adjusting HTTP Headers

The following information is relevant for HTTP filters

=over

=item * Unsetting the Content-Length header

HTTP response filters modifying the length of the body they process
must unset the C<Content-Length> header. For example, a compression
filter modifies the body length, whereas a lowercasing filter doesn't;
therefore the former has to unset the header, and the latter doesn't
have to.

The header must be unset before any output is sent from the filter. If
this rule is not followed, an HTTP response header with incorrect
C<Content-Length> value might be sent.

Since you want to run this code once during the multiple filter
invocations, use the C<ctx()> method to set the flag:

  unless ($f->ctx) {
      $f->r->headers_out->unset('Content-Length');
      $f->ctx(1);
  }

=item *

META: Same goes for last-modified/etags, which may need to be unset,
"vary" might need to be added if you want caching to work properly
(depending on what your filter does.


=back

=head2 Other issues

META: to be written. Meanwhile collecting important inputs from
various sources.

[

If a filter desires to store the incoming buckets for post
processing. It must check whether the bucket type is transient. If it
is -- the data must be copied away. If not -- the buckets may contain
corrupted data when used later. The right thing is accomplished
transparently by apr_bucket_setaside, for which we need to provide a
perl glue.

]


[

This one will be expanded by Geoff at some point:

HTTP output filter developers are ought to handle conditional GETs
properly... (mostly for the reason of efficiency?)

]

[

talk about issues like not losing meta-buckets. e.g. if the filter
runs a switch statement and propagates buckets types that were known
at the time of writing, it may drop buckets of new types which may be
added later, so it's important to ensure that there is a default cause
where the bucket is passed as is.

of course mention the fact where things like EOS buckets must be
passed, or the whole chain will be broken. Or if some filter decides
to inject an EOS bucket by itself, it should probably consume and
destroy the rest of the incoming bb. need to check on this issue.

]



[

Need to document somewhere (concepts?) that the buckets should never
be modified directly, because the filter can't know ho else could be
referencing it at the same time.  (shared mem/cache/memory mapped
files are examples on where you don't want to modify the
data). Instead the data should be moved into a new bucket.

Also it looks like we need to $b-E<gt>destroy (need to add the API) in
addition to $b-E<gt>remove. Which can be done in one stroke using
$b-E<gt>delete (need to add the API).

]

[

Mention mod_bucketeer as filter debugging tool (in addition to FilterSnoop)

]

=head1 Writing Efficient Filters

As of this writing, the Apache network input filter reads in 8000B
chunks (not 8192B) and makes each bucket 8000B in size. Based on this,
the most efficient reading technique is:

  use constant BUFF_LEN => 8000;
  while ($f->read(my $buffer, BUFF_LEN)) {
      # manip $buffer
      $f->print($buffer);
  }

however if there is some filter in between, it may change the size of
the buckets. Also this number may change in the future.

Hmm, I've also seen it read in 7819 chunks. I suppose this is not very
reliable. But it's probably a good idea to ask at least 8k, so if a
bucket brigade has < 8k, nothing will need to be stored in the
internal buffer. i.e. C<read()> will return less than asked for.

]

[

Bucket Brigades are used to make the data flow between filters and
handlers more efficient. e.g. a file handle can be put in a bucket and
the read from the file can be postponed to the very moment when the
data is sent to the client, thus saving a lot of memory and CPU
cycles.  though filters writers should be aware that if they call
$b-E<gt>read(), or any other operation that internally forces the
bucket to read the information into the memory (like the length() op)
and thus making the data handling inefficient. therefore a care should
be taken so not to read the data in, unless it's really necessary.

]




=head1 CPAN Modules

Several modules are available on the CPAN that implement mod_perl 2.0
filters. As with all code on the CPAN, the source code is fully
available, so you can download these modules and see exactly how they
work.

=over

=item C<Apache::Clean> - Interface into HTML::Clean for mod_perl 2.0

http://search.cpan.org/dist/Apache-Clean/

=item C<Apache::Filter::HTTPHeadersFixup> - Manipulate Apache 2 HTTP Headers

http://search.cpan.org/dist/Apache-Filter-HTTPHeadersFixup/

=back







=head1 Maintainers

Maintainer is the person(s) you should contact with updates,
corrections and patches.

=over

=item *

Stas Bekman [http://stason.org/]

=back


=head1 Authors

=over

=item *

=back

Only the major authors are listed above. For contributors see the
Changes file.



=cut