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
  
2996
  
2997
  
2998
  
2999
  
3000
  
3001
  
3002
  
3003
  
3004
  
3005
  
3006
  
3007
  
3008
  
3009
  
3010
  
3011
  
3012
  
3013
  
3014
  
3015
  
3016
  
3017
  
3018
  
3019
  
3020
  
3021
  
3022
  
3023
  
3024
  
3025
  
3026
  
3027
  
3028
  
3029
  
3030
  
3031
  
3032
  
3033
  
3034
  
3035
  
3036
  
3037
  
3038
  
3039
  
3040
  
3041
  
3042
  
3043
  
3044
  
3045
  
3046
  
3047
  
3048
  
3049
  
3050
  
3051
  
3052
  
3053
  
3054
  
3055
  
3056
  
3057
  
3058
  
3059
  
3060
  
3061
  
3062
  
3063
  
3064
  
3065
  
3066
  
3067
  
3068
  
3069
  
3070
  
3071
  
3072
  
3073
  
3074
  
3075
  
3076
  
3077
  
3078
  
3079
  
3080
  
3081
  
3082
  
3083
  
3084
  
3085
  
3086
  
3087
  
3088
  
3089
  
3090
  
3091
  
3092
  
3093
  
3094
  
3095
  
3096
  
3097
  
3098
  
3099
  
3100
  
3101
  
3102
  
3103
  
3104
  
3105
  
3106
  
3107
  
3108
  
3109
  
3110
  
3111
  
3112
  
3113
  
3114
  
3115
  
3116
  
3117
  
3118
  
3119
  
3120
  
3121
  
3122
  
3123
  
3124
  
3125
  
3126
  
3127
  
3128
  
3129
  
3130
  
3131
  
3132
  
3133
  
3134
  
3135
  
3136
  
3137
  
3138
  
3139
  
3140
  
3141
  
3142
  
3143
  
3144
  
3145
  
3146
  
3147
  
3148
  
3149
  
3150
  
3151
  
3152
  
3153
  
3154
  
3155
  
3156
  
3157
  
3158
  
3159
  
3160
  
3161
  
3162
  
3163
  
3164
  
3165
  
3166
  
3167
  
3168
  
3169
  
3170
  
3171
  
3172
  
3173
  
3174
  
3175
  
3176
  
3177
  
3178
  
3179
  
3180
  
3181
  
3182
  
3183
  
3184
  
3185
  
3186
  
3187
  
3188
  
3189
  
3190
  
3191
  
3192
  
3193
  
3194
  
3195
  
3196
  
3197
  
3198
  
3199
  
3200
  
3201
  
3202
  
3203
  
3204
  
3205
  
3206
  
3207
  
3208
  
3209
  
3210
  
3211
  
3212
  
3213
  
3214
  
3215
  
3216
  
3217
  
3218
  
3219
  
3220
  
3221
  
3222
  
3223
  
3224
  
3225
  
3226
  
3227
  
3228
  
3229
  
3230
  
3231
  
3232
  
3233
  
3234
  
3235
  
3236
  
3237
  
3238
  
3239
  
3240
  
3241
  
3242
  
3243
  
3244
  
3245
  
3246
  
3247
  
3248
  
3249
  
3250
  
3251
  
3252
  
3253
  
3254
  
3255
  
3256
  
3257
  
3258
  
3259
  
3260
  
3261
  
3262
  
3263
  
3264
  
3265
  
3266
  
3267
  
3268
  
3269
  
3270
  
3271
  
3272
  
3273
  
3274
  
3275
  
3276
  
3277
  
3278
  
3279
  
3280
  
3281
  
3282
  
3283
  
3284
  
3285
  
3286
  
3287
  
3288
  
3289
  
3290
  
3291
  
3292
  
3293
  
3294
  
3295
  
3296
  
3297
  
3298
  
3299
  
3300
  
3301
  
3302
  
3303
  
3304
  
3305
  
3306
  
3307
  
3308
  
3309
  
3310
  
3311
  
3312
  
3313
  
3314
  
3315
  
3316
  
3317
  
3318
  
3319
  
3320
  
3321
  
3322
  
3323
  
3324
  
3325
  
3326
  
3327
  
3328
  
3329
  
3330
  
3331
  
3332
  
3333
  
3334
  
3335
  
3336
  
3337
  
3338
  
3339
  
3340
  
3341
  
3342
  
3343
  
3344
  
3345
  
3346
  
3347
  
3348
  
3349
  
3350
  
3351
  
3352
  
3353
  
3354
  
3355
  
3356
  
3357
  
3358
  
3359
  
3360
  
3361
  
3362
  
3363
  
3364
  
3365
  
3366
  
3367
  
3368
  
3369
  
3370
  
3371
  
3372
  
3373
  
3374
  
3375
  
3376
  
3377
  
3378
  
3379
  
3380
  
3381
  
3382
  
3383
  
3384
  
3385
  
3386
  
3387
  
3388
  
3389
  
3390
  
3391
  
3392
  
3393
  
3394
  
3395
  
3396
  
3397
  
3398
  
3399
  
3400
  
3401
  
3402
  
3403
  
3404
  
3405
  
3406
  
3407
  
3408
  
3409
  
3410
  
3411
  
3412
  
3413
  
3414
  
3415
  
3416
  
3417
  
3418
  
3419
  
3420
  
3421
  
3422
  
3423
  
3424
  
3425
  
3426
  
3427
  
3428
  
3429
  
3430
  
3431
  
3432
  
3433
  
3434
  
3435
  
3436
  
3437
  
3438
  
3439
  
3440
  
3441
  
3442
  
3443
  
3444
  
3445
  
3446
  
3447
  
3448
  
3449
  
3450
  
3451
  
3452
  
3453
  
3454
  
3455
  
3456
  
3457
  
3458
  
3459
  
3460
  
3461
  
3462
  
3463
  
3464
  
3465
  
3466
  
3467
  
3468
  
3469
  
3470
  
3471
  
3472
  
3473
  
3474
  
3475
  
3476
  
3477
  
3478
  
3479
  
3480
  
3481
  
3482
  
3483
  
3484
  
3485
  
3486
  
3487
  
3488
  
3489
  
3490
  
3491
  
3492
  
3493
  
3494
  
3495
  
3496
  
3497
  
3498
  
3499
  
3500
  
3501
  
3502
  
3503
  
3504
  
3505
  
3506
  
3507
  
3508
  
3509
  
3510
  
3511
  
3512
  
3513
  
3514
  
3515
  
3516
  
3517
  
3518
  
3519
  
3520
  
3521
  
3522
  
3523
  
3524
  
3525
  
3526
  
3527
  
3528
  
3529
  
3530
  
3531
  
3532
  
3533
  
3534
  
3535
  
3536
  
3537
  
3538
  
3539
  
3540
  
3541
  
3542
  
3543
  
3544
  
3545
  
3546
  
3547
  
3548
  
3549
  
3550
  
3551
  
3552
  
3553
  
3554
  
3555
  
3556
  
3557
  
3558
  
3559
  
3560
  
3561
  
3562
  
3563
  
3564
  
3565
  
3566
  
3567
  
3568
  
3569
  
3570
  
3571
  
3572
  
3573
  
3574
  
3575
  
3576
  
3577
  
3578
  
3579
  
3580
  
3581
  
3582
  
3583
  
3584
  
3585
  
3586
  
3587
  
3588
  
3589
  
3590
  
3591
  
3592
  
3593
  
3594
  
3595
  
3596
  
3597
  
3598
  
3599
  
3600
  
3601
  
3602
  
3603
  
3604
  
3605
  
3606
  
3607
  
3608
  
3609
  
3610
  
3611
  
3612
  
3613
  
3614
  
3615
  
3616
  
3617
  
3618
  
3619
  
3620
  
3621
  
3622
  
3623
  
3624
  
3625
  
3626
  
3627
  
3628
  
3629
  
3630
  
3631
  
3632
  
3633
  
3634
  
3635
  
3636
  
3637
  
3638
  
3639
  
3640
  
3641
  
3642
  
3643
  
3644
  
3645
  
3646
  
3647
  
3648
  
3649
  
3650
  
3651
  
3652
  
3653
  
3654
  
3655
  
3656
  
3657
  
3658
  
3659
  
3660
  
3661
  
3662
  
3663
  
3664
  
3665
  
3666
  
3667
  
3668
  
3669
  
3670
  
3671
  
3672
  
3673
  
3674
  
3675
  
3676
  
3677
  
3678
  
3679
  
3680
  
3681
  
3682
  
3683
  
3684
  
3685
  
3686
  
3687
  
3688
  
3689
  
3690
  
3691
  
3692
  
3693
  
3694
  
3695
  
3696
  
3697
  
3698
  
3699
  
3700
  
3701
  
3702
  
3703
  
3704
  
3705
  
3706
  
3707
  
3708
  
3709
  
3710
  
3711
  
3712
  
3713
  
3714
  
3715
  
3716
  
3717
  
3718
  
3719
  
3720
  
3721
  
3722
  
3723
  
3724
  
3725
  
3726
  
3727
  
3728
  
3729
  
3730
  
3731
  
3732
  
3733
  
3734
  
3735
  
3736
  
3737
  
3738
  
3739
  
3740
  
3741
  
3742
  
3743
  
3744
  
3745
  
3746
  
3747
  
3748
  
3749
  
3750
  
3751
  
3752
  
3753
  
3754
  
3755
  
3756
  
3757
  
3758
  
3759
  
3760
  
3761
  
3762
  
3763
  
3764
  
3765
  
3766
  
3767
  
3768
  
3769
  
3770
  
3771
  
3772
  
3773
  
3774
  
3775
  
3776
  
3777
  
3778
  
3779
  
3780
  
3781
  
3782
  
3783
  
3784
  
3785
  
3786
  
3787
  
3788
  
3789
  
3790
  
3791
  
3792
  
3793
  
3794
  
3795
  
3796
  
3797
  
3798
  
3799
  
3800
  
3801
  
3802
  
3803
  
3804
  
3805
  
3806
  
3807
  
3808
  
3809
  
3810
  
3811
  
3812
  
3813
  
3814
  
3815
  
3816
  
3817
  
3818
  
3819
  
3820
  
3821
  
3822
  
3823
  
3824
  
3825
  
3826
  
3827
  
3828
  
3829
  
3830
  
3831
  
3832
  
3833
  
3834
  
3835
  
3836
  
3837
  
3838
  
3839
  
3840
  
3841
  
3842
  
3843
  
3844
  
3845
  
3846
  
3847
  
3848
  
3849
  
3850
  
3851
  
3852
  
3853
  
3854
  
3855
  
3856
  
3857
  
3858
  
3859
  
3860
  
3861
  
3862
  
3863
  
3864
  
3865
  
3866
  
3867
  
3868
  
3869
  
3870
  
3871
  
3872
  
3873
  
3874
  
3875
  
3876
  
3877
  
3878
  
3879
  
3880
  
3881
  
3882
  
3883
  
3884
  
3885
  
3886
  
3887
  
3888
  
3889
  
3890
  
3891
  
3892
  
3893
  
3894
  
3895
  
3896
  
3897
  
3898
  
3899
  
3900
  
3901
  
3902
  
3903
  
3904
  
3905
  
3906
  
3907
  
3908
  
3909
  
3910
  
3911
  
3912
  
3913
  
3914
  
3915
  
3916
  
3917
  
3918
  
3919
  
3920
  
3921
  
3922
  
3923
  
3924
  
3925
  
3926
  
3927
  
3928
  
3929
  
3930
  
3931
  
3932
  
3933
  
3934
  
3935
  
3936
  
3937
  
3938
  
3939
  
3940
  
3941
  
3942
  
3943
  
3944
  
3945
  
3946
  
3947
  
3948
  
3949
  
3950
  
3951
  
3952
  
3953
  
3954
  
3955
  
3956
  
3957
  
3958
  
3959
  
3960
  
3961
  
3962
  
3963
  
3964
  
3965
  
3966
  
3967
  
3968
  
3969
  
3970
  
3971
  
3972
  
3973
  
3974
  
3975
  
3976
  
3977
  
3978
  
3979
  
3980
  
3981
  
3982
  
3983
  
3984
  
3985
  
3986
  
3987
  
3988
  
3989
  
3990
  
3991
  
3992
  
3993
  
3994
  
3995
  
3996
  
3997
  
3998
  
3999
  
4000
  
4001
  
4002
  
4003
  
4004
  
4005
  
4006
  
4007
  
4008
  
4009
  
4010
  
4011
  
4012
  
4013
  
4014
  
4015
  
4016
  
4017
  
4018
  
4019
  
4020
  
4021
  
4022
  
4023
  
4024
  
4025
  
4026
  
4027
  
4028
  
4029
  
4030
  
4031
  
4032
  
4033
  
4034
  
4035
  
4036
  
4037
  
4038
  
4039
  
4040
  
4041
  
4042
  
4043
  
4044
  
4045
  
4046
  
4047
  
4048
  
4049
  
4050
  
4051
  
4052
  
4053
  
4054
  
4055
  
4056
  
4057
  
4058
  
4059
  
4060
  
4061
  
4062
  
4063
  
4064
  
4065
  
4066
  
4067
  
4068
  
4069
  
4070
  
4071
  
4072
  
4073
  
4074
  
4075
  
4076
  
4077
  
4078
  
4079
  
4080
  
4081
  
4082
  
4083
  
4084
  
4085
  
4086
  
4087
  
4088
  
4089
  
4090
  
4091
  
4092
  
4093
  
4094
  
4095
  
4096
  
4097
  
4098
  
4099
  
4100
  
4101
  
4102
  
4103
  
4104
  
4105
  
4106
  
4107
  
4108
  
4109
  
4110
  
4111
  
4112
  
4113
  
4114
  
4115
  
4116
  
4117
  
4118
  
4119
  
4120
  
4121
  
4122
  
4123
  
4124
  
4125
  
4126
  
4127
  
4128
  
4129
  
4130
  
4131
  
4132
  
4133
  
4134
  
4135
  
4136
  
4137
  
4138
  
4139
  
4140
  
4141
  
4142
  
4143
  
4144
  
4145
  
4146
  
4147
  
4148
  
4149
  
4150
  
4151
  
4152
  
4153
  
4154
  
4155
  
4156
  
4157
  
4158
  
4159
  
4160
  
4161
  
4162
  
4163
  
4164
  
4165
  
4166
  
4167
  
4168
  
4169
  
4170
  
4171
  
4172
  
4173
  
4174
  
4175
  
4176
  
4177
  
4178
  
4179
  
4180
  
4181
  
4182
  
4183
  
4184
  
4185
  
4186
  
4187
  
4188
  
4189
  
4190
  
4191
  
4192
  
4193
  
4194
  
4195
  
4196
  
4197
  
4198
  
4199
  
4200
  
4201
  
4202
  
4203
  
4204
  
4205
  
4206
  
4207
  
4208
  
4209
  
4210
  
4211
  
4212
  
4213
  
4214
  
4215
  
4216
  
4217
  
4218
  
4219
  
4220
  
4221
  
4222
  
4223
  
4224
  
4225
  
4226
  
4227
  
4228
  
4229
  
4230
  
4231
  
4232
  
4233
  
4234
  
4235
  
4236
  
4237
  
4238
  
4239
  
4240
  
4241
  
4242
  
4243
  
4244
  
4245
  
4246
  
4247
  
4248
  
4249
  
4250
  
4251
  
4252
  
4253
  
4254
  
4255
  
4256
  
4257
  
4258
  
4259
  
4260
  
4261
  
4262
  
4263
  
4264
  
4265
  
4266
  
4267
  
4268
  
4269
  
4270
  
4271
  
4272
  
4273
  
4274
  
4275
  
4276
  
4277
  
4278
  
4279
  
4280
  
4281
  
4282
  
4283
  
4284
  
4285
  
4286
  
4287
  
4288
  
4289
  
4290
  
4291
  
4292
  
4293
  
4294
  
4295
  
4296
  
4297
  
4298
  
4299
  
4300
  
4301
  
4302
  
4303
  
4304
  
4305
  
4306
  
4307
  
4308
  
4309
  
4310
  
4311
  
4312
  
4313
  
4314
  
4315
  
4316
  
4317
  
4318
  
4319
  
4320
  
4321
  
4322
  
4323
  
4324
  
4325
  
4326
  
4327
  
4328
  
4329
  
4330
  
4331
  
4332
  
4333
  
4334
  
4335
  
4336
  
4337
  
4338
  
4339
  
4340
  
4341
  
4342
  
4343
  
4344
  
4345
  
4346
  
4347
  
4348
  
4349
  
4350
  
4351
  
4352
  
4353
  
4354
  
4355
  
4356
  
4357
  
4358
  
4359
  
4360
  
4361
  
4362
  
4363
  
4364
  
4365
  
4366
  
4367
  
4368
  
4369
  
4370
  
4371
  
4372
  
4373
  
4374
  
4375
  
4376
  
4377
  
4378
  
4379
  
4380
  
4381
  
4382
  
4383
  
4384
  
4385
  
4386
  
4387
  
4388
  
4389
  
4390
  
4391
  
4392
  
4393
  
4394
  
4395
  
4396
  
4397
  
4398
  
4399
  
4400
  
4401
  
4402
  
4403
  
4404
  
4405
  
4406
  
4407
  
4408
  
4409
  
4410
  
4411
  
4412
  
4413
  
4414
  
4415
  
4416
  
4417
  
4418
  
4419
  
4420
  
4421
  
4422
  
4423
  
4424
  
4425
  
4426
  
4427
  
4428
  
4429
  
4430
  
4431
  
4432
  
4433
  
4434
  
4435
  
4436
  
4437
  
4438
  
4439
  
4440
  
4441
  
4442
  
4443
  
4444
  
4445
  
4446
  
4447
  
4448
  
4449
  
4450
  
4451
  
4452
  
4453
  
4454
  
4455
  
4456
  
4457
  
4458
  
4459
  
4460
  
4461
  
4462
  
4463
  
4464
  
4465
  
4466
  
4467
  
4468
  
4469
  
4470
  
4471
  
4472
  
4473
  
4474
  
4475
  
4476
  
4477
  
4478
  
4479
  
4480
  
4481
  
4482
  
4483
  
4484
  
4485
  
4486
  
4487
  
4488
  
4489
  
4490
  
4491
  
4492
  
4493
  
4494
  
4495
  
4496
  
4497
  
4498
  
4499
  
4500
  
4501
  
4502
  
4503
  
4504
  
4505
  
4506
  
4507
  
4508
  
4509
  
4510
  
4511
  
4512
  
4513
  
4514
  
4515
  
4516
  
4517
  
4518
  
4519
  
4520
  
4521
  
4522
  
4523
  
4524
  
4525
  
4526
  
4527
  
4528
  
4529
  
4530
  
4531
  
4532
  
4533
  
4534
  
4535
  
4536
  
4537
  
4538
  
4539
  
4540
  
4541
  
4542
  
4543
  
4544
  
4545
  
4546
  
4547
  
4548
  
4549
  
4550
  
4551
  
4552
  
4553
  
4554
  
4555
  
4556
  
4557
  
4558
  
4559
  
4560
  
4561
  
4562
  
4563
  
4564
  
4565
  
4566
  
4567
  
4568
  
4569
  
4570
  
4571
  
4572
  
4573
  
4574
  
4575
  
4576
  
4577
  
4578
  
4579
  
4580
  
4581
  
4582
  
4583
  
4584
  
4585
  
4586
  
4587
  
4588
  
4589
  
4590
  
4591
  
4592
  
4593
  
4594
  
4595
  
4596
  
4597
  
4598
  
4599
  
4600
  
4601
  
4602
  
4603
  
4604
  
4605
  
4606
  
4607
  
4608
  
4609
  
4610
  
4611
  
4612
  
4613
  
4614
  
4615
  
4616
  
4617
  
4618
  
4619
  
4620
  
4621
  
4622
  
4623
  
4624
  
4625
  
4626
  
4627
  
4628
  
4629
  
4630
  
4631
  
4632
  
4633
  
4634
  
4635
  
4636
  
4637
  
4638
  
4639
  
4640
  
4641
  
4642
  
4643
  
4644
  
4645
  
4646
  
4647
  
4648
  
4649
  
4650
  
4651
  
4652
  
4653
  
4654
  
4655
  
4656
  
4657
  
4658
  
4659
  
4660
  
4661
  
4662
  
4663
  
4664
  
4665
  
4666
  
4667
  
4668
  
4669
  
4670
  
4671
  
4672
  
4673
  
4674
  
4675
  
4676
  
4677
  
4678
  
4679
  
4680
  
4681
  
4682
  
4683
  
4684
  
4685
  
4686
  
4687
  
4688
  
4689
  
4690
  
4691
  
4692
  
4693
  
4694
  
4695
  
4696
  
4697
  
4698
  
4699
  
4700
  
4701
  
4702
  
4703
  
4704
  
4705
  
4706
  
4707
  
4708
  
4709
  
4710
  
4711
  
4712
  
4713
  
4714
  
4715
  
4716
  
4717
  
4718
  
4719
  
4720
  
4721
  
4722
  
4723
  
4724
  
4725
  
4726
  
4727
  
4728
  
4729
  
4730
  
4731
  
4732
  
4733
  
4734
  
4735
  
4736
  
4737
  
4738
  
4739
  
4740
  
4741
  
4742
  
4743
  
4744
  
4745
  
4746
  
4747
  
4748
  
4749
  
4750
  
4751
  
4752
  
4753
  
4754
  
4755
  
4756
  
4757
  
4758
  
4759
  
4760
  
4761
  
4762
  
4763
  
4764
  
4765
  
4766
  
4767
  
4768
  
4769
  
4770
  
4771
  
4772
  
4773
  
4774
  
4775
  
4776
  
4777
  
4778
  
4779
  
4780
  
4781
  
4782
  
4783
  
4784
  
4785
  
4786
  
4787
  
4788
  
4789
  
4790
  
4791
  
4792
  
4793
  
4794
  
4795
  
4796
  
4797
  
4798
  
4799
  
4800
  
4801
  
4802
  
4803
  
4804
  
4805
  
4806
  
4807
  
4808
  
4809
  
4810
  
4811
  
4812
  
4813
  
4814
  
4815
  
4816
  
4817
  
4818
  
4819
  
4820
  
4821
  
4822
  
4823
  
4824
  
4825
  
4826
  
4827
  
4828
  
4829
  
4830
  
4831
  
4832
  
4833
  
4834
  
4835
  
4836
  
4837
  
4838
  
4839
  
4840
  
4841
  
4842
  
4843
  
4844
  
4845
  
4846
  
4847
  
4848
  
4849
  
4850
  
4851
  
4852
  
4853
  
4854
  
4855
  
4856
  
4857
  
4858
  
4859
  
4860
  
4861
  
4862
  
4863
  
4864
  
4865
  
4866
  
4867
  
4868
  
4869
  
4870
  
4871
  
4872
  
4873
  
4874
  
4875
  
4876
  
4877
  
4878
  
4879
  
4880
  
4881
  
4882
  
4883
  
4884
  
4885
  
4886
  
4887
  
4888
  
4889
  
4890
  
4891
  
4892
  
4893
  
4894
  
4895
  
4896
  
4897
  
4898
  
4899
  
4900
  
4901
  
4902
  
4903
  
4904
  
4905
  
4906
  
4907
  
4908
  
4909
  
4910
  
4911
  
4912
  
4913
  
4914
  
4915
  
4916
  
4917
  
4918
  
4919
  
4920
  
4921
  
4922
  
4923
  
4924
  
4925
  
4926
  
4927
  
4928
  
4929
  
4930
  
4931
  
4932
  
4933
  
4934
  
4935
  
4936
  
4937
  
4938
  
4939
  
4940
  
4941
  
4942
  
4943
  
4944
  
4945
  
4946
  
4947
  
4948
  
4949
  
4950
  
4951
  
4952
  
4953
  
4954
  
4955
  
4956
  
4957
  
4958
  
4959
  
4960
  
4961
  
4962
  
4963
  
4964
  
4965
  
4966
  
4967
  
4968
  
4969
  
4970
  
4971
  
4972
  
4973
  
4974
  
4975
  
4976
  
4977
  
4978
  
4979
  
4980
  
4981
  
4982
  
4983
  
4984
  
4985
  
4986
  
4987
  
4988
  
4989
  
4990
  
4991
  
4992
  
4993
  
4994
  
4995
  
4996
  
4997
  
4998
  
4999
  
5000
  
5001
  
5002
  
5003
  
5004
  
5005
  
5006
  
5007
  
5008
  
5009
  
5010
  
5011
  
5012
  
5013
  
5014
  
5015
  
5016
  
5017
  
5018
  
5019
  
5020
  
5021
  
5022
  
5023
  
5024
  
5025
  
5026
  
5027
  
5028
  
5029
  
5030
  
5031
  
5032
  
5033
  
5034
  
5035
  
5036
  
5037
  
5038
  
5039
  
5040
  
5041
  
5042
  
5043
  
5044
  
5045
  
5046
  
5047
  
5048
  
5049
  
5050
  
5051
  
5052
  
5053
  
5054
  
5055
  
5056
  
5057
  
5058
  
5059
  
5060
  
5061
  
5062
  
5063
  
5064
  
5065
  
5066
  
5067
  
5068
  
5069
  
5070
  
5071
  
5072
  
5073
  
5074
  
5075
  
5076
  
5077
  
5078
  
5079
  
5080
  
5081
  
5082
  
5083
  
5084
  
5085
  
5086
  
5087
  
5088
  
5089
  
5090
  
5091
  
5092
  
5093
  
5094
  
5095
  
5096
  
5097
  
5098
  
5099
  
5100
  
5101
  
5102
  
5103
  
5104
  
5105
  
5106
  
5107
  
5108
  
5109
  
5110
  
5111
  
5112
  
5113
  
5114
  
5115
  
5116
  
5117
  
5118
  
5119
  
5120
  
5121
  
5122
  
5123
  
5124
  
5125
  
5126
  
5127
  
5128
  
5129
  
5130
  
5131
  
5132
  
5133
  
5134
  
5135
  
// This file is part of Roxen WebServer. 
// Copyright © 1996 - 2004, Roxen IS. 
// 
 
// @appears Configuration 
//! A site's main configuration 
 
constant cvs_version = "$Id: configuration.pike,v 1.644 2008/01/09 16:40:43 mast Exp $"; 
#include <module.h> 
#include <module_constants.h> 
#include <roxen.h> 
#include <request_trace.h> 
#include <timers.h> 
 
#define CATCH(P,X) do{mixed e;if(e=catch{X;})report_error("While "+P+"\n"+describe_backtrace(e));}while(0) 
 
// --- Locale defines --- 
//<locale-token project="roxen_start">   LOC_S  </locale-token> 
//<locale-token project="roxen_config">  LOC_C  </locale-token> 
//<locale-token project="roxen_message"> LOC_M  </locale-token> 
//<locale-token project="roxen_config"> DLOCALE </locale-token> 
#define LOC_S(X,Y)  _STR_LOCALE("roxen_start",X,Y) 
#define LOC_C(X,Y)  _STR_LOCALE("roxen_config",X,Y) 
#define LOC_M(X,Y)  _STR_LOCALE("roxen_message",X,Y) 
#define DLOCALE(X,Y) _DEF_LOCALE("roxen_config",X,Y) 
 
#ifdef THROTTLING_DEBUG 
#undef THROTTLING_DEBUG 
#define THROTTLING_DEBUG(X) report_debug("Throttling: "+X+"\n") 
#else 
#define THROTTLING_DEBUG(X) 
#endif 
 
#ifdef REQUEST_DEBUG 
# define REQUEST_WERR(X) report_debug("CONFIG: "+X+"\n") 
#else 
# define REQUEST_WERR(X) 
#endif 
 
 
#ifdef AVERAGE_PROFILING 
 
#if !constant(gethrvtime) 
#define gethrvtime()        gethrtime() 
#endif /* !constant(gethrvtime) */ 
 
class ProfStack 
{ 
  array current_stack = ({}); 
 
  void enter( string k, RequestID id ) 
  { 
    current_stack += ({ ({ k, gethrtime(), gethrvtime() }) }); 
  } 
   
  void leave( string k, RequestID id ) 
  { 
    int t0 = gethrtime(); 
    int t1 = gethrvtime(); 
 
    if( !sizeof(current_stack ) ) 
    { 
//       report_error("Popping out of profiling stack\n"); 
      return; 
    } 
       
    int i = sizeof( current_stack )-1; 
    while( current_stack[ i ][0] != k && i >= 0 ) i--; 
 
    if(i < 0 ) 
    { 
      return; 
    } 
    void low_leave( int i ) 
    { 
      int tt = t0-current_stack[i][1]; 
      int ttv = t1-current_stack[i][2]; 
 
      if( i > 0 ) // Do not count child time in parent. 
      { 
        current_stack[i-1][1]+=tt+gethrtime()-t0; 
        current_stack[i-1][2]+=ttv+gethrvtime()-t1; 
      } 
      current_stack = current_stack[..i-1]; 
      add_prof_entry( id, k, tt, ttv ); 
    }; 
 
    if( i != sizeof( current_stack )-1 ) 
    { 
      for( int j = sizeof( current_stack )-1; j>=i; j-- ) 
        low_leave( j ); 
      return; 
    } 
    low_leave( i ); 
  } 
} 
 
class ProfInfo( string url ) 
{ 
  mapping data = ([]); 
  void add( string k, int h, int hrv ) 
  { 
    if( !data[k] ) 
      data[k] = ({ h, hrv, 1 }); 
    else 
    { 
      data[k][0]+=h; 
      data[k][1]+=hrv; 
      data[k][2]++; 
    } 
  } 
 
  array summarize_table( ) 
  { 
    array table = ({}); 
    int n, t, v; 
    foreach( indices( data ), string k  ) 
      table += ({ ({ k, 
                     sprintf( "%d", (n=data[k][2]) ), 
                     sprintf("%5.2f",(t=data[k][0])/1000000.0), 
                     sprintf("%5.2f", (v=data[k][1])/1000000.0), 
                     sprintf("%8.2f", t/n/1000.0), 
                     sprintf("%8.2f",v/n/1000.0), }) }); 
    sort( (array(float))column(table,2), table ); 
    return reverse(table); 
  } 
 
  void dump( ) 
  { 
    write( "\n"+url+": \n" ); 
    ADT.Table.table t = ADT.Table.table( summarize_table(), 
                                         ({ "What", "Calls", 
                                            "Time", "CPU", 
                                            "t/call(ms)", "cpu/call(ms)" })); 
 
    write( ADT.Table.ASCII.encode( t )+"\n" ); 
 
  } 
} 
 
mapping profiling_info = ([]); 
 
void debug_write_prof( ) 
{ 
  foreach( sort( indices( profiling_info ) ), string p ) 
    profiling_info[p]->dump(); 
} 
 
void add_prof_entry( RequestID id, string k, int hr, int hrv ) 
{ 
  string l = id->not_query; 
//   if( has_prefix( k, "find_internal" ) ) l = dirname(l); 
  if( has_prefix( l, query_internal_location() ) ) 
    l = dirname( l ); // enough, really.  
 
  if( !profiling_info[l] ) 
    profiling_info[l] = ProfInfo(l); 
 
  profiling_info[l]->add( k, hr, hrv ); 
} 
 
void avg_prof_enter( string name, string type, RequestID id ) 
{ 
  if( !id->misc->prof_stack ) 
    id->misc->prof_stack = ProfStack(); 
  id->misc->prof_stack->enter( name+":"+type,id ); 
} 
void avg_prof_leave( string name, string type, RequestID id ) 
{ 
  if( !id->misc->prof_stack ) id->misc->prof_stack = ProfStack(); 
  id->misc->prof_stack->leave( name+":"+type,id ); 
} 
#endif 
 
 
/* A configuration.. */ 
inherit Configuration; 
inherit "basic_defvar"; 
 
static mapping(RequestID:mapping) current_connections = 
  set_weak_flag( ([ ]), 1 ); 
 
void connection_add( RequestID id, mapping data ) 
//! Add a connection. The data mapping can contain things such as 
//! currently sent bytes. 
//! 
//! See protocols/http.pike and slowpipe.pike for more information. 
//! 
//! You are not in any way forced to use this method from your 
//! protocol module. The information is only used for debug purposes 
//! in the configuration interface. 
//! 
//! You have to keep a reference to the mapping on your own, none is 
//! kept by the configuration object. 
{ 
  current_connections[id] = data; 
} 
 
mapping connection_drop( RequestID id ) 
//! Remove a connection from the list of currently active connections. 
//! Returns the mapping previously added with connection_add, if any. 
{ 
  return m_delete( current_connections, id ); 
} 
 
mapping(RequestID:mapping) connection_get( ) 
//! Return all currently active connections. 
{ 
  return current_connections; 
} 
 
// It's nice to have the name when the rest of __INIT executes. 
string name = roxen->bootstrap_info->get(); 
 
class DataCache 
{ 
  static typedef array(string|mapping(string:mixed))|string| 
                 function(string, RequestID:string|int) EntryType; 
 
  mapping(string:EntryType) cache = ([]); 
 
  int current_size; 
  int max_size; 
  int max_file_size; 
 
  int hits, misses; 
 
  void flush() 
  { 
#ifndef RAM_CACHE_NO_RELOAD_FLUSH 
    current_size = 0; 
    cache = ([]); 
#endif 
  } 
 
  // Expire a single entry. 
  static void really_low_expire_entry(string key) 
  { 
    EntryType e = m_delete(cache, key); 
    if (arrayp(e)) { 
      current_size -= sizeof(e[0]); 
      if (e[1]->co_handle) { 
        remove_call_out(e[1]->co_handle); 
      } 
    } 
  } 
 
  // NOTE: Avoid using this function if possible! O(n) 
  static int low_expire_entry(string key_prefix) 
  { 
    if (!key_prefix) return 0; 
    if (arrayp(cache[key_prefix])) { 
      // Leaf node. No need to loop. 
      really_low_expire_entry(key_prefix); 
      return 1; 
    } 
    // Inner node. Find all its children. 
    int res = 0; 
    foreach(indices(cache); int ind; string key) { 
      if (!key) continue; 
      if (has_prefix(key, key_prefix)) { 
        really_low_expire_entry(key); 
        res++; 
      } 
    } 
    return res; 
  } 
 
  void expire_entry(string key_prefix, RequestID|void id) 
  { 
    if (!id) { 
      low_expire_entry(key_prefix); 
      return; 
    } 
    string url = key_prefix; 
    sscanf(url, "%[^\0]", url); 
    while(1) { 
      EntryType val; 
      if (arrayp(val = cache[key_prefix])) { 
        current_size -= sizeof(val[0]); 
        m_delete(cache, key_prefix); 
        return; 
      } 
      if (!val) { 
        return; 
      } 
 
      string|array(string) key_frag; 
      if (stringp(val)) { 
        key_frag = id->request_headers[val]; 
      } else { 
        key_frag = val(url, id); 
      } 
      if (key_frag) 
        // Avoid spoofing if key_frag happens to contain "\0\0". 
        key_frag = replace (key_frag, "\0", "\0\1"); 
      else key_frag = ""; 
      key_prefix += "\0\0" + key_frag; 
    } 
  } 
 
  //! Clear ~1/10th of the cache. 
  static void clear_some_cache() 
  { 
    array(string) q = indices(cache); 
    if(!sizeof(q)) 
    { 
      current_size=0; 
      return; 
    } 
 
    // The following code should be ~O(n * log(n)). 
    sort(q); 
    for(int i = 0; i < sizeof(q)/10; i++) { 
      int r = random(sizeof(q)); 
      string key_prefix = q[r = random(sizeof(q))]; 
      if (!key_prefix) continue; 
      for(;r < sizeof(q); r++,i++) { 
        if (!q[r]) continue; 
        if (!has_prefix(q[r], key_prefix)) break; 
        really_low_expire_entry(q[r]); 
        q[r] = 0; 
      } 
    } 
  } 
 
  void set(string url, string data, mapping meta, int expire, RequestID id) 
  { 
    if( strlen( data ) > max_file_size ) { 
      SIMPLE_TRACE_ENTER (this, "Result of size %d is too large " 
                          "to store in the protocol cache (limit %d)", 
                          sizeof (data), max_file_size); 
      SIMPLE_TRACE_LEAVE (""); 
      return; 
    } 
 
    SIMPLE_TRACE_ENTER (this, "Storing result of size %d in the protocol cache " 
                        "using key %O (expire in %ds)", 
                        sizeof (data), url, expire); 
    string key = url; 
 
    foreach(id->misc->vary_cb_order || ({}), 
            string|function(string, RequestID: string|int) vary_cb) { 
      array(string|mapping(string:mixed))|string| 
        function(string, RequestID:string|int) old = cache[key]; 
      if (old && (old != vary_cb)) { 
        SIMPLE_TRACE_ENTER (this, "Registering vary cb %O - conflicts with " 
                            "existing entry %s, old entry expired", 
                            vary_cb, 
                            (arrayp (old) ? "of size " + sizeof (old[0]) : 
                             sprintf ("%O", old))); 
        low_expire_entry(key); 
        SIMPLE_TRACE_LEAVE (""); 
      } 
      cache[key] = vary_cb; 
 
      SIMPLE_TRACE_ENTER (this, "Registering vary cb %O", vary_cb); 
 
      string key_frag; 
      if (stringp(vary_cb)) { 
        string|array(string) header = id->request_headers[vary_cb]; 
        if (arrayp(header)) key_frag = header * ","; 
        else key_frag = header; 
      } else { 
        int|string frag = vary_cb(url, id); 
        if (intp(frag) && frag) { 
          key_frag = frag->digits(256); 
        } else { 
          key_frag = frag; 
        } 
      } 
 
      SIMPLE_TRACE_LEAVE ("Vary cb resolved to key fragment %O", 
                          key_frag || ""); 
 
      if (key_frag) 
        // Avoid spoofing if key_frag happens to contain "\0\0". 
        key_frag = replace (key_frag, "\0", "\0\1"); 
      else key_frag = ""; 
      key += "\0\0" + key_frag; 
    } 
 
    array(string|mapping(string:mixed))|string| 
      function(string, RequestID:string) old = cache[key]; 
    if (old) { 
      SIMPLE_TRACE_LEAVE ("Entry conflicts with existing entry %s, " 
                          "old entry expired", 
                          (arrayp (old) ? "of size " + sizeof (old[0]) : 
                           sprintf ("%O", old))); 
      low_expire_entry(key); 
    } 
    else 
      SIMPLE_TRACE_LEAVE (""); 
 
    current_size += strlen( data ); 
    cache[key] = ({ data, meta }); 
 
    // Only the actual cache entry is expired. 
    // FIXME: This could lead to lots and lots of call outs.. :P 
    meta->co_handle = call_out(really_low_expire_entry, expire, key); 
    int n; 
    while( (current_size > max_size) && (n++<10)) 
      clear_some_cache(); 
  } 
   
  array(string|mapping(string:mixed)) get(string url, RequestID id) 
  { 
    SIMPLE_TRACE_ENTER (this, "Looking up entry for %O in the protocol cache", 
                        url); 
 
    array(string|mapping(string:mixed))|string| 
      function(string, RequestID:string|int) res; 
    string key = url; 
    while(1) { 
      id->misc->protcache_cost++; 
      if (arrayp(res = cache[key])) { 
        hits++; 
        SIMPLE_TRACE_LEAVE ("Found entry of size %d", sizeof (res[0])); 
        return [array(string|mapping(string:mixed))]res; 
      } 
      if (!res) { 
        misses++; 
        SIMPLE_TRACE_LEAVE ("Found no entry"); 
        return UNDEFINED; 
      } 
 
      SIMPLE_TRACE_ENTER (this, "Found vary cb %O", res); 
 
      string key_frag; 
      if (stringp(res)) { 
        string|array(string) header = id->request_headers[res]; 
        if (arrayp(header)) key_frag = header * ","; 
        else key_frag = header; 
      } else { 
        int|string frag = res(url, id); 
        if (intp(frag) && frag) { 
          key_frag = frag->digits(256); 
        } else { 
          key_frag = frag; 
        } 
      } 
 
      SIMPLE_TRACE_LEAVE ("Vary cb resolved to key fragment %O", 
                          key_frag || ""); 
 
      if (key_frag) 
        // Avoid spoofing if key_frag happens to contain "\0\0". 
        key_frag = replace (key_frag, "\0", "\0\1"); 
      else key_frag = ""; 
      key += "\0\0" + key_frag; 
    }; 
  } 
 
  void init_from_variables( ) 
  { 
    max_size = query( "data_cache_size" ) * 1024; 
    max_file_size = query( "data_cache_file_max_size" ) * 1024; 
    if( max_size < max_file_size ) 
      max_size += max_file_size; 
    int n; 
    while( (current_size > max_size) && (n++<10)) 
      clear_some_cache(); 
  } 
 
  static void create() 
  { 
    init_from_variables(); 
  } 
} 
 
#include "rxml.pike"; 
constant    store = roxen.store; 
constant    retrieve = roxen.retrieve; 
constant    remove = roxen.remove; 
 
int config_id; 
int get_config_id()  
{ 
  if(config_id) return config_id; 
  for(int i=sizeof(roxen->configurations); i;) 
    if(roxen->configurations[--i]->name==name) return config_id=i; 
} 
 
string get_doc_for( string region, string variable ) 
{ 
  RoxenModule module; 
  if(variable[0] == '_') 
    return 0; 
  if((int)reverse(region)) 
    return 0; 
  if(module = find_module( region )) 
  { 
    if(module->variables[variable]) 
      return module->variables[variable]->name()+ 
        "\n"+module->variables[ variable ]->doc(); 
  } 
  if(variables[ variable ]) 
    return variables[variable]->name()+ 
      "\n"+variables[ variable ]->doc(); 
} 
 
string query_internal_location(RoxenModule|void mod) 
{ 
  return internal_location+(mod?replace(otomod[mod]||"", "#", "!")+"/":""); 
} 
 
string query_name() 
{ 
  if(strlen(query("name"))) 
    return query("name"); 
  return name; 
} 
 
string comment() 
{ 
  return query("comment"); 
} 
 
private float cached_compat_level; 
 
float compat_level() 
{ 
  if (cached_compat_level == 0.0) 
    cached_compat_level = (float) query ("compat_level"); 
  return cached_compat_level; 
} 
 
/* A 'pri' is one of the ten priority objects. Each one holds a list 
 * of modules for that priority. They are all merged into one list for 
 * performance reasons later on. 
 */ 
 
array (Priority) allocate_pris() 
{ 
  return allocate(10, Priority)(); 
} 
 
array(int) query_oid() 
{ 
  return SNMP.RIS_OID_WEBSERVER + ({ 2 }); 
} 
 
//! @returns 
//!   Returns an array with two elements: 
//!   @array 
//!     @item array(int) oid 
//!     @item array(int) oid_suffix 
//!   @endarray 
array(int) generate_module_oid_segment(RoxenModule me) 
{ 
  string s = otomod[me]; 
  array(string) a = s/"#"; 
  return ({ sizeof(a[0]), @((array(int))a[0]), ((int)a[1]) + 1 }); 
} 
 
ADT.Trie generate_module_mib(array(int) oid, 
                             array(int) oid_suffix, 
                             RoxenModule me, 
                             ModuleInfo moduleinfo, 
                             ModuleCopies module) 
{ 
  array(int) segment = generate_module_oid_segment(me); 
  return SNMP.SimpleMIB(oid, 
                        oid_suffix + segment, 
                        ({ 
                          UNDEFINED, 
                          SNMP.Integer(segment[-1], "moduleCopy"), 
                          SNMP.String(otomod[me], 
                                      "moduleIdentifier"), 
                          SNMP.Integer(moduleinfo->type, 
                                       "moduleType"), 
                          SNMP.String(me->cvs_version || "", 
                                      "moduleVersion"), 
                        })); 
} 
 
// Cache some configuration variables. 
private int sub_req_limit = 30; 
private string internal_location = "/_internal/"; 
 
// The logging format used. This will probably move to the above 
// mentioned module in the future. 
private mapping (int|string:string) log_format = ([]); 
 
// A list of priority objects 
array (Priority) pri = allocate_pris(); 
 
mapping modules = ([]); 
//! All enabled modules in this site. 
//! The format is "module":{ "copies":([ num:instance, ... ]) } 
 
mapping (RoxenModule:string) otomod = ([]); 
//! A mapping from the module objects to module names 
 
 
// Caches to speed up the handling of the module search. 
// They are all sorted in priority order, and created by the functions 
// below. 
private array (function) url_module_cache, last_module_cache; 
private array (function) logger_module_cache, first_module_cache; 
private array (function) filter_module_cache; 
private array (array (string|function)) location_module_cache; 
private mapping (string:array (function)) file_extension_module_cache=([]); 
private mapping (string:array (RoxenModule)) provider_module_cache=([]); 
private array (RoxenModule) auth_module_cache, userdb_module_cache; 
 
 
void unregister_urls() 
{ 
  foreach( registered_urls + failed_urls, string url ) 
    roxen.unregister_url(url, this_object()); 
  registered_urls = ({}); 
} 
 
private int num_modules = 0; 
#ifdef THREADS 
private Thread.Condition modules_stopped = Thread.Condition(); 
private Thread.Mutex modules_stopped_mutex = Thread.Mutex(); 
#endif 
private void safe_stop_module (RoxenModule mod, string desc) 
{ 
  if (mixed err = catch (mod && mod->stop && mod->stop())) 
    report_error ("While stopping " + desc + ": " + describe_backtrace (err)); 
#ifdef THREADS 
  Thread.MutexKey lock = modules_stopped_mutex->lock(); 
  if (!--num_modules) 
    modules_stopped->signal(); 
  lock = 0; 
#else 
  --num_modules; 
#endif 
} 
 
void stop (void|int asynch) 
//! Unregisters the urls and calls stop in all modules. Uses the 
//! handler threads to lessen the impact if a module hangs. Doesn't 
//! wait for all modules to finish if @[asynch] is nonzero. 
{ 
 
#ifdef SNMP_AGENT 
  if(query("snmp_process") && objectp(roxen->snmpagent)) { 
    roxen->snmpagent->vs_stop_trap(get_config_id()); 
    roxen->snmpagent->del_virtserv(get_config_id()); 
  } 
#endif 
 
  unregister_urls(); 
 
  multiset allmods = mkmultiset (indices (otomod)); 
  num_modules = 17; 
 
  if (types_module) { 
    num_modules++; 
    roxen.handle (safe_stop_module, types_module, "type module"); 
    allmods[types_module] = 0; 
  } 
  if (dir_module) { 
    num_modules++; 
    roxen.handle (safe_stop_module, dir_module, "directory module"); 
    allmods[dir_module] = 0; 
  } 
  for(int i=0; i<10; i++) 
    if (Priority p = pri[i]) { 
#define STOP_MODULES(MODS, DESC)                                      \ 
      foreach(MODS, RoxenModule m)                                      \ 
        if (allmods[m]) {                                               \ 
          num_modules++;                                                \ 
          roxen.handle (safe_stop_module, m, DESC);                     \ 
          allmods[m] = 0;                                               \ 
        } 
      STOP_MODULES (p->url_modules, "url module"); 
      STOP_MODULES (p->logger_modules, "logging module"); 
      STOP_MODULES (p->filter_modules, "filter module"); 
      STOP_MODULES (p->location_modules, "location module"); 
      STOP_MODULES (p->last_modules, "last module"); 
      STOP_MODULES (p->first_modules, "first module"); 
      STOP_MODULES (indices (p->provider_modules), "provider module"); 
    } 
  if (mixed err = catch { 
    if (object m = log_function && function_object (log_function)) { 
      destruct (m); 
      allmods[m] = 0; 
    } 
  }) report_error ("While stopping the logger: " + describe_backtrace (err)); 
  STOP_MODULES(indices (allmods), "unclassified module"); 
#undef STOP_MODULES 
 
  if (!asynch) { 
#ifdef THREADS 
    Thread.MutexKey lock = modules_stopped_mutex->lock(); 
    num_modules -= 17; 
    if (num_modules) modules_stopped->wait (lock); 
    lock = 0; 
#else 
    if (num_modules != 17) 
      error ("num_modules shouldn't be nonzero here when running nonthreaded.\n"); 
#endif 
  } 
} 
 
string|array(string) type_from_filename( string file, int|void to, 
                                         string|void myext ) 
{ 
  array(string)|string tmp; 
  if(!types_fun) 
    return to?({ "application/octet-stream", 0 }):"application/octet-stream"; 
 
  string ext = lower_case(myext || Roxen.extension(file)); 
 
  if(tmp = types_fun(ext)) 
  { 
    // FIXME: Ought to support several levels of "strip". 
    if (tmp[0] == "strip") 
    { 
      array(string) tmp2 = file/"."; 
      string nx; 
      if (sizeof(tmp2) > 2) 
        nx = lower_case(tmp2[-2]); 
      tmp[0] = (nx && types_fun(nx)) || types_fun("default") || 
        "application/octet-stream"; 
    } 
  } else if (!(tmp = types_fun("default"))) { 
    tmp = ({ "application/octet-stream", 0 }); 
  } 
  return to?tmp:tmp[0]; 
} 
 
array (RoxenModule) get_providers(string provides) 
//! Returns an array with all provider modules that provides "provides". 
{ 
  // This cache is cleared in the invalidate_cache() call. 
  if(!provider_module_cache[provides]) 
  { 
    int i; 
    provider_module_cache[provides]  = ({ }); 
    for(i = 9; i >= 0; i--) 
    { 
      array(RoxenModule) modules = indices(pri[i]->provider_modules); 
      array(string) module_identifiers = modules->module_identifier(); 
      sort(module_identifiers, modules); 
      foreach(modules, RoxenModule d) 
        if(pri[i]->provider_modules[ d ][ provides ]) 
          provider_module_cache[provides] += ({ d }); 
    } 
  } 
  return provider_module_cache[provides]; 
} 
 
RoxenModule get_provider(string provides) 
//! Returns the first provider module that provides "provides". 
{ 
  array (RoxenModule) prov = get_providers(provides); 
  if(sizeof(prov)) 
    return prov[0]; 
  return 0; 
} 
 
array(mixed) map_providers(string provides, string fun, mixed ... args) 
//! Maps the function "fun" over all matching provider modules. 
{ 
  array (RoxenModule) prov = get_providers(provides); 
  mixed error; 
  array a=({ }); 
  mixed m; 
  foreach(prov, RoxenModule mod) 
  { 
    if(!objectp(mod)) 
      continue; 
    if(functionp(mod[fun])) 
      error = catch(m=mod[fun](@args)); 
    if(error) { 
      report_debug("Error in map_providers(): " + describe_backtrace(error)); 
    } 
    else 
      a += ({ m }); 
    error = 0; 
  } 
  return a; 
} 
 
mixed call_provider(string provides, string fun, mixed ... args) 
//! Maps the function "fun" over all matching provider modules and 
//! returns the first positive response. 
{ 
  foreach(get_providers(provides), RoxenModule mod) 
  { 
    function f; 
    if(objectp(mod) && functionp(f = mod[fun])) { 
      mixed ret; 
      if (ret = f(@args)) { 
        return ret; 
      } 
    } 
  } 
} 
 
array (function) file_extension_modules(string ext) 
{ 
  if(!file_extension_module_cache[ext = lower_case(ext)]) 
  { 
    int i; 
    file_extension_module_cache[ext]  = ({ }); 
    for(i=9; i>=0; i--) 
    { 
      array(RoxenModule) d; 
      RoxenModule p; 
      if(d = pri[i]->file_extension_modules[ext]) 
        foreach(d, p) 
          file_extension_module_cache[ext] += ({ p->handle_file_extension }); 
    } 
  } 
  return file_extension_module_cache[ext]; 
} 
 
array (function) url_modules() 
{ 
  if(!url_module_cache) 
  { 
    int i; 
    url_module_cache=({ }); 
    for(i=9; i>=0; i--) 
    { 
      array(RoxenModule) d; 
      RoxenModule p; 
      if(d=pri[i]->url_modules) 
        foreach(d, p) 
          url_module_cache += ({ p->remap_url }); 
    } 
  } 
  return url_module_cache; 
} 
 
static mapping api_module_cache = ([]); 
mapping api_functions(void|RequestID id) 
{ 
  return api_module_cache+([]); 
} 
 
array (function) logger_modules() 
{ 
  if(!logger_module_cache) 
  { 
    int i; 
    logger_module_cache=({ }); 
    for(i=9; i>=0; i--) 
    { 
      array(RoxenModule) d; 
      RoxenModule p; 
      if(d=pri[i]->logger_modules) 
        foreach(d, p) 
          if(p->log) 
            logger_module_cache += ({ p->log }); 
    } 
  } 
  return logger_module_cache; 
} 
 
array (function) last_modules() 
{ 
  if(!last_module_cache) 
  { 
    int i; 
    last_module_cache=({ }); 
    for(i=9; i>=0; i--) 
    { 
      array(RoxenModule) d; 
      RoxenModule p; 
      if(d=pri[i]->last_modules) 
        foreach(d, p) 
          if(p->last_resort) 
            last_module_cache += ({ p->last_resort }); 
    } 
  } 
  return last_module_cache; 
} 
 
static mixed strip_fork_information(RequestID id) 
{ 
  if (uname()->sysname == "Darwin") { 
    //  Look for Mac OS X special filenames that are used access files in 
    //  magic ways: 
    // 
    //    foo.txt/..namedfork/data     (same as foo.txt) 
    //    foo.txt/..namedfork/rsrc     (resource fork of foo.txt) 
    //    foo.txt/rsrc                 (resource fork of foo.txt) 
    //    .DS_Store                    (Finder info file with catalog data) 
    if (has_value(id->not_query, "..namedfork/") || 
        has_suffix(id->not_query, "/rsrc") || 
        has_value(lower_case(id->not_query), ".ds_store")) 
      //  Show 404 page 
      return error_file(id); 
  } 
   
  array a = id->not_query/"::"; 
  //  FIX: Must not subtract ":" chars since it breaks proper URL:s, 
  //  e.g. "/internal-roxen-colorbar:x,y,z" and several others. 
  //  id->not_query = a[0]-":"; 
  id->not_query = a[0]; 
  id->misc->fork_information = a[1..]; 
  return 0; 
} 
 
array (function) first_modules() 
{ 
  if(!first_module_cache) 
  { 
    int i; 
    first_module_cache = ({ }); 
     
    //  Add special fork handlers on Windows and Mac OS X 
    if ( 
#ifdef __NT__ 
      1 || 
#endif 
      uname()->sysname == "Darwin") { 
      first_module_cache= ({ 
        strip_fork_information, // Always first! 
      }); 
    } 
     
    for(i=9; i>=0; i--) 
    { 
      array(RoxenModule) d; RoxenModule p; 
      if(d=pri[i]->first_modules) { 
        foreach(d, p) { 
          if(p->first_try) { 
            first_module_cache += ({ p->first_try }); 
          } 
        } 
      } 
    } 
  } 
 
  return first_module_cache; 
} 
 
void set_userdb_module_cache( array to ) 
// Used by the config_filesystem.pike module to enforce the usage of 
// the config userdb module, for now. 
{ 
  userdb_module_cache = to;      
} 
 
array(UserDB) user_databases() 
{ 
  if( userdb_module_cache ) 
    return userdb_module_cache; 
  array tmp = ({}); 
  foreach( values( modules ), mapping m ) 
    foreach( values(m->copies), RoxenModule mo ) 
      if( mo->module_type & MODULE_USERDB ) 
        tmp += ({ ({ mo->query( "_priority" ), mo }) }); 
 
  sort( tmp ); 
//   tmp += ({ ({ 0, roxen->config_userdb_module }) }); 
  return userdb_module_cache = reverse(column(tmp,1)); 
} 
 
array(AuthModule) auth_modules() 
{ 
  if( auth_module_cache ) 
    return auth_module_cache; 
  array tmp = ({}); 
  foreach( values( modules ), mapping m ) 
    foreach( values(m->copies), RoxenModule mo ) 
      if( mo->module_type & MODULE_AUTH ) 
        tmp += ({ ({ mo->query( "_priority" ), mo }) }); 
  sort( tmp ); 
  return auth_module_cache = reverse(column(tmp,1)); 
} 
 
array location_modules() 
//! Return an array of all location modules the request should be 
//! mapped through, by order of priority. 
{ 
  if(!location_module_cache) 
  { 
    int i; 
    array new_location_module_cache=({ }); 
    for(i=9; i>=0; i--) 
    { 
      array(RoxenModule) d; 
      RoxenModule p; 
      if(d=pri[i]->location_modules) { 
        array level_find_files = ({}); 
        array level_locations = ({}); 
        foreach(d, p) { 
          string location; 
          // FIXME: Should there be a catch() here? 
          if(p->find_file && (location = p->query_location())) { 
            level_find_files += ({ p->find_file }); 
            level_locations += ({ location }); 
          } 
        } 
        sort(map(level_locations, sizeof), level_locations, level_find_files); 
        int j; 
        for (j = sizeof(level_locations); j--;) { 
          // Order after longest path first. 
          new_location_module_cache += ({ ({ level_locations[j], 
                                             level_find_files[j] }) }); 
        } 
      } 
    } 
    location_module_cache = new_location_module_cache; 
  } 
  return location_module_cache; 
} 
 
array(function) filter_modules() 
{ 
  if(!filter_module_cache) 
  { 
    int i; 
    filter_module_cache=({ }); 
    for(i=9; i>=0; i--) 
    { 
      array(RoxenModule) d; 
      RoxenModule p; 
      if(d=pri[i]->filter_modules) 
        foreach(d, p) 
          if(p->filter) 
            filter_module_cache+=({ p->filter }); 
    } 
  } 
  return filter_module_cache; 
} 
 
 
void init_log_file() 
{ 
  if(log_function) 
  { 
    // Free the old one. 
    destruct(function_object(log_function)); 
    log_function = 0; 
  } 
  // Only try to open the log file if logging is enabled!! 
  if(query("Log")) 
  { 
    string logfile = query("LogFile"); 
    if(strlen(logfile)) 
      log_function = roxen.LogFile(logfile, query("LogFileCompressor"))->write; 
  } 
} 
 
private void parse_log_formats() 
{ 
  array foo=query("LogFormat")/"\n"; 
  log_format = ([]); 
  foreach(foo; int i; string b) 
    if(strlen(b) && b[0] != '#') { 
      if (sscanf (b, "%d:%*[\t ]%s", int status, b)) 
        log_format[status] = b; 
      else if (sscanf (b, "*:%*[\t ]%s", b)) 
        log_format[0] = b; 
      else if (sscanf (b, "%[-_.#a-zA-Z0-9*]/%[-_.#a-zA-Z0-9*]:%*[\t ]%s", 
                       string facility, string action, b) >= 2) 
        log_format[facility + "/" + action] = b; 
      else 
        // Ought to be an error when the variable is set, but that's 
        // not entirely backward compatible. 
        report_warning ("Unrecognized format on line %d " 
                        "in log format setting: %O\n", i + 1, b); 
    } 
} 
 
void log(mapping file, RequestID request_id) 
{ 
  // Call all logging functions 
  array(function) log_funs = logger_module_cache||logger_modules(); 
  if (sizeof(log_funs)) { 
    request_id->init_cookies(1); 
    foreach(log_funs, function f) 
      if( f( request_id, file ) ) 
        return; 
  } 
 
  if( !log_function )  
    return; // No file is open for logging. 
 
  if(do_not_log_patterns &&  
     Roxen._match(request_id->remoteaddr, do_not_log_patterns)) 
    return; 
 
  string form; 
  if(!(form=log_format[(int) file->error])) 
    form = log_format[0]; 
  if(!form) return; 
 
  roxen.run_log_format( form, log_function, request_id, file ); 
} 
 
void log_event (string facility, string action, string resource, 
                void|mapping(string:mixed) info) 
//! Log an event. 
//! 
//! This function is primarily intended for logging arbitrary internal 
//! events for performance monitoring purposes; see @[log_event] for 
//! details. The events are sent to the access log, where they 
//! typically are formatted in a CommonLog lookalike format. 
//! 
//! The intention is to extend this function to be able to collect 
//! statistics of these events for polling by e.g. SNMP. 
//! 
//! @param facility 
//!   An identifier for the module or subsystem that the event comes 
//!   from. This defaults to the module identifier returned by 
//!   @[RoxenModule.module_local_id] when the @[RoxenModule.log_event] 
//!   wrapper is used. It should be unique within the configuration. 
//!   Valid characters are @expr{[-_.#a-zA-Z0-9]@} but the first 
//!   character has to be alphanumeric. 
//! 
//! @param action 
//!   An identifier for the specific event within the facility. Should 
//!   be enumerable. Valid characters are @expr{[-_.#a-zA-Z0-9]@}. 
//! 
//! @param resource 
//!   Identifies the resource that the event acts on. 
//! 
//!   If applicable, this is the path within the virtual file system 
//!   of the module, beginning with a "@expr{/@}". 
//! 
//!   Otherwise it is some other string, not beginning with 
//!   "@expr{/@}", that has a format suitable for describing the 
//!   resource handled by the facility, e.g. "@expr{pclass:17@}". 
//! 
//!   This string should preferably contain URI valid chars only, but 
//!   other chars are allowed and will be encoded if necessary. 
//! 
//! @param info 
//!   An optional mapping containing arbitrary info about the event. 
//!   The entries here can be accessed as @expr{$@} format specifiers 
//!   in the @expr{LogFormat@} configuration variable. 
//! 
//!   The values must be castable to strings. The strings should 
//!   preferably contain URI valid chars only, but other chars are 
//!   allowed and will be encoded if necessary. 
//! 
//!   The strings should preferably never be empty. If a string might 
//!   be, it should be documented in the doc blurb for the 
//!   @expr{LogFormat@} configuration variable. 
//! 
//!   Most but not all of the predefined format specifiers can be 
//!   overridden this way, but if any is overridden it should map very 
//!   closely to the syntax and semantics of the original. 
//! 
//!   Note that "@expr{_@}" cannot be used in names in the indices 
//!   here since the log formatter code replaces "@expr{_@}" with 
//!   "@expr{-@}" before doing lookups. 
//! 
//! @note 
//! Events should be documented in the doc blurb for the 
//! @expr{LogFormat@} configuration variable. 
{ 
  // Currently this bypasses logger modules. Might change in the future. 
 
  if( !log_function )  
    return; // No file is open for logging. 
 
  if(do_not_log_patterns && 
     Roxen._match("0.0.0.0", do_not_log_patterns)) 
    return; 
 
  sscanf (facility, "%[^#]", string modname); 
 
  if (string format = 
      log_format[facility + "/" + action] || 
      log_format[facility + "/*"] || 
      // Also try without the module copy number if the facility 
      // appears to be a module identifier. 
      modname != "" && (log_format[modname + "/" + action] || 
                        log_format[modname + "/*"]) || 
      log_format["*/*"]) 
    roxen.run_log_event_format (format, log_function, 
                                facility, action, resource, info); 
} 
 
array(string) userinfo(string u, RequestID|void id) 
//! @note 
//!   DEPRECATED COMPATIBILITY FUNCTION 
//!  
//! Fetches user information from the authentication module by calling 
//! its userinfo() method. Returns zero if no auth module was present. 
//! 
//! Note that you should always supply id if it's possible, some user 
//! databases require it (such as the htaccess database) 
{ 
  User uid; 
  foreach( user_databases(), UserDB m ) 
    if( uid = m->find_user( u ) ) 
      return uid->compat_userinfo(id); 
} 
 
array(string) userlist(RequestID|void id) 
//! @note 
//!   DEPRECATED COMPATIBILITY FUNCTION 
//!  
//! Fetches the full list of valid usernames from the authentication 
//! module by calling its userlist() method. Returns zero if no auth 
//! module was present. 
//! 
//! Note that you should always supply id if it's possible, some user 
//! databases require it (such as the htaccess database) 
{ 
  array(string) list = ({}); 
  foreach( user_databases(), UserDB m ) 
    list |= m->list_users(id); 
  return list; 
} 
 
array(string) user_from_uid(int u, RequestID|void id) 
//! @note 
//!   DEPRECATED COMPATIBILITY FUNCTION 
//!  
//! Return the user data for id u from the authentication module. The 
//! id parameter might be left out if FTP. Returns zero if no auth 
//! module was present. 
//! 
//! Note that you should always supply id if it's possible, some user 
//! databases require it (such as the htaccess database) 
{ 
  User uid; 
  foreach( user_databases(), UserDB m ) 
    if( uid = m->find_user_from_uid( u,id ) ) 
      return uid->compat_userinfo(); 
} 
 
UserDB find_user_database( string name ) 
//! Given a user database name, returns it if it exists in this 
//! configuration, otherwise returns 0. 
{ 
  foreach( user_databases(), UserDB m ) 
    if( m->name == name ) 
      return m; 
} 
 
AuthModule find_auth_module( string name ) 
//! Given a authentication method name, returns it if it exists in 
//! this configuration, otherwise returns 0. 
{ 
  foreach( auth_modules(), AuthModule m ) 
    if( m->name == name ) 
      return m; 
} 
 
User authenticate( RequestID id, UserDB|void database) 
//! Try to authenticate the request with users from the specified user 
//! database. If no @[database] is specified, all datbases in the 
//! current configuration are searched in priority order. 
//! 
//! The return value is the autenticated user. 
//! id->misc->authenticated_user is always set to the return value. 
{ 
  User u; 
  if (!zero_type (u = id->misc->authenticated_user)) 
    return u; 
  foreach( auth_modules(), AuthModule method ) 
    if( u = method->authenticate( id, database ) ) 
      return id->misc->authenticated_user = u; 
} 
 
mapping authenticate_throw( RequestID id, string realm, 
                            UserDB|void database) 
//! Returns a reply mapping, similar to @[Roxen.http_rxml_reply] with 
//! friends. If no @[database] is specified, all datbases in the 
//! current configuration are searched in priority order. 
{ 
  mapping m; 
  foreach( auth_modules(), AuthModule method ) 
    if( m  = method->authenticate_throw( id, realm, database ) ) 
      return m; 
} 
 
User find_user( string user, RequestID|void id ) 
//! Tries to find the specified user in the currently available user 
//! databases. If id is specified, this function defaults to the 
//! database that the currently authenticated user came from, if any. 
//! 
//! The other user databases are processed in priority order 
//! 
//! Note that you should always supply id if it's possible, some user 
//! databases require it (such as the htaccess database) 
{ 
  User uid; 
 
  if( id && id->misc->authenticated_user 
      && ( uid = id->misc->authenticated_user->database->find_user(user,id))) 
    return uid; 
   
  foreach( user_databases(), UserDB m ) 
    if( uid = m->find_user( user,id ) ) 
      return uid; 
} 
 
array(string) list_users(RequestID|void id) 
//! Fetches the full list of valid usernames from the authentication 
//! modules by calling the list-users() methods. 
//! 
//! Note that you should always supply id if it's possible, some user 
//! databases require it (such as the htaccess database) 
{ 
  array(string) list = ({}); 
  foreach( user_databases(), UserDB m ) 
    list |= m->list_users(id); 
  return list; 
} 
 
array(string) list_groups(RequestID|void id) 
//! Fetches the full list of valid groupnames from the authentication 
//! modules by calling the list-users() methods. 
//! 
//! Note that you should always supply id if it's possible, some user 
//! databases require it (such as the htaccess database) 
{ 
  array(string) list = ({}); 
  foreach( user_databases(), UserDB m ) 
    list |= m->list_groups(id); 
  return list; 
} 
 
 
 
Group find_group( string group, RequestID|void id ) 
//! Tries to find the specified group in the currently available user 
//! databases. If id is specified, this function defaults to the 
//! database that the currently authenticated user came from, if any. 
//! 
//! The other user databases are processed in priority order 
//! 
//! Note that you should always supply id if it's possible, some user 
//! databases require it (such as the htaccess database) 
{ 
  Group uid; 
 
  if( id && id->misc->authenticated_user 
      && ( uid = id->misc->authenticated_user->database->find_group( group ) )) 
    return uid; 
   
  foreach( user_databases(), UserDB m ) 
    if( uid = m->find_group( group,id ) ) 
      return uid; 
} 
 
 
string last_modified_by(Stdio.File file, RequestID id) 
{ 
  Stat s; 
  int uid; 
  array u; 
 
  if(objectp(file)) s=file->stat(); 
  if(!s || sizeof(s)<5) return "A. Nonymous"; 
  uid=s[5]; 
  u=user_from_uid(uid, id); 
  if(u) return u[0]; 
  return "A. Nonymous"; 
} 
 
 
 
 
// Some clients does _not_ handle the magic 'internal-gopher-...'. 
// So, lets do it here instead. 
private mapping internal_gopher_image(string from) 
{ 
  sscanf(from, "%s.gif", from); 
  sscanf(from, "%s.jpg", from); 
  from -= "."; 
  // Disallow "internal-gopher-..", it won't really do much harm, but a list of 
  // all files in '..' might be retrieved (that is, the actual directory 
  // file was sent to the browser) 
  Stdio.File f = lopen("roxen-images/dir/"+from+".gif","r"); 
  if (f)  
    return (["file":f, "type":"image/gif", "stat":f->stat(),]); 
  else 
    return 0; 
  // File not found. 
} 
 
#ifdef MODULE_LEVEL_SECURITY 
private mapping(RoxenModule:array) security_level_cache = set_weak_flag (([]), 1); 
 
int|mapping check_security(function|RoxenModule a, RequestID id, 
                           void|int slevel) 
{ 
  array seclevels; 
  // NOTE: 
  //   ip_ok and auth_ok are three-state variables. 
  //   Valid contents for them are: 
  //     0  Unknown state -- No such restriction encountered yet. 
  //     1  May be bad -- Restriction encountered, and test failed. 
  //    ~0  OK -- Test passed. 
 
  if (RoxenModule mod = Roxen.get_owning_module (a)) { 
    // Only store the module objects in the cache and not `a' directly 
    // since it can be (in) an object that is very short lived. 
    if (!(seclevels = security_level_cache[mod])) { 
      if(mod->query_seclevels) 
        seclevels = ({ 
          mod->query_seclevels(), 
          mod->query("_seclvl"), 
        }); 
      else 
        seclevels = ({0,0}); 
      security_level_cache[mod] = seclevels; 
    } 
  } 
  else 
    seclevels = ({0,0}); 
 
  if(slevel && (seclevels[1] > slevel)) // "Trustlevel" to low. 
    // Regarding memory cache: This won't have any impact, since it's 
    // always the same, regardless of the client requesting the file. 
    return 1; 
 
  mixed err; 
  if( function(RequestID:int|mapping) f = seclevels[0] ) 
    // And here we don't have to take notice of the RAM-cache either, 
    // since the security patterns themselves does that. 
    // 
    // All patterns that varies depending on the client must use 
    // NOCACHE(), to force the request to be uncached. 
    // 
    err=catch { return f( id ); }; 
  else 
    return 0; // Ok if there are no patterns. 
 
  report_error("check_security(): %s:\n%s\n", 
               LOC_M(39, "Error during module security check"), 
               describe_backtrace(err)); 
 
  return 1; 
} 
#endif 
// Empty all the caches above. 
void invalidate_cache() 
{ 
  last_module_cache = 0; 
  filter_module_cache = 0; 
  userdb_module_cache = 0; 
  auth_module_cache = 0; 
  first_module_cache = 0; 
  url_module_cache = 0; 
  location_module_cache = 0; 
  logger_module_cache = 0; 
  file_extension_module_cache = ([]); 
  provider_module_cache = ([]); 
#ifdef MODULE_LEVEL_SECURITY 
  security_level_cache = set_weak_flag (([ ]), 1); 
#endif 
} 
 
// Empty all the caches above AND the ones in the loaded modules. 
void clear_memory_caches() 
{ 
  invalidate_cache(); 
  foreach(indices(otomod), RoxenModule m) 
    if (m && m->clear_memory_caches) 
      if (mixed err = catch( m->clear_memory_caches() )) 
        report_error("clear_memory_caches() "+ 
                     LOC_M(40, "failed for module %O:\n%s\n"), 
                     otomod[m], describe_backtrace(err)); 
} 
 
//  Returns tuple < image, mime-type > 
static array(string) draw_saturation_bar(int hue,int brightness, int where, 
                                         int small_version) 
{ 
  Image.Image bar = 
    small_version ? Image.Image(16, 128) : Image.Image(30, 256); 
   
  for(int i=0;i<128;i++) 
  { 
    int j = i * 2; 
    array color = hsv_to_rgb(hue, 255 - j, brightness); 
    if (small_version) { 
      bar->line(0, i, 15, i, @color); 
    } else { 
      bar->line(0, j, 29, j, @color); 
      bar->line(0, j + 1,29, j + 1, @color); 
    } 
  } 
 
  if (where >= 0 && where <= 255) { 
    where = 255 - where; 
    int hilite = (brightness > 128) ? 0 : 255; 
    if (small_version) 
      bar->line(0, where / 2, 15, where / 2, hilite, hilite, hilite); 
    else 
      bar->line(0, where, 29, where, hilite, hilite, hilite); 
  } 
   
#if constant(Image.JPEG) && constant(Image.JPEG.encode) 
  return ({ Image.JPEG.encode(bar), "image/jpeg" }); 
#else 
  return ({ Image.PNG.encode(bar), "image/png" }); 
#endif 
} 
 
 
#if constant(Image.GIF) && constant(Image.PNG) 
array(mapping) spinner_data = 0; 
 
//  Returns tuple < image, mime type > 
static array(string) draw_spinner(string bgcolor) 
{ 
  //  Parse color 
  array color = parse_color(bgcolor); 
   
  //  Load all spinner PNGs 
  if (!spinner_data) { 
    array(mapping) temp_spinner_data = ({ }); 
    for (int i = 0; i < 12; i++) { 
      string src = lopen("roxen-images/spinner" + i + ".png", "r")->read(); 
      temp_spinner_data += ({ Image.PNG._decode(src) }); 
    } 
    spinner_data = temp_spinner_data; 
  } 
   
  //  Create non-transparent Image object for each frame 
  array(Image.Image) frames = ({ }); 
  foreach(spinner_data, mapping data) { 
    Image.Image frame = Image.Image(17, 17, @color); 
    frame->paste_mask(data->image, data->alpha); 
    frames += ({ frame }); 
  } 
   
  //  Create animated GIF using colortable based on first frame (all of 
  //  them have the same set of colors) 
  Image.Colortable colors = Image.Colortable(frames[0]); 
  string res = Image.GIF.header_block(17, 17, colors); 
  foreach(frames, Image.Image frame) 
    res += Image.GIF.render_block(frame, colors, 0, 0, 0, 1); 
  res += 
    Image.GIF.netscape_loop_block(0) + 
    Image.GIF.end_block(); 
   
  return ({ res, "image/gif" }); 
} 
#endif 
 
 
// Inspired by the internal-gopher-... thingie, this is the images 
// from the administration interface. :-) 
private mapping internal_roxen_image( string from, RequestID id ) 
{ 
  sscanf(from, "%s.gif", from); 
  sscanf(from, "%s.jpg", from); 
  sscanf(from, "%s.xcf", from); 
  sscanf(from, "%s.png", from); 
 
#if constant(Image.GIF) && constant(Image.PNG) 
  //  Animated spinner image 
  if (has_prefix(from, "spinner-")) { 
    array(string) spinner = draw_spinner(from[8..]); 
    return ([ "data" : spinner[0], 
              "type" : spinner[1], 
              "stat" : ({ 0, 0, 0, 900000000, 0, 0, 0 }) ]); 
  } 
#endif 
 
  // Automatically generated colorbar. Used by wizard code... 
  int hue,bright,w; 
  string colorbar; 
  if(sscanf(from, "%s:%d,%d,%d", colorbar, hue, bright,w)==4) { 
    array bar = draw_saturation_bar(hue, bright, w, 
                                    colorbar == "colorbar-small"); 
    return Roxen.http_string_answer(bar[0], bar[1]); 
  } 
 
  Stdio.File f; 
 
  if( !id->misc->internal_get ) 
    if(f = lopen("roxen-images/"+from+".gif", "r")) 
      return (["file":f, "type":"image/gif", "stat":f->stat()]); 
 
  if(f = lopen("roxen-images/"+from+".png", "r")) 
    return (["file":f, "type":"image/png", "stat":f->stat()]); 
 
  if(f = lopen("roxen-images/"+from+".jpg", "r")) 
    return (["file":f, "type":"image/jpeg", "stat":f->stat()]); 
 
  if(f = lopen("roxen-images/"+from+".xcf", "r")) 
    return (["file":f, "type":"image/x-gimp-image", "stat":f->stat()]); 
 
  if(f = lopen("roxen-images/"+from+".gif", "r")) 
    return (["file":f, "type":"image/gif", "stat":f->stat()]); 
  // File not found. 
  return 0; 
} 
 
 
mapping (mixed:function|int) locks = ([]); 
 
#ifdef THREADS 
// import Thread; 
 
mapping locked = ([]), thread_safe = ([]); 
 
mixed _lock(object|function f) 
{ 
  Thread.MutexKey key; 
  function|int l; 
  TIMER_START(module_lock); 
  if (functionp(f)) { 
    f = function_object(f); 
  } 
  if (l = locks[f]) 
  { 
    if (l != -1) 
    { 
      // Allow recursive locks. 
      catch{ 
        // report_debug("lock %O\n", f); 
        locked[f]++; 
        key = l(); 
      }; 
    } else 
      thread_safe[f]++; 
  } else if (f->thread_safe) { 
    locks[f]=-1; 
    thread_safe[f]++; 
  } else { 
    if (!locks[f]) 
    { 
      // Needed to avoid race-condition. 
      l = Thread.Mutex()->lock; 
      if (!locks[f]) { 
        locks[f]=l; 
      } 
    } 
    // report_debug("lock %O\n", f); 
    locked[f]++; 
    key = l(); 
  } 
  TIMER_END(module_lock); 
  return key; 
} 
 
#define LOCK(X) key=_lock(X) 
#define UNLOCK() do{key=0;}while(0) 
#else 
#define LOCK(X) 
#define UNLOCK() 
#endif 
 
string examine_return_mapping(mapping m) 
{ 
   string res; 
 
   if (m->extra_heads) 
      m->extra_heads=mkmapping(Array.map(indices(m->extra_heads), 
                                         lower_case), 
                               values(m->extra_heads)); 
   else 
      m->extra_heads=([]); 
 
   switch (m->error||200) 
   { 
      case 302: // redirect 
         if (m->extra_heads && 
             (m->extra_heads->location)) 
           res = sprintf("Returned redirect to %O ", m->extra_heads->location); 
         else 
           res = "Returned redirect, but no location header. "; 
         break; 
 
      case 401: 
         if (m->extra_heads["www-authenticate"]) 
           res = sprintf("Returned authentication failed: %O ", 
                         m->extra_heads["www-authenticate"]); 
         else 
           res = "Returned authentication failed. "; 
         break; 
 
      case 200: 
         res = "Returned ok. "; 
         break; 
 
      default: 
         res = sprintf("Returned %O. ", m->error); 
   } 
 
   if (!zero_type(m->len)) 
      if (m->len<0) 
         res += "No data "; 
      else 
         res += sprintf("%O bytes ", m->len); 
   else if (stringp(m->data)) 
     res += sprintf("%d bytes ", strlen(m->data)); 
   else if (objectp(m->file)) 
      if (catch { 
         Stat a=m->file->stat(); 
         res += sprintf("%O bytes ", a[1]-m->file->tell()); 
      }) 
        res += "? bytes "; 
 
   if (m->data) res += "(static)"; 
   else if (m->file) res += "(open file)"; 
 
   if (stringp(m->extra_heads["content-type"]) || 
       stringp(m->type)) { 
      res += sprintf(" of %O", m->type||m->extra_heads["content-type"]); 
   } 
 
   res+="<br />"; 
 
   return res; 
} 
 
//! Find all applicable locks for this user on @[path]. 
multiset(DAVLock) find_locks(string path, int(0..1) recursive, 
                             int(0..1) exclude_shared, RequestID id) 
{ 
  SIMPLE_TRACE_ENTER(0, "find_locks(%O, %O, %O, X)", 
                     path, recursive, exclude_shared); 
  multiset(DAVLock) locks = (<>); 
 
  foreach(location_module_cache||location_modules(), 
          [string loc, function func]) 
  { 
    SIMPLE_TRACE_ENTER(function_object(func), 
                       "Finding locks in %O.", loc); 
    string subpath; 
    if (has_prefix(path, loc)) { 
      // path == loc + subpath. 
      subpath = path[sizeof(loc)..]; 
    } else if (recursive && has_prefix(loc, path)) { 
      // loc == path + ignored. 
      subpath = "/"; 
    } else { 
      // Does not apply to this location module. 
      TRACE_LEAVE("Skip this module."); 
      continue; 
    } 
    TRACE_ENTER(sprintf("subpath: %O", subpath), 
                function_object(func)->find_locks); 
    multiset(DAVLock) sub_locks = 
      function_object(func)->find_locks(subpath, recursive, 
                                        exclude_shared, id); 
    TRACE_LEAVE(""); 
    if (sub_locks) { 
      SIMPLE_TRACE_LEAVE("Got some locks: %O", sub_locks); 
      locks |= sub_locks; 
    } else { 
      TRACE_LEAVE("Got no locks."); 
    } 
  } 
  SIMPLE_TRACE_LEAVE("Returning %O", locks); 
  return locks; 
} 
 
//! Check if there are any applicable locks for this user on @[path]. 
DAVLock|LockFlag check_locks(string path, int(0..1) recursive, RequestID id) 
{ 
  LockFlag state = 0; 
  foreach(location_module_cache||location_modules(), 
          [string loc, function func]) 
  { 
    string subpath; 
    int check_above; 
    if (has_prefix(path, loc)) { 
      // path == loc + subpath. 
      subpath = path[sizeof(loc)..]; 
    } else if (recursive && has_prefix(loc, path)) { 
      // loc == path + ignored. 
      subpath = ""; 
      check_above = 1; 
    } else { 
      // Does not apply to this location module. 
      continue; 
    } 
    int/*LockFlag*/|DAVLock lock_info = 
      function_object(func)->check_locks(subpath, recursive, id); 
    if (objectp(lock_info)) { 
      if (!check_above) { 
        return lock_info; 
      } else { 
        lock_info = LOCK_OWN_BELOW; // We have a lock on some subpath. 
      } 
    } 
    else 
      if (check_above && (lock_info & 1)) 
        // Convert LOCK_*_AT to LOCK_*_BELOW. 
        lock_info &= ~1; 
    if (lock_info > state) state = lock_info; 
    if (state == LOCK_EXCL_AT) return LOCK_EXCL_AT; // Doesn't get any worse. 
  } 
  return state; 
} 
 
static multiset(DAVLock) active_locks = (<>); 
 
//! Unlock the lock represented by @[lock] on @[path]. 
//! 
//! @returns 
//!   Returns a result-mapping on error, and @expr{0@} (zero) on success. 
mapping(string:mixed) unlock_file(string path, DAVLock lock, RequestID id) 
{ 
  // Canonicalize path. 
  if (!has_suffix(path, "/")) path+="/"; 
 
  foreach(location_module_cache||location_modules(), 
          [string loc, function func]) 
  { 
    if (has_prefix(path, loc)) { 
      // path == loc + subpath. 
      mapping(string:mixed) ret = 
        function_object(func)->unlock_file(path[sizeof(loc)..], lock, id); 
 
      // FIXME: Semantics for partial unlocking? 
      if (ret) return ret; 
    } else if (lock->recursive && has_prefix(loc, path)) { 
      // loc == path + ignored. 
      mapping(string:mixed) ret = 
        function_object(func)->unlock_file("/", lock, id); 
 
      // FIXME: Semantics for partial unlocking? 
      if (ret) return ret; 
    } 
  } 
  active_locks[lock] = 0; 
  // destruct(lock); 
  return 0; 
} 
 
//! Force expiration of any locks that have timed out. 
int expire_locks(RequestID id) 
{ 
  int t = time(1); 
  int min_time = 0x7fffffff; 
  foreach(active_locks; DAVLock lock;) { 
    if (lock->expiry_time) { 
      if (lock->expiry_time < t) { 
        unlock_file(lock->path, lock, id); 
      } else if (lock->expiry_time < min_time) { 
        min_time = lock->expiry_time; 
      } 
    } 
  } 
  return min_time - t; 
} 
 
static void expire_lock_loop() 
{ 
  int t = expire_locks(0);  // NOTE: Called with RequestID 0! 
 
  if (sizeof(active_locks)) { 
    // Expire locks at least once every hour. 
    if (t < 3600) { 
      roxen.background_run(t, expire_lock_loop); 
    } else { 
      roxen.background_run(3600, expire_lock_loop); 
    } 
  } 
} 
 
//! Refresh a lock. 
//! 
//! Update the expiry time for the lock. 
void refresh_lock(DAVLock lock) 
{ 
  if (lock->expiry_delta) { 
    lock->expiry_time = lock->expiry_delta + time(1); 
  } 
} 
 
//! Attempt to lock @[path]. 
//! 
//! @param path 
//!   Path to lock. 
//! 
//! @param locktype 
//!   Type of lock (currently only @expr{"DAV:write"@} is defined). 
//! 
//! @param lockscope 
//!   Scope of lock either @expr{"DAV:exclusive"@} or 
//!   @expr{"DAV:shared"@}. 
//! 
//! @param expiry_delta 
//!   Idle time in seconds before the lock expires. @expr{0@} (zero) 
//!   means no expiry. 
//! 
//! @returns 
//!   Returns a result mapping on failure, 
//!   and the resulting @[DAVLock] on success. 
mapping(string:mixed)|DAVLock lock_file(string path, 
                                        int(0..1) recursive, 
                                        string lockscope, 
                                        string locktype, 
                                        int(0..) expiry_delta, 
                                        array(Parser.XML.Tree.Node) owner, 
                                        RequestID id) 
{ 
  // Canonicalize path. 
  if (!has_suffix(path, "/")) path+="/"; 
 
  // First check if there's already some lock on path that prevents 
  // us from locking it. 
  int/*LockFlag*/|DAVLock lock_info = check_locks(path, recursive, id); 
 
  if (!intp(lock_info)) { 
    // We already hold a lock that prevents us. 
    if (id->request_headers->if) { 
      return Roxen.http_status(412, "Precondition Failed"); 
    } else { 
      return Roxen.http_status(423, "Locked"); 
    } 
  } else if (lockscope == "DAV:exclusive" ? 
             lock_info >= LOCK_SHARED_BELOW : 
             lock_info >= LOCK_OWN_BELOW) { 
    // Some other lock prevents us. 
    return Roxen.http_status(423, "Locked"); 
  } 
 
  // Create the new lock. 
 
  string locktoken = "opaquelocktoken:" + roxen->new_uuid_string(); 
  DAVLock lock = DAVLock(locktoken, path, recursive, lockscope, locktype, 
                         expiry_delta, owner); 
  foreach(location_module_cache||location_modules(), 
          [string loc, function func]) 
  { 
    string subpath; 
    if (has_prefix(path, loc)) { 
      // path == loc + subpath. 
      subpath = path[sizeof(loc)..]; 
    } else if (recursive && has_prefix(loc, path)) { 
      // loc == path + ignored. 
      subpath = "/"; 
    } else { 
      // Does not apply to this location module. 
      continue; 
    } 
 
    mapping(string:mixed) lock_error = 
      function_object(func)->lock_file(subpath, lock, id); 
    if (lock_error) { 
      // Failure. Unlock the new lock. 
      foreach(location_module_cache||location_modules(), 
              [string loc2, function func2]) 
      { 
        if (has_prefix(path, loc2)) { 
          // path == loc2 + subpath. 
          mapping(string:mixed) ret = 
            function_object(func2)->unlock_file(path[sizeof(loc2)..], 
                                                lock, id); 
        } else if (recursive && has_prefix(loc2, path)) { 
          // loc2 == path + ignored. 
          mapping(string:mixed) ret = 
            function_object(func2)->unlock_file("/", lock, id); 
        } 
        if (func == func2) break; 
      } 
      // destruct(lock); 
      return lock_error; 
    } 
  } 
 
  if (expiry_delta) { 
    // Lock with timeout. 
    // FIXME: Race-conditions. 
    if (!sizeof(active_locks)) { 
      // Start the lock expiration loop. 
      active_locks[lock] = 1; 
      expire_lock_loop(); 
    } else { 
      active_locks[lock] = 1; 
    } 
  } 
 
  // Success. 
  return lock; 
} 
 
mapping|int(-1..0) low_get_file(RequestID id, int|void no_magic) 
//! The function that actually tries to find the data requested. All 
//! modules except last and filter type modules are mapped, in order, 
//! and the first one that returns a suitable response is used. If 
//! `no_magic' is set to one, the internal magic roxen images and the 
//! @[find_internal()] callbacks will be ignored. 
//! 
//! The return values 0 (no such file) and -1 (the data is a 
//! directory) are only returned when `no_magic' was set to 1; 
//! otherwise a result mapping is always generated. 
{ 
#ifdef MODULE_LEVEL_SECURITY 
  int slevel; 
#endif 
 
#ifdef THREADS 
  Thread.MutexKey key; 
#endif 
 
  id->not_query = VFS.normalize_path( id->not_query ); 
 
  TRACE_ENTER(sprintf("Request for %s", id->not_query), 0); 
 
  string file=id->not_query; 
  string loc; 
  function funp; 
  mixed tmp, tmp2; 
  mapping|object(Stdio.File)|int fid; 
 
  if(!no_magic) 
  { 
    TIMER_START(internal_magic); 
#ifndef NO_INTERNAL_HACK 
    // Find internal-foo-bar images 
    // min length == 17 (/internal-roxen-?..) 
    // This will save some time indeed. 
    string type; 
    if(sizeof(file) > 17 && 
#if ROXEN_COMPAT <= 2.1 
       (file[0] == '/') && 
       sscanf(file, "%*s/internal-%s-%[^/]", type, loc) == 3 
#else 
       sscanf(file, "/internal-%s-%[^/]", type, loc) == 2 
#endif 
       ) { 
      switch(type) { 
       case "roxen": 
        //  Mark all /internal-roxen-* as cacheable even though the user might be 
        //  authenticated (which normally disables protocol-level caching). 
        RAISE_CACHE(60 * 60 * 24 * 365);  //  1 year 
        PROTO_CACHE(); 
         
        TRACE_LEAVE("Magic internal roxen image"); 
        if(loc=="unit" || loc=="pixel-of-destiny") 
        { 
          TIMER_END(internal_magic); 
          return (["data":"GIF89a\1\0\1\0\200ÿ\0ÀÀÀ\0\0\0!ù\4\1\0\0\0\0," 
                   "\0\0\0\0\1\0\1\0\0\1\1""2\0;", 
                   "type":"image/gif", 
                   "stat": ({0, 0, 0, 900000000, 0, 0, 0})]); 
        } 
        if(has_prefix(loc, "pixel-")) 
        { 
          TIMER_END(internal_magic); 
          return (["data":sprintf("GIF89a\1\0\1\0\200\0\0\0\0\0%c%c%c,\0\0\0" 
                                  "\0\1\0\1\0\0\2\2L\1\0;", 
                                  @parse_color(loc[6..])), 
                   "type":"image/gif", 
                   "stat": ({0, 0, 0, 900000000, 0, 0, 0})]); 
        } 
        TIMER_END(internal_magic); 
        return internal_roxen_image(loc, id); 
 
       case "gopher": 
        TRACE_LEAVE("Magic internal gopher image"); 
        TIMER_END(internal_magic); 
        return internal_gopher_image(loc); 
      } 
    } 
#endif 
 
    // Locate internal location resources. 
    if(has_prefix(file, internal_location)) 
    { 
      TRACE_ENTER("Magic internal module location", 0); 
      RoxenModule module; 
      string name, rest; 
      function find_internal; 
      if(2==sscanf(file[strlen(internal_location)..], "%s/%s", name, rest) && 
         (module = find_module(replace(name, "!", "#"))) && 
         (find_internal = module->find_internal)) 
      { 
#ifdef MODULE_LEVEL_SECURITY 
      if(tmp2 = check_security(find_internal, id, slevel)) 
          if(intp(tmp2)) 
          { 
            TRACE_LEAVE("Permission to access module denied."); 
            find_internal = 0; 
          } else { 
            TRACE_LEAVE(""); 
            TRACE_LEAVE("Request denied."); 
            TIMER_END(internal_magic); 
            return tmp2; 
          } 
#endif 
      if(find_internal) 
        { 
          TRACE_ENTER("Calling find_internal()...", find_internal); 
          PROF_ENTER("find_internal","location"); 
          LOCK(find_internal); 
          fid=find_internal( rest, id ); 
          UNLOCK(); 
          //TRACE_LEAVE(sprintf("find_internal has returned %O", fid)); 
          TRACE_LEAVE(""); 
          PROF_LEAVE("find_internal","location"); 
          if(fid) 
          { 
            if(mappingp(fid)) 
            { 
              TRACE_LEAVE(""); 
              TRACE_LEAVE(examine_return_mapping(fid)); 
              TIMER_END(internal_magic); 
              return fid; 
            } 
            else 
            { 
#ifdef MODULE_LEVEL_SECURITY 
            int oslevel = slevel; 
              array slca; 
              if(slca = security_level_cache[ Roxen.get_owning_module (find_internal) ]) 
                slevel = slca[1]; 
              // security_level_cache from 
              // check_security 
              id->misc->seclevel = slevel; 
#endif 
            if(objectp(fid)) 
                TRACE_LEAVE("Returned open filedescriptor. " 
#ifdef MODULE_LEVEL_SECURITY 
                          +(slevel != oslevel? 
                              sprintf(" The security level is now %d.", slevel):"") 
#endif 
                          ); 
              else 
                TRACE_LEAVE("Returned directory indicator." 
#ifdef MODULE_LEVEL_SECURITY 
                          +(oslevel != slevel? 
                              sprintf(" The security level is now %d.", slevel):"") 
#endif 
                          ); 
            } 
          } else 
            TRACE_LEAVE(""); 
        } else 
          TRACE_LEAVE(""); 
      } else 
        TRACE_LEAVE(""); 
    } 
    TIMER_END(internal_magic); 
  } 
 
  // Well, this just _might_ be somewhat over-optimized, since it is 
  // quite unreadable, but, you cannot win them all.. 
  if(!fid) 
  { 
#ifdef URL_MODULES 
  // Map URL-modules 
    TIMER_START(url_modules); 
    foreach(url_module_cache||url_modules(), funp) 
    { 
      PROF_ENTER(Roxen.get_owning_module(funp)->module_name,"url module"); 
      LOCK(funp); 
      TRACE_ENTER("URL module", funp); 
      tmp=funp( id, file ); 
      UNLOCK(); 
      PROF_LEAVE(Roxen.get_owning_module(funp)->module_name,"url module"); 
 
      if(mappingp(tmp)) 
      { 
        TRACE_LEAVE(""); 
        TRACE_LEAVE("Returning data"); 
        TIMER_END(url_modules); 
        return tmp; 
      } 
      if(objectp( tmp )) 
      { 
        mixed err; 
 
        id->misc->get_file_nest++; 
        err = catch { 
          if( id->misc->get_file_nest < 20 ) 
            tmp = (id->conf || this_object())->low_get_file( tmp, no_magic ); 
          else 
          { 
            TRACE_LEAVE("Too deep recursion"); 
            error("Too deep recursion in roxen::get_file() while mapping " 
                  +file+".\n"); 
          } 
        }; 
        id->misc->get_file_nest = 0; 
        if(err) throw(err); 
        TRACE_LEAVE(""); 
        TRACE_LEAVE("Returning data"); 
        TIMER_END(url_modules); 
        return tmp; 
      } 
      TRACE_LEAVE(""); 
      TIMER_END(url_modules); 
    } 
#endif 
 
    TIMER_START(location_modules); 
    foreach(location_module_cache||location_modules(), tmp) 
    { 
      loc = tmp[0]; 
      if(has_prefix(file, loc)) 
      { 
        TRACE_ENTER(sprintf("Location module [%s] ", loc), tmp[1]); 
#ifdef MODULE_LEVEL_SECURITY 
      if(tmp2 = check_security(tmp[1], id, slevel)) 
          if(intp(tmp2)) 
          { 
            TRACE_LEAVE("Permission to access module denied."); 
            continue; 
          } else { 
            TRACE_LEAVE(""); 
            TRACE_LEAVE("Request denied."); 
            TIMER_END(location_modules); 
            return tmp2; 
          } 
#endif 
      PROF_ENTER(Roxen.get_owning_module(tmp[1])->module_name,"location"); 
        TRACE_ENTER("Calling find_file()...", 0); 
        LOCK(tmp[1]); 
        fid=tmp[1]( file[ strlen(loc) .. ] + id->extra_extension, id); 
        UNLOCK(); 
        TRACE_LEAVE(""); 
        PROF_LEAVE(Roxen.get_owning_module(tmp[1])->module_name,"location"); 
        if(fid) 
        { 
          id->virtfile = loc; 
 
          if(mappingp(fid)) 
          { 
            TRACE_LEAVE(""); // Location module [...] 
            TRACE_LEAVE(examine_return_mapping(fid)); 
            TIMER_END(location_modules); 
            return fid; 
          } 
          else 
          { 
#ifdef MODULE_LEVEL_SECURITY 
          int oslevel = slevel; 
            array slca; 
            if(slca = security_level_cache[ Roxen.get_owning_module (tmp[1]) ]) 
              slevel = slca[1]; 
            // security_level_cache from 
            // check_security 
            id->misc->seclevel = slevel; 
#endif 
          if(objectp(fid)) 
              TRACE_LEAVE("Returned open filedescriptor." 
#ifdef MODULE_LEVEL_SECURITY 
                        +(slevel != oslevel? 
                            sprintf(" The security level is now %d.", slevel):"") 
#endif 
 
                          ); 
            else 
              TRACE_LEAVE("Returned directory indicator." 
#ifdef MODULE_LEVEL_SECURITY 
                        +(oslevel != slevel? 
                            sprintf(" The security level is now %d.", slevel):"") 
#endif 
                        ); 
            break; 
          } 
        } else 
          TRACE_LEAVE(""); 
      } else if(strlen(loc)-1==strlen(file) && file+"/" == loc) { 
        // This one is here to allow accesses to /local, even if 
        // the mountpoint is /local/. It will slow things down, but... 
 
        TRACE_ENTER("Automatic redirect to location_module.", tmp[1]); 
        TRACE_LEAVE(""); 
        TRACE_LEAVE("Returning data"); 
 
        // Keep query (if any). 
        // FIXME: Should probably keep config <foo> 
        string new_query = Roxen.http_encode_invalids(id->not_query) + "/" + 
          (id->query?("?"+id->query):""); 
        new_query=Roxen.add_pre_state(new_query, id->prestate); 
 
        TIMER_END(location_modules); 
        return Roxen.http_redirect(new_query, id); 
      } 
    } 
    TIMER_END(location_modules); 
  } 
 
  if(fid == -1) 
  { 
    if(no_magic) 
    { 
      TRACE_LEAVE("No magic requested. Returning -1."); 
      return -1; 
    } 
    TIMER_START(directory_module); 
    if(dir_module) 
    { 
      PROF_ENTER(dir_module->module_name,"directory"); 
      LOCK(dir_module); 
      TRACE_ENTER("Directory module", dir_module); 
      fid = dir_module->parse_directory(id); 
      TRACE_LEAVE(""); 
      UNLOCK(); 
      PROF_LEAVE(dir_module->module_name,"directory"); 
    } 
    else 
    { 
      TRACE_LEAVE("No directory module. Returning 'no such file'"); 
      return 0; 
    } 
    TIMER_END(directory_module); 
    if(mappingp(fid)) 
    { 
      TRACE_LEAVE("Returning data"); 
      return (mapping)fid; 
    } 
  } 
 
  // Map the file extensions, but only if there is a file... 
  TIMER_START(extension_module); 
  if(objectp(fid) && 
     (tmp = file_extension_modules(loc = 
                                   lower_case(Roxen.extension(id->not_query, 
                                                              id))))) 
  { 
    foreach(tmp, funp) 
    { 
      TRACE_ENTER(sprintf("Extension module [%s] ", loc), funp); 
#ifdef MODULE_LEVEL_SECURITY 
      if(tmp=check_security(funp, id, slevel)) 
        if(intp(tmp)) 
        { 
          TRACE_LEAVE("Permission to access module denied."); 
          continue; 
        } 
        else 
        { 
          TRACE_LEAVE(""); 
          TRACE_LEAVE("Permission denied"); 
          TIMER_END(extension_module); 
          return tmp; 
        } 
#endif 
      PROF_ENTER(Roxen.get_owning_module(funp)->module_name,"ext"); 
      LOCK(funp); 
      tmp=funp(fid, loc, id); 
      UNLOCK(); 
      PROF_LEAVE(Roxen.get_owning_module(funp)->module_name,"ext"); 
      if(tmp) 
      { 
        if(!objectp(tmp)) 
        { 
          TRACE_LEAVE(""); 
          TRACE_LEAVE("Returning data"); 
          TIMER_END(extension_module); 
          return tmp; 
        } 
        if(fid && tmp != fid) 
          destruct(fid); 
        TRACE_LEAVE("Returned new open file"); 
        fid = tmp; 
        break; 
      } else 
        TRACE_LEAVE(""); 
    } 
  } 
  TIMER_END(extension_module); 
 
  if(objectp(fid)) 
  { 
    TIMER_START(content_type_module); 
    if(stringp(id->extension)) { 
      id->not_query += id->extension; 
      loc = lower_case(Roxen.extension(id->not_query, id)); 
    } 
    TRACE_ENTER("Content-type mapping module", types_module); 
    tmp=type_from_filename(id->not_query, 1, loc); 
    TRACE_LEAVE(tmp?sprintf("Returned type %s %s.", tmp[0], tmp[1]||"") 
                : "Missing type."); 
    if(tmp) 
    { 
      TRACE_LEAVE(""); 
      TIMER_END(content_type_module); 
      return ([ "file":fid, "type":tmp[0], "encoding":tmp[1] ]); 
    } 
    TRACE_LEAVE(""); 
    TIMER_END(content_type_module); 
    return ([ "file":fid, ]); 
  } 
 
  if(!fid) 
    TRACE_LEAVE("Returned 'no such file'."); 
  else 
    TRACE_LEAVE("Returning data"); 
  return fid; 
} 
 
#define TRY_FIRST_MODULES(FILE, RECURSE_CALL) do {                    \ 
    TIMER_START(first_modules);                                         \ 
    foreach(first_module_cache||first_modules(), function funp)         \ 
    {                                                                   \ 
      TRACE_ENTER ("First try module", funp);                           \ 
      if(FILE = funp( id )) {                                           \ 
        TRACE_LEAVE ("Got response");                                   \ 
        break;                                                          \ 
      }                                                                 \ 
      TRACE_LEAVE ("No response");                                      \ 
      if(id->conf != this_object()) {                                   \ 
        TRACE_ENTER (sprintf ("Configuration changed to %O - "          \ 
                              "redirecting", id->conf), 0);             \ 
        TRACE_LEAVE ("");                                               \ 
        TIMER_END (first_modules);                                      \ 
        TIMER_END (handle_request);                                     \ 
        return id->conf->RECURSE_CALL;                                  \ 
      }                                                                 \ 
    }                                                                   \ 
    TIMER_END(first_modules);                                           \ 
  } while (0) 
 
#define TRY_LAST_MODULES(FILE, RECURSE_CALL) do {                     \ 
    mixed ret;                                                          \ 
    TIMER_START(last_modules);                                          \ 
    foreach(last_module_cache||last_modules(), function funp) {         \ 
      TRACE_ENTER ("Last try module", funp);                            \ 
      if(ret = funp(id)) {                                              \ 
        if (ret == 1) {                                                 \ 
          TRACE_LEAVE ("Request rewritten - try again");                \ 
          TIMER_END(last_modules);                                      \ 
          TIMER_END(handle_request);                                    \ 
          return RECURSE_CALL;                                          \ 
        }                                                               \ 
        TRACE_LEAVE ("Got response");                                   \ 
        break;                                                          \ 
      }                                                                 \ 
      TRACE_LEAVE ("No response");                                      \ 
    }                                                                   \ 
    FILE = ret;                                                         \ 
    TIMER_END(last_modules);                                            \ 
  } while (0) 
 
mixed handle_request( RequestID id, void|int recurse_count) 
{ 
  mixed file; 
  REQUEST_WERR("handle_request()"); 
 
  if (recurse_count > 50) { 
    TRACE_ENTER ("Looped " + recurse_count + 
                 " times in internal redirects - giving up", 0); 
    TRACE_LEAVE (""); 
    return 0; 
  } 
 
  TIMER_START(handle_request); 
  TRY_FIRST_MODULES (file, handle_request (id, recurse_count + 1)); 
  if(!mappingp(file) && !mappingp(file = get_file(id))) 
    TRY_LAST_MODULES (file, handle_request(id, recurse_count + 1)); 
  TIMER_END(handle_request); 
 
  REQUEST_WERR("handle_request(): Done"); 
  MERGE_TIMERS(roxen); 
  return file; 
} 
 
mapping|int get_file(RequestID id, int|void no_magic, int|void internal_get) 
//! Return a result mapping for the id object at hand, mapping all 
//! modules, including the filter modules. This function is mostly a 
//! wrapper for @[low_get_file()]. 
{ 
  TIMER_START(get_file); 
  int orig_internal_get = id->misc->internal_get; 
  id->misc->internal_get = internal_get; 
  RequestID root_id = id->root_id || id; 
  root_id->misc->_request_depth++; 
  if(sub_req_limit && root_id->misc->_request_depth > sub_req_limit) 
    error("Subrequest limit reached. (Possibly an insertion loop.)"); 
 
  mapping|int res; 
  mapping res2; 
  function tmp; 
  res = low_get_file(id, no_magic); 
  TIMER_END(get_file); 
 
  // finally map all filter type modules. 
  // Filter modules are like TYPE_LAST modules, but they get called 
  // for _all_ files. 
  TIMER_START(filter_modules); 
  foreach(filter_module_cache||filter_modules(), tmp) 
  { 
    TRACE_ENTER("Filter module", tmp); 
    PROF_ENTER(Roxen.get_owning_module(tmp)->module_name,"filter"); 
    if(res2=tmp(res,id)) 
    { 
      if(mappingp(res) && res->file && (res2->file != res->file)) 
        destruct(res->file); 
      TRACE_LEAVE("Rewrote result."); 
      res=res2; 
    } else 
      TRACE_LEAVE(""); 
    PROF_LEAVE(Roxen.get_owning_module(tmp)->module_name,"filter"); 
  } 
  TIMER_END(filter_modules); 
 
  root_id->misc->_request_depth--; 
  id->misc->internal_get = orig_internal_get; 
  return res; 
} 
 
array(string) find_dir(string file, RequestID id, void|int(0..1) verbose) 
{ 
  array dir; 
  TRACE_ENTER(sprintf("List directory %O.", file), 0); 
 
  if(!sizeof (file) || file[0] != '/') 
    file = "/" + file; 
 
#ifdef URL_MODULES 
#ifdef THREADS 
  Thread.MutexKey key; 
#endif 
  // Map URL-modules 
  foreach(url_modules(), function funp) 
  { 
    string of = id->not_query; 
    id->not_query = file; 
    LOCK(funp); 
    TRACE_ENTER("URL module", funp); 
    mixed remap=funp( id, file ); 
    UNLOCK(); 
 
    if(mappingp( remap )) 
    { 
      id->not_query=of; 
      TRACE_LEAVE("Returned 'No thanks'."); 
      TRACE_LEAVE(""); 
      return 0; 
    } 
    if(objectp( remap )) 
    { 
      mixed err; 
      id->misc->find_dir_nest++; 
 
      TRACE_LEAVE("Recursing"); 
      file = id->not_query; 
      err = catch { 
        if( id->misc->find_dir_nest < 20 ) 
          dir = (id->conf || this_object())->find_dir( file, id ); 
        else 
          error("Too deep recursion in roxen::find_dir() while mapping " 
                +file+".\n"); 
      }; 
      id->misc->find_dir_nest = 0; 
      TRACE_LEAVE(""); 
      if(err) 
        throw(err); 
      return dir; 
    } 
    id->not_query=of; 
  } 
#endif /* URL_MODULES */ 
 
  array | mapping d; 
  array(string) locks=({}); 
  RoxenModule mod; 
  string loc; 
  foreach(location_modules(), array tmp) 
  { 
    loc = tmp[0]; 
    if(!search(file, loc)) { 
      /* file == loc + subpath */ 
      TRACE_ENTER(sprintf("Location module [%s] ", loc), tmp[1]); 
#ifdef MODULE_LEVEL_SECURITY 
      if(check_security(tmp[1], id)) { 
        TRACE_LEAVE("Permission denied"); 
        continue; 
      } 
#endif 
      mod=function_object(tmp[1]); 
      if(d=mod->find_dir(file[strlen(loc)..], id)) 
      { 
        if(mappingp(d)) 
        { 
          if(d->files) { 
            TRACE_LEAVE("Got exclusive directory."); 
            TRACE_LEAVE(sprintf("Returning list of %d files.", sizeof(d->files))); 
            return d->files; 
          } else 
            TRACE_LEAVE(""); 
        } else { 
          TRACE_LEAVE("Got files."); 
          if(!dir) dir=({ }); 
          dir |= d; 
        } 
      } 
      else { 
        if(verbose && mod->list_lock_files) 
          locks |= mod->list_lock_files(); 
        TRACE_LEAVE(""); 
      } 
    } else if((search(loc, file)==0) && (loc[strlen(file)-1]=='/') && 
              (loc[0]==loc[-1]) && (loc[-1]=='/') && 
              (function_object(tmp[1])->stat_file(".", id))) { 
      /* loc == file + "/" + subpath + "/" 
       * and stat_file(".") returns non-zero. 
       */ 
      TRACE_ENTER(sprintf("Location module [%s] ", loc), tmp[1]); 
      loc=loc[strlen(file)..]; 
      sscanf(loc, "%s/", loc); 
      if (dir) { 
        dir |= ({ loc }); 
      } else { 
        dir = ({ loc }); 
      } 
      TRACE_LEAVE("Added module mountpoint."); 
    } 
  } 
  if(!dir) return verbose ? ({0})+locks : ([])[0]; 
  if(sizeof(dir)) 
  { 
    TRACE_LEAVE(sprintf("Returning list of %d files.", sizeof(dir))); 
    return dir; 
  } 
  TRACE_LEAVE("Returning 'No such directory'."); 
  return 0; 
} 
 
// Stat a virtual file. 
 
array(int)|Stat stat_file(string file, RequestID id) 
{ 
  mixed s, tmp; 
#ifdef THREADS 
  Thread.MutexKey key; 
#endif 
  TRACE_ENTER(sprintf("Stat file %O.", file), 0); 
 
  file=replace(file, "//", "/"); // "//" is really "/" here... 
 
#ifdef URL_MODULES 
  // Map URL-modules 
  string of = id->not_query; 
  id->not_query = file; 
  foreach(url_module_cache||url_modules(), function funp) 
  { 
    TRACE_ENTER("URL module", funp); 
    LOCK(funp); 
    tmp=funp( id, file ); 
    UNLOCK(); 
 
    if (tmp) { 
      if(mappingp( tmp )) { 
        id->not_query = of; 
        TRACE_LEAVE(""); 
        TRACE_LEAVE("Returned 'No thanks'."); 
        return 0; 
      } 
      if(objectp( tmp )) 
      { 
        mixed err; 
        id->misc->stat_file_nest++; 
        TRACE_LEAVE("Recursing"); 
        err = catch { 
            if( id->misc->stat_file_nest < 20 ) 
              tmp = (id->conf || this_object())->stat_file(id->not_query, id ); 
            else 
              error("Too deep recursion in roxen::stat_file() while mapping " 
                    +file+".\n"); 
          }; 
        id->not_query = of; 
        id->misc->stat_file_nest = 0; 
        if(err) 
          throw(err); 
        TRACE_LEAVE(""); 
        TRACE_LEAVE("Returning data"); 
        return tmp; 
      } 
    } 
    TRACE_LEAVE(""); 
  } 
  id->not_query = of; 
#endif 
 
  // Map location-modules. 
  foreach(location_module_cache||location_modules(), 
          [string loc, function fun]) { 
    if((file == loc) || ((file+"/")==loc)) 
    { 
      TRACE_ENTER(sprintf("Location module [%s] ", loc), fun); 
      TRACE_LEAVE("Exact match."); 
      TRACE_LEAVE(""); 
      return Stdio.Stat(({ 0775, -3, 0, 0, 0, 0, 0 })); 
    } 
    if(has_prefix(file, loc)) 
    { 
      TRACE_ENTER(sprintf("Location module [%s] ", loc), fun); 
#ifdef MODULE_LEVEL_SECURITY 
      if(check_security(fun, id)) { 
        TRACE_LEAVE(""); 
        TRACE_LEAVE("Permission denied"); 
        continue; 
      } 
#endif 
      if(s=function_object(fun)->stat_file(file[strlen(loc)..], id)) 
      { 
        TRACE_LEAVE(""); 
        TRACE_LEAVE("Stat ok."); 
        return s; 
      } 
      TRACE_LEAVE(""); 
    } 
  } 
  TRACE_LEAVE("Returned 'no such file'."); 
} 
 
mapping error_file( RequestID id ) 
{ 
  string data = query("ZNoSuchFile"); 
  NOCACHE(); 
#if ROXEN_COMPAT <= 2.1 
  data = replace(data,({"$File", "$Me"}), 
                 ({"&page.virtfile;", "&roxen.server;"})); 
#endif 
  mapping res = Roxen.http_rxml_answer( data, id, 0, "text/html" ); 
  res->error = 404; 
  return res; 
} 
 
mapping auth_failed_file( RequestID id, string message ) 
{ 
  // Avoid recurssion in 401 messages. This could occure if the 
  // 401 messages used files that also where access denied. 
  if(id->misc->generate_auth_failed) 
    return Roxen.http_low_answer(401, "<title>Access Denied</title>" 
                                 "<h2 align=center>Access Denied</h2>"); 
  id->misc->generate_auth_failed = 1; 
   
  string data = query("ZAuthFailed"); 
  NOCACHE(); 
  mapping res = Roxen.http_rxml_answer( data, id, 0, "text/html" ); 
  res->error = 401; 
  return res; 
} 
 
// this is not as trivial as it sounds. Consider gtext. :-) 
array open_file(string fname, string mode, RequestID id, void|int internal_get, 
                void|int recurse_count) 
{ 
  mapping|int(0..1) file; 
  string oq = id->not_query; 
 
  if( id->conf && (id->conf != this_object()) ) 
    return id->conf->open_file( fname, mode, id, internal_get, recurse_count ); 
 
  if (recurse_count > 50) { 
    TRACE_ENTER ("Looped " + recurse_count + 
                 " times in internal redirects - giving up", 0); 
    TRACE_LEAVE (""); 
  } 
 
  else { 
    Configuration oc = id->conf; 
    id->not_query = fname; 
    TRY_FIRST_MODULES (file, open_file (fname, mode, id, 
                                        internal_get, recurse_count + 1)); 
    fname = id->not_query; 
 
    if(search(mode, "R")!=-1) //  raw (as in not parsed..) 
    { 
      string f; 
      mode -= "R"; 
      if(f = real_file(fname, id)) 
      { 
        // report_debug("opening "+fname+" in raw mode.\n"); 
        return ({ open(f, mode), ([]) }); 
      } 
      // return ({ 0, (["error":302]) }); 
    } 
 
    if(mode!="r") { 
      id->not_query = oq; 
      return ({ 0, (["error":501, "data":"Not implemented." ]) }); 
    } 
 
    if(!file) 
    { 
      file = get_file( id, 0, internal_get ); 
      if(!file) 
        TRY_LAST_MODULES (file, open_file (id->not_query, mode, id, 
                                           internal_get, recurse_count + 1)); 
    } 
  } 
 
  if(!mappingp(file)) 
  { 
    if(id->misc->error_code) 
      file = Roxen.http_low_answer(id->misc->error_code, "Failed" ); 
    else if(id->method!="GET"&&id->method != "HEAD"&&id->method!="POST") 
      file = Roxen.http_low_answer(501, "Not implemented."); 
    else 
      file = error_file( id ); 
 
    id->not_query = oq; 
 
    return ({ 0, file }); 
  } 
 
  if( file->data ) 
  { 
    file->file = StringFile(file->data); 
    m_delete(file, "data"); 
  } 
  id->not_query = oq; 
  return ({ file->file || StringFile(""), file }); 
} 
 
 
mapping(string:array(mixed)) find_dir_stat(string file, RequestID id) 
{ 
  string loc; 
  mapping(string:array(mixed)) dir = ([]); 
  mixed d, tmp; 
 
 
  file=replace(file, "//", "/"); 
 
  if(!sizeof (file) || file[0] != '/') 
    file = "/" + file; 
 
  // FIXME: Should I append a "/" to file if missing? 
 
  TRACE_ENTER(sprintf("Request for directory and stat's \"%s\".", file), 0); 
 
#ifdef URL_MODULES 
#ifdef THREADS 
  Thread.MutexKey key; 
#endif 
  // Map URL-modules 
  foreach(url_modules(), function funp) 
  { 
    string of = id->not_query; 
    id->not_query = file; 
    LOCK(funp); 
    TRACE_ENTER("URL module", funp); 
    tmp=funp( id, file ); 
    UNLOCK(); 
 
    if(mappingp( tmp )) 
    { 
      id->not_query=of; 
#ifdef MODULE_DEBUG 
      report_debug("conf->find_dir_stat(\"%s\"): url_module returned mapping:%O\n", 
                  file, tmp); 
#endif /* MODULE_DEBUG */ 
      TRACE_LEAVE("Returned mapping."); 
      TRACE_LEAVE(""); 
      return 0; 
    } 
    if(objectp( tmp )) 
    { 
      mixed err; 
      id->misc->find_dir_stat_nest++; 
 
      file = id->not_query; 
      err = catch { 
        if( id->misc->find_dir_stat_nest < 20 ) 
          tmp = (id->conf || this_object())->find_dir_stat( file, id ); 
        else { 
          TRACE_LEAVE("Too deep recursion"); 
          error("Too deep recursion in roxen::find_dir_stat() while mapping " 
                +file+".\n"); 
        } 
      }; 
      id->misc->find_dir_stat_nest = 0; 
      if(err) 
        throw(err); 
#ifdef MODULE_DEBUG 
      report_debug("conf->find_dir_stat(\"%s\"): url_module returned object:\n", 
                  file); 
#endif /* MODULE_DEBUG */ 
      TRACE_LEAVE("Returned object."); 
      TRACE_LEAVE("Returning it."); 
      return tmp;   // FIXME: Return 0 instead? 
    } 
    id->not_query=of; 
    TRACE_LEAVE(""); 
  } 
#endif /* URL_MODULES */ 
 
  foreach(location_modules(), tmp) 
  { 
    loc = tmp[0]; 
 
    TRACE_ENTER(sprintf("Location module [%s] ", loc), 0); 
    /* Note that only new entries are added. */ 
    if(!search(file, loc)) 
    { 
      /* file == loc + subpath */ 
#ifdef MODULE_LEVEL_SECURITY 
      if(check_security(tmp[1], id)) { 
        TRACE_LEAVE("Security check failed."); 
        continue; 
      } 
#endif 
      RoxenModule c = function_object(tmp[1]); 
      string f = file[strlen(loc)..]; 
      if (c->find_dir_stat) { 
        SIMPLE_TRACE_ENTER(c, "Calling find_dir_stat()."); 
        if (d = c->find_dir_stat(f, id)) { 
          SIMPLE_TRACE_LEAVE("Returned mapping with %d entries.", sizeof (d)); 
          dir = d | dir; 
        } 
        else 
          SIMPLE_TRACE_LEAVE("Returned zero."); 
      } else { 
        SIMPLE_TRACE_ENTER(c, "Calling find_dir()."); 
        if(d = c->find_dir(f, id)) { 
          SIMPLE_TRACE_LEAVE("Returned array with %d entries.", sizeof (d)); 
          dir = mkmapping(d, Array.map(d, lambda(string fn) 
                                          { 
                                            return c->stat_file(f + fn, id); 
                                          })) | dir; 
        } 
        else 
          SIMPLE_TRACE_LEAVE("Returned zero."); 
      } 
    } else if(search(loc, file)==0 && loc[strlen(file)-1]=='/' && 
              (loc[0]==loc[-1]) && loc[-1]=='/' && 
              (function_object(tmp[1])->stat_file(".", id))) { 
      /* loc == file + "/" + subpath + "/" 
       * and stat_file(".") returns non-zero. 
       */ 
      TRACE_ENTER(sprintf("The file %O is on the path to the mountpoint %O.", 
                          file, loc), 0); 
      loc=loc[strlen(file)..]; 
      sscanf(loc, "%s/", loc); 
      if (!dir[loc]) { 
        dir[loc] = ({ 0775, -3, 0, 0, 0, 0, 0 }); 
      } 
      TRACE_LEAVE(""); 
    } 
    TRACE_LEAVE(""); 
  } 
  if(sizeof(dir)) 
    return dir; 
} 
 
 
// Access a virtual file? 
 
array access(string file, RequestID id) 
{ 
  string loc; 
  array s, tmp; 
 
  file=replace(file, "//", "/"); // "//" is really "/" here... 
 
  // Map location-modules. 
  foreach(location_modules(), tmp) 
  { 
    loc = tmp[0]; 
    if((file+"/")==loc) { 
#ifdef MODULE_LEVEL_SECURITY 
      if(check_security(tmp[1], id)) continue; 
#endif 
      if(s=function_object(tmp[1])->access("", id)) 
        return s; 
    } else if(!search(file, loc)) { 
#ifdef MODULE_LEVEL_SECURITY 
      if(check_security(tmp[1], id)) continue; 
#endif 
      if(s=function_object(tmp[1])->access(file[strlen(loc)..], id)) 
        return s; 
    } 
  } 
  return 0; 
} 
 
string real_file(string file, RequestID id) 
//! Return the _real_ filename of a virtual file, if any. 
{ 
  string loc; 
  string s; 
  array tmp; 
  file=replace(file, "//", "/"); // "//" is really "/" here... 
 
  if(!id) error("No id passed to real_file"); 
 
  // Map location-modules. 
  foreach(location_modules(), tmp) 
  { 
    loc = tmp[0]; 
    if(!search(file, loc)) 
    { 
#ifdef MODULE_LEVEL_SECURITY 
      if(check_security(tmp[1], id)) continue; 
#endif 
      if(s=function_object(tmp[1])->real_file(file[strlen(loc)..], id)) 
        return s; 
    } 
  } 
} 
 
array(int)|Stat try_stat_file(string s, RequestID id, int|void not_internal) 
{ 
  RequestID fake_id; 
  array(int)|Stat res; 
 
  if(!objectp(id)) 
    error("No ID passed to 'try_stat_file'\n"); 
 
  // id->misc->common is here for compatibility; it's better to use 
  // id->root_id->misc. 
  if ( !id->misc ) 
    id->misc = ([]); 
 
  fake_id = make_fake_id(s, id); 
 
  fake_id->misc->internal_get = !not_internal; 
  fake_id->method = "GET"; 
 
  res = stat_file(fake_id->not_query, fake_id); 
 
  destruct (fake_id); 
  return res; 
} 
 
static RequestID make_fake_id (string s, RequestID id) 
{ 
  RequestID fake_id; 
 
  // id->misc->common is here for compatibility; it's better to use 
  // id->root_id->misc. 
  if ( !id->misc->common ) 
    id->misc->common = ([]); 
 
  fake_id = id->clone_me(); 
 
  fake_id->misc->common = id->misc->common; 
  fake_id->conf = this_object(); 
 
  fake_id->raw_url=s; 
 
  if (fake_id->scan_for_query) 
    // FIXME: If we're using e.g. ftp this doesn't exist. But the 
    // right solution might be that clone_me() in an ftp id object 
    // returns a vanilla (i.e. http) id instead when this function is 
    // used. 
    s = fake_id->scan_for_query (s); 
 
  s = http_decode_string(s); 
 
  s = Roxen.fix_relative (s, id); 
 
  // s is sent to Unix API's that take NUL-terminated strings... 
  if (search(s, "\0") != -1) 
    sscanf(s, "%s\0", s); 
 
  fake_id->not_query=s; 
 
  return fake_id; 
} 
 
int|string try_get_file(string s, RequestID id, 
                        int|void stat_only, int|void nocache, 
                        int|void not_internal, 
                        mapping|void result_mapping) 
//! Convenience function used in quite a lot of modules. Tries to read 
//! a file into memory, and then returns the resulting string. 
//! 
//! NOTE: A 'file' can be a cgi script, which will be executed, 
//! resulting in a horrible delay. 
//! 
//! Unless the not_internal flag is set, this tries to get an external 
//! or internal file. Here "internal" means a file that never should be 
//! sent directly as a request response. E.g. an internal redirect to a 
//! different file is still considered "external" since its contents is 
//! sent directly to the client. Internal requests are recognized by 
//! the id->misc->internal_get flag being non-zero. 
{ 
  string res; 
  RequestID fake_id = make_fake_id (s, id); 
  mapping m; 
 
  fake_id->misc->internal_get = !not_internal; 
  fake_id->method = "GET"; 
 
  array a = open_file( fake_id->not_query, "r", fake_id, !not_internal ); 
 
  m = a[1]; 
 
  if (result_mapping) { 
    foreach(indices(m), string i) 
      result_mapping[i] = m[i]; 
    if (string|function(string:string) charset = fake_id->get_output_charset()) 
      result_mapping->charset = charset; 
    result_mapping->last_modified = fake_id->misc->last_modified; 
  } 
 
  if(a[0]) { 
    m->file = a[0]; 
  } 
  else { 
    destruct (fake_id); 
    return 0; 
  } 
 
  CACHE( fake_id->misc->cacheable ); 
  destruct (fake_id); 
 
  // Allow 2* and 3* error codes, not only a few specific ones. 
  if (!(< 0,2,3 >)[m->error/100]) return 0; 
 
  if(stat_only) return 1; 
 
  if(m->data) 
    res = m->data; 
  else 
    res=""; 
  m->data = 0; 
 
  if( objectp(m->file) ) 
  { 
    res += m->file->read(); 
    if (m->file) { 
      // Some wrappers may destruct themselves in read()... 
      destruct(m->file); 
    } 
    m->file = 0; 
  } 
 
  if(m->raw) 
  { 
    res -= "\r"; 
    if(!sscanf(res, "%*s\n\n%s", res)) 
      sscanf(res, "%*s\n%s", res); 
  } 
  return res; 
} 
 
mapping(string:string) try_get_headers(string s, RequestID id, 
                                       int|void not_internal) 
//! Like @[try_get_file] but performs a HEAD request and only returns 
//! the response headers. Note that the returned headers are as they 
//! would be in a formatted response by the http protocol module, 
//! which is completely different from a response mapping. 
{ 
  RequestID fake_id = make_fake_id (s, id); 
  mapping m; 
 
  fake_id->misc->internal_get = !not_internal; 
  fake_id->method = "HEAD"; 
 
  array a = open_file( s, "r", fake_id, !not_internal ); 
  if(a && a[1]) { 
    if (a[0]) a[0]->close(); 
    m = a[1]; 
  } 
  else { 
    destruct (fake_id); 
    return 0; 
  } 
 
  CACHE( fake_id->misc->cacheable ); 
 
  if (!m->raw) 
    m = fake_id->make_response_headers (m); 
 
  else { 
    Roxen.HeaderParser hp = Roxen.HeaderParser(); 
    array res; 
 
    if(m->data) 
      res = hp->feed (m->data); 
 
    if (!res && objectp(m->file)) 
    { 
      hp->feed (m->file->read()); 
      if (m->file) { 
        // Some wrappers may destruct themselves in read()... 
        destruct(m->file); 
      } 
    } 
 
    m = res && res[2]; 
  } 
 
  destruct (fake_id); 
  return m; 
} 
 
mapping(string:mixed) try_put_file(string path, string data, RequestID id) 
{ 
  TIMER_START(try_put_file); 
 
  // id->misc->common is here for compatibility; it's better to use 
  // id->root_id->misc. 
  if ( !id->misc ) 
    id->misc = ([]); 
 
  RequestID fake_id = make_fake_id(path, id); 
   
  fake_id->root_id->misc->_request_depth++; 
  if(sub_req_limit && fake_id->root_id->misc->_request_depth > sub_req_limit) 
    error("Subrequest limit reached. (Possibly an insertion loop.)"); 
 
  fake_id->method = "PUT"; 
  fake_id->data = data; 
  fake_id->misc->len = sizeof(data); 
  fake_id->misc->internal_get = 1; 
 
  mapping(string:mixed) res = low_get_file(fake_id, 1); 
  TIMER_END(try_put_file); 
  return res; 
} 
 
int(0..1) is_file(string virt_path, RequestID id, int(0..1)|void internal) 
//! Is @[virt_path] a file in our virtual filesystem? If @[internal] is 
//! set, internal files is "visible" as well. 
{ 
  if(internal) { 
    int(0..1) was_internal = id->misc->internal_get; 
    id->misc->internal_get = 1; 
    int(0..1) res = !!stat_file(virt_path, id); 
    if(!was_internal) 
      m_delete(id->misc, "internal_get"); 
    return res; 
  } 
  if(stat_file(virt_path, id) || 
     has_suffix(virt_path, "/internal-roxen-unit")) 
    return 1; 
  string f = (virt_path/"/")[-1]; 
  if( sscanf(f, "internal-roxen-%s", f) ) { 
    if(internal_roxen_image(f, id) || 
       has_prefix(f, "pixel-")) 
      return 1; 
    return 0; 
  } 
  if( sscanf(f, "internal-gopher-%s", f) && 
      internal_gopher_image(f) ) 
    return 1; 
  return 0; 
} 
 
array registered_urls = ({}), failed_urls = ({ }); 
array do_not_log_patterns = 0; 
void start(int num) 
{ 
  fix_my_url(); 
 
#if 0 
  report_debug(sprintf("configuration:start():\n" 
                       "  registered_urls: ({ %{%O, %}})\n" 
                       "  failed_urls:     ({ %{%O, %}})\n" 
                       "  URLs:            ({ %{%O, %}})\n", 
                       registered_urls, 
                       failed_urls, 
                       query("URLs"))); 
#endif /* 0 */ 
 
  // Note: This is run as root if roxen is started as root 
  foreach( (registered_urls-query("URLs"))+failed_urls, string url ) 
  { 
    registered_urls -= ({ url }); 
    roxen.unregister_url(url, this_object()); 
  } 
 
  failed_urls = ({ }); 
 
  foreach( (query( "URLs" )-registered_urls), string url ) 
  { 
    if( roxen.register_url( url, this_object() ) ) 
      registered_urls += ({ url }); 
    else 
      failed_urls += ({ url }); 
  } 
  if( !datacache ) 
    datacache = DataCache( ); 
  else 
    datacache->init_from_variables(); 
 
  parse_log_formats(); 
  init_log_file(); 
  do_not_log_patterns = query("NoLog"); 
  if(!sizeof(do_not_log_patterns)) 
    do_not_log_patterns = 0; 
 
  if( query("throttle") ) 
  { 
    if( !throttler ) 
      throttler=.throttler(); 
    throttler->throttle(query("throttle_fill_rate"), 
                        query("throttle_bucket_depth"), 
                        query("throttle_min_grant"), 
                        query("throttle_max_grant")); 
  } 
  else if( throttler ) 
  { 
    // This is done to give old connections more bandwidth. 
    throttler->throttle( 1000000000, 1000000000, // 800Mbit. 
                         1024, 65536 ); 
    // and new connections does not even have to care. 
    throttler = 0; 
  } 
 
#ifdef SNMP_AGENT 
  if(query("snmp_process") && objectp(roxen->snmpagent)) 
      roxen->snmpagent->add_virtserv(get_config_id()); 
#endif 
 
  foreach(registered_urls, string url) { 
    mapping(string:string|Configuration|Protocol) port_info = roxen.urls[url]; 
 
    foreach((port_info && port_info->ports) || ({}), Protocol prot) { 
      if ((prot->prot_name != "snmp") || (!prot->mib)) { 
        continue; 
      } 
 
      string path = port_info->path || ""; 
      if (has_prefix(path, "/")) { 
        path = path[1..]; 
      } 
      if (has_suffix(path, "/")) { 
        path = path[..sizeof(path)-2]; 
      } 
     
      array(int) oid_suffix = ({ sizeof(path), @((array(int))path) }); 
 
      ADT.Trie mib = 
        SNMP.SimpleMIB(query_oid(), oid_suffix, 
                       ({ 
                         UNDEFINED, 
                         UNDEFINED, 
                         SNMP.String(query_name, "siteName"), 
                         SNMP.String(comment, "siteComment"), 
                         SNMP.Counter64(lambda() { return sent; }, 
                                        "sent"), 
                         SNMP.Counter64(lambda() { return received; }, 
                                        "received"), 
                         SNMP.Counter64(lambda() { return hsent; }, 
                                        "sentHeaders"), 
                         SNMP.Counter64(lambda() { return requests; }, 
                                        "numRequests"), 
                         UNDEFINED,     // NOTE: Reserved for modules! 
                       })); 
      SNMP.set_owner(mib, this_object()); 
      prot->mib->merge(mib); 
    } 
  } 
} 
 
void save_me() 
{ 
  save_one( 0 ); 
} 
 
void save(int|void all) 
//! Save this configuration. If all is included, save all configuration 
//! global variables as well, otherwise only all module variables. 
{ 
  if(all) 
  { 
    store("spider#0", variables, 0, this_object()); 
    start(2); 
  } 
 
  store( "EnabledModules", enabled_modules, 1, this_object()); 
  foreach(indices(modules), string modname) 
  { 
    foreach(indices(modules[modname]->copies), int i) 
    { 
      store(modname+"#"+i, modules[modname]->copies[i]->query(), 0, this_object()); 
      if (mixed err = catch(modules[modname]->copies[i]-> 
                            start(2, this_object(), 0))) 
        report_error("Error calling start in module.\n%s", 
                     describe_backtrace (err)); 
    } 
  } 
  invalidate_cache(); 
} 
 
int save_one( RoxenModule o ) 
//! Save all variables in a given module. 
{ 
  mapping mod; 
  if(!o) 
  { 
    store("spider#0", variables, 0, this_object()); 
    start(2); 
    return 1; 
  } 
  string q = otomod[ o ]; 
  if( !q ) 
    error("Invalid module"); 
 
  store(q, o->query(), 0, this_object()); 
  invalidate_cache(); 
  mixed error; 
  if( error = catch( o->start(2, this_object(), 0) ) ) 
  { 
    if( objectp(error ) ) 
      error = (array)error; 
    if( sizeof(error)>1 && arrayp( error[1] ) ) 
    { 
      int i; 
      for( i = 0; i<sizeof( error[1] ); i++ ) 
        if( error[1][i][2] == save_one ) 
          break; 
      error[1] = error[1][i+1..]; 
    } 
    if( o->report_error ) 
      o->report_error( "Call to start failed.\n"+describe_backtrace( error ) ); 
    else 
      report_error( "Call to start failed.\n"+describe_backtrace( error )); 
  } 
  invalidate_cache(); 
  return 1; 
} 
 
RoxenModule reload_module( string modname ) 
{ 
  RoxenModule old_module = find_module( modname ); 
  ModuleInfo mi = roxen.find_module( (modname/"#")[0] ); 
 
  roxen->bootstrap_info->set (({this_object(), modname })); 
 
  if( !old_module ) return 0; 
 
  // Temporarily shift out of the rxml parsing context if we're inside 
  // any (e.g. due to delayed loading from inside the admin 
  // interface). 
  RXML.Context old_ctx = RXML.get_context(); 
  RXML.set_context (0); 
  mixed err = catch { 
 
      master()->clear_compilation_failures(); 
 
      if( !old_module->not_a_module ) 
      { 
        save_one( old_module ); 
        master()->refresh_inherit( object_program( old_module ) ); 
        master()->refresh( object_program( old_module ), 1 ); 
      } 
 
      array old_error_log = (array) old_module->error_log; 
 
      RoxenModule nm; 
 
      // Load up a new instance. 
      nm = mi->instance( this_object() ); 
      // If this is a faked module, let's call it a failure. 
      if( nm->not_a_module ) 
      { 
        old_module->report_error(LOC_C(385,"Reload failed")+"\n"); 
        RXML.set_context (old_ctx); 
        return old_module; 
      } 
 
      disable_module( modname, 1 ); 
      destruct( old_module );  
 
      mixed err = catch { 
          mi->update_with( nm,0 ); // This is sort of nessesary... 
        }; 
      if (err) 
        if (stringp (err)) { 
          // Error from the register_module call. We can't enable the old 
          // module now, and I don't dare changing the order so that 
          // register_module starts to get called before the old module is 
          // destructed. /mast 
          report_error (err); 
          report_error(LOC_C(385,"Reload failed")+"\n"); 
          RXML.set_context (old_ctx); 
          return 0;                 // Use a placeholder module instead? 
        } 
        else 
          throw (err); 
      enable_module( modname, nm, mi ); 
 
      foreach (old_error_log, [string msg, array(int) times]) 
        nm->error_log[msg] += times; 
 
      nm->report_notice(LOC_C(11, "Reloaded %s.")+"\n", mi->get_name()); 
      RXML.set_context (old_ctx); 
      return nm; 
 
    }; 
  RXML.set_context (old_ctx); 
  throw (err); 
} 
 
#ifdef THREADS 
Thread.Mutex enable_modules_mutex = Thread.Mutex(); 
#define MODULE_LOCK(TYPE) \ 
  Thread.MutexKey enable_modules_lock = enable_modules_mutex->lock (TYPE) 
#else 
#define MODULE_LOCK(TYPE) 
#endif 
 
static int enable_module_batch_msgs; 
 
RoxenModule enable_module( string modname, RoxenModule|void me,  
                           ModuleInfo|void moduleinfo,  
                           int|void nostart, int|void nosave ) 
{ 
  MODULE_LOCK (2); 
  int id; 
  ModuleCopies module; 
  int pr; 
  mixed err; 
  int module_type; 
 
  if( forcibly_added[modname] == 2 ) 
    return search(otomod, modname); 
   
  if( datacache ) datacache->flush(); 
 
  if( sscanf(modname, "%s#%d", modname, id ) != 2 ) 
    while( modules[ modname ] && modules[ modname ][ id ] ) 
      id++; 
 
  roxen->bootstrap_info->set (({this_object(), modname + "#" + id})); 
 
#ifdef MODULE_DEBUG 
  int start_time = gethrtime(); 
#endif 
 
  if( !moduleinfo ) 
  { 
    moduleinfo = roxen->find_module( modname ); 
 
    if (!moduleinfo) 
    { 
      report_warning("Failed to load %s. The module probably " 
                     "doesn't exist in the module path.\n", modname); 
      got_no_delayed_load = -1; 
      roxen->bootstrap_info->set (0); 
      return 0; 
    } 
  } 
 
  string descr = moduleinfo->get_name() + (id ? " copy " + (id + 1) : ""); 
  //  sscanf(descr, "%*s: %s", descr); 
 
#ifdef MODULE_DEBUG 
  if (enable_module_batch_msgs) 
    report_debug(" %-43s... \b", descr ); 
  else 
    report_debug("Enabling " + descr + "\n"); 
#endif 
 
  module = modules[ modname ]; 
 
  if(!module) 
    modules[ modname ] = module = ModuleCopies(); 
 
  if( !me ) 
  { 
    if(err = catch(me = moduleinfo->instance(this_object()))) 
    { 
#ifdef MODULE_DEBUG 
      if (enable_module_batch_msgs) report_debug("\bERROR\n"); 
      if (err != "") { 
#endif 
      string bt=describe_backtrace(err); 
        report_error("enable_module(): " + 
                     LOC_M(41, "Error while initiating module copy of %s%s"), 
                     moduleinfo->get_name(), (bt ? ":\n"+bt : "\n")); 
#ifdef MODULE_DEBUG 
      } 
#endif 
      got_no_delayed_load = -1; 
      roxen->bootstrap_info->set (0); 
      return module[id]; 
    } 
  } 
 
  if(module[id] && module[id] != me) 
  { 
    if( module[id]->stop ) { 
      if (err = catch( module[id]->stop() )) { 
        string bt=describe_backtrace(err); 
        report_error("disable_module(): " + 
                     LOC_M(44, "Error while disabling module %s%s"), 
                     descr, (bt ? ":\n"+bt : "\n")); 
      } 
    } 
  } 
 
  me->set_configuration( this_object() ); 
 
  module_type = moduleinfo->type; 
  if (module_type & (MODULE_LOCATION|MODULE_EXTENSION| 
                     MODULE_CONFIG|MODULE_FILE_EXTENSION|MODULE_LOGGER| 
                     MODULE_URL|MODULE_LAST|MODULE_PROVIDER| 
                     MODULE_FILTER|MODULE_TAG|MODULE_FIRST| 
                     MODULE_USERDB)) 
  { 
    if(module_type != MODULE_CONFIG) 
    { 
      if (err = catch { 
        me->defvar("_priority", 5, DLOCALE(12, "Priority"), TYPE_INT_LIST, 
                   DLOCALE(13, "The priority of the module. 9 is highest and 0 is lowest." 
                   " Modules with the same priority can be assumed to be " 
                   "called in random order."), 
                   ({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})); 
      }) { 
        roxen->bootstrap_info->set (0); 
        throw(err); 
      } 
    } 
 
#ifdef MODULE_LEVEL_SECURITY 
    if( (module_type & ~(MODULE_LOGGER|MODULE_PROVIDER|MODULE_USERDB)) != 0 ) 
    { 
//       me->defvar("_sec_group", "user", DLOCALE(14, "Security: Realm"),  
//             TYPE_STRING, 
//             DLOCALE(15, "The realm to use when requesting password from the " 
//                     "client. Usually used as an informative message to the " 
//                     "user.")); 
       
      me->defvar("_seclevels", "", DLOCALE(16, "Security: Patterns"),  
                 TYPE_TEXT_FIELD, 
                 DLOCALE(245, 
                         "The syntax is:\n" 
                         " \n<dl>" 
                         "  <dt><b>userdb</b> <i>userdatabase module</i></dt>\n" 
                         "  <dd> Select a non-default userdatabase module. The default is to " 
                         " search all modules. The userdatabase module config_userdb is always " 
                         " present, and contains the configuration users</dd>\n" 
                         "<dt><b>authmethod</b> <i>authentication module</i></dt>\n" 
                         "<dd>Select a non-default authentication method.</dd>" 
                         "<dt><b>realm</b> <i>realm name</i></dt>\n" 
                         "<dd>The realm is used when user authentication info is requested</dd>" 
                         "</dl>\n" 
                         "  Below, CMD is one of 'allow' and 'deny'\n" 
                         " <dl>\n" 
                         "  <dt>CMD <b>ip</b>=<i>ip/bits</i>  [return]<br />\n" 
                         "  CMD <b>ip</b>=<i>ip:mask</i>  [return] <br />\n" 
                         "  CMD <b>ip</b>=<i>pattern[,pattern,...]</i>  [return] <br /></dt>\n" 
                         "  <dd>Match the remote IP-address.</dd>\n" 
                         " \n" 
                         "  <dt>CMD <b>user</b>=<i>name[,name,...]</i>  [return]</dt>\n" 
                         "  <dd>Requires a authenticated user. If the user name 'any' is used, any " 
                         "valid user will be OK. Otherwise, one of the listed users are required.</dd>" 
                         "  <dt>CMD <b>group</b>=<i>name[,name,...]</i> [return]</dt>\n" 
                         "<dd>Requires a authenticated user with a group. If the group name " 
                         " 'any' is used, any valid group will be OK. Otherwise, one of the " 
                         "listed groups are required.</dd>\n" 
                         " \n" 
                         "<dt>CMD <b>dns</b>=<i>pattern[,pattern,...]</i>           [return]</dt>\n" 
                         "<dd>Require one of the specified DNS domain-names</dd>" 
                         " \n" 
                         "<dt>CMD <b>time</b>=<i>HH:mm-HH:mm</i>   [return]</dt>\n" 
                         "<dd>Only allow access to the module from the first time to the " 
                         " second each day. Both times should be specified in 24-hour " 
                         " HH:mm format.</dd>\n" 
                         "<dt>CMD <b>day</b>=<i>day[,day,...]</i> [return]</dt>\n" 
                         "<dd>Only allow access during certain days. Day is either a numerical " 
                         "    value (Monday=1, Sunday=7) or a string (monday, tuesday etc)</dd>" 
                         "</dl><p>\n" 
                         "  pattern is always a glob pattern (* = any characters, ? = any character).\n" 
                         "</p><p>\n" 
                         "  return means that reaching this command results in immediate\n" 
                         "  return, only useful for 'allow'.</p>\n" 
                         " \n" 
                         " <p>'deny' always implies a return, no futher testing is done if a\n" 
                         " 'deny' match.</p>\n")); 
 
      if(!(module_type & MODULE_PROXY)) 
      { 
        me->defvar("_seclvl",  0, DLOCALE(18, "Security: Security level"),  
                   TYPE_INT, 
                   DLOCALE(305, "The modules security level is used to determine if a " 
                   " request should be handled by the module." 
                   "\n<p><h2>Security level vs Trust level</h2>" 
                   " Each module has a configurable <i>security level</i>." 
                   " Each request has an assigned trust level. Higher" 
                   " <i>trust levels</i> grants access to modules with higher" 
                   " <i>security levels</i>." 
                   "\n<p><h2>Definitions</h2><ul>" 
                   " <li>A requests initial Trust level is infinitely high.</li>" 
                   " <li> A request will only be handled by a module if its" 
                   "     <i>trust level</i> is higher or equal to the" 
                   "     <i>security level</i> of the module.</li>" 
                   " <li> Each time the request is handled by a module the" 
                   "     <i>trust level</i> of the module will be set to the" 
                   "      lower of its <i>trust level</i> and the modules" 
                   "     <i>security level</i>, <i>unless</i> the security " 
                   "        level of the module is 0, which is a special " 
                   "        case and means that no change should be made.</li>" 
                   " </ul></p>" 
                   "\n<p><h2>Example</h2>" 
                   " Modules:<ul>" 
                   " <li>  User filesystem, <i>security level</i> 1</li>" 
                   " <li>  Filesystem module, <i>security level</i> 3</li>" 
                   " <li>  CGI module, <i>security level</i> 2</li>" 
                   " </ul></p>" 
                   "\n<p>A request handled by \"User filesystem\" is assigned" 
                   " a <i>trust level</i> of one after the <i>security" 
                   " level</i> of that module. That request can then not be" 
                   " handled by the \"CGI module\" since that module has a" 
                   " higher <i>security level</i> than the requests trust" 
                   " level.</p>" 
                   "\n<p>On the other hand, a request handled by the the" 
                   " \"Filsystem module\" could later be handled by the" 
                   " \"CGI module\".</p>")); 
 
      } else { 
        me->definvisvar("_seclvl", -10, TYPE_INT); /* A very low one */ 
      } 
    } 
#endif 
  } else { 
    me->defvar("_priority", 0, "", TYPE_INT, "", 0, 1); 
  } 
 
  mapping(string:mixed) stored_vars = retrieve(modname + "#" + id, this_object()); 
  int has_stored_vars = sizeof (stored_vars); // A little ugly, but it suffices. 
  me->setvars(stored_vars); 
 
  module[ id ] = me; 
  otomod[ me ] = modname+"#"+id; 
 
  if(!nostart) call_start_callbacks( me, moduleinfo, module ); 
 
#ifdef MODULE_DEBUG 
  if (enable_module_batch_msgs) { 
    if(moduleinfo->config_locked[this_object()]) 
      report_debug("\bLocked %6.1fms\n", (gethrtime()-start_time)/1000.0); 
    else 
      report_debug("\bOK %6.1fms\n", (gethrtime()-start_time)/1000.0); 
  } 
#else 
  if(moduleinfo->config_locked[this_object()]) 
    report_error("   Error: \"%s\" not loaded (license restriction).\n", 
                 moduleinfo->get_name()); 
#endif 
  if( !enabled_modules[modname+"#"+id] ) 
  { 
    enabled_modules[modname+"#"+id] = 1; 
    if(!nosave) 
      store( "EnabledModules", enabled_modules, 1, this_object()); 
  } 
 
  if (!has_stored_vars && !nosave) 
    store (modname + "#" + id, me->query(), 0, this_object()); 
 
  if( me->no_delayed_load && got_no_delayed_load >= 0 ) 
    got_no_delayed_load = 1; 
 
  roxen->bootstrap_info->set (0); 
  return me; 
} 
 
void call_start_callbacks( RoxenModule me,  
                           ModuleInfo moduleinfo,  
                           ModuleCopies module, 
                           void|int newly_added) 
{ 
  call_low_start_callbacks(  me, moduleinfo, module ); 
  call_high_start_callbacks (me, moduleinfo, newly_added); 
} 
 
void call_low_start_callbacks( RoxenModule me,  
                               ModuleInfo moduleinfo,  
                               ModuleCopies module ) 
{ 
  if(!me) return; 
  if(!moduleinfo) return; 
  if(!module) return; 
 
  int module_type = moduleinfo->type, pr; 
  mixed err; 
  if (err = catch(pr = me->query("_priority"))) 
  { 
#ifdef MODULE_DEBUG 
    if (enable_module_batch_msgs) report_debug("\bERROR\n"); 
#endif 
    string bt=describe_backtrace(err); 
    report_error(LOC_M(41, "Error while initiating module copy of %s%s"), 
                        moduleinfo->get_name(), (bt ? ":\n"+bt : "\n")); 
    pr = 3; 
  } 
 
  api_module_cache |= me->api_functions(); 
 
  if(module_type & MODULE_EXTENSION) 
  { 
    report_error("%s is an MODULE_EXTENSION, that type is no " 
                 "longer available.\nPlease notify the modules writer.\n" 
                 "Suitable replacement types include MODULE_FIRST and " 
                 " MODULE_LAST.\n", moduleinfo->get_name()); 
  } 
 
  if(module_type & MODULE_FILE_EXTENSION) 
    if (err = catch { 
      array arr = me->query_file_extensions(); 
      if (arrayp(arr)) 
      { 
        string foo; 
        foreach( me->query_file_extensions(), foo ) 
          if(pri[pr]->file_extension_modules[foo = lower_case(foo)] ) 
            pri[pr]->file_extension_modules[foo] += ({me}); 
          else 
            pri[pr]->file_extension_modules[foo] = ({me}); 
      } 
    }) { 
#ifdef MODULE_DEBUG 
      if (enable_module_batch_msgs) report_debug("\bERROR\n"); 
#endif 
      string bt=describe_backtrace(err); 
      report_error(LOC_M(41, "Error while initiating module copy of %s%s"), 
                   moduleinfo->get_name(), (bt ? ":\n"+bt : "\n")); 
      got_no_delayed_load = -1; 
    } 
 
  if(module_type & MODULE_PROVIDER) 
    if (err = catch 
    { 
      mixed provs = me->query_provides ? me->query_provides() : ({}); 
      if(stringp(provs)) 
        provs = (< provs >); 
      if(arrayp(provs)) 
        provs = mkmultiset(provs); 
      if (multisetp(provs)) { 
        pri[pr]->provider_modules [ me ] = provs; 
      } 
    }) { 
#ifdef MODULE_DEBUG 
      if (enable_module_batch_msgs) report_debug("\bERROR\n"); 
#endif 
      string bt=describe_backtrace(err); 
      report_error(LOC_M(41, "Error while initiating module copy of %s%s"), 
                   moduleinfo->get_name(), (bt ? ":\n"+bt : "\n")); 
      got_no_delayed_load = -1; 
    } 
 
  if(module_type & MODULE_TYPES) 
  { 
    types_module = me; 
    types_fun = me->type_from_extension; 
  } 
 
  if(module_type & MODULE_TAG) 
    add_parse_module( me ); 
 
  if(module_type & MODULE_DIRECTORIES) 
    if (me->parse_directory) 
      dir_module = me; 
 
  if(module_type & MODULE_LOCATION) 
    pri[pr]->location_modules += ({ me }); 
 
  if(module_type & MODULE_LOGGER) 
    pri[pr]->logger_modules += ({ me }); 
 
  if(module_type & MODULE_URL) 
    pri[pr]->url_modules += ({ me }); 
 
  if(module_type & MODULE_LAST) 
    pri[pr]->last_modules += ({ me }); 
 
  if(module_type & MODULE_FILTER) 
    pri[pr]->filter_modules += ({ me }); 
 
  if(module_type & MODULE_FIRST) 
    pri[pr]->first_modules += ({ me }); 
 
  foreach(registered_urls, string url) { 
    mapping(string:string|Configuration|Protocol) port_info = roxen.urls[url]; 
 
    foreach((port_info && port_info->ports) || ({}), Protocol prot) { 
      if ((prot->prot_name != "snmp") || (!prot->mib)) { 
        continue; 
      } 
 
      string path = port_info->path || ""; 
      if (has_prefix(path, "/")) { 
        path = path[1..]; 
      } 
      if (has_suffix(path, "/")) { 
        path = path[..sizeof(path)-2]; 
      } 
     
      array(int) oid_suffix = ({ sizeof(path), @((array(int))path) }); 
 
      ADT.Trie sub_mib = generate_module_mib(query_oid() + ({ 8, 1 }), 
                                             oid_suffix, me, moduleinfo, module); 
      SNMP.set_owner(sub_mib, this_object(), me); 
 
      prot->mib->merge(sub_mib); 
 
      if (me->query_snmp_mib) { 
        array(int) segment = generate_module_oid_segment(me); 
        sub_mib = me->query_snmp_mib(query_oid() + ({ 8, 2 }) + 
                                     segment[..sizeof(segment)-2], 
                                     oid_suffix + ({ segment[-1] })); 
        SNMP.set_owner(sub_mib, this_object(), me); 
        prot->mib->merge(sub_mib); 
      } 
    } 
  } 
 
  invalidate_cache(); 
} 
 
void call_high_start_callbacks (RoxenModule me, ModuleInfo moduleinfo, 
                                void|int newly_added) 
{ 
  // This is icky, but I don't know if it's safe to remove. /mast 
  if(!me) return; 
  if(!moduleinfo) return; 
 
  mixed err; 
  if((me->start) && 
     (err = catch( me->start(0, this_object(), newly_added) ) ) ) 
  { 
#ifdef MODULE_DEBUG 
    if (enable_module_batch_msgs)  
      report_debug("\bERROR\n"); 
#endif 
    string bt=describe_backtrace(err); 
    report_error(LOC_M(41, "Error while initiating module copy of %s%s"), 
                        moduleinfo->get_name(), (bt ? ":\n"+bt : "\n")); 
    got_no_delayed_load = -1; 
  } 
  if( inited && me->ready_to_receive_requests ) 
    if( mixed q = catch( me->ready_to_receive_requests( this_object() ) ) )  
    { 
#ifdef MODULE_DEBUG 
      if (enable_module_batch_msgs) report_debug("\bERROR\n"); 
#endif 
      report_error( "While calling ready_to_receive_requests:\n"+ 
                    describe_backtrace( q ) ); 
      got_no_delayed_load = -1; 
    } 
} 
 
// Called from the administration interface. 
string check_variable(string name, mixed value) 
{ 
  switch(name) 
  { 
//    case "MyWorldLocation": 
//     if(strlen(value)<7 || value[-1] != '/' || 
//        !(sscanf(value,"%*s://%*s/")==2)) 
//       return LOCALE->url_format(); 
//     return 0; 
  case "MyWorldLocation": 
  case "URLs": 
    fix_my_url(); 
    return 0; 
//    case "throttle": 
//      // There was code here to sett the throttling. That's not a 
//      // good idea. Moved to start. The code now also avoids 
//      // creating new throttle objects each time a value is changed. 
//    case "throttle_fill_rate": 
//    case "throttle_bucket_depth": 
//    case "throttle_min_grant": 
//    case "throttle_max_grant": 
//      return 0; 
#ifdef SNMP_AGENT 
  case "snmp_process": 
    if (objectp(roxen->snmpagent)) { 
      int cid = get_config_id(); 
      value ? roxen->snmpagent->add_virtserv(cid) : roxen->snmpagent->del_virtserv(cid); 
    } 
    return 0; 
#endif 
  } 
} 
 
void module_changed( ModuleInfo moduleinfo, 
                     RoxenModule me  ) 
{ 
  clean_up_for_module( moduleinfo, me ); 
  call_low_start_callbacks( me, 
                            moduleinfo, 
                            modules[ moduleinfo->sname ] ); 
} 
 
void clean_up_for_module( ModuleInfo moduleinfo, 
                          RoxenModule me ) 
{ 
  int pr; 
  if(moduleinfo->type & MODULE_FILE_EXTENSION) 
  { 
    string foo; 
    for(pr=0; pr<10; pr++) 
      foreach( indices (pri[pr]->file_extension_modules), foo ) 
        pri[pr]->file_extension_modules[foo]-=({me}); 
  } 
 
  if(moduleinfo->type & MODULE_PROVIDER) { 
    for(pr=0; pr<10; pr++) 
      m_delete(pri[pr]->provider_modules, me); 
  } 
 
  if(moduleinfo->type & MODULE_TYPES) 
  { 
    types_module = 0; 
    types_fun = 0; 
  } 
 
  if(moduleinfo->type & MODULE_TAG) 
    remove_parse_module( me ); 
 
  if( moduleinfo->type & MODULE_DIRECTORIES ) 
    dir_module = 0; 
 
  if( moduleinfo->type & MODULE_LOCATION ) 
    for(pr=0; pr<10; pr++) 
     pri[pr]->location_modules -= ({ me }); 
 
  if( moduleinfo->type & MODULE_URL ) 
    for(pr=0; pr<10; pr++) 
      pri[pr]->url_modules -= ({ me }); 
 
  if( moduleinfo->type & MODULE_LAST ) 
    for(pr=0; pr<10; pr++) 
      pri[pr]->last_modules -= ({ me }); 
 
  if( moduleinfo->type & MODULE_FILTER ) 
    for(pr=0; pr<10; pr++) 
      pri[pr]->filter_modules -= ({ me }); 
 
  if( moduleinfo->type & MODULE_FIRST ) { 
    for(pr=0; pr<10; pr++) 
      pri[pr]->first_modules -= ({ me }); 
  } 
 
  if( moduleinfo->type & MODULE_LOGGER ) 
    for(pr=0; pr<10; pr++) 
      pri[pr]->logger_modules -= ({ me }); 
 
  foreach(registered_urls, string url) { 
    mapping(string:string|Configuration|Protocol) port_info = roxen.urls[url]; 
    foreach((port_info && port_info->ports) || ({}), Protocol prot) { 
      if ((prot->prot_name != "snmp") || (!prot->mib)) { 
        continue; 
      } 
 
      SNMP.remove_owned(prot->mib, this_object(), me); 
    } 
  } 
} 
 
int disable_module( string modname, int|void nodest ) 
{ 
  MODULE_LOCK (2); 
  RoxenModule me; 
  int id, pr; 
  sscanf(modname, "%s#%d", modname, id ); 
 
  if( datacache ) datacache->flush(); 
 
  ModuleInfo moduleinfo =  roxen.find_module( modname ); 
  mapping module = modules[ modname ]; 
  string descr = moduleinfo->get_name() + (id ? " copy " + (id + 1) : ""); 
 
  if(!module) 
  { 
    report_error("disable_module(): " + 
                 LOC_M(42, "Failed to disable module:\n" 
                        "No module by that name: \"%s\".\n"), modname); 
    return 0; 
  } 
 
  me = module[id]; 
  m_delete(module->copies, id); 
 
  if(!sizeof(module->copies)) 
    m_delete( modules, modname ); 
 
  invalidate_cache(); 
 
  if(!me) 
  { 
    report_error("disable_module(): " + 
                 LOC_M(43, "Failed to disable module \"%s\".\n"), 
                 descr); 
    return 0; 
  } 
 
  if(me->stop) 
    if (mixed err = catch (me->stop())) { 
      string bt=describe_backtrace(err); 
      report_error("disable_module(): " + 
                   LOC_M(44, "Error while disabling module %s%s"), 
                   descr, (bt ? ":\n"+bt : "\n")); 
    } 
 
#ifdef MODULE_DEBUG 
  report_debug("Disabling "+descr+"\n"); 
#endif 
 
  clean_up_for_module( moduleinfo, me ); 
 
  if( !nodest ) 
  { 
    m_delete( enabled_modules, modname + "#" + id ); 
    m_delete( forcibly_added, modname + "#" + id ); 
    store( "EnabledModules",enabled_modules, 1, this_object()); 
    destruct(me); 
  } 
  return 1; 
} 
 
RoxenModule find_module(string name) 
//! Return the module corresponding to the name (eg "rxmlparse", 
//! "rxmlparse#0" or "filesystem#1") or zero, if there was no such 
//! module. The string is on the same format as the one returned by 
//! @[RoxenModule.module_local_id]. 
{ 
  int id; 
  sscanf(name, "%s#%d", name, id); 
  if(modules[name]) 
    return modules[name]->copies[id]; 
  return 0; 
} 
 
mapping forcibly_added = ([]); 
int add_modules( array(string) mods, int|void now ) 
{ 
#ifdef MODULE_DEBUG 
  int wr; 
#endif 
  foreach (mods, string mod) 
  { 
    sscanf( mod, "%s#", mod ); 
    if( ((now && !modules[ mod ]) || 
         !enabled_modules[ mod+"#0" ] ) 
        && !forcibly_added[ mod+"#0" ]) 
    { 
#ifdef MODULE_DEBUG 
      if( !wr++ ) 
        if (enable_module_batch_msgs) 
          report_debug("\b[ adding req module" + (sizeof (mods) > 1 ? "s" : "") + "\n"); 
        else 
          report_debug("Adding required module" + (sizeof (mods) > 1 ? "s" : "") + "\n"); 
#endif 
      forcibly_added[ mod+"#0" ] = 1; 
      enable_module( mod+"#0" ); 
      forcibly_added[ mod+"#0" ] = 2; 
    } 
  } 
#ifdef MODULE_DEBUG 
  if( wr && enable_module_batch_msgs ) 
    report_debug("] \b"); 
#endif 
} 
 
#if ROXEN_COMPAT < 2.2 
// BEGIN SQL 
 
mapping(string:string) sql_urls = ([]); 
 
constant sql_cache_get = DBManager.sql_cache_get; 
 
Sql.Sql sql_connect(string db, void|string charset) 
{ 
  if (sql_urls[db]) 
    return sql_cache_get(sql_urls[db], 0, charset); 
  else 
    return sql_cache_get(db, 0, charset); 
} 
 
// END SQL 
#endif 
 
static string my_url; 
 
void fix_my_url() 
{ 
  my_url = query ("MyWorldLocation"); 
  if (!sizeof (my_url) && 
      !(my_url = Roxen.get_world (query ("URLs")))) 
    // Probably no port configured. The empty string is used as a 
    // flag; there shouldn't be any bad fallback here. 
    my_url = ""; 
  else 
    if (!has_suffix (my_url, "/")) my_url += "/"; 
} 
 
//! Returns some URL for accessing the configuration. (Should be 
//! used instead of querying MyWorldLocation directly.) 
string get_url() {return my_url;} 
 
array after_init_hooks = ({}); 
mixed add_init_hook( mixed what ) 
{ 
  if( inited ) 
    call_out( what, 0, this_object() ); 
  else 
    after_init_hooks |= ({ what }); 
} 
 
static int got_no_delayed_load = 0; 
// 0 -> enable delayed loading, 1 -> disable delayed loading, 
// -1 -> don't change. 
 
void fix_no_delayed_load_flag() 
{ 
  if( got_no_delayed_load >= 0 && 
      query ("no_delayed_load") != got_no_delayed_load ) { 
    set( "no_delayed_load", got_no_delayed_load ); 
    save_one( 0 ); 
  } 
} 
 
void enable_all_modules() 
{ 
  MODULE_LOCK (0); 
 
  // Temporarily shift out of the rxml parsing context if we're inside 
  // any (e.g. due to delayed loading from inside the admin 
  // interface). 
  RXML.Context old_ctx = RXML.get_context(); 
  RXML.set_context (0); 
  mixed err = catch { 
 
      low_init( ); 
      fix_no_delayed_load_flag(); 
 
    }; 
  RXML.set_context (old_ctx); 
  if (err) throw (err); 
} 
 
void low_init(void|int modules_already_enabled) 
{ 
  if( inited ) 
    return; // already done 
 
  int start_time = gethrtime(); 
  if (!modules_already_enabled) 
    report_debug("\nEnabling all modules for "+query_name()+"... \n"); 
 
  if (!modules_already_enabled) 
  { 
    enabled_modules = retrieve("EnabledModules", this_object()); 
//     roxenloader.LowErrorContainer ec = roxenloader.LowErrorContainer(); 
//     roxenloader.push_compile_error_handler( ec ); 
 
    array modules_to_process = indices( enabled_modules ); 
    string tmp_string; 
 
    mixed err; 
    forcibly_added = ([]); 
    enable_module_batch_msgs = 1; 
    foreach( modules_to_process, tmp_string ) 
    { 
      if( !forcibly_added[ tmp_string ] ) 
        if(err = catch( enable_module( tmp_string ))) 
        { 
          report_error(LOC_M(45, "Failed to enable the module %s. Skipping.") 
                       +"\n%s\n", tmp_string, describe_backtrace(err)); 
          got_no_delayed_load = -1; 
        } 
    } 
    enable_module_batch_msgs = 0; 
//      roxenloader.pop_compile_error_handler(); 
    forcibly_added = ([]); 
  } 
     
  foreach( ({this_object()})+indices( otomod ), RoxenModule mod ) 
    if( mod->ready_to_receive_requests ) 
      if( mixed q = catch( mod->ready_to_receive_requests( this_object() ) ) ) { 
        report_error( "While calling ready_to_receive_requests in "+ 
                      otomod[mod]+":\n"+ 
                      describe_backtrace( q ) ); 
        got_no_delayed_load = -1; 
      } 
 
  foreach( after_init_hooks, function q ) 
    if( mixed w = catch( q(this_object()) ) ) { 
      report_error( "While calling after_init_hook %O:\n%s", 
                    q,  describe_backtrace( w ) ); 
      got_no_delayed_load = -1; 
    } 
 
  after_init_hooks = ({}); 
 
  inited = 1; 
  if (!modules_already_enabled) 
    report_notice(LOC_S(4, "All modules for %s enabled in %3.1f seconds") + 
                  "\n\n", query_name(), (gethrtime()-start_time)/1000000.0); 
 
#ifdef SNMP_AGENT 
  // Start trap after real virt.serv. loading 
  if(query("snmp_process") && objectp(roxen->snmpagent)) 
    roxen->snmpagent->vs_start_trap(get_config_id()); 
#endif 
 
} 
 
DataCache datacache; 
 
static void create() 
{ 
  if (!name) error ("Configuration name not set through bootstrap_info.\n"); 
//   int st = gethrtime(); 
  roxen.add_permission( "Site:"+name, LOC_C(306,"Site")+": "+name ); 
 
  // for now only these two. In the future there might be more variables. 
  defvar( "data_cache_size", 16384, DLOCALE(274, "Cache:Cache size"), 
          TYPE_INT| VAR_PUBLIC, 
          DLOCALE(275, "The size of the data cache used to speed up requests " 
                  "for commonly requested files, in KBytes")); 
 
  defvar( "data_cache_file_max_size", 100, DLOCALE(276, "Cache:Max file size"), 
          TYPE_INT | VAR_PUBLIC, 
          DLOCALE(277, "The maximum size of a file that is to be considered for " 
                  "the cache, in KBytes.")); 
 
 
  defvar("default_server", 0, DLOCALE(20, "Ports: Default site"), 
         TYPE_FLAG| VAR_PUBLIC, 
         DLOCALE(21, "If true, this site will be selected in preference of " 
         "other sites when virtual hosting is used and no host " 
         "header is supplied, or the supplied host header does not " 
         "match the address of any of the other servers.") ); 
 
  defvar("comment", "", DLOCALE(22, "Site comment"), 
         TYPE_TEXT_FIELD|VAR_MORE, 
         DLOCALE(23, "This text will be visible in the administration " 
                 "interface, it can be quite useful to use as a memory helper.")); 
 
  defvar("name", "", DLOCALE(24, "Site name"), 
         TYPE_STRING|VAR_MORE| VAR_PUBLIC|VAR_NO_DEFAULT, 
         DLOCALE(25, "This is the name that will be used in the administration " 
         "interface. If this is left empty, the actual name of the " 
         "site will be used.")); 
 
  defvar("compat_level", Variable.StringChoice ( 
           "", roxen.compat_levels, VAR_NO_DEFAULT, 
           DLOCALE(246, "Compatibility level"), 
           DLOCALE(386, #"\ 
<p>The compatibility level is used by different modules to select the 
right behavior to remain compatible with earlier Roxen versions. When 
a server configuration is created, this variable is set to the current 
version. After that it's never changed automatically, thereby ensuring 
that server configurations migrated from earlier Roxen versions is 
kept at the right compatibility level.</p> 
 
<p>This variable may be changed manually, but it's advisable to test 
the site carefully afterwards. A reload of the whole server 
configuration is required to propagate the change properly to all 
modules.</p> 
 
<p>Available compatibility levels: 
<table> 
<tr valign='top'><td>2.1&nbsp;&nbsp;</td> 
  <td>Corresponds to Roxen WebServer 2.1.</td></tr> 
<tr valign=