[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]
/*****************************************************************************/
/*
                                WebSock(et).c

WebSocket protocol based on the RFC published December 2011 below:

  https://tools.ietf.org/html/rfc6455
  https://datatracker.ietf.org/doc/rfc6455/
  https://www.rfc-editor.org/rfc/rfc6455.txt

The WebSocket protocol is an emerging techncology that enables two-way
communication between a user agent running untrusted code  running in a
controlled environment to a remote host that has opted-in to communications
from that code.

  http://websocket.org/

This module deals exclusively with WebSocket scripting.  Also see DCL.C.

For WASD a WebSocket script is one that is activated in the same fashion as an
equivalent CGI/CGIplus/RTE and has an identical CGI environment (variables,
streams, etc.) but which uses a unique HTTP response and communicates with its
client using the WebSocket protocol.

Firstly, the WebSocket connection is an asynchronous, bi-directional channel. 
WASD already implements this for its scripts.  The major difference is the use
of dedicated channels (mailboxes) for the WebSocket IPC.  Client supplied data
is available to the script via the WEBSOCKET_INPUT mailbox and data from the
script supplied via the WEBSOCKET_OUTPUT mailbox (indicated via CGI variables).
Communication using a WebSocket requires the use of a framing protocol while
WEBSOCKET_INPUT and WEBSOCKET_OUTPUT are opaque octet-streams pushing stream
processing onto the script (which best knows what it is trying to accomplish
anyway).  CGI variables WEBSOCKET_INPUT_MRS and WEBSOCKET_OUTPUT_MRS indicate
the respective mailbox capacity.

Secondly, the WASD server just acts as a conduit for the WebSocket client
octet-stream.  It is up to the server application (script) to perform all of
the protocol framing, etc.  In WASD's case this is largely available through
the wsLIB.c library.

Long-lived WebSocket scripts by default have timeouts and other limits set to
infinite.  If control is required it must be exercised using the appropriate
mapping SETings or DCL callouts.


MULTI-CLIENT SCRIPT PROCESSES
-----------------------------
A WebSocket connection to a script is maintained by the WEBSOCKET_INPUT and
WEBSOCKET_OUTPUT channels remaining connected to the script.  If the script
closes them (or the image or process exits, etc.) the WebSocket connection is
closed.  WebSocket requests are maintained as long as the script maintains
them, for a CGIplus script, until it exits.  If a CGIplus script requires to
disconnect from a WebSocket client without exiting it must do so explicitly (by
closing C streams, deassigning channels, etc.)

Of course this is an advantage because it allows a single CGIplus script to
maintain connections with multiple WebSocket clients.  Provided the script
remains connected to the WebSocket IPC mailboxes and processes that I/O
asynchronously (via ASTs or POSIX Threads for example) a single script can
concurrently handle multiple clients.  The script just processes each request
it is given, adding the new client to the existing group (and removing them as
the IPC indicates they disconnect).

Obviously the script must remain resident via CGIplus or RTE.

The server will continue to provide requests to the script for as long as it
appears idle (i.e. the sentinal EOF is returned even though concurrent
processing may continue).  Obviously a single scripting process cannot accept
an unlimited number of concurrent WebSockets.  When a script decides it can
process no more it should not return the sentinal EOF from the most recent
request until it is in a position to process more, when it then provides the
EOF and the server again will supply another request.

The original request is access logged at request run-down (when the WebSocket
is finally closed either because the client disconnected or the script closed
its connection to the WEBSOCKET_.. mailboxes).  The access log status is 101
(Switching Protocols) and the bytes rx and tx reflect the total for the
duration.


PROTOCOL VERSION SUPPORT
------------------------
Normally WASD supports the current base protocol number and any higher.  At
some time in the future it may be necessary to limit that down to a supported
version number or set of numbers.  Defining WASD_WEBSOCKET_VERSION to be one or
more comma-separated numbers will limit the supported protocol versions.  For
example

  $ DEFINE /SYSTEM WASD_WEBSOCKET_VERSION "10, 9, 8"

limits requests to protocol version 10 (current), 9 (earlier) and 8 (earliest). 
Logical name is only tested  once for each server startup (the first WebSocket
request received).  This logical name only controls server handshake support
and behaviour.  The underlying WebSocket libarary used by the application (e.g.
wsLIB.c) supports version idiosyncracies for other aspects.

This string is also used as the list of versions reported in a 426 (upgrade
required) response when the requested version is not supported.


WEBSOCKET THROTTLING
--------------------
Throttle mapping rules may be applied to WebSocket requests.  There is however,
a FUNDAMENTAL DIFFERENCE between request throttling and WebSocket throttling
though.  HTTP request throttling applies control to the entire life of the
response.  WebSocket throttling applies only to establishing connection to the
underlying script/application.  Once the script responds to accept the
connection (status 101) or reject it (error status) throttling is concluded.

Long-lived WebSocket connections are considered less suitable to full
life-cycle throttling and should use internal mechanisms to control resource
utilisation (i.e. using the delayed sentinal EOF mechanism described above). 
Essentially it is used to limit the impact concurrent requests have on the
number of supporting script processes allowed to be instantiated to support the
application.

For example, the rule

  set /cgi-bin/ws_application throttle=1

will only allow one new request at a time attempt to connect to and/or create a
WebSocket application script.  This will effectively limit the number of
supporting processes to one however many clients wish to connect.

To support concurrent requests distributed across multiple application scripts
specify the throttle value as the number of separate scripts

  set /cgi-bin/ws_application throttle=5

and if each script is to support a maximum number of individual connections
then have it delay the EOF sentinal (described above) to block the server
selecting it for the next request.  Requests will be allocated until all
processes have blocked after which they will be queued.

To return a "too busy" 503 to clients (almost) immediately upon all processes
become full and blocking (maximum application concurrency has been reached)
then set the 't/o-busy' value to 1 second. 

  set /cgi-bin/ws_application throttle=5,,,,,1


RAW [WEB]SOCKET
---------------
A WASD variant on the WebSocket theme is the "raw" socket.  This is NOT really
a WebSocket at all but does use the WASD WebSocket script processing
infrastructure to process otherwise network communication.  This activates a
script which operates in the CGI(plus) request environment.  The request detail
is available using CGI variables.  A "100 Continue" response header signals the
server the script is active but is NOT relayed to the client.  Once activated,
only the raw network octets from the client are provided to the script, and
only the raw octets from the script are transmitted to the client.  The script
application can effectively perform asynchronous, bidirectional input/output
with the client.  Intended for communication with non-HTTP (non-WebSocket)
networking clients (e.g. telnet, SSH, IMAP, POP "server" scripts, and the
like).

The [ServiceRawSocket] directive makes a service process RawSockets.

  # WASD_CONFIG_SERVICE
  [[http:*:1234]]
  [ServiceRawSocket]  enabled

The script to be activated must then be mapped to the service.
 
  # WASD_CONFIG_MAP
  [[*:1234]]
  map * /cgiplus-bin/rawsocket.com



VERSION HISTORY
---------------
01-DEC-2016  MGD  "raw" WebSocket
16-SEP-2012  MGD  bugfix; WebSockEnd() do not NetCloseSocket()
12-DEC-2011  MGD  bugfix; WebSockCloseMailboxes() logic
                  http://www.rfc-editor.org/rfc/rfc6455.txt finally!
30-SEP-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-17
31-AUG-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-13
23-AUG-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-11
11-JUL-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-10
13-JUN-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-09
07-JUN-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-07 (BANG!)
25-FEB-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-06
05-FEB-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-05
11-JAN-2011  MGD  draft-ietf-hybi-thewebsocketprotocol-04
17-OCT-2010  MGD  draft-ietf-hybi-thewebsocketprotocol-03
24-SEP-2010  MGD  draft-ietf-hybi-thewebsocketprotocol-02
26-AUG-2010  MGD  draft-ietf-hybi-thewebsocketprotocol-01
26-JUN-2010  MGD  initial
*/
/*****************************************************************************/

#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 <errno.h>
#include <stdio.h>
#include <string.h>

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

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

#define WASD_MODULE "WEBSOCK"
#define FI_LI WASD_MODULE, __LINE__

#define CMB$M_READONLY 0x01
#define CMB$M_WRITEONLY 0x02

#define DVI$_DEVNAM 32
#define DVI$_UNIT 12

/* version(s) supported, current (most recent) first */
#define WEBSOCKET_VERSION "13, 8"

/* number of individual versions supported */
#define WEBSOCKET_VERSION_MAX 32

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

int  WebSockCurrent,
     WebSockOutputSize;

LIST_HEAD  WebSockList;

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

extern int  DclMailboxBytLmRequired,
            DclSysOutputSize,
            EfnNoWait,
            EfnWait,
            NetAcceptBytLmRequired,
            NetReadBufferSize;

extern int  ToLowerCase[],
            ToUpperCase[];

extern char  ErrorSanityCheck[];

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

/*****************************************************************************/
/*
Ensure the WebSocket request makes sense and can be supported.
Return TRUE if it does, else end the request and return FALSE if it doesn't.
*/ 

BOOL WebSockRequest (REQUEST_STRUCT *rqptr)

{
   static int  VersionCount;
   static int  VersionArray [WEBSOCKET_VERSION_MAX];
   /* times four as two digits plus one comma plus one space */
   static char  SecWebSocketVersion [25+(WEBSOCKET_VERSION_MAX*4)+1],
                WebSocketVersion [WEBSOCKET_VERSION_MAX*4];

   int  idx, status;
   char  *cptr;
   DICT_ENTRY_STRUCT  *denptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_RESPONSE))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL, "WebSockRequest()");

   if (rqptr->RawSocketRequest) return (true);

   if (!WebSocketVersion[0])
   {
      /* initialise */
      if (cptr = SysTrnLnm (WASD_WEBSOCKET_VERSION))
         strncpy (WebSocketVersion, cptr, sizeof(WebSocketVersion)-2);
      else
         strcpy (WebSocketVersion, WEBSOCKET_VERSION);
      VersionCount = 0;
      cptr = WebSocketVersion;
      while (*cptr && VersionCount < WEBSOCKET_VERSION_MAX)
      {
         VersionArray[VersionCount++] = atoi(cptr);
         while (isdigit(*cptr)) cptr++;
         while (*cptr && !isdigit(*cptr)) cptr++;
      }
      if (VersionCount >= WEBSOCKET_VERSION_MAX)
         ErrorExitVmsStatus (SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
      FaoToBuffer (SecWebSocketVersion, sizeof(SecWebSocketVersion), NULL,
                   "Sec-WebSocket-Version: !AZ\r\n", WebSocketVersion);
   }

   if (denptr = DictLookup (rqptr->rqDictPtr, DICT_TYPE_REQUEST,
                            "sec-websocket-key", 17))
      rqptr->rqHeader.SecWebSocketKeyPtr = DICT_GET_VALUE(denptr);

   if (denptr = DictLookup (rqptr->rqDictPtr, DICT_TYPE_REQUEST,
                            "sec-websocket-protocol", 22))
      rqptr->rqHeader.SecWebSocketProtocolPtr = DICT_GET_VALUE(denptr);

   if (denptr = DictLookup (rqptr->rqDictPtr, DICT_TYPE_REQUEST,
                            "sec-websocket-version", 21))
      rqptr->rqHeader.WebSocketVersion = atoi(DICT_GET_VALUE(denptr));

   if (!rqptr->rqHeader.SecWebSocketKeyPtr)
   {
      ErrorNoticed (rqptr, SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
      return (SS$_BUGCHECK);
   }

   if (rqptr->rqHeader.Method != HTTP_METHOD_GET ||
       !rqptr->rqHeader.SecWebSocketKeyPtr)
   {
      InstanceGblSecIncrLong (&AccountingPtr->RequestErrorCount);
      rqptr->rqResponse.HttpStatus = 400;
      ErrorGeneral (rqptr, MsgFor(rqptr,MSG_REQUEST_FORMAT), FI_LI);
      RequestEnd (rqptr);
      return (false);
   }

   for (idx = 0; idx < VersionCount; idx++)
      if (VersionArray[idx] == rqptr->rqHeader.WebSocketVersion) break;

   if (idx >= VersionCount)
   {
      /* version not supported */
      InstanceGblSecIncrLong (&AccountingPtr->RequestErrorCount);
      ResponseHeader (rqptr, 426, NULL, 0, NULL, SecWebSocketVersion);
      RequestEnd (rqptr);
      return (false);
   }

   InstanceGblSecIncrLong (&AccountingPtr->DoWebSockCount);

   return (true);
}

/****************************************************************************/
/*
Generate a WebSocket 101 Switching Protocols response.
*/

int WebSockSwitchResponse (REQUEST_STRUCT *rqptr)

{
   static char  WebSockHand [] =
"HTTP/1.1 101 Switching Protocols\r\n\
Upgrade: websocket\r\n\
Connection: Upgrade\r\n\
Sec-WebSocket-Accept: ";

   static char  WebSockExts [] = "\r\nSec-WebSocket-Extensions: ",
                WebSockProt [] = "\r\nSec-WebSocket-Protocol: ";

   int  AcceptBase64Length;
   char  *cptr, *sptr, *zptr;
   char  AcceptBase64 [48],
         Buffer [1024],
         KeyGuid [96];
   unsigned char  KeyGuidHash [20];
   SHA1Context  Sha1Ctx;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_RESPONSE,
                 "WebSockSwitchResponse()");

   /* no update to status code counters as this is an intermediate response */
   rqptr->rqResponse.HttpStatus = 101;
   rqptr->rqResponse.HeaderSent = true;

   /* if it's a WebSocket then by default do-not-disturb */
   if (rqptr->DclTaskPtr)
      if (rqptr->DclTaskPtr->LifeTimeSecond != DCL_DO_NOT_DISTURB)
         rqptr->DclTaskPtr->LifeTimeSecond = DCL_WEBSOCKET_DND;
   HttpdTimerSet (rqptr, TIMER_OUTPUT, -1);
   HttpdTimerSet (rqptr, TIMER_NOPROGRESS, -1);

   if (rqptr->RawSocketRequest) return (SS$_NORMAL);

   zptr = (sptr = KeyGuid) + sizeof(KeyGuid)-1;
   for (cptr = rqptr->rqHeader.SecWebSocketKeyPtr;
        *cptr && sptr < zptr;
        *sptr++ = *cptr++);
   for (cptr = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
        *cptr && sptr < zptr;
        *sptr++ = *cptr++);
   *sptr = '\0';

   SHA1Reset (&Sha1Ctx);
   SHA1Input (&Sha1Ctx, KeyGuid, sptr-KeyGuid);
   if (!SHA1Result (&Sha1Ctx))
      ErrorNoticed (rqptr, SS$_BUGCHECK, ErrorSanityCheck, FI_LI);
   /* copy into the hash buffer (converting from big to little endian) */
   SHA1LitEnd (&Sha1Ctx, KeyGuidHash);

   AcceptBase64Length = sizeof(AcceptBase64)-1;
   base64_encode (AcceptBase64, &AcceptBase64Length,
                  KeyGuidHash, sizeof(KeyGuidHash));

   zptr = (sptr = Buffer) + sizeof(Buffer)-1;

   for (cptr = WebSockHand; *cptr && sptr < zptr; *sptr++ = *cptr++);
   for (cptr = AcceptBase64; *cptr && sptr < zptr; *sptr++ = *cptr++);

   if (rqptr->rqHeader.SecWebSocketProtocolPtr)
   {
      for (cptr = WebSockProt; *cptr && sptr < zptr; *sptr++ = *cptr++);
      for (cptr = rqptr->rqHeader.SecWebSocketProtocolPtr;
           *cptr && sptr < zptr;
           *sptr++ = *cptr++);
   }

   for (cptr = "\r\n\r\n"; *cptr && sptr < zptr; *sptr++ = *cptr++);

   if (WATCHING (rqptr, WATCH_RESPONSE_HEADER))
   {
      WatchThis (WATCHITM(rqptr), WATCH_RESPONSE_HEADER,
                 "HEADER !UL bytes", sptr-Buffer);
      WatchData (Buffer, sptr-Buffer);
   }

   /* synchronous network write (just for the convenience of it!) */
   NetWrite (rqptr, NULL, Buffer, sptr-Buffer);

   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
These are emphemeral mailboxes and will be deleted as soon as there are no
channels assigned to them (with the loss of any data still buffered).
*/ 

WebSockCreateMailboxes (REQUEST_STRUCT *rqptr)

{
   static unsigned long  DevNamItem = DVI$_DEVNAM,
                         UnitItem = DVI$_UNIT;

   int  status;
   unsigned short  Length;
   unsigned long  BytLmAfter,
                  BytLmBefore,
                  LongUnit;
   struct RequestWebSocketStruct  *wsptr;
   struct dsc$descriptor_s  *dscptr;

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

   BytLmBefore = GetJpiBytLm ();

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
                 "WebSockCreateMailboxes() !UL !UL",
                 BytLmBefore, DclMailboxBytLmRequired);

   if (!WebSockOutputSize) WebSockOutputSize = DclSysOutputSize;

   /* ensure we're leaving enough BYTLM for client socket creation at least */
   if (DclMailboxBytLmRequired &&
       BytLmBefore - DclMailboxBytLmRequired <=
       NetAcceptBytLmRequired * Config.cfServer.ProcessMax)
   {
      ErrorNoticed (NULL, 0, "BYTLM exhausted", FI_LI);
      return (SS$_EXQUOTA);
   }

   wsptr = &rqptr->rqWebSocket;

   wsptr->RequestPtr = rqptr;

   ListAddHead (&WebSockList, wsptr, LIST_ENTRY_TYPE_WEBSOCKET);

   /***************************/
   /* WEBSOCKET_INPUT mailbox */
   /***************************/

   if (rqptr->rqPathSet.WebSocketInputSize)
      wsptr->InputSize = rqptr->rqPathSet.WebSocketInputSize;
   else
      wsptr->InputSize = NetReadBufferSize;
   if (wsptr->InputSize < WEBSOCKET_INPUT_MIN)
      wsptr->InputSize = WEBSOCKET_INPUT_MIN;

   if (VMSnok (status =
       sys$crembx (0,
                   &wsptr->InputChannel,
                   wsptr->InputSize, wsptr->InputSize,
                   DCL_PROCESS_MBX_PROT_MASK,
                   0, 0, CMB$M_WRITEONLY)))
      goto WebSockCreateMailboxesError;

   dscptr = &wsptr->InputDevNameDsc;
   dscptr->dsc$w_length = sizeof(wsptr->InputDevName);
   dscptr->dsc$a_pointer = wsptr->InputDevName;
   dscptr->dsc$b_class = DSC$K_CLASS_S;
   dscptr->dsc$b_dtype = DSC$K_DTYPE_T;

   if (VMSnok (status =
       lib$getdvi (&DevNamItem, &wsptr->InputChannel,
                   0, 0, &wsptr->InputDevNameDsc, &Length)))
      goto WebSockCreateMailboxesError;
   wsptr->InputDevName[dscptr->dsc$w_length = Length] = '\0';

   if (VMSnok (status =
       DclMailboxAcl (wsptr->InputDevName,
                      rqptr->DclTaskPtr->CrePrcUserName)))
      goto WebSockCreateMailboxesError;

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
                 "$CREMBX() WEBSOCKET_INPUT !AZ !UL",
                 wsptr->InputDevName, NetReadBufferSize);

   /****************************/
   /* WEBSOCKET_OUTPUT mailbox */
   /****************************/

   if (rqptr->rqPathSet.WebSocketOutputSize)
      wsptr->OutputSize = rqptr->rqPathSet.WebSocketOutputSize;
   else
      wsptr->OutputSize = WebSockOutputSize;
   if (wsptr->OutputSize < WEBSOCKET_OUTPUT_MIN)
      wsptr->OutputSize = WEBSOCKET_OUTPUT_MIN;

   wsptr->OutputPtr = VmGetHeap (rqptr, wsptr->OutputSize);

   if (VMSnok (status =
       sys$crembx (0,
                   &wsptr->OutputChannel,
                   wsptr->OutputSize, wsptr->OutputSize,
                   DCL_PROCESS_MBX_PROT_MASK,
                   0, 0, CMB$M_READONLY)))
      goto WebSockCreateMailboxesError;

   dscptr = &wsptr->OutputDevNameDsc;
   dscptr->dsc$w_length = sizeof(wsptr->OutputDevName);
   dscptr->dsc$a_pointer = wsptr->OutputDevName;
   dscptr->dsc$b_class = DSC$K_CLASS_S;
   dscptr->dsc$b_dtype = DSC$K_DTYPE_T;

   if (VMSnok (status =
       lib$getdvi (&DevNamItem, &wsptr->OutputChannel,
                   0, 0, &wsptr->OutputDevNameDsc, &Length)))
      goto WebSockCreateMailboxesError;
   wsptr->OutputDevName [dscptr->dsc$w_length = Length] = '\0';

   if (VMSnok (status =
       DclMailboxAcl (wsptr->OutputDevName,
                      rqptr->DclTaskPtr->CrePrcUserName)))
      goto WebSockCreateMailboxesError;

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
                 "$CREMBX() WEBSOCKET_OUTPUT !AZ !UL",
                 wsptr->OutputDevName, WebSockOutputSize);

   /******/
   /* OK */
   /******/

   if (WATCHING (rqptr, WATCH_DCL))
   {
      WatchThis (WATCHITM(rqptr), WATCH_DCL, "WEBSOCKET connected");
      WatchThis (WATCHITM(rqptr), WATCH_DCL, "MBX WEBSOCKET_INPUT !AZ size:!UL",
                 wsptr->InputDevName, wsptr->InputSize);
      WatchThis (WATCHITM(rqptr), WATCH_DCL, "MBX WEBSOCKET_OUTPUT !AZ size:!UL",
                 wsptr->OutputDevName, wsptr->OutputSize);
   }

   if (!DclMailboxBytLmRequired)
   {
      BytLmAfter = GetJpiBytLm ();
      DclMailboxBytLmRequired = BytLmBefore - BytLmAfter;
      if (WATCH_MODULE(WATCH_MOD_DCL))
         WatchThis (WATCHALL, WATCH_MOD_DCL, "BytLm: !UL",
                    DclMailboxBytLmRequired);
   }

   return (SS$_NORMAL);

   /*********/
   /* ERROR */
   /*********/

WebSockCreateMailboxesError:

   ErrorNoticed (rqptr, status, "$CREMBX()", FI_LI);

   if (wsptr->InputChannel) sys$dassgn (wsptr->InputChannel);
   if (wsptr->OutputChannel) sys$dassgn (wsptr->OutputChannel);
   wsptr->InputChannel = wsptr->OutputChannel = 0;

   return (status);
}

/*****************************************************************************/
/*
Discard any outstanding WebSocket I/O and deassign respective channels.  Wait
for all outstanding I/O.  Return true when all I/O complete and channels have
been deassigned, false until then.
*/ 

BOOL WebSockCloseMailboxes (REQUEST_STRUCT *rqptr)

{
   int  status;
   struct RequestWebSocketStruct  *wsptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
"WebSockCloseMailboxes() netin:!UL mbxin:!UL:!UL mbxout:!UL:!UL netout:!UL",
                 rqptr->rqWebSocket.QueuedNetRead,
                 rqptr->rqWebSocket.InputChannel,
                 rqptr->rqWebSocket.QueuedInput,
                 rqptr->rqWebSocket.OutputChannel,
                 rqptr->rqWebSocket.QueuedOutput,
                 rqptr->rqWebSocket.QueuedNetWrite);

   wsptr = &rqptr->rqWebSocket;

   if (wsptr->QueuedInput)
   {
      /* cancel outstanding I/O but keep channel for EOF */
      sys$cancel (wsptr->InputChannel);
   }
   else
   if (wsptr->InputChannel)
   {
      /* give any scripting process a subsequent heads-up */
      status = sys$qio (EfnNoWait, wsptr->InputChannel,
                        IO$_WRITEOF | IO$M_NORSWAIT | IO$M_READERCHECK,
                        0, &WebSockDassgnInput, wsptr->InputChannel,
                        0, 0, 0, 0, 0, 0);
      /* if the AST is not going to be delivered deassign it here! */
      if (VMSnok (status)) sys$dassgn (wsptr->InputChannel);
      wsptr->InputChannel = 0;
   }

   if (wsptr->OutputChannel)
   {
      /* deassign will cancel outstanding I/O */
      sys$dassgn (wsptr->OutputChannel);
      wsptr->OutputChannel = 0;
   }

   if (wsptr->QueuedInput || wsptr->QueuedOutput) return (false);

   if (wsptr->InputChannel || wsptr->OutputChannel) return (false);

   return (true);
}

/*****************************************************************************/
/*
After EOF delivery (or otherwise) deassign the WEBSOCKET_INPUT mailbox channel.
*/ 

WebSockDassgnInput (unsigned short InputChannel)

{
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "WebSockDassgnInput() !UL", InputChannel);

   sys$dassgn (InputChannel);
}

/*****************************************************************************/
/*
When the mailboxes have been closed and no network I/O then call RequestEnd().
*/ 

WebSockClose (REQUEST_STRUCT *rqptr)

{
   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL, "WebSockClose()");

   NetCloseSocket (rqptr);

   if (WebSockCloseMailboxes (rqptr))
      if (!(rqptr->rqWebSocket.QueuedNetRead ||
            rqptr->rqWebSocket.QueuedNetWrite)) RequestEnd (rqptr);
}

/*****************************************************************************/
/*
Return true if a WebSocket request is ready for the finale, false otherwise.
*/ 

BOOL WebSockEnd (REQUEST_STRUCT *rqptr)

{
   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
                 "WebSockEnd() dcl:!&B netread:!UL netwrite:!UL",
                 rqptr->DclTaskPtr && rqptr->DclTaskPtr->ScriptProcessPid,
                 rqptr->rqWebSocket.QueuedNetRead,
                 rqptr->rqWebSocket.QueuedNetWrite);

   if (!WebSockCloseMailboxes (rqptr)) return (false);

   if (rqptr->rqWebSocket.QueuedNetRead ||
       rqptr->rqWebSocket.QueuedNetWrite) return (false);

   if (rqptr->DclTaskPtr &&
       rqptr->DclTaskPtr->ScriptProcessPid) return (false);

   WebSockRemove (rqptr);

   if (WATCHING (rqptr, WATCH_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_DCL, "WEBSOCKET disconnected");

   return (true);
}

/*****************************************************************************/
/*
Remove the request from the WebSocket request list.
*/ 

WebSockRemove (REQUEST_STRUCT *rqptr)

{
   /*********/
   /* begin */
   /*********/

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL, "WebSockRemove()");

   if (!rqptr->rqWebSocket.RequestPtr) return;

   if (rqptr == rqptr->rqWebSocket.RequestPtr)
      ListRemove (&WebSockList, &rqptr->rqWebSocket);
   else
   if (rqptr->rqWebSocket.RequestPtr)
      ErrorNoticed (rqptr, SS$_BUGCHECK, ErrorSanityCheck, FI_LI);

   rqptr->rqWebSocket.RequestPtr = NULL;
}

/*****************************************************************************/
/*
Specifically for running down the request from DCL EOF sentinal received.
*/ 

WebSockIfEnd (REQUEST_STRUCT *rqptr)

{
   struct RequestWebSocketStruct  *wsptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
"WebSockIfEnd() read:!UL in:!UL out:!UL write:!UL",
                 rqptr->rqWebSocket.QueuedNetRead,
                 rqptr->rqWebSocket.InputChannel,
                 rqptr->rqWebSocket.OutputChannel,
                 rqptr->rqWebSocket.QueuedNetWrite);

   wsptr = &rqptr->rqWebSocket;

   if (wsptr->InputChannel) return;
   if (wsptr->OutputChannel) return;
   if (wsptr->QueuedNetRead) return;
   if (wsptr->QueuedNetWrite) return;

   RequestEnd (rqptr);
}

/*****************************************************************************/
/*
Queue up a read from the script process WEBSOCKET_OUTPUT mailbox.  When the
read completes call function WebSockOutputAst(), do any post-processing
required and write the data to the client over the network.
*/ 

WebSockOutput (REQUEST_STRUCT *rqptr)

{
   int  status;
   struct RequestWebSocketStruct  *wsptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
                 "WebSockOutput() !8XL",
                 rqptr->rqWebSocket.ScriptProcessPid);

   wsptr = &rqptr->rqWebSocket;

   if (!wsptr->OutputChannel)
   {
      WebSockClose (rqptr);
      return;
   }

   wsptr->QueuedOutput++;

   status = sys$qio (EfnNoWait, wsptr->OutputChannel,
                     IO$_READLBLK, &wsptr->OutputIOsb,
                     &WebSockOutputAst, rqptr,
                     wsptr->OutputPtr,
                     wsptr->OutputSize,
                     0, 0, 0, 0);

   if (VMSok (status)) return;

   ErrorNoticed (NULL, status, "sys$qio", FI_LI);

   /* report error via the AST */
   wsptr->OutputIOsb.Status = status;
   SysDclAst (&WebSockOutputAst, rqptr);
}

/*****************************************************************************/
/*
A queued read from the script process WEBSOCKET_OUTPUT mailbox has completed.
*/ 

WebSockOutputAst (REQUEST_STRUCT *rqptr)

{
   int  cnt, status;
   char  *cptr;
   struct RequestWebSocketStruct  *wsptr;
   struct dsc$descriptor_s  *dscptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
         "WebSockOutputAst() !&F !8XL", &WebSockOutputAst,
         rqptr->rqWebSocket.ScriptProcessPid);

   wsptr = &rqptr->rqWebSocket;

   if (WATCHING (rqptr, WATCH_DCL))
   {
      if (VMSnok (wsptr->OutputIOsb.Status))
         WatchThis (WATCHITM(rqptr), WATCH_DCL,
                    "READ WEBSOCKET_OUTPUT !&S",
                    wsptr->OutputIOsb.Status);
      else
      {
         WatchThis (WATCHITM(rqptr), WATCH_DCL,
                    "READ WEBSOCKET_OUTPUT !&S !UL byte!%s",
                    wsptr->OutputIOsb.Status,
                    wsptr->OutputIOsb.Count);
         if (wsptr->OutputIOsb.Count)
            WatchDataDump (wsptr->OutputPtr, wsptr->OutputIOsb.Count);
      }
   }

   if (wsptr->QueuedOutput) wsptr->QueuedOutput--;

   if (VMSnok (wsptr->OutputIOsb.Status))
   {
      WebSockClose (rqptr);
      return;
   }

   cptr = wsptr->OutputPtr;
   cnt = wsptr->OutputIOsb.Count;

   if (rqptr->rqCgi.EscLength)
   {
      if (cnt >= rqptr->rqCgi.EscLength &&
          cnt <= rqptr->rqCgi.EscLength+2 &&
          MATCH0 (cptr, rqptr->rqCgi.EscStr, rqptr->rqCgi.EscLength))
      {
         /***********************/
         /* escape from output! */
         /***********************/

         wsptr->CalloutInProgress = true;
         /* queue the next read from WEBSOCKET_OUTPUT */
         WebSockOutput (rqptr);
         return;
      }
   }

   if (wsptr->CalloutInProgress)
   {
      if (cnt >= rqptr->rqCgi.EotLength &&
          cnt <= rqptr->rqCgi.EotLength+2 &&
          MATCH0 (cptr, rqptr->rqCgi.EotStr, rqptr->rqCgi.EotLength))
      {
         /******************/
         /* end of escape! */
         /******************/

         wsptr->CalloutInProgress = false;
         /* queue the next read from WEBSOCKET_OUTPUT */
         WebSockOutput (rqptr);
         return;
      }

      /***********/
      /* callout */
      /***********/

      WebSockCallout (rqptr);
      /* queue the next read from WEBSOCKET_OUTPUT */
      WebSockOutput (rqptr);
      return;
   }

   wsptr->QueuedNetWrite++;

   NetWrite (rqptr, &WebSockOutputWriteAst, cptr, cnt);
}

/*****************************************************************************/
/*
A queued asynchronous write of script process WEBSOCKET_OUTPUT (mailbox) to
the client over the network has completed.
*/ 

WebSockOutputWriteAst (REQUEST_STRUCT *rqptr)

{
   int  status;
   struct RequestWebSocketStruct  *wsptr;

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

   wsptr = &rqptr->rqWebSocket;

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
                 "WebSockOutputWriteAst() !&F !8XL !&S",
                 &WebSockOutputWriteAst, rqptr->rqWebSocket.ScriptProcessPid,
                 rqptr->NetIoPtr->WriteStatus);

   if (wsptr->QueuedNetWrite) wsptr->QueuedNetWrite--;

   if (VMSnok (rqptr->NetIoPtr->WriteStatus))
   {
      WebSockClose (rqptr);
      return;
   }

   /* queue the next read of the script process' WEBSOCKET_OUTPUT */
   WebSockOutput (rqptr);
}

/*****************************************************************************/
/*
Get (more) data directly from the WebSocket client.
*/ 

WebSockInput (REQUEST_STRUCT *rqptr)

{
   unsigned int  DataCount;
   unsigned char  *DataPtr;
   struct RequestWebSocketStruct  *wsptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
                 "WebSockInput() !8XL", rqptr->rqWebSocket.ScriptProcessPid);

   wsptr = &rqptr->rqWebSocket;

   if (!wsptr->InputChannel)
   {
      WebSockClose (rqptr);
      return;
   }

   wsptr->QueuedNetRead++;

   NetRead (rqptr, &WebSockInputAst,
            rqptr->rqNet.ReadBufferPtr,
            rqptr->rqNet.ReadBufferSize);
}

/*****************************************************************************/
/*
WebSocket client read has completed.
*/ 

WebSockInputAst (REQUEST_STRUCT *rqptr)

{
   int  status;
   unsigned int  DataCount;
   unsigned char  *DataPtr;
   struct RequestWebSocketStruct  *wsptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
                 "WebSockInputAst() !&F !8XL !&S !UL", &WebSockInputAst,
                 rqptr->rqWebSocket.ScriptProcessPid,
                 rqptr->NetIoPtr->ReadStatus,
                 rqptr->NetIoPtr->ReadCount);

   wsptr = &rqptr->rqWebSocket;

   if (wsptr->QueuedNetRead) wsptr->QueuedNetRead--;

   if (VMSnok (rqptr->NetIoPtr->ReadStatus))
   {
      WebSockClose (rqptr);
      return;
   }

   DataCount = rqptr->NetIoPtr->ReadCount;
   DataPtr = rqptr->rqNet.ReadBufferPtr;

   if (WATCHING (rqptr, WATCH_DCL))
   {
      WatchThis (WATCHITM(rqptr), WATCH_DCL,
                 "WRITE WEBSOCKET_INPUT !UL byte!%s", DataCount);
      WatchDataDump (DataPtr, DataCount);
   }

   if (!rqptr->rqWebSocket.InputChannel)
   {
      WebSockClose (rqptr);
      return;
   }

   wsptr->QueuedInput++;

   status = sys$qio (EfnNoWait, rqptr->rqWebSocket.InputChannel,
                     IO$_WRITELBLK, &rqptr->rqWebSocket.InputIOsb,
                     &WebSockInputWriteAst, rqptr,
                     DataPtr, DataCount, 0, 0, 0, 0);
   if (VMSok (status)) return;

   ErrorNoticed (NULL, status, "sys$qio", FI_LI);

   /* report error via the AST */
   rqptr->rqWebSocket.InputIOsb.Status = status;
   SysDclAst (&WebSockInputWriteAst, rqptr);
}

/*****************************************************************************/
/*
A queued write to the script process WEBSOCKET_INPUT mailbox has completed.
*/ 

WebSockInputWriteAst (REQUEST_STRUCT *rqptr)

{
   int  status;
   struct RequestWebSocketStruct  *wsptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
         "WebSockInputWriteAst() !&F !8XL", &WebSockInputWriteAst,
         rqptr->rqWebSocket.ScriptProcessPid);

   wsptr = &rqptr->rqWebSocket;

   if (WATCHING (rqptr, WATCH_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_DCL,
                 "WRITE WEBSOCKET_INPUT !&S",
                 rqptr->rqWebSocket.InputIOsb.Status);

   if (wsptr->QueuedInput) wsptr->QueuedInput--;

   /* abort if an error writing WebSocket stream to script process */
   if (VMSnok (rqptr->rqWebSocket.InputIOsb.Status))
   {
      WebSockClose (rqptr);
      return;
   }

   /* get more from the client */
   WebSockInput (rqptr);
}

/*****************************************************************************/
/*
(Currently) a basic callout allowing WATCHing of WebSocket scripts.
*/ 

WebSockCallout (REQUEST_STRUCT *rqptr)

{
   static char  RspBadParam [] = "400 Bad parameter",
                RspUnauthorized [] = "401 Unauthorized",
                RspForbidden [] = "403 Forbidden",
                RspSuccess [] = "200 Success",
                RspUnknown [] = "400 Unknown request";

   BOOL  ProvideResponse;
   int  status,
        OutputCount;
   char  *cptr,
         *OutputPtr;
   struct RequestWebSocketStruct  *wsptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL, "WebSockCallout()");

   wsptr = &rqptr->rqWebSocket;

   OutputPtr = wsptr->OutputPtr;
   OutputCount = wsptr->OutputIOsb.Count;

   if (OutputPtr[0] == '!' || OutputPtr[0] == '#')
   {
      ProvideResponse = false;
      OutputPtr++;
      OutputCount--;
   }
   else
      ProvideResponse = true;

   if (TOUP(OutputPtr[0]) == 'N')
   {
      if (strsame (OutputPtr, "NOOP:", 5))
      {
         /*********/
         /* NOOP: */
         /*********/

         /* used for WATCHable debugging information, comments, etc. */
         if (ProvideResponse)
            WebSockCalloutQio (rqptr, RspSuccess, sizeof(RspSuccess)-1);
         return;
      }
   }

   if (TOUP(OutputPtr[0]) == 'W')
   {
      if (strsame (OutputPtr, "WATCH:", 6))
      {
         /**********/
         /* WATCH: */
         /**********/

         /* WATCHing script */
         if (WATCHING (rqptr, WATCH_SCRIPT))
         {
            for (cptr = OutputPtr+6; *cptr && isspace(*cptr); cptr++);
            WatchThis (WATCHITM(rqptr), WATCH_SCRIPT, "!#AZ",
                       OutputCount-(cptr-OutputPtr), cptr);
            if (ProvideResponse)
               WebSockCalloutQio (rqptr, RspSuccess, sizeof(RspSuccess)-1);
         }
         else
         if (ProvideResponse)
            WebSockCalloutQio (rqptr, RspBadParam, sizeof(RspBadParam)-1);
         return;
      }
   }

   if (ProvideResponse)
      WebSockCalloutQio (rqptr, RspUnknown, sizeof(RspUnknown)-1);
}

/*****************************************************************************/
/*
Obviously, for this not to be confused with client input, it be identifiable
from the client data stream.  Callout responses (where required) are
encapsulated by a leading ESC sentinal (record) and a trailing EOT sentinal
(record), both the same sequences as used to make the callout.  The intervening
record contains the response.  Each callout response requires three $QIOs; the
actual response is sandwiched between ESC and EOT records.
*/ 

WebSockCalloutQio
(
REQUEST_STRUCT *rqptr,
char *DataPtr,
int DataCount
)
{
   int  status;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
                 "WebSockCalloutQio() !UL !&Z",
                 rqptr->rqWebSocket.QueuedInput, DataPtr);

   if (!rqptr->rqWebSocket.InputChannel) return;

   if (rqptr->rqWebSocket.QueuedInput > 32)
   {
      /* getting a bit suspicious - let's not compound things */
      return;
   }

   status = sys$qio (EfnNoWait, rqptr->rqWebSocket.InputChannel,
                     IO$_WRITELBLK, 0,
                     WebSockCalloutQioAst, rqptr,
                     rqptr->rqCgi.EscStr, rqptr->rqCgi.EscLength, 0, 0, 0, 0);
   if (VMSok (status))
   {
      rqptr->rqWebSocket.QueuedInput++;

      status = sys$qio (EfnNoWait, rqptr->rqWebSocket.InputChannel,
                        IO$_WRITELBLK, 0,
                        WebSockCalloutQioAst, rqptr,
                        DataPtr, DataCount, 0, 0, 0, 0);
      if (VMSok (status))
      {
         rqptr->rqWebSocket.QueuedInput++;

         status = sys$qio (EfnNoWait, rqptr->rqWebSocket.InputChannel,
                           /* IO status block reports on the final $QIO */
                           IO$_WRITELBLK, &rqptr->rqWebSocket.CalloutIOsb,
                           WebSockCalloutQioAst, rqptr,
                           rqptr->rqCgi.EotStr, rqptr->rqCgi.EotLength, 
                           0, 0, 0, 0);
         if (VMSok (status)) rqptr->rqWebSocket.QueuedInput++;
      }
   }

   if (VMSnok (status) && status != SS$_IVCHAN)
   {
      ErrorNoticed (NULL, status, "sys$qio", FI_LI);
      WebSockClose (rqptr);
   }
}

/*****************************************************************************/
/*
WebSocket callout response write (to input) has completed (three per callout).
*/ 

WebSockCalloutQioAst (REQUEST_STRUCT *rqptr)

{
   int  status;
   struct RequestWebSocketStruct  *wsptr;

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

   if (WATCHMOD (rqptr, WATCH_MOD_DCL))
      WatchThis (WATCHITM(rqptr), WATCH_MOD_DCL,
"WebSockCalloutQioAst() !&F !8XL !&S !UL", &WebSockCalloutQioAst,
                 rqptr->rqWebSocket.ScriptProcessPid,
                 rqptr->rqWebSocket.CalloutIOsb.Status,
                 rqptr->rqWebSocket.CalloutIOsb.Count);

   wsptr = &rqptr->rqWebSocket;

   if (wsptr->QueuedInput) wsptr->QueuedInput--;

   if (status = rqptr->rqWebSocket.CalloutIOsb.Status)
   {
      /* IO status block reports on the final of three $QIO */
      if (VMSnok (status))
      {
         /* error */
         ErrorNoticed (NULL, status, "sys$qio", FI_LI);
         WebSockClose (rqptr);
      }
   }
}

/*****************************************************************************/
/*
Return the number of WebSocket requests connected to the scripting process PID.
*/ 

int WebSockCount (unsigned long ScriptProcessPid)

{
   int  Count = 0;
   REQUEST_STRUCT  *rqeptr;
   LIST_ENTRY  *leptr;

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

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
         "WebSockCount() !8XL", ScriptProcessPid);

   if (!ScriptProcessPid) return (0);
   for (leptr = WebSockList.HeadPtr; leptr; leptr = leptr->NextPtr)
   {
      rqeptr = ((struct RequestWebSocketStruct*)leptr)->RequestPtr;
      if (rqeptr->rqWebSocket.ScriptProcessPid != ScriptProcessPid) continue;
      Count++;
   }
   return (Count);
}

/*****************************************************************************/
/*
Disconnect WebSockets from their clients.  Return count of disconnections.
*/ 

int WebSockControl
(
int ConnectNumber,
char *ScriptName,
char *UserName
)
{
   int  Count = 0;
   REQUEST_STRUCT  *rqeptr;
   LIST_ENTRY  *leptr, *dleptr;
   DCL_TASK  *tkptr;

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

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "WebSockControl() !UL !&Z !&Z",
                 ConnectNumber, ScriptName, UserName);

   for (leptr = WebSockList.HeadPtr; leptr; leptr = leptr->NextPtr)
   {
      rqeptr = ((struct RequestWebSocketStruct*)leptr)->RequestPtr;
      if (!rqeptr->WebSocketRequest) continue;

      /* if we're looking for a particular request and this is not it */
      if (ConnectNumber && rqeptr->ConnectNumber != ConnectNumber)
         continue;

      /* only matching scripts */
      if (ScriptName && ScriptName[0])
      {
         if (!rqeptr->rqWebSocket.ScriptProcessPid) continue;
         if (!StringMatch (NULL, rqeptr->ScriptName, ScriptName)) continue;
      }

      /* only WebSocket scripts running as a specific VMS user */
      if (UserName && UserName[0])
      {
         if (!rqeptr->rqWebSocket.ScriptProcessPid) continue;
         for (dleptr = DclTaskList.HeadPtr; dleptr; dleptr = dleptr->NextPtr)
         {
            tkptr = (DCL_TASK*)dleptr;
            if (tkptr->ScriptProcessPid !=
                rqeptr->rqWebSocket.ScriptProcessPid) continue;
            if (StringMatch (NULL, tkptr->CrePrcUserName, UserName)) break;
         }
         if (!dleptr) continue;
      }

      /* make the closure asynchronous to this list traversal */
      SysDclAst (WebSockClose, rqeptr);
      Count++;
   }

   return (Count);
}

/*****************************************************************************/
/*
Return a report on current WebSocket requests.
This function blocks while executing.
*/ 

WebSockReport (REQUEST_STRUCT *rqptr)

{
   /* the final column just adds a little white-space on the page far right */
   static char  BeginPageFao [] =
"<p><table class=\"rghtrght\">\n\
<tr><th></th>\
<th class=\"sbttl talft\"><u>Service&nbsp;/&nbsp;Client</th>\
<th class=\"sbttl talft\"><u>Time&nbsp;/&nbsp;Request</th>\
<th class=\"sbttl\">Rx</th>\
<th class=\"sbttl\">Tx</th>\
<th class=\"sbttl\">Bytes/Sec</th>\
<th class=\"sbttl\">Duration</th>\
<th class=\"sbttl\">Script&nbsp;PID</th>\
<th class=\"sbttl\">WATCH</th>\
<th class=\"sbttl\">Connect</u></th>\
</tr>\n";

   static char  WebSocketFao [] =
"<tr>\
<th>!3ZL</th>\
<td class=\"talft\">!AZ//!AZ</td>\
<td class=\"talft\">!20%D</td>\
<td>!&,@SQ</td>\
<td>!&,@SQ</td>\
<td>!&L</td>\
<td>!AZ</td>\
<td>!8XL</td>\
<td>!&@</td>\
<td>!UL</td>\
</tr>\n\
<tr class=\"hlght\"><th></th>\
<td class=\"talft\">!&@</td>\
<td class=\"talft maxvis\" colspan=\"8\"><tt>!AZ&nbsp;!AZ</tt></td>\
</tr>\n";

   static char  NoneFao [] =
"<tr class=\"hlght\">\
<th><b>000</b></th>\
<td class=\"talft\" colspan=\"9\"><i>none</i></td>\
</tr>\n";

   static char  EndPageFao [] =
"</table>\n\
<p><table class=\"lftlft\">\n\
<tr><td>\n\
<form method=\"GET\" action=\"!AZ\">\n\
<input type=\"submit\" value=\" Force Disconnect \">\n\
</form>\n\
</td></tr>\n\
</table>\n\
!AZ\
</div>\n\
</body>\n\
</html>\n";

   int  status,
        DisplayCount,
        ReportType;
   unsigned long  *vecptr;
   unsigned long  FaoVector [32];
   int64  Time64,
          Duration64,
          ResultTime64;
   char  *cptr;
   REQUEST_STRUCT  *rqeptr;
   LIST_ENTRY  *leptr;

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

   if (WATCH_MODULE(WATCH_MOD_REQUEST))
      WatchThis (WATCHALL, WATCH_MOD_REQUEST, "WebSockReport()");

   AdminPageTitle (rqptr, "WebSocket Report");

   status = FaolToNet (rqptr, BeginPageFao, NULL);
   if (VMSnok (status)) ErrorNoticed (rqptr, status, "FaolToNet()", FI_LI);

   DisplayCount = 0;

   sys$gettim (&Time64);

   /* process web socket list from least to most recent */
   for (leptr = WebSockList.HeadPtr; leptr; leptr = leptr->NextPtr)
   {
      rqeptr = ((struct RequestWebSocketStruct*)leptr)->RequestPtr;

      vecptr = FaoVector;

      *vecptr++ = ++DisplayCount;
      *vecptr++ = rqeptr->ServicePtr->RequestSchemeNamePtr;
      *vecptr++ = rqeptr->ServicePtr->ServerHostPort;
      *vecptr++ = &rqeptr->rqTime.BeginTime64;

      *vecptr++ = &rqeptr->NetIoPtr->BytesRawRx64;
      *vecptr++ = &rqeptr->NetIoPtr->BytesRawTx64;

      Duration64 = rqeptr->rqTime.BeginTime64 - Time64;
      *vecptr++ = BytesPerSecond (&rqeptr->NetIoPtr->BytesRawRx64,
                                  &rqeptr->NetIoPtr->BytesRawTx64,
                                  &Duration64);

      *vecptr++ = DurationString (rqptr, &Duration64);
      *vecptr++ = rqeptr->rqWebSocket.ScriptProcessPid;

      *vecptr++ =
"<nobr>[<a href=\"!AZ?at=!UL\">P</a>]\
[<a href=\"!AZ?at=!UL&this=!UL\">+</a>]\
[<a href=\"!AZ?this=!UL\">W</a>]</nobr>";
      *vecptr++ = ADMIN_REPORT_WATCH;
      *vecptr++ = rqeptr->ConnectNumber;
      *vecptr++ = ADMIN_REPORT_WATCH;
      *vecptr++ = rqeptr->ConnectNumber;
      *vecptr++ = rqeptr->ConnectNumber;
      *vecptr++ = ADMIN_REPORT_WATCH;
      *vecptr++ = rqeptr->ConnectNumber;
      *vecptr++ = rqeptr->ConnectNumber;

      *vecptr++ = UserAtClient(rqeptr);

      *vecptr++ = rqeptr->rqHeader.MethodName;
      *vecptr++ = rqeptr->rqHeader.RequestUriPtr;

      status = FaolToNet (rqptr, WebSocketFao, &FaoVector);
      if (VMSnok (status)) ErrorNoticed (rqptr, status, "FaolToNet()", FI_LI);
   }

   if (!DisplayCount)
   {
      status = FaolToNet (rqptr, NoneFao, NULL);
      if (VMSnok (status)) ErrorNoticed (rqptr, status, "FaolToNet()", FI_LI);
   }

   vecptr = FaoVector;
   *vecptr++ = ADMIN_CONTROL_WEBSOCKET_DISCONNECT;
   *vecptr++ = AdminRefresh();

   status = FaolToNet (rqptr, EndPageFao, &FaoVector);
   if (VMSnok (status)) ErrorNoticed (rqptr, status, "FaolToNet()", FI_LI);

   rqptr->rqResponse.PreExpired = PRE_EXPIRE_ADMIN;
   ResponseHeader200 (rqptr, "text/html", NULL);

   AdminEnd (rqptr);
}

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