[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]
[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]
/*****************************************************************************/
/*
                                 File.c

This module implements a full multi-threaded, AST-driven, asynchronous file 
send.  The AST-driven nature makes the code a little more difficult to follow, 
but creates a powerful, event-driven, multi-threaded server.  All of the 
necessary functions implementing this module are designed to be non-blocking. 

It can operate in one of four modes.

1) File direct to network.
   ~~~~~~~~~~~~~~~~~~~~~~
A smallish, standard output buffer is allocated if variable length record file,
successive records are read into the buffer until it fills, then written to the
network as a single block.  Records always have a newline character added to
each record (variable length record files are invariably text).  If a stream,
fixed or undefined record format the buffer is filled in a single virtual block
read and then immediately written to the network.

It uses the same buffer space as, and interworks with, NetWriteBuffered().  If 
there is already data (text) buffered in this area the file module will, for 
record-oriented, non-HTML-escaped transfers, continue to fill the area (using 
its own buffering function), flushing when and if necessary.  At end-of-file 
explicitly flush the buffer only if escaping HTML-forbidden characters, 
otherwise allow subsequent processing to do it as necessary.  For block-mode 
files the buffer is explicitly flushed before commencing the file transfer. 

2) File into file cache buffer, simultaneously to network.
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A file cache buffer of sufficient size to contain the whole file is allocated. 
If reading in record mode this is filled with successive reads (as per above)
until a chunk the size of a standard output buffer is filled, at which time it
is written to the network as a block.  In block mode each group of blocks read
is output to the network.

3) File into contents buffer.
   ~~~~~~~~~~~~~~~~~~~~~~~~~
A file contents buffer of sufficient size to contain the whole file is
allocated.  When reading in either record or block mode the buffer is just
filled with the file reads - NO NETWORK OUTPUT.  When complete the request gets
control of the contents buffer for subsequent processing.

4) File into file cache as well as into contents buffer.
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Both file cache and contents buffers of sufficient size are allocated.  Records
and blocks fill both buffers during the reads - NO NETWORK OUTPUT.  When
complete the file is cached and the contents buffer filled.  When complete the
request gets control of the contents buffer for subsequent processing. 
(Subsequent access to the file can then be serviced from the file cache.)


BYTE RANGES
-----------
The "Range:" request header is supported for non variable length record files
(i.e. those that can be considered to contain 'binary' content, e.g. stream-LF,
stream-CR, stream, fixed, undefined).  The cache module also supports
byte-ranges for cached file content (though without the restriction - all cache
content is 'binary' in nature).  The majority of files that byte ranges might
be applied to are probably large and having 'binary' content (e.g. restarting
ZIP archive transfers, accessing 'linearized' PDF documents, etc.)  The $READs
of virtual blocks used to access these files allow a relatively simple
algorithm to access these ranges and return 206 (partial content) responses. 
No effort is made to support this for variable record length files.  If a
byte-range is invalid or cannot be applied to the particular file type it is
just ignored and a 200 full transfer is performed instead.


MD5 DIGEST
----------
An MD5 digest (16 byte hash) is used to uniquely identify cached files.  The
hash is generated either from a mapped path or from the file name.  See the
CACHE.C module for further detail.


FILE LANGUAGE VARIANTS
----------------------
If the path is SET to ACCEPT=LANG then this module attempts to find
language-specific variants of the file.  The format of the file names for these
variants is <file-name>.<file-type>_<language> where 'language' is one of the
ISO language abbreviations, e.g. "en" for English, "fr" for French, "de" for
German, "ru" for Russian, etc.  Hence if the basic file name is EXAMPLE.HTML
then a specifically English version would be EXAMPLE.HTML_EN, a French version
EXAMPLE.HTML_FR, etc.  Language variants may be provided for any file type the
WASD_CONFIG_GLOBAL directive [AddType] specifies as content-type "text/..".

The language variant code behaves as follows.  When FileBegin() is called with
a path SET to ACCEPT=LANG and a default language is specified (for those files
without the language variant abbreviation) this is checked against the
request's accepted languages to see if the default would be the request's first
choice.  If so then there is no need for further accept language processing. 
If not then a series of functions search using the basic specification for
files matching "EXAMPLE.HTML_*".  All files matching this wildcard have the '*'
portion (e.g. "EN", "FR", "DE", "RU") added to a list of matching variants. 
When the search is complete a final function compares each of the request's
"Accept-Language:" list to each of the searched-for variants.  If one matches
the contents of that file are returned.  If none are matched the original
EXAMPLE.HTML would be returned.

Example behaviour.  A directory contains

  EXAMPLE.HTML
  EXAMPLE.HTML_FR
  EXAMPLE.HTML_DE
  EXAMPLE.HTML_RU

and a request specifies

  Accept-Language: en,fr,de

then the EXAMPLE.HTML_FR file will be returned.  For a request specifying

  Accept-Language: ru

then the EXAMPLE.HTML_RU file is returned, and if

  Accept-Language: en

EXAMPLE.HTML returned (without having searched for any other variants).

One or other file is always returned, with the default, non-language file
always the fallback source of data.  If it does not exist and no other
language variant is selected the request returns a 404 file-not-found error.


IGNORED CONTENT-TYPE
--------------------
Some browsers and/or some operating systems and/or some versions of both insist
on ignoring the response header specified content-type and instead seem to
second-guess (wrongly) on the file name extension.  A common example is the
content of DCL procedures on Windows and up-until-fairly-recent versions of
Internet Explorer.  I note that versions 9 and 10 seem more compliant with the
the returned header.  Notwithstanding, if a '$' and then a second extension is
added to the URI this is often sufficient to coerce the browser into accepting
(and display) the content with the type associated with that extension.  In the
case of the DCL procedure /wasd_root/src/build_all.com making the URI into
/wasd_root/src/build_all.com$.txt, etc.  Any suitable additional file extension
that will be accepted can be used.


GENERAL COMMENTS
----------------
Uses the ACP-QIO interface detailed in the "OpenVMS I/O User's Reference
Manual" to retrieve file record attributes, revision date/time and size.

Use QIOs to access and transfer disk blocks (just a bit more efficient and
flexible than RMS).  RMS structures are used to parse the file name and obtain
the DID and FID uses to QIO access the file.  Some record types are considered
binary content and can be served without 'massaging' the data.  These are
STREAM (CR,LF), FIXED-512 (or where the record size falls on a block boundary),
and UNDEFINED.  Those record types that have an internal structure and must be
'massaged' to get the data into the stream format the Web is so fond of have
functions for doing just that.  These are VARIABLE, VFC and
FIXED-non-span-non-512 (where the record size is not on a block boundary and is
not allowed to span block boundaries - who the hell uses this stuff anyway?!) 
VARIABLE and VFC record formats have newline carriage control added to records.

When not buffering to cache or contents the module can encapsulate plain-text
and escape HTML-forbidden characters.

Works in conjunction with the CACHE module.  A file read can be simultaneously
used to send the data to the client and load a cache buffer. The request
structure fields 'CacheContentPtr' and 'CacheContentCount' being used in both
block I/O and record mode access to track through the available buffer space,
for this located in the cache structure. For non-cache-load reads uses the
standard buffer space pointed to by 'OutputBufferPtr' and for record mode
access tracked using 'OutputBufferCurrentPtr' and 'OutputBufferRemaining'
fields.

Implements defacto HTTP/1.0 persistent connections.  Only provide "keep-alive"
response if we're sending a file that has 'binary content' (e.g. stream, fixed
512) and know its precise length.  An accurate "Content-Length:" field is vital
for the client's correct reception of the transmitted data.  Currently the only
other time a "keep-alive" response can be provided is when a "304 not-modified"
header is returned (it has a content length of precisely zero!).  October 1997:
noted that Netscape Navigator 3.n seems to pay no attention to a
"Content-Length: 0" with a "Keep-Alive:" connection, it just sits there waiting
until the keep-alive time closes the connection, after which it reports
"document contains no data". (IE 3.02 seems to behave correctly ;^)  I have
therefore disabled persistent connections for zero-length files.


VERSION HISTORY
---------------
29-JUN-2021  MGD  bugfix; FileBegin() ERROR_REPORTED() free file task
20-MAR-2021  MGD  FILE_VAR_ASIS provides exactly what is on the disk
12-NOV-2020  MGD  file ->SizeInBytes64 now 64 bit datum
04-JAN-2019  MGD  bugfix; AuthCompleted()
13-AUG-2018  MGD  FileVariableRecord() implement SET mapping carriage control
02-MAY-2016  MGD  bugfix; FileParseAst() regression with search list file
21-JUN-2014  MGD  bugfix; FileParseAst() allow for non-dir .DIR files
21-SEP-2013  MGD  FileAcpInfoAst() '$.' file extension kludge
07-SEP-2013  MGD  bugfix; ensure "?httpd=content&type=" is URL-decoded
01-JUN-2013  MGD  bugfix; non-ODS_EXTENDED platforms must OdsParse()
                    NAM$M_NOCONCEAL before OdsSearchNoConceal()
30-JAN-2012  MGD  bugfix; FileAcpInfoAst() SS$_BADPARAM >2GB <4GB (per JPP)
06-NOV-2010  MGD  FileInfoAcpAst() if directory listing README ignore NOPRIV
23-MAY-2010  MGD  FileNextBlocks() change QIO file size from long to quad
                  to cater for files greater than 4GB (4GB+ is limited to
                  file serving only, no ranges, etc.)
05-OCT-2009  MGD  use OutputFileBufferSize to maximise file transfer
19-AUG-2009  MGD  bugfix; FileAcpInfoAst() byte-range limit negative offset
11-JUN-2009  MGD  "*.*__WASDAV;" reports not found
09-JUN-2007  MGD  use STR_DSC
                  FileGenerateEntityTag()
20-OCT-2005  MGD  bugfix; FileNextBlocks() ensure VARiable record format
                  files have records read on word (even byte) boundaries
28-MAY-2005  MGD  bugfix; if none-match entity and IfModifiedSince() logic
29-NOV-2004  MGD  bugfix; FileVariableRecord() memset only if positive
19-SEP-2004  MGD  bugfix; even number of bytes on a disk $QIO READVBLK
20-JUL-2004  MGD  HTTP/1.1 compliance,
                  HttpIfUnModifiedSince() check,
                  calculate content-length for byte range response header
26-APR-2004  MGD  major changes to eliminate RMS from file access
                  (WASD's doing all the content conversion work anyway!)
                  by using ACP/QIOs and massaging record content explicitly
                  (outgrowth of returns from 18-FEB-2004 changes)
18-FEB-2004  MGD  read variable record format files using block IO and then
                  explicitly process the those records to produce a stream-LF
                  block of data in their place!
                  (provides in excess of 400% throughput boost!!! :^)
                  bugfix; rare RECTOOBIG on variable record length file where
                  longest record exceeded 'OutputBufferSize' so initialize
                  buffer to maximum of 'OutputBufferSize' or file lrl (use
                  'rqptr->rqOutput.BufferSize' instead of 'OutputBufferSize')
21-AUG-2003  MGD  support byte-range requests on non-VAR files
12-AUG-2003  MGD  access to HTA or HTL file now reports not found
09-JUL-2003  MGD  rework for new caching requirements
16-JUN-2003  MGD  bugfix; FileSetCharset() following initial CacheSearch()
                  moved to CACHE.C module (ACCVIO if entry NULLed)
05-OCT-2002  MGD  no sneaky getting directory contents by downloading files!
                  refine VMS security profile usage
03-JUN-2002  MGD  bugfix; (well sort of) it would appear that after NO_CONCEAL
                  searching and a sys$open() you must sys$close() *before* the
                  SYNCHCK sys$parse() release of resources otherwise a channel
                  to the device is left assigned!!
27-APR-2002  MGD  make SYSPRV enabled ASTs asynchronous
19-MAR-2002  MGD  bugfix; OdsParse() for VMS authenticated request
15-MAR-2002  MGD  bugfix; FileNextRecordAst() VAR file into contents buffer
18-NOV-2001  MGD  FileAcceptLang..()
23-OCT-2001  MGD  bugfix; FileNextBlocksAst() 'ContentRemaining'
04-AUG-2001  MGD  modifications in line with changes in the handling
                  of file and cache (now MD5 hash based) processing,
                  block I/O complete if _rsz is less than _usz
                  support module WATCHing
10-MAY-2001  MGD  bugfix; FileNextRecordAst() buffer flush
05-MAR-2001  MGD  bugfix; FileNextBlocks() 32767 to 0xfe00 (65024)
29-DEC-2000  MGD  allow a file's contents to be read into a buffer
23-DEC-2000  MGD  allow access to an HTL if it is authorized
06-DEC-2000  MGD  make a search list DNF appear as a FNF
01-SEP-2000  MGD  add optional, local path authorization
                  (for calls from the likes of SSI.C)
09-JUN-2000  MGD  search-list processing refined
04-MAR-2000  MGD  use NetWriteInit(), et.al.
27-DEC-1999  MGD  support ODS-2 and ODS-5 using ODS module
10-OCT-1999  MGD  "scrunched" (in fact all) SSI files, prevent streamLFing
17-SEP-1999  MGD  bugfix; sys$parse() NAM$M_NOCONCEAL for search lists
23-MAY-1999  MGD  do not allow "?httpd=content" requests to be cached
05-FEB-1999  MGD  bugfix; FileNextRecord() zero '_usz'
07-NOV-1998  MGD  WATCH facility
19-SEP-1998  MGD  improve granularity of file open, connect, close, ACP
14-MAY-1998  MGD  request-specified content-type ("httpd=content&type=")
19-MAR-1998  MGD  buffer VBN and first free byte for use by cache module
19-JAN-1998  MGD  new NetWriteBuffered() and structures
07-JAN-1998  MGD  groan, bugfix; record processing for files > 4096 bytes
                  completely brain-dead ... sorry
22-NOV-1997  MGD  sigh, bugfix; heap corruption by file cache
05-OCT-1997  MGD  file cache,
                  keep-alive now not used if the content-length is zero
17-SEP-1997  MGD  if block-mode open is locked retry open in record-mode
17-AUG-1997  MGD  message database,
                  SYSUAF-authenticated users security-profile,
                  addressed potential problem with FIXed and odd-byte records
08-JUN-1997  MGD  if request "Pragma: no-cache" then always return 
27-FEB-1997  MGD  delete on close for "temporary" files
01-FEB-1997  MGD  HTTPd version 4
01-AUG-1996  MGD  Variable to stream-LF file conversion "on-the-fly"
12-APR-1996  MGD  determine read method (record or binary) from record format;
                  implemented persistent connections ("keep-alive")
01-DEC-1995  MGD  HTTPd version 3
27-SEP-1995  MGD  added If-Modified-Since: functionality;
                  changed carriage-control on records from <CR><LF> to single
                  <LF> ('\n' ... newline), to better comply with some browsers
                  (Netscape was spitting on X-bitmap files, for example!)
07-AUG-1995  MGD  ConfigcfReport.MetaInfoEnabled to allow physical file
                  specification included as commentary within an HTML file
13-JUL-1995  MGD  bugfix; occasionally a record was re-read after flushing
                  the records accumulated in the buffer NOT due to RMS$_RTB
20-DEC-1994  MGD  initial development for 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 <stdio.h>
#include <string.h>

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

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

#define WASD_MODULE "FILE"

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

char ErrorBufferFileMaxBytes [] =
  "Exceeds reasonable limit on the size of this type of file.";

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

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

extern BOOL  CacheEnabled,
             OdsExtended,
             WebDavEnabled;

extern int  EfnWait,
            EfnNoWait,
            HttpdTickSecond,
            OutputFileBufferSize;

extern int  ToLowerCase[],
            ToUpperCase[];

extern ulong  SysPrvMask[];

extern char  ConfigContentTypeSsi[],
             ConfigContentTypeUnknown[],
             ConfigDefaultFileContentType[],
             ErrorSanityCheck[],
             HttpProtocol[],
             SoftwareID[];

extern ACCOUNTING_STRUCT  *AccountingPtr;
extern CONFIG_STRUCT  Config;
extern MSG_STRUCT  Msgs;
extern WATCH_STRUCT  Watch;

/*****************************************************************************/
/*
Initalize the file task structure.
NOTE: FILE PATHS ARE ALWAYS AUTHORIZED UNLESS EXPLICITLY TURNED OFF.
*/ 
 
FILE_TASK* FileNewTask (REQUEST_STRUCT *rqptr)

{
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "FileNewTask()");

   if (tkptr = rqptr->FileTaskPtr)
      ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   rqptr->FileTaskPtr = tkptr =
      (FILE_TASK*)VmGetHeap (rqptr, sizeof(FILE_TASK));

   tkptr->AuthorizePath = true;

   return (tkptr);
}

/*****************************************************************************/
/*
Authorize the path to FileBegin() 'FileName' before accessing.
NOTE: FILE PATHS ARE ALWAYS AUTHORIZED UNLESS EXPLICITLY TURNED OFF.
*/ 
 
void FileSetAuthorizePath
(
REQUEST_STRUCT *rqptr,
BOOL YesNo
)
{
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileSetAuthorizePath() !&B", YesNo);

   /* if not previously initialised */
   if (!(tkptr = rqptr->FileTaskPtr)) tkptr = FileNewTask (rqptr);

   tkptr->AuthorizePath = YesNo;
}

/*****************************************************************************/
/*
Read the file into memory pointed to by 'rqptr->FileContentPtr->ContentPtr'
and 'rqptr->FileContentPtr->ContentLength' in length.  File is null-terminated
in case it is text of some sort (this null is not counted in the length).
*/ 
 
void FileSetContentHandler
(
REQUEST_STRUCT *rqptr,
REQUEST_AST ContentHandlerFunction,
int SizeMax
)
{
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileSetContentHandler() !&X !UL",
                 ContentHandlerFunction, SizeMax);

   /* if not previously initialised */
   if (!(tkptr = rqptr->FileTaskPtr)) tkptr = FileNewTask (rqptr);

   /* setting this non-NULL indicates the file contents should be buffered */
   tkptr->ContentHandlerFunction = ContentHandlerFunction;
   tkptr->FileContentsSizeMax = SizeMax;
}

/*****************************************************************************/
/*
File allowed to be cached?
*/ 
 
void FileSetCacheAllowed
(
REQUEST_STRUCT *rqptr,
BOOL YesNo
)
{
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileSetCacheAllowed() !&B", YesNo);

   /* if not previously initialised */
   if (!(tkptr = rqptr->FileTaskPtr)) tkptr = FileNewTask (rqptr);

   tkptr->CacheAllowed = YesNo;
}

/*****************************************************************************/
/*
Escape HTML-forbidden characters (e.g. '<') during file output.
*/ 
 
void FileSetEscapeHtml
(
REQUEST_STRUCT *rqptr,
BOOL YesNo
)
{
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileSetEscapeHtml() !&B", YesNo);

   /* if not previously initialised */
   if (!(tkptr = rqptr->FileTaskPtr)) tkptr = FileNewTask (rqptr);

   tkptr->EscapeHtml = YesNo;
   if (!rqptr->rqCache.DoNotCache) rqptr->rqCache.DoNotCache = YesNo;
}

/*****************************************************************************/
/*
Enclose the file output with <pre>...</pre> tags.
*/ 
 
void FileSetPreTag
(
REQUEST_STRUCT *rqptr,
BOOL YesNo
)
{
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileSetPreTag() !&B", YesNo);

   /* if not previously initialised */
   if (!(tkptr = rqptr->FileTaskPtr)) tkptr = FileNewTask (rqptr);

   tkptr->PreTagFileContents = YesNo;
   if (!rqptr->rqCache.DoNotCache) rqptr->rqCache.DoNotCache = YesNo;
}

/*****************************************************************************/
/*
Begin to transfer a file.  
*/ 
 
FileBegin
(
REQUEST_STRUCT *rqptr,
REQUEST_AST NextTaskFunction,
REQUEST_AST NoSuchFileFunction,
MD5_HASH *Md5HashPtr,
char *FileName,
char *ContentTypePtr
)
{
   int  status,
        FilePathLength;
   char  *cptr, *sptr, *zptr;
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileBegin() !&F !&B !&A !&A !16&H !&Z !&Z",
                 &FileBegin, ERROR_REPORTED(rqptr),
                 NextTaskFunction, NoSuchFileFunction,
                 Md5HashPtr, FileName, ContentTypePtr);

   if (ERROR_REPORTED (rqptr))
   {
      /* previous error, cause threaded processing to unravel */
      VmFreeFromHeap (rqptr, rqptr->FileTaskPtr, FI_LI);
      rqptr->FileTaskPtr = NULL;
      SysDclAst (NextTaskFunction, rqptr);
      return;
   }

   InstanceGblSecIncrLong (&AccountingPtr->DoFileModuleCount);

   /* if not previously initialised */
   if (!(tkptr = rqptr->FileTaskPtr)) tkptr = FileNewTask (rqptr);

   tkptr->ContentTypePtr = ContentTypePtr;
   tkptr->NextTaskFunction = NextTaskFunction;
   tkptr->NoSuchFileFunction = NoSuchFileFunction;

   if (rqptr->rqPathSet.AcceptLangChar &&
       ((ContentTypePtr && strsame (ContentTypePtr, "text/", 5)) ||
        rqptr->rqContentInfo.TypeText ||
        rqptr->rqPathSet.AcceptLangWildcard))
   {
      /* if a default language specified and default is satisfactory */
      if (!rqptr->rqPathSet.AcceptLangPtr || !FileAcceptLangDefault (rqptr))
      {
         /* try and resolve a language-specific document */
         if (!FileName)
         {
            /* must be a cache search, not interested at this stage */
            FileEnd (rqptr);
            return;
         }
         FileAcceptLangBegin (rqptr, FileName);
         return;
      }
   }

   if (!(cptr = FileName)) cptr = "";
   zptr = (sptr = tkptr->FileName) + sizeof(tkptr->FileName);
   while (*cptr && sptr < zptr) *sptr++ = *cptr++;
   if (sptr >= zptr)
   {
      ErrorGeneralOverflow (rqptr, FI_LI);
      FileEnd (rqptr);
      return;
   }
   *sptr = '\0';
   tkptr->FileNameLength = sptr - tkptr->FileName;

   if (Md5HashPtr)
   {
      /* buffer the supplied resource hash in the task structure */
      memcpy (&tkptr->Md5Hash, Md5HashPtr, sizeof(MD5_HASH));
   }
   else
   {
      /* generate a hash representing the file name being accessed */
      Md5Digest (tkptr->FileName, tkptr->FileNameLength, &tkptr->Md5Hash);
   }

   /* no file name supplied indicates only searching the cache */
   if (tkptr->AuthorizePath && tkptr->FileNameLength)
   {
      /***********************/
      /* check authorization */
      /***********************/

      cptr = MapVmsPath (tkptr->FileName, rqptr);
      if (!*cptr)
      {
         /* MAPURL errors are returned with a leading null (historical ;^) */
         ErrorGeneral (rqptr, cptr+1, FI_LI);
         FileEnd (rqptr);
         return;
      }
      Authorize (rqptr, cptr, -1, NULL, 0, &FileAuthorizationAst);
      if (VMSnok (rqptr->rqAuth.FinalStatus))
      {
         /* if asynchronous authentication is not underway */
         if (rqptr->rqAuth.FinalStatus != AUTH_PENDING) FileEnd (rqptr);
         return;
      }
   }

   /* not to-be-authorized, or authorized ... just carry on regardless! */
   FileAuthorizationAst (rqptr);
} 

/*****************************************************************************/
/*
There is a default language set against the path.  Compare the request header
"Accept-Language:" first language (if any) to the default language.  If the
same return true, otherwise false.
*/ 
 
BOOL FileAcceptLangDefault (REQUEST_STRUCT *rqptr)

{
   char  *cptr, *sptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "FileAcceptLangDefault()");

   if (WATCHING (rqptr, WATCH_RESPONSE))
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
                 "LANGUAGE default:!AZ accept:!AZ",
                 rqptr->rqPathSet.AcceptLangPtr,
                 rqptr->rqHeader.AcceptLangPtr ?
                    rqptr->rqHeader.AcceptLangPtr : "(none)");

   if (!(cptr = rqptr->rqPathSet.AcceptLangPtr)) return (true);
   if (!(sptr = rqptr->rqHeader.AcceptLangPtr)) return (true);
   if (!*sptr || *sptr == '*') return (true);
   while (*cptr && *sptr && *sptr != ',' && *sptr != ';' &&
          TOLO(*cptr) == TOLO(*sptr))
   {
      cptr++;
      sptr++;
   }
   if (!*cptr && (!*sptr || *sptr == ',' || *sptr == ';'))
      return (true);
   else
      return (false);
} 

/*****************************************************************************/
/*
This series if functions attempts to resolve a language-specific document based
on the file name originally supplied.  A seachable file specification is
contructed and used to find all possible language variants of the file
originally specified.
*/ 
 
FileAcceptLangBegin
(
REQUEST_STRUCT *rqptr,
char *FileName
)
{
   BOOL  LowerCaseHit;
   int  cnt, status;
   char  *cptr, *sptr, *zptr;
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileAcceptLangBegin() !&Z", FileName);

   if (WATCHING (rqptr, WATCH_RESPONSE))
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE, "LANGUAGE !AZ", FileName);

   tkptr = rqptr->FileTaskPtr;

   cptr = FileName;
   zptr = (sptr = tkptr->FileName) + sizeof(tkptr->FileName);
   /* copy the file name until the end of directory */
   while (*cptr && sptr < zptr)
   {
      if (*cptr == ']' && !SAME2(cptr,'][')) break;
      *sptr++ = *cptr++;
   }
   LowerCaseHit = false;

   /* copy the file name to the type delimiting period */
   while (*cptr && sptr < zptr)
   {
#ifdef ODS_EXTENDED
      if (OdsExtended)
      {
         if (*cptr == '^' && SAME2(cptr,'^.'))
         {
            *sptr++ = *cptr++;
            if (sptr < zptr) *sptr++ = *cptr++;
            continue;
         }
      }
      if (*cptr == '.') break;
      if (islower(*cptr)) LowerCaseHit = true;
      *sptr++ = *cptr++;
#else /* ODS_EXTENDED */
      if (*cptr == '.') break;
      *sptr++ = *cptr++;
#endif /* ODS_EXTENDED */
   }

#ifdef ODS_EXTENDED
   /* introduce an escaping '^' for the original type delimiting period */
   if (OdsExtended &&
       *cptr && rqptr->rqPathSet.AcceptLangChar == '.' &&
       sptr < zptr)
      *sptr++ = '^';
#endif /* ODS_EXTENDED */

   if (rqptr->rqPathSet.AcceptLangTypeVariant)
   {
      /*********************/
      /* variant file type */
      /*********************/

      /* if there is no existing period add one */
      if (!*cptr && sptr < zptr) *sptr++ = '.';

      /* copy file type to end of string */
      while (*cptr && sptr < zptr)
      {
#ifdef ODS_EXTENDED
         if (islower(*cptr)) LowerCaseHit = true;
#endif /* ODS_EXTENDED */
         *sptr++ = *cptr++;
      }

      /* note the point at which we begin to add wildcarded variants */
      tkptr->AcceptLangVariantPtr = sptr;

      /* append the variant delimiting character and a wildcard */
      if (sptr < zptr) *sptr++ = rqptr->rqPathSet.AcceptLangChar;
      if (sptr < zptr) *sptr++ = '*';
   }
   else
   {
      /*********************/
      /* variant file name */
      /*********************/

      /* note the point at which we begin to add wildcarded variants */
      tkptr->AcceptLangVariantPtr = sptr;

      /* insert the variant delimiting character, then wildcard(s :^) */
      if (sptr < zptr) *sptr++ = rqptr->rqPathSet.AcceptLangChar;
      /*
         Bit of a shonky here.
         I'm reserving space in the filename for the largest possible
         ISO language string which (as far as I know) is 5 characters
         (e.g. "fr-BE", "fr-CA").  This makes reusing the file buffer
         space in FileAcceptLangSelect() much easier (I'm getting
         lazier faster than I'm actually getting older).  The multiple
         wildcards used here (seem to) make no difference for RMS.
      */
      for (cnt = FILE_ACCEPT_LANG_VARIANT_MAX; cnt && sptr < zptr; cnt--)
         *sptr++ = '*';

      /* copy file type to end of string */
      while (*cptr && sptr < zptr)
      {
#ifdef ODS_EXTENDED
         if (islower(*cptr)) LowerCaseHit = true;
#endif /* ODS_EXTENDED */
         *sptr++ = *cptr++;
      }
   }

   if (sptr >= zptr)
   {
      ErrorGeneralOverflow (rqptr, FI_LI);
      FileEnd (rqptr);
      return;
   }
   *sptr = '\0';
   tkptr->FileNameLength = sptr - tkptr->FileName;

   tkptr->AcceptLangLowerCase = LowerCaseHit;

   OdsStructInit (&tkptr->FileOds, true);

   OdsParse (&tkptr->FileOds,
             tkptr->FileName, tkptr->FileNameLength, NULL, 0,
             0, &FileAcceptLangParseAst, rqptr);
} 

/*****************************************************************************/
/*
AST called from FileAcceptLangBegin() when asynchronous parse completes.  Check
the status and if OK begin the search.
*/

FileAcceptLangParseAst (REQUEST_STRUCT *rqptr)

{
   int  status;
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileAcceptLangParseAst() !&F sts:!&S stv:!&S",
                 &FileAcceptLangParseAst,
                 rqptr->FileTaskPtr->FileOds.Fab.fab$l_sts,
                 rqptr->FileTaskPtr->FileOds.Fab.fab$l_stv);

   tkptr = rqptr->FileTaskPtr;

   if (VMSnok (status = tkptr->FileOds.Fab.fab$l_sts))
   {
      /*****************/
      /* error parsing */
      /*****************/

      /* ensure only the original file name appears in any error messages */
      tkptr->FileName[tkptr->FileNameLength] = '\0';

      /* if its a search list treat directory not found as if file not found */
      if ((tkptr->FileOds.Nam_fnb & NAM$M_SEARCH_LIST) && status == RMS$_DNF)
         status = RMS$_FNF;

      if (tkptr->NoSuchFileFunction)
      {
         if (WATCHING (rqptr, WATCH_RESPONSE))
            WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
                       "FILE !&S", status);
         rqptr->HomePageStatus = status;
         FileEnd (rqptr);
         return;
      }

      rqptr->rqResponse.ErrorTextPtr = MapVmsPath (tkptr->FileName, rqptr);
      rqptr->rqResponse.ErrorOtherTextPtr = tkptr->FileName;
      ErrorVmsStatus (rqptr, status, FI_LI);
      FileEnd (rqptr);
      return;
   }

   OdsSearch (&tkptr->FileOds, &FileAcceptLangSearchAst, rqptr);
}

/*****************************************************************************/
/*
AST called from FileAcceptLangParseAst() and then from
FileAcceptLangSearchAst() when asynchronous search completes.  Check the status
and if OK buffer the resolved language component in a comma-separated list. 
The continue the search.  Once no-more-files status occurs call
FileAcceptLangSelect() to assess the language variants (if any).
*/

FileAcceptLangSearchAst (REQUEST_STRUCT *rqptr)

{
   int  status;
   char  *cptr, *sptr, *zptr;
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileAcceptLangSearchAst() !&F sts:!&S stv:!&S",
                 &FileAcceptLangSearchAst,
                 rqptr->FileTaskPtr->FileOds.Fab.fab$l_sts,
                 rqptr->FileTaskPtr->FileOds.Fab.fab$l_stv);

   tkptr = rqptr->FileTaskPtr;

   if (VMSnok (status = tkptr->FileOds.Fab.fab$l_sts))
   {
      if (status == RMS$_FNF || status == RMS$_NMF)
      {
         /***************************/
         /* end of directory search */
         /***************************/

         tkptr->FileOds.ParseInUse = false;
         FileAcceptLangSelect (rqptr);
         return;
      }

      /**********************/
      /* sys$search() error */
      /**********************/

      /* ensure only the original file name appears in any error messages */
      tkptr->FileName[tkptr->FileNameLength] = '\0';

      rqptr->rqResponse.ErrorTextPtr = MapVmsPath (tkptr->FileName, rqptr);
      rqptr->rqResponse.ErrorOtherTextPtr = tkptr->FileName;
      ErrorVmsStatus (rqptr, status, FI_LI);
      FileEnd (rqptr);
      return;
   }

   /****************/
   /* add language */
   /****************/

   if (!tkptr->AcceptLangTypesSize)
   {
      tkptr->AcceptLangTypesPtr = VmGetHeap (rqptr, FILE_ACCEPT_LANG_SIZE);
      tkptr->AcceptLangTypesSize = FILE_ACCEPT_LANG_SIZE;
   }
   zptr = (sptr = tkptr->AcceptLangTypesPtr) + tkptr->AcceptLangTypesSize;
   sptr += tkptr->AcceptLangTypesLength;
   if (tkptr->AcceptLangTypesLength && sptr < zptr) *sptr++ = ',';

   if (rqptr->rqPathSet.AcceptLangTypeVariant)
   {
      /* variant file type */
      cptr = tkptr->FileOds.NamVersionPtr;
      while (cptr > tkptr->FileOds.NamTypePtr &&
            *cptr != rqptr->rqPathSet.AcceptLangChar) cptr--;
      if (*cptr) cptr++;
      while (cptr < tkptr->FileOds.NamVersionPtr && sptr < zptr)
         *sptr++ = *cptr++;
   }
   else
   {
      /* variant file name */
      cptr = tkptr->FileOds.NamTypePtr;
      while (cptr > tkptr->FileOds.NamNamePtr &&
            *cptr != rqptr->rqPathSet.AcceptLangChar) cptr--;
      if (*cptr) cptr++;
      while (cptr < tkptr->FileOds.NamTypePtr && sptr < zptr)
         *sptr++ = *cptr++;
   }

   /* check the length of the (potential) variant (may not be) */
   if ((sptr - tkptr->AcceptLangTypesPtr) - tkptr->AcceptLangTypesLength <=
       FILE_ACCEPT_LANG_VARIANT_MAX)
   {
      if (sptr >= zptr)
      {
         /* more than FILE_ACCEPT_LANG_SIZE / _VARIANT (256/5~=51!!) */
         ErrorNoticed (rqptr, SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
         ErrorGeneral (rqptr, ErrorSanityCheck, FI_LI);
         FileEnd (rqptr);
         return;
      }
      *sptr = '\0';
      tkptr->AcceptLangTypesLength = sptr - tkptr->AcceptLangTypesPtr;
   }

   OdsSearch (&tkptr->FileOds, &FileAcceptLangSearchAst, rqptr);
}

/*****************************************************************************/
/*
Explicitly called by FileAcceptLangSearchAst() when no-more-files status occurs
to assess the comma-separated list of language components generated (if any).
By comparing each "Accept-Language:" entry against each of the file language
components select the first to match (if any).  If one matches then adjust the
original file name to include the language component with the type.  If none
matches revert to the original file name.  Commence file processing.
*/

FileAcceptLangSelect (REQUEST_STRUCT *rqptr)

{
   BOOL  AcceptLangVariant;
   int  status;
   char  *cptr, *sptr, *tptr, *zptr;
   FILE_TASK  *tkptr;

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

   tkptr = rqptr->FileTaskPtr;

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileAcceptLangSelect() !&F !&Z !&Z",
                 &FileAcceptLangSelect, tkptr->AcceptLangTypesPtr,
                 rqptr->rqHeader.AcceptLangPtr);

   if (WATCHING (rqptr, WATCH_RESPONSE))
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
                 "LANGUAGE variants:!AZ accept:!AZ",
                 tkptr->AcceptLangTypesPtr ?
                    tkptr->AcceptLangTypesPtr : "(none)",
                 rqptr->rqHeader.AcceptLangPtr ?
                    rqptr->rqHeader.AcceptLangPtr : "(none)");

   AcceptLangVariant = false;
   if (tkptr->AcceptLangTypesLength)
   {
      /*********************************/
      /* at least one possible variant */
      /*********************************/

      cptr = rqptr->rqHeader.AcceptLangPtr;
      while (*cptr)
      {
         while (*cptr && ISLWS(*cptr)) cptr++;
         if (!*cptr) break;
         sptr = tkptr->AcceptLangTypesPtr;
         while (*sptr)
         {
            tptr = cptr;
            while (*tptr && *tptr != ',' && *tptr != ';' &&
                   *sptr && *sptr != ',' &&
                   TOLO(*tptr) == TOLO(*sptr))
            {
               tptr++;
               sptr++;
            }
            if ((!*tptr || *tptr == ',' || *tptr == ';') &&
                (!*sptr || *sptr == ','))
            {
               AcceptLangVariant = true;
               break;
            }
            while (*sptr && *sptr != ',') sptr++;
            if (*sptr) sptr++;
         }
         if (AcceptLangVariant) break;
         while (*cptr && *cptr != ',') cptr++;
         if (*cptr) cptr++;
      }
   }

   if (AcceptLangVariant)
   {
      /**********************************/
      /* add language variant component */
      /**********************************/

      /*
         This handles both the post-file-type language variant, where the
         wildcard has just been appended to the file type, as well as the
         post-file-name language variant, where the wildcard was inserted
         between the end of the file name and the type delimiting period
         (in this case it reserved space using 5 wildcards).
      */
      zptr = tkptr->FileName + sizeof(tkptr->FileName);
      /* step over any period-escaping character (would be EFS only) */
      if (*(sptr = tkptr->AcceptLangVariantPtr) == '^') sptr++;
      /* step over the variant delimiting character */
      sptr++;
      /* 'cptr' has been left pointing at the matching language */
      tptr = cptr;
      if (tkptr->AcceptLangLowerCase)
         while (*cptr && *cptr != ',' && *cptr != ';' && sptr < zptr)
            *sptr++ = TOLO(*cptr++);
      else
         while (*cptr && *cptr != ',' && *cptr != ';' && sptr < zptr)
            *sptr++ = TOUP(*cptr++);
      if (sptr >= zptr)
      {
         ErrorGeneralOverflow (rqptr, FI_LI);
         FileEnd (rqptr);
         return;
      }
      /* absorb any remaining wildcards */
      for (cptr = sptr; *cptr == '*'; cptr++);
      while (*cptr && sptr < zptr) *sptr++ = *cptr++;
      if (sptr >= zptr)
      {
         ErrorGeneralOverflow (rqptr, FI_LI);
         FileEnd (rqptr);
         return;
      }
      *sptr = '\0';
      tkptr->FileNameLength = sptr - tkptr->FileName;

      if (rqptr->rqPathSet.AcceptLangTypeVariant)
      {
         /* (re)determine the (possibly) new content-type */
         while (sptr > tkptr->FileName && *sptr != '.') sptr--;
         ConfigContentType (&rqptr->rqContentInfo, sptr);
         tkptr->ContentTypePtr = rqptr->rqContentInfo.ContentTypePtr;
      }
   }
   else
   {
      /*******************************/
      /* no language variant, revert */
      /*******************************/

      /* eliminate any period-escaping character (would be EFS only) */
      if (*(sptr = cptr = tkptr->AcceptLangVariantPtr) == '^') cptr++;
      /* eliminate the variant delimiting character and the wildcard(s) */
      cptr++;
      while (*cptr && *cptr == '*') cptr++;
      while (*cptr) *sptr++ = *cptr++;
      *sptr = '\0';
      tkptr->FileNameLength = sptr - tkptr->FileName;
   }

   if (WATCHING (rqptr, WATCH_RESPONSE))
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
                 "LANGUAGE !AZ", tkptr->FileName);

   /*************************/
   /* begin processing file */
   /*************************/

   /* generate a hash representing the file name being accessed */
   Md5Digest (tkptr->FileName, tkptr->FileNameLength, &tkptr->Md5Hash);

   if (WATCHING (rqptr, WATCH_RESPONSE))
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
                 "FILE !16&H !AZ", &tkptr->Md5Hash, tkptr->FileName);

   /* no file name supplied indicates only searching the cache */
   if (tkptr->AuthorizePath && tkptr->FileNameLength)
   {
      /***********************/
      /* check authorization */
      /***********************/

      cptr = MapVmsPath (tkptr->FileName, rqptr);
      if (!*cptr)
      {
         /* MAPURL errors are returned with a leading null (historical ;^) */
         ErrorGeneral (rqptr, cptr+1, FI_LI);
         FileEnd (rqptr);
         return;
      }
      Authorize (rqptr, cptr, -1, NULL, 0, &FileAuthorizationAst);
      if (VMSnok (rqptr->rqAuth.FinalStatus))
      {
         /* if asynchronous authentication is not underway */
         if (rqptr->rqAuth.FinalStatus != AUTH_PENDING) FileEnd (rqptr);
         return;
      }
   }

   /* not to-be-authorized, or authorized ... just carry on regardless! */
   FileAuthorizationAst (rqptr);
} 

/*****************************************************************************/
/*
This function provides an AST target is Authorize()ation ended up being done
asynchronously, otherwise it is just called directly to continue the modules
processing.
*/

FileAuthorizationAst (REQUEST_STRUCT *rqptr)

{
   int  status;
   char  *cptr;
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileAuthorizationAst() !&F !&S",
                 &FileAuthorizationAst, rqptr->rqAuth.FinalStatus);

   /* may have been delivered asynchronously */
   rqptr->rqAuth.AstFunction = NULL;

   if (VMSnok (rqptr->rqAuth.FinalStatus))
   {
      FileEnd (rqptr);
      return;
   }

   tkptr = rqptr->FileTaskPtr;

   if (!tkptr->ContentTypePtr)
      tkptr->ContentTypePtr = ConfigContentTypeUnknown;
   if (ConfigSameContentType (tkptr->ContentTypePtr,
                              ConfigContentTypeUnknown, -1))
   {
      /* if the content-type is not known then use the default */
      if (Config.cfContent.ContentTypeDefaultPtr[0])
         tkptr->ContentTypePtr = Config.cfContent.ContentTypeDefaultPtr;
      else
         tkptr->ContentTypePtr = ConfigDefaultFileContentType;
   }

   if (!rqptr->rqCache.DoNotCache)
   {
      status = CacheSearch (rqptr);
      if (WATCHMOD (rqptr, WATCH_MOD_FILE))
         WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                    "CacheSearch() !&S", status);

      /* success status indicates the file is being supplied from cache */
      if (VMSok (status)) return;

      /* no file name supplied indicates only searching the cache */
      if (!tkptr->FileNameLength)
      {
         FileEnd (rqptr);
         return;
      }
   }

   FileParse (rqptr);
} 

/*****************************************************************************/
/*
Parse the file task's file name.
*/

FileParse (REQUEST_STRUCT *rqptr)

{
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileParse() !&F", &FileParse);

   tkptr = rqptr->FileTaskPtr;

   AuthAccessEnable (rqptr, tkptr->FileName, AUTH_ACCESS_READ);

   OdsParse (&tkptr->FileOds, tkptr->FileName, tkptr->FileNameLength,
             ".", 1, 0, &FileParseAst, rqptr);

   AuthAccessEnable (rqptr, 0, 0);
} 

/*****************************************************************************/
/*
AST when FileParse() asynchronous parsecompletes.  If status OK set up and
queue an ACP QIO to get file size and revision date/time, ASTing to
FileAcpInfoAst().
*/

FileParseAst (REQUEST_STRUCT *rqptr)

{
   int  status;
   char  *cptr;
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileParseAst() !&F sts:!&S stv:!&S", &FileParseAst,
                 rqptr->FileTaskPtr->FileOds.Fab.fab$l_sts,
                 rqptr->FileTaskPtr->FileOds.Fab.fab$l_stv);

#if WATCH_MOD
   HttpdCheckPriv (FI_LI);
#endif /* WATCH_MOD */

   tkptr = rqptr->FileTaskPtr;

   if (VMSok (status = tkptr->FileOds.Fab.fab$l_sts))
   {
      /**************/
      /* exclusions */
      /**************/

      if (MATCH5 (tkptr->FileOds.NamTypePtr, ".DIR;") ||
          MATCH5 (tkptr->FileOds.NamTypePtr, ".dir;"))
      {
         /* no sneaky getting directory contents by downloading files! */
         status = OdsReallyADir (rqptr, &tkptr->FileOds);
         if (VMSok (status)) status = SS$_NOPRIV;
      }
      else
      if (rqptr->RemoteUser[0] &&
          strsame (tkptr->FileOds.NamTypePtr,
                    HTL_FILE_TYPE, sizeof(HTL_FILE_TYPE)-1) &&
          *(tkptr->FileOds.NamTypePtr+sizeof(HTL_FILE_TYPE)-1) == ';')
      {
         /* access to an HTL is OK *IF* it is authorized!! (for admin) */
         tkptr->ContentTypePtr = "text/plain";
      }
      else
      if ((strsame (tkptr->FileOds.NamTypePtr,
                    HTA_FILE_TYPE, sizeof(HTA_FILE_TYPE)-1) &&
           *(tkptr->FileOds.NamTypePtr+sizeof(HTA_FILE_TYPE)-1) == ';') ||
          (strsame (tkptr->FileOds.NamTypePtr,
                    HTL_FILE_TYPE, sizeof(HTL_FILE_TYPE)-1) &&
           *(tkptr->FileOds.NamTypePtr+sizeof(HTL_FILE_TYPE)-1) == ';'))
      {
         /* attempt to retrieve an HTA/HTL authorization file, scotch that! */
         status = SS$_NOSUCHFILE;
      }
      else
      if (DavMetaFile (&tkptr->FileOds))
         status = SS$_NOSUCHFILE;
   }

   /* if a wildcard the ACP function will return the first matching file! */
   if (tkptr->FileOds.Nam_fnb & NAM$M_WILDCARD) status = RMS$_WLD;

   if (status == SS$_ABORT &&
       (MATCH5 (tkptr->FileOds.NamTypePtr, ".DIR;") ||
        MATCH5 (tkptr->FileOds.NamTypePtr, ".dir;")))
      status = SS$_NORMAL;

   if (VMSok (status))
   {
      if (WebDavEnabled)
      {
         /* WebDAV metadata (sub)directory? */
         if (rqptr->rqPathSet.WebDavRead ||
             rqptr->rqPathSet.WebDavWrite ||
             rqptr->rqPathSet.WebDavProfile ||
             rqptr->rqPathSet.WebDavServer)
            if (DavMetaDir (rqptr, &tkptr->FileOds))
               status = RMS$_DNF;
      }
   }

   if (VMSnok (status))
   {
      /*****************/
      /* error parsing */
      /*****************/

      /* if its a search list treat directory not found as if file not found */
      if ((tkptr->FileOds.Nam_fnb & NAM$M_SEARCH_LIST) && status == RMS$_DNF)
         status = RMS$_FNF;

      if (tkptr->NoSuchFileFunction)
      {
         if (WATCHING (rqptr, WATCH_RESPONSE))
            WatchThis (WATCHITM(rqptr), WATCH_RESPONSE, "FILE !&S", status);
         rqptr->HomePageStatus = status;
         FileEnd (rqptr);
         return;
      }

      rqptr->rqResponse.ErrorTextPtr = MapVmsPath (tkptr->FileName, rqptr);
      rqptr->rqResponse.ErrorOtherTextPtr = tkptr->FileName;
      ErrorVmsStatus (rqptr, status, FI_LI);
      FileEnd (rqptr);
      return;
   }

   if (tkptr->FileOds.Nam_fnb & NAM$M_SEARCH_LIST &&
       !tkptr->SearchListCount++)
   {
      /*******************************/
      /* search to get actual device */
      /*******************************/

      if (WATCHMOD (rqptr, WATCH_MOD_FILE))
         WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "SEARCH-LIST");

#ifndef ODS_EXTENDED
      /* for no-conceal search to work */
      OdsParse (&tkptr->FileOds,
                tkptr->FileName, tkptr->FileNameLength,
                ".", 1, NAM$M_NOCONCEAL,
                NULL, 0);
#endif

      AuthAccessEnable (rqptr, tkptr->FileName, AUTH_ACCESS_READ);
      OdsSearchNoConceal (&tkptr->FileOds, &FileParseAst, rqptr);
      AuthAccessEnable (rqptr, 0, 0);
      return;
   }

   /************/
   /* ACP info */
   /************/

   AuthAccessEnable (rqptr, tkptr->FileName, AUTH_ACCESS_READ);

   OdsFileAcpInfo (&tkptr->FileOds, &FileAcpInfoAst, rqptr); 

   AuthAccessEnable (rqptr, 0, 0);
}

/****************************************************************************/
/*
AST called from FileParseAst() when ACP QIO completes.  If status indicates no
such file then call any file open error processing function originally
supplied, otherwise report the error.  If status OK 'open' the file.
*/

FileAcpInfoAst (REQUEST_STRUCT *rqptr)

{
   static $DESCRIPTOR (FibDsc, "");

   BOOL  RangeValid;
   int  idx, status,
        BufferSize;
   char  *cptr, *sptr, *zptr,
         *rfmptr;
   FILE_CONTENT  *fcptr;
   FILE_QIO  *fqptr;
   FILE_TASK  *tkptr;
   RANGE_BYTE  *rbptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileAcpInfoAst() !&F !&S", &FileAcpInfoAst,
                 rqptr->FileTaskPtr->FileOds.FileQio.IOsb.Status);

#if WATCH_MOD
   HttpdCheckPriv (FI_LI);
#endif /* WATCH_MOD */

   tkptr = rqptr->FileTaskPtr;
   fqptr = &tkptr->FileOds.FileQio;

   if ((status = fqptr->IOsb.Status) == SS$_NOSUCHFILE) status = RMS$_FNF;

   if (status == RMS$_FNF)
   {
      /*************************/
      /* file extension kludge */
      /*************************/

      zptr = (cptr = rqptr->rqHeader.PathInfoPtr) +
             rqptr->rqHeader.PathInfoLength;
      while (zptr > cptr && *zptr != '$' && *zptr != '/') zptr--;
      if (zptr > cptr && *(USHORTPTR)zptr == '$.')
      {
         /* hit what looks like the sentinal */
         while (zptr > cptr && *zptr != '.' && *zptr != '/') zptr--;
         if (zptr > cptr && *zptr == '.')
         {
            /* a second period (and perhaps file name extension) */
            while (*zptr != '$') zptr++;
            ResponseLocation (rqptr, cptr, zptr-cptr);
            FileEnd (rqptr);
            return;
         }
      }
   }

   if (status == RMS$_FNF && tkptr->NoSuchFileFunction)
   {
      if (WATCHING (rqptr, WATCH_RESPONSE))
         WatchThis (WATCHITM(rqptr), WATCH_RESPONSE, "FILE !&S", status);
      rqptr->HomePageStatus = status;
      FileEnd (rqptr);
      return;
   }
   else
   {
      /* the last point at which no-such-file could have been reported */
      tkptr->NoSuchFileFunction = NULL;

      /* if it's a directory listing README then ignore NOPRIV */
      if (tkptr->NextTaskFunction == &DirHeading && status == SS$_NOPRIV)
         status = SS$_NORMAL;

      if (VMSnok (status))
      {
         if (!rqptr->AccountingDone++)
            InstanceGblSecIncrLong (&AccountingPtr->DoFileCount);
         rqptr->rqResponse.ErrorTextPtr = MapVmsPath (tkptr->FileName, rqptr);
         rqptr->rqResponse.ErrorOtherTextPtr = tkptr->FileName;
         ErrorVmsStatus (rqptr, status, FI_LI);
         FileEnd (rqptr);
         return;
      }
   }

   /***************/
   /* file exists */
   /***************/

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "did:!UL,!UL,!UL fid:!UL,!UL,!UL {!UL}!-!#AZ",
                 fqptr->Fib.fib$w_did[0], fqptr->Fib.fib$w_did[1],
                 fqptr->Fib.fib$w_did[2], fqptr->Fib.fib$w_fid[0],
                 fqptr->Fib.fib$w_fid[1], fqptr->Fib.fib$w_fid[2],
                 fqptr->FileNameDsc.dsc$w_length,
                 fqptr->FileNameDsc.dsc$a_pointer);

   fqptr->AllocatedVbn = ((fqptr->RecAttr.fat$l_hiblk & 0xffff) << 16) |
                         ((fqptr->RecAttr.fat$l_hiblk & 0xffff0000) >> 16);
   fqptr->EndOfFileVbn = ((fqptr->RecAttr.fat$l_efblk & 0xffff) << 16) |
                         ((fqptr->RecAttr.fat$l_efblk & 0xffff0000) >> 16);
   fqptr->FirstFreeByte = fqptr->RecAttr.fat$w_ffbyte;

   if ((uint)fqptr->EndOfFileVbn <= 1)
      fqptr->SizeInBytes64 = fqptr->FirstFreeByte;
   else
   if ((uint)fqptr->EndOfFileVbn < 0x400000)
      fqptr->SizeInBytes64 = ((fqptr->EndOfFileVbn-1) << 9) +
                             fqptr->FirstFreeByte;
   else
   {
      /* greater than 4GB */
      fqptr->SizeInBytes64 = fqptr->EndOfFileVbn-1;
      fqptr->SizeInBytes64 *= 512;
      fqptr->SizeInBytes64 += fqptr->FirstFreeByte;
   }

   if (WATCHING (rqptr, WATCH_RESPONSE))
   {
      switch (fqptr->RecAttr.fat$b_rtype)
      {
         case FAT$C_VARIABLE :  rfmptr = "VAR"; break;
         case FAT$C_VFC :       rfmptr = "VFC"; break;
         case FAT$C_FIXED :     rfmptr = "FIX"; break;
         case FAT$C_STREAM :    rfmptr = "STM"; break;
         case FAT$C_STMLF :     rfmptr = "STMLF"; break;
         case FAT$C_STMCR :     rfmptr = "STMCR"; break;
         case FAT$C_UNDEFINED : rfmptr = "UDF"; break;
         default : rfmptr = "?";
      }
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
#ifdef ODS_EXTENDED   
"FILE !AZ ODS:!UL rfm:!AZ ebk:!UL ffb:!UL (!AZ!@SQ bytes!AZ) rdt:!%D",
                 tkptr->FileName, rqptr->PathOds,
#else
"FILE !AZ rfm:!AZ ebk:!UL ffb:!UL (!AZ!@SQ bytes!AZ) rdt:!%D",
                 tkptr->FileName,
#endif /* ODS_EXTENDED */
                 rfmptr, fqptr->EndOfFileVbn, fqptr->FirstFreeByte,
                 fqptr->RecAttr.fat$b_rtype == FAT$C_VARIABLE ||
                    fqptr->RecAttr.fat$b_rtype == FAT$C_VFC ? "~" : "",
                 &fqptr->SizeInBytes64,
                 fqptr->SizeInBytes64 > __UINT32_MAX ? " >4GB!" : "",
                 &fqptr->RdtTime64);
   }

   if (rqptr->rqPathSet.CharsetPtr)
   {
      cptr = tkptr->FileOds.ResFileName;
      if (!*cptr) cptr = tkptr->FileOds.ExpFileName;
      rqptr->rqPathSet.CharsetPtr = FileSetCharset (rqptr, cptr);
   }

   if (rqptr->rqHeader.RangeBytePtr &&
       rqptr->rqHeader.RangeBytePtr->Total &&
       !rqptr->rqResponse.HeaderGenerated &&
       !tkptr->ContentHandlerFunction &&
       fqptr->RecAttr.fat$b_rtype != FAT$C_VARIABLE &&
       fqptr->RecAttr.fat$b_rtype != FAT$C_VFC)
   {
      /******************************/
      /* byte-range on non-VAR file */
      /******************************/

      RangeValid = true;
      rbptr = rqptr->rqHeader.RangeBytePtr; 
      for (idx = 0; idx < rbptr->Total; idx++)
      {
         if (!rbptr->Last[idx])
         {
            /* last byte not specified, set at EOF */
            rbptr->Last[idx] = fqptr->SizeInBytes64 - 1;
         }
         else
         if (rbptr->Last[idx] < 0)
         {
            /* first byte a negative offset from end, last byte at EOF */
            rbptr->First[idx] = fqptr->SizeInBytes64 + rbptr->Last[idx];
            if (rbptr->First[idx] < 0) rbptr->First[idx] = 0;
            rbptr->Last[idx] = fqptr->SizeInBytes64 - 1;
         }
         else
         if (rbptr->Last[idx] >= fqptr->SizeInBytes64)
         {
            /* if the last byte is ambit make it at the EOF */
            rbptr->Last[idx] = fqptr->SizeInBytes64 - 1;
         }
         /* if the range still does not make sense then back out now */
         if (rbptr->Last[idx] <= rbptr->First[idx])
         {
            RangeValid = false;
            rbptr->Length = 0;
         }
         else
            rbptr->Length = rbptr->Last[idx] - rbptr->First[idx] + 1;

         if (WATCHING (rqptr, WATCH_RESPONSE))
            WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
               "RANGE !UL !@SQ-@SQ !@SQ byte!%s!&? INVALID\r\r",
               idx+1, &rbptr->First[idx], &rbptr->Last[idx],
               &rbptr->Length, !rbptr->Length);
      }
      if (RangeValid) rbptr->Count = idx;
   }

   if (Config.cfMisc.EntityTag)
   {
      FileGenerateEntityTag (tkptr->EntityTag, fqptr);
      if (rqptr->rqResponse.HttpVersion == HTTP_VERSION_1_1)
         strcpy (rqptr->rqResponse.EntityTag, tkptr->EntityTag);
   }

   if (!rqptr->rqResponse.HeaderGenerated &&
       !tkptr->ContentHandlerFunction)
   {
      /**************************/
      /* full response required */
      /**************************/

      if (!rqptr->AccountingDone++)
         InstanceGblSecIncrLong (&AccountingPtr->DoFileCount);

      /* variable-record length file size cannot be accurately determined */
      if (fqptr->RecAttr.fat$b_rtype == FAT$C_VARIABLE ||
          fqptr->RecAttr.fat$b_rtype == FAT$C_VFC)
         status = FileResponseHeader (rqptr, tkptr->ContentTypePtr,
                                      -1, &fqptr->RdtTime64);
      else
         status = FileResponseHeader (rqptr, tkptr->ContentTypePtr,
                                      fqptr->SizeInBytes64,
                                      &fqptr->RdtTime64);
      /* here status is a boolean */
      if (!status)
      {
         InstanceGblSecIncrLong (&AccountingPtr->DoFileNotModifiedCount);
         FileEnd (rqptr);
         return;
      }

      /* quit here if the HTTP method was HEAD */
      if (rqptr->rqHeader.Method == HTTP_METHOD_HEAD)
      {
         FileEnd (rqptr);
         return;
      }
   }

   /******************************/
   /* stream-LF file conversion? */
   /******************************/

   if (Config.cfMisc.StreamLfConversionMaxKbytes &&
       (fqptr->RecAttr.fat$b_rtype == FAT$C_VARIABLE ||
        fqptr->RecAttr.fat$b_rtype == FAT$C_VFC) &&
       rqptr->rqPathSet.StmLF &&
       ConfigSameContentType (tkptr->ContentTypePtr, "text/", 5) &&
       !ConfigSameContentType (tkptr->ContentTypePtr, ConfigContentTypeSsi, -1))
   {
      /* divide by two to get the number of kilobytes (1024) in the file */
      if (!fqptr->SizeInBytes64 && (fqptr->SizeInBytes64 >> 10) <=
          Config.cfMisc.StreamLfConversionMaxKbytes)
         StmLfBegin (MapVmsPath(tkptr->FileName, rqptr),
                     tkptr->FileName,
                     tkptr->FileNameLength,
                     rqptr->PathOdsExtended,
                     rqptr->rqAuth.VmsUserProfileLength);
   }

   /********************/
   /* begin processing */
   /********************/

   /*
      Cache load is not initiated if this is not a stand-alone file request
      (i.e. not part of some other activity, e.g. a directory read-me file,
      if it is already being loaded via another request, if a "temporary"
      file ('DeleteOnClose'), or via requests with a VMS authentication
      profile attached.
   */
   if (CacheEnabled &&
       tkptr->CacheAllowed &&
       !rqptr->rqPathSet.NoCache &&
       !rqptr->rqPathSet.CacheNoFile &&
       !rqptr->rqCache.LoadCheck &&
       !rqptr->rqCache.NotUsable &&
       !tkptr->EscapeHtml &&
       !rqptr->DeleteOnClose &&
       !rqptr->rqAuth.VmsUserProfileLength &&
       (!rqptr->rqHeader.RangeBytePtr ||
        !rqptr->rqHeader.RangeBytePtr->Count) &&
       fqptr->SizeInBytes64 <= __INT32_MAX)
   {
      /* begin caching during file content read */
      rqptr->rqCache.LoadFromFile =
         CacheLoadBegin (rqptr, (int)fqptr->SizeInBytes64, NULL);
   }

   if (tkptr->ContentHandlerFunction)
   {
      /************************/
      /* buffer file contents */
      /************************/

      if (WATCHMOD (rqptr, WATCH_MOD_FILE))
         WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "!@SQ !UL",
                    &fqptr->SizeInBytes64, tkptr->FileContentsSizeMax);

      if (fqptr->SizeInBytes64 > tkptr->FileContentsSizeMax)
      {
         rqptr->rqResponse.HttpStatus = 500;
         ErrorGeneral (rqptr, ErrorBufferFileMaxBytes, FI_LI);
         FileEnd (rqptr);
         return;
      }
      /* make buffer size a multiple of 512 byte blocks (for block I/O) */
      BufferSize = (fqptr->SizeInBytes64 / 512);
      if (fqptr->SizeInBytes64 % 512) BufferSize++;
      BufferSize *= 512;

      /* allocate a buffer */
      rqptr->FileContentPtr = fcptr = (FILE_CONTENT*)
         VmGetHeap (rqptr, sizeof(FILE_CONTENT) + BufferSize);
      fcptr->ContentSize = BufferSize;
      /* buffer space immediately follows the structured storage */
      fcptr->ContentPtr = (char*)fcptr + sizeof(FILE_CONTENT);

      /* populate the file contents structure with some file data */
      zptr = (sptr = fcptr->FileName) + sizeof(fcptr->FileName);
      for (cptr = tkptr->FileName; *cptr && sptr < zptr; *sptr++ = *cptr++);
      if (sptr >= zptr)
      {
         ErrorGeneralOverflow (rqptr, FI_LI);
         FileEnd (rqptr);
         return;
      }
      *sptr = '\0';
      fcptr->FileNameLength = sptr - fcptr->FileName;

      fcptr->CdtTime64 = fqptr->CdtTime64;
      fcptr->RdtTime64 = fqptr->RdtTime64;

      fcptr->UicGroup = (fqptr->AtrUic & 0x0fff0000) >> 16;
      fcptr->UicMember = (fqptr->AtrUic & 0x0000ffff);
      fcptr->Protection = fqptr->AtrFpro;

      /* set the content structure handler to the supplied function */
      rqptr->FileContentPtr->ContentHandlerFunction = 
         tkptr->ContentHandlerFunction;

      /* none of these little tricks, just get the raw file! */
      tkptr->PreTagFileContents = tkptr->EscapeHtml = false;
   }
   else
   {
      /* network writes are checked for success, fudge the first one! */
      rqptr->NetIoPtr->WriteStatus = SS$_NORMAL;
   }

   /*******************/
   /* 'open' the file */
   /*******************/

   /*
      Fill out the FIB, override any exclusive locking - requires SYSPRV.
      Access to this file without SYSPRV has already been established via
      the ACP QIO that effectively performs the protection checks.
   */
   fqptr->Fib.fib$l_acctl = FIB$M_SEQONLY | FIB$M_NOLOCK;
   fqptr->Fib.fib$w_nmctl = FIB$M_FINDFID;

   sys$setprv (1, &SysPrvMask, 0, 0);

   status = sys$qio (EfnNoWait, fqptr->AcpChannel,
                     IO$_ACCESS | IO$M_ACCESS,
                     &fqptr->IOsb, &FileAccessAst, rqptr,
                     &fqptr->FibDsc, 0, 0, 0, 0, 0);

   sys$setprv (0, &SysPrvMask, 0, 0);

   if (VMSnok (status))
   {
      /* let the AST routine handle it! */
      fqptr->IOsb.Status = status;
      SysDclAst (FileAccessAst, rqptr);
   }
} 

/*****************************************************************************/
/*
Match the supplied file name (generally needs to be a NOCONCEAL sys$search()
result file name as provided by OdsSearchNoConceal() with the 'name=value' pair
'name' string of a "SET /path/* charset=(pattern,charset)" rule.  If a
successful match return the corresponding 'value' otherwise return NULL.
*/

char* FileSetCharset
(
REQUEST_STRUCT *rqptr,
char *FileName
)
{
   static char  Charset [64];

   int  status;
   char  *cptr;
   char  FileSpec [128];

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileSetCharset() !&Z !&Z",
                 rqptr->rqPathSet.CharsetPtr, FileName);

   if (!(cptr = rqptr->rqPathSet.CharsetPtr)) return (NULL);
   if (cptr[0] != '(') return (cptr);
   if (!FileName) return (NULL);

   for (;;)
   {
      status = StringParseNameValue (&cptr, false,
                                     FileSpec, sizeof(FileSpec),
                                     Charset, sizeof(Charset));
      if (VMSnok (status)) return (NULL);
      if (StringMatch (NULL, FileName, FileSpec)) return (Charset);
   }
}

/****************************************************************************/
/*
Generate a response header suitable for the file being returned.
This may be a 304 (not modified) if appropriate.
This function is also used by the CACHE.C module.
Return /true/ to continue to send the file, /false/ if not to.
*/

BOOL FileResponseHeader
(
REQUEST_STRUCT *rqptr,
char *ContentType,
int64 ContentLength,
ulong *RdtTime64Ptr
)
{
   int  idx, status;
   int64  RangeContentLength;
   ushort  Length;
   char  *cptr, *sptr, *zptr,
         *ContentTypePtr;
   char  Buffer [256],
         TypeBuffer [256];
   RANGE_BYTE  *rbptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "FileResponseHeader()");

   /****************/
   /* entity match */
   /****************/

   /* if entity precondition fails (generated 412) then just return */
   if (!ResponseEntityMatch (rqptr, rqptr->rqResponse.EntityTag))
      return (true);

   if (rqptr->rqTime.IfModifiedSinceTime64 &&
       !rqptr->rqHeader.PragmaNoCache &&
       !rqptr->DeleteOnClose)
   {
      /*********************/
      /* if modified since */
      /*********************/

      if (!HttpIfModifiedSince (rqptr, RdtTime64Ptr, ContentLength))
      {
         if (WATCHING (rqptr, WATCH_RESPONSE))
             WatchThis (WATCHITM(rqptr), WATCH_RESPONSE, "NOT modified");
         return (false);
      }
   }

   if (rqptr->rqTime.IfUnModifiedSinceTime64 &&
       !rqptr->DeleteOnClose)
   {
      /*************************/
      /* if NOT modified since */
      /*************************/

      if (HttpIfUnModifiedSince (rqptr, RdtTime64Ptr))
      {
         if (WATCHING (rqptr, WATCH_RESPONSE))
             WatchThis (WATCHITM(rqptr), WATCH_RESPONSE, "NOT modified");
         return (false);
      }
   }

   rbptr = rqptr->rqHeader.RangeBytePtr;

   if (rqptr->rqHeader.QueryStringLength &&
       TOLO(rqptr->rqHeader.QueryStringPtr[0]) == 'h' &&
       strsame (rqptr->rqHeader.QueryStringPtr, "httpd=content", 13))
   {
      /* request-specified content-type (default to plain-text) */
      if (rbptr) rbptr->Count = 0;  /* cancel any byte range */
      if (strsame (rqptr->rqHeader.QueryStringPtr+13, "&type=", 6))
      {
         zptr = (sptr = TypeBuffer) + sizeof(TypeBuffer)-1;
         for (cptr = rqptr->rqHeader.QueryStringPtr + 19;
              *cptr && *cptr != '&' && sptr < zptr;
              *sptr++ = *cptr++);
         *sptr = '\0';
         StringUrlDecode (ContentTypePtr = TypeBuffer);
      }
      else
         ContentTypePtr = "text/plain";
   }
   else
      ContentTypePtr = ContentType;

   if (rbptr && rbptr->Count &&
       rqptr->rqTime.IfRangeBeginTime64 &&
       !rqptr->NotFromCache &&
       !rqptr->DeleteOnClose)
   {
      /*********************/
      /* if range modified */
      /*********************/

      /* cancels range data in the request header if file has been modified */
      HttpIfRange (rqptr, RdtTime64Ptr);
   }

   if (!rbptr || !rbptr->Count)
   {
      /*********************/
      /* standard response */
      /*********************/

      ResponseHeader (rqptr, 200, ContentTypePtr,
                      ContentLength, RdtTime64Ptr, NULL);
      return (true);
   }

   /***********************/
   /* byte range response */
   /***********************/

   if (rbptr->Count == 1)
   {
      /* single byte-range requested */
      FaoToBuffer (Buffer, sizeof(Buffer), NULL,
                   "Content-Range: bytes !@SQ-!@SQ/!@SQ\r\n",
                   &rbptr->First[0], &rbptr->Last[0], &ContentLength);
      ResponseHeader (rqptr, 206, ContentTypePtr,
                      rbptr->Last[0] - rbptr->First[0] + 1,
                      RdtTime64Ptr, Buffer);
      return (true);
   }

   /* multiple byte-ranges requested */
   rqptr->rqResponse.MultipartBoundaryPtr = VmGetHeap (rqptr, 32+1);
   FaoToBuffer (rqptr->rqResponse.MultipartBoundaryPtr, 32+1, &Length,
                "!32&H", &rqptr->Md5HashPath);

   /* calculate the content-length */
   RangeContentLength = 0;
   for (idx = 0; idx < rbptr->Count; idx++)
   {
      FaoToBuffer (Buffer, sizeof(Buffer), &Length,
"!AZ--!AZ\r\n\
Content-Type: !AZ\r\n\
Range: bytes !@SQ-!@SQ/!@SQ\r\n\
\r\n",
                   idx ? "\r\n" : "",
                   rqptr->rqResponse.MultipartBoundaryPtr, ContentTypePtr,
                   &rbptr->First[idx], &rbptr->Last[idx], &ContentLength);
      RangeContentLength += Length + rbptr->Last[idx] - rbptr->First[idx] + 1;
   }
   RangeContentLength += 32 + 8;

   FaoToBuffer (Buffer, sizeof(Buffer), NULL,
                "multipart/byteranges; boundary=!AZ",
                rqptr->rqResponse.MultipartBoundaryPtr);
   ResponseHeader (rqptr, 206, Buffer, RangeContentLength,
                   RdtTime64Ptr, NULL);

   return (true);
} 

/*****************************************************************************/
/*
End of file transfer, successful or otherwise.
*/

FileEnd (REQUEST_STRUCT *rqptr)

{
   int  status;
   FILE_QIO  *fqptr;
   FILE_TASK  *tkptr;
   FILE_CONTENT  *fcptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileEnd() !&F !&A", &FileEnd, rqptr->FileTaskPtr);

   tkptr = rqptr->FileTaskPtr;

   fqptr = &tkptr->FileOds.FileQio;

   if (rqptr->DeleteOnClose && (fqptr->AcpChannel || fqptr->QioChannel))
   {
      /* delete-on-close happens VERY infrequently */
      sys$setprv (1, &SysPrvMask, 0, 0);
      status = sys$qiow (EfnWait,
                         fqptr->AcpChannel ? fqptr->AcpChannel :
                                             fqptr->QioChannel,
                         IO$_DELETE | IO$M_DELETE,
                         &fqptr->IOsb, 0, 0,
                         &fqptr->FibDsc, &fqptr->FileNameDsc, 0, 0, 0, 0);
      sys$setprv (0, &SysPrvMask, 0, 0);
      if (WATCHMOD (rqptr, WATCH_MOD_FILE))
         WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "IO$_DELETE !&S !&S",
                    status, fqptr->IOsb.Status);
      if (VMSok (status)) status = fqptr->IOsb.Status;
      if (VMSnok (status)) ErrorNoticed (rqptr, status, NULL, FI_LI);
   }

   if (fqptr->AcpChannel)
   {
      /* the file has only had it's attributes read */
      sys$dassgn (fqptr->AcpChannel);
      fqptr->AcpChannel = 0;
   }
   else
   if (fqptr->QioChannel)
   {
      /* file has been accessed (contents 'open'ed and read) */
      status = sys$qiow (EfnWait, fqptr->QioChannel, IO$_DEACCESS,
                         &fqptr->IOsb, 0, 0,
                         &fqptr->FibDsc, 0, 0, 0, 0, 0);
      if (WATCHMOD (rqptr, WATCH_MOD_FILE))
         WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "IO$_DEACCESS !&S !&S",
                    status, fqptr->IOsb.Status);
      if (fqptr->IOsb.Status && VMSnok (fqptr->IOsb.Status))
         ErrorNoticed (rqptr, fqptr->IOsb.Status, NULL, FI_LI);
      if (VMSnok (status))
         ErrorNoticed (rqptr, status, NULL, FI_LI);

      sys$dassgn (fqptr->QioChannel);
      fqptr->QioChannel = 0;
   }

   /* if the file was being cached at the same time */
   if (rqptr->rqCache.LoadFromFile)
   {
      if (rqptr->FileContentPtr)
      {
         /* copy from the cache buffer */
         fcptr = rqptr->FileContentPtr;
         if (fcptr->ContentLength <= rqptr->rqCache.ContentLength)
         {
            memcpy (fcptr->ContentPtr,
                    rqptr->rqCache.ContentPtr,
                    rqptr->rqCache.ContentLength);
            fcptr->ContentLength = rqptr->rqCache.ContentLength;
            /* null terminate, it's usually text! */
            fcptr->ContentPtr[fcptr->ContentLength] = '\0';
         }
      }

      CacheLoadEnd (rqptr);
   }

   /* release internal RMS parse structures */
   OdsParseRelease (&tkptr->FileOds);

#if WATCH_MOD
   HttpdCheckPriv (FI_LI);
#endif /* WATCH_MOD */

   if (tkptr->NoSuchFileFunction)
   {
      /* file could not have been found, declare the appropriate handler */
      SysDclAst (tkptr->NoSuchFileFunction, rqptr);
   }
   else
   if (rqptr->FileContentPtr)
   {
      /* next task gets control once the file has been content-handled */
      rqptr->FileContentPtr->NextTaskFunction = tkptr->NextTaskFunction;

      /* file contents loaded, now process using the specified handler */
      SysDclAst (rqptr->FileContentPtr->ContentHandlerFunction, rqptr);
      rqptr->FileContentPtr->ContentHandlerFunction = NULL;
   }
   else
   if (tkptr->PreTagEndFileContents)
   {
      /* success, encapsulating a file, add the end tag */
      NetWriteBuffered (rqptr, tkptr->NextTaskFunction, "</pre>\n", 7);
   }
   else
   {
      /* success, declare the next task */
      SysDclAst (tkptr->NextTaskFunction, rqptr);
   }

   rqptr->FileTaskPtr = NULL;

   VmFreeFromHeap (rqptr, tkptr, FI_LI);
} 

/*****************************************************************************/
/*
The file access $QIO (the 'open') has completed.  Check status.
Ensure any output buffer contents are flushed.
*/

FileAccessAst (REQUEST_STRUCT *rqptr)

{
   int  status;
   FILE_QIO  *fqptr;
   FILE_TASK  *tkptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileAccessAst() !&F !UL !UL !&S",
                 &FileAccessAst, STR_DSC_LEN(&rqptr->NetWriteBufferDsc),
                 rqptr->FileTaskPtr->FileOds.FileQio.IOsb.Count,
                 rqptr->FileTaskPtr->FileOds.FileQio.IOsb.Status);

   tkptr = rqptr->FileTaskPtr;
   fqptr = &tkptr->FileOds.FileQio;

   if (VMSnok (fqptr->IOsb.Status))
   {
      if (rqptr->rqCache.LoadFromFile)
         rqptr->rqCache.LoadStatus = fqptr->IOsb.Status;
      rqptr->rqResponse.ErrorTextPtr = MapVmsPath (tkptr->FileName, rqptr);
      rqptr->rqResponse.ErrorOtherTextPtr = tkptr->FileName;
      ErrorVmsStatus (rqptr, fqptr->IOsb.Status, FI_LI);
      FileEnd (rqptr);
      return;
   }

   /* the file access is on the channel originally assigned for ACP-QIO */
   fqptr->QioChannel = fqptr->AcpChannel;
   fqptr->AcpChannel = 0;

   if (tkptr->PreTagFileContents)
   {
      tkptr->PreTagEndFileContents = true;
      NetWriteBuffered (rqptr, FileNextBlocks, "<pre>", 5);
      return;
   }

   FileNextBlocks (rqptr);
}

/*****************************************************************************/
/*
QIO a read of blocks from the file.  When the read completes call
FileNextBlocksAst() function to post-process the read and/or send the data to
the client.  Don't bother to test any status here, the AST routine will do
that!
*/ 

FileNextBlocks (REQUEST_STRUCT *rqptr)

{
   int  idx, status;
   ushort  Length;
   uint  BufferSize;
   ulong  EmulBlock;
   char  *bptr;
   FILE_CONTENT  *fcptr;
   FILE_QIO  *fqptr;
   FILE_TASK  *tkptr;
   RANGE_BYTE  *rbptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                "FileNextBlocks() !&F !&S !UL !&B",
                 FileNextBlocks, rqptr->NetIoPtr->WriteStatus,
                 rqptr->FileTaskPtr->FileOds.FileQio.BlockNumber,
                 rqptr->FileTaskPtr->FileOds.FileQio.EndOfFile);

   if (rqptr->RequestState >= REQUEST_STATE_ABORT)
   {
      FileEnd (rqptr);
      return;
   }

   tkptr = rqptr->FileTaskPtr;
   fcptr = rqptr->FileContentPtr;
   fqptr = &tkptr->FileOds.FileQio;

   if (!fcptr && VMSnok (rqptr->NetIoPtr->WriteStatus))
   {
      /* network write has failed (delivered via AST), bail out now */
      FileEnd (rqptr);
      return;
   }

   if (fqptr->EndOfFile)
   {
      /* calculated EOF */
      fqptr->EndOfFile = false;
      fqptr->IOsb.Status = SS$_ENDOFFILE;
      SysDclAst (FileNextBlocksAst, rqptr);
      return;
   }

   if (!fqptr->BlockNumber)
   {
      /**************/
      /* initialize */
      /**************/

      if (STR_DSC_LEN(&rqptr->NetWriteBufferDsc))
      {
         /* need exclusive use, flush the current contents */
         NetWriteFullFlush (rqptr, FileNextBlocks);
         return;
      }

      /* ensure we have an initialised buffer */
      if ((ulong)OutputFileBufferSize > fqptr->SizeInBytes64)
      {
         /* make buffer size a multiple of 512 byte blocks (for block I/O) */
         BufferSize = fqptr->SizeInBytes64 / 512;
         if (fqptr->SizeInBytes64 % 512) BufferSize++;
         BufferSize *= 512;
      }
      else
      if (OutputFileBufferSize)
         BufferSize = OutputFileBufferSize;
      else
         BufferSize = 65023;
      StrDscIfNotBegin (rqptr, &rqptr->NetWriteBufferDsc, BufferSize);

      fqptr->BlockNumber = 1;

      if (rqptr->rqHeader.RangeBytePtr &&
          rqptr->rqHeader.RangeBytePtr->Count)
      {
         /* returning a byte range within the file (partial content) */
         rbptr = rqptr->rqHeader.RangeBytePtr;
         idx = rbptr->Index;
         fqptr->BlockNumber += rbptr->First[idx] / 512;
         rbptr->Offset = rbptr->First[idx] % 512;
         rbptr->Length = rbptr->Last[idx] - rbptr->First[idx] + 1;
         rbptr->Remaining = rbptr->Length;

         if (rbptr->Count > 1)
         {
            /* returning 'multipart/byteranges' range content */
            char Buffer [256];
            FaoToBuffer (Buffer, sizeof(Buffer), &Length,
"!AZ--!AZ\r\n\
Content-Type: !AZ\r\n\
Range: bytes !@SQ-!@SQ/!@SQ\r\n\
\r\n",
                         rbptr->Index ? "\r\n" : "",
                         rqptr->rqResponse.MultipartBoundaryPtr,
                         tkptr->ContentTypePtr,
                         &rbptr->First[idx], &rbptr->Last[idx],
                         &fqptr->SizeInBytes64);
            /* synchronous network write (just for the convenience of it!) */
            NetWrite (rqptr, NULL, Buffer, Length);
         }

         if (WATCHMOD (rqptr, WATCH_MOD_FILE))
            WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                       "range !UL-!UL(!UL) vbn:!UL off:!UL rem:!UL",
                       rbptr->First[idx], rbptr->Last[idx],
                       rbptr->Length, fqptr->BlockNumber,
                       rbptr->Offset, rbptr->Remaining);
      }

      if (fcptr)
      {
         /* file content buffer (perhaps concurrently with cache buffer) */
         fcptr->CurrentPtr = fcptr->ContentPtr;
         fcptr->ContentRemaining = fcptr->ContentSize;
         fcptr->ContentLength = 0;
      }
      else
      if (!rqptr->rqCache.LoadFromFile)
      {
         /* initialize output buffer */
         if (OutputFileBufferSize > fqptr->SizeInBytes64)
         {
            /* make buffer size a multiple of 512 byte blocks (for block I/O) */
            BufferSize = (fqptr->SizeInBytes64 / 512);
            if (fqptr->SizeInBytes64 % 512) BufferSize++;
            BufferSize *= 512;
         }
         else
         if (OutputFileBufferSize)
            BufferSize = OutputFileBufferSize;
         else
            BufferSize = 65023;
         StrDscIfNotBegin (rqptr, &rqptr->NetWriteBufferDsc, BufferSize);
      }

      if (!fqptr->SizeInBytes64)
      {
         /* empty file */
         fqptr->IOsb.Status = SS$_ENDOFFILE;
         SysDclAst (FileNextBlocksAst, rqptr);
         return;
      }
   }
   else
   {
      /********************/
      /* subsequent reads */
      /********************/

      fqptr->BlockNumber += fqptr->BufferSize >> 9;
   }

   /**************/
   /* queue read */
   /**************/

   if (rqptr->rqCache.LoadFromFile)
   {
      /* populating a cache buffer, load it progressively */
      fqptr->BufferPtr = rqptr->rqCache.CurrentPtr;
      if (rqptr->rqCache.ContentRemaining > 0xfe00)
         BufferSize = 0xfe00;
      else
      {
         BufferSize = rqptr->rqCache.ContentRemaining;
         fqptr->EndOfFile = true;
      }
   }
   else
   if (fcptr)
   {
      /* populating a file contents buffer, load it progressively */
      fqptr->BufferPtr = fcptr->CurrentPtr;
      if (fcptr->ContentRemaining > 0xfe00)
         BufferSize = 0xfe00;
      else
      {
         BufferSize = fcptr->ContentRemaining;
         fqptr->EndOfFile = true;
      }
   }
   else
   {
      /* standard output buffer, make it a round number of 512 byte blocks */
      BufferSize = STR_DSC_SIZE(&rqptr->NetWriteBufferDsc) & 0xfe00;
      fqptr->BufferPtr = STR_DSC_PTR(&rqptr->NetWriteBufferDsc);
   }

   /* adjust the buffer size for where we have been told the EOF to be */
   if (((fqptr->BlockNumber-1)<<9) + BufferSize > fqptr->SizeInBytes64)
   {
      BufferSize = fqptr->SizeInBytes64 - ((fqptr->BlockNumber-1)<<9);
      fqptr->EndOfFile = true;
   }
   fqptr->BufferSize = BufferSize;

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "BufferSize:!UL", BufferSize);

   /*
      Documented in the VMS I/O Users Guide section entitled "Disk Function
      Codes" ... "P2--The number of bytes that are to be read from the disk,
      or written from memory to the disk. An even number must be specified if
      the controller is an RK611, RL11, RX211, or UDA50".
      Well, make sure we ask for an even number of bytes!
      Thanks to Dave Holland for demonstrating this bug on SIMH, and to
      Mark Pizzolato from the SIMH mailing list for pointing out the above.
   */
   if (BufferSize & 1)
   {
      BufferSize++;
      fqptr->AdjustBuffer = 1;
   }
   else
      fqptr->AdjustBuffer = 0;

   bptr = fqptr->BufferPtr;
   if (fqptr->RecAttr.fat$b_rtype == FAT$C_VARIABLE ||
       fqptr->RecAttr.fat$b_rtype == FAT$C_VFC)
   {
      /*
         Records in VARiable format files need to be word aligned.
         If this is on an odd boundary then push the buffer forward one
         byte in the storage ('elbow-room' in memory allocation means this
         is not an issue).  FileVariableRecord() detects and undoes this.
         This should only be necessary when when using a cache or content
         buffer as the FileVariableRecord() processing can create odd
         numbers of bytes when turning them into stream content.
      */
      if ((int)bptr & 1) bptr++;
   }

   status = sys$qio (EfnNoWait, fqptr->QioChannel,
                     IO$_READVBLK, &fqptr->IOsb,
                     FileNextBlocksAst, rqptr,
                     bptr, BufferSize, fqptr->BlockNumber,
                     0, 0, 0);
   if (VMSok (status)) return;

   /* let the AST routine handle it! */
   fqptr->IOsb.Status = status;
   SysDclAst (FileNextBlocksAst, rqptr);
}

/*****************************************************************************/
/*
The QIO read of blocks from the file has completed.  Post-process and/or queue
a network write to the client.  When the network  write completes it will call
the function FileNextBlocks() to queue a read  of the next series of blocks.
*/ 

FileNextBlocksAst (REQUEST_STRUCT *rqptr)

{
   int  status, bcnt;
   char  *bptr, *cptr, *sptr, *zptr;
   FILE_CONTENT  *fcptr;
   FILE_TASK  *tkptr;
   FILE_QIO  *fqptr;
   RANGE_BYTE  *rbptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
   {
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
"FileNextBlocksAst() !&F AcpIOsb.Status:!&S \
QioBlockNumber:!UL QioBufferSize:!UL IOsb.Count:!UL end:!&B %!&M",
                 FileNextBlocksAst,
                 rqptr->FileTaskPtr->FileOds.FileQio.IOsb.Status,
                 rqptr->FileTaskPtr->FileOds.FileQio.BlockNumber,
                 rqptr->FileTaskPtr->FileOds.FileQio.BufferSize,
                 rqptr->FileTaskPtr->FileOds.FileQio.IOsb.Count,
                 rqptr->FileTaskPtr->FileOds.FileQio.EndOfFile,
                 rqptr->FileTaskPtr->FileOds.FileQio.IOsb.Status);
/****** +++++
      if (VMSok (rqptr->FileTaskPtr->FileOds.FileQio.IOsb.Status))
         WatchDataDump (rqptr->FileTaskPtr->FileOds.FileQio.BufferPtr,
                        rqptr->FileTaskPtr->FileOds.FileQio.IOsb.Count);
*****/
   }

   if (rqptr->RequestState >= REQUEST_STATE_ABORT)
   {
      FileEnd (rqptr);
      return;
   }

   tkptr = rqptr->FileTaskPtr;
   fcptr = rqptr->FileContentPtr;
   fqptr = &tkptr->FileOds.FileQio;

   tkptr->NextBlocksTickSecond = HttpdTickSecond;

   if (VMSnok (fqptr->IOsb.Status))
   {
      if (fqptr->IOsb.Status == SS$_ENDOFFILE)
      {
         /***************/
         /* end-of-file */
         /***************/

         if (rqptr->rqHeader.RangeBytePtr &&
             rqptr->rqHeader.RangeBytePtr->Count)
         {
            /* transfering byte-range(s) */
            rqptr->rqHeader.RangeBytePtr->Index++;
            if (rqptr->rqHeader.RangeBytePtr->Index <
                rqptr->rqHeader.RangeBytePtr->Count)
            {
               /* multiple byte ranges, restart with next range */
               fqptr->BlockNumber = 0;
               SysDclAst (FileNextBlocks, rqptr);
               return;
            }
            else
            if (rqptr->rqHeader.RangeBytePtr->Count > 1)
            {
               /* end of multiple byte ranges, provide final boundary */
               char Buffer [64];
               zptr = (sptr = Buffer) + sizeof(Buffer)-1;
               for (cptr = "\r\n--"; *cptr && sptr < zptr; *sptr++ = *cptr++);
               for (cptr = rqptr->rqResponse.MultipartBoundaryPtr;
                    *cptr && sptr < zptr;
                    *sptr++ = *cptr++);
               for (cptr = "--\r\n"; *cptr && sptr < zptr; *sptr++ = *cptr++);
               *sptr = '\0';
               /* synchronous network write (for the convenience of it!) */
               NetWrite (rqptr, NULL, Buffer, sptr-Buffer);
            }
         }
         else
         if (rqptr->rqCache.LoadFromFile)
            rqptr->rqCache.LoadStatus = fqptr->IOsb.Status;

         if (!fcptr)
         {
            /* reset the standard output buffer */
            StrDscNoContent (&rqptr->NetWriteBufferDsc);
         }

         FileEnd (rqptr);
         return;
      }

      /**************/
      /* read error */
      /**************/

      if (rqptr->rqCache.LoadFromFile)
         rqptr->rqCache.LoadStatus = fqptr->IOsb.Status;
      rqptr->rqResponse.ErrorTextPtr = MapVmsPath (tkptr->FileName, rqptr);
      rqptr->rqResponse.ErrorOtherTextPtr = tkptr->FileName;
      ErrorVmsStatus (rqptr, fqptr->IOsb.Status, FI_LI);
      FileEnd (rqptr);
      return;
   }

   /******************/
   /* process blocks */
   /******************/

   /* get the count from the I/O status block (adjusted as necessary) */
   fqptr->IOsb.Count -= fqptr->AdjustBuffer;
   fqptr->BufferCount = fqptr->IOsb.Count;

   /* if NOT transferring the content EXACTLY as it is on-disk */
   if (rqptr->rqPathSet.ResponseVarRecord != FILE_VAR_ASIS)
   {
      /* if not 'binary content' in the blocks then massage */
      if (fqptr->RecAttr.fat$b_rtype == FAT$C_VARIABLE ||
          fqptr->RecAttr.fat$b_rtype == FAT$C_VFC)
         FileVariableRecord (rqptr);
      else
      if (fqptr->RecAttr.fat$b_rtype == FAT$C_FIXED &&
          fqptr->RecAttr.fat$b_rattrib & FAT$M_NOSPAN)
         FileFixedRecordNoSpan (rqptr);
   }

   /* ensure leftover space in a reused buffer is zeroed (just to be tidy) */
   if (fqptr->BlockNumber > 1)
      memset (fqptr->BufferPtr + fqptr->BufferCount,
              0,
              fqptr->BufferSize - fqptr->BufferCount);

   if (rqptr->FileTaskPtr->EscapeHtml)
   {
      /* queue a network write to the client, AST to FileNextBlocks() */
      STR_DSC_LEN(&rqptr->NetWriteBufferDsc) = fqptr->BufferCount;

      FileWriteBufferEscapeHtml (rqptr, FileNextBlocks);
      return;
   }

   bptr = fqptr->BufferPtr;
   bcnt = fqptr->BufferCount;

   if (rqptr->rqCache.LoadFromFile)
   {
      /* populating a cache buffer, possibly also a file contents buffer */
      rqptr->rqCache.ContentRemaining -= bcnt;
      rqptr->rqCache.ContentLength += bcnt;
      rqptr->rqCache.CurrentPtr += bcnt;
      if (WATCHMOD (rqptr, WATCH_MOD_FILE))
         WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "!UL",
                    rqptr->rqCache.ContentRemaining);
      if (!rqptr->rqCache.ContentRemaining)
         fqptr->EndOfFile = true;
      else
      if (rqptr->rqCache.ContentRemaining < 0)
      {
         /*
            This is not supposed to happen!
            Possibly in between getting the file size and opening and
            reading this far the file has been extended or rewritten.
            Shouldn't happen often enough to be a worry!
         */
         rqptr->rqResponse.ErrorTextPtr = MapVmsPath (tkptr->FileName, rqptr);
         rqptr->rqResponse.ErrorOtherTextPtr = tkptr->FileName;
         ErrorVmsStatus (rqptr, RMS$_RTB, FI_LI);
         FileEnd (rqptr);
         return;
      }
      if (!fcptr)
      {
         /* not populating file content buffer, write cache buffer to client */
         NetWrite (rqptr, FileNextBlocks, bptr, bcnt);
         return;
      }
      /* filling the cache/contents buffer, just get more file data */
      SysDclAst (FileNextBlocks, rqptr);
      return;
   }

   if (fcptr)
   {
      /* populating a file contents buffer, but not a cache buffer */
      fcptr->ContentRemaining -= bcnt;
      fcptr->ContentLength += bcnt;
      fcptr->CurrentPtr += bcnt;
      /* just get more file data */
      SysDclAst (FileNextBlocks, rqptr);
      return;
   }

   if (rqptr->rqHeader.RangeBytePtr &&
       rqptr->rqHeader.RangeBytePtr->Remaining)
   {
      /* returning a byte-range within the file (partial content) */
      rbptr = rqptr->rqHeader.RangeBytePtr;
      if (rbptr->Offset)
      {
         /* first block read, first byte won't necessarily be at the start */
         bptr += rbptr->Offset;
         bcnt -= rbptr->Offset;                   
         rbptr->Offset = 0;
      }
      /* if the range is less than what was read then discard the rest */
      if (bcnt > rbptr->Remaining) bcnt = rbptr->Remaining;
      rbptr->Remaining -= bcnt;
      /* if no range remaining it's an effective EOF for block I/O! */
      if (!rbptr->Remaining) fqptr->EndOfFile = true;

      if (WATCHMOD (rqptr, WATCH_MOD_FILE))
         WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                    "range !UL-!UL(!UL) vbn:!UL off:!UL rem:!UL",
                    rbptr->First[rbptr->Index],
                    rbptr->Last[rbptr->Index],
                    rbptr->Length,
                    fqptr->BlockNumber,
                    rbptr->Offset,
                    rbptr->Remaining);
   }

   /* write raw/massaged data to client */
   if (VMSok (rqptr->NetIoPtr->WriteStatus))
      NetWrite (rqptr, FileNextBlocks, bptr, bcnt);
   else
      SysDclAst (FileNextBlocks, rqptr);
}

/*****************************************************************************/
/*
Create an in situ stream buffer of characters out of disk virtual block
variable-length records where each record is now terminated by

1)  the default - newline character (LF)
    (i.e. turn variable-length into stream-LF on the fly).

2)  with a SET path mapping

    i)  SET /path response=VAR=CRLF    terminate record with <CR><LF>
   ii)  SET /path response=VAR=LF      terminate record with <LF> (as default)
  iii)  SET /path response=VAR=NONE    do not add carriage control
   iv)  SET /path response=var         revert to default

The word (two byte) record size allows sufficient space to support the single
byte <LF> and two byte <CR><LF> carriage-control.

Handles block spanning and non-spanning, LSB and MSB record length word.  In
other words - all variations (hopefully!)
*/ 

FileVariableRecord (REQUEST_STRUCT *rqptr)

{
   BOOL  MsbRcw, NoSpan;
   int  bcnt;
   ushort  VfcSize, MaxRecordSize, rcnt, recnt;
   char  *bptr, *cptr, *sptr, *zptr;
   FILE_QIO  *fqptr;
   FILE_TASK  *tkptr;
                         
   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "FileVariableRecord()");

   tkptr = rqptr->FileTaskPtr;
   fqptr = &tkptr->FileOds.FileQio;

   bptr = fqptr->BufferPtr;
   bcnt = fqptr->BufferCount;
   MsbRcw = fqptr->RecAttr.fat$b_rattrib & FAT$M_MSBRCW;
   NoSpan = fqptr->RecAttr.fat$b_rattrib & FAT$M_NOSPAN;
   VfcSize = fqptr->RecAttr.fat$b_vfcsize;
   MaxRecordSize = fqptr->RecAttr.fat$w_maxrec;
   recnt = rcnt = fqptr->RecordCount;

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
"NoSpan:!&B MsbRcw:!&B VfcSize:!UL MaxRecordSize:!UL rcnt:!UL \
RecordSize:!UL aligned:!&B ResponseVarRecord:!UL",
                 NoSpan, MsbRcw, VfcSize, MaxRecordSize,
                 rcnt, fqptr->RecordSize, (int)bptr & 1,
                 rqptr->rqPathSet.ResponseVarRecord);

   if (WATCHING (rqptr, WATCH_RESPONSE))
   {
      if (!tkptr->WatchVarRecordCC)
      {
         tkptr->WatchVarRecordCC = true;
         switch (rqptr->rqPathSet.ResponseVarRecord)
         {
            case FILE_VAR_CRLF : cptr = "<CR><LF>"; break;
            case FILE_VAR_LF   : cptr = "<LF>"; break;
            case FILE_VAR_NONE : cptr = "NONE"; break;
            default : if (rqptr->rqPathSet.ResponseVarRecord)
                         cptr = "(unknown)";
                      else
                         cptr = "DEFAULT (<LF>)";
         }
         WatchThis (WATCHITM(rqptr), WATCH_RESPONSE,
                    "RFM:VAR carriage control !AZ", cptr);
      }
   }

   zptr = (cptr = sptr = bptr) + bcnt;
   /* see note about VAR record alignment in FileNextBlocks() above */
   if ((int)bptr & 1) cptr++;

   while (cptr < zptr)
   {
      if (WATCHMOD (rqptr, WATCH_MOD_FILE))
         if (rcnt || (cptr > sptr && cptr > bptr+1))
            WatchdataFormatted ("!5ZL |!#AZ|\n",
               rcnt, rcnt <= zptr-cptr ? rcnt : zptr-cptr, cptr);

      if (VfcSize && rcnt && rcnt == fqptr->RecordSize)
      {
         /* adjust for any fixed-length control on start of new record */
         cptr += VfcSize;
         rcnt -= VfcSize;
         if (!rcnt)
         {
            /* ensure this last record has carriage control */
            if (rqptr->rqPathSet.ResponseVarRecord == FILE_VAR_CRLF)
            {
               if (!recnt || (recnt >= 2 && *(USHORTPTR)(sptr-2) != '\r\n'))
               {
                  *(USHORTPTR)sptr = '\r\n';
                  sptr += 2;
               }
            }
            else
            if (rqptr->rqPathSet.ResponseVarRecord != FILE_VAR_NONE)
               if (!recnt || (recnt >= 1 && *(sptr-1) != '\n'))
                  *sptr++ = '\n';
         }
      }

      /* copy the contents of this record */
      while (rcnt && cptr < zptr)
      {
         rcnt--;
         *sptr++ = *cptr++;
      }

      /* if no data left in the buffer */
      if (cptr >= zptr)
      {
         /* if still some data to go in this record */
         if (rcnt) break;

         /* ensure this last record has carriage control */
         if (rqptr->rqPathSet.ResponseVarRecord == FILE_VAR_CRLF)
         {
            if (!recnt || (recnt >= 2 && *(USHORTPTR)(sptr-2) != '\r\n'))
            {
               *(USHORTPTR)sptr = '\r\n';
               sptr += 2;
            }
         }
         else
         if (rqptr->rqPathSet.ResponseVarRecord != FILE_VAR_NONE)
            if (!recnt || (recnt >= 1 && *(sptr-1) != '\n'))
               *sptr++ = '\n';

         break;
      }

      /* step to an even byte boundary */
      if ((int)cptr & 1) cptr++;

      /* if no-span and insufficient space for a record left in this block */
      if (NoSpan && 510 - ((int)cptr & 0x1ff) < MaxRecordSize)
      {
         /* step to the start of the next block */
         cptr = ((int)cptr & ~0x1ff) + 512;
      }

      /* if no data left in the buffer */
      if (cptr >= zptr)
      {
         /* ensure this previous record has carriage control */
         if (rqptr->rqPathSet.ResponseVarRecord == FILE_VAR_CRLF)
         {
            if (!recnt || (recnt >= 2 && *(USHORTPTR)(sptr-2) != '\r\n'))
            {
               *(USHORTPTR)sptr = '\r\n';
               sptr += 2;
            }
         }
         else
         if (rqptr->rqPathSet.ResponseVarRecord != FILE_VAR_NONE)
            if (!recnt || (recnt >= 1 && *(sptr-1) != '\n'))
               *sptr++ = '\n';

         break;
      }

      /* get the new record count */
      rcnt = *(USHORTPTR)cptr;

      /* if MSB then swap the bytes */
      if (MsbRcw) rcnt = (rcnt >> 8) | (rcnt << 8);
      cptr += sizeof(ushort);

      /* note the current record count */
      recnt = rcnt;

      /* note the original size of this record */
      fqptr->RecordSize = rcnt;

      /* ensure previous record has carriage control (after count retrieved!) */
      if (sptr > bptr)
      {
         if (rqptr->rqPathSet.ResponseVarRecord == FILE_VAR_CRLF)
         {
            if (!recnt || (recnt >= 2 && *(USHORTPTR)(sptr-2) != '\r\n'))
            {
               *(USHORTPTR)sptr = '\r\n';
               sptr += 2;
            }
         }
         else
         if (rqptr->rqPathSet.ResponseVarRecord != FILE_VAR_NONE)
            if (!recnt || (recnt >= 1 && *(sptr-1) != '\n'))
               *sptr++ = '\n';
      }

      if (!rcnt && !VfcSize)
      {
         if (rqptr->rqPathSet.ResponseVarRecord == FILE_VAR_CRLF)
         {
            if (!recnt || (recnt >= 2 && *(USHORTPTR)(sptr-2) != '\r\n'))
            {
               *(USHORTPTR)sptr = '\r\n';
               sptr += 2;
            }
         }
         else
         if (rqptr->rqPathSet.ResponseVarRecord != FILE_VAR_NONE)
            if (!recnt || (recnt >= 1 && *(sptr-1) != '\n'))
               *sptr++ = '\n';
      }
   }

   /* zero any remainder from the move */
   if (zptr - sptr > 0) memset (sptr, 0, zptr - sptr);

   fqptr->RecordCount = rcnt;
   fqptr->BufferCount = sptr - bptr;

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "rcnt:!UL bcnt:!UL now:!UL", rcnt, bcnt, sptr - bptr);
}

/*****************************************************************************/
/*
Fixed length records where the record is not allowed to span the blocks (who
the hell uses these formats anyway?!)  Fixed length block spanning records are
just handled in the blocks.  No adjustments to carriage control!
*/ 

FileFixedRecordNoSpan (REQUEST_STRUCT *rqptr)

{
   int  bcnt;
   ushort  MaxRecordSize, rcnt;
   char  *bptr, *cptr, *sptr, *zptr;
   FILE_QIO  *fqptr;
   FILE_TASK  *tkptr;
                         
   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE, "FileFixedRecordNoSpan()");

   tkptr = rqptr->FileTaskPtr;
   fqptr = &tkptr->FileOds.FileQio;

   bptr = fqptr->BufferPtr;
   bcnt = fqptr->BufferCount;
   MaxRecordSize = fqptr->RecAttr.fat$w_maxrec;
   rcnt = fqptr->RecordCount;

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "MaxRecordSize:!UL rcnt:!UL", MaxRecordSize, rcnt);

   zptr = (cptr = sptr = bptr) + bcnt;
   while (cptr < zptr)
   {
      if (WATCHMOD (rqptr, WATCH_MOD_FILE))
         if (rcnt || cptr > sptr)
            WatchdataFormatted ("!5ZL |!#AZ|\n",
               rcnt, rcnt <= zptr-cptr ? rcnt : zptr-cptr, cptr);

      while (rcnt && cptr < zptr)
      {
         rcnt--;
         *sptr++ = *cptr++;
      }
      if (cptr >= zptr) break;
      /* if insufficient space for a record left in this block */
      if (510 - ((int)cptr & 0x1ff) < MaxRecordSize)
      {
         /* step to the start of the next block */
         cptr = ((int)cptr & ~0x1ff) + 512;
         if (cptr >= zptr) break;
      }
      rcnt = fqptr->RecAttr.fat$w_rsize;
   }

   fqptr->RecordCount = rcnt;
   fqptr->BufferCount = sptr - bptr;

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "rcnt:!UL bcnt:!UL now:!UL", rcnt, bcnt, sptr - bptr);
}

/*****************************************************************************/
/*
Send the buffer contents escaping any HTML-forbidden characters.
*/ 

FileWriteBufferEscapeHtml
(
REQUEST_STRUCT *rqptr,
REQUEST_AST AstFunction
)
{
   FILE_TASK  *tkptr;
                         
   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD_FILE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_FILE,
                 "FileWriteBufferEscapeHtml() !&A !UL",
                 AstFunction, STR_DSC_LEN(&rqptr->NetWriteBufferDsc));

   tkptr = rqptr->FileTaskPtr;

   /* ensure the HTML descriptor is initialised */
   StrDscIfNotBegin (rqptr, &tkptr->BufferDsc,
                     STR_DSC_SIZE(&rqptr->NetWriteBufferDsc));

   /* swap the file read descriptor and the HTML descriptor */
   StrDscSwap (&rqptr->NetWriteBufferDsc, &tkptr->BufferDsc);

   /* reset the (now) output descriptor */
   StrDscNoContent (&rqptr->NetWriteBufferDsc);

   /* copy as HTML into the output descriptor */
   StrDscBuildHtmlEscape (&rqptr->NetWriteBufferDsc, &tkptr->BufferDsc, NULL);

   /* write it out */
   NetWriteStrDsc (rqptr, AstFunction);
}

/*****************************************************************************/
/*
The opaque entity identifier is generated from the six byte file-id plus the
four byte revision date-time.  In this way if the file is moved or revised the
entity should reflect that.  Assumes the supplied string is large enough to
store the generated entity tag (29 bytes).
*/ 

FileGenerateEntityTag
(
char *EntityString,
FILE_QIO *fqptr
)
{
   /* hex digits: 12 for FID, 16 for RDT */
   static $DESCRIPTOR (EntityTagFaoDsc, "!4XL!4XL!4XL!8XL!8XL\0");
   static $DESCRIPTOR (EntityTagDsc, "");

   ulong  *qptr;
   char  *cptr;
                         
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_FILE))
      WatchThis (WATCHALL, WATCH_MOD_FILE, "FileGenerateEntityTag()");

   EntityTagDsc.dsc$w_length = 29;
   EntityTagDsc.dsc$a_pointer = EntityString;

   qptr = &fqptr->RdtTime64;
   sys$fao (&EntityTagFaoDsc, NULL, &EntityTagDsc,
            fqptr->Fib.fib$w_fid[0], fqptr->Fib.fib$w_fid[1],
            fqptr->Fib.fib$w_fid[2], qptr[0], qptr[1]);
   for (cptr = EntityString; *cptr; cptr++) *cptr = TOLO(*cptr);

   if (WATCH_MODULE(WATCH_MOD_FILE))
      WatchThis (WATCHALL, WATCH_MOD_FILE, "!AZ", EntityString);
}

/*****************************************************************************/