[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[0010]
[0011]
[0012]
[0013]
[0014]
[0015]
[0016]
[0017]
[0018]
[0019]
[0020]
[0021]
[0022]
[0023]
[0024]
[0025]
[0026]
[0027]
[0028]
[0029]
[0030]
[0031]
[0032]
[0033]
[0034]
[0035]
[0036]
[0037]
[0038]
[0039]
[0040]
[0041]
[0042]
[0043]
[0044]
[0045]
[0046]
[0047]
[0048]
[0049]
[0050]
[0051]
[0052]
[0053]
[0054]
[0055]
[0056]
[0057]
[0058]
[0059]
[0060]
[0061]
[0062]
[0063]
[0064]
[0065]
[0066]
[0067]
[0068]
[0069]
[0070]
[0071]
[0072]
[0073]
[0074]
[0075]
[0076]
[0077]
[0078]
[0079]
[0080]
[0081]
[0082]
[0083]
[0084]
[0085]
[0086]
[0087]
[0088]
[0089]
[0090]
[0091]
[0092]
[0093]
[0094]
[0095]
[0096]
[0097]
[0098]
[0099]
[0100]
[0101]
[0102]
[0103]
[0104]
[0105]
[0106]
[0107]
[0108]
[0109]
[0110]
[0111]
[0112]
[0113]
[0114]
[0115]
[0116]
[0117]
[0118]
[0119]
[0120]
[0121]
[0122]
[0123]
[0124]
[0125]
[0126]
[0127]
[0128]
[0129]
[0130]
[0131]
[0132]
[0133]
[0134]
[0135]
[0136]
[0137]
[0138]
[0139]
[0140]
[0141]
[0142]
[0143]
[0144]
[0145]
[0146]
[0147]
[0148]
[0149]
[0150]
[0151]
[0152]
[0153]
[0154]
[0155]
[0156]
[0157]
[0158]
[0159]
[0160]
[0161]
[0162]
[0163]
[0164]
[0165]
[0166]
[0167]
[0168]
[0169]
[0170]
[0171]
[0172]
[0173]
[0174]
[0175]
[0176]
[0177]
[0178]
[0179]
[0180]
[0181]
[0182]
[0183]
[0184]
[0185]
[0186]
[0187]
[0188]
[0189]
[0190]
[0191]
[0192]
[0193]
[0194]
[0195]
[0196]
[0197]
[0198]
[0199]
[0200]
[0201]
[0202]
[0203]
[0204]
[0205]
[0206]
[0207]
[0208]
[0209]
[0210]
[0211]
[0212]
[0213]
[0214]
[0215]
[0216]
[0217]
[0218]
[0219]
[0220]
[0221]
[0222]
[0223]
[0224]
[0225]
[0226]
[0227]
[0228]
[0229]
[0230]
[0231]
[0232]
[0233]
[0234]
[0235]
[0236]
[0237]
[0238]
[0239]
[0240]
[0241]
[0242]
[0243]
[0244]
[0245]
[0246]
[0247]
[0248]
[0249]
[0250]
[0251]
[0252]
[0253]
[0254]
[0255]
[0256]
[0257]
[0258]
[0259]
[0260]
[0261]
[0262]
[0263]
[0264]
[0265]
[0266]
[0267]
[0268]
[0269]
[0270]
[0271]
[0272]
[0273]
[0274]
[0275]
[0276]
[0277]
[0278]
[0279]
[0280]
[0281]
[0282]
[0283]
[0284]
[0285]
[0286]
[0287]
[0288]
[0289]
[0290]
[0291]
[0292]
[0293]
[0294]
[0295]
[0296]
[0297]
[0298]
[0299]
[0300]
[0301]
[0302]
[0303]
[0304]
[0305]
[0306]
[0307]
[0308]
[0309]
[0310]
[0311]
[0312]
[0313]
[0314]
[0315]
[0316]
[0317]
[0318]
[0319]
[0320]
[0321]
[0322]
[0323]
[0324]
[0325]
[0326]
[0327]
[0328]
[0329]
[0330]
[0331]
[0332]
[0333]
[0334]
[0335]
[0336]
[0337]
[0338]
[0339]
[0340]
[0341]
[0342]
[0343]
[0344]
[0345]
[0346]
[0347]
[0348]
[0349]
[0350]
[0351]
[0352]
[0353]
[0354]
[0355]
[0356]
[0357]
[0358]
[0359]
[0360]
[0361]
[0362]
[0363]
[0364]
[0365]
[0366]
[0367]
[0368]
[0369]
[0370]
[0371]
[0372]
[0373]
[0374]
[0375]
[0376]
[0377]
[0378]
[0379]
[0380]
[0381]
[0382]
[0383]
[0384]
[0385]
[0386]
[0387]
[0388]
[0389]
[0390]
[0391]
[0392]
[0393]
[0394]
[0395]
[0396]
[0397]
[0398]
[0399]
[0400]
[0401]
[0402]
[0403]
[0404]
[0405]
[0406]
[0407]
[0408]
[0409]
[0410]
[0411]
[0412]
[0413]
[0414]
[0415]
[0416]
[0417]
[0418]
[0419]
[0420]
[0421]
[0422]
[0423]
[0424]
[0425]
[0426]
[0427]
[0428]
[0429]
[0430]
[0431]
[0432]
[0433]
[0434]
[0435]
[0436]
[0437]
[0438]
[0439]
[0440]
[0441]
[0442]
[0443]
[0444]
[0445]
[0446]
[0447]
[0448]
[0449]
[0450]
[0451]
[0452]
[0453]
[0454]
[0455]
[0456]
[0457]
[0458]
[0459]
[0460]
[0461]
[0462]
[0463]
[0464]
[0465]
[0466]
[0467]
[0468]
[0469]
[0470]
[0471]
[0472]
[0473]
[0474]
[0475]
[0476]
[0477]
[0478]
[0479]
[0480]
[0481]
[0482]
[0483]
[0484]
[0485]
[0486]
[0487]
[0488]
[0489]
[0490]
[0491]
[0492]
[0493]
[0494]
[0495]
[0496]
[0497]
[0498]
[0499]
[0500]
[0501]
[0502]
[0503]
[0504]
[0505]
[0506]
[0507]
[0508]
[0509]
[0510]
[0511]
[0512]
[0513]
[0514]
[0515]
[0516]
[0517]
[0518]
[0519]
[0520]
[0521]
[0522]
[0523]
[0524]
[0525]
[0526]
[0527]
[0528]
[0529]
[0530]
[0531]
[0532]
[0533]
[0534]
[0535]
[0536]
[0537]
[0538]
[0539]
[0540]
[0541]
[0542]
[0543]
[0544]
[0545]
[0546]
[0547]
[0548]
[0549]
[0550]
[0551]
[0552]
[0553]
[0554]
[0555]
[0556]
[0557]
[0558]
[0559]
[0560]
[0561]
[0562]
[0563]
[0564]
[0565]
[0566]
[0567]
[0568]
[0569]
[0570]
[0571]
[0572]
[0573]
[0574]
[0575]
[0576]
[0577]
[0578]
[0579]
[0580]
[0581]
[0582]
[0583]
[0584]
[0585]
[0586]
[0587]
[0588]
[0589]
[0590]
[0591]
[0592]
[0593]
[0594]
[0595]
[0596]
[0597]
[0598]
[0599]
[0600]
[0601]
[0602]
[0603]
[0604]
[0605]
[0606]
[0607]
[0608]
[0609]
[0610]
[0611]
[0612]
[0613]
[0614]
[0615]
[0616]
[0617]
[0618]
[0619]
[0620]
[0621]
[0622]
[0623]
[0624]
[0625]
[0626]
[0627]
[0628]
[0629]
[0630]
[0631]
[0632]
[0633]
[0634]
[0635]
[0636]
[0637]
[0638]
[0639]
[0640]
[0641]
[0642]
[0643]
[0644]
[0645]
[0646]
[0647]
[0648]
[0649]
[0650]
[0651]
[0652]
[0653]
[0654]
[0655]
[0656]
[0657]
[0658]
[0659]
[0660]
[0661]
[0662]
[0663]
[0664]
[0665]
[0666]
[0667]
[0668]
[0669]
[0670]
[0671]
[0672]
[0673]
[0674]
[0675]
[0676]
[0677]
[0678]
[0679]
[0680]
[0681]
[0682]
[0683]
[0684]
[0685]
[0686]
[0687]
[0688]
[0689]
[0690]
[0691]
[0692]
[0693]
[0694]
[0695]
[0696]
[0697]
[0698]
[0699]
[0700]
[0701]
[0702]
[0703]
[0704]
[0705]
[0706]
[0707]
[0708]
[0709]
[0710]
[0711]
[0712]
[0713]
[0714]
[0715]
[0716]
[0717]
[0718]
[0719]
[0720]
[0721]
[0722]
[0723]
[0724]
[0725]
[0726]
[0727]
[0728]
[0729]
[0730]
[0731]
[0732]
[0733]
[0734]
[0735]
[0736]
[0737]
[0738]
[0739]
[0740]
[0741]
[0742]
[0743]
[0744]
[0745]
[0746]
[0747]
[0748]
[0749]
[0750]
[0751]
[0752]
[0753]
[0754]
[0755]
[0756]
[0757]
[0758]
[0759]
[0760]
[0761]
[0762]
[0763]
[0764]
[0765]
[0766]
[0767]
[0768]
[0769]
[0770]
[0771]
[0772]
[0773]
[0774]
[0775]
[0776]
[0777]
[0778]
[0779]
[0780]
[0781]
[0782]
[0783]
[0784]
[0785]
[0786]
[0787]
[0788]
[0789]
[0790]
[0791]
[0792]
[0793]
[0794]
[0795]
[0796]
[0797]
[0798]
[0799]
[0800]
[0801]
[0802]
[0803]
[0804]
[0805]
[0806]
[0807]
[0808]
[0809]
[0810]
[0811]
[0812]
[0813]
[0814]
[0815]
[0816]
[0817]
[0818]
[0819]
[0820]
[0821]
[0822]
[0823]
[0824]
[0825]
[0826]
[0827]
[0828]
[0829]
[0830]
[0831]
[0832]
[0833]
[0834]
[0835]
[0836]
[0837]
[0838]
[0839]
[0840]
[0841]
[0842]
[0843]
[0844]
[0845]
[0846]
[0847]
[0848]
[0849]
[0850]
[0851]
[0852]
[0853]
[0854]
[0855]
[0856]
[0857]
[0858]
[0859]
[0860]
[0861]
[0862]
[0863]
[0864]
[0865]
[0866]
[0867]
[0868]
[0869]
[0870]
[0871]
[0872]
[0873]
[0874]
[0875]
[0876]
[0877]
[0878]
[0879]
[0880]
[0881]
[0882]
[0883]
[0884]
[0885]
[0886]
[0887]
[0888]
[0889]
[0890]
[0891]
[0892]
[0893]
[0894]
[0895]
[0896]
[0897]
[0898]
[0899]
[0900]
[0901]
[0902]
[0903]
[0904]
[0905]
[0906]
[0907]
[0908]
[0909]
[0910]
[0911]
[0912]
[0913]
[0914]
[0915]
[0916]
[0917]
[0918]
[0919]
[0920]
[0921]
[0922]
[0923]
[0924]
[0925]
[0926]
[0927]
[0928]
[0929]
[0930]
[0931]
[0932]
[0933]
[0934]
[0935]
[0936]
[0937]
[0938]
[0939]
[0940]
[0941]
[0942]
[0943]
[0944]
[0945]
[0946]
[0947]
[0948]
[0949]
[0950]
[0951]
[0952]
[0953]
[0954]
[0955]
[0956]
[0957]
[0958]
[0959]
[0960]
[0961]
[0962]
[0963]
[0964]
[0965]
[0966]
[0967]
[0968]
[0969]
[0970]
[0971]
[0972]
[0973]
[0974]
[0975]
[0976]
[0977]
[0978]
[0979]
[0980]
[0981]
[0982]
[0983]
[0984]
[0985]
[0986]
[0987]
[0988]
[0989]
[0990]
[0991]
[0992]
[0993]
[0994]
[0995]
[0996]
[0997]
[0998]
[0999]
[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]
/*****************************************************************************/
/*
                                 Error.c

Error reporting functions.

All errors are reported using HTML formatting.  The assumptions being: 

  o  That most errors will occur before a document begins being transfered
     (e.g. file not found), and so its content type has not been set, and 
     the error can sent with a "text/html" type.

  o  HTML documents are probably more extensive than plain-text documents.
     Hence if an error occurs during transfer (e.g. record too big for user's
     buffer) it can be reported within the document with correct formatting 
     (document context notwithstanding).

  o  If an error occurs while transfering a plain-text document the HTML
     message is still readable, albeit a little cluttered.  Additional,
     obvious text is included to try and highlight the message.

  o  In non-"text/..." transfers the message text is not included anyway.

After a request thread is created and an error is generated the storage
'rqptr->rqResponse.ErrorReportPtr' becomes non-NULL.  This mechanism can be
used  to detect whether an error has occured.  Most common errors create an
error  message in heap memory and point at this using
'rqptr->rqResponse.ErrorReportPtr'.  This message can be output at some time
convenient to the task underway.   After an error message is sent to the client
the 'rqptr->rqResponse.ErrorReportPtr'  is returned to a NULL value to indicate
no outstanding error (but usually the  thread is disposed of after an error
occurs).  Notification of other, more  critical errors are sent directly to the
client at the time they occur (for  instance, failed heap memory allocation).

If ErrorGeneral() is called without first setting rqptr->rqResponse.HttpStatus
it will default to 500, or "internal server error".  Therefore string
overflows and the like can be called without setting this code.  Query string
errors should set to 403 or 404 indication request cannot be processed.

If ErrorVmsStatus() is called without first setting rqptr->rqResponse.HttpStatus
the function will attempt to determine the most appropriate HTTP status code
from the VMS status value.  It will then default to 500, or "internal server
error", indicating some unsual (e.g. non file system) condition.

The server configuration can supply additional information included whenever
an error report is generated.  This includes a URL for contacting the system
administrator (note: URL, not text, etc.) when an implementation,
configuration, process quota, etc., error is reported.   It also can provide
text to be included against any other error report.  This text can contain
HTML markup, carriage control, etc., and is designed for a brief message
containing a link to an explanation page.

Error reporting can be performed by local (server-internal) redirection.  This
provides the opportunity for greater local-site control over content and
format.  When an error is redirected essential error information is encoded
into a query-string formatted (URL-encoded) string.  The redirection merely
creates a new request with the redirection document (SSI or CGI script) as the
path and this information as the query string.  The reporting document
environment can then use this information via the following CGI variables:

FORM_ERROR_ABOUT        item message is about (if applicable)
FORM_ERROR_ABOUT2       additional information (if applicable)
FORM_ERROR_BASIC        basic authentication challenge
FORM_ERROR_DIGEST       digest authentication challenge
FORM_ERROR_LINE         name of source code line (e.g. "1732")
FORM_ERROR_MODULE       name of source code module (e.g. "REQUEST")
FORM_ERROR_REALM        authentication realm
FORM_ERROR_STATUS       HTTP status code (e.g. "404")
FORM_ERROR_STATUS_TEXT  text meaning of above status code (e.g. "Not Found")
FORM_ERROR_STATUS_EXPLANATION  more detailed explanation of above status code
FORM_ERROR_TEXT         server-generated error message
FORM_ERROR_TEXT2        server suggestion about what to do about it :^)
FORM_ERROR_TYPE         "basic" or "detailed"
FORM_ERROR_URI          URI of original request
FORM_ERROR_VMS          VMS status value in decimal (if applicable)


VERSION HISTORY
---------------
22-SEP-2020  MGD  bugfix; ErrorReportFooter() use request heap for signature
20-JAN-2019  MGD  ErrorNoticed() extended to allow $FAO formatting
15-JUN-2019  MGD  ErrorNoticed() tweak NULL explanation from "" to "(none)"
30-JUN-2016  MGD  ErrorRedirectQueryString() ERROR_URI variable
29-APR-2015  MGD  bugfix; Error..() earlier and broader detection of WebDAV
10-MAY-2010  JPP  bugfix; do not use REDIRECT for WebDAV request error report
18-MAY-2008  MGD  bugfix; ErrorNoticed() use of 'rqptr' (from 16-NOV-2007)
16-NOV-2007  MGD  ErrorNoticed() add script/CGI data to OPCOM message
09-JUN-2007  MGD  use STR_DSC
17-SEP-2006  MGD  ErrorExitVmsStatus() add '-HTTPD-E-WHEN, <date/time>'
                  add '-HTTPD-E-STATUS, %X99999999' and restrict exit with
                  error status to the instance supervisor (as applicable) to
                  restrict image restart to the supervisor instance
16-JUL-2005  MGD  add selected request data to ErrorNoticed() report
14-JUL-2005  MGD  provide "Allow:" header with 405 (method not supported)
11-JUN-2005  MGD  allow report path to exclude using negative codes
12-AUG-2004  MGD  simple tunnel suitable messages
02-AUG-2004  MGD  error reports now have the response header generated
                  using the report buffer length, where possible, as a
                  content length (allowing connections to persist)
01-JUN-2004  MGD  detailed report explicitly set by mapping rule
13-JAN-2004  MGD  bugfix; ErrorGeneral() always get module name and number
26-AUG-2003  MGD  remove suppression of detail for success messages
10-JUN-2003  MGD  SS$_BAD.. moved from 500 to 403 response
01-MAR-2003  MGD  set html= header and footer
12-OCT-2002  MGD  tweak RMS error HTTP status slightly
16-SEP-2002  MGD  rqPathSet.Report4NNas
04-SEP-2002  MGD  add end-of-tag markup to the divider in ErrorSendToClient()
04-AUG-2001  MGD  support module WATCHing
16-FEB-2001  MGD  add service-based error report status code handling
17-JAN-2001  MGD  bugfix; "!&%AZ" formatting for basic/digest challenge
03-DEC-2000  MGD  bugfix; ErrorGeneral()
23-NOV-2000  MGD  bugfix; ErrorRedirectQueryString() 
08-APR-2000  MGD  rework error functions (including redirect)
29-MAR-2000  MGD  bugfix; ErrorExitVmsStatus() line number from !AZ to !UL
04-MAR-2000  MGD  use FaoToBuffer/l(),
                  add ErrorNoticed(), ErrorRedirectQueryString()
04-DEC-1999  MGD  divider handling
02-JAN-1999  MGD  proxy service
18-OCT-1998  MGD  error report redirection
30-AUG-1997  MGD  401 without a realm is now converted into a 403
09-AUG-1997  MGD  message database, with considerable rewrite!
01-FEB-1997  MGD  HTTPd version 4
06-JUN-1996  MGD  added authentication failure
01-DEC-1995  MGD  HTTPd version 3
25-MAR-1995  MGD  minor functional and cosmetic changes
20-DEC-1994  MGD  multi-threaded daemon
*/
/*****************************************************************************/

#ifdef WASD_VMS_V7
#undef _VMS__V6__SOURCE
#define _VMS__V6__SOURCE
#undef __VMS_VER
#define __VMS_VER 70000000
#undef __CRTL_VER
#define __CRTL_VER 70000000
#endif

/* standard C header files */
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

/* VMS related header files */
#include <descrip.h>
#include <iodef.h>
#include <ssdef.h>
#include <stsdef.h>

/* application related header files */
#include "wasd.h"

#define WASD_MODULE "ERROR"

/******************/
/* global storage */
/******************/

$DESCRIPTOR (ErrorNumberFaoDsc, "!UL\0");
$DESCRIPTOR (ErrorVmsStatusFaoDsc, "!&S\0");

BOOL  ErrorStatusReportFormatAbi;

int  ErrorsNoticedCount;

int64  ErrorsNoticedTime64;

char  ErrorSanityCheck [] = "sanity check failure!",
      ErrorServerFao [] = "significant HTTPd sys$fao() failed.",
      ErrorSslFailure [] = "secure connection failure",
      ErrorXvalNotValid [] = "reverting to pre-V8.2 locking,";

char  *ErrorRecommendNotifyPtr;

/********************/
/* external storage */
/********************/

#ifdef DBUG
extern BOOL Debug;
#else
#define Debug 0 
#endif

extern BOOL  InstanceNodeSupervisor,
             LoggingEnabled;

extern int  InstanceNodeConfig,
            OpcomMessages;

extern int64  HttpdTime64;

extern int  ToLowerCase[],
            ToUpperCase[];

extern char  HttpProtocol[],
             ResponseAllowResource[],
             ResponseAllowServer[],
             SoftwareID[];

extern CONFIG_STRUCT  Config;
extern MSG_STRUCT  Msgs;
extern WATCH_STRUCT  Watch;

/*****************************************************************************/
/*
Check that the quasi-FAO directives expected in a possibly user-configured
report format are those that are expected, and no more/no less.  This obviously
provides some pre-runtime integrity confidence, reducing the chance of a server
ACCVIO or some such.
*/

void ErrorCheckReportFormats ()

{
   static struct {
      char  *ReportName;
      char  *CheckString;
      char  *ReportFormat;
   } FormatCheck [] =
   {
     { "Report Format", "!AZ!AZ!UL!AZ!**!AZ!UL!AZ!&@!&@!&@", NULL },
     { "Signature Format", "!AZ", NULL },
     { "Server Signature", "!AZ!AZ!AZ!UL", NULL },
     { NULL, NULL, NULL, }
   };

   BOOL  ErrorReported;
   int  idx;
   char  *cptr, *sptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "ErrorCheckReportFormats()");

   FormatCheck[0].ReportFormat = MsgFor(NULL,MSG_STATUS_REPORT_FORMAT);
   FormatCheck[1].ReportFormat = MsgFor(NULL,MSG_STATUS_REPORT_FORMAT_SIG);
   FormatCheck[2].ReportFormat = MsgFor(NULL,MSG_STATUS_SIGNATURE);
   ErrorReported = false;

   for (idx = 0; FormatCheck[idx].ReportName; idx++)
   {
      cptr = FormatCheck[idx].ReportFormat;
      sptr = FormatCheck[idx].CheckString;

      while (*cptr)
      {
         while (*cptr && *cptr != '!') cptr++;
         if (SAME2(cptr,'!!'))
         {
            cptr += 2;
            continue;
         }
         if (*sptr != '!') break;
         if (SAME2(cptr,'!+'))
         {
            sptr++;
            if (SAME2(sptr,'AZ') ||
                SAME2(sptr,'UL') ||
                SAME2(sptr,'&@'))
            {
               cptr += 2;
               sptr += 2;
               continue;
            }
            break;
         }
         if (*cptr == '!' && SAME2(cptr+1,'%%') &&
             *sptr == '!' && SAME2(sptr+1,'&@'))
         {
            /* '%%' as <-7.3 backward compatible with 7.3-> '&@' */
            cptr += 3;
            sptr += 3;
            continue;
         }
         if ((*cptr == '!' && SAME2(cptr+1,'AZ') ||
              *cptr == '!' && SAME2(cptr+1,'%%') ||
              *cptr == '!' && SAME2(cptr+1,'&@')) &&
              *sptr == '!' && SAME2(sptr+1,'**'))
         {
            /* allow either "!AZ" or "!%%" in report format string */
            cptr += 3;
            sptr += 3;
            continue;
         }
         while (*sptr && *cptr && *sptr == *cptr)
         {
            cptr++;
            sptr++;
            if (*sptr == '!') break;
         }
         if (*sptr && *sptr != '!') break;
      }

      if (*cptr && *sptr)
         sptr = "problem detected";
      else
      if (*sptr)
         sptr = "too few directives";
      else
      if (*cptr)
         sptr = "too many directives";
      else
         sptr = "";
      if (*sptr)
      {
         FaoToStdout ("%HTTPD-E-MSG, !AZ in \"!AZ\"\n \\!AZ\\\n",
                      sptr, FormatCheck[idx].ReportName,
                      *cptr ? cptr : "(end-of-string)");
         ErrorReported = true;
      }
   }

   if (ErrorReported)
   {
      if (OpcomMessages & OPCOM_HTTPD)
         FaoToOpcom ("%HTTPD-E-MSG, problem(s) with reports format");
      exit (SS$_ABORT);
   }

   /* check whether the fifth directive is a !AZ (<-8.2) or !%%/!&@ (8.2->) */
   cptr = FormatCheck[0].ReportFormat;
   for (idx = 0; idx < 5; idx++)
   {
      while (*cptr && *cptr != '!') cptr++;
      if (*cptr == '!') cptr++;
   }
   if (SAME2(cptr,'%%') ||
       SAME2(cptr,'&@'))
       ErrorStatusReportFormatAbi = true;
   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "!&B !&Z",
                 ErrorStatusReportFormatAbi, cptr);
}

/*****************************************************************************/
/*
Exit the application after output of the module and line location reporting 
the error, a brief explanation, and then exiting generating a DCL-reported 
message.
*/

void ErrorExitVmsStatus
(
int StatusValue,
char *ExplanationPtr,
char *SourceModule,
int SourceLine
)
{
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "ErrorExitVmsStatus()");

   if (!ExplanationPtr) ExplanationPtr = "(null)";

   FaoToStdout (
"%HTTPD-F-SOFTWAREID, !AZ\n\
-HTTPD-F-WHEN, !%D\n\
-HTTPD-F-WHERE, module:!AZ line:!UL\n\
-HTTPD-F-WHAT, !AZ\n",
      SoftwareID, 0,
      SourceModule, SourceLine,
      ExplanationPtr);

   exit (StatusValue | STS$M_INHIB_MSG);
}

/*****************************************************************************/
/*
Report to the server process log, and if enabled to OPCOM, a non-fatal error
that has been noticed.  Add selected request data to the process log in an
effort to provide additional, pertinent information related to the report.
*/

void ErrorNoticed
(
REQUEST_STRUCT *rqptr,
int  StatusValue,
char *FaoPtr,
char *SourceModule,
int SourceLine,
...
)
{
   int  argcnt, hlen, llen, tlen, status;
   ushort  slen;
   ulong  *vecptr;
   ulong  FaoVector [32];
   char  *cptr, *sptr;
   char  Buffer [4096];
   va_list  argptr;

   /*********/
   /* begin */
   /*********/

   va_count (argcnt);

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER,
                 "ErrorNoticed() !UL", argcnt);

   ErrorsNoticedCount++;
   ErrorsNoticedTime64 = HttpdTime64;

   if (argcnt > 5 + 32)
   {
      ErrorNoticed (rqptr, SS$_OVRMAXARG, "!AZ", FI_LI, FaoPtr);
      return;
   }

   if (!FaoPtr) FaoPtr = "(none)";

   if (argcnt > 5)
   {
      vecptr = FaoVector;
      va_start (argptr, SourceLine);
      for (argcnt -= 5; argcnt; argcnt--)
         *vecptr++ = va_arg (argptr, ulong);
      va_end (argptr);

      status = FaolToBuffer (Buffer, sizeof(Buffer), &slen, FaoPtr, &FaoVector);
      if (VMSnok (status) || status == SS$_BUFFEROVF)
      {
         ErrorNoticed (rqptr, status, "!AZ", FI_LI, FaoPtr);
         return;
      }

      FaoPtr = Buffer;
   }

   if (StatusValue)
   {
      if (WATCH_CAT && Watch.Category)
      {
         WatchThis (WATCHALL, WATCH_INTERNAL, "!AZ:!UL, !AZ!AZ%X!XL",
                    SourceModule, SourceLine,
                    FaoPtr, FaoPtr[0] ? " " : "", StatusValue);
         WatchDataFormatted ("%!&M\n", StatusValue);
      }

      FaoToStdout (
"%HTTPD-W-NOTICED, !20%D, !AZ:!UL, !AZ!AZ%X!XL\n-!-!&M\n",
         0, SourceModule, SourceLine,
         FaoPtr, FaoPtr[0] ? " " : "", StatusValue);

      FaoToOpcom (
"%HTTPD-W-NOTICED, !AZ:!UL, !AZ!AZ%X!XL\r\n-!-!&M",
         SourceModule, SourceLine,
         FaoPtr, FaoPtr[0] ? " " : "", StatusValue);
   }
   else
   {
      if (WATCH_CAT && Watch.Category)
         WatchThis (WATCHALL, WATCH_INTERNAL, "!AZ:!UL, !AZ",
                    SourceModule, SourceLine, FaoPtr);

      FaoToStdout ("%HTTPD-W-NOTICED, !20%D, !AZ:!UL, !AZ\n",
                   0, SourceModule, SourceLine, FaoPtr);

      if (rqptr && rqptr->ScriptName[0])
      {
         if (rqptr->rqCgi.HeaderPtr && rqptr->rqCgi.HeaderPtr[0])
         {
            for (sptr = cptr = rqptr->rqCgi.HeaderPtr; *sptr; sptr++);
            tlen = hlen = sptr - cptr;
            if (hlen > 24) hlen = 24;
            sptr = cptr;
            /* in case there is leading carriage-control */
            if (*sptr == '\r') sptr++;
            if (*sptr == '\n') sptr++;
            while (*sptr && *sptr != '\n') sptr++;
            llen = sptr - cptr;
            if (llen < hlen) llen = hlen;

            FaoToBuffer (Buffer, sizeof(Buffer), 0,
"!AZ\r\n\
-HTTPD-I-SCRIPT, !AZ (!AZ) !AZ\r\n\
-HTTPD-I-CGI, !#&H (!UL bytes) !#&.AZ",
                         FaoPtr,
                         rqptr->ScriptName,
                         rqptr->RequestMappedScript,
                         rqptr->RequestMappedRunTime,
                         hlen, cptr, tlen, llen, cptr);

            FaoPtr = Buffer;
         }
      }

      FaoToOpcom ("%HTTPD-W-NOTICED, !AZ:!UL, !AZ",
                  SourceModule, SourceLine, FaoPtr);
   }

   if (!rqptr) return;

   if (rqptr->RequestState >= REQUEST_STATE_SHUTDOWN) return;

   /****************/
   /* request data */
   /****************/

   if (rqptr->DclTaskPtr &&
       rqptr->DclTaskPtr->NextTaskFunction == &DclScriptProctorAst)
      FaoToStdout ("-NOTICED-I-PROCTOR, !UL+!UL !AZ!AZ!AZ!AZ !AZ!AZ!AZ\n",
                       rqptr->DclTaskPtr->ProctorPtr->NumberMin,
                       rqptr->DclTaskPtr->ProctorPtr->NumberIdle,
                       *rqptr->DclTaskPtr->ProctorPtr->RunTimePtr ? "(" : "",
                       rqptr->DclTaskPtr->ProctorPtr->RunTimePtr,
                       *rqptr->DclTaskPtr->ProctorPtr->RunTimePtr ? ")" : "",
                       rqptr->DclTaskPtr->ProctorPtr->ScriptPtr,
                       rqptr->DclTaskPtr->ProctorPtr->ActivatePtr,
                       *rqptr->DclTaskPtr->ProctorPtr->NotePadPtr ? " " : "",
                       rqptr->DclTaskPtr->ProctorPtr->NotePadPtr);

   if (rqptr->ServicePtr &&
       rqptr->ServicePtr->RequestSchemeNamePtr)
      FaoToStdout ("-NOTICED-I-SERVICE, !AZ//!AZ\n",
                   rqptr->ServicePtr->RequestSchemeNamePtr,
                   rqptr->ServicePtr->ServerHostPort);

   if (rqptr->ClientPtr &&
       &rqptr->ClientPtr->IpAddressString[0])
      FaoToStdout ("-NOTICED-I-CLIENT, !AZ\n", ClientHostString(rqptr));

    FaoToStdout ("-NOTICED-I-HTTP, !UL\n",
                 rqptr->Http2Stream.Http2Ptr ? 2 : 1);

   if (rqptr->rqHeader.RequestUriPtr &&
       rqptr->rqHeader.RequestUriLength)
      FaoToStdout ("-NOTICED-I-URI, !AZ (!UL bytes) !AZ\n",
                   rqptr->rqHeader.MethodName,
                   rqptr->rqHeader.RequestUriLength,
                   rqptr->rqHeader.RequestUriPtr);

   if (rqptr->FileTaskPtr)
      if (rqptr->FileTaskPtr->FileNameLength)
         FaoToStdout ("-NOTICED-I-FILE, !#AZ\n",
                      rqptr->FileTaskPtr->FileNameLength,
                      rqptr->FileTaskPtr->FileName);

   if (rqptr->ScriptName[0])
   {
      FaoToStdout ("-NOTICED-I-SCRIPT, !AZ !AZ (!AZ) !AZ\n",
                   rqptr->ScriptName,
                   rqptr->RequestMappedScript,
                   rqptr->RequestMappedRunTime,
                   rqptr->rqCgi.ScriptFileNamePtr ?
                      rqptr->rqCgi.ScriptFileNamePtr : "");

      if (rqptr->rqCgi.HeaderPtr && rqptr->rqCgi.HeaderPtr[0])
      {
         for (sptr = cptr = rqptr->rqCgi.HeaderPtr; *sptr; sptr++);
         tlen = hlen = sptr - cptr;
         if (hlen > 24) hlen = 24;
         sptr = cptr;
         /* in case there is leading carriage-control */
         if (*sptr == '\r') sptr++;
         if (*sptr == '\n') sptr++;
         while (*sptr && *sptr != '\n') sptr++;
         llen = sptr - cptr;
         if (llen < hlen) llen = hlen;
         FaoToStdout ("-NOTICED-I-CGI, !#&H (!UL bytes) !#&.AZ\n",
                      hlen, cptr, tlen, llen, cptr);
      }
   }

   FaoToStdout ("-NOTICED-I-RXTX, err:!UL/!UL raw:!@SQ/!@SQ net:!@SQ/!@SQ\n",
                rqptr->rqNet.ReadErrorCount, rqptr->rqNet.WriteErrorCount,
                &rqptr->NetIoPtr->BytesRawRx64, &rqptr->NetIoPtr->BytesRawTx64,
                &rqptr->BytesRx64, &rqptr->BytesTx64);
}

/****************************************************************************/
/*
It is a fatal error to call this function without an error message.  If a
response header and output has already been generated insert a plain-text and
HTML obvious divider into the output (hopefully it's text ... it doesn't really
matter anyway, it's an error condition after all :^) and then send the actual
error message.  If no response has yet been made then either redirect error
processing appropriately (using the query-string format error information
generated by ErrorVmsStatus() and ErrorGeneral()), or generate an response
header then send the message.
*/

void ErrorSendToClient (REQUEST_STRUCT *rqptr)

{
   /* contains HTML, also enough to make it obvious in plain text */
   static char  DividerFao [] =
"</tt></b></u></i></strike></sub></sup></pre>\
</nobr></OL></UL></DL></BLOCKQUOTE></table>\n\
<p><font color=\"#ff0000\"><b><hr width=\"100%\" size=\"2\" noshade>\n\
\n\
<!!-- -- --   !AZ   -- -- -->\n\
\n";

   static int  DividerLength;
   static char  *DividerPtr;

   int  status,
        ContentLength;
   ushort  Length;
   ulong  *vecptr;
   ulong  FaoVector [32];
   char  *cptr, *sptr,
         *ResponseAllowPtr;
   char  Buffer [1024];

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER, "ErrorSendToClient()");

   if (!rqptr->rqResponse.ErrorReportPtr)
      ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   if (rqptr->rqPathSet.ReportType == ERROR_REPORT_TUNNEL ||
       rqptr->ServicePtr->ProxyTunnel == PROXY_TUNNEL_FIREWALL ||
       rqptr->ServicePtr->ProxyTunnel == PROXY_TUNNEL_RAW)
   {
      /******************/
      /* tunnel message */
      /******************/

      if (rqptr->rqPathSet.ReportType == ERROR_REPORT_TUNNEL)
         ResponseHeader (rqptr, rqptr->rqResponse.HttpStatus,
                         "text/plain", -1, NULL, NULL);

      /* errors are always sent as the request is finalized */
      NetWrite (rqptr, &RequestEnd,
                rqptr->rqResponse.ErrorReportPtr,
                rqptr->rqResponse.ErrorReportLength);

      /* indicate the error message has been sent */
      rqptr->rqResponse.ErrorReportPtr = NULL;
      rqptr->rqResponse.ErrorReportLength = 0;

      return;
   }

   if (rqptr->rqResponse.HeaderGenerated &&
       STR_DSC_PTR(&rqptr->NetWriteBufferDsc))
   {
      /*******************/
      /* provide divider */
      /*******************/

      if (!DividerPtr)
      {
         status = FaoToBuffer (Buffer, sizeof(Buffer), &Length,
                               DividerFao, MsgFor(rqptr,MSG_STATUS_ERROR));
         if (VMSnok (status) || status == SS$_BUFFEROVF)
            ErrorNoticed (rqptr, status, NULL, FI_LI);

         DividerPtr = VmGet (Length+1);
         memcpy (DividerPtr, Buffer, (DividerLength=Length)+1);
      }

      NetWrite (rqptr, &ErrorMessageDividerAst, DividerPtr, DividerLength);

      return;
   }

   if (rqptr->rqResponse.ErrorReportByRedirect)
   {
      /******************************/
      /* redirect to error reporter */
      /******************************/

      /* log the original request */
      if (LoggingEnabled) Logging (rqptr, LOGGING_ENTRY);

      status = FaoToBuffer (Buffer, sizeof(Buffer), &Length,
                            rqptr->ServicePtr->ErrorReportPath,
                            rqptr->rqResponse.HttpStatus);
      if (VMSnok (status) || status == SS$_BUFFEROVF)
         ErrorNoticed (rqptr, status, NULL, FI_LI);

      /* combine the redirection path and the error query string */
      Length += rqptr->rqResponse.ErrorReportLength;

      /* reserve space in the dictionary then build in situ */
      sptr = ResponseLocation (rqptr, NULL, Length);
      for (cptr = Buffer; *cptr; *sptr++ = *cptr++);
      for (cptr = rqptr->rqResponse.ErrorReportPtr; *cptr; *sptr++ = *cptr++);

      /* indicate the error message has been sent */
      rqptr->rqResponse.ErrorReportPtr = NULL;
      rqptr->rqResponse.ErrorReportLength = 0;

      /* indicate error redirection in this thread-permanent storage */
      rqptr->RedirectErrorStatusCode = rqptr->rqResponse.HttpStatus;
      rqptr->RedirectErrorAuthRealmDescrPtr = rqptr->rqAuth.RealmDescrPtr;

      /* errors are always sent as the request is finalized, continue that */
      RequestEnd (rqptr);

      return;
   }

   /***********************/
   /* provide HTTP header */
   /***********************/

   if (!rqptr->rqResponse.HeaderGenerated &&
       !rqptr->rqResponse.HeaderSent)
      ContentLength = rqptr->rqResponse.ErrorReportLength;
   else
      ContentLength = -1;

   if (rqptr->rqResponse.HttpStatus == 405)
      ResponseAllowPtr = ResponseAllowResource;
   else
      ResponseAllowPtr = "";

   ResponseHeader (rqptr, rqptr->rqResponse.HttpStatus,
                   "text/html", ContentLength, NULL, ResponseAllowPtr);

   /* errors are always sent as the request is finalized */
   NetWrite (rqptr, &RequestEnd,
             rqptr->rqResponse.ErrorReportPtr,
             rqptr->rqResponse.ErrorReportLength);

   /* indicate the error message has been sent */
   rqptr->rqResponse.ErrorReportPtr = NULL;
   rqptr->rqResponse.ErrorReportLength = 0;
}

/*****************************************************************************/
/*
Divider delivered, now output the error report.
*/

void ErrorMessageDividerAst (REQUEST_STRUCT *rqptr)

{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER,
                 "ErrorMessageDividerAst() !&F", &ErrorMessageDividerAst);

   /* errors are always sent as the request is finalized, continue that */
   NetWrite (rqptr, &RequestEnd,
             rqptr->rqResponse.ErrorReportPtr,
             rqptr->rqResponse.ErrorReportLength);

   /* indicate the error message has been sent */
   rqptr->rqResponse.ErrorReportPtr = NULL;
   rqptr->rqResponse.ErrorReportLength = 0;
}

/*****************************************************************************/
/*
Set html= body and/or header.
*/

void ErrorReportHeader
(
REQUEST_STRUCT *rqptr,
ulong  **vecptrptr
)
{
   ulong  *vecptr;

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER, "ErrorReportHeader()");

   vecptr = *vecptrptr;

   if (ErrorStatusReportFormatAbi)
   {
      if (rqptr->rqPathSet.HtmlBodyTagPtr)
      {
         if (rqptr->rqPathSet.HtmlBodyTagPtr[0] == '<')
            *vecptr++ = "!AZ!&@";
         else
            *vecptr++ = "<body!&+AZ>\n!&@";
         *vecptr++ = rqptr->rqPathSet.HtmlBodyTagPtr;
      }
      else
      if (rqptr->ServicePtr->BodyTag[0])
      {
         *vecptr++ = "!AZ!&@";
         *vecptr++ = rqptr->ServicePtr->BodyTag;
      }
      else
      {
         *vecptr++ = "!AZ!&@";
         *vecptr++ = REPORT_BODY_TAG;
      }

      if (rqptr->rqPathSet.HtmlHeaderPtr ||
          rqptr->rqPathSet.HtmlHeaderTagPtr)
      {
         if (rqptr->rqPathSet.HtmlHeaderTagPtr &&
             rqptr->rqPathSet.HtmlHeaderTagPtr[0] == '<')
            *vecptr++ = "!AZ\n!&@";
         else
            *vecptr++ =
"<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td!&+AZ>\n!&@";
         *vecptr++ = rqptr->rqPathSet.HtmlHeaderTagPtr;
         *vecptr++ = "!AZ<p>";
         *vecptr++ = rqptr->rqPathSet.HtmlHeaderPtr;
      }
      else
         *vecptr++ = "";
   }
   else
   if (rqptr->ServicePtr->BodyTag[0])
      *vecptr++ = rqptr->ServicePtr->BodyTag;
   else
      *vecptr++ = REPORT_BODY_TAG;

   *vecptrptr = vecptr;
}

/*****************************************************************************/
/*
Set html= body and/or header.
*/

void ErrorReportEndHeader
(
REQUEST_STRUCT *rqptr,
ulong  **vecptrptr
)
{
   ulong  *vecptr;

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER, "ErrorReportEndHeader()");

   vecptr = *vecptrptr;

   if (ErrorStatusReportFormatAbi &&
       (rqptr->rqPathSet.HtmlHeaderPtr ||
        rqptr->rqPathSet.HtmlHeaderTagPtr))
      *vecptr++ = "</td></tr></table>\n!&@";

   *vecptrptr = vecptr;
}

/*****************************************************************************/
/*
Set html= footer.
*/

void ErrorReportFooter
(
REQUEST_STRUCT *rqptr,
ulong  **vecptrptr
)
{
#define SIG_SIZE 256

   ulong  *vecptr;
   char  *sptr;

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER, "ErrorReportFooter()");

   if (Config.cfServer.Signature) sptr = VmGetHeap (rqptr, SIG_SIZE);

   vecptr = *vecptrptr;

   if (ErrorStatusReportFormatAbi &&
       (rqptr->rqPathSet.HtmlFooterPtr ||
        rqptr->rqPathSet.HtmlFooterTagPtr))
   {
      if (rqptr->rqPathSet.HtmlFooterTagPtr &&
          rqptr->rqPathSet.HtmlFooterTagPtr[0] == '<')
         *vecptr++ = "\n<p>!AZ\n!&@";
      else
         *vecptr++ =
"\n<p><table cellpadding=\"5\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td!&+AZ>\n!&@";
      *vecptr++ = rqptr->rqPathSet.HtmlFooterTagPtr;
      if (Config.cfServer.Signature)
      {
         *vecptr++ = "!AZ\n<p>!&@";
         *vecptr++ = ServerSignature (rqptr, sptr, SIG_SIZE);
      }
      else
         *vecptr++ = "!&@";
       *vecptr++ = "!AZ\n</td></tr></table>";
       *vecptr++ = rqptr->rqPathSet.HtmlFooterPtr;
   }
   else
   if (Config.cfServer.Signature)
   {
      *vecptr++ = "\n!&@";
      *vecptr++ = MsgFor(rqptr,MSG_STATUS_REPORT_FORMAT_SIG);
      *vecptr++ = ServerSignature (rqptr, sptr, SIG_SIZE);
   }
   else
      *vecptr++ = "";

   *vecptrptr = vecptr;
}

/*****************************************************************************/
/*
Yes!  Report success from the error module.  Most consistant place to put it. 
Perhaps the ERROR module should be renamed to REPORT!
*/
 
void ErrorReportSuccess
(
REQUEST_STRUCT *rqptr,
char *ExplanationPtr,
...
)
{
   int  status,
        argcnt,
        StatusCode;
   ushort  Length;
   ulong  *vecptr;
   ulong  FaoVector [32];
   char  *cptr, *sptr, *zptr,
         *CategoryPtr,
         *ReportPtr;
   char  Buffer [4096];
   va_list  argptr;

   /*********/
   /* begin */
   /*********/

   va_count (argcnt);

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER,
                 "ErrorReportSuccess() !UL", argcnt);

   if (argcnt > 16)
   {
      ErrorNoticed (rqptr, SS$_OVRMAXARG, NULL, FI_LI);
      return;
   }

   if (!rqptr->rqResponse.HttpStatus) rqptr->rqResponse.HttpStatus = 200;
   StatusCode = rqptr->rqResponse.HttpStatus;
   /*
      HTTP status 201 inhibits the response informational message
      (in at least Netscape Navigator 3.0), so let's revert to 200!
   */
   if (rqptr->rqResponse.HttpStatus == 201) rqptr->rqResponse.HttpStatus = 200;

   if (WATCHING (rqptr, WATCH_RESPONSE))
   {
      cptr = "default";
      switch (rqptr->rqPathSet.ReportType)
      {
         case ERROR_REPORT_BASIC : cptr = "basic"; break;
         case ERROR_REPORT_DETAILED : cptr = "detailed"; break;
         case ERROR_REPORT_TUNNEL : cptr = "tunnel"; break;
         default: if (Config.cfReport.BasicOnly) cptr = "basic-only";
      }
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
                 "SUCCESS (!AZ) !3ZL \"!AZ\"",
                 cptr, StatusCode, ExplanationPtr);
   }
                
   vecptr = FaoVector;

   *vecptr++ = "";
   *vecptr++ = CategoryPtr = MsgFor(rqptr,MSG_STATUS_SUCCESS);
   *vecptr++ = StatusCode;
   *vecptr++ = HttpStatusCodeText(StatusCode);

   ErrorReportHeader (rqptr, &vecptr);

   *vecptr++ = CategoryPtr;
   *vecptr++ = StatusCode;
   *vecptr++ = HttpStatusCodeExplanation (rqptr,StatusCode);

   ErrorReportEndHeader (rqptr, &vecptr);

   *vecptr++ = "<p>!&@";
   /* a leading dollar indicates a server administration report */
   if (ExplanationPtr[0] == '$')
      *vecptr++ = ExplanationPtr+1;
   else
      *vecptr++ = ExplanationPtr;
   /* now add the variable length arguments to the vector */
   va_start (argptr, ExplanationPtr);
   for (argcnt -= 2; argcnt; argcnt--)
      *vecptr++ = va_arg (argptr, ulong);
   va_end (argptr);
   *vecptr++ = MsgFor(rqptr,MSG_STATUS_INFO);

   ErrorReportFooter (rqptr, &vecptr);

   ReportPtr = MsgFor(rqptr,MSG_STATUS_REPORT_FORMAT);
   status = FaolToBuffer (Buffer, sizeof(Buffer), &Length, ReportPtr, &FaoVector);
   if (VMSnok (status) || status == SS$_BUFFEROVF)
      ErrorExitVmsStatus (status, ErrorServerFao, FI_LI);

   ResponseHeader (rqptr, 200, "text/html", Length, NULL, NULL);

   /* dollar indicates a server administration report, write immediately! */
   if (ExplanationPtr[0] == '$')
      NetWrite (rqptr, NULL, Buffer, Length);
   else
      NetWriteBuffered (rqptr, NULL, Buffer, Length);
}

/*****************************************************************************/
/*
Generate an error message about a general (non-VMS status) problem for
subsequent reporting to the client.  If error redirection applies to the
request then generate the error information in a form suitable for provision as
a request query-string.
*/
 
void ErrorGeneral
(
REQUEST_STRUCT *rqptr,
char *ExplanationPtr,
...
)
{
   BOOL  StandardReport;
   int  idx, status,
        argcnt,
        OriginalStatusCode,
        SourceLine,
        StatusCode;
   ushort  Length;
   ulong  *vecptr;
   ulong  FaoVector [32];
   char  *cptr, *sptr, *zptr,
         *CategoryPtr,
         *ReportPtr,
         *SourceModule;
   char  Buffer [4096];
   va_list  argptr;

   /*********/
   /* begin */
   /*********/

   va_count (argcnt);

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER,
                 "ErrorGeneral() !UL", argcnt);

   if (argcnt > 16)
   {
      ErrorNoticed (rqptr, SS$_OVRMAXARG, NULL, FI_LI);
      return;
   }

   /* don't overwrite any existing error message */
   if (ERROR_REPORTED (rqptr)) return;

   /* not if WebDAV is involved */
   if (rqptr->WebDavRequest || rqptr->WhiffOfWebDav || rqptr->WebDavTaskPtr)
      rqptr->rqResponse.ErrorReportByRedirect = false;

   /* if this request is processing an error report redirect */
   if (rqptr->rqResponse.ErrorReportByRedirect &&
       rqptr->RedirectErrorStatusCode)
   {
      /* problem processing the error report, no infinite loops here please! */
      rqptr->rqResponse.ErrorReportByRedirect = false;
      rqptr->RedirectErrorStatusCode = 0;
      rqptr->RedirectErrorAuthRealmDescrPtr = NULL;
   }

   if (rqptr->rqResponse.HeaderGenerated &&
       STR_DSC_PTR(&rqptr->NetWriteBufferDsc))
   {
      /* some output has already occured, internal error reporting */
      rqptr->rqResponse.ErrorReportByRedirect = false;
   }

   OriginalStatusCode = rqptr->rqResponse.HttpStatus;

   if (!rqptr->rqResponse.HttpStatus) rqptr->rqResponse.HttpStatus = 500;

   /* when mapping error status by implication its a basic report! */
   if ((rqptr->rqResponse.HttpStatus == 400 ||
        rqptr->rqResponse.HttpStatus == 409) &&
       rqptr->rqPathSet.Report400as)
   {
      rqptr->rqResponse.HttpStatus = rqptr->rqPathSet.Report400as;
      rqptr->rqPathSet.ReportType = ERROR_REPORT_BASIC;
   }
   else
   if ((rqptr->rqResponse.HttpStatus == 403 ||
        rqptr->rqResponse.HttpStatus == 409) &&
       rqptr->rqPathSet.Report403as)
   {
      rqptr->rqResponse.HttpStatus = rqptr->rqPathSet.Report403as;
      rqptr->rqPathSet.ReportType = ERROR_REPORT_BASIC;
   }
   else
   if ((rqptr->rqResponse.HttpStatus == 404 ||
        rqptr->rqResponse.HttpStatus == 409) &&
       rqptr->rqPathSet.Report404as)
   {
      rqptr->rqResponse.HttpStatus = rqptr->rqPathSet.Report404as;
      rqptr->rqPathSet.ReportType = ERROR_REPORT_BASIC;
   }

   StatusCode = rqptr->rqResponse.HttpStatus;

   if (StatusCode == 401 || StatusCode == 407) ResponseHeaderChallenge (rqptr);

   /* get the reporting module name and line number */
   va_start (argptr, ExplanationPtr);
   for (argcnt -= 2; argcnt; argcnt--)
   {
      /* the last two arguments must be the macro "FI_LI" */
      if (argcnt == 2)
         SourceModule = va_arg (argptr, ulong);
      else
      if (argcnt == 1)
         SourceLine = va_arg (argptr, ulong);
      else
         va_arg (argptr, ulong);
   }
   va_end (argptr);

   if (WATCHING (rqptr, WATCH_ERROR))
   {
      cptr = "default";
      switch (rqptr->rqPathSet.ReportType)
      {
         case ERROR_REPORT_BASIC : cptr = "basic"; break;
         case ERROR_REPORT_DETAILED : cptr = "detailed"; break;
         case ERROR_REPORT_TUNNEL : cptr = "tunnel"; break;
         default: if (Config.cfReport.BasicOnly) cptr = "basic-only";
      }
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
                 "!AZ:!UL (!AZ) !3ZL(!3ZL) \"!AZ\"",
                 SourceModule, SourceLine, cptr,
                 StatusCode, OriginalStatusCode, ExplanationPtr);
   }

   if (rqptr->rqPathSet.ReportType == ERROR_REPORT_TUNNEL ||
       rqptr->ServicePtr->ProxyTunnel == PROXY_TUNNEL_FIREWALL ||
       rqptr->ServicePtr->ProxyTunnel == PROXY_TUNNEL_RAW)
   {
      /******************/
      /* tunnel message */
      /******************/

      va_count (argcnt);
      if (argcnt <= 4)
         cptr = "!UL !AZ - !AZ\r\n";
      else
         cptr = "!UL !AZ - !&@\r\n";

      vecptr = FaoVector;

      *vecptr++ = StatusCode;
      *vecptr++ = HttpStatusCodeText(StatusCode);
      *vecptr++ = ExplanationPtr;
      /* add any variable length arguments to the vector */
      va_start (argptr, ExplanationPtr);
      for (argcnt -= 4; argcnt; argcnt--)
         *vecptr++ = va_arg (argptr, ulong);
      va_end (argptr);

      status = FaolToBuffer (Buffer, sizeof(Buffer), &Length, cptr, &FaoVector);
      if (VMSnok (status) || status == SS$_BUFFEROVF)
         ErrorExitVmsStatus (status, ErrorServerFao, FI_LI);

      Buffer[rqptr->rqResponse.ErrorReportLength = Length] = '\0';
      rqptr->rqResponse.ErrorReportPtr = VmGetHeap (rqptr, Length+1);
      memcpy (rqptr->rqResponse.ErrorReportPtr, Buffer, Length+1);

      return;
   }

   if (rqptr->rqResponse.ErrorReportByRedirect)
   {
      /*************************/
      /* by-redirect available */
      /*************************/

      if (rqptr->ServicePtr->ErrorReportPathCodesCount)
      {
         /* check for handled error code */
         StandardReport = true;
         for (idx = 0;
              idx < rqptr->ServicePtr->ErrorReportPathCodesCount;
              idx++)
         {
            if (rqptr->ServicePtr->ErrorReportPathCodes[idx] >= 0)
            {
               /* included HTTP status code */
               if (rqptr->ServicePtr->ErrorReportPathCodes[idx] == StatusCode)
               {
                  StandardReport = false;
                  break;
               }
            }
            else
            {
               /* excluded HTTP status code */
               StandardReport = false;
               if (rqptr->ServicePtr->ErrorReportPathCodes[idx] == -StatusCode)
               {
                  StandardReport = true;
                  break;
               }
            }
         }
         if (StandardReport)
         {
            /* use standard error reporting */
            rqptr->rqResponse.ErrorReportByRedirect = false;
            rqptr->RedirectErrorStatusCode = 0;
            rqptr->RedirectErrorAuthRealmDescrPtr = NULL;
         }
      }

      if (rqptr->rqResponse.ErrorReportByRedirect)
      {
         /******************************************/
         /* redirect report, generate query string */
         /******************************************/

         va_count (argcnt);
         if (argcnt <= 4)
            cptr = "<p>!AZ";
         else
            cptr = "<p>!&@";

         vecptr = FaoVector;
         *vecptr++ = ExplanationPtr;
         /* add any variable length arguments to the vector */
         va_start (argptr, ExplanationPtr);
         for (argcnt -= 4; argcnt; argcnt--)
            *vecptr++ = va_arg (argptr, ulong);
         va_end (argptr);

         status = FaolToBuffer (Buffer, sizeof(Buffer), &Length, cptr, &FaoVector);
         if (VMSnok (status) || status == SS$_BUFFEROVF)
            ErrorExitVmsStatus (status, ErrorServerFao, FI_LI);

         ErrorRedirectQueryString (rqptr, 0, Buffer, "",
                                   SourceModule, SourceLine);
         return;
      }
   }

   /*******************/
   /* generate report */
   /*******************/

   va_count (argcnt);

   vecptr = FaoVector;
   if (rqptr->rqPathSet.ReportType == ERROR_REPORT_DETAILED)
      *vecptr++ = ErrorSourceInfo (SourceModule, SourceLine);
   else
   if (rqptr->rqPathSet.ReportType == ERROR_REPORT_BASIC ||
       Config.cfReport.BasicOnly)
      *vecptr++ = "";
   else
      *vecptr++ = ErrorSourceInfo (SourceModule, SourceLine);
   *vecptr++ = CategoryPtr = MsgFor(rqptr,MSG_STATUS_ERROR);
   *vecptr++ = StatusCode;
   *vecptr++ = HttpStatusCodeText(StatusCode);

   ErrorReportHeader (rqptr, &vecptr);

   *vecptr++ = CategoryPtr;
   *vecptr++ = StatusCode;
   *vecptr++ = HttpStatusCodeExplanation (rqptr,StatusCode);

   ErrorReportEndHeader (rqptr, &vecptr);

   if (rqptr->rqPathSet.ReportType != ERROR_REPORT_DETAILED &&
       (rqptr->rqPathSet.ReportType == ERROR_REPORT_BASIC ||
        Config.cfReport.BasicOnly))
      *vecptr++ = "";
   else 
   if (argcnt <= 4)
   {
      *vecptr++ = "<p>!AZ";
      *vecptr++ = ExplanationPtr;
   }
   else 
   {
      *vecptr++ = "<p>!&@";
      *vecptr++ = ExplanationPtr;
      /* add any variable length arguments to the vector */
      va_start (argptr, ExplanationPtr);
      for (argcnt -= 4; argcnt; argcnt--)
         *vecptr++ = va_arg (argptr, ulong);
      va_end (argptr);
   }
   *vecptr++ = MsgFor(rqptr,MSG_STATUS_INFO);

   ErrorReportFooter (rqptr, &vecptr);

   ReportPtr = MsgFor(rqptr,MSG_STATUS_REPORT_FORMAT);
   status = FaolToBuffer (Buffer, sizeof(Buffer), &Length, ReportPtr, &FaoVector);
   if (VMSnok (status) || status == SS$_BUFFEROVF)
      ErrorExitVmsStatus (status, ErrorServerFao, FI_LI);

   Buffer[rqptr->rqResponse.ErrorReportLength = Length] = '\0';
   rqptr->rqResponse.ErrorReportPtr = VmGetHeap (rqptr, Length+1);
   memcpy (rqptr->rqResponse.ErrorReportPtr, Buffer, Length+1);
}

/*****************************************************************************/
/*
Generate an error message about a VMS status problem for subsequent reporting
to the client.  Generally most errors are reported as documents in their own
right, making the full HTML document desirable.  If reported part-way through
some action the redundant tags should be ignored anyway.  If error redirection
applies to the request then generate the error information in a form suitable
for provision as a request query-string.
*/

void ErrorVmsStatus
(
REQUEST_STRUCT *rqptr,
int StatusValue,
char *SourceModule,
int SourceLine
)
{
   BOOL  StandardReport;
   int  idx, status,
        OriginalStatusCode,
        StatusCode;
   ushort  Length;
   ushort  ShortLength;
   ulong  *vecptr;
   ulong  FaoVector [32];
   char  *cptr, *sptr, *zptr,
         *CategoryPtr,
         *ContentTypePtr,
         *ExplanationPtr,
         *RecommendPtr,
         *ReportPtr;
   char  Message [256],
         Buffer [4096];
   $DESCRIPTOR (MessageDsc, Message);

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER, "ErrorVmsStatus()");
                                 
   /* don't overwrite any existing error message */
   if (ERROR_REPORTED (rqptr)) return;

   /* not if WebDAV is involved */
   if (rqptr->WebDavRequest || rqptr->WhiffOfWebDav || rqptr->WebDavTaskPtr)
      rqptr->rqResponse.ErrorReportByRedirect = false;

   /* if this request is processing an error report redirect */
   if (rqptr->rqResponse.ErrorReportByRedirect &&
       rqptr->RedirectErrorStatusCode)
   {
      /* problem processing the error report, no infinite loops here please! */
      rqptr->rqResponse.ErrorReportByRedirect = false;
      rqptr->RedirectErrorStatusCode = 0;
      rqptr->RedirectErrorAuthRealmDescrPtr = NULL;
   }

   if (rqptr->rqResponse.HeaderGenerated &&
       STR_DSC_PTR(&rqptr->NetWriteBufferDsc))
   {
      /* some output has already occured, internal error reporting */
      rqptr->rqResponse.ErrorReportByRedirect = false;
   }

   OriginalStatusCode = rqptr->rqResponse.HttpStatus;

   /* if this error occured after header generation turn it into an error! */
   if (rqptr->rqResponse.HttpStatus == 200) rqptr->rqResponse.HttpStatus = 0;
   if (!rqptr->rqResponse.HttpStatus)
   {
      if (StatusValue == RMS$_FNF ||
          StatusValue == RMS$_DNF ||
          StatusValue == RMS$_DEV ||
          StatusValue == SS$_NOSUCHDEV ||
          StatusValue == SS$_NOSUCHFILE)
          rqptr->rqResponse.HttpStatus = 404;
      else
      if (StatusValue == RMS$_SYN ||
          StatusValue == RMS$_FNM ||
          StatusValue == RMS$_TYP ||
          StatusValue == RMS$_VER ||
          StatusValue == RMS$_DIR ||
          StatusValue == SS$_BADFILENAME ||
          StatusValue == SS$_BADFILEVER ||
          StatusValue == SS$_BADIRECTORY)
         rqptr->rqResponse.HttpStatus = 403;
      else
      if (StatusValue == RMS$_FLK ||
          StatusValue == SS$_DIRNOTEMPTY ||
          StatusValue == SS$_EXDISKQUOTA)
         rqptr->rqResponse.HttpStatus = 409;
      else
      if (StatusValue == RMS$_PRV ||
          StatusValue == SS$_NOPRIV)
         rqptr->rqResponse.HttpStatus = 403;
      else
         rqptr->rqResponse.HttpStatus = 500;
   }

   /* when mapping error status by implication its a basic report! */
   if ((rqptr->rqResponse.HttpStatus == 400 ||
        rqptr->rqResponse.HttpStatus == 409) &&
       rqptr->rqPathSet.Report400as)
   {
      rqptr->rqResponse.HttpStatus = rqptr->rqPathSet.Report400as;
      rqptr->rqPathSet.ReportType = ERROR_REPORT_BASIC;
   }
   else
   if ((rqptr->rqResponse.HttpStatus == 403 ||
        rqptr->rqResponse.HttpStatus == 409) &&
       rqptr->rqPathSet.Report403as)
   {
      rqptr->rqResponse.HttpStatus = rqptr->rqPathSet.Report403as;
      rqptr->rqPathSet.ReportType = ERROR_REPORT_BASIC;
   }
   else
   if ((rqptr->rqResponse.HttpStatus == 404 ||
        rqptr->rqResponse.HttpStatus == 409) &&
       rqptr->rqPathSet.Report404as)
   {
      rqptr->rqResponse.HttpStatus = rqptr->rqPathSet.Report404as;
      rqptr->rqPathSet.ReportType = ERROR_REPORT_BASIC;
   }

   StatusCode = rqptr->rqResponse.HttpStatus;

   if (StatusCode == 401 || StatusCode == 407) ResponseHeaderChallenge (rqptr);

   if (rqptr->rqContentInfo.ContentTypePtr)
      ContentTypePtr = rqptr->rqContentInfo.ContentTypePtr;
   else
      ContentTypePtr = "";

   if (StatusValue == RMS$_FNF)
   {
      if (ConfigSameContentType (ContentTypePtr, "text/", 5))
         ExplanationPtr = MsgFor(rqptr,MSG_STATUS_DOC_NOT_FOUND);
      else
         ExplanationPtr = MsgFor(rqptr,MSG_STATUS_FILE_NOT_FOUND);
   }
   else
   if (StatusValue == RMS$_PRV ||
       StatusValue == SS$_NOPRIV)
   {
      if (ConfigSameContentType (ContentTypePtr, "text/", 5))
         ExplanationPtr = MsgFor(rqptr,MSG_STATUS_DOC_PROTECTION);
      else
         ExplanationPtr = MsgFor(rqptr,MSG_STATUS_FILE_PROTECTION);
   }
   else
   if (VMSok (status =
       sys$getmsg (StatusValue, &ShortLength, &MessageDsc, 1, 0)))
   {
      Message[ShortLength] = '\0';
      cptr = sptr = ExplanationPtr = Message;
      *cptr = TOUP(*cptr);

      /* don't allow access violation to be confused with forbidden access! */
      if (StatusValue == SS$_ACCVIO)
      {
         while (*cptr && *cptr != ',') cptr++;
         if (*cptr) strcpy (cptr, " (segmentation fault)");
         cptr = sptr;
      }

      /* improve the look by removing any embedded sys$fao() formatting */
      while (*cptr)
      {
         if (*cptr == '!')
         {
            cptr++;
            *sptr++ = '?';
            /* step over any field width digits */
            while (isdigit(*cptr)) cptr++;
            /* usually two formatting characters */
            if (isalpha(*cptr)) cptr++;
            if (isalpha(*cptr)) cptr++;
         }
         else
            *sptr++ = *cptr++;
      }
      *sptr = '\0';
   }
   else
      strcpy (Message, "*ERROR* sys$getmsg()");

   if (!rqptr->rqResponse.ErrorTextPtr)
   {
      if (rqptr->rqHeader.PathInfoPtr)
         rqptr->rqResponse.ErrorTextPtr = rqptr->rqHeader.PathInfoPtr;
      else
         rqptr->rqResponse.ErrorTextPtr =
            MsgFor(rqptr, MSG_STATUS_NO_INFORMATION);
   }

   if (!rqptr->rqResponse.ErrorOtherTextPtr ||
       !Config.cfReport.MetaInfoEnabled)
      rqptr->rqResponse.ErrorOtherTextPtr =
         MsgFor(rqptr,MSG_STATUS_NO_INFORMATION);

   RecommendPtr = NULL;
   if (!Config.cfReport.ErrorRecommend ||
       StatusCode == 401 ||
       StatusCode == 407)
      RecommendPtr = "";
   else
   if (StatusValue == RMS$_FNF ||
       StatusValue == RMS$_DNF ||
       StatusValue == SS$_NOSUCHFILE)
      RecommendPtr = MsgFor(rqptr,MSG_STATUS_ADVISE_NOSUCHFILE);
   else
   if (StatusValue == RMS$_PRV)
      RecommendPtr = MsgFor(rqptr,MSG_STATUS_ADVISE_PRV);
   else
   if (StatusValue == SS$_NOPRIV)
      RecommendPtr = MsgFor(rqptr,MSG_STATUS_ADVISE_NOPRIV);
   else
   if (StatusValue == RMS$_SYN ||
       StatusValue == RMS$_DEV ||
       StatusValue == RMS$_DIR ||
       StatusValue == RMS$_FNM ||
       StatusValue == RMS$_TYP)
      RecommendPtr = MsgFor(rqptr,MSG_STATUS_ADVISE_SYNTAX);
   else
   if (StatusValue == RMS$_FLK)
      RecommendPtr = MsgFor(rqptr,MSG_STATUS_ADVISE_FLK);
   else
   if (StatusValue == SS$_DIRNOTEMPTY ||
       StatusValue == SS$_EXDISKQUOTA)
      RecommendPtr = MsgFor(rqptr,MSG_STATUS_ADVISE_CORRECT);

   if (!RecommendPtr) RecommendPtr = "";

   if (WATCHING (rqptr, WATCH_ERROR))
   {
      cptr = "default";
      switch (rqptr->rqPathSet.ReportType)
      {
         case ERROR_REPORT_BASIC : cptr = "basic"; break;
         case ERROR_REPORT_DETAILED : cptr = "detailed"; break;
         case ERROR_REPORT_TUNNEL : cptr = "tunnel"; break;
         default: if (Config.cfReport.BasicOnly) cptr = "basic-only";
      }
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
                 "!AZ:!UL (!AZ) !3ZL(!3ZL) !&S \"!AZ\" \"!AZ\" \"!AZ\"",
                 SourceModule, SourceLine, cptr,
                 StatusCode, OriginalStatusCode, StatusValue,
                 ExplanationPtr, rqptr->rqResponse.ErrorTextPtr,
                 rqptr->rqResponse.ErrorOtherTextPtr);
   }

   if (rqptr->rqPathSet.ReportType == ERROR_REPORT_TUNNEL ||
       rqptr->ServicePtr->ProxyTunnel == PROXY_TUNNEL_FIREWALL ||
       rqptr->ServicePtr->ProxyTunnel == PROXY_TUNNEL_RAW)
   {
      /******************/
      /* tunnel message */
      /******************/

      vecptr = FaoVector;
      *vecptr++ = StatusCode;
      *vecptr++ = HttpStatusCodeText(StatusCode);
      *vecptr++ = ExplanationPtr;
      *vecptr++ = rqptr->rqResponse.ErrorTextPtr;

      status = FaolToBuffer (Buffer, sizeof(Buffer), &Length,
                          "!UL !AZ - !AZ ... !AZ\r\n", &FaoVector);
      if (VMSnok (status) || status == SS$_BUFFEROVF)
         ErrorExitVmsStatus (status, ErrorServerFao, FI_LI);

      Buffer[rqptr->rqResponse.ErrorReportLength = Length] = '\0';
      rqptr->rqResponse.ErrorReportPtr = VmGetHeap (rqptr, Length+1);
      memcpy (rqptr->rqResponse.ErrorReportPtr, Buffer, Length+1);

      return;
   }

   if (rqptr->rqResponse.ErrorReportByRedirect)
   {
      /*************************/
      /* by-redirect available */
      /*************************/

      if (rqptr->ServicePtr->ErrorReportPathCodesCount)
      {
         /* check for handled error code */
         StandardReport = true;
         for (idx = 0;
              idx < rqptr->ServicePtr->ErrorReportPathCodesCount;
              idx++)
         {
            if (rqptr->ServicePtr->ErrorReportPathCodes[idx] >= 0)
            {
               /* included HTTP status code */
               if (rqptr->ServicePtr->ErrorReportPathCodes[idx] == StatusCode)
               {
                  StandardReport = false;
                  break;
               }
            }
            else
            {
               /* excluded HTTP status code */
               StandardReport = false;
               if (rqptr->ServicePtr->ErrorReportPathCodes[idx] == -StatusCode)
               {
                  StandardReport = true;
                  break;
               }
            }
         }
         if (StandardReport)
         {
            /* use standard error reporting */
            rqptr->rqResponse.ErrorReportByRedirect = false;
            rqptr->RedirectErrorStatusCode = 0;
            rqptr->RedirectErrorAuthRealmDescrPtr = NULL;
         }
      }

      if (rqptr->rqResponse.ErrorReportByRedirect)
      {
         /******************************************/
         /* redirect report, generate query string */
         /******************************************/

         ErrorRedirectQueryString (rqptr, StatusValue,
                                   ExplanationPtr, RecommendPtr,
                                   SourceModule, SourceLine);
         return;
      }
   }

   /*******************/
   /* generate report */
   /*******************/

   vecptr = FaoVector;
   if (rqptr->rqPathSet.ReportType == ERROR_REPORT_DETAILED)
      *vecptr++ = ErrorSourceInfo (SourceModule, SourceLine);
   else 
   if (rqptr->rqPathSet.ReportType == ERROR_REPORT_BASIC ||
       Config.cfReport.BasicOnly)
      *vecptr++ = "";
   else 
      *vecptr++ = ErrorSourceInfo (SourceModule, SourceLine);
   *vecptr++ = CategoryPtr = MsgFor(rqptr,MSG_STATUS_ERROR);
   *vecptr++ = StatusCode;
   *vecptr++ = HttpStatusCodeText(StatusCode);

   ErrorReportHeader (rqptr, &vecptr);

   *vecptr++ = CategoryPtr;
   *vecptr++ = StatusCode;
   *vecptr++ = HttpStatusCodeExplanation (rqptr,StatusCode);

   ErrorReportEndHeader (rqptr, &vecptr);

   if (rqptr->rqPathSet.ReportType != ERROR_REPORT_DETAILED &&
       (rqptr->rqPathSet.ReportType == ERROR_REPORT_BASIC ||
        Config.cfReport.BasicOnly))
      *vecptr++ = "";
   else
   {
      *vecptr++ =
"<p>!AZ &nbsp;...&nbsp; !&@\n\
<!!-- sts: %X!XL \"!&;AZ\" -->\n\
!AZ";
      *vecptr++ = ExplanationPtr;
      /* if it looks like it starts with a path forward-slash */
      if (rqptr->rqResponse.ErrorTextPtr[0] == '/')
         *vecptr++ = "!&;&_AZ";
      else
         *vecptr++ = "!AZ";
      *vecptr++ = rqptr->rqResponse.ErrorTextPtr;
      *vecptr++ = StatusValue;
      *vecptr++ = rqptr->rqResponse.ErrorOtherTextPtr;
      *vecptr++ = RecommendPtr;
   }
   *vecptr++ = MsgFor(rqptr,MSG_STATUS_INFO);

   ErrorReportFooter (rqptr, &vecptr);

   ReportPtr = MsgFor(rqptr,MSG_STATUS_REPORT_FORMAT);
   status = FaolToBuffer (Buffer, sizeof(Buffer), &Length, ReportPtr, &FaoVector);
   if (VMSnok (status) || status == SS$_BUFFEROVF)
      ErrorNoticed (rqptr, status, NULL, FI_LI);

   Buffer[rqptr->rqResponse.ErrorReportLength = Length] = '\0';
   rqptr->rqResponse.ErrorReportPtr = VmGetHeap (rqptr, Length+1);
   memcpy (rqptr->rqResponse.ErrorReportPtr, Buffer, Length+1);
}

/*****************************************************************************/
/*
Internal redirect error report, generate a query string containing all the
information required to generate an error report.
*/

void ErrorRedirectQueryString
(
REQUEST_STRUCT *rqptr,
int StatusValue,
char *ExplanationPtr,
char *RecommendPtr,
char *SourceModule,
int SourceLine
)
{
   int  status;
   ushort  Length;
   ulong  *vecptr;
   ulong  FaoVector [32];
   char  Buffer [16384];

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER,
                 "ErrorRedirectQueryString()");

   vecptr = FaoVector;
   *vecptr++ = rqptr->rqPathSet.ReportType != ERROR_REPORT_DETAILED &&
               (rqptr->rqPathSet.ReportType == ERROR_REPORT_BASIC ||
                Config.cfReport.BasicOnly) ? "basic" : "detailed",
   *vecptr++ = rqptr->rqResponse.HttpStatus;
   *vecptr++ = HttpStatusCodeText(rqptr->rqResponse.HttpStatus);
   *vecptr++ = HttpStatusCodeExplanation(rqptr,rqptr->rqResponse.HttpStatus);
   /* '_vms' is empty to indicate no VMS status code */
   if (StatusValue)
   {
      *vecptr++ = "!UL";
      *vecptr++ = StatusValue;
   }
   else
      *vecptr++ = "";
   *vecptr++ = rqptr->rqHeader.RequestUriPtr;
   if (rqptr->rqResponse.ErrorTextPtr)
      *vecptr++ = rqptr->rqResponse.ErrorTextPtr;
   else
      *vecptr++ = "";
   if (rqptr->rqResponse.ErrorOtherTextPtr)
      *vecptr++ = rqptr->rqResponse.ErrorOtherTextPtr;
   else
      *vecptr++ = "";
   *vecptr++ = ExplanationPtr;
   *vecptr++ = RecommendPtr;
   *vecptr++ = SourceModule;
   *vecptr++ = SourceLine;
   *vecptr++ = rqptr->ServicePtr->ServerHostPort;

   if (rqptr->rqResponse.HttpStatus == 401 ||
       rqptr->rqResponse.HttpStatus == 407)
   {
      *vecptr++ = "&error_realm=!AZ!&@!&@";
      *vecptr++ = rqptr->rqAuth.RealmPtr;
      if (rqptr->rqAuth.BasicChallengePtr &&
          rqptr->rqAuth.BasicChallengePtr[0])
      {
         *vecptr++ = "&error_basic=!&%AZ";
         *vecptr++ = rqptr->rqAuth.BasicChallengePtr;
      }
      else
         *vecptr++ = "";
      if (rqptr->rqAuth.DigestChallengePtr &&
          rqptr->rqAuth.DigestChallengePtr[0])
      {
         *vecptr++ = "&error_digest=!&%AZ";
         *vecptr++ = rqptr->rqAuth.DigestChallengePtr;
      }
      else
         *vecptr++ = "";
   }
   else
      *vecptr++ = "";

   status = FaolToBuffer (Buffer, sizeof(Buffer), &Length,
"?httpd=error\
&error_type=!AZ\
&error_status=!UL\
&error_status_text=!&%AZ\
&error_status_explanation=!&%AZ\
&error_vms=!&@\
&error_uri=!&%AZ\
&error_about=!&%AZ\
&error_about2=!&%AZ\
&error_text=!&%AZ\
&error_text2=!&%AZ\
&error_module=!&%AZ\
&error_line=!UL\
&error_proxy=!&%AZ\
!&@",
      &FaoVector);

   if (VMSnok (status) || status == SS$_BUFFEROVF)
      ErrorNoticed (rqptr, status, NULL, FI_LI);

   rqptr->rqResponse.ErrorReportLength = Length;
   rqptr->rqResponse.ErrorReportPtr = VmGetHeap (rqptr, Length+1);
   memcpy (rqptr->rqResponse.ErrorReportPtr, Buffer, Length+1);
}

/*****************************************************************************/
/*
Report general string overflow.
*/

void ErrorGeneralOverflow
(
REQUEST_STRUCT *rqptr,
char *SourceModule,
int SourceLine
)
{
   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER, "ErrorGeneralOverflow()");

   if (!rqptr->rqResponse.HttpStatus) rqptr->rqResponse.HttpStatus = 500;
   ErrorGeneral (rqptr, MsgFor(rqptr,MSG_GENERAL_OVERFLOW),
                 SourceModule, SourceLine);
}

/*****************************************************************************/
/*
*/
 
void ErrorInternal
(
REQUEST_STRUCT *rqptr,
int StatusValue,
char *ExplanationPtr,
char *SourceModule,
int SourceLine
)
{
   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD__OTHER))
      WatchThis (WATCHITM(rqptr), WATCH_MOD__OTHER, "ErrorInternal()");

   rqptr->rqResponse.HttpStatus = 500;

   if (ExplanationPtr)
      ErrorGeneral (rqptr, ExplanationPtr, SourceModule, SourceLine);
   else
      ErrorVmsStatus (rqptr, StatusValue, SourceModule, SourceLine);
}

/*****************************************************************************/
/*
If the server is configured for it return a pointer to some META information
containing softwareID, source code module and line in which the error occured,
otherwise pointer to an empty string.  This function is not used reentrantly
and so provided the contents of the static buffer are used before it is
recalled it will continue to work. The source file format provided by the
"__FILE__" macro will be "device:[directory]name.type;ver".  Reduce that to
"name".  The "__LINE__" macro just provides an integer.
*/
 
char* ErrorSourceInfo
(
char *SourceModule,
int SourceLine
) 
{
   static char  Buffer [256];

   int  status;
 
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD__OTHER))
      WatchThis (WATCHALL, WATCH_MOD__OTHER, "ErrorSourceInfo()");

   if (!Config.cfReport.MetaInfoEnabled) return ("");

   status = FaoToBuffer (Buffer, sizeof(Buffer), NULL,
"<meta name=\"generator\" content=\"!AZ\">\n\
<meta name=\"module\" content=\"!AZ\">\n\
<meta name=\"line\" content=\"!UL\">\n",
      SoftwareID, SourceModule, SourceLine);

   if (VMSnok (status) || status == SS$_BUFFEROVF)
      ErrorExitVmsStatus (status, ErrorServerFao, FI_LI);

   return (Buffer);
}
 
/****************************************************************************/