simple_components_4.68.0_da9b0f3a/gnat-sockets-connection_state_machine-http_server.ads

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
--                                                                    --
--  package                         Copyright (c)  Dmitry A. Kazakov  --
--     GNAT.Sockets.Connection_State_Machine.      Luebeck            --
--     HTTP_Server                                 Winter, 2013       --
--  Interface                                                         --
--                                Last revision :  20:46 27 Aug 2020  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, or (at your option) any later version. This library  --
--  is distributed in the hope that it will be useful,  but  WITHOUT  --
--  ANY   WARRANTY;   without   even   the   implied   warranty   of  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.Calendar;                   use Ada.Calendar;
with Ada.Exceptions;                 use Ada.Exceptions;
with Ada.Strings.Maps;               use Ada.Strings.Maps;
with Ada.Task_Identification;        use Ada.Task_Identification;
with Strings_Edit.Time_Conversions;  use Strings_Edit.Time_Conversions;

with Ada.Finalization;
with Generic_Discrete_Set;
with GNAT.Sockets.Connection_State_Machine.Expected_Sequence;
with GNAT.Sockets.Connection_State_Machine.Terminated_Strings;
with Stack_Storage;
with Tables.Names;

package GNAT.Sockets.Connection_State_Machine.HTTP_Server is
--
-- HTTP_Method -- Methods
--
   type HTTP_Method is
        (  HTTP_GET,
           HTTP_HEAD,
           HTTP_POST,
           HTTP_PUT,
           HTTP_DELETE,
           HTTP_TRACE,
           HTTP_OPTIONS,
           HTTP_CONNECT,
           HTTP_PATCH
        );
   type HTTP_Allowed is array (HTTP_Method) of Boolean;
   type HTTP_Version is delta 0.1 digits 6 range 1.0..1_000.0;
--
-- Scheme_Type -- Recognized  schemes  (RFC  3986). Permanentl, historic
--                and provisional schemas registered to 2017-09-13
--
   type Scheme_Type is
        (  Aaa_Scheme,
           Aaas_Scheme,
           About_Scheme,
           Acap_Scheme,
           Acct_Scheme,
           Acr_Scheme,
           Adiumxtra_Scheme,
           AFP_Scheme,
           AFS_Scheme,
           Aim_Scheme,
           Appdata_Scheme,
           APT_Scheme,
           Attachment_Scheme,
           Aw_Scheme,

           Barion_Scheme,
           Beshare_Scheme,
           Bitcoin_Scheme,
           Blob_Scheme,
           Bolo_Scheme,
           Browserext_Scheme,

           Callto_Scheme,
           Cap_Scheme,
           Chrome_Scheme,
           Chrome_Extension_Scheme,
           Cid_Scheme,
           Coap_Scheme,
           Coap_Tcp_Scheme,
           Coaps_Scheme,
           Coaps_Tcp_Scheme,
           Com_Eventbrite_Attendee_Scheme,
           Content_Scheme,
           Crid_Scheme,
           CVS_Scheme,

           Data_Scheme,
           Dav_Scheme,
           Diaspora_Scheme,
           Dict_Scheme,
           DIS_Scheme,
           DLNA_Playcontainer_Scheme,
           DLNA_Playsingle_Scheme,
           DNS_Scheme,
           DNTP_Scheme,
           DTN_Scheme,
           DVB_Scheme,

           Ed2k_Scheme,
           Example_Scheme,

           Facetime_Scheme,
           Fax_Scheme,
           Feed_Scheme,
           Feedready_Scheme,
           File_Scheme,
           Filesystem_Scheme,
           Finger_Scheme,
           Fish_Scheme,
           FTP_Scheme,

           Geo_Scheme,
           Gg_Scheme,
           Git_Scheme,
           Gizmoproject_Scheme,
           Go_Scheme,
           Gopher_Scheme,
           Graph_Scheme,
           Gtalk_Scheme,

           H323_Scheme,
           Ham_Scheme,
           HCP_Scheme,
           HTTP_Scheme,
           HTTPS_Scheme,
           HXXP_Scheme,
           HXXPS_Scheme,
           Hydrazone_Scheme,

           Iax_Scheme,
           Icap_Scheme,
           Icon_Scheme,
           Im_Scheme,
           Imap_Scheme,
           Info_Scheme,
           Iotdisco_Scheme,
           IPN_Scheme,
           IPP_Scheme,
           IPPS_Scheme,
           IRC_Scheme,
           IRC6_Scheme,
           IRCS_Scheme,
           Iris_Scheme,
           Iris_Beep_Scheme,
           Iris_LWZ_Scheme,
           Iris_XPC_Scheme,
           Iris_XPCS_Scheme,
           Isostore_Scheme,
           ITMS_Scheme,

           Jabber_Scheme,
           Jar_Scheme,
           Jms_Scheme,

           Keyparc_Scheme,

           Lastfm_Scheme,
           LDAP_Scheme,
           LDAPS_Scheme,
           LVLT_Scheme,

           Magnet_Scheme,
           Mailserver_Scheme,
           Mailto_Scheme,
           Maps_Scheme,
           Market_Scheme,
           Message_Scheme,
           Mid_Scheme,
           MMS_Scheme,
           Modem_Scheme,
           Mongodb_Scheme,
           MOZ_Scheme,
           MS_Access_Scheme,
           MS_Browser_Extension_Scheme,
           MS_Drive_To_Scheme,
           MS_Enrollment_Scheme,
           MS_Excel_Scheme,
           MS_Gamebarservices_Scheme,
           MS_Getoffice_Scheme,
           MS_Help_Scheme,
           MS_Infopath_Scheme,
           MS_Inputapp_Scheme,
           MS_Media_Stream_ID_Scheme,
           MS_Officeapp_Scheme,
           MS_People_Scheme,
           MS_Project_Scheme,
           MS_Powerpoint_Scheme,
           MS_Publisher_Scheme,
           MS_Search_Repair_Scheme,
           MS_Secondary_Screen_Controller_Scheme,
           MS_Secondary_Screen_Setup_Scheme,
           MS_Settings_Scheme,
           MS_Settings_Airplanemode_Scheme,
           MS_Settings_Bluetooth_Scheme,
           MS_Settings_Camera_Scheme,
           MS_Settings_Cellular_Scheme,
           MS_Settings_Cloudstorage_Scheme,
           MS_Settings_Connectabledevices_Scheme,
           MS_Settings_Displays_Topology_Scheme,
           MS_Settings_Emailandaccounts_Scheme,
           MS_Settings_Language_Scheme,
           MS_Settings_Location_Scheme,
           MS_Settings_Lock_Scheme,
           MS_Settings_Nfctransactions_Scheme,
           MS_Settings_Notifications_Scheme,
           MS_Settings_Power_Scheme,
           MS_Settings_Privacy_Scheme,
           MS_Settings_Proximity_Scheme,
           MS_Settings_Screenrotation_Scheme,
           MS_Settings_WiFi_Scheme,
           MS_Settings_Workplace_Scheme,
           MS_SPD_Scheme,
           MS_Sttoverlay_Scheme,
           MS_Transit_To_Scheme,
           MS_Virtualtouchpad_Scheme,
           MS_Visio_Scheme,
           MS_Walk_To_Scheme,
           MS_Whiteboard_Scheme,
           MS_Whiteboard_CMD_Scheme,
           MS_Word_Scheme,
           MSnim_Scheme,
           MSRP_Scheme,
           MSRPS_Scheme,
           MTQP_Scheme,
           Mumble_Scheme,
           Mupdate_Scheme,
           MVN_Scheme,

           News_Scheme,
           NFS_Scheme,
           NI_Scheme,
           NIH_Scheme,
           NNTP_Scheme,
           Notes_Scheme,

           OCF_Scheme,
           OID_Scheme,
           Onenote_Scheme,
           Onenote_CMD_Scheme,
           Opaquelocktoken_Scheme,

           Pack_Scheme,
           Palm_Scheme,
           Paparazzi_Scheme,
           Pkcs11_Scheme,
           Platform_Scheme,
           POP_Scheme,
           Pres_Scheme,
           Prospero_Scheme,
           Proxy_Scheme,
           Pwid_Scheme,
           Psyc_Scheme,

           QB_Scheme,
           Query_Scheme,

           Redis_Scheme,
           Rediss_Scheme,
           Reload_Scheme,
           Res_Scheme,
           Resource_Scheme,
           RMI_Scheme,
           Rsync_Scheme,
           RTMFP_Scheme,
           RTMP_Scheme,
           RTSP_Scheme,
           RTSPS_Scheme,
           RTSPU_Scheme,

           Secondlife_Scheme,
           Service_Scheme,
           Session_Scheme,
           SFTP_Scheme,
           SGN_Scheme,
           SHTTP_Scheme,
           Sieve_Scheme,
           Sip_Scheme,
           Sips_Scheme,
           Skype_Scheme,
           SMB_Scheme,
           SMS_Scheme,
           SMTP_Scheme,
           SNews_Scheme,
           SNTP_Scheme,
           Soap_Beep_Scheme,
           Soap_Beeps_Scheme,
           Soldat_Scheme,
           Spotify_Scheme,
           SSH_Scheme,
           Steam_Scheme,
           Stun_Scheme,
           Stuns_Scheme,
           Submit_Scheme,
           SVN_Scheme,

           Tag_Scheme,
           Teamspeak_Scheme,
           Tel_Scheme,
           Teliaeid_Scheme,
           Telnet_Scheme,
           TFTP_Scheme,
           Things_Scheme,
           Thismessage_Scheme,
           Tip_Scheme,
           Tn3270_Scheme,
           Tool_Scheme,
           Turn_Scheme,
           Turns_Scheme,
           TV_Scheme,

           UDP_Scheme,
           Unreal_Scheme,
           URN_Scheme,
           UT2004_Scheme,

           V_Event_Scheme,
           VEMMI_Scheme,
           Ventrilo_Scheme,
           Videotex_Scheme,
           VNC_Scheme,
           View_Source_Scheme,

           Wais_Scheme,
           Webcal_Scheme,
           Wpid_Scheme,
           WS_Scheme,
           WSS_Scheme,
           WTAI_Scheme,
           Wyciwyg_Scheme,

           Xcon_Scheme,
           Xcon_Userid_Scheme,
           Xfire_Scheme,
           XMLRPC_Beep_Scheme,
           XMLRPC_Beeps_Scheme,
           XMPP_Scheme,
           XRI_Scheme,

           YMSGR_Scheme,

           Z39_50_Scheme,
           Z39_50r_Scheme,
           Z39_50s_Scheme,

           Unknown_Scheme
        );
--
-- Image -- Scheme name
--
--    Scheme - The scheme
--
-- Returns :
--
--    The corresponding name
--
   function Image (Scheme : Scheme_Type) return String;
--
-- Request_Header -- Request header fields
--
   type Request_Header is
        (  Accept_Header,
           Accept_Charset_Header,
           Accept_Encoding_Header,
           Accept_Language_Header,
           Accept_Datetime_Header,
           Allow_Header,
           Authorization_Header,
           Cache_Control_Header,
           Cookie_Header,
           Content_Encoding_Header,
           Content_Disposition_Header,
           Content_Language_Header,
           Content_Location_Header,
           Content_MD5_Header,
           Content_Type_Header,
           Expect_Header,
           Expires_Header,
           From_Header,
           Host_Header,
           If_Match_Header,
           If_None_Match_Header,
           If_Range_Header,
           Max_Forwards_Header,
           Origin_Header,
           Proxy_Authorization_Header,
           Pragma_Header,
           Referer_Header,
           TE_Header,
           Trailer_Header,
           Transfer_Encoding_Header,
           Upgrade_Header,
           Upgrade_Insecure_Requests,
           User_Agent_Header,
           Via_Header,
           Sec_WebSocket_Extensions_Header,
           Sec_WebSocket_Protocol_Header,
           Sec_WebSocket_Version_Header,
           Sec_WebSocket_Key_Header,
           X_Requested_By_Header,   -- Non-standard text header
           X_Requested_With_Header, -- Non-standard text header
           X_XSRF_Token_Header,     -- Non-standard text header
           X_CSRF_Token_Header,     -- Non-standard text header
           Warning_Header,

           Range_Header,

           Connection_Header,
           Content_Length_Header,
           Date_Header,
           If_Modified_Since_Header,
           If_Unmodified_Since_Header,
           Last_Modified_Header
        );
--
-- Image -- Header name
--
--    Header - The header type
--
-- Returns :
--
--    The corresponding name, e.g. Keep-Alive
--
   function Image (Header : Request_Header) return String;
--
-- Value -- String to header type conversion
--
--    Text - The header name (case-insensitive)
--
-- Returns :
--
--    The header type
--
   type Header_Value (None : Boolean := True) is record
      case None is
         when True =>
            null;
         when False =>
            Header : Request_Header;
      end case;
   end record;
   function Value (Text : String) return Header_Value;

   subtype Text_Header is Request_Header
      range Accept_Header..Warning_Header;
   subtype Multipart_Header is Request_Header
      range Content_Encoding_Header..Content_Type_Header;

   type WebSocket_Status is range 0..2**16 - 1;
--
-- Status codes in the range 0-999 are not used.
--
   subtype WebSocket_Unused   is WebSocket_Status range 0..999;
--
-- Status codes in the range 1000-2999 are reserved  for  definition  by
-- this  protocol,  its  future revisions, and extensions specified in a
-- permanent and readily available public specification.
--
   subtype WebSocket_Mandated is WebSocket_Status range 1000..2999;
   WebSocket_Normal_Closure    : constant WebSocket_Mandated := 1000;
   WebSocket_Endpoint_Shutdown : constant WebSocket_Mandated := 1001;
   WebSocket_Protocol_Error    : constant WebSocket_Mandated := 1002;
   WebSocket_Type_Error        : constant WebSocket_Mandated := 1003;
   WebSocket_No_Status_Code    : constant WebSocket_Mandated := 1005;
   WebSocket_Aborted           : constant WebSocket_Mandated := 1006;
   WebSocket_Data_Error        : constant WebSocket_Mandated := 1007;
   WebSocket_Generic_Error     : constant WebSocket_Mandated := 1008;
   WebSocket_Constraint_Error  : constant WebSocket_Mandated := 1009;
   WebSocket_Not_Supported     : constant WebSocket_Mandated := 1010;
   WebSocket_Internal_Error    : constant WebSocket_Mandated := 1011;
   WebSocket_TLS_Error         : constant WebSocket_Mandated := 1012;
--
-- Status  codes  in  the  range  3000-3999  are  reserved  for  use  by
-- libraries, frameworks,  and  applications.  These  status  codes  are
-- registered directly with IANA. The interpretation of these  codes  is
-- undefined by this protocol.
--
   subtype WebSocket_IANA     is WebSocket_Status range 3000..3999;
--
-- Status  codes in the range 4000-4999 are reserved for private use and
-- thus  can't be registered. Such codes can be used by prior agreements
-- between WebSocket applications. The interpretation of these codes  is
-- undefined by this protocol.
--
   subtype WebSocket_Private  is WebSocket_Status range 4000..4999;

   subtype WebSocket_Message_Size is Stream_Element_Count
      range 125..Stream_Element_Count'Last;
--
-- Content_Ranges -- Ranges of content bytes
--
   package Content_Ranges is
      new Generic_Discrete_Set (Stream_Element_Count);
--
-- Range_Type -- Type of range
--
--    Explicit_Range - A range with both bounds specified
--    Suffix_Range   - A range with unknown upper bound
--
   type Range_Type is (Explicit_Range, Suffix_Range);
--
-- Ranges_Set -- Set of ranges with or without one suffix range
--
   type Ranges_Set (Kind : Range_Type := Explicit_Range) is record
      Set : Content_Ranges.Set; -- Specified ranges
      case Kind is
         when Explicit_Range =>
            null;
         when Suffix_Range =>
            Tail : Stream_Element_Offset;
      end case;
   end record;
------------------------------------------------------------------------
   Content_Not_Ready : exception;
--
-- Content_Source -- User-provided content
--
   type Content_Source is
      abstract new Ada.Finalization.Limited_Controlled with null record;
--
-- Get -- Get next piece of data
--
--    Source - The content source
--
-- The length of returned piece  should  not exceed  the capacity of the
-- output buffer including prefix and suffix used for chunked transfer.
--
-- Returns :
--
--    The chunk of data
--
-- Exceptions :
--
--    Content_Not_Ready - No content ready yet
--
   function Get (Source : access Content_Source)
      return String is abstract;
------------------------------------------------------------------------
-- Content_Destination -- User-provided content
--
   type Content_Destination is
      abstract new Ada.Finalization.Limited_Controlled with null record;
--
-- Commit -- Finish receipt
--
--    Destination - The content
--
-- This procedure is called to finish successful receipt of the content.
-- The default implementation does nothing.
--
   procedure Commit (Destination : in out Content_Destination);
--
-- Put -- Store next piece of data
--
--    Destination - The content
--    Data        - Chunk of data to store
--
   procedure Put
             (  Destination : in out Content_Destination;
                Data        : String
             )  is abstract;
--
-- CGI_Keys -- Key to value maps.  Memory  allocation  of  values in the
--             table is managed by the connection object. They shall not
-- be freed or changed.
--
   type String_Ptr is access all String;
   package CGI_Keys is new Tables (String_Ptr);
------------------------------------------------------------------------
-- HTTP_Client -- An object  implementing  HTTP 1.1  connection  from  a
--                client
--
--    Listener       - The connection object
--    Request_Length - The maximum length of one request line
--    Input_Size     - The input buffer size
--    Output_Size    - The output buffer size
--
-- The output  buffer  size should  be large  enough  to accommodate all
-- headers  of a response  and its status line and the body when that is
-- sent as a single string.  The response body sent from  a stream  or a
-- content source  using chunked transfer are  not bound  by t he output
-- buffer size.
--
   type HTTP_Client
        (  Listener       : access Connections_Server'Class;
           Request_Length : Positive;
           Input_Size     : Buffer_Length;
           Output_Size    : Buffer_Length
        )  is new State_Machine with private;
--
-- Status_Line_Type -- Request status line type
--
--    None - Nothing specified
--    File - File path specified
--    URI  - URI specified
--
   type Status_Line_Type is (None, File, URI);
--
-- Status_Line -- Status line
--
   type Status_Line
        (  Kind         : Status_Line_Type;
           Path_Length  : Natural;
           Host_Length  : Natural;
           Query_Length : Natural
        )  is
   record
      Query : String (1..Query_Length);
      case Kind is
         when None =>
            null;
         when File =>
            File : String (1..Path_Length);
         when URI =>
            Scheme : Scheme_Type;
            Host   : String (1..Host_Length);
            Port   : Port_Type;
            Path   : String (1..Path_Length);
      end case;
   end record;
--
-- Finalize -- Destruction
--
--    Client - The client connection object
--
-- This  procedure  shall  be called  from  the  new  implementation  if
-- overridden by the derived type.
--
   procedure Finalize (Client : in out HTTP_Client);
--
-- Get_Allowed -- The list options the server supports
--
--    Client - The client connection object
--
-- Returns :
--
--    The set of supported requests as reperted to OPTIONS
--
   function Get_Allowed (Client : HTTP_Client) return HTTP_Allowed;
--
-- Get_Name -- The HTTP server name
--
--    Client - The client connection object
--
-- This function can be overridden to provide a custom name  of the HTTP
-- server.
--
-- Returns :
--
--    The server name as told to the clients
--
   function Get_Name (Client : HTTP_Client) return String;
--
-- Initialize -- Construction
--
--    Client - The client connection object
--
-- This  procedure  shall  be called  from  the  new  implementation  if
-- overridden by the derived type.
--
   procedure Initialize (Client : in out HTTP_Client);
--
-- Received -- Overrides GNAT.Sockets.Connection_State_Machine...
--
   procedure Received
             (  Client  : in out HTTP_Client;
                Data    : Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             );
--
-- Set_Allowed -- Change the list options the server supports
--
--    Client  - The client connection object
--    Allowed - The set of supported requests (as reperted to OPTIONS)
--
   procedure Set_Allowed
             (  Client  : in out HTTP_Client;
                Allowed : HTTP_Allowed
             );
--
-- Trace -- Tracing
--
--    Client  - The client connection object
--    Message - To write into the trace
--
   procedure Trace
             (  Client  : in out HTTP_Client;
                Message : String
             );
------------------------------------------------------------------------
--
-- Do_<method> -- Callback on client requests, such as GET and POST
--
--    Client - The client connection object
--
   procedure Do_Connect   (Client : in out HTTP_Client);
   procedure Do_Delete    (Client : in out HTTP_Client);
   procedure Do_Get       (Client : in out HTTP_Client);
   procedure Do_Head      (Client : in out HTTP_Client);
   procedure Do_Options   (Client : in out HTTP_Client);
   procedure Do_Patch     (Client : in out HTTP_Client);
   procedure Do_Post      (Client : in out HTTP_Client);
   procedure Do_Put       (Client : in out HTTP_Client);
   procedure Do_Trace     (Client : in out HTTP_Client);
   procedure Do_WebSocket (Client : in out HTTP_Client);
------------------------------------------------------------------------
-- Request header information
--
-- Get_Closing -- The last connection request header
--
--    Client - The client connection object
--
-- When True is the result the connection  will be closed as soon as the
-- last data are sent to the client.
--
-- Returns :
--
--    True if connection header has "close"
--
   function Get_Closing (Client : HTTP_Client) return Boolean;
--
-- Get_Date -- The last date request header
--
--    Client - The client connection object
--
-- Returns :
--
--    The time stamp
--
-- Exceptions :
--
--    Time_Error - No date specified
--
   function Get_Date (Client : HTTP_Client) return Time;
--
-- Get_Header -- The last request header specified as a text
--
--    Client - The client connection object
--    Header - Request header
--
-- Returns :
--
--    The header value of the current request or empty string
--
   function Get_Header
            (  Client : HTTP_Client;
               Header : Text_Header
            )  return String;
--
-- Get_If_Modified_Since -- The last date request header
--
--    Client - The client connection object
--
-- Returns :
--
--    The time stamp
--
   function Get_If_Modified_Since (Client : HTTP_Client) return Time;
--
-- Get_If_Unmodified_Since -- The last date request header
--
--    Client - The client connection object
--
-- Returns :
--
--    The time stamp
--
   function Get_If_Unmodified_Since (Client : HTTP_Client) return Time;
--
-- Get_Last_Modified -- The last date request header
--
--    Client - The client connection object
--
-- Returns :
--
--    The time stamp
--
   function Get_Last_Modified (Client : HTTP_Client) return Time;
--
-- Get_Method -- The last request method
--
--    Client - The client connection object
--
-- Returns :
--
--    The method of the current request
--
   function Get_Method (Client : HTTP_Client) return HTTP_Method;
--
-- Get_Ranges -- The ranges specified by the last request header
--
--    Client - The client connection object
--
-- Returns :
--
--    The ranges specified (empty if none)
--
   function Get_Ranges (Client : HTTP_Client) return Ranges_Set;
--
-- Get_Status_Line -- The last request status line
--
--    Client - The client connection object
--
-- Returns :
--
--    The status line of the current request
--
   function Get_Status_Line (Client : HTTP_Client) return Status_Line;
--
-- Get_Version -- The last request method
--
--    Client - The client connection object
--
-- Returns :
--
--    The version of the current request
--
   function Get_Version (Client : HTTP_Client) return HTTP_Version;
------------------------------------------------------------------------
-- Sending response header fields:
--
--    Client - The client connection object
--    ...    - Header parameters
--
-- The following  procedures send response header field.  The procedures
-- do not block.  If the output buffer cannot accept the header then the
-- exception Data_Error is propagated.  Note that the output buffer size
-- is controlled by the discriminant Output_Size.
--
-- Exceptions :
--
--    Data_Error - No room for output
--
-- Reply_{HTML|Text} -- Send short reply, usually error response
--
--    Client  - The client connection object
--    Core    - The status code, e.g. 404
--    Reason  - The reason phrase, e.g. Not found
--    Message - The error response (plain text or HTML)
--    Get     - Send body if true (no body is sent for POST requests)
--
   procedure Reply_HTML
             (  Client  : in out HTTP_Client;
                Code    : Positive;
                Reason  : String;
                Message : String;
                Get     : Boolean := True
             );
   procedure Reply_Text
             (  Client  : in out HTTP_Client;
                Code    : Positive;
                Reason  : String;
                Message : String;
                Get     : Boolean := True
             );
--
-- Send_Accept_Ranges -- Accept ranges infurmation
--
--    Client        - The client connection object
--    Accept_Ranges - True if byte ranges are accepted, otherwise none
--
   procedure Send_Accept_Ranges
             (  Client        : in out HTTP_Client;
                Accept_Ranges : Boolean
             );
--
-- Send_Age -- Age field
--
--    Client - The client connection object
--    Age    - The age to send
--
   procedure Send_Age (Client : in out HTTP_Client; Age : Duration);
--
-- Send_Allow -- Allow field
--
--    Client  - The client connection object
--    Allowed - The list of allowed options
--
   procedure Send_Allow
             (  Client  : in out HTTP_Client;
                Allowed : HTTP_Allowed
             );
--
-- Send_Connection -- Connection field
--
--    Client     - The client connection object
--    Persistent - True if "keep-alive"
--
-- Note that Persistent  True is ignored if the client already requested
-- connection closing. In that case "close" is responded anyway.
--
   procedure Send_Connection
             (  Client     : in out HTTP_Client;
                Persistent : Boolean := True
             );
--
-- Send_Content_Range -- Content-range field
--
--    Client        - The client connection object
--    Content_Range - The range of the content
--
   procedure Send_Content_Range
             (  Client        : in out HTTP_Client;
                Content_Range : String := "none"
             );
--
-- Send_Content_Range -- Content-range field
--
--    Client - The client connection object
--    From   - The range's lower bound
--    To     - The range's upper bound
--    Length - The total content length
--
   procedure Send_Content_Range
             (  Client : in out HTTP_Client;
                From   : Stream_Element_Count;
                To     : Stream_Element_Count;
                Length : Stream_Element_Count
             );
--
-- Send_Content_Type -- Content-type field
--
--    Client  - The client connection object
--    Media   - The media type
--    Charset - The character set
--
   procedure Send_Content_Type
             (  Client  : in out HTTP_Client;
                Media   : String := "text/plain";
                Charset : String := "UTF-8"
             );
--
-- Send_Date -- Date field
--
--    Client - The client connection object
--    Date   - The time to send
--
   procedure Send_Date
             (  Client : in out HTTP_Client;
                Date   : Time := Clock
             );
--
-- Send_If_Modified_Since -- Date field
--
--    Client - The client connection object
--    Date   - The time to send
--
   procedure Send_If_Modified_Since
             (  Client : in out HTTP_Client;
                Date   : Time
             );
--
-- Send_If_Unmodified_Since -- Date field
--
--    Client - The client connection object
--    Date   - The time to send
--
   procedure Send_If_Unmodified_Since
             (  Client : in out HTTP_Client;
                Date   : Time
             );
--
-- Send_Last_Modified -- Date field
--
--    Client - The client connection object
--    Date   - The time to send
--
   procedure Send_Last_Modified
             (  Client : in out HTTP_Client;
                Date   : Time
             );
--
-- Send_Length -- Content-length field
--
--    Client - The client connection object
--    Length - The length of content
--
   procedure Send_Length
             (  Client : in out HTTP_Client;
                Length : Natural
             );
   procedure Send_Length
             (  Client : in out HTTP_Client;
                Length : Stream_Element_Count
             );
--
-- Send_Server -- Server field
--
--    Client - The client connection object
--
-- This procedure makes a dispatching call to Client's Get_Name
--
   procedure Send_Server (Client : in out HTTP_Client);
--
-- Send_Status_Line -- Status line of a response
--
--    Client  - The HTTP client connection object
--    Code    - The status code
--    Text    - The reason phrase
--    Version - The HTTP version to use
--
   procedure Send_Status_Line
             (  Client  : in out HTTP_Client;
                Code    : Positive;
                Text    : String;
                Version : String := "HTTP/1.1"
             );
--
-- Send -- To the client
--
--    Client  - The HTTP client connection object
--    Message - To send
--
   procedure Send
             (  Client  : in out HTTP_Client;
                Message : String
             );
   procedure Send
             (  Client  : in out HTTP_Client;
                Message : Stream_Element_Array
             );
------------------------------------------------------------------------
-- Sending response body:
--
-- Accumulate_Body -- Add a piece of data to the accumuated body
--
--    Client  - The HTTP client connection object
--    Content - To add to the body
--
-- The accumulated body can be seny using Send_Body.  The body is erased
-- when when the server start to accept a new request.
--
   procedure Accumulate_Body
             (  Client  : in out HTTP_Client;
                Content : Stream_Element_Array
             );
   procedure Accumulate_Body
             (  Client  : in out HTTP_Client;
                Content : access Stream_Element_Array
             );
   procedure Accumulate_Body
             (  Client  : in out HTTP_Client;
                Content : String
             );
   procedure Accumulate_Body
             (  Client  : in out HTTP_Client;
                Content : access String
             );
--
-- Accumulated_Body_Length -- Length of accumuated body
--
--    Client - The HTTP client connection object
--
-- Dynamically composed bodies can be accumulated in the client's memory
-- before  sending it  back in  the response.  For this one  can use the
-- procedures Accumulate_Body.
--
-- Returns :
--
--    Length of the body accumulated using Accumulate_Body
--
   function Accumulated_Body_Length (Client : HTTP_Client)
      return Stream_Element_Count;
--
-- Body_Sent -- Called on chunked transfer completion
--
--    Client - The HTTP client connection object
--    Stream - Used during transfer
--    Get    - In response to GET or HEAD
--
-- The default implmentation does nothing.
--
   procedure Body_Sent
             (  Client : in out HTTP_Client;
                Stream : in out Root_Stream_Type'Class;
                Get    : Boolean
             );
--
-- Send_Body -- Start chunked transfer using a stream
--
--    Client   - The HTTP client connection object
--    Stream   - That contains data to transfer
--  [ Length ] - The response length or 0 if unknown
--    Get      - In response to GET or HEAD
--
-- When no Length is specified this procedure sends the header:
--
--    Transfer-Encoding: chunked
--
-- followed by the data read from Stream.  The operation  ends  when the
-- stream  ends.  The chunk  size is  at least  the size  of  the output
-- buffer. When Length is specified this procedure sends the header:
--
--    Content-Length: <length>
--
-- followed by the data read from Stream.  Only Length elements are read
-- from the stream.  This variant  is used when the length of the stream
-- is known.  It has the advantage than the client will know  the length
-- and thus be able to indicate the progress.
--
-- When  Get  is  false,  no  body  is sent.  Upon  transfer  completion
-- Body_Sent is called.  This is  done  independently on whether Get was
-- false or true.
--
   procedure Send_Body
             (  Client : in out HTTP_Client;
                Stream : access Root_Stream_Type'Class;
                Get    : Boolean := True
             );
   procedure Send_Body
             (  Client : in out HTTP_Client;
                Stream : access Root_Stream_Type'Class;
                Length : Stream_Element_Count;
                Get    : Boolean := True
             );
--
-- Send_Body -- Start chunked transfer using a stream
--
--    Client   - The HTTP client connection object
--    Content  - That contains data to transfer
--  [ Length ] - The response length or 0 if unknown
--    Get      - In response to GET or HEAD
--
-- This operation sends the header:
--
--    Transfer-Encoding: chunked
--
-- followed by the data read from Stream.  The operation  ends  when the
-- stream  ends.  The chunk  size is  at least  the size  of  the output
-- buffer. When Length is specified this procedure sends the header:
--
--    Content-Length: <length>
--
-- followed by the data read from Stream. Only Length elements are sent.
-- This variant is used when the length of the contents is known. It has
-- the advantage than the client will know  the length  and thus be able
-- to indicate the progress.
--
   procedure Send_Body
             (  Client  : in out HTTP_Client;
                Content : access Content_Source'Class;
                Get     : Boolean := True
             );
   procedure Send_Body
             (  Client  : in out HTTP_Client;
                Content : access Content_Source'Class;
                Length  : Stream_Element_Count;
                Get     : Boolean := True
             );
--
-- Send_Body -- Complete response sending string content
--
--    Client  - The HTTP client connection object
--    Content - That contains data to transfer
--    Get     - In response to GET or HEAD
--
-- This operation sends the header:
--
--    Content-Length: <length>
--
-- followed by the content. The content must fit into the output buffer.
-- Larger  contents  should  be first  accumulated  in the memory  using
-- Accumulate_Body. When Get is false, no message body is sent.
--
   procedure Send_Body
             (  Client  : in out HTTP_Client;
                Content : String;
                Get     : Boolean := True
             );
--
-- Send_Body -- Complete response sending accumulated content
--
--    Client  - The HTTP client connection object
--    Get     - In response to GET or HEAD
--
-- This operation sends the header:
--
--    Content-Length: <length>
--
-- followed by the content. When Get is false, no message body is sent.
--
   procedure Send_Body
             (  Client : in out HTTP_Client;
                Get    : Boolean := True
             );
------------------------------------------------------------------------
-- Receiving bodies:
--
-- Do_Body -- Prepare to start receipt a request's body
--
--    Client - The HTTP client connection object
--
-- This  procedure is  called before  the server starts receiving of the
-- body of  the pending  request.  The implementation  may create a file
-- to write here  using information from the header fields.  The default
-- implementation  does nothing  which has  the effect  that the body is
-- received but ignored. For multipart bodies Do_Body is called for each
-- part.
--
   procedure Do_Body (Client : in out HTTP_Client);
--
-- Body_Error -- Called on body receipt error
--
--    Client           - The HTTP client connection object
--    Stream / Content - Used during transfer
--    Error            - The error occurence
--
-- This procedure is called  on  an error  occurred  during  storing the
-- request's body.  The rest of  the body  is accepted but ignored.  The
-- default implementation raises Data_Error exception.
--
   procedure Body_Error
             (  Client : in out HTTP_Client;
                Stream : in out Root_Stream_Type'Class;
                Error  : Exception_Occurrence
             );
   procedure Body_Error
             (  Client  : in out HTTP_Client;
                Content : in out Content_Destination'Class;
                Error   : Exception_Occurrence
             );
   procedure Body_Error
             (  Client  : in out HTTP_Client;
                Content : in out CGI_Keys.Table'Class;
                Error   : Exception_Occurrence
             );
--
-- Body_Received -- Called on body receipt completion
--
--    Client           - The HTTP client connection object
--    Stream / Content - Used during transfer
--
-- This procedure is called  on the request's  body  receipt completion,
-- when that  was initiated using  a call  to Receive_Body.  The default
-- implmentation does nothing.
--
   procedure Body_Received
             (  Client : in out HTTP_Client;
                Stream : in out Root_Stream_Type'Class
             );
   procedure Body_Received
             (  Client  : in out HTTP_Client;
                Content : in out Content_Destination'Class
             );
   procedure Body_Received
             (  Client  : in out HTTP_Client;
                Content : in out CGI_Keys.Table'Class
             );
--
-- Get_CGI_Key -- CGI key of the body received
--
--    Client - The client connection object
--    Index  - The key's position starting with 1
--
-- The  key/value  pairs  are set  using  a call to a Receive_Body which
-- parses the application/x-www-form-urlencoded bodies.
--
-- Returns :
--
--    The key or empty string
--
-- Exceptions :
--
--    Constraint_Error - Index is not in 1..Get_CGI_Size
--
   function Get_CGI_Key (Client : HTTP_Client; Index : Positive)
      return String;
--
-- Get_CGI_Size -- The number of CGI values
--
--    Client - The client connection object
--
-- Returns :
--
--    The number of CGI key/value pairs
--
   function Get_CGI_Size (Client : HTTP_Client) return Natural;
--
-- Get_CGI_Value -- CGI value of the body received
--
--    Client      - The client connection object
--    Key / Index - The key name or position starting with 1
--
-- The  key/value  pairs are  set using  a call to a Receive_Body  which
-- parses the application/x-www-form-urlencoded bodies.
--
-- Returns :
--
--    The value corresponding to Key or empty string
--
-- Exceptions :
--
--    Constraint_Error - Index is not in 1..Get_CGI_Size
--
   function Get_CGI_Value (Client : HTTP_Client; Key : String)
      return String;
   function Get_CGI_Value (Client : HTTP_Client; Index : Positive)
      return String;
--
-- Get_Multipart_Header -- The header of the current multipart body
--
--    Client - The client connection object
--    Header - Multipart header
--
-- Returns :
--
--    The header value of the current request or empty string
--
   function Get_Multipart_Header
            (  Client : HTTP_Client;
               Header : Multipart_Header
            )  return String;
--
-- Receive_Body -- Start receipt into a CGI key/value pairs
--
--    Client      - The HTTP client connection object
--  [ Content   ] - The mapping of the expected keys (table or text)
--  [ Delimiter ] - Keys delimiter in Content
--
-- This procedure is typically  called  from  Do_Body to start receiving
-- body  of  the  application/x-www-form-urlencoded  content  type.  The
-- parameter  Content specifies  the list of  expected keys.  Not listed
-- keys are ignored.  After  successful body receipt Content has a value
-- set  if the key was present  in the body or  empty  string otherwise.
-- The object  Content shall  exist at least until  CGI_Body_Received is
-- called. When Content is a string it is a list of keys separated using
-- Delimiter character. When Content is omitted any key is accepted  for
-- which the primitive operation Validate_Key returns True, which is the
-- default. Note that this behavior is unsafe.  When the variant of this
-- procedure without the Content parameter is used,  Validate_Key should
-- be overridden.
--
   procedure Receive_Body
             (  Client  : in out HTTP_Client;
                Content : access CGI_Keys.Table'Class
             );
   procedure Receive_Body
             (  Client    : in out HTTP_Client;
                Content   : String;
                Delimiter : Character := ','
             );
   procedure Receive_Body (Client : in out HTTP_Client);
--
-- Receive_Body -- Start receipt into a stream
--
--    Client - The HTTP client connection object
--    Stream - To write
--
-- This procedure  is typcally  called  from  Do_Body to start receiving
-- the request's body into the stream.
--
   procedure Receive_Body
             (  Client : in out HTTP_Client;
                Stream : access Root_Stream_Type'Class
             );
--
-- Receive_Body -- Start receipt into a user-content object
--
--    Client  - The HTTP client connection object
--    Content - The content object to accept the body
--
-- This procedure is typically  called  from  Do_Body to start receiving
-- the request's body by Content object.
--
   procedure Receive_Body
             (  Client  : in out HTTP_Client;
                Content : access Content_Destination'Class
             );
--
-- Receive_Body_Tracing -- Enable tracing
--
--    Client - The HTTP client connection object
--    Enable - Trace receiving bodies
--
   procedure Receive_Body_Tracing
             (  Client : in out HTTP_Client;
                Enable : Boolean
             );
--
-- Receive_Header_Tracing -- Enable tracing
--
--    Client - The HTTP client connection object
--    Enable - Trace receiving bodies
--
   procedure Receive_Header_Tracing
             (  Client : in out HTTP_Client;
                Enable : Boolean
             );
--
-- Validate_Key -- Validate a CGI key
--
--    Client - The HTTP client connection object
--    Key    - The CGI key to validate
--
-- This function is called for each CGI key when Receive_Body is used to
-- receive  CGI key/value pairs without the content parameter.  The keys
-- for which  the  function  returns  false  are  ignored.  The  default
-- implementation accepts any key.
--
-- Returns :
--
--    True if the CGI key is accepted
--
   function Validate_Key
            (  Client : HTTP_Client;
               Key    : String
            )  return Boolean;
------------------------------------------------------------------------
-- WebSocket operations. WebSockets are defined in RFC 6455
--
-- WebSocket_Close -- Initiate socket close
--
--    Client  - The HTTP client connection object
--    Status  - Status code
--    Message - To the client (UTF-8 encoded)
--
-- Usually the socket is closed on client's request. If the server wants
-- to close it, this procedure is called.  Note that a client's response
-- will be awaited.
--
-- Exceptions :
--
--    Constraint_Error - Message is longer than 123 characters
--    End_Error        - The socket is closed
--
   procedure WebSocket_Close
             (  Client  : in out HTTP_Client;
                Status  : WebSocket_Status := WebSocket_Normal_Closure;
                Message : String := ""
             );
--
-- WebSocket_Closed -- Socket closed callback
--
--    Client  - The HTTP client connection object
--    Status  - The status code
--    Message - The close socket message
--
-- This  procedure  is  called  when  the  WebSocket is    closed at the
-- client's request. That is when the server receives a close frame from
-- the client. The Status code is the first two octets of the  message's
-- payload  data.  Message the UTF-8 encoded text from the payload data.
-- The default implementation does nothing.
--
   procedure WebSocket_Closed
             (  Client  : in out HTTP_Client;
                Status  : WebSocket_Status;
                Message : String
             );
--
-- WebSocket_Error -- Socket error callback
--
--    Client - The HTTP client connection object
--    Error  - The exception occurrence
--
-- This   procedure   is   called   on  WebSocket  errors.  The  default
-- implementation does nothing.
--
   procedure WebSocket_Error
             (  Client : in out HTTP_Client;
                Error  : Exception_Occurrence
             );
--
-- WebSocket_Finalize -- Socket finalization
--
--    Client - The HTTP client connection object
--
-- This  is  the  last  procedure  called  on  WebSocket.  The   default
-- implementation does nothing.
--
   procedure WebSocket_Finalize (Client : in out HTTP_Client);
--
-- WebSocket_Initialize -- Socket initialization callback
--
--    Client - The HTTP client connection object
--
-- This  is the first operation called on  the WebSocket called when the
-- socket  becomes  fully  operational.  This  is  an appropriate  place
-- to start a task if duplex I/O is intended. The default implementation
-- does nothing.
--
   procedure WebSocket_Initialize (Client : in out HTTP_Client);
--
-- WebSocket_Open -- Socket connection request callback
--
--    Client - The HTTP client connection object
--
-- The implementation verifies the header fields and the URI in order to
-- decide to accept or reject connection.
--
-- Returns :
--
--    WebSocket_Accept:
--
--    (o)  If   Accepted   discriminant  is  True,  the  server  accepts
--         connection and:
--
--         - Size  determines  the maximal length in octets  of an input
--           WebSocket message (maybe split in several frames);
--
--         - Duplex  is set to  True  when the server will  send to  the
--           WebSocket from an independent task.  In particular  it will
--           enable  receiving  and sending  messages in parallel  (full
--           duplex).  When  Duplex is False  the communication  is half
--           duplex.  The server responds to each incoming message  by a
--           series of messages. While sending these messages the server
--           stops receiving new messages;
--
--         - Chunked  controls  chunked  message  receipt.  When   False
--           messages larger than Size cause error and the connection is
--           dropped.   When   True   incomplete   message  parts  cause
--           WebSocket_Received_Part  called.  The  last  part  of   the
--           message   is   always   delivered   using   a    call    to
--           WebSocket_Received.
--
--         - Protocols  is the list of protocols reported  to the client
--           as supported.  If  this string  is  empty the  contents  of
--           Sec-WebSocket-Protocol is taken.
--
--    (o)  If Accepted is false, the connection is rejected and:
--
--         - Code is the code to report, e.g. 400;
--
--         - Reason is its text, e.g. Bad Request.
--
   type WebSocket_Accept
        (  Accepted : Boolean;
           Length   : Natural
        )  is
   record
      case Accepted is
         when True =>
            Size      : WebSocket_Message_Size;
            Duplex    : Boolean;
            Chunked   : Boolean;
            Protocols : String (1..Length);
         when False =>
            Code      : Positive;
            Reason    : String (1..Length);
      end case;
   end record;
   function WebSocket_Open
            (  Client : access HTTP_Client
            )  return WebSocket_Accept;
--
-- WebSocket_Received -- Socket message callback
--
--    Client  - The HTTP client connection object
--    Message - From the client
--
-- These procedures are called on receipt of a  WebSocket  message.  The
-- variant  with  Stream_Element_Array parameter is called when a binary
-- message has been received. The variant with String is called  when  a
-- text  message  has. The text messages are UTF-8 encoded (according to
-- RFC  6455). The validity of the encoding is not checked, though. Note
-- that  the  maximal  message  length  is limited by the value set upon
-- socket connection. The default implementations do nothing.
--
   procedure WebSocket_Received
             (  Client  : in out HTTP_Client;
                Message : Stream_Element_Array
             );
   procedure WebSocket_Received
             (  Client  : in out HTTP_Client;
                Message : String
             );
--
-- WebSocket_Received_Part -- Socket message callback
--
--    Client  - The HTTP client connection object
--    Message - From the client
--
-- These  procedures  are  called  on  receipt  of  a  part of WebSocket
-- message. It is called only when  Chunked  field  of  WebSocket_Accept
-- returned    by    WebSocket_Open    is   True.   The   variant   with
-- Stream_Element_Array parameter is called when a  binary  message  has
-- been  received. The variant with String is called when a text message
-- has. The text messages are UTF-8 encoded (according to RFC 6455). The
-- validity of the encoding  is  not  checked,  though.  Note  that  the
-- maximal message length is  limited  by  the  value  set  upon  socket
-- connection. The default implementations do nothing.
--
   procedure WebSocket_Received_Part
             (  Client  : in out HTTP_Client;
                Message : Stream_Element_Array
             );
   procedure WebSocket_Received_Part
             (  Client  : in out HTTP_Client;
                Message : String
             );
--
-- WebSocket_Send -- Send a message over the socket
--
--    Client  - The HTTP client connection object
--    Message - To the client
--
-- These procedures are used to send a binary or a text message over the
-- WebSocket.
--
-- Exceptions :
--
--    End_Error - The socket is closed
--
   procedure WebSocket_Send
             (  Client  : in out HTTP_Client;
                Message : Stream_Element_Array
             );
   procedure WebSocket_Send
             (  Client  : in out HTTP_Client;
                Message : String
             );
------------------------------------------------------------------------
--
-- From_Escaped -- Convert RFC-escaped name to plain string
--
--    Name           - To convert
--    Translate_Plus - When True + it replaces with SP
--
-- The RFC 2396 uses %HH escape sequences in the URI.
--
-- Returns :
--
--    Equivalent plain string
--
-- Exceptions :
--
--    Data_Error - Syntax error
--
   function From_Escaped
            (  Name           : String;
               Translate_Plus : Boolean := False
            )  return String;
--
-- To_HTML -- Convert to HTML format
--
--    Text - UTF-8 encoded string to encode
--
-- Returns :
--
--    String representation in HTML format
--
-- Exceptions :
--
--    Data_Error - Invalid UTF-8 sequence
--
   function To_HTML (Text : String) return String;
--
-- To_Escaped -- Convert plain string to RFC-escaped
--
--    Name - To convert
--
-- Returns :
--
--    Equivalent escaped string
--
   function To_Escaped (Name : String) return String;
--
-- To_HTTP -- Convert time to HTTP format
--
--    Date - The time to convert
--
-- Returns :
--
--    String representation of the parameter Date in the format:
--    Sun, 17 Feb 2013 21:02:43 +0100
--
   function To_HTTP (Date : Time) return String
      renames Strings_Edit.Time_Conversions.To_String;
--
-- To_Time -- Time conversion
--
--    Date - To convert
--
-- Supported formats:
--
--    Fri, 31 Dec 1999 23:59:59 GMT
--    Friday, 31-Dec-99 23:59:59 GMT
--    Fri Dec 31 23:59:59 1999
--
-- Exceptions :
--
--    Time_Error - On error
--
   function To_Time (Date : String) return Time
      renames Strings_Edit.Time_Conversions.To_Time;
------------------------------------------------------------------------
   CRLF  : constant String := (Character'Val (13), Character'Val (10));
   Lower : constant Character_Mapping :=
                    To_Mapping
                    (  "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                       "abcdefghijklmnopqrstuvwxyz"
                    );

   type Connection_Flags is mod 2**3;
   Connection_Close      : Connection_Flags := 1; -- "close"
   Connection_Persistent : Connection_Flags := 2; -- "keep-alive"
   Connection_Upgrade    : Connection_Flags := 4; -- "upgrade"
--
-- To_Flags -- Conversion of connection header value into flags
--
--    Value - The header value
--
-- Returns :
--
--    Connection flags
--
   function To_Flags (Value : String) return Connection_Flags;
private
   use GNAT.Sockets.Connection_State_Machine.Expected_Sequence;
   use GNAT.Sockets.Connection_State_Machine.Terminated_Strings;

   type Stream_Ptr is access all Root_Stream_Type'Class;

   type Action is access procedure (Client : in out HTTP_Client'Class);
   procedure Skip (Source : String; Pointer : in out Integer);
--
-- Request_Line_Type -- Types of request lines/processing states
--
   type Request_Line_Type is
        (  Request_Line,          -- Request status line
           Header_Line,           -- Request header field line
           Body_Data,             -- Data of a body
           Chunk_Line,            -- Header of a chunk
           Multipart_Preamble,    -- Multipart preamble
           Multipart_Header_Line, -- Header of multipart body
           Multipart_Body_Data,   -- Data of a multipart body
           Multipart_Body_Tail,   -- The multipart body data tail
           Multipart_Epilogue,    -- Multipart epilogue
           WebSocket_Header,      -- Incoming frame beginning
           WebSocket_Length,      -- Length octet
           WebSocket_Length_Ex,   -- Extended length octets
           WebSocket_Mask,        -- Mask octets
           WebSocket_Payload_Data -- Payload data octets
        );
   type Stream_Element_Array_Ptr is access all Stream_Element_Array;
   type Content_Source_Ptr is access all Content_Source'Class;
   type Content_Destination_Ptr is access all Content_Destination'Class;
   type Data_Pool is new Stack_Storage.Pool with null record;
   procedure Write
             (  Stream : access Root_Stream_Type'Class;
                Item   : Data_Pool
             );
   for Data_Pool'Write use Write;

   subtype Specific_Header is Request_Header
      range Date_Header..Last_Modified_Header;

   type Status_Line_Ptr is access constant Status_Line;
   type Text_Ptr is access constant String;
   type Request_Header_Array is array (Text_Header) of Text_Ptr;
   type Multipart_Header_Array is array (Multipart_Header) of Text_Ptr;
   type Specific_Header_Array is array (Specific_Header) of Boolean;

   type Content_Item;
   type Content_Item_Ptr is access all Content_Item;
   type Content_Item_Type is
        (  Literal_Value,
           String_Pointer,
           Stream_Elements_Pointer
        );
   type Content_Item
        (  Kind   : Content_Item_Type;
           Length : Stream_Element_Count
        )  is
   record
      Next  : Content_Item_Ptr;
      First : Stream_Element_Offset := 1;
      case Kind is
         when Literal_Value =>
            Data : Stream_Element_Array (1..Length);
         when String_Pointer =>
            Text_Ptr : String_Ptr;
         when Stream_Elements_Pointer =>
            Data_Ptr : Stream_Element_Array_Ptr;
      end case;
   end record;
   type Content_Stream is new Root_Stream_Type with record
      Length : Stream_Element_Count := 0;
      First  : Content_Item_Ptr;
      Last   : Content_Item_Ptr;
   end record;
   function Is_Empty (Stream : Content_Stream) return Boolean;
   procedure Read
             (  Stream : in out Content_Stream;
                Item   : out Stream_Element_Array;
                Last   : out Stream_Element_Offset
             );
   procedure Write
             (  Stream : in out Content_Stream;
                Item   : Stream_Element_Array
             );
   procedure Write
             (  Stream : access Root_Stream_Type'Class;
                Item   : Content_Stream
             );
   for Content_Stream'Write use Write;

   procedure Write
             (  Stream : access Root_Stream_Type'Class;
                Item   : Content_Destination
             );
   for Content_Destination'Write use Write;

   type CGI_Keys_Ptr is access all CGI_Keys.Table'Class;
   type CGI_State is (CGI_Key, CGI_Value);
   type CGI_Content (Client : access HTTP_Client'Class) is
      new Content_Destination with
   record
      State  : CGI_State;
      Offset : Natural := 0;
      Keys   : CGI_Keys_Ptr;
      Map    : aliased CGI_Keys.Table; -- Keys from a string
   end record;
   procedure Commit (Destination : in out CGI_Content);
   procedure Put
             (  Destination : in out CGI_Content;
                Data        : String
             );

   subtype WebSocket_Frame_Type is Stream_Element range 0..15;
   WebSocket_Continunation_Type : constant WebSocket_Frame_Type := 0;
   WebSocket_Text_Type          : constant WebSocket_Frame_Type := 1;
   WebSocket_Binary_Type        : constant WebSocket_Frame_Type := 2;
   WebSocket_Close_Type         : constant WebSocket_Frame_Type := 8;
   WebSocket_Ping_Type          : constant WebSocket_Frame_Type := 9;
   WebSocket_Pong_Type          : constant WebSocket_Frame_Type := 10;

   type WebSocket_Mask_Index is mod 4;
   type WebSocket_Mask_Array is
      array (WebSocket_Mask_Index) of Stream_Element;
   type WebSocket_Length_Count is range 1..8;

   subtype WebSocket_Control_Frame_Data is
      Stream_Element_Array (Stream_Element_Offset range 0..125);

   type WebSocket_Message (Size : WebSocket_Message_Size) is record
      Type_Of : WebSocket_Frame_Type;  -- The frame type
      Length  : Stream_Element_Count;  -- The message length
      Pointer : Stream_Element_Offset; -- To element to write
      Data    : Stream_Element_Array (1..Size);
   end record;
   type WebSocket_Message_Ptr is access all WebSocket_Message;

   type Duplex_Status is
        (  Disabled,
           Closing,
           Idle,
           Task_Sending,
           Server_Sending
        );
--
-- Send_Mutex -- The  object  used  for  interlocking  send  buffer  and
--               signaling its state when going multi-tasking.  The only
-- case when it happens is in a WebSocket full-duplex exchange.
--
   protected type Send_Mutex (Client : access HTTP_Client'Class) is
   --
   -- Failed -- Send failure notification
   --
      procedure Failed (Error : Exception_Occurrence);
   --
   -- Get_Status -- Get current status
   --
   -- Returns :
   --
   --    The current status
   --
      function Get_Status return Duplex_Status;
   --
   -- Grab -- Lock sending for the server
   --
   --    Seized - True if the lock was taken
   --
      procedure Grab (Seized : out Boolean);
   --
   -- Release -- Sending previously locked by Seize or Grab
   --
      procedure Release;
   --
   -- Set -- Change object state
   --
   --    New_State - The new state
   --
      procedure Set (New_State : Duplex_Status);
   --
   -- Seize -- Lock sending from an external task
   --
      entry Seize;

   private
      State : Duplex_Status := Disabled;     -- Socket is closed
   end Send_Mutex;
   procedure Write
             (  Stream : access Root_Stream_Type'Class;
                Item   : Send_Mutex
             );
   for Send_Mutex'Write use Write;

   type WebSocket_State is (Open_Socket, Closing_Socket, Closed_Socket);
   type WebSocket_Data is record
      State        : WebSocket_State := Closed_Socket;
      Final        : Boolean := False;        -- Current frame is last
      Pending      : Boolean := False;        -- A message is pending
      Duplex       : Boolean := False;        -- Use mutex
      Chunked      : Boolean := False;        -- Chunked messages
      Frame_Type   : WebSocket_Frame_Type;    -- Current frame type
      Frame_Length : Stream_Element_Count;    -- Current frame length
      Length_Count : WebSocket_Length_Count;  -- Extended length
      Max_Length   : WebSocket_Message_Size;  -- Maximal message length
      Frame        : WebSocket_Message_Ptr;   -- Current frame buffer
      Data         : WebSocket_Message_Ptr;   -- Incoming messages frame
      Context      : Task_ID := Null_Task_Id; -- Calling context
      Mask_Index   : WebSocket_Mask_Index;    -- Mask index
      Mask         : WebSocket_Mask_Array;    -- Mask
      Control      : aliased WebSocket_Message (125);
      pragma Atomic (State);
      pragma Atomic (Duplex);
   end record;
   procedure Write
             (  Stream : access Root_Stream_Type'Class;
                Item   : WebSocket_Data
             );
   for WebSocket_Data'Write use Write;

   type HTTP_Client
        (  Listener       : access Connections_Server'Class;
           Request_Length : Positive;
           Input_Size     : Buffer_Length;
           Output_Size    : Buffer_Length
        )  is new State_Machine (Input_Size, Output_Size) with
   record
      Expecting    : Request_Line_Type    := Request_Line;
      Data_Length  : Stream_Element_Count := 0;
      Chunk_Type   : Request_Line_Type;
      Method       : HTTP_Method;
      Stream       : Stream_Ptr;
      Source       : Content_Source_Ptr;
      Destination  : Content_Destination_Ptr;
      Boundary     : String_Ptr;       -- Multipart boundary
      Position     : Integer;          -- Boundary character to match
      Chain        : Action := null;
      Connection   : Connection_Flags := 0;
      Chunked      : Boolean := False; -- Using chunked transfer
      Trace_Body   : Boolean := False;
      Trace_Header : Boolean := False;
      Validate_CGI : Boolean := False;
         -- Time header fields
      Date                : Time := Clock;
      Last_Modified       : Time := Clock;
      If_Modified_Since   : Time := Clock;
      If_Unmodified_Since : Time := Clock;
         -- Options
      Allowed   : HTTP_Allowed := (  HTTP_GET     => True,
                                     HTTP_HEAD    => True,
                                     HTTP_OPTIONS => True,
                                     others       => False
                                  );
      Version   : HTTP_Version;
      Port      : Port_Type;
      Stub      : Content_Item_Ptr; -- First allocated content item
         -- WebSocket data
      WebSocket : WebSocket_Data;
         --
      Ranges    : Content_Ranges.Set;    -- Request ranges
      Suffix    : Stream_Element_Offset := Stream_Element_Offset'First;

         -- Parsed line, CR LF terminated
      Line         : Dynamic_String_Data_Item;
      LF           : Expected_Item (1);

      Status       : Status_Line_Ptr;
      Part_Mark    : Text_Ptr;               -- Mark of part headers
      Headers      : Request_Header_Array;   -- Request headers
      Multipart    : Multipart_Header_Array; -- Part headers
      Specific     : Specific_Header_Array;  -- Specific headers
      Body_Content : aliased Content_Stream;
      CGI          : aliased CGI_Content (HTTP_Client'Unchecked_Access);
      Pool         : Data_Pool (0, 4);
      Mutex        : aliased Send_Mutex (HTTP_Client'Unchecked_Access);
      pragma Atomic (Expecting);
   end record;

   function Check_WebSocket
            (  Client : access HTTP_Client
            )  return Boolean;

   procedure Process_Body_Tail    (Client : in out HTTP_Client'Class);
   procedure Process_Chunk_Line   (Client : in out HTTP_Client'Class);
   procedure Process_Epilogue     (Client : in out HTTP_Client'Class);
   procedure Process_Header_Line  (Client : in out HTTP_Client'Class);
   procedure Process_Part_Header  (Client : in out HTTP_Client'Class);
   procedure Process_Preamble     (Client : in out HTTP_Client'Class);
   procedure Process_Request      (Client : in out HTTP_Client'Class);
   procedure Process_Request_Line (Client : in out HTTP_Client'Class);
--
-- Queue_Content -- Queue data to send later
--
--    Client - The HTTP client connection object
--    Data   - To send
--
-- The procedure adds message the content's queue
--
   procedure Queue_Content
             (  Client : in out HTTP_Client;
                Data   : Stream_Element_Array
             );
   procedure Queue_Content
             (  Client : in out HTTP_Client;
                Data   : String
             );
   procedure Set_Failed
             (  Client : in out HTTP_Client;
                Error  : Exception_Occurrence
             );
   procedure Write
             (  Client  : in out HTTP_Client;
                Factory : in out Connections_Factory'Class;
                Blocked : out Boolean
             );

   type Completion is access
      procedure (Client : in out HTTP_Client'Class);
   procedure Receive_Header_Line
             (  Client  : in out HTTP_Client'Class;
                Data    : Stream_Element_Array;
                Pointer : in out Stream_Element_Offset;
                Handler : Completion
             );
   procedure Receive_Multipart_Line
             (  Client  : in out HTTP_Client'Class;
                Data    : Stream_Element_Array;
                Pointer : in out Stream_Element_Offset;
                Handler : Completion
             );

   procedure Header_Received
             (  Client : in out HTTP_Client;
                Header : Request_Header;
                Value  : String
             );
   procedure Multipart_Header_Received
             (  Client : in out HTTP_Client;
                Header : Request_Header;
                Value  : String
             );

   procedure Cleanup_Body_Part (Client : in out HTTP_Client'Class);
   procedure Continue (Client : in out HTTP_Client; Chain : Action);
   procedure Content_Chunk (Client : in out HTTP_Client'Class);
   procedure Message_Chunk (Client : in out HTTP_Client'Class);
   procedure Sent (Client : in out HTTP_Client);
   procedure Stream_Chunk  (Client : in out HTTP_Client'Class);
--
-- Send_Content -- Send immediately or queue rest
--
--    Client - The HTTP client connection object
--    Data   - To send
--
-- The procedure adds message the content's queue
--
   procedure Send_Content
             (  Client : in out HTTP_Client;
                Data   : Stream_Element_Array
             );
   procedure Send_Content
             (  Client : in out HTTP_Client;
                Data   : String
             );
--
-- Status_Line_Received -- Stauts line receipt
--
--    Client   - The HTTP client connection object
--    Method   - The method being requested
--  [ Path     - The file path
--    Host     - The host specified in the URI
--    Port     - The port specified in the URI
--  [ Query ]] - The query part specified in the URI
--    Version  - The HTTP version requested
--
-- This procedure is called when the status list is successfully parsed.
-- The default implementation stores fields of the status line.
--
   procedure Status_Line_Received
             (  Client  : in out HTTP_Client;
                Method  : HTTP_Method;
                Version : HTTP_Version
             );
   procedure Status_Line_Received
             (  Client  : in out HTTP_Client;
                Method  : HTTP_Method;
                Path    : String;
                Query   : String;
                Version : HTTP_Version
             );
   procedure Status_Line_Received
             (  Client  : in out HTTP_Client;
                Scheme  : Scheme_Type;
                Method  : HTTP_Method;
                Host    : String;
                Port    : Port_Type;
                Path    : String;
                Query   : String;
                Version : HTTP_Version
             );
--
-- WebSocket_Blocking_Send -- Send a piece of message from an alien task
--
--    Client  - The HTTP client connection object
--    Data    - To send
--    First   - True if Data is the first chunk of the message
--    Last    - True if Data is the last chunk of the message
--
-- Exceptions :
--
--    End_Error - WebSocket is closed
--
   procedure WebSocket_Blocking_Send
             (  Client  : in out HTTP_Client'Class;
                Data    : Stream_Element_Array;
                First   : Boolean;
                Last    : Boolean
             );
--
-- WebSocket_Cleanup -- Socket finalization
--
--    Client - The HTTP client connection object
--
   procedure WebSocket_Cleanup (Client : in out HTTP_Client'Class);

   type Multipart_Body (Boundary : access String) is abstract
      new Content_Destination with
   record
      Header : Boolean := True;
   end record;

   package HTTP_Tables_Raw is new Tables (HTTP_Method);
   package HTTP_Tables is new HTTP_Tables_Raw.Names;
   use HTTP_Tables;

   package Request_Header_Tables_Raw is new Tables (Request_Header);
   package Request_Header_Tables is
      new Request_Header_Tables_Raw.Names;
   use Request_Header_Tables;

   package Connection_Tables_Raw is new Tables (Connection_Flags);
   package Connection_Tables is new Connection_Tables_Raw.Names;
   use Connection_Tables;

   package Scheme_Tables_Raw is new Tables (Scheme_Type);
   package Scheme_Tables is new Scheme_Tables_Raw.Names;
   use Scheme_Tables;

   Commands        : HTTP_Tables.Dictionary;
   Request_Headers : Request_Header_Tables.Dictionary;
   Connections     : Connection_Tables.Dictionary;
   Schemes         : Scheme_Tables.Dictionary;

end GNAT.Sockets.Connection_State_Machine.HTTP_Server;