aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_upgrade.c
blob: fe39f5cf5ff194cf1e1560878859e276c39e6488 (plain) (blame)
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
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
/*
     This file is part of libmicrohttpd
     Copyright (C) 2016-2020 Christian Grothoff
     Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)

     libmicrohttpd 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 3, or (at your
     option) any later version.

     libmicrohttpd 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 libmicrohttpd; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file test_upgrade.c
 * @brief  Testcase for libmicrohttpd upgrading a connection
 * @author Christian Grothoff
 * @author Karlson2k (Evgeny Grin)
 */

#include "mhd_options.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <errno.h>
#ifndef WINDOWS
#include <unistd.h>
#endif
#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#endif /* HAVE_STDBOOL_H */

#include "mhd_sockets.h"
#ifdef HAVE_NETINET_IP_H
#include <netinet/ip.h>
#endif /* HAVE_NETINET_IP_H */

#include "platform.h"
#include "microhttpd.h"

#include "test_helpers.h"

#ifdef HTTPS_SUPPORT
#include <gnutls/gnutls.h>
#include "../testcurl/https/tls_test_keys.h"

#if defined(HAVE_FORK) && defined(HAVE_WAITPID)
#include <sys/types.h>
#include <sys/wait.h>
#endif /* HAVE_FORK && HAVE_WAITPID */
#endif /* HTTPS_SUPPORT */

#if defined(MHD_POSIX_SOCKETS)
#  ifdef MHD_WINSOCK_SOCKETS
#    error Both MHD_POSIX_SOCKETS and MHD_WINSOCK_SOCKETS are defined
#  endif /* MHD_WINSOCK_SOCKETS */
#elif ! defined(MHD_WINSOCK_SOCKETS)
#  error Neither MHD_POSIX_SOCKETS nor MHD_WINSOCK_SOCKETS are defined
#endif /* MHD_WINSOCK_SOCKETS */


#ifndef MHD_STATICSTR_LEN_
/**
 * Determine length of static string / macro strings at compile time.
 */
#define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1)
#endif /* ! MHD_STATICSTR_LEN_ */


_MHD_NORETURN static void
_externalErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
{
  fflush (stdout);
  if ((NULL != errDesc) && (0 != errDesc[0]))
    fprintf (stderr, "%s", errDesc);
  else
    fprintf (stderr, "System or external library call failed");
  if ((NULL != funcName) && (0 != funcName[0]))
    fprintf (stderr, " in %s", funcName);
  if (0 < lineNum)
    fprintf (stderr, " at line %d", lineNum);

  fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
           strerror (errno));
#ifdef MHD_WINSOCK_SOCKETS
  fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ());
#endif /* MHD_WINSOCK_SOCKETS */
  fflush (stderr);
  exit (99);
}


_MHD_NORETURN static void
_mhdErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
{
  fflush (stdout);
  if ((NULL != errDesc) && (0 != errDesc[0]))
    fprintf (stderr, "%s", errDesc);
  else
    fprintf (stderr, "MHD unexpected error");
  if ((NULL != funcName) && (0 != funcName[0]))
    fprintf (stderr, " in %s", funcName);
  if (0 < lineNum)
    fprintf (stderr, " at line %d", lineNum);

  fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
           strerror (errno));

  fflush (stderr);
  exit (8);
}


static void
_testErrorLog_func (const char *errDesc, const char *funcName, int lineNum)
{
  fflush (stdout);
  if ((NULL != errDesc) && (0 != errDesc[0]))
    fprintf (stderr, "%s", errDesc);
  else
    fprintf (stderr, "System or external library call resulted in error");
  if ((NULL != funcName) && (0 != funcName[0]))
    fprintf (stderr, " in %s", funcName);
  if (0 < lineNum)
    fprintf (stderr, " at line %d", lineNum);

  fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
           strerror (errno));
#ifdef MHD_WINSOCK_SOCKETS
  fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ());
#endif /* MHD_WINSOCK_SOCKETS */
  fflush (stderr);
}


#ifdef MHD_HAVE_MHD_FUNC_
#define externalErrorExit(ignore) \
    _externalErrorExit_func(NULL, MHD_FUNC_, __LINE__)
#define externalErrorExitDesc(errDesc) \
    _externalErrorExit_func(errDesc, MHD_FUNC_, __LINE__)
#define mhdErrorExit(ignore) \
    _mhdErrorExit_func(NULL, MHD_FUNC_, __LINE__)
#define mhdErrorExitDesc(errDesc) \
    _mhdErrorExit_func(errDesc, MHD_FUNC_, __LINE__)
#define testErrorLog(ignore) \
    _testErrorLog_func(NULL, MHD_FUNC_, __LINE__)
#define testErrorLogDesc(errDesc) \
    _testErrorLog_func(errDesc, MHD_FUNC_, __LINE__)
#else  /* ! MHD_HAVE_MHD_FUNC_ */
#define externalErrorExit(ignore) _externalErrorExit_func(NULL, NULL, __LINE__)
#define externalErrorExitDesc(errDesc) \
  _externalErrorExit_func(errDesc, NULL, __LINE__)
#define mhdErrorExit(ignore) _mhdErrorExit_func(NULL, NULL, __LINE__)
#define mhdErrorExitDesc(errDesc) _mhdErrorExit_func(errDesc, NULL, __LINE__)
#define testErrorLog(ignore) _testErrorLog_func(NULL, NULL, __LINE__)
#define testErrorLogDesc(errDesc) _testErrorLog_func(errDesc, NULL, __LINE__)
#endif /* ! MHD_HAVE_MHD_FUNC_ */

/* ** External parameters ** */
static bool use_large;

static bool use_vlarge;

static bool test_tls;

static int verbose = 0;

enum tls_tool
{
  TLS_CLI_NO_TOOL = 0,
  TLS_CLI_GNUTLS,
  TLS_CLI_OPENSSL,
  TLS_LIB_GNUTLS
};

static enum tls_tool use_tls_tool;


/* ** Internal values ** */

/* Could be increased to facilitate debugging */
static int test_timeout = 5;

static uint16_t global_port;

static const void *rclient_msg;

static size_t rclient_msg_size;

static const void *app_msg;

static size_t app_msg_size;

static void *alloc_ptr[2] = {NULL, NULL};


static void
fflush_allstd (void)
{
  fflush (stderr);
  fflush (stdout);
}


#if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID)
/**
 * Fork child that connects via GnuTLS-CLI to our @a port.  Allows us to
 * talk to our port over a socket in @a sp without having to worry
 * about TLS.
 *
 * @param location where the socket is returned
 * @return -1 on error, otherwise PID of TLS child process
 */
static pid_t
gnutlscli_connect (int *sock,
                   uint16_t port)
{
  pid_t chld;
  int sp[2];
  char destination[30];

  if (0 != socketpair (AF_UNIX,
                       SOCK_STREAM,
                       0,
                       sp))
  {
    testErrorLogDesc ("socketpair() failed");
    return (pid_t) -1;
  }
  chld = fork ();
  if (0 != chld)
  {
    *sock = sp[1];
    MHD_socket_close_chk_ (sp[0]);
    return chld;
  }
  MHD_socket_close_chk_ (sp[1]);
  (void) close (0);
  (void) close (1);
  if (-1 == dup2 (sp[0], 0))
    externalErrorExitDesc ("dup2() failed");
  if (-1 == dup2 (sp[0], 1))
    externalErrorExitDesc ("dup2() failed");
  MHD_socket_close_chk_ (sp[0]);
  if (TLS_CLI_GNUTLS == use_tls_tool)
  {
    snprintf (destination,
              sizeof(destination),
              "%u",
              (unsigned int) port);
    execlp ("gnutls-cli",
            "gnutls-cli",
            "--insecure",
            "-p",
            destination,
            "127.0.0.1",
            (char *) NULL);
  }
  else if (TLS_CLI_OPENSSL == use_tls_tool)
  {
    snprintf (destination,
              sizeof(destination),
              "127.0.0.1:%u",
              (unsigned int) port);
    execlp ("openssl",
            "openssl",
            "s_client",
            "-connect",
            destination,
            "-verify",
            "1",
            (char *) NULL);
  }
  _exit (1);
}


#endif /* HTTPS_SUPPORT && HAVE_FORK && HAVE_WAITPID */


#if 0 /* Unused code */
/**
 * Change socket to blocking.
 *
 * @param fd the socket to manipulate
 */
static void
make_blocking (MHD_socket fd)
{
#if defined(MHD_POSIX_SOCKETS)
  int flags;

  flags = fcntl (fd, F_GETFL);
  if (-1 == flags)
    externalErrorExitDesc ("fcntl() failed");
  if ((flags & ~O_NONBLOCK) != flags)
    if (-1 == fcntl (fd, F_SETFL, flags & ~O_NONBLOCK))
      externalErrorExitDesc ("fcntl() failed");
#elif defined(MHD_WINSOCK_SOCKETS)
  unsigned long flags = 0;

  if (0 != ioctlsocket (fd, (int) FIONBIO, &flags))
    externalErrorExitDesc ("ioctlsocket() failed");
#endif /* MHD_WINSOCK_SOCKETS */
}


#endif /* Unused code */


/**
 * Change socket to non-blocking.
 *
 * @param fd the socket to manipulate
 */
static void
make_nonblocking (MHD_socket fd)
{
#if defined(MHD_POSIX_SOCKETS)
  int flags;

  flags = fcntl (fd, F_GETFL);
  if (-1 == flags)
    externalErrorExitDesc ("fcntl() failed");
  if (O_NONBLOCK != (flags & O_NONBLOCK))
    if (-1 == fcntl (fd, F_SETFL, flags | O_NONBLOCK))
      externalErrorExitDesc ("fcntl() failed");
#elif defined(MHD_WINSOCK_SOCKETS)
  unsigned long flags = 1;

  if (0 != ioctlsocket (fd, (int) FIONBIO, &flags))
    externalErrorExitDesc ("ioctlsocket() failed");
#endif /* MHD_WINSOCK_SOCKETS */
}


/**
 * Enable TCP_NODELAY on TCP/IP socket.
 *
 * @param fd the socket to manipulate
 */
static void
make_nodelay (MHD_socket fd)
{
#ifdef TCP_NODELAY
  const MHD_SCKT_OPT_BOOL_ on_val = 1;

  if (0 == setsockopt (fd,
                       IPPROTO_TCP,
                       TCP_NODELAY,
                       (const void *) &on_val,
                       sizeof (on_val)))
    return; /* Success exit point */

#ifndef MHD_WINSOCK_SOCKETS
  fprintf (stderr, "Failed to enable TCP_NODELAY on socket (ignored). "
           "errno: %d (%s)\n", (int) errno, strerror (errno));
#else /* MHD_WINSOCK_SOCKETS */
  fprintf (stderr, "Failed to enable TCP_NODELAY on socket (ignored). "
           "WSAGetLastError() value: %d\n", (int) WSAGetLastError ());
#endif /* MHD_WINSOCK_SOCKETS */
  fflush (stderr);
#endif /* TCP_NODELAY */
}


/**
 * Wrapper structure for plain&TLS sockets
 */
struct wr_socket
{
  /**
   * Real network socket
   */
  MHD_socket fd;

  /**
   * Type of this socket
   */
  enum wr_type
  {
    wr_invalid = 0,
    wr_plain = 1,
    wr_tls = 2
  } t;

  bool is_nonblocking;

  bool eof_recieved;
#ifdef HTTPS_SUPPORT
  /**
   * TLS credentials
   */
  gnutls_certificate_credentials_t tls_crd;

  /**
   * TLS session.
   */
  gnutls_session_t tls_s;

  /**
   * TLS handshake already succeed?
   */
  bool tls_connected;
#endif
};


/**
 * Get underlying real socket.
 * @return FD of real socket
 */
#define wr_fd(s) ((s)->fd)


#if 0 /* Unused code */
static void
wr_make_blocking  (struct wr_socket *s)
{
  if (s->is_nonblocking)
    make_blocking (s->fd);
  s->is_nonblocking = false;
}


#endif /* Unused code */


static void
wr_make_nonblocking  (struct wr_socket *s)
{
  if (! s->is_nonblocking)
    make_nonblocking (s->fd);
  s->is_nonblocking = true;
}


/**
 * Create wr_socket with plain TCP underlying socket
 * @return created socket on success, NULL otherwise
 */
static struct wr_socket *
wr_create_plain_sckt (void)
{
  struct wr_socket *s = malloc (sizeof(struct wr_socket));
  if (NULL == s)
  {
    testErrorLogDesc ("malloc() failed");
    return NULL;
  }
  s->t = wr_plain;
  s->eof_recieved = false;
  s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  s->is_nonblocking = false;
  if (MHD_INVALID_SOCKET != s->fd)
  {
    make_nodelay (s->fd);
    return s; /* Success */
  }
  testErrorLogDesc ("socket() failed");
  free (s);
  return NULL;
}


/**
 * Create wr_socket with TLS TCP underlying socket
 * @return created socket on success, NULL otherwise
 */
static struct wr_socket *
wr_create_tls_sckt (void)
{
#ifdef HTTPS_SUPPORT
  struct wr_socket *s = malloc (sizeof(struct wr_socket));
  if (NULL == s)
  {
    testErrorLogDesc ("malloc() failed");
    return NULL;
  }
  s->t = wr_tls;
  s->eof_recieved = false;
  s->tls_connected = 0;
  s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  s->is_nonblocking = false;
  if (MHD_INVALID_SOCKET != s->fd)
  {
    make_nodelay (s->fd);
    if (GNUTLS_E_SUCCESS == gnutls_init (&(s->tls_s), GNUTLS_CLIENT))
    {
      if (GNUTLS_E_SUCCESS == gnutls_set_default_priority (s->tls_s))
      {
        if (GNUTLS_E_SUCCESS ==
            gnutls_certificate_allocate_credentials (&(s->tls_crd)))
        {
          if (GNUTLS_E_SUCCESS == gnutls_credentials_set (s->tls_s,
                                                          GNUTLS_CRD_CERTIFICATE,
                                                          s->tls_crd))
          {
#if (GNUTLS_VERSION_NUMBER + 0 >= 0x030109) && ! defined(_WIN64)
            gnutls_transport_set_int (s->tls_s, (int) (s->fd));
#else  /* GnuTLS before 3.1.9 or Win x64 */
            gnutls_transport_set_ptr (s->tls_s,
                                      (gnutls_transport_ptr_t) (intptr_t) (s->fd));
#endif /* GnuTLS before 3.1.9 or Win x64 */
            return s;
          }
          else
            testErrorLogDesc ("gnutls_credentials_set() failed");
          gnutls_certificate_free_credentials (s->tls_crd);
        }
        else
          testErrorLogDesc ("gnutls_certificate_allocate_credentials() failed");
      }
      else
        testErrorLogDesc ("gnutls_set_default_priority() failed");
      gnutls_deinit (s->tls_s);
    }
    else
      testErrorLogDesc ("gnutls_init() failed");
    (void) MHD_socket_close_ (s->fd);
  }
  else
    testErrorLogDesc ("socket() failed");
  free (s);
#endif /* HTTPS_SUPPORT */
  return NULL;
}


/**
 * Create wr_socket with plain TCP underlying socket
 * from already created TCP socket.
 * @param plain_sk real TCP socket
 * @return created socket on success, NULL otherwise
 */
static struct wr_socket *
wr_create_from_plain_sckt (MHD_socket plain_sk)
{
  struct wr_socket *s = malloc (sizeof(struct wr_socket));

  if (NULL == s)
  {
    testErrorLogDesc ("malloc() failed");
    return NULL;
  }
  s->t = wr_plain;
  s->eof_recieved = false;
  s->fd = plain_sk;
  s->is_nonblocking = false; /* The actual mode is unknown */
  wr_make_nonblocking (s);   /* Force set mode to have correct status */
  make_nodelay (s->fd);
  return s;
}


/**
 * Check whether shutdown of connection was received from remote
 * @param s socket to check
 * @return zero if shutdown signal has not been received,
 *         1 if shutdown signal was already received
 */
static int
wr_is_eof_received (struct wr_socket *s)
{
  return s->eof_recieved ? 1 : 0;
}


enum wr_wait_for_type
{
  WR_WAIT_FOR_RECV = 0,
  WR_WAIT_FOR_SEND = 1
};

static bool
wr_wait_socket_ready_noabort_ (struct wr_socket *s,
                               int timeout_ms,
                               enum wr_wait_for_type wait_for)
{
  fd_set fds;
  int sel_res;
  struct timeval tmo;
  struct timeval *tmo_ptr;

#ifndef MHD_WINSOCK_SOCKETS
  if (FD_SETSIZE <= s->fd)
    externalErrorExitDesc ("Too large FD value");
#endif /* ! MHD_WINSOCK_SOCKETS */
  FD_ZERO (&fds);
  FD_SET (s->fd, &fds);
  if (0 <= timeout_ms)
  {
#if ! defined(_WIN32) || defined(__CYGWIN__)
    tmo.tv_sec = (time_t) (timeout_ms / 1000);
#else  /* Native W32 */
    tmo.tv_sec = (long) (timeout_ms / 1000);
#endif /* Native W32 */
    tmo.tv_usec = ((long) (timeout_ms % 1000)) * 1000;
    tmo_ptr = &tmo;
  }
  else
    tmo_ptr = NULL; /* No timeout */

  do
  {
    if (WR_WAIT_FOR_RECV == wait_for)
      sel_res = select (1 + (int) s->fd, &fds, NULL, NULL, tmo_ptr);
    else
      sel_res = select (1 + (int) s->fd, NULL, &fds, NULL, tmo_ptr);
  } while (0 > sel_res && MHD_SCKT_ERR_IS_EINTR_ (MHD_socket_get_error_ ()));

  if (1 == sel_res)
    return true;

  if (0 == sel_res)
    fprintf (stderr, "Timeout");
  else
  {
#ifndef MHD_WINSOCK_SOCKETS
    fprintf (stderr, "Error %d (%s)", (int) errno, strerror (errno));
#else /* MHD_WINSOCK_SOCKETS */
    fprintf (stderr, "Error (WSAGetLastError code: %d)",
             (int) WSAGetLastError ());
#endif /* MHD_WINSOCK_SOCKETS */
  }
  fprintf (stderr, " waiting for socket to be available for %s.\n",
           (WR_WAIT_FOR_RECV == wait_for) ? "receiving" : "sending");
  return false;
}


static void
wr_wait_socket_ready_ (struct wr_socket *s,
                       int timeout_ms,
                       enum wr_wait_for_type wait_for)
{
  if (wr_wait_socket_ready_noabort_ (s, timeout_ms, wait_for))
    return;

  if (WR_WAIT_FOR_RECV == wait_for)
    mhdErrorExitDesc ("Client or application failed to receive the data");
  else
    mhdErrorExitDesc ("Client or application failed to send the data");
}


/**
 * Connect socket to specified address.
 * @param s socket to use
 * @param addr address to connect
 * @param length of structure pointed by @a addr
 * @param timeout_ms the maximum wait time in milliseconds to send the data,
 *                   no limit if negative value is used
 * @return zero on success, -1 otherwise.
 */
static int
wr_connect_tmo (struct wr_socket *s,
                const struct sockaddr *addr,
                unsigned int length,
                int timeout_ms)
{
  if (0 != connect (s->fd, addr, (socklen_t) length))
  {
    int err;
    bool connect_completed = false;

    err = MHD_socket_get_error_ ();
#if defined(MHD_POSIX_SOCKETS)
    while (! connect_completed && (EINTR == err))
    {
      connect_completed = (0 == connect (s->fd, addr, (socklen_t) length));
      if (! connect_completed)
      {
        err = errno;
        if (EALREADY == err)
          err = EINPROGRESS;
        else if (EISCONN == err)
          connect_completed = true;
      }
    }
#endif /* MHD_POSIX_SOCKETS */
    if (! connect_completed &&
        (MHD_SCKT_ERR_IS_ (err, MHD_SCKT_EINPROGRESS_)
         || MHD_SCKT_ERR_IS_EAGAIN_ (err))) /* No modern system uses EAGAIN, except W32 */
      connect_completed =
        wr_wait_socket_ready_noabort_ (s, timeout_ms, WR_WAIT_FOR_SEND);
    if (! connect_completed)
    {
      testErrorLogDesc ("connect() failed");
      return -1;
    }
  }
  if (wr_plain == s->t)
    return 0;
#ifdef HTTPS_SUPPORT
  if (wr_tls == s->t)
  {
    /* Do not try handshake here as
     * it requires processing on MHD side and
     * when testing with "external" polling,
     * test will call MHD processing only
     * after return from wr_connect(). */
    s->tls_connected = 0;
    return 0;
  }
#endif /* HTTPS_SUPPORT */
  testErrorLogDesc ("HTTPS socket connect called, but code does not support" \
                    " HTTPS sockets");
  return -1;
}


/**
 * Connect socket to specified address.
 * @param s socket to use
 * @param addr address to connect
 * @param length of structure pointed by @a addr
 * @return zero on success, -1 otherwise.
 */
static int
wr_connect (struct wr_socket *s,
            const struct sockaddr *addr,
            unsigned int length)
{
  return wr_connect_tmo (s, addr, length, test_timeout * 1000);
}


#ifdef HTTPS_SUPPORT
/* Only to be called from wr_send() and wr_recv() ! */
static bool
wr_handshake_tmo_ (struct wr_socket *s,
                   int timeout_ms)
{
  int res = gnutls_handshake (s->tls_s);

  while ((GNUTLS_E_AGAIN == res) || (GNUTLS_E_INTERRUPTED  == res))
  {
    wr_wait_socket_ready_ (s, timeout_ms,
                           gnutls_record_get_direction (s->tls_s) ?
                           WR_WAIT_FOR_SEND : WR_WAIT_FOR_RECV);
    res = gnutls_handshake (s->tls_s);
  }
  if (GNUTLS_E_SUCCESS == res)
    s->tls_connected = true;
  else
  {
    fprintf (stderr, "The error returned by gnutls_handshake() is "
             "'%s' ", gnutls_strerror ((int) res));
#if GNUTLS_VERSION_NUMBER >= 0x020600
    fprintf (stderr, "(%s)\n", gnutls_strerror_name ((int) res));
#else  /* GNUTLS_VERSION_NUMBER < 0x020600 */
    fprintf (stderr, "(%d)\n", (int) res);
#endif /* GNUTLS_VERSION_NUMBER < 0x020600 */
    testErrorLogDesc ("gnutls_handshake() failed with hard error");
    MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */
  }
  return s->tls_connected;
}


#if 0 /* Unused function */
/* Only to be called from wr_send() and wr_recv() ! */
static bool
wr_handshake_ (struct wr_socket *s)
{
  return wr_handshake_tmo_ (s, test_timeout * 1000);
}


#endif /* Unused function */

#endif /* HTTPS_SUPPORT */


/**
 * Send data to remote by socket.
 * @param s the socket to use
 * @param buf the buffer with data to send
 * @param len the length of data in @a buf
 * @param timeout_ms the maximum wait time in milliseconds to send the data,
 *                   no limit if negative value is used
 * @return number of bytes were sent if succeed,
 *         -1 if failed. Use #MHD_socket_get_error_()
 *         to get socket error.
 */
static ssize_t
wr_send_tmo (struct wr_socket *s,
             const void *buf,
             size_t len,
             int timeout_ms)
{
  if (wr_plain == s->t)
  {
    ssize_t res;
    while (! 0)
    {
      int err;
      res = MHD_send_ (s->fd, buf, len);
      if (0 <= res)
        break; /* Success */
      err = MHD_socket_get_error_ ();
      if (! MHD_SCKT_ERR_IS_EAGAIN_ (err) && ! MHD_SCKT_ERR_IS_EINTR_ (err))
        break; /* Failure */
      wr_wait_socket_ready_ (s, timeout_ms, WR_WAIT_FOR_SEND);
    }
    return res;
  }
#ifdef HTTPS_SUPPORT
  else if (wr_tls == s->t)
  {
    ssize_t ret;
    if (! s->tls_connected && ! wr_handshake_tmo_ (s, timeout_ms))
      return -1;

    while (1)
    {
      ret = gnutls_record_send (s->tls_s, buf, len);
      if (ret >= 0)
        return ret;
      if ((GNUTLS_E_AGAIN != ret) && (GNUTLS_E_INTERRUPTED  != ret))
        break;
      wr_wait_socket_ready_ (s, timeout_ms,
                             gnutls_record_get_direction (s->tls_s) ?
                             WR_WAIT_FOR_SEND : WR_WAIT_FOR_RECV);
    }
    fprintf (stderr, "The error returned by gnutls_record_send() is "
             "'%s' ", gnutls_strerror ((int) ret));
#if GNUTLS_VERSION_NUMBER >= 0x020600
    fprintf (stderr, "(%s)\n", gnutls_strerror_name ((int) ret));
#else  /* GNUTLS_VERSION_NUMBER < 0x020600 */
    fprintf (stderr, "(%d)\n", (int) ret);
#endif /* GNUTLS_VERSION_NUMBER < 0x020600 */
    testErrorLogDesc ("gnutls_record_send() failed with hard error");
    MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_);   /* hard error */
    return -1;
  }
#endif /* HTTPS_SUPPORT */
  testErrorLogDesc ("HTTPS socket send called, but code does not support" \
                    " HTTPS sockets");
  return -1;
}


/**
 * Send data to remote by socket.
 * @param s the socket to use
 * @param buf the buffer with data to send
 * @param len the length of data in @a buf
 * @return number of bytes were sent if succeed,
 *         -1 if failed. Use #MHD_socket_get_error_()
 *         to get socket error.
 */
static ssize_t
wr_send (struct wr_socket *s,
         const void *buf,
         size_t len)
{
  return wr_send_tmo (s, buf, len, test_timeout * 1000);
}


/**
 * Receive data from remote by socket.
 * @param s the socket to use
 * @param buf the buffer to store received data
 * @param len the length of @a buf
 * @param timeout_ms the maximum wait time in milliseconds to receive the data,
 *                   no limit if negative value is used
 * @return number of bytes were received if succeed,
 *         -1 if failed. Use #MHD_socket_get_error_()
 *         to get socket error.
 */
static ssize_t
wr_recv_tmo (struct wr_socket *s,
             void *buf,
             size_t len,
             int timeout_ms)
{
  if (wr_plain == s->t)
  {
    ssize_t res;
    while (! 0)
    {
      int err;
      res = MHD_recv_ (s->fd, buf, len);
      if (0 == res)
        s->eof_recieved = true;
      if (0 <= res)
        break; /* Success */
      err = MHD_socket_get_error_ ();
      if (! MHD_SCKT_ERR_IS_EAGAIN_ (err) && ! MHD_SCKT_ERR_IS_EINTR_ (err))
        break; /* Failure */
      wr_wait_socket_ready_ (s, timeout_ms, WR_WAIT_FOR_RECV);
    }
    return res;
  }
#ifdef HTTPS_SUPPORT
  if (wr_tls == s->t)
  {
    ssize_t ret;
    if (! s->tls_connected && ! wr_handshake_tmo_ (s, timeout_ms))
      return -1;

    while (1)
    {
      ret = gnutls_record_recv (s->tls_s, buf, len);
      if (0 == ret)
        s->eof_recieved = true;
      if (ret >= 0)
        return ret;
      if ((GNUTLS_E_AGAIN != ret) && (GNUTLS_E_INTERRUPTED  != ret))
        break;
      wr_wait_socket_ready_ (s, timeout_ms,
                             gnutls_record_get_direction (s->tls_s) ?
                             WR_WAIT_FOR_SEND : WR_WAIT_FOR_RECV);
    }

    fprintf (stderr, "The error returned by gnutls_record_recv() is "
             "'%s' ", gnutls_strerror ((int) ret));
#if GNUTLS_VERSION_NUMBER >= 0x020600
    fprintf (stderr, "(%s)\n", gnutls_strerror_name ((int) ret));
#else  /* GNUTLS_VERSION_NUMBER < 0x020600 */
    fprintf (stderr, "(%d)\n", (int) ret);
#endif /* GNUTLS_VERSION_NUMBER < 0x020600 */
    testErrorLogDesc ("gnutls_record_recv() failed with hard error");
    MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_);   /* hard error */
    return -1;
  }
#endif /* HTTPS_SUPPORT */
  return -1;
}


/**
 * Receive data from remote by socket.
 * @param s the socket to use
 * @param buf the buffer to store received data
 * @param len the length of @a buf
 * @return number of bytes were received if succeed,
 *         -1 if failed. Use #MHD_socket_get_error_()
 *         to get socket error.
 */
static ssize_t
wr_recv (struct wr_socket *s,
         void *buf,
         size_t len)
{
  return wr_recv_tmo (s, buf, len, test_timeout * 1000);
}


/**
 * Shutdown send/write on the socket.
 * @param s the socket to shutdown
 * @param how the type of shutdown: SHUT_WR or SHUT_RDWR
 * @param timeout_ms the maximum wait time in milliseconds to receive the data,
 *                   no limit if negative value is used
 * @return zero on succeed, -1 otherwise
 */
static int
wr_shutdown_tmo (struct wr_socket *s, int how, int timeout_ms)
{
  switch (how)
  {
  case SHUT_WR: /* Valid value */
    break;
  case SHUT_RDWR: /* Valid value */
    break;
  case SHUT_RD:
    externalErrorExitDesc ("Unsupported 'how' value");
    break;
  default:
    externalErrorExitDesc ("Invalid 'how' value");
    break;
  }
  if (wr_plain == s->t)
  {
    (void) timeout_ms; /* Unused parameter for plain sockets */
    return shutdown (s->fd, how);
  }
#ifdef HTTPS_SUPPORT
  if (wr_tls == s->t)
  {
    ssize_t ret;
    if (! s->tls_connected && ! wr_handshake_tmo_ (s, timeout_ms))
      return -1;

    while (1)
    {
      ret =
        gnutls_bye  (s->tls_s,
                     (SHUT_WR == how) ?  GNUTLS_SHUT_WR :  GNUTLS_SHUT_RDWR);
      if (GNUTLS_E_SUCCESS == ret)
      {
#if 0 /* Disabled to test pure behaviour */
        if (SHUT_RDWR == how)
          (void) shutdown (s->fd, how); /* Also shutdown the underlying transport layer */
#endif
        return 0;
      }
      if ((GNUTLS_E_AGAIN != ret) && (GNUTLS_E_INTERRUPTED  != ret))
        break;
      wr_wait_socket_ready_ (s, timeout_ms,
                             gnutls_record_get_direction (s->tls_s) ?
                             WR_WAIT_FOR_SEND : WR_WAIT_FOR_RECV);
    }

    fprintf (stderr, "The error returned by gnutls_bye() is "
             "'%s' ", gnutls_strerror ((int) ret));
#if GNUTLS_VERSION_NUMBER >= 0x020600
    fprintf (stderr, "(%s)\n", gnutls_strerror_name ((int) ret));
#else  /* GNUTLS_VERSION_NUMBER < 0x020600 */
    fprintf (stderr, "(%d)\n", (int) ret);
#endif /* GNUTLS_VERSION_NUMBER < 0x020600 */
    testErrorLogDesc ("gnutls_bye() failed with hard error");
    MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_);   /* hard error */
    return -1;
  }
#endif /* HTTPS_SUPPORT */
  return -1;
}


/**
 * Shutdown the socket.
 * @param s the socket to shutdown
 * @return zero on succeed, -1 otherwise
 */
static int
wr_shutdown (struct wr_socket *s, int how)
{
  return wr_shutdown_tmo (s, how, test_timeout * 1000);
}


/**
 * Close socket and release allocated resourced
 * @param s the socket to close
 * @return zero on succeed, -1 otherwise
 */
static int
wr_close (struct wr_socket *s)
{
  int ret = (MHD_socket_close_ (s->fd)) ? 0 : -1;
#ifdef HTTPS_SUPPORT
  if (wr_tls == s->t)
  {
    gnutls_deinit (s->tls_s);
    gnutls_certificate_free_credentials (s->tls_crd);
  }
#endif /* HTTPS_SUPPORT */
  free (s);
  return ret;
}


/**
 * Thread we use to run the interaction with the upgraded socket.
 */
static pthread_t pt;

/**
 * Will be set to the upgraded socket.
 */
static struct wr_socket *volatile usock;

/**
 * Thread we use to run the interaction with the upgraded socket.
 */
static pthread_t pt_client;

/**
 * Flag set to true once the client is finished.
 */
static volatile bool client_done;

/**
 * Flag set to true once the app is finished.
 */
static volatile bool app_done;


static const char *
term_reason_str (enum MHD_RequestTerminationCode term_code)
{
  switch ((int) term_code)
  {
  case MHD_REQUEST_TERMINATED_COMPLETED_OK:
    return "COMPLETED_OK";
  case MHD_REQUEST_TERMINATED_WITH_ERROR:
    return "TERMINATED_WITH_ERROR";
  case MHD_REQUEST_TERMINATED_TIMEOUT_REACHED:
    return "TIMEOUT_REACHED";
  case MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN:
    return "DAEMON_SHUTDOWN";
  case MHD_REQUEST_TERMINATED_READ_ERROR:
    return "READ_ERROR";
  case MHD_REQUEST_TERMINATED_CLIENT_ABORT:
    return "CLIENT_ABORT";
  case -1:
    return "(not called)";
  default:
    return "(unknown code)";
  }
  return "(problem)"; /* unreachable */
}


/**
 * Callback used by MHD to notify the application about completed
 * requests.  Frees memory.
 *
 * @param cls client-defined closure
 * @param connection connection handle
 * @param req_cls value as set by the last call to
 *        the #MHD_AccessHandlerCallback
 * @param toe reason for request termination
 */
static void
notify_completed_cb (void *cls,
                     struct MHD_Connection *connection,
                     void **req_cls,
                     enum MHD_RequestTerminationCode toe)
{
  (void) cls;
  (void) connection;  /* Unused. Silent compiler warning. */
  if (verbose)
    printf ("notify_completed_cb() has been called with '%s' code.\n",
            term_reason_str (toe));
  if ( (toe != MHD_REQUEST_TERMINATED_COMPLETED_OK) &&
       (toe != MHD_REQUEST_TERMINATED_CLIENT_ABORT) &&
       (toe != MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN) )
    mhdErrorExitDesc ("notify_completed_cb() called with wrong code");
  if (NULL == req_cls)
    mhdErrorExitDesc ("'req_cls' parameter is NULL");
  if (NULL == *req_cls)
    mhdErrorExitDesc ("'*req_cls' pointer is NULL");
  if (! pthread_equal (**((pthread_t **) req_cls),
                       pthread_self ()))
    mhdErrorExitDesc ("notify_completed_cb() is called in wrong thread");
  free (*req_cls);
  *req_cls = NULL;
}


/**
 * Logging callback.
 *
 * @param cls logging closure (NULL)
 * @param uri access URI
 * @param connection connection handle
 * @return #TEST_PTR
 */
static void *
log_cb (void *cls,
        const char *uri,
        struct MHD_Connection *connection)
{
  pthread_t *ppth;

  (void) cls;
  (void) connection;  /* Unused. Silent compiler warning. */
  if (NULL == uri)
    mhdErrorExitDesc ("The 'uri' parameter is NULL");
  if (0 != strcmp (uri, "/"))
  {
    fprintf (stderr, "Wrong 'uri' value: '%s'. ", uri);
    mhdErrorExit ();
  }
  ppth = malloc (sizeof (pthread_t));
  if (NULL == ppth)
    externalErrorExitDesc ("malloc() failed");
  *ppth = pthread_self ();
  return (void *) ppth;
}


/**
 * Function to check that MHD properly notifies about starting
 * and stopping.
 *
 * @param cls client-defined closure
 * @param connection connection handle
 * @param socket_context socket-specific pointer where the
 *                       client can associate some state specific
 *                       to the TCP connection; note that this is
 *                       different from the "req_cls" which is per
 *                       HTTP request.  The client can initialize
 *                       during #MHD_CONNECTION_NOTIFY_STARTED and
 *                       cleanup during #MHD_CONNECTION_NOTIFY_CLOSED
 *                       and access in the meantime using
 *                       #MHD_CONNECTION_INFO_SOCKET_CONTEXT.
 * @param toe reason for connection notification
 * @see #MHD_OPTION_NOTIFY_CONNECTION
 * @ingroup request
 */
static void
notify_connection_cb (void *cls,
                      struct MHD_Connection *connection,
                      void **socket_context,
                      enum MHD_ConnectionNotificationCode toe)
{
  static int started = MHD_NO;

  (void) cls;
  (void) connection;  /* Unused. Silent compiler warning. */
  switch (toe)
  {
  case MHD_CONNECTION_NOTIFY_STARTED:
    if (MHD_NO != started)
      mhdErrorExitDesc ("The connection has been already started");
    started = MHD_YES;
    *socket_context = &started;
    break;
  case MHD_CONNECTION_NOTIFY_CLOSED:
    if (MHD_YES != started)
      mhdErrorExitDesc ("The connection has not been started before");
    if (&started != *socket_context)
      mhdErrorExitDesc ("Wrong '*socket_context' value");
    *socket_context = NULL;
    started = MHD_NO;
    break;
  }
}


static void
send_all (struct wr_socket *sock,
          const void *data,
          size_t data_size)
{
  ssize_t ret;
  size_t sent;
  const uint8_t *const buf = (const uint8_t *) data;

  wr_make_nonblocking (sock);
  for (sent = 0; sent < data_size; sent += (size_t) ret)
  {
    ret = wr_send (sock,
                   buf + sent,
                   data_size - sent);
    if (0 > ret)
    {
      if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ()) ||
          MHD_SCKT_ERR_IS_EINTR_ (MHD_socket_get_error_ ()))
      {
        ret = 0;
        continue;
      }
      externalErrorExitDesc ("send() failed");
    }
  }
}


#define send_all_stext(sk,st) send_all(sk,st,MHD_STATICSTR_LEN_(st))


/**
 * Read character-by-character until we
 * get 'CRLNCRLN'.
 */
static void
recv_hdr (struct wr_socket *sock)
{
  unsigned int i;
  char next;
  char c;
  ssize_t ret;

  wr_make_nonblocking (sock);
  next = '\r';
  i = 0;
  while (i < 4)
  {
    ret = wr_recv (sock,
                   &c,
                   1);
    if (0 > ret)
    {
      if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ()))
        continue;
      if (MHD_SCKT_ERR_IS_EINTR_ (MHD_socket_get_error_ ()))
        continue;
      externalErrorExitDesc ("recv() failed");
    }
    if (0 == ret)
      mhdErrorExitDesc ("The server unexpectedly closed connection");
    if (c == next)
    {
      i++;
      if (next == '\r')
        next = '\n';
      else
        next = '\r';
      continue;
    }
    if (c == '\r')
    {
      i = 1;
      next = '\n';
      continue;
    }
    i = 0;
    next = '\r';
  }
}


static void
recv_all (struct wr_socket *sock,
          const void *data,
          size_t data_size)
{
  uint8_t *buf;
  ssize_t ret;
  size_t rcvd;

  buf = (uint8_t *) malloc (data_size);
  if (NULL == buf)
    externalErrorExitDesc ("malloc() failed");

  wr_make_nonblocking (sock);
  for (rcvd = 0; rcvd < data_size; rcvd += (size_t) ret)
  {
    ret = wr_recv (sock,
                   buf + rcvd,
                   data_size - rcvd);
    if (0 > ret)
    {
      if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ()) ||
          MHD_SCKT_ERR_IS_EINTR_ (MHD_socket_get_error_ ()))
      {
        ret = 0;
        continue;
      }
      externalErrorExitDesc ("recv() failed");
    }
    else if (0 == ret)
    {
      fprintf (stderr, "Partial only received text. Expected: '%.*s' "
               "(length: %ud). Got: '%.*s' (length: %ud). ",
               (int) data_size, (const char *) data, (unsigned int) data_size,
               (int) rcvd, (const char *) buf, (unsigned int) rcvd);
      mhdErrorExitDesc ("The server unexpectedly closed connection");
    }
    if ((data_size - rcvd) < (size_t) ret)
      externalErrorExitDesc ("recv() returned excessive amount of data");
    if (0 != memcmp (data, buf, rcvd + (size_t) ret))
    {
      fprintf (stderr, "Wrong received text. Expected: '%.*s'. "
               "Got: '%.*s'. ",
               (int) (rcvd + (size_t) ret), (const char *) data,
               (int) (rcvd + (size_t) ret), (const char *) buf);
      mhdErrorExit ();
    }
  }
  if (0 != memcmp (data, buf, data_size))
  {
    fprintf (stderr, "Wrong received text. Expected: '%.*s'. "
             "Got: '%.*s'. ",
             (int) data_size, (const char *) data,
             (int) data_size, (const char *) buf);
    mhdErrorExit ();
  }
  free (buf);
}


#define recv_all_stext(sk,st) recv_all(sk,st,MHD_STATICSTR_LEN_(st))


/**
 * Shutdown write of the connection to signal end of transmission
 * for the remote side
 * @param sock the socket to shutdown
 */
static void
send_eof (struct wr_socket *sock)
{
  if (0 != wr_shutdown (sock, wr_is_eof_received (sock) ? SHUT_RDWR : SHUT_WR))
    externalErrorExitDesc ("Failed to shutdown connection");
}


/**
 * Receive end of the transmission indication from the remote side
 * @param sock the socket to use
 */
static void
receive_eof (struct wr_socket *sock)
{
  uint8_t buf[127];
  ssize_t ret;
  size_t rcvd;
  bool got_eof = false;

  wr_make_nonblocking (sock);
  for (rcvd = 0; rcvd < sizeof(buf); rcvd += (size_t) ret)
  {
    ret = wr_recv (sock,
                   buf + rcvd,
                   sizeof(buf) - rcvd);
    if (0 > ret)
    {
      if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ()) ||
          MHD_SCKT_ERR_IS_EINTR_ (MHD_socket_get_error_ ()))
      {
        ret = 0;
        continue;
      }
      externalErrorExitDesc ("recv() failed");
    }
    else if (0 == ret)
    {
      got_eof = true;
      break;
    }
  }
  if (got_eof && (0 == rcvd))
    return; /* Success */

  if (0 != rcvd)
  {
    if (sizeof(buf) == rcvd)
    {
      fprintf (stderr, "Received at least %lu extra bytes while "
               "end-of-file is expected.\n", (unsigned long) sizeof(buf));
      mhdErrorExit ();
    }
    fprintf (stderr, "Received at %lu extra bytes and then %s"
             "end-of-file marker.\n", (unsigned long) rcvd,
             got_eof ? "" : "NO ");
    mhdErrorExit ();
  }
  if (! got_eof)
    mhdErrorExitDesc ("Failed to receive end-of-file marker.");
}


/**
 * Main function for the thread that runs the interaction with
 * the upgraded socket.
 *
 * @param cls the handle for the upgrade
 */
static void *
run_usock (void *cls)
{
  struct MHD_UpgradeResponseHandle *urh = cls;

  recv_all (usock, rclient_msg, rclient_msg_size);
  send_all (usock, app_msg, app_msg_size);
  recv_all_stext (usock,
                  "Finished");
  if (! test_tls)
  {
    receive_eof (usock);
    send_eof (usock);
  }
  MHD_upgrade_action (urh,
                      MHD_UPGRADE_ACTION_CLOSE);
  free (usock);
  usock = NULL;
  app_done = true;
  return NULL;
}


/**
 * Main function for the thread that runs the client-side of the
 * interaction with the upgraded socket.
 *
 * @param cls the client socket
 */
static void *
run_usock_client (void *cls)
{
  struct wr_socket *sock = cls;

  send_all_stext (sock,
                  "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: Upgrade\r\n\r\n");
  recv_hdr (sock);
  send_all (sock, rclient_msg, rclient_msg_size);
  recv_all (sock, app_msg, app_msg_size);
  send_all_stext (sock,
                  "Finished");
  if (! test_tls)
  {
    send_eof (sock);
    receive_eof (sock);
  }
  wr_close (sock);
  client_done = true;
  return NULL;
}


/**
 * Function called after a protocol "upgrade" response was sent
 * successfully and the socket should now be controlled by some
 * protocol other than HTTP.
 *
 * Any data already received on the socket will be made available in
 * @e extra_in.  This can happen if the application sent extra data
 * before MHD send the upgrade response.  The application should
 * treat data from @a extra_in as if it had read it from the socket.
 *
 * Note that the application must not close() @a sock directly,
 * but instead use #MHD_upgrade_action() for special operations
 * on @a sock.
 *
 * Except when in 'thread-per-connection' mode, implementations
 * of this function should never block (as it will still be called
 * from within the main event loop).
 *
 * @param cls closure, whatever was given to #MHD_create_response_for_upgrade().
 * @param connection original HTTP connection handle,
 *                   giving the function a last chance
 *                   to inspect the original HTTP request
 * @param req_cls last value left in `req_cls` of the `MHD_AccessHandlerCallback`
 * @param extra_in if we happened to have read bytes after the
 *                 HTTP header already (because the client sent
 *                 more than the HTTP header of the request before
 *                 we sent the upgrade response),
 *                 these are the extra bytes already read from @a sock
 *                 by MHD.  The application should treat these as if
 *                 it had read them from @a sock.
 * @param extra_in_size number of bytes in @a extra_in
 * @param sock socket to use for bi-directional communication
 *        with the client.  For HTTPS, this may not be a socket
 *        that is directly connected to the client and thus certain
 *        operations (TCP-specific setsockopt(), getsockopt(), etc.)
 *        may not work as expected (as the socket could be from a
 *        socketpair() or a TCP-loopback).  The application is expected
 *        to perform read()/recv() and write()/send() calls on the socket.
 *        The application may also call shutdown(), but must not call
 *        close() directly.
 * @param urh argument for #MHD_upgrade_action()s on this @a connection.
 *        Applications must eventually use this callback to (indirectly)
 *        perform the close() action on the @a sock.
 */
static void
upgrade_cb (void *cls,
            struct MHD_Connection *connection,
            void *req_cls,
            const char *extra_in,
            size_t extra_in_size,
            MHD_socket sock,
            struct MHD_UpgradeResponseHandle *urh)
{
  (void) cls;
  (void) connection;
  (void) req_cls;
  (void) extra_in; /* Unused. Silent compiler warning. */

  usock = wr_create_from_plain_sckt (sock);
  wr_make_nonblocking (usock);
  if (0 != extra_in_size)
    mhdErrorExitDesc ("'extra_in_size' is not zero");
  if (0 != pthread_create (&pt,
                           NULL,
                           &run_usock,
                           urh))
    externalErrorExitDesc ("pthread_create() failed");
}


/**
 * A client has requested the given url using the given method
 * (#MHD_HTTP_METHOD_GET, #MHD_HTTP_METHOD_PUT,
 * #MHD_HTTP_METHOD_DELETE, #MHD_HTTP_METHOD_POST, etc).  The callback
 * must call MHD callbacks to provide content to give back to the
 * client and return an HTTP status code (i.e. #MHD_HTTP_OK,
 * #MHD_HTTP_NOT_FOUND, etc.).
 *
 * @param cls argument given together with the function
 *        pointer when the handler was registered with MHD
 * @param url the requested url
 * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
 *        #MHD_HTTP_METHOD_PUT, etc.)
 * @param version the HTTP version string (i.e.
 *        #MHD_HTTP_VERSION_1_1)
 * @param upload_data the data being uploaded (excluding HEADERS,
 *        for a POST that fits into memory and that is encoded
 *        with a supported encoding, the POST data will NOT be
 *        given in upload_data and is instead available as
 *        part of #MHD_get_connection_values; very large POST
 *        data *will* be made available incrementally in
 *        @a upload_data)
 * @param upload_data_size set initially to the size of the
 *        @a upload_data provided; the method must update this
 *        value to the number of bytes NOT processed;
 * @param req_cls pointer that the callback can set to some
 *        address and that will be preserved by MHD for future
 *        calls for this request; since the access handler may
 *        be called many times (i.e., for a PUT/POST operation
 *        with plenty of upload data) this allows the application
 *        to easily associate some request-specific state.
 *        If necessary, this state can be cleaned up in the
 *        global #MHD_RequestCompletedCallback (which
 *        can be set with the #MHD_OPTION_NOTIFY_COMPLETED).
 *        Initially, `*req_cls` will be NULL.
 * @return #MHD_YES if the connection was handled successfully,
 *         #MHD_NO if the socket must be closed due to a serious
 *         error while handling the request
 */
static enum MHD_Result
ahc_upgrade (void *cls,
             struct MHD_Connection *connection,
             const char *url,
             const char *method,
             const char *version,
             const char *upload_data,
             size_t *upload_data_size,
             void **req_cls)
{
  struct MHD_Response *resp;
  (void) cls;
  (void) url;
  (void) method;                        /* Unused. Silent compiler warning. */
  (void) version;
  (void) upload_data;
  (void) upload_data_size;  /* Unused. Silent compiler warning. */

  if (NULL == req_cls)
    mhdErrorExitDesc ("'req_cls' is NULL");
  if (NULL == *req_cls)
    mhdErrorExitDesc ("'*req_cls' value is NULL");
  if (! pthread_equal (**((pthread_t **) req_cls), pthread_self ()))
    mhdErrorExitDesc ("ahc_upgrade() is called in wrong thread");
  resp = MHD_create_response_for_upgrade (&upgrade_cb,
                                          NULL);
  if (NULL == resp)
    mhdErrorExitDesc ("MHD_create_response_for_upgrade() failed");
  if (MHD_YES != MHD_add_response_header (resp,
                                          MHD_HTTP_HEADER_UPGRADE,
                                          "Hello World Protocol"))
    mhdErrorExitDesc ("MHD_add_response_header() failed");
  if (MHD_YES != MHD_queue_response (connection,
                                     MHD_HTTP_SWITCHING_PROTOCOLS,
                                     resp))
    mhdErrorExitDesc ("MHD_queue_response() failed");
  MHD_destroy_response (resp);
  return MHD_YES;
}


/**
 * Run the MHD external event loop using select or epoll.
 *
 * select/epoll modes are used automatically based on daemon's flags.
 *
 * @param daemon daemon to run it for
 */
static void
run_mhd_select_loop (struct MHD_Daemon *daemon)
{
  const time_t start_time = time (NULL);
  const union MHD_DaemonInfo *pdinfo;
  bool connection_was_accepted;
  bool connection_has_finished;
#ifdef EPOLL_SUPPORT
  bool use_epoll = false;
  int ep = -1;

  pdinfo = MHD_get_daemon_info (daemon,
                                MHD_DAEMON_INFO_FLAGS);
  if (NULL == pdinfo)
    mhdErrorExitDesc ("MHD_get_daemon_info() failed");
  else
    use_epoll = (0 != (pdinfo->flags & MHD_USE_EPOLL));
  if (use_epoll)
  {
    pdinfo = MHD_get_daemon_info (daemon,
                                  MHD_DAEMON_INFO_EPOLL_FD);
    if (NULL == pdinfo)
      mhdErrorExitDesc ("MHD_get_daemon_info() failed");
    ep = pdinfo->listen_fd;
    if (0 > ep)
      mhdErrorExitDesc ("Invalid epoll FD value");
  }
#endif /* EPOLL_SUPPORT */

  connection_was_accepted = false;
  connection_has_finished = false;
  while (1)
  {
    fd_set rs;
    fd_set ws;
    fd_set es;
    MHD_socket max_fd;
    struct timeval tv;
    uint64_t to64;
    bool has_mhd_timeout;

    FD_ZERO (&rs);
    FD_ZERO (&ws);
    FD_ZERO (&es);
    max_fd = MHD_INVALID_SOCKET;

    if (time (NULL) - start_time > ((time_t) test_timeout))
      mhdErrorExitDesc ("Test timeout");

    pdinfo = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS);

    if (NULL == pdinfo)
      mhdErrorExitDesc ("MHD_get_daemon_info() failed");

    if (0 != pdinfo->num_connections)
      connection_was_accepted = true;
    else
    {
      if (connection_was_accepted)
        connection_has_finished = true;
    }
    if (connection_has_finished)
      return;

    if (MHD_YES !=
        MHD_get_fdset (daemon,
                       &rs,
                       &ws,
                       &es,
                       &max_fd))
      mhdErrorExitDesc ("MHD_get_fdset() failed");

#ifdef EPOLL_SUPPORT
    if (use_epoll)
    {
      if (ep != max_fd)
        mhdErrorExitDesc ("Wrong 'max_fd' value");
      if (! FD_ISSET (ep, &rs))
        mhdErrorExitDesc ("Epoll FD is NOT set in read fd_set");
    }
#endif /* EPOLL_SUPPORT */

    has_mhd_timeout = (MHD_NO != MHD_get_timeout64 (daemon,
                                                    &to64));
    if (has_mhd_timeout)
    {
#if ! defined(_WIN32) || defined(__CYGWIN__)
      tv.tv_sec = (time_t) (to64 / 1000);
#else  /* Native W32 */
      tv.tv_sec = (long) (to64 / 1000);
#endif /* Native W32 */
      tv.tv_usec = (long) (1000 * (to64 % 1000));
    }
    else
    {
#if ! defined(_WIN32) || defined(__CYGWIN__)
      tv.tv_sec = (time_t) test_timeout;
#else  /* Native W32 */
      tv.tv_sec = (long) test_timeout;
#endif /* Native W32 */
      tv.tv_usec = 0;
    }

#ifdef MHD_WINSOCK_SOCKETS
    if ((0 == rs.fd_count) && (0 == ws.fd_count) && (0 != es.fd_count))
      Sleep ((DWORD) (tv.tv_sec * 1000 + tv.tv_usec / 1000));
    else /* Combined with the next 'if' */
#endif
    if (1)
    {
      int sel_res;
      sel_res = MHD_SYS_select_ (max_fd + 1,
                                 &rs,
                                 &ws,
                                 &es,
                                 &tv);
      if (0 == sel_res)
      {
        if (! has_mhd_timeout)
          mhdErrorExitDesc ("Timeout waiting for data on sockets");
      }
      else if (0 > sel_res)
      {
#ifdef MHD_POSIX_SOCKETS
        if (EINTR != errno)
#endif /* MHD_POSIX_SOCKETS */
        mhdErrorExitDesc ("Unexpected select() error");
      }
    }
    MHD_run_from_select (daemon,
                         &rs,
                         &ws,
                         &es);
  }
}


#ifdef HAVE_POLL

/**
 * Run the MHD external event loop using select.
 *
 * @param daemon daemon to run it for
 */
_MHD_NORETURN static void
run_mhd_poll_loop (struct MHD_Daemon *daemon)
{
  (void) daemon; /* Unused. Silent compiler warning. */
  externalErrorExitDesc ("Not implementable with MHD API");
}


#endif /* HAVE_POLL */


/**
 * Run the MHD external event loop using select.
 *
 * @param daemon daemon to run it for
 */
static void
run_mhd_loop (struct MHD_Daemon *daemon,
              unsigned int flags)
{
  if (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL)))
    run_mhd_select_loop (daemon);
#ifdef HAVE_POLL
  else if (0 != (flags & MHD_USE_POLL))
    run_mhd_poll_loop (daemon);
#endif /* HAVE_POLL */
#ifdef EPOLL_SUPPORT
  else if (0 != (flags & MHD_USE_EPOLL))
    run_mhd_select_loop (daemon);
#endif
  else
    externalErrorExitDesc ("Wrong 'flags' value");
}


/**
 * Test upgrading a connection.
 *
 * @param flags which event loop style should be tested
 * @param pool size of the thread pool, 0 to disable
 */
static unsigned int
test_upgrade (unsigned int flags,
              unsigned int pool)
{
  struct MHD_Daemon *d = NULL;
  struct wr_socket *sock;
  struct sockaddr_in sa;
  enum MHD_FLAG used_flags;
  const union MHD_DaemonInfo *dinfo;
#if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID)
  pid_t pid = -1;
#endif /* HTTPS_SUPPORT && HAVE_FORK && HAVE_WAITPID */
  size_t mem_limit;

  /* Handle memory limits. Actually makes sense only for TLS */
  if (use_vlarge)
    mem_limit = 64U * 1024U;  /* Half of the buffer should be large enough to take more than max TLS packet */
  else if (use_large)
    mem_limit = 4U * 1024;    /* Make sure that several iteration required to deliver a single message */
  else
    mem_limit = 0;            /* Use default value */

  client_done = false;
  app_done = false;

  if (! test_tls)
    d = MHD_start_daemon (flags | MHD_USE_ERROR_LOG | MHD_ALLOW_UPGRADE
                          | MHD_USE_ITC,
                          global_port,
                          NULL, NULL,
                          &ahc_upgrade, NULL,
                          MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
                          MHD_OPTION_NOTIFY_COMPLETED, &notify_completed_cb,
                          NULL,
                          MHD_OPTION_NOTIFY_CONNECTION, &notify_connection_cb,
                          NULL,
                          MHD_OPTION_THREAD_POOL_SIZE, pool,
                          MHD_OPTION_CONNECTION_TIMEOUT, test_timeout,
                          MHD_OPTION_CONNECTION_MEMORY_LIMIT, mem_limit,
                          MHD_OPTION_END);
#ifdef HTTPS_SUPPORT
  else
    d = MHD_start_daemon (flags | MHD_USE_ERROR_LOG | MHD_ALLOW_UPGRADE
                          | MHD_USE_TLS | MHD_USE_ITC,
                          global_port,
                          NULL, NULL,
                          &ahc_upgrade, NULL,
                          MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
                          MHD_OPTION_NOTIFY_COMPLETED, &notify_completed_cb,
                          NULL,
                          MHD_OPTION_NOTIFY_CONNECTION, &notify_connection_cb,
                          NULL,
                          MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem,
                          MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem,
                          MHD_OPTION_THREAD_POOL_SIZE, pool,
                          MHD_OPTION_CONNECTION_TIMEOUT, test_timeout,
                          MHD_OPTION_CONNECTION_MEMORY_LIMIT, mem_limit,
                          MHD_OPTION_END);
#endif /* HTTPS_SUPPORT */
  if (NULL == d)
    mhdErrorExitDesc ("MHD_start_daemon() failed");
  dinfo = MHD_get_daemon_info (d,
                               MHD_DAEMON_INFO_FLAGS);
  if (NULL == dinfo)
    mhdErrorExitDesc ("MHD_get_daemon_info() failed");
  used_flags = dinfo->flags;
  dinfo = MHD_get_daemon_info (d,
                               MHD_DAEMON_INFO_BIND_PORT);
  if ( (NULL == dinfo) ||
       (0 == dinfo->port) )
    mhdErrorExitDesc ("MHD_get_daemon_info() failed");
  global_port = dinfo->port; /* Re-use the same port for the next checks */
  if (! test_tls || (TLS_LIB_GNUTLS == use_tls_tool))
  {
    sock = test_tls ? wr_create_tls_sckt () : wr_create_plain_sckt ();
    if (NULL == sock)
      externalErrorExitDesc ("Create socket failed");
    wr_make_nonblocking (sock);
    sa.sin_family = AF_INET;
    sa.sin_port = htons (dinfo->port);
    sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
    if (0 != wr_connect (sock,
                         (struct sockaddr *) &sa,
                         sizeof (sa)))
      externalErrorExitDesc ("Connect socket failed");
  }
  else
  {
#if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID)
    MHD_socket tls_fork_sock;
    uint16_t port;

    port = dinfo->port;
    if (-1 == (pid = gnutlscli_connect (&tls_fork_sock,
                                        port)))
      externalErrorExitDesc ("gnutlscli_connect() failed");

    sock =  wr_create_from_plain_sckt (tls_fork_sock);
    if (NULL == sock)
      externalErrorExitDesc ("wr_create_from_plain_sckt() failed");

    wr_make_nonblocking (sock);
#else  /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */
    externalErrorExitDesc ("Unsupported 'use_tls_tool' value");
#endif /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */
  }

  if (0 != pthread_create (&pt_client,
                           NULL,
                           &run_usock_client,
                           sock))
    externalErrorExitDesc ("pthread_create() failed");
  if (0 == (flags & MHD_USE_INTERNAL_POLLING_THREAD) )
    run_mhd_loop (d, used_flags);
  if (0 != pthread_join (pt_client,
                         NULL))
    externalErrorExitDesc ("pthread_join() failed");
  if (0 != pthread_join (pt,
                         NULL))
    externalErrorExitDesc ("pthread_join() failed");
#if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID)
  if (test_tls && (TLS_LIB_GNUTLS != use_tls_tool))
  {
    if ((pid_t) -1 == waitpid (pid, NULL, 0))
      externalErrorExitDesc ("waitpid() failed");
  }
#endif /* HTTPS_SUPPORT && HAVE_FORK && HAVE_WAITPID */
  if (! client_done)
    externalErrorExitDesc ("The client thread has not signalled " \
                           "successful finish");
  if (! app_done)
    externalErrorExitDesc ("The application thread has not signalled " \
                           "successful finish");
  MHD_stop_daemon (d);
  return 0;
}


enum test_msg_type
{
  test_msg_large_app_data,
  test_msg_large_rclient_data,
  test_msg_vlarge_app_data,
  test_msg_vlarge_rclient_data
};

/**
 * Initialise test message data
 * @param buf the pointer to the buffer to fill with the test data
 * @param buf_size the size of the @a buf
 * @param msg_type the type of the data to fill the @a buf
 * @return the @a buf pointer
 */
static void *
init_test_msg (void *buf, size_t buf_size, enum test_msg_type msg_type)
{
  size_t i;
  char *const text_buf = (char *) buf;
  uint8_t *const bin_buf = (uint8_t *) buf;
  if (0 == buf_size)
    return buf;
  switch (msg_type)
  {
  case test_msg_large_app_data:
  case test_msg_large_rclient_data:
    /* Simulate text data */
    for (i = 0; i < buf_size; ++i)
    {
      size_t pos;
      if (test_msg_large_app_data == msg_type)
        pos = i + 43;
      else
        pos = i + 26;
      if ((0 == i) || (2 == pos % 100) )
        text_buf[i] =
          (char) (unsigned char) ((test_msg_large_app_data == msg_type) ?
                                  ('Z' - pos % ('Z' - 'A' + 1)) :
                                  ('A' + pos % ('Z' - 'A' + 1)));
      else if (0 == pos % 100)
        text_buf[i] = '.';
      else if (1 == pos % 100)
        text_buf[i] = ' ';
      else if ((99 != pos % 100) && (2 != pos % 100) && (0 == pos % 5))
        text_buf[i] = ' ';
      else if (test_msg_large_app_data == msg_type)
        text_buf[i] = (char) (unsigned char) ('z' - pos % ('z' - 'a' + 1));
      else
        text_buf[i] = (char) (unsigned char) ('a' + pos % ('z' - 'a' + 1));
    }
    break;
  case test_msg_vlarge_app_data:
    /* Simulate binary data */
    for (i = 0; i < buf_size; ++i)
    {
      bin_buf[i] = (uint8_t) ((i + 182) & 0xFF);
    }
    break;
  case test_msg_vlarge_rclient_data:
    /* Simulate binary data */
    for (i = 0; i < buf_size; ++i)
    {
      bin_buf[i] = (uint8_t) ((111 - i) & 0xFF);
    }
    break;
  default:
    exit (99);
    break;
  }
  return buf;
}


/**
 * Perform initialisation of variables used in all check in this test
 * @return true if succeed,
 *         false if failed.
 */
static bool
global_test_init (void)
{
  if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    global_port = 0;
  else
  {
    global_port = 1090;
    if (test_tls)
      global_port += 1U << 0;
    if (use_large)
      global_port += 1U << 1;
    else if (use_vlarge)
      global_port += 1U << 2;
  }
  if (use_large || use_vlarge)
  {
    unsigned int i;
    size_t alloc_size;
    alloc_size =  use_vlarge ? (64U * 1024U) : (17U * 1024U);
    for (i = 0; i < (sizeof(alloc_ptr) / sizeof(alloc_ptr[0])); ++i)
    {
      alloc_ptr[i] = malloc (alloc_size);
      if (NULL == alloc_ptr[i])
      {
        for (--i; i < (sizeof(alloc_ptr) / sizeof(alloc_ptr[0])); --i)
        {
          free (alloc_ptr[i]);
        }
        return false;
      }
    }

    rclient_msg_size = alloc_size;
    rclient_msg = init_test_msg (alloc_ptr[0], rclient_msg_size,
                                 use_vlarge ?
                                 test_msg_vlarge_rclient_data :
                                 test_msg_large_rclient_data);
    app_msg_size = alloc_size;
    app_msg = init_test_msg (alloc_ptr[1], app_msg_size,
                             use_vlarge ?
                             test_msg_vlarge_app_data :
                             test_msg_large_app_data);
  }
  else
  {
    unsigned int i;
    for (i = 0; i < (sizeof(alloc_ptr) / sizeof(alloc_ptr[0])); ++i)
      alloc_ptr[i] = NULL;

    rclient_msg_size = MHD_STATICSTR_LEN_ ("Hello");
    rclient_msg = "Hello";
    app_msg_size = MHD_STATICSTR_LEN_ ("World");;
    app_msg = "World";
  }
  return true;
}


/**
 * Perform de-initialisation of variables with memory de-allocation if required.
 */
static void
global_test_deinit (void)
{
  unsigned int i;
  for (i = ((sizeof(alloc_ptr) / sizeof(alloc_ptr[0])) - 1);
       i < (sizeof(alloc_ptr) / sizeof(alloc_ptr[0]));
       --i)
  {
    if (NULL != alloc_ptr[i])
      free (alloc_ptr[i]);
  }
}


int
main (int argc,
      char *const *argv)
{
  unsigned int error_count = 0;
  unsigned int res;

  use_vlarge = (0 != has_in_name (argv[0], "_vlarge"));
  use_large = (! use_vlarge) && (0 != has_in_name (argv[0], "_large"));

  use_tls_tool = TLS_CLI_NO_TOOL;
  test_tls = has_in_name (argv[0], "_tls");

  verbose = ! (has_param (argc, argv, "-q") ||
               has_param (argc, argv, "--quiet") ||
               has_param (argc, argv, "-s") ||
               has_param (argc, argv, "--silent"));

  if ((((int) ((~((unsigned int) 0U)) >> 1)) / 1000) < test_timeout)
  {
    fprintf (stderr, "The test timeout value (%d) is too large.\n"
             "The test cannot run.\n", test_timeout);
    fprintf (stderr, "The maximum allowed timeout value is %d.\n",
             (((int) ((~((unsigned int) 0U)) >> 1)) / 1000));
    return 3;
  }

  if (test_tls)
  {
    use_tls_tool = TLS_LIB_GNUTLS;   /* Should be always available as MHD uses it. */
#ifdef HTTPS_SUPPORT
    if (has_param (argc, argv, "--use-gnutls-cli"))
      use_tls_tool = TLS_CLI_GNUTLS;
    else if (has_param (argc, argv, "--use-openssl"))
      use_tls_tool = TLS_CLI_OPENSSL;
    else if (has_param (argc, argv, "--use-gnutls-lib"))
      use_tls_tool = TLS_LIB_GNUTLS;
#if defined(HAVE_FORK) && defined(HAVE_WAITPID)
    else if (0 == system ("gnutls-cli --version 1> /dev/null 2> /dev/null"))
      use_tls_tool = TLS_CLI_GNUTLS;
    else if (0 == system ("openssl version 1> /dev/null 2> /dev/null"))
      use_tls_tool = TLS_CLI_OPENSSL;
#endif /* HAVE_FORK && HAVE_WAITPID */
    if (verbose)
    {
      switch (use_tls_tool)
      {
      case TLS_CLI_GNUTLS:
        printf ("GnuTLS-CLI will be used for testing.\n");
        break;
      case TLS_CLI_OPENSSL:
        printf ("Command line version of OpenSSL will be used for testing.\n");
        break;
      case TLS_LIB_GNUTLS:
        printf ("GnuTLS library will be used for testing.\n");
        break;
      case TLS_CLI_NO_TOOL:
      default:
        externalErrorExitDesc ("Wrong 'use_tls_tool' value");
      }
    }
    if ( (TLS_LIB_GNUTLS == use_tls_tool) &&
         (GNUTLS_E_SUCCESS != gnutls_global_init ()) )
      externalErrorExitDesc ("gnutls_global_init() failed");

#else  /* ! HTTPS_SUPPORT */
    fprintf (stderr, "HTTPS support was disabled by configure.\n");
    return 77;
#endif /* ! HTTPS_SUPPORT */
  }

  if (! global_test_init ())
  {
#ifdef HTTPS_SUPPORT
    if (test_tls && (TLS_LIB_GNUTLS == use_tls_tool))
      gnutls_global_deinit ();
#endif /* HTTPS_SUPPORT */
    fprintf (stderr, "Failed to initialise the test.\n");
    return 99;
  }

  /* run tests */
  if (verbose)
    printf ("Starting HTTP \"Upgrade\" tests with %s connections.\n",
            test_tls ? "TLS" : "plain");
  /* try external select */
  res = test_upgrade (0,
                      0);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with external select, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with external select.\n");

  /* Try external auto */
  res = test_upgrade (MHD_USE_AUTO,
                      0);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with external 'auto', return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with external 'auto'.\n");

#ifdef EPOLL_SUPPORT
  res = test_upgrade (MHD_USE_EPOLL,
                      0);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with external select with EPOLL, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with external select with EPOLL.\n");
#endif

  /* Test thread-per-connection */
  res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD
                      | MHD_USE_THREAD_PER_CONNECTION,
                      0);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with thread per connection, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with thread per connection.\n");

  res = test_upgrade (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD
                      | MHD_USE_THREAD_PER_CONNECTION,
                      0);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with thread per connection and 'auto', return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with thread per connection and 'auto'.\n");
#ifdef HAVE_POLL
  res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD
                      | MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL,
                      0);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with thread per connection and poll, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with thread per connection and poll.\n");
#endif /* HAVE_POLL */

  /* Test different event loops, with and without thread pool */
  res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD,
                      0);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with internal select, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with internal select.\n");
  res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD,
                      2);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with internal select with thread pool, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with internal select with thread pool.\n");
  res = test_upgrade (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
                      0);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with internal 'auto' return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with internal 'auto'.\n");
  res = test_upgrade (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
                      2);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with internal 'auto' with thread pool, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with internal 'auto' with thread pool.\n");
#ifdef HAVE_POLL
  res = test_upgrade (MHD_USE_POLL_INTERNAL_THREAD,
                      0);
  fflush_allstd ();
  error_count += res;
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with internal poll, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with internal poll.\n");
  res = test_upgrade (MHD_USE_POLL_INTERNAL_THREAD,
                      2);
  fflush_allstd ();
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with internal poll with thread pool, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with internal poll with thread pool.\n");
#endif
#ifdef EPOLL_SUPPORT
  res = test_upgrade (MHD_USE_EPOLL_INTERNAL_THREAD,
                      0);
  fflush_allstd ();
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with internal epoll, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with internal epoll.\n");
  res = test_upgrade (MHD_USE_EPOLL_INTERNAL_THREAD,
                      2);
  fflush_allstd ();
  if (res)
    fprintf (stderr,
             "FAILED: Upgrade with internal epoll, return code %u.\n",
             res);
  else if (verbose)
    printf ("PASSED: Upgrade with internal epoll.\n");
#endif
  /* report result */
  if (0 != error_count)
    fprintf (stderr,
             "Error (code: %u)\n",
             error_count);

  global_test_deinit ();
#ifdef HTTPS_SUPPORT
  if (test_tls && (TLS_LIB_GNUTLS == use_tls_tool))
    gnutls_global_deinit ();
#endif /* HTTPS_SUPPORT */

  return error_count != 0;       /* 0 == pass */
}