1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
|
implement CharonUtils;
include "common.m";
include "transport.m";
include "date.m";
include "translate.m";
date: Date;
me: CharonUtils;
sys: Sys;
D: Draw;
S: String;
U: Url;
T: StringIntTab;
Font : import D;
Parsedurl: import U;
convcs : Convcs;
trans : Translate;
Dict : import trans;
dict : ref Dict;
NCTimeout : con 100000; # free NC slot after 100 seconds
UBufsize : con 40*1024; # initial buffer size for unknown lengths
UEBufsize : con 1024; # initial buffer size for unknown lengths, error responses
botchexception := "EXInternal: ByteSource protocol botch";
bytesourceid := 0;
crlf : con "\r\n";
ctype : array of byte; # local ref to C->ctype[]
dbgproto : int;
dbg: int;
netconnid := 0;
netconns := array[10] of ref Netconn;
sptab : con " \t";
THTTP, TFTP, TFILE, TMAX: con iota;
transports := array[TMAX] of Transport;
tpaths := array [TMAX] of {
THTTP => Transport->HTTPPATH,
TFTP => Transport->FTPPATH,
TFILE => Transport->FILEPATH,
};
schemes := array [] of {
("http", THTTP),
("https", THTTP),
("ftp", TFTP),
("file", TFILE),
};
ngchan : chan of (int, list of ref ByteSource, ref Netconn, chan of ref ByteSource);
# must track HTTP methods in chutils.m
# (upper-case, since that's required in HTTP requests)
hmeth = array[] of { "GET", "POST" };
# following array must track media type def in chutils.m
# keep in alphabetical order
mnames = array[] of {
"application/msword",
"application/octet-stream",
"application/pdf",
"application/postscript",
"application/rtf",
"application/vnd.framemaker",
"application/vnd.ms-excel",
"application/vnd.ms-powerpoint",
"application/x-unknown",
"audio/32kadpcm",
"audio/basic",
"image/cgm",
"image/g3fax",
"image/gif",
"image/ief",
"image/jpeg",
"image/png",
"image/tiff",
"image/x-bit",
"image/x-bit2",
"image/x-bitmulti",
"image/x-inferno-bit",
"image/x-xbitmap",
"model/vrml",
"multipart/digest",
"multipart/mixed",
"text/css",
"text/enriched",
"text/html",
"text/javascript",
"text/plain",
"text/richtext",
"text/sgml",
"text/tab-separated-values",
"text/xml",
"video/mpeg",
"video/quicktime"
};
ncstatenames = array[] of {
"free", "idle", "connect", "gethdr", "getdata",
"done", "err"
};
hsnames = array[] of {
"none", "information", "ok", "redirect", "request error", "server error"
};
hcphrase(code: int) : string
{
ans : string;
case code {
HCContinue => ans = X("Continue", "http");
HCSwitchProto => ans = X("Switching Protocols", "http");
HCOk => ans = X("Ok", "http");
HCCreated => ans = X("Created", "http");
HCAccepted => ans = X("Accepted", "http");
HCOkNonAuthoritative => ans = X("Non-Authoratative Information", "http");
HCNoContent => ans = X("No content", "http");
HCResetContent => ans = X("Reset content", "http");
HCPartialContent => ans = X("Partial content", "http");
HCMultipleChoices => ans = X("Multiple choices", "http");
HCMovedPerm => ans = X("Moved permanently", "http");
HCMovedTemp => ans = X("Moved temporarily", "http");
HCSeeOther => ans = X("See other", "http");
HCNotModified => ans = X("Not modified", "http");
HCUseProxy => ans = X("Use proxy", "http");
HCBadRequest => ans = X("Bad request", "http");
HCUnauthorized => ans = X("Unauthorized", "http");
HCPaymentRequired => ans = X("Payment required", "http");
HCForbidden => ans = X("Forbidden", "http");
HCNotFound => ans = X("Not found", "http");
HCMethodNotAllowed => ans = X("Method not allowed", "http");
HCNotAcceptable => ans = X("Not Acceptable", "http");
HCProxyAuthRequired => ans = X("Proxy authentication required", "http");
HCRequestTimeout => ans = X("Request timed-out", "http");
HCConflict => ans = X("Conflict", "http");
HCGone => ans = X("Gone", "http");
HCLengthRequired => ans = X("Length required", "http");
HCPreconditionFailed => ans = X("Precondition failed", "http");
HCRequestTooLarge => ans = X("Request entity too large", "http");
HCRequestURITooLarge => ans = X("Request-URI too large", "http");
HCUnsupportedMedia => ans = X("Unsupported media type", "http");
HCRangeInvalid => ans = X("Requested range not valid", "http");
HCExpectFailed => ans = X("Expectation failed", "http");
HCServerError => ans = X("Internal server error", "http");
HCNotImplemented => ans = X("Not implemented", "http");
HCBadGateway => ans = X("Bad gateway", "http");
HCServiceUnavailable => ans = X("Service unavailable", "http");
HCGatewayTimeout => ans = X("Gateway time-out", "http");
HCVersionUnsupported => ans = X("HTTP version not supported", "http");
HCRedirectionFailed => ans = X("Redirection failed", "http");
* => ans = X("Unknown code", "http");
}
return ans;
}
# This array should be kept sorted
fileexttable := array[] of { T->StringInt
("ai", ApplPostscript),
("au", AudioBasic),
# ("bit", ImageXBit),
("bit", ImageXInfernoBit),
("bit2", ImageXBit2),
("bitm", ImageXBitmulti),
("eps", ApplPostscript),
("gif", ImageGif),
("gz", ApplOctets),
("htm", TextHtml),
("html", TextHtml),
("jpe", ImageJpeg),
("jpeg", ImageJpeg),
("jpg", ImageJpeg),
("pdf", ApplPdf),
("png", ImagePng),
("ps", ApplPostscript),
("shtml", TextHtml),
("text", TextPlain),
("tif", ImageTiff),
("tiff", ImageTiff),
("txt", TextPlain),
("zip", ApplOctets)
};
# argl is command line
# Return string that is empty if all ok, else path of module
# that failed to load.
init(ch: Charon, c: CharonUtils, argl: list of string, evc: chan of ref E->Event, cksrv: Cookiesrv, ckc: ref Cookiesrv->Client) : string
{
me = c;
sys = load Sys Sys->PATH;
startres = ResourceState.cur();
D = load Draw Draw->PATH;
CH = ch;
S = load String String->PATH;
if(S == nil)
return String->PATH;
U = load Url Url->PATH;
if(U == nil)
return Url->PATH;
U->init();
T = load StringIntTab StringIntTab->PATH;
if(T == nil)
return StringIntTab->PATH;
trans = load Translate Translate->PATH;
if (trans != nil) {
trans->init();
(dict, nil) = trans->opendict(trans->mkdictname(nil, "charon"));
}
# Now have all the modules needed to process command line
# (hereafter can use our loadpath() function to substitute the
# build directory version if dbg['u'] is set)
setconfig(argl);
dbg = int config.dbg['d'];
G = load Gui loadpath(Gui->PATH);
if(G == nil)
return loadpath(Gui->PATH);
C = load Ctype loadpath(Ctype->PATH);
if(C == nil)
return loadpath(Ctype->PATH);
E = load Events Events->PATH;
if(E == nil)
return loadpath(Events->PATH);
J = load Script loadpath(Script->JSCRIPTPATH);
# don't report an error loading JavaScript, handled elsewhere
LX = load Lex loadpath(Lex->PATH);
if(LX == nil)
return loadpath(Lex->PATH);
B = load Build loadpath(Build->PATH);
if(B == nil)
return loadpath(Build->PATH);
I = load Img loadpath(Img->PATH);
if(I == nil)
return loadpath(Img->PATH);
L = load Layout loadpath(Layout->PATH);
if(L == nil)
return loadpath(Layout->PATH);
date = load Date loadpath(Date->PATH);
if (date == nil)
return loadpath(Date->PATH);
convcs = load Convcs Convcs->PATH;
if (convcs == nil)
return loadpath(Convcs->PATH);
# Intialize all modules after loading all, so that each
# may cache pointers to the other modules
# (G will be initialized in main charon routine, and L has to
# be inited after that, because it needs G's display to allocate fonts)
E->init(evc);
I->init(me);
err := convcs->init(nil);
if (err != nil)
return err;
if(J != nil) {
err = J->init(me);
if (err != nil) {
# non-fatal: just don't handle javascript
J = nil;
if (dbg)
sys->print("%s\n", err);
}
}
B->init(me);
LX->init(me);
date->init(me);
if (config.docookies) {
CK = cksrv;
ckclient = ckc;
if (CK == nil) {
path := loadpath(Cookiesrv->PATH);
CK = load Cookiesrv path;
if (CK == nil)
sys->print("cookies: cannot load server %s: %r\n", path);
else
ckclient = CK->start(config.userdir + "/cookies", 0);
}
}
# preload some transports
gettransport("http");
gettransport("file");
progresschan = chan of (int, int, int, string);
imcache = ref ImageCache;
ctype = C->ctype;
dbgproto = int config.dbg['p'];
ngchan = chan of (int, list of ref ByteSource, ref Netconn, chan of ref ByteSource);
return "";
}
# like startreq() but special case for a string ByteSource
# which doesn't need an associated netconn
stringreq(s : string) : ref ByteSource
{
bs := ByteSource.stringsource(s);
G->progress <-= (bs.id, G->Pstart, 0, "text");
anschan := chan of ref ByteSource;
ngchan <-= (NGstartreq, bs :: nil, nil, anschan);
<-anschan;
return bs;
}
# Make a ByteSource for given request, and make sure
# that it is on the queue of some Netconn.
# If don't have a transport for the request's scheme,
# the returned bs will have err set.
startreq(req: ref ReqInfo) : ref ByteSource
{
bs := ref ByteSource(
bytesourceid++,
req, # req
nil, # hdr
nil, # data
0, # edata
"", # err
nil, # net
1, # refgo
1, # refnc
0, # eof
0, # lim
0 # seenhdr
);
G->progress <-= (bs.id, G->Pstart, 0, req.url.tostring());
anschan := chan of ref ByteSource;
ngchan <-= (NGstartreq, bs::nil, nil, anschan);
<-anschan;
return bs;
}
# Wait for some ByteSource in current go generation to
# have a state change that goproc hasn't seen yet.
waitreq(bsl: list of ref ByteSource) : ref ByteSource
{
anschan := chan of ref ByteSource;
ngchan <-= (NGwaitreq, bsl, nil, anschan);
return <-anschan;
}
# Notify netget that goproc is finished with bs.
freebs(bs: ref ByteSource)
{
anschan := chan of ref ByteSource;
ngchan <-= (NGfreebs, bs::nil, nil, anschan);
<-anschan;
}
abortgo(gopgrp: int)
{
if(int config.dbg['d'])
sys->print("abort go\n");
kill(gopgrp, 1);
freegoresources();
# renew the channels so that receives/sends by killed threads don't
# muck things up
ngchan = chan of (int, list of ref ByteSource, ref Netconn, chan of ref ByteSource);
}
freegoresources()
{
for(i := 0; i < len netconns; i++) {
nc := netconns[i];
nc.makefree();
}
}
# This runs as a separate thread.
# It acts as a monitor to synchronize access to the Netconn data
# structures, as a dispatcher to start runnetconn's as needed to
# process work on Netconn queues, and as a notifier to let goproc
# know when any ByteSources have advanced their state.
netget()
{
msg, n, i: int;
bsl : list of ref ByteSource;
nc: ref Netconn;
waitix := 0;
c : chan of ref ByteSource;
waitpending : list of (list of ref ByteSource, chan of ref ByteSource);
maxconn := config.nthreads;
gncs := array[maxconn] of int;
for(n = 0; n < len netconns; n++)
netconns[n] = Netconn.new(n);
# capture netget chan to prevent abortgo() reset of
# ngchan from breaking us (channel hungup) before kill() does its job
ngc := ngchan;
mainloop:
for(;;) {
(msg,bsl,nc,c) = <- ngc;
case msg {
NGstartreq =>
bs := hd bsl;
# bs has req filled in, and is otherwise in its initial state.
# Find a suitable Netconn and add bs to its queue of work,
# then send nil along c to let goproc continue.
# if ReqInfo is nil then this is a string ByteSource
# in which case we don't need a netconn to service it as we have all
# data already
if (bs.req == nil) {
c <- = nil;
continue;
}
if(dbgproto)
sys->print("Startreq BS=%d for %s\n", bs.id, bs.req.url.tostring());
scheme := bs.req.url.scheme;
host := bs.req.url.host;
(transport, err) := gettransport(scheme);
if(err != "")
bs.err = err;
else {
sport :=bs.req.url.port;
if(sport == "")
port := transport->defaultport(scheme);
else
port = int sport;
i = 0;
freen := -1;
for(n = 0; n < len netconns && (i < maxconn || freen == -1); n++) {
nc = netconns[n];
if(nc.state == NCfree) {
if(freen == -1)
freen = n;
}
else if(nc.host == host
&& nc.port == port && nc.scheme == scheme && i < maxconn) {
gncs[i++] = n;
}
}
if(i < maxconn) {
# use a new netconn for this bs
if(freen == -1) {
freen = len netconns;
newncs := array[freen+10] of ref Netconn;
newncs[0:] = netconns;
for(n = freen; n < freen+10; n++)
newncs[n] = Netconn.new(n);
netconns = newncs;
}
nc = netconns[freen];
nc.host = host;
nc.port = port;
nc.scheme = scheme;
nc.qlen = 0;
nc.ngcur = 0;
nc.gocur = 0;
nc.reqsent = 0;
nc.pipeline = 0;
nc.connected = 0;
}
else {
# use existing netconn with fewest outstanding requests
nc = netconns[gncs[0]];
if(maxconn > 1) {
minqlen := nc.qlen - nc.gocur;
for(i = 1; i < maxconn; i++) {
x := netconns[gncs[i]];
if(x.qlen-x.gocur < minqlen) {
nc = x;
minqlen = x.qlen-x.gocur;
}
}
}
}
if(nc.qlen == len nc.queue) {
nq := array[nc.qlen+10] of ref ByteSource;
nq[0:] = nc.queue;
nc.queue = nq;
}
nc.queue[nc.qlen++] = bs;
bs.net = nc;
if(dbgproto)
sys->print("Chose NC=%d for BS %d, qlen=%d\n", nc.id, bs.id, nc.qlen);
if(nc.state == NCfree || nc.state == NCidle) {
if(nc.connected) {
nc.state = NCgethdr;
if(dbgproto)
sys->print("NC %d: starting runnetconn in gethdr state\n", nc.id);
}
else {
nc.state = NCconnect;
if(dbgproto)
sys->print("NC %d: starting runnetconn in connect state\n", nc.id);
}
spawn runnetconn(nc, transport);
}
}
c <-= nil;
NGwaitreq =>
# goproc wants to be notified when some ByteSource
# changes to a state that the goproc hasn't seen yet.
# Send such a ByteSource along return channel c.
if(dbgproto)
sys->print("Waitreq\n");
for (scanlist := bsl; scanlist != nil; scanlist = tl scanlist) {
bs := hd scanlist;
if (bs.refnc == 0) {
# string ByteSource or completed or error
if (bs.err != nil || bs.edata >= bs.lim) {
c <-= bs;
continue mainloop;
}
continue;
}
# netcon based bytesource
if ((bs.hdr != nil && !bs.seenhdr && bs.hdr.mtype != UnknownType) || bs.edata > bs.lim) {
c <-= bs;
continue mainloop;
}
}
if(dbgproto)
sys->print("Waitpending\n");
waitpending = (bsl, c) :: waitpending;
NGfreebs =>
# goproc is finished with bs.
bs := hd bsl;
if(dbgproto)
sys->print("Freebs BS=%d\n", bs.id);
nc = bs.net;
bs.refgo = 0;
if(bs.refnc == 0) {
bs.free();
if(nc != nil)
nc.queue[nc.gocur] = nil;
}
if(nc != nil) {
# can be nil if no transport was found
nc.gocur++;
if(dbgproto)
sys->print("NC %d: gocur=%d, ngcur=%d, qlen=%d\n", nc.id, nc.gocur, nc.ngcur, nc.qlen);
if(nc.gocur == nc.qlen && nc.ngcur == nc.qlen) {
if(!nc.connected)
nc.makefree();
}
}
# don't need to check waitpending fro NGwait requests involving bs
# the only thread doing a freebs() should be the only thread that
# can do a waitreq() on the same bs. Same thread cannot be in both states.
c <-= nil;
NGstatechg =>
# Some runnetconn is telling us tht it changed the
# state of nc. Send a nil along c to let it continue.
bs : ref ByteSource;
if(dbgproto)
sys->print("Statechg NC=%d, state=%s\n",
nc.id, ncstatenames[nc.state]);
sendtopending : ref ByteSource = nil;
pendingchan : chan of ref ByteSource;
if(waitpending != nil && nc.gocur < nc.qlen) {
bs = nc.queue[nc.gocur];
if(dbgproto) {
totlen := 0;
if(bs.hdr != nil)
totlen = bs.hdr.length;
sys->print("BS %d: havehdr=%d seenhdr=%d edata=%d lim=%d, length=%d\n",
bs.id, bs.hdr != nil, bs.seenhdr, bs.edata, bs.lim, totlen);
if(bs.err != "")
sys->print (" err=%s\n", bs.err);
}
if(bs.refgo &&
(bs.err != "" ||
(bs.hdr != nil && !bs.seenhdr) ||
(nc.gocur == nc.ngcur && nc.state == NCdone) ||
(bs.edata > bs.lim))) {
nwp: list of (list of ref ByteSource, chan of ref ByteSource) = nil;
for (waitlist := waitpending; waitlist != nil; waitlist = tl waitlist) {
(bslist, anschan) := hd waitlist;
if (sendtopending != nil) {
nwp = (bslist, anschan) :: nwp;
continue;
}
for (look := bslist; look != nil; look = tl look) {
if (bs == hd look) {
sendtopending = bs;
pendingchan = anschan;
break;
}
}
if (sendtopending == nil)
nwp = (bslist, anschan) :: nwp;
}
waitpending = nwp;
}
}
if(nc.state == NCdone || nc.state == NCerr) {
if(dbgproto)
sys->print("NC %d: runnetconn finishing\n", nc.id);
assert(nc.ngcur < nc.qlen);
bs = nc.queue[nc.ngcur];
bs.refnc = 0;
if(bs.refgo == 0) {
bs.free();
nc.queue[nc.ngcur] = nil;
}
nc.ngcur++;
if(dbgproto)
sys->print("NC %d: ngcur=%d\n", nc.id, nc.ngcur);
nc.state = NCidle;
if(dbgproto)
sys->print("NC %d: idle\n", nc.id);
if(nc.ngcur < nc.qlen) {
if(nc.connected) {
nc.state = NCgethdr;
if(dbgproto)
sys->print("NC %d: starting runnetconn in gethdr state\n", nc.id);
}
else {
nc.state = NCconnect;
if(dbgproto)
sys->print("NC %d: starting runnetconn in connect state\n", nc.id);
}
(t, nil) := gettransport(nc.scheme);
spawn runnetconn(nc, t);
}
else if(nc.gocur == nc.qlen && !nc.connected)
nc.makefree();
}
c <-= nil;
if(sendtopending != nil) {
if(dbgproto)
sys->print("Send BS %d to pending waitreq\n", bs.id);
pendingchan <-= sendtopending;
sendtopending = nil;
}
}
}
}
# A separate thread, to handle ngcur request of transport.
# If nc.gen ever goes < gen, we have aborted this go.
runnetconn(nc: ref Netconn, t: Transport)
{
ach := chan of ref ByteSource;
retry := 4;
# retry := 0;
err := "";
assert(nc.ngcur < nc.qlen);
bs := nc.queue[nc.ngcur];
# dummy loop, just for breaking out of in error cases
eloop:
for(;;) {
# Make the connection, if necessary
if(nc.state == NCconnect) {
t->connect(nc, bs);
if(bs.err != "") {
if (retry) {
retry--;
bs.err = "";
sys->sleep(100);
continue eloop;
}
break eloop;
}
nc.state = NCgethdr;
}
assert(nc.state == NCgethdr && nc.connected);
if(nc.scheme == "https")
G->progress <-= (bs.id, G->Psslconnected, 0, "");
else
G->progress <-= (bs.id, G->Pconnected, 0, "");
t->writereq(nc, bs);
nc.reqsent++;
if (bs.err != "") {
if (retry) {
retry--;
bs.err = "";
nc.state = NCconnect;
sys->sleep(100);
continue eloop;
}
break eloop;
}
# managed to write the request
# do not retry if we are doing form POSTs
# See RFC1945 section 12.2 "Safe Methods"
if (bs.req.method == HPost)
retry = 0;
# Get the header
t->gethdr(nc, bs);
if(bs.err != "") {
if (retry) {
retry--;
bs.err = "";
nc.state = NCconnect;
sys->sleep(100);
continue eloop;
}
break eloop;
}
assert(bs.hdr != nil);
G->progress <-= (bs.id, G->Phavehdr, 0, "");
nc.state = NCgetdata;
# read enough data to guess media type
while (bs.hdr.mtype == UnknownType && ncgetdata(t, nc, bs))
bs.hdr.setmediatype(bs.hdr.actual.path, bs.data[:bs.edata]);
if (bs.hdr.mtype == UnknownType) {
bs.hdr.mtype = TextPlain;
bs.hdr.chset = "utf8";
}
ngchan <-= (NGstatechg,nil,nc,ach);
<- ach;
while (ncgetdata(t, nc, bs)) {
ngchan <-= (NGstatechg,nil,nc,ach);
<- ach;
}
nc.state = NCdone;
G->progress <-= (bs.id, G->Phavedata, 100, "");
break;
}
if(bs.err != "") {
nc.state = NCerr;
nc.connected = 0;
G->progress <-= (bs.id, G->Perr, 0, bs.err);
}
bs.eof = 1;
ngchan <-= (NGstatechg, nil, nc, ach);
<- ach;
}
ncgetdata(t: Transport, nc: ref Netconn, bs: ref ByteSource): int
{
hdr := bs.hdr;
if (bs.data == nil) {
blen := hdr.length;
if (blen <= 0) {
if(hdr.code == HCOk || hdr.code == HCOkNonAuthoritative)
blen = UBufsize;
else
blen = UEBufsize;
}
bs.data = array[blen] of byte;
}
nr := 0;
if (hdr.length > 0) {
if (bs.edata == hdr.length)
return 0;
nr = t->getdata(nc, bs);
if (nr <= 0)
return 0;
} else {
# don't know data length - keep growing input buffer as needed
if (bs.edata == len bs.data) {
nd := array [2*len bs.data] of byte;
nd[:] = bs.data;
bs.data = nd;
}
nr = t->getdata(nc, bs);
if (nr <= 0) {
# assume EOF
bs.data = bs.data[0:bs.edata];
bs.err = "";
hdr.length = bs.edata;
nc.connected = 0;
return 0;
}
}
bs.edata += nr;
G->progress <-= (bs.id, G->Phavedata, 100*bs.edata/len bs.data, "");
return 1;
}
Netconn.new(id: int) : ref Netconn
{
return ref Netconn(
id, # id
"", # host
0, # port
"", # scheme
sys->Connection(nil, nil, ""), # conn
nil, # ssl context
0, # undetermined ssl version
NCfree, # state
array[10] of ref ByteSource, # queue
0, # qlen
0,0,0, # gocur, ngcur, reqsent
0, # pipeline
0, # connected
0, # tstate
nil, # tbuf
0 # idlestart
);
}
Netconn.makefree(nc: self ref Netconn)
{
if(dbgproto)
sys->print("NC %d: free\n", nc.id);
nc.state = NCfree;
nc.host = "";
nc.conn.dfd = nil;
nc.conn.cfd = nil;
nc.conn.dir = "";
nc.qlen = 0;
nc.gocur = 0;
nc.ngcur = 0;
nc.reqsent = 0;
nc.pipeline = 0;
nc.connected = 0;
nc.tstate = 0;
nc.tbuf = nil;
for(i := 0; i < len nc.queue; i++)
nc.queue[i] = nil;
}
ByteSource.free(bs: self ref ByteSource)
{
if(dbgproto)
sys->print("BS %d freed\n", bs.id);
if(bs.err == "")
G->progress <-= (bs.id, G->Pdone, 100, "");
else
G->progress <-= (bs.id, G->Perr, 0, bs.err);
bs.req = nil;
bs.hdr = nil;
bs.data = nil;
bs.err = "";
bs.net = nil;
}
# Return an ByteSource that is completely filled, from string s
ByteSource.stringsource(s: string) : ref ByteSource
{
a := array of byte s;
n := len a;
hdr := ref Header(
HCOk, # code
nil, # actual
nil, # base
nil, # location
n, # length
TextHtml, # mtype
"utf8", # chset
"", # msg
"", # refresh
"", # chal
"", # warn
"" # last-modified
);
bs := ref ByteSource(
bytesourceid++,
nil, # req
hdr, # hdr
a, # data
n, # edata
"", # err
nil, # net
1, # refgo
0, # refnc
1, # eof - edata is final
0, # lim
1 # seenhdr
);
return bs;
}
MaskedImage.free(mim: self ref MaskedImage)
{
mim.im = nil;
mim.mask = nil;
}
CImage.new(src: ref U->Parsedurl, lowsrc: ref U->Parsedurl, width, height: int) : ref CImage
{
return ref CImage(src, lowsrc, nil, strhash(src.host + "/" + src.path), width, height, nil, nil, 0);
}
# Return true if Cimages a and b represent the same image.
# As well as matching the src urls, the specified widths and heights must match too.
# (Widths and heights are specified if at least one of those is not zero.)
#
# BUG: the width/height matching code isn't right. If one has width and height
# specified, and the other doesn't, should say "don't match", because the unspecified
# one should come in at its natural size. But we overwrite the width and height fields
# when the actual size comes in, so we can't tell whether width and height are nonzero
# because they were specified or because they're their natural size.
CImage.match(a: self ref CImage, b: ref CImage) : int
{
if(a.imhash == b.imhash) {
if(urlequal(a.src, b.src)) {
return (a.width == 0 || b.width == 0 || a.width == b.width) &&
(a.height == 0 || b.height == 0 || a.height == b.height);
# (above is not quite enough: should also check that don't have
# situation where one has width set, not height, and the other has reverse,
# but it is unusual for an image to have a spec in only one dimension anyway)
}
}
return 0;
}
# Return approximate number of bytes in image memory used
# by ci.
CImage.bytes(ci: self ref CImage) : int
{
tot := 0;
for(i := 0; i < len ci.mims; i++) {
mim := ci.mims[i];
dim := mim.im;
if(dim != nil)
tot += ((dim.r.max.x-dim.r.min.x)*dim.depth/8) *
(dim.r.max.y-dim.r.min.y);
dim = mim.mask;
if(dim != nil)
tot += ((dim.r.max.x-dim.r.min.x)*dim.depth/8) *
(dim.r.max.y-dim.r.min.y);
}
return tot;
}
# Call this after initial windows have been made,
# so that resetlimits() will exclude the images for those
# windows from the available memory.
ImageCache.init(ic: self ref ImageCache)
{
ic.imhd = nil;
ic.imtl = nil;
ic.n = 0;
ic.memused = 0;
ic.resetlimits();
}
# Call resetlimits when amount of non-image-cache image
# memory might have changed significantly (e.g., on main window resize).
ImageCache.resetlimits(ic: self ref ImageCache)
{
res := ResourceState.cur();
avail := res.imagelim - (res.image-ic.memused);
# (res.image-ic.memused) is used memory not in image cache
avail = 8*avail/10; # allow 20% slop for other applications, etc.
ic.memlimit = config.imagecachemem;
if(ic.memlimit > avail)
ic.memlimit = avail;
# ic.nlimit = config.imagecachenum;
ic.nlimit = 10000; # let's try this
ic.need(0); # if resized, perhaps need to shed some images
}
# Look for a CImage matching ci, and if found, move it
# to the tail position (i.e., MRU)
ImageCache.look(ic: self ref ImageCache, ci: ref CImage) : ref CImage
{
ans : ref CImage = nil;
prev : ref CImage = nil;
for(i := ic.imhd; i != nil; i = i.next) {
if(i.match(ci)) {
if(ic.imtl != i) {
# remove from current place in cache chain
# and put at tail
if(prev != nil)
prev.next = i.next;
else
ic.imhd = i.next;
i.next = nil;
ic.imtl.next = i;
ic.imtl = i;
}
ans = i;
break;
}
prev = i;
}
return ans;
}
# Call this to add ci as MRU of cache chain (should only call if
# it is known that a ci with same image isn't already there).
# Update ic.memused.
# Assume ic.need has been called to ensure that neither
# memlimit nor nlimit will be exceeded.
ImageCache.add(ic: self ref ImageCache, ci: ref CImage)
{
ci.next = nil;
if(ic.imhd == nil)
ic.imhd = ci;
else
ic.imtl.next = ci;
ic.imtl = ci;
ic.memused += ci.bytes();
ic.n++;
}
# Delete least-recently-used image in image cache
# and update memused and n.
ImageCache.deletelru(ic: self ref ImageCache)
{
ci := ic.imhd;
if(ci != nil) {
ic.imhd = ci.next;
if(ic.imhd == nil) {
ic.imtl = nil;
ic.memused = 0;
}
else
ic.memused -= ci.bytes();
for(i := 0; i < len ci.mims; i++)
ci.mims[i].free();
ci.mims = nil;
ic.n--;
}
}
ImageCache.clear(ic: self ref ImageCache)
{
while(ic.imhd != nil)
ic.deletelru();
}
# Call this just before allocating an Image that will used nbytes
# of image memory, to ensure that if the image were to be
# added to the image cache then memlimit and nlimit will be ok.
# LRU images will be shed if necessary.
# Return 0 if it will be impossible to make enough memory.
ImageCache.need(ic: self ref ImageCache, nbytes: int) : int
{
while(ic.n >= ic.nlimit || ic.memused+nbytes > ic.memlimit) {
if(ic.imhd == nil)
return 0;
ic.deletelru();
}
return 1;
}
strhash(s: string) : int
{
prime: con 8388617;
hash := 0;
n := len s;
for(i := 0; i < n; i++) {
hash = hash % prime;
hash = (hash << 7) + s[i];
}
return hash;
}
schemeid(s: string): int
{
for (i := 0; i < len schemes; i++) {
(n, id) := schemes[i];
if (n == s)
return id;
}
return -1;
}
schemeok(s: string): int
{
return schemeid(s) != -1;
}
gettransport(scheme: string) : (Transport, string)
{
err := "";
transport: Transport = nil;
tindex := schemeid(scheme);
if (tindex == -1)
return (nil, "Unknown scheme");
transport = transports[tindex];
if (transport == nil) {
transport = load Transport loadpath(tpaths[tindex]);
if(transport == nil)
return (nil, sys->sprint("Can't load transport %s: %r", tpaths[tindex]));
transport->init(me);
transports[tindex] = transport;
}
return (transport, err);
}
# Return new Header with default values for fields
Header.new() : ref Header
{
return ref Header(
HCOk, # code
nil, # actual
nil, # base
nil, # location
-1, # length
UnknownType, # mtype
nil, # chset
"", # msg
"", # refresh
"", # chal
"", # warn
"" # last-modified
);
}
jpmagic := array[] of {byte 16rFF, byte 16rD8, byte 16rFF, byte 16rE0,
byte 0, byte 0, byte 'J', byte 'F', byte 'I', byte 'F', byte 0};
pngsig := array[] of { byte 137, byte 80, byte 78, byte 71, byte 13, byte 10, byte 26, byte 10 };
# Set the mtype (and possibly chset) fields of h based on (in order):
# first bytes of file, if unambigous
# file name extension
# first bytes of file, even if unambigous (guess)
# if all else fails, then leave as UnknownType.
# If it's a text type, also set the chset.
# (HTTP Transport will try to use Content-Type first, and call this if that
# doesn't work; other Transports will have to rely on this "guessing" function.)
Header.setmediatype(h: self ref Header, name: string, first: array of byte)
{
# Look for key signatures at beginning of file (perhaps after whitespace)
n := len first;
mt := UnknownType;
for(i := 0; i < n; i++)
if(ctype[int first[i]] != C->W)
break;
if(n - i >= 6) {
s := string first[i:i+6];
case S->tolower(s) {
"<html " or "<html\t" or "<html>" or "<head>" or "<title" =>
mt = TextHtml;
"<!doct" =>
if(n - i >= 14 && string first[i+6:i+14] == "ype html")
mt = TextHtml;
"gif87a" or "gif89a" =>
if(i == 0)
mt = ImageGif;
"#defin" =>
# perhaps should check more definitively...
mt = ImageXXBitmap;
}
if (mt == UnknownType && n > 0) {
if (first[0] == jpmagic[0] && n >= len jpmagic) {
for(i++; i<len jpmagic; i++)
if(jpmagic[i]>byte 0 && first[i]!=jpmagic[i])
break;
if (i == len jpmagic)
mt = ImageJpeg;
} else if (first[0] == pngsig[0] && n >= len pngsig) {
for(i++; i<len pngsig; i++)
if (first[i] != pngsig[i])
break;
if (i == len pngsig)
mt = ImagePng;
}
}
}
if(mt == UnknownType) {
# Try file name extension
(nil, file) := S->splitr(name, "/");
if(file != "") {
(f, ext) := S->splitr(file, ".");
if(f != "" && ext != "") {
(fnd, val) := T->lookup(fileexttable, S->tolower(ext));
if(fnd)
mt = val;
}
}
}
# if(mt == UnknownType) {
# mt = TextPlain;
# h.chset = "utf8";
# }
h.mtype = mt;
}
Header.print(h: self ref Header)
{
mtype := "?";
if(h.mtype >= 0 && h.mtype < len mnames)
mtype = mnames[h.mtype];
chset := "?";
if(h.chset != nil)
chset = h.chset;
# sys->print("code=%d (%s) length=%d mtype=%s chset=%s\n",
# h.code, hcphrase(h.code), h.length, mtype, chset);
if(h.base != nil)
sys->print(" base=%s\n", h.base.tostring());
if(h.location != nil)
sys->print(" location=%s\n", h.location.tostring());
if(h.refresh != "")
sys->print(" refresh=%s\n", h.refresh);
if(h.chal != "")
sys->print(" chal=%s\n", h.chal);
if(h.warn != "")
sys->print(" warn=%s\n", h.warn);
}
mfd : ref sys->FD = nil;
ResourceState.cur() : ResourceState
{
ms := sys->millisec();
main := 0;
mainlim := 0;
heap := 0;
heaplim := 0;
image := 0;
imagelim := 0;
if(mfd == nil)
mfd = sys->open("/dev/memory", sys->OREAD);
if (mfd == nil)
raisex(sys->sprint("can't open /dev/memory: %r"));
sys->seek(mfd, big 0, Sys->SEEKSTART);
buf := array[400] of byte;
n := sys->read(mfd, buf, len buf);
if (n <= 0)
raisex(sys->sprint("can't read /dev/memory: %r"));
(nil, l) := sys->tokenize(string buf[0:n], "\n");
# p->cursize, p->maxsize, p->hw, p->nalloc, p->nfree, p->nbrk, poolmax(p), p->name)
while(l != nil) {
s := hd l;
cur_size := int s[0:12];
max_size := int s[12:24];
case s[7*12:] {
"main" =>
main = cur_size;
mainlim = max_size;
"heap" =>
heap = cur_size;
heaplim = max_size;
"image" =>
image = cur_size;
imagelim = max_size;
}
l = tl l;
}
return ResourceState(ms, main, mainlim, heap, heaplim, image, imagelim);
}
ResourceState.since(rnew: self ResourceState, rold: ResourceState) : ResourceState
{
return (rnew.ms - rold.ms,
rnew.main - rold.main,
rnew.heaplim,
rnew.heap - rold.heap,
rnew.heaplim,
rnew.image - rold.image,
rnew.imagelim);
}
ResourceState.print(r: self ResourceState, msg: string)
{
sys->print("%s:\n\ttime: %d.%#.3ds; memory: main %dk, mainlim %dk, heap %dk, heaplim %dk, image %dk, imagelim %dk\n",
msg, r.ms/1000, r.ms % 1000, r.main / 1024, r.mainlim / 1024,
r.heap / 1024, r.heaplim / 1024, r.image / 1024, r.imagelim / 1024);
}
# Decide what to do based on Header and whether this is
# for the main entity or not, and the number of redirections-so-far.
# Return tuple contains:
# (use, error, challenge, redir)
# and action to do is:
# If use==1, use the entity else drain its byte source.
# If error != nil, mesg was put in progress bar
# If challenge != nil, get auth info and make new request with auth
# Else if redir != nil, make a new request with redir for url
#
# (if challenge or redir is non-nil, use will be 0)
hdraction(bs: ref ByteSource, ismain: int, nredirs: int) : (int, string, string, ref U->Parsedurl)
{
use := 1;
error := "";
challenge := "";
redir : ref U->Parsedurl = nil;
h := bs.hdr;
assert(h != nil);
bs.seenhdr = 1;
code := h.code;
case code/100 {
HSOk =>
if(code != HCOk)
error = "unexpected code: " + hcphrase(code);
HSRedirect =>
if(h.location != nil) {
redir = h.location;
# spec says url should be absolute, but some
# sites give relative ones
if(redir.scheme == nil)
redir = U->mkabs(redir, h.base);
if(dbg)
sys->print("redirect %s to %s\n", h.actual.tostring(), redir.tostring());
if(nredirs >= Maxredir) {
redir = nil;
error = "probable redirect loop";
}
else
use = 0;
}
HSError =>
if(code == HCUnauthorized && h.chal != "") {
challenge = h.chal;
use = 0;
}
else {
error = hcphrase(code);
use = ismain;
}
HSServererr =>
error = hcphrase(code);
use = ismain;
* =>
error = "unexpected code: " + string code;
use = 0;
}
if(error != "")
G->progress <-= (bs.id, G->Perr, 0, error);
return (use, error, challenge, redir);
}
# Use event when only care about time stamps on events
event(s: string, data: int)
{
sys->print("%s: %d %d\n", s, sys->millisec()-startres.ms, data);
}
kill(pid: int, dogroup: int)
{
msg : array of byte;
if(dogroup)
msg = array of byte "killgrp";
else
msg = array of byte "kill";
ctl := sys->open("#p/" + string pid + "/ctl", sys->OWRITE);
if(ctl != nil)
if (sys->write(ctl, msg, len msg) < 0)
sys->print("charon: kill write failed (pid %d, grp %d): %r\n", pid, dogroup);
}
# Read a line up to and including cr/lf (be tolerant and allow missing cr).
# Look first in buf[bstart:bend], and if that isn't sufficient to get whole line,
# refill buf from fd as needed.
# Return values:
# array of byte: the line, not including cr/lf
# eof, true if there was no line to get or a read error
# bstart', bend': new valid portion of buf (after cr/lf).
getline(fd: ref sys->FD, buf: array of byte, bstart, bend: int) :
(array of byte, int, int, int)
{
ans : array of byte = nil;
last : array of byte = nil;
eof := 0;
mainloop:
for(;;) {
for(i := bstart; i < bend; i++) {
if(buf[i] == byte '\n') {
k := i;
if(k > bstart && buf[k-1] == byte '\r')
k--;
last = buf[bstart:k];
bstart = i+1;
break mainloop;
}
}
if(bend > bstart)
ans = append(ans, buf[bstart:bend]);
last = nil;
bstart = 0;
bend = sys->read(fd, buf, len buf);
if(bend <= 0) {
eof = 1;
bend = 0;
break mainloop;
}
}
return (append(ans, last), eof, bstart, bend);
}
# Append copy of second array to first, return (possibly new)
# address of the concatenation.
append(a: array of byte, b: array of byte) : array of byte
{
if(b == nil)
return a;
na := len a;
nb := len b;
ans := realloc(a, nb);
ans[na:] = b;
return ans;
}
# Return copy of a, but incr bytes bigger
realloc(a: array of byte, incr: int) : array of byte
{
n := len a;
newa := array[n + incr] of byte;
if(a != nil)
newa[0:] = a;
return newa;
}
# Look (linearly) through a for s; return its index if found, else -1.
strlookup(a: array of string, s: string) : int
{
n := len a;
for(i := 0; i < n; i++)
if(s == a[i])
return i;
return -1;
}
# Set up config global to defaults, then try to read user-specifiic
# config data from /usr/<username>/charon/config, then try to
# override from command line arguments.
setconfig(argl: list of string)
{
# Defaults, in absence of any other information
config.userdir = "";
config.srcdir = "/appl/cmd/charon";
config.starturl = "file:/services/webget/start.html";
config.homeurl = config.starturl;
config.change_homeurl = 1;
config.helpurl = "file:/services/webget/help.html";
config.usessl = SSLV3; # was NOSSL
config.devssl = 0;
config.custbkurl = "/services/config/bookmarks.html";
config.dualbkurl = "/services/config/dualdisplay.html";
config.httpproxy = nil;
config.noproxydoms = nil;
config.buttons = "help,resize,hide,exit";
config.framework = "all";
config.defaultwidth = 640;
config.defaultheight = 480;
config.x = -1;
config.y = -1;
config.nocache = 0;
config.maxstale = 0;
config.imagelvl = ImgFull;
config.imagecachenum = 120;
config.imagecachemem = 100000000; # 100Meg, will get lowered later
config.docookies = 1;
config.doscripts = 1;
config.httpminor = 0;
config.agentname = "Mozilla/4.08 (Charon; Inferno)";
config.nthreads = 4;
config.offersave = 1;
config.charset = "windows-1252";
config.plumbport = "web";
config.wintitle = "Charon"; # tkclient->titlebar() title, used by GUI
config.dbgfile = "";
config.dbg = array[128] of { * => byte 0 };
# Reading default config file
readconf("/services/config/charon.cfg");
# Try reading user config file
user := "";
fd := sys->open("/dev/user", sys->OREAD);
if(fd != nil) {
b := array[40] of byte;
n := sys->read(fd, b, len b);
if(n > 0)
user = string b[0:n];
}
if(user != "") {
config.userdir = "/usr/" + user + "/charon";
readconf(config.userdir + "/config");
}
if(argl == nil)
return;
# Try command line arguments
# All should be 'key=val' or '-key' or '-key val', except last which can be url to start
for(l := tl argl; l != nil; l = tl l) {
s := hd l;
if(s == "")
continue;
if (s[0] != '-')
break;
a := s[1:];
b := "";
if(tl l != nil) {
b = hd tl l;
if(S->prefix("-", b))
b = "";
else
l = tl l;
}
if(!setopt(a, b)) {
if (b != nil)
s += " "+b;
sys->print("couldn't set option from arg '%s'\n", s);
}
}
if(l != nil) {
if (tl l != nil)
# usage error
sys->print("too many URL's\n");
else
if(!setopt("starturl", hd l))
sys->print("couldn't set starturl from arg '%s'\n", hd l);
}
}
readconf(fname: string)
{
cfgio := sys->open(fname, sys->OREAD);
if(cfgio != nil) {
buf := array[sys->ATOMICIO] of byte;
i := 0;
j := 0;
aline : array of byte;
eof := 0;
for(;;) {
(aline, eof, i, j) = getline(cfgio, buf, i, j);
if(eof)
break;
line := string aline;
if(len line == 0 || line[0]=='#')
continue;
(key, val) := S->splitl(line, " \t=");
if(key != "") {
val = S->take(S->drop(val, " \t="), "^#\r\n");
if(!setopt(key, val))
sys->print("couldn't set option from line '%s'\n", line);
}
}
}
}
# Set config option named 'key' to val, returning 1 if OK
setopt(key: string, val: string) : int
{
ok := 1;
if(val == "none")
val = "";
v := int val;
case key {
"userdir" =>
config.userdir = val;
"srcdir" =>
config.srcdir = val;
"starturl" =>
if(val != "")
config.starturl = val;
else
ok = 0;
"change_homeurl" =>
config.change_homeurl = v;
"homeurl" =>
if(val != "")
if(config.change_homeurl) {
config.homeurl = val;
# order dependent
config.starturl = config.homeurl;
}
else
ok = 0;
"helpurl" =>
if(val != "")
config.helpurl = val;
else
ok = 0;
"usessl" =>
if(val == "v2")
config.usessl |= SSLV2;
if(val == "v3")
config.usessl |= SSLV3;
"devssl" =>
if(v == 0)
config.devssl = 0;
else
config.devssl = 1;
# "custbkurl" =>
# "dualbkurl" =>
"httpproxy" =>
if(val != "")
config.httpproxy = makeabsurl(val);
else
config.httpproxy = nil;
"noproxy" or "noproxydoms" =>
(nil, config.noproxydoms) = sys->tokenize(val, ";, \t");
"buttons" =>
config.buttons = S->tolower(val);
"framework" =>
config.framework = S->tolower(val);
"defaultwidth" or "width" =>
if(v > 200)
config.defaultwidth = v;
else
ok = 0;
"defaultheight" or "height" =>
if(v > 100)
config.defaultheight = v;
else
ok = 0;
"x" =>
config.x = v;
"y" =>
config.y = v;
"nocache" =>
config.nocache = v;
"maxstale" =>
config.maxstale = v;
"imagelvl" =>
config.imagelvl = v;
"imagecachenum" =>
config.imagecachenum = v;
"imagecachemem" =>
config.imagecachemem = v;
"docookies" =>
config.docookies = v;
"doscripts" =>
config.doscripts = v;
"http" =>
if(val == "1.1")
config.httpminor = 1;
else
config.httpminor = 0;
"agentname" =>
config.agentname = val;
"nthreads" =>
if (v < 1)
ok = 0;
else
config.nthreads = v;
"offersave" =>
if (v < 1)
config.offersave = 0;
else
config.offersave = 1;
"charset" =>
config.charset = val;
"plumbport" =>
config.plumbport = val;
"wintitle" =>
config.wintitle = val;
"dbgfile" =>
config.dbgfile = val;
"dbg" =>
for(i := 0; i < len val; i++) {
c := val[i];
if(c < len config.dbg)
config.dbg[c]++;
else {
ok = 0;
break;
}
}
* =>
ok = 0;
}
return ok;
}
saveconfig(): int
{
fname := config.userdir + "/config";
buf := array [Sys->ATOMICIO] of byte;
fd := sys->create(fname, Sys->OWRITE, 8r600);
if(fd == nil)
return -1;
nbyte := savealine(fd, buf, "# Charon user configuration\n", 0);
nbyte = savealine(fd, buf, "userdir=" + config.userdir + "\n", nbyte);
nbyte = savealine(fd, buf, "srcdir=" + config.srcdir +"\n", nbyte);
if(config.change_homeurl){
nbyte = savealine(fd, buf, "starturl=" + config.starturl + "\n", nbyte);
nbyte = savealine(fd, buf, "homeurl=" + config.homeurl + "\n", nbyte);
}
if(config.httpproxy != nil)
nbyte = savealine(fd, buf, "httpproxy=" + config.httpproxy.tostring() + "\n", nbyte);
if(config.usessl & SSLV23) {
nbyte = savealine(fd, buf, "usessl=v2\n", nbyte);
nbyte = savealine(fd, buf, "usessl=v3\n", nbyte);
}
else {
if(config.usessl & SSLV2)
nbyte = savealine(fd, buf, "usessl=v2\n", nbyte);
if(config.usessl & SSLV3)
nbyte = savealine(fd, buf, "usessl=v3\n", nbyte);
}
if(config.devssl == 0)
nbyte = savealine(fd, buf, "devssl=0\n", nbyte);
else
nbyte = savealine(fd, buf, "devssl=1\n", nbyte);
if(config.noproxydoms != nil) {
doms := "";
doml := config.noproxydoms;
while(doml != nil) {
doms += hd doml + ",";
doml = tl doml;
}
nbyte = savealine(fd, buf, "noproxy=" + doms + "\n", nbyte);
}
nbyte = savealine(fd, buf, "defaultwidth=" + string config.defaultwidth + "\n", nbyte);
nbyte = savealine(fd, buf, "defaultheight=" + string config.defaultheight + "\n", nbyte);
if(config.x >= 0)
nbyte = savealine(fd, buf, "x=" + string config.x + "\n", nbyte);
if(config.y >= 0)
nbyte = savealine(fd, buf, "y=" + string config.y + "\n", nbyte);
nbyte = savealine(fd, buf, "nocache=" + string config.nocache + "\n", nbyte);
nbyte = savealine(fd, buf, "maxstale=" + string config.maxstale + "\n", nbyte);
nbyte = savealine(fd, buf, "imagelvl=" + string config.imagelvl + "\n", nbyte);
nbyte = savealine(fd, buf, "imagecachenum=" + string config.imagecachenum + "\n", nbyte);
nbyte = savealine(fd, buf, "imagecachemem=" + string config.imagecachemem + "\n", nbyte);
nbyte = savealine(fd, buf, "docookies=" + string config.docookies + "\n", nbyte);
nbyte = savealine(fd, buf, "doscripts=" + string config.doscripts + "\n", nbyte);
nbyte = savealine(fd, buf, "http=" + "1." + string config.httpminor + "\n", nbyte);
nbyte = savealine(fd, buf, "agentname=" + string config.agentname + "\n", nbyte);
nbyte = savealine(fd, buf, "nthreads=" + string config.nthreads + "\n", nbyte);
nbyte = savealine(fd, buf, "charset=" + config.charset + "\n", nbyte);
#for(i := 0; i < len config.dbg; i++)
#nbyte = savealine(fd, buf, "dbg=" + string config.dbg[i] + "\n", nbyte);
if(nbyte > 0)
sys->write(fd, buf, nbyte);
return 0;
}
savealine(fd: ref Sys->FD, buf: array of byte, s: string, n: int): int
{
if(Sys->ATOMICIO < n + len s) {
sys->write(fd, buf, n);
buf[0:] = array of byte s;
return len s;
}
buf[n:] = array of byte s;
return n + len s;
}
# Make a StringInt table out of a, mapping each string
# to its index. Check that entries are in alphabetical order.
makestrinttab(a: array of string) : array of T->StringInt
{
n := len a;
ans := array[n] of T->StringInt;
for(i := 0; i < n; i++) {
ans[i].key = a[i];
ans[i].val = i;
if(i > 0 && a[i] < a[i-1])
raisex("EXInternal: table out of alphabetical order");
}
return ans;
}
# Should really move into Url module.
# Don't include fragment in test, since we are testing if the
# pointed to docs are the same, not places within docs.
urlequal(a, b: ref U->Parsedurl) : int
{
return a.scheme == b.scheme
&& a.host == b.host
&& a.port == b.port
&& a.user == b.user
&& a.passwd == b.passwd
&& a.path == b.path
&& a.query == b.query;
}
# U->makeurl, but add http:// if not an absolute path already
makeabsurl(s: string) : ref Parsedurl
{
if (s == "")
return nil;
u := U->parse(s);
if (u.scheme != nil)
return u;
if (s[0] == '/')
# try file:
s = "file://localhost" + s;
else
# try http
s = "http://" + s;
u = U->parse(s);
return u;
}
# Return place to load from, given installed-path name.
# (If config.dbg['u'] is set, change directory to config.srcdir.)
loadpath(s: string) : string
{
if(config.dbg['u'] == byte 0)
return s;
(nil, f) := S->splitr(s, "/");
return config.srcdir + "/" + f;
}
color_tab := array[] of { T->StringInt
("aqua", 16r00FFFF),
("black", Black),
("blue", Blue),
("fuchsia", 16rFF00FF),
("gray", 16r808080),
("green", 16r008000),
("lime", 16r00FF00),
("maroon", 16r800000),
("navy", Navy),
("olive", 16r808000),
("purple", 16r800080),
("red", Red),
("silver", 16rC0C0C0),
("teal", 16r008080),
("white", White),
("yellow", 16rFFFF00)
};
# Convert HTML color spec to RGB value, returning dflt if can't.
# Argument is supposed to be a valid HTML color, or "".
# Return the RGB value of the color, using dflt if s
# is "" or an invalid color.
color(s: string, dflt: int) : int
{
if(s == "")
return dflt;
s = S->tolower(s);
c := s[0];
if(c < C->NCTYPE && ctype[c] == C->L) {
(fnd, v) := T->lookup(color_tab, s);
if(fnd)
return v;
}
if(s[0] == '#')
s = s[1:];
(v, rest) := S->toint(s, 16);
if(rest == "")
return v;
# s was invalid, so choose a valid one
return dflt;
}
max(a,b: int) : int
{
if(a > b)
return a;
return b;
}
min(a,b: int) : int
{
if(a < b)
return a;
return b;
}
raisex(e: string)
{
raise e;
}
assert(i: int)
{
if(!i) {
raisex("EXInternal: assertion failed");
# sys->print("assertion failed\n");
# s := hmeth[-1];
}
}
getcookies(host, path: string, secure: int): string
{
if (CK == nil || ckclient == nil)
return nil;
Client: import CK;
return ckclient.getcookies(host, path, secure);
}
setcookie(host, path, cookie: string)
{
if (CK == nil || ckclient == nil)
return;
Client: import CK;
ckclient.set(host, path, cookie);
}
ex_mkdir(dirname: string): int
{
(ok, nil) := sys->stat(dirname);
if(ok < 0) {
f := sys->create(dirname, sys->OREAD, sys->DMDIR + 8r777);
if(f == nil) {
sys->print("mkdir: can't create %s: %r\n", dirname);
return 0;
}
f = nil;
}
return 1;
}
stripscript(s: string): string
{
# strip leading whitespace and SGML comment start symbol '<!--'
if (s == nil)
return nil;
cs := "<!--";
ci := 0;
for (si := 0; si < len s; si++) {
c := s[si];
if (c == cs[ci]) {
if (++ci >= len cs)
ci = 0;
} else {
ci = 0;
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
continue;
break;
}
}
# strip trailing whitespace and SGML comment terminator '-->'
cs = "-->";
ci = len cs -1;
for (se := len s - 1; se > si; se--) {
c := s[se];
if (c == cs[ci]) {
if (ci-- == 0)
ci = len cs -1;
} else {
ci = len cs - 1;
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
continue;
break;
}
}
if (se < si)
return nil;
return s[si:se+1];
}
# Split a value (guaranteed trimmed) into sep-separated list of one of
# token
# token = token
# token = "quoted string"
# and put into list of Namevals (lowercase the first token)
Nameval.namevals(s: string, sep: int) : list of Nameval
{
ans : list of Nameval = nil;
n := len s;
i := 0;
while(i < n) {
tok : string;
(tok, i) = gettok(s, i, n);
if(tok == "")
break;
tok = S->tolower(tok);
val := "";
while(i < n && ctype[s[i]] == C->W)
i++;
if(i == n || s[i] == sep)
i++;
else if(s[i] == '=') {
i++;
while(i < n && ctype[s[i]] == C->W)
i++;
if (i == n)
break;
if(s[i] == '"')
(val, i) = getqstring(s, i, n);
else
(val, i) = gettok(s, i, n);
}
else
break;
ans = Nameval(tok, val) :: ans;
}
return ans;
}
gettok(s: string, i,n: int) : (string, int)
{
while(i < n && ctype[s[i]] == C->W)
i++;
if(i == n)
return ("", i);
is := i;
for(; i < n; i++) {
c := s[i];
ct := ctype[c];
if(!(int (ct&(C->D|C->L|C->U|C->N|C->S))))
if(int (ct&(C->W|C->C)) || S->in(c, "()<>@,;:\\\"/[]?={}"))
break;
}
return (s[is:i], i);
}
# get quoted string; return it without quotes, and index after it
getqstring(s: string, i,n: int) : (string, int)
{
while(i < n && ctype[s[i]] == C->W)
i++;
if(i == n || s[i] != '"')
return ("", i);
is := ++i;
for(; i < n; i++) {
c := s[i];
if(c == '\\')
i++;
else if(c == '"')
return (s[is:i], i+1);
}
return (s[is:i], i);
}
# Find value corresponding to key (should be lowercase)
# and return (1, value) if found or (0, "")
Nameval.find(l: list of Nameval, key: string) : (int, string)
{
for(; l != nil; l = tl l)
if((hd l).key == key)
return (1, (hd l).val);
return (0, "");
}
# this should be a converter cache
getconv(chset : string) : Btos
{
(btos, err) := convcs->getbtos(chset);
if (err != nil)
sys->print("Converter error: %s\n", err);
return btos;
}
X(s, note : string) : string
{
if (dict == nil)
return s;
return dict.xlaten(s, note);
}
|