summaryrefslogtreecommitdiffstats
path: root/src/lib-http/test-http-payload.c
blob: 4e638618a41b4396a27510213fdc962b8fa3be11 (plain)
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
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "llist.h"
#include "path-util.h"
#include "hostpid.h"
#include "ioloop.h"
#include "istream.h"
#include "ostream.h"
#include "istream-crlf.h"
#include "iostream-temp.h"
#include "iostream-ssl.h"
#include "iostream-ssl-test.h"
#ifdef HAVE_OPENSSL
#include "iostream-openssl.h"
#endif
#include "connection.h"
#include "test-common.h"
#include "test-subprocess.h"
#include "http-url.h"
#include "http-request.h"
#include "http-server.h"
#include "http-client.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>

#define CLIENT_PROGRESS_TIMEOUT     30
#define SERVER_KILL_TIMEOUT_SECS    20

enum payload_handling {
	PAYLOAD_HANDLING_LOW_LEVEL,
	PAYLOAD_HANDLING_FORWARD,
	PAYLOAD_HANDLING_HANDLER,
};

static bool debug = FALSE;
static bool small_socket_buffers = FALSE;
static const char *failure = NULL;
static struct timeout *to_continue = NULL;
static bool files_finished = FALSE;
static bool running_continue = FALSE;

static struct test_settings {
	/* client */
	bool client_blocking;
	unsigned int max_pending;
	unsigned int client_ioloop_nesting;
	bool request_100_continue;
	unsigned int parallel_clients;
	bool parallel_clients_global;
	size_t read_client_partial;
	bool unknown_size;

	/* server */
	bool server_blocking;
	bool server_ostream;
	enum payload_handling server_payload_handling;
	size_t read_server_partial;
	bool server_cork;

	bool ssl;
} tset;

static struct ip_addr bind_ip;
static in_port_t bind_port = 0;
static int fd_listen = -1;
static struct ioloop *ioloop_nested = NULL;
static unsigned ioloop_nested_first = 0;
static unsigned ioloop_nested_last = 0;
static unsigned ioloop_nested_depth = 0;

static void main_deinit(void);

/*
 * Test settings
 */

static void test_init_defaults(void)
{
	i_zero(&tset);
	tset.max_pending = 200;
	tset.server_payload_handling = PAYLOAD_HANDLING_FORWARD;
	tset.parallel_clients = 1;
}

/*
 * Test files
 */
static const char unsafe_characters[] = "\"<>#%{}|\\^~[]` ;/?:@=&";

static ARRAY_TYPE(const_string) files;
static pool_t files_pool;

static void test_files_read_dir(const char *path)
{
	DIR *dirp;

	/* open the directory */
	if ((dirp = opendir(path)) == NULL) {
		if (errno == ENOENT || errno == EACCES)
			return;
		i_fatal("test files: "
			"failed to open directory %s: %m", path);
	}

	/* read entries */
	for (;;) {
		const char *file;
		struct dirent *dp;
		struct stat st;

		errno = 0;
		if ((dp = readdir(dirp)) == NULL)
			break;
		if (*dp->d_name == '.' ||
		    dp->d_name[strcspn(dp->d_name, unsafe_characters)] != '\0')
			continue;

		file = t_abspath_to(dp->d_name, path);
		if (stat(file, &st) == 0) {
			if (S_ISREG(st.st_mode)) {
				file += 2; /* skip "./" */
				file = p_strdup(files_pool, file);
				array_push_back(&files, &file);
			} else if (S_ISDIR(st.st_mode)) {
				test_files_read_dir(file);
			}
		}
	}

	if (errno != 0)
		i_fatal("test files: "
			"failed to read directory %s: %m", path);

	/* Close the directory */
	if (closedir(dirp) < 0)
		i_error("test files: "
			"failed to close directory %s: %m", path);
}

static void test_files_init(void)
{
	/* initialize file array */
	files_pool = pool_alloconly_create(
		MEMPOOL_GROWING"http_server_request", 4096);
	p_array_init(&files, files_pool, 512);

	/* obtain all filenames */
	test_files_read_dir(".");
}

static void test_files_deinit(void)
{
	pool_unref(&files_pool);
}

static struct istream *
test_file_open(const char *path, unsigned int *status_r, const char **reason_r)
	       ATTR_NULL(2, 3)
{
	int fd;

	if (status_r != NULL)
		*status_r = 200;
	if (reason_r != NULL)
		*reason_r = "OK";

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		if (debug)
			i_debug("test files: open(%s) failed: %m", path);

		switch (errno) {
		case EFAULT:
		case ENOENT:
			if (status_r != NULL)
				*status_r = 404;
			if (reason_r != NULL)
				*reason_r = "Not Found";
			break;
		case EISDIR:
		case EACCES:
			if (status_r != NULL)
				*status_r = 403;
			if (reason_r != NULL)
				*reason_r = "Forbidden";
			break;
		default:
			if (status_r != NULL)
				*status_r = 500;
			if (reason_r != NULL)
				*reason_r = "Internal Server Error";
		}
		return NULL;
	}

	return i_stream_create_fd_autoclose(&fd, 40960);
}

/*
 * Test server
 */

struct client {
	pool_t pool;
	struct client *prev, *next;

	struct http_server_connection *http_conn;
};

struct client_request {
	struct client *client;
	struct http_server_request *server_req;

	const char *path;

	struct istream *data;
	struct istream *payload_input;
	struct ostream *payload_output;
	struct io *io;

	bool all_sent:1;
};

static const struct http_server_callbacks http_callbacks;
static struct http_server *http_server;

static struct io *io_listen;
static struct client *clients;

/* location: /succes */

static void client_handle_success_request(struct client_request *creq)
{
	struct http_server_request *req = creq->server_req;
	const struct http_request *hreq = http_server_request_get(req);
	struct http_server_response *resp;

	if (strcmp(hreq->method, "GET") != 0) {
		http_server_request_fail(req,
			405, "Method Not Allowed");
		return;
	}

	resp = http_server_response_create(req, 200, "OK");
	http_server_response_submit(resp);
}

/* location: /download/... */

static void
client_handle_download_request(struct client_request *creq,
			       const char *path)
{
	struct http_server_request *req = creq->server_req;
	const struct http_request *hreq = http_server_request_get(req);
	struct http_server_response *resp;
	const char *fpath, *reason;
	struct istream *fstream;
	struct ostream *output;
	unsigned int status;
	int ret;

	if (strcmp(hreq->method, "GET") != 0) {
		http_server_request_fail(req,
			405, "Method Not Allowed");
		return;
	}

	fpath = t_strconcat(".", path, NULL);

	if (debug) {
		i_debug("test server: download: "
			"sending payload for %s", fpath);
	}

	fstream = test_file_open(fpath, &status, &reason);
	if (fstream == NULL) {
		http_server_request_fail(req, status, reason);
		return;
	}

	resp = http_server_response_create(req, 200, "OK");
	http_server_response_add_header(resp, "Content-Type", "text/plain");

	if (tset.server_blocking) {
		output = http_server_response_get_payload_output(
			resp, IO_BLOCK_SIZE, TRUE);

		switch (o_stream_send_istream(output, fstream)) {
		case OSTREAM_SEND_ISTREAM_RESULT_WAIT_INPUT:
		case OSTREAM_SEND_ISTREAM_RESULT_WAIT_OUTPUT:
			i_unreached();
		case OSTREAM_SEND_ISTREAM_RESULT_FINISHED:
			/* finish it */
			ret = o_stream_finish(output);
			i_assert(ret != 0);
			if (ret > 0)
				break;
			/* fall through */
		case OSTREAM_SEND_ISTREAM_RESULT_ERROR_OUTPUT:
			i_assert(output->stream_errno != 0);
			i_fatal("test server: download: "
				"write(%s) failed: %s",
				o_stream_get_name(output),
				o_stream_get_error(output));
		case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
			i_assert(fstream->stream_errno != 0);
			i_fatal("test server: download: "
				"read(%s) failed: %s",
				i_stream_get_name(fstream),
				i_stream_get_error(fstream));
		}

		if (debug) {
			i_debug("test server: download: "
				"finished sending blocking payload for %s"
				"(%"PRIuUOFF_T":%"PRIuUOFF_T")",
				fpath, fstream->v_offset, output->offset);
		}

		o_stream_destroy(&output);
	} else {
		http_server_response_set_payload(resp, fstream);
		http_server_response_submit(resp);
	}
	i_stream_unref(&fstream);
}

/* location: /echo */

static int client_request_echo_send_more(struct client_request *creq)
{
	struct ostream *output = creq->payload_output;
	enum ostream_send_istream_result res;
	uoff_t offset;
	int ret;

	if ((ret = o_stream_flush(output)) <= 0) {
		if (ret < 0) {
			i_fatal("test server: echo: "
				"write(%s) failed for %s (flush): %s",
				o_stream_get_name(output), creq->path,
				o_stream_get_error(output));
		}
		return ret;
	}

	if (creq->all_sent) {
		if (debug) {
			i_debug("test server: echo: "
				"flushed all payload for %s", creq->path);
		}
		i_stream_unref(&creq->data);
		o_stream_destroy(&creq->payload_output);
		return 1;
	}

	i_assert(output != NULL);
	i_assert(creq->data != NULL);

	offset = creq->data->v_offset;
	o_stream_set_max_buffer_size(output, IO_BLOCK_SIZE);
	res = o_stream_send_istream(output, creq->data);
	o_stream_set_max_buffer_size(output, SIZE_MAX);

	i_assert(creq->data->v_offset >= offset);
	if (debug) {
		i_debug("test server: echo: sent data for %s "
			"(sent %"PRIuUOFF_T", buffered %zu)",
			creq->path, (uoff_t)(creq->data->v_offset - offset),
			o_stream_get_buffer_used_size(output));
	}

	switch (res) {
	case OSTREAM_SEND_ISTREAM_RESULT_FINISHED:
		/* finish it */
		creq->all_sent = TRUE;
		if ((ret = o_stream_finish(output)) < 0) {
			i_fatal("test server: echo: "
				"write(%s) failed for %s (finish): %s",
				o_stream_get_name(output), creq->path,
				o_stream_get_error(output));
		}
		if (debug) {
			i_debug("test server: echo: "
				"finished sending payload for %s", creq->path);
		}
		if (ret == 0)
			return 0;
		if (debug) {
			i_debug("test server: echo: "
				"flushed all payload for %s", creq->path);
		}
		i_stream_unref(&creq->data);
		o_stream_destroy(&creq->payload_output);
		return 1;
	case OSTREAM_SEND_ISTREAM_RESULT_WAIT_INPUT:
		i_unreached();
	case OSTREAM_SEND_ISTREAM_RESULT_WAIT_OUTPUT:
		if (debug) {
			i_debug("test server echo: "
				"partially sent payload for %s", creq->path);
		}
		return 1;
	case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
		i_fatal("test server: echo: "
			"read(%s) failed for %s: %s",
			i_stream_get_name(creq->data), creq->path,
			i_stream_get_error(creq->data));
	case OSTREAM_SEND_ISTREAM_RESULT_ERROR_OUTPUT:
		i_fatal("test server: echo: "
			"write(%s) failed for %s: %s",
			o_stream_get_name(output), creq->path,
			o_stream_get_error(output));
	}
	i_unreached();
}

static void
client_request_echo_ostream_nonblocking(struct client_request *creq,
					struct http_server_response *resp,
					struct istream *data)
{
	creq->data = data;
	i_stream_ref(data);

	creq->payload_output = http_server_response_get_payload_output(
		resp, IO_BLOCK_SIZE, FALSE);
	if (tset.server_cork)
		o_stream_cork(creq->payload_output);
	o_stream_set_flush_callback(creq->payload_output,
				    client_request_echo_send_more, creq);
	o_stream_set_flush_pending(creq->payload_output, TRUE);
}

static void
client_request_echo_blocking(struct client_request *creq,
			     struct http_server_response *resp,
			     struct istream *input)
{
	const unsigned char *data;
	size_t size;
	int ret;

	while ((ret = i_stream_read_more(input, &data, &size)) > 0) {
		ret = http_server_response_send_payload(&resp, data, size);
		i_assert(ret <= 0);
		if (ret < 0)
			break;
		i_stream_skip(input, size);
	}
	i_assert(ret < 0);
	if (input->stream_errno != 0) {
		i_fatal("test server: echo: "
			"read(%s) failed for %s: %s",
			i_stream_get_name(input), creq->path,
			i_stream_get_error(input));
	} else if (i_stream_have_bytes_left(input)) {
		i_fatal("test server: echo: "
			"failed to send all blocking payload for %s",
			creq->path);
	}

	/* finish it */
	if (http_server_response_finish_payload(&resp) < 0) {
		i_fatal("test server: echo: "
			"failed to finish blocking payload for %s", creq->path);
	}

	if (debug) {
		i_debug("test server: echo: "
			"sent all payload for %s", creq->path);
	}
}

static void
client_request_echo_ostream_blocking(struct client_request *creq,
				     struct http_server_response *resp,
				     struct istream *input)
{
	struct ostream *payload_output;
	int ret;

	payload_output = http_server_response_get_payload_output(
		resp, IO_BLOCK_SIZE, TRUE);

	if (tset.server_cork)
		o_stream_cork(payload_output);

	switch (o_stream_send_istream(payload_output, input)) {
	case OSTREAM_SEND_ISTREAM_RESULT_WAIT_INPUT:
	case OSTREAM_SEND_ISTREAM_RESULT_WAIT_OUTPUT:
		i_unreached();
	case OSTREAM_SEND_ISTREAM_RESULT_FINISHED:
		/* finish it */
		ret = o_stream_finish(payload_output);
		i_assert(ret != 0);
		if (ret > 0)
			break;
		/* fall through */
	case OSTREAM_SEND_ISTREAM_RESULT_ERROR_OUTPUT:
		i_assert(payload_output->stream_errno != 0);
		i_fatal("test server: echo: "
			"write(%s) failed for %s: %s",
			o_stream_get_name(payload_output), creq->path,
			o_stream_get_error(payload_output));
	case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
		i_assert(input->stream_errno != 0);
		i_fatal("test server: echo: "
			"read(%s) failed for %s: %s",
			i_stream_get_name(input), creq->path,
			i_stream_get_error(input));
	}

	if (debug) {
		i_debug("test server: echo: "
			"sent all payload for %s", creq->path);
	}

	o_stream_destroy(&payload_output);
}

static void client_request_finish_payload_in(struct client_request *creq)
{
	struct http_server_response *resp;
	struct istream *payload_input;

	payload_input =
		iostream_temp_finish(&creq->payload_output, 4096);

	if (debug) {
		i_debug("test server: echo: "
			"finished receiving payload for %s", creq->path);
	}

	resp = http_server_response_create(creq->server_req, 200, "OK");
	http_server_response_add_header(resp, "Content-Type", "text/plain");

	if (tset.server_ostream) {
		client_request_echo_ostream_nonblocking(creq, resp,
							payload_input);
	} else {
		http_server_response_set_payload(resp, payload_input);
		http_server_response_submit(resp);
	}

	i_stream_unref(&payload_input);
}

static void client_request_read_echo(struct client_request *creq)
{
	enum ostream_send_istream_result res;

	o_stream_set_max_buffer_size(creq->payload_output, IO_BLOCK_SIZE);
	res = o_stream_send_istream(creq->payload_output, creq->payload_input);
	o_stream_set_max_buffer_size(creq->payload_output, SIZE_MAX);

	switch (res) {
	case OSTREAM_SEND_ISTREAM_RESULT_FINISHED:
		break;
	case OSTREAM_SEND_ISTREAM_RESULT_WAIT_INPUT:
	case OSTREAM_SEND_ISTREAM_RESULT_WAIT_OUTPUT:
		return;
	case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
		i_fatal("test server: echo: "
			"Failed to read all echo payload [%s]",
			creq->path);
	case OSTREAM_SEND_ISTREAM_RESULT_ERROR_OUTPUT:
		i_fatal("test server: echo: "
			"Failed to write all echo payload [%s]",
			creq->path);
	}

	client_request_finish_payload_in(creq);
	i_stream_unref(&creq->payload_input);
}

static void client_request_read_echo_more(struct client_request *creq)
{
	client_request_read_echo(creq);

	if (creq->payload_input != NULL)
		return;

	io_remove(&creq->io);

	if (debug) {
		i_debug("test server: echo: "
			"finished receiving payload for %s",
			creq->path);
	}
}

static void
client_handle_echo_request(struct client_request *creq,
			   const char *path)
{
	struct http_server_request *req = creq->server_req;
	const struct http_request *hreq = http_server_request_get(req);
	struct http_server_response *resp;
	struct ostream *payload_output;
	uoff_t size;

	creq->path = p_strdup(http_server_request_get_pool(req), path);

	if (strcmp(hreq->method, "PUT") != 0) {
		http_server_request_fail(req,
			405, "Method Not Allowed");
		return;
	}

	size = 0;
	if (http_request_get_payload_size(hreq, &size) > 0 && size == 0) {
		if (debug) {
			i_debug("test server: echo: "
				"empty payload for %s", creq->path);
		}

		resp = http_server_response_create(creq->server_req, 200, "OK");
		http_server_response_add_header(
			resp, "Content-Type", "text/plain");
		http_server_response_submit(resp);
		return;
	}

	payload_output = iostream_temp_create("/tmp/test-http-server", 0);

	if (tset.server_blocking) {
		struct istream *payload_input;

		payload_input =
			http_server_request_get_payload_input(req, TRUE);

		if (tset.read_server_partial > 0) {
			struct istream *partial =
				i_stream_create_limit(payload_input,
						      tset.read_server_partial);
			i_stream_unref(&payload_input);
			payload_input = partial;
		}

		if (o_stream_send_istream(payload_output, payload_input) !=
			OSTREAM_SEND_ISTREAM_RESULT_FINISHED) {
			i_fatal("test server: echo: "
				"failed to receive blocking echo payload");
		}
		i_stream_unref(&payload_input);

		payload_input = iostream_temp_finish(&payload_output, 4096);

		if (debug) {
			i_debug("test server: echo: "
				"finished receiving blocking payload for %s",
				path);
		}

		resp = http_server_response_create(req, 200, "OK");
		http_server_response_add_header(resp,
			"Content-Type", "text/plain");

		if (tset.server_ostream) {
			client_request_echo_ostream_blocking(creq, resp,
							     payload_input);
		} else {
			client_request_echo_blocking(creq, resp, payload_input);
		}
		i_stream_unref(&payload_input);
	} else {
		creq->payload_output = payload_output;

		switch (tset.server_payload_handling) {
		case PAYLOAD_HANDLING_LOW_LEVEL:
			creq->payload_input =
				http_server_request_get_payload_input(req, FALSE);

			if (tset.read_server_partial > 0) {
				struct istream *partial =
					i_stream_create_limit(creq->payload_input,
							      tset.read_server_partial);
				i_stream_unref(&creq->payload_input);
				creq->payload_input = partial;
			}

			creq->io = io_add_istream(creq->payload_input,
					 client_request_read_echo_more, creq);
			client_request_read_echo_more(creq);
			break;
		case PAYLOAD_HANDLING_FORWARD:
			http_server_request_forward_payload(req,
				payload_output, SIZE_MAX,
				client_request_finish_payload_in, creq);
			break;
		case PAYLOAD_HANDLING_HANDLER:
			creq->payload_input =
				http_server_request_get_payload_input(req, FALSE);
			http_server_request_handle_payload(req,
				client_request_read_echo, creq);
			break;
		}
	}
}

/* request */

static void http_server_request_destroyed(struct client_request *creq);

static struct client_request *
client_request_init(struct client *client,
		    struct http_server_request *req)
{
	struct client_request *creq;
	pool_t pool = http_server_request_get_pool(req);

	http_server_request_ref(req);

	creq = p_new(pool, struct client_request, 1);
	creq->client = client;
	creq->server_req = req;

	http_server_request_set_destroy_callback(req,
		http_server_request_destroyed, creq);

	return creq;
}

static void client_request_deinit(struct client_request **_creq)
{
	struct client_request *creq = *_creq;
	struct http_server_request *req = creq->server_req;

	*_creq = NULL;

	i_stream_unref(&creq->data);
	i_stream_unref(&creq->payload_input);
	io_remove(&creq->io);

	http_server_request_unref(&req);
}

static void http_server_request_destroyed(struct client_request *creq)
{
	client_request_deinit(&creq);
}

static void
client_handle_request(void *context,
		      struct http_server_request *req)
{
	const struct http_request *hreq = http_server_request_get(req);
	const char *path = hreq->target.url->path, *p;
	struct client *client = (struct client *)context;
	struct client_request *creq;

	if (debug) {
		i_debug("test server: request method=`%s' path=`%s'",
			hreq->method, path);
	}

	creq = client_request_init(client, req);

	if (strcmp(path, "/success") == 0) {
		client_handle_success_request(creq);
		return;
	}

	if ((p = strchr(path+1, '/')) == NULL) {
		http_server_request_fail(req, 404, "Not found");
		return;
	}
	if (strncmp(path, "/download", p-path) == 0) {
		client_handle_download_request(creq, p);
		return;
	}
	if (strncmp(path, "/echo", p-path) == 0) {
		client_handle_echo_request(creq, p);
		return;
	}

	http_server_request_fail(req, 404, "Not found");
	return;
}

/* client connection */

static void client_connection_destroy(void *context, const char *reason);

static const struct http_server_callbacks http_callbacks = {
	.connection_destroy = client_connection_destroy,
	.handle_request = client_handle_request,
};

static void client_init(int fd)
{
	struct client *client;
	pool_t pool;

	net_set_nonblock(fd, TRUE);

	pool = pool_alloconly_create("client", 512);
	client = p_new(pool, struct client, 1);
	client->pool = pool;

	client->http_conn = http_server_connection_create(
		http_server, fd, fd, tset.ssl, &http_callbacks, client);
	DLLIST_PREPEND(&clients, client);
}

static void client_deinit(struct client **_client)
{
	struct client *client = *_client;

	*_client = NULL;

	DLLIST_REMOVE(&clients, client);

	if (client->http_conn != NULL) {
		http_server_connection_close(&client->http_conn,
					     "deinit");
	}
	pool_unref(&client->pool);
}

static void
client_connection_destroy(void *context, const char *reason ATTR_UNUSED)
{
	struct client *client = context;

	client->http_conn = NULL;
	client_deinit(&client);
}

static void client_accept(void *context ATTR_UNUSED)
{
	int fd;

	for (;;) {
		/* accept new client */
		if ((fd = net_accept(fd_listen, NULL, NULL)) < 0) {
			if (errno == EAGAIN)
				break;
			if (errno == ECONNABORTED)
				continue;
			i_fatal("test server: accept() failed: %m");
		}

		client_init(fd);
	}
}

/* */

static void test_server_init(const struct http_server_settings *server_set)
{
	/* open server socket */
	io_listen = io_add(fd_listen, IO_READ, client_accept, NULL);

	http_server = http_server_init(server_set);
}

static void test_server_deinit(void)
{
	/* close server socket */
	io_remove(&io_listen);

	/* deinitialize */
	http_server_deinit(&http_server);
}

/*
 * Test client
 */

struct test_client_request {
	int refcount;

	struct test_client_request *prev, *next;
	struct http_client *client;
	struct http_client_request *hreq;

	struct io *io;
	struct istream *payload;
	struct istream *file_in, *file_out;
	unsigned int files_idx;
};

static struct http_client **http_clients;
static struct test_client_request *client_requests;
static unsigned int client_files_first, client_files_last;
struct timeout *to_client_progress = NULL;

static struct test_client_request *
test_client_request_new(struct http_client *client)
{
	struct test_client_request *tcreq;

	tcreq = i_new(struct test_client_request, 1);
	tcreq->refcount = 1;
	tcreq->client = client;
	DLLIST_PREPEND(&client_requests, tcreq);

	return tcreq;
}

static void test_client_request_ref(struct test_client_request *tcreq)
{
	tcreq->refcount++;
}

static void test_client_request_unref(struct test_client_request **_tcreq)
{
	struct test_client_request *tcreq = *_tcreq;

	*_tcreq = NULL;

	i_assert(tcreq->refcount > 0);
	if (--tcreq->refcount > 0)
		return;

	io_remove(&tcreq->io);
	i_stream_unref(&tcreq->payload);
	i_stream_unref(&tcreq->file_in);
	i_stream_unref(&tcreq->file_out);

	DLLIST_REMOVE(&client_requests, tcreq);
	i_free(tcreq);
}

static void test_client_request_destroy(struct test_client_request *tcreq)
{
	test_client_request_unref(&tcreq);
}

static void test_client_switch_ioloop(void)
{
	struct test_client_request *tcreq;

	if (to_continue != NULL)
		to_continue = io_loop_move_timeout(&to_continue);
	if (to_client_progress != NULL)
		to_client_progress = io_loop_move_timeout(&to_client_progress);

	for (tcreq = client_requests; tcreq != NULL;
		tcreq = tcreq->next) {
		if (tcreq->io != NULL)
			tcreq->io = io_loop_move_io(&tcreq->io);
		if (tcreq->payload != NULL)
			i_stream_switch_ioloop(tcreq->payload);
	}
}

static void test_client_progress_timeout(void *context ATTR_UNUSED)
{
	/* Terminate test due to lack of progress */
	failure = "Test is hanging";
	timeout_remove(&to_client_progress);
	io_loop_stop(current_ioloop);
}

static void
test_client_create_clients(const struct http_client_settings *client_set)
{
	struct http_client_context *http_context = NULL;
	unsigned int i;

	if (!small_socket_buffers) {
		to_client_progress = timeout_add(
			CLIENT_PROGRESS_TIMEOUT*1000,
			test_client_progress_timeout, NULL);
	}

	if (!tset.parallel_clients_global)
		http_context = http_client_context_create(client_set);

	if (tset.parallel_clients < 1)
		tset.parallel_clients = 1;
	http_clients = i_new(struct http_client *, tset.parallel_clients);
	for (i = 0; i < tset.parallel_clients; i++) {
		http_clients[i] = (tset.parallel_clients_global ?
				   http_client_init(client_set) :
				   http_client_init_shared(http_context, NULL));
	}

	if (!tset.parallel_clients_global)
		http_client_context_unref(&http_context);
}

/* download */

static void test_client_download_continue(void);

static void test_client_download_finished(struct test_client_request *tcreq)
{
	const char **paths;
	unsigned int files_idx = tcreq->files_idx;
	unsigned int count;

	paths = array_get_modifiable(&files, &count);
	i_assert(files_idx < count);
	i_assert(client_files_first < count);
	i_assert(paths[files_idx] != NULL);

	paths[files_idx] = NULL;
	test_client_download_continue();
}

static void
test_client_download_payload_input(struct test_client_request *tcreq)
{
	struct istream *payload = tcreq->payload;
	const unsigned char *pdata, *fdata;
	size_t psize, fsize, pleft;
	off_t ret;

	if (to_client_progress != NULL)
		timeout_reset(to_client_progress);

	/* read payload */
	while ((ret = i_stream_read_more(payload, &pdata, &psize)) > 0) {
		if (debug) {
			i_debug("test client: download: "
				"got data for [%u] (size=%d)",
				tcreq->files_idx, (int)psize);
		}
		/* compare with file on disk */
		pleft = psize;
		while ((ret = i_stream_read_more(tcreq->file_in,
						 &fdata, &fsize)) > 0 &&
		       pleft > 0) {
			fsize = (fsize > pleft ? pleft : fsize);
			if (memcmp(pdata, fdata, fsize) != 0) {
				i_fatal("test client: download: "
					"received data does not match file "
					"(%"PRIuUOFF_T":%"PRIuUOFF_T")",
					payload->v_offset,
					tcreq->file_in->v_offset);
			}
			i_stream_skip(tcreq->file_in, fsize);
			pleft -= fsize;
			pdata += fsize;
		}
		if (ret < 0 && tcreq->file_in->stream_errno != 0) {
			i_fatal("test client: download: "
				"failed to read file: %s",
				i_stream_get_error(tcreq->file_in));
		}
		i_stream_skip(payload, psize);
	}

	if (ret == 0) {
		if (debug) {
			i_debug("test client: download: "
				"need more data for [%u]",
				tcreq->files_idx);
		}
		/* we will be called again for this request */
	} else {
		(void)i_stream_read(tcreq->file_in);
		if (payload->stream_errno != 0) {
			i_fatal("test client: download: "
				"failed to read request payload: %s",
				i_stream_get_error(payload));
		} if (i_stream_have_bytes_left(tcreq->file_in)) {
			if (i_stream_read_more(tcreq->file_in,
					       &fdata, &fsize) <= 0)
				fsize = 0;
			i_fatal("test client: download: "
				"payload ended prematurely "
				"(at least %zu bytes left)", fsize);
		} else if (debug) {
			i_debug("test client: download: "
				"finished request for [%u]",
				tcreq->files_idx);
		}

		/* finished */
		tcreq->payload = NULL;
		test_client_download_finished(tcreq);

		/* dereference payload stream; finishes the request */
		i_stream_unref(&tcreq->file_in);
		io_remove(&tcreq->io); /* holds a reference too */
		i_stream_unref(&payload);
	}
}

static void
test_client_download_response(const struct http_response *resp,
			      struct test_client_request *tcreq)
{
	const char **paths;
	const char *path;
	unsigned int count, status;
	struct istream *fstream;
	const char *reason;

	if (debug) {
		i_debug("test client: download: got response for [%u]",
			tcreq->files_idx);
	}

	if (to_client_progress != NULL)
		timeout_reset(to_client_progress);

	paths = array_get_modifiable(&files, &count);
	i_assert(tcreq->files_idx < count);
	i_assert(client_files_first < count);
	path = paths[tcreq->files_idx];
	i_assert(path != NULL);

	if (debug) {
		i_debug("test client: download: path for [%u]: %s",
			tcreq->files_idx, path);
	}

	fstream = test_file_open(path, &status, &reason);
	i_assert(fstream != NULL);

	if (status != resp->status) {
		i_fatal("test client: download: "
			"got wrong response for %s: %u %s "
			"(expected: %u %s)", path,
			resp->status, resp->reason, status, reason);
	}

	if (resp->status / 100 != 2) {
		if (debug) {
			i_debug("test client: download: "
				"HTTP request for %s failed: %u %s",
				path, resp->status, resp->reason);
		}
		i_stream_unref(&fstream);
		test_client_download_finished(tcreq);
		return;
	}

	if (resp->payload == NULL) {
		if (debug) {
			i_debug("test client: download: "
				"no payload for %s [%u]",
				path, tcreq->files_idx);
		}
		i_stream_unref(&fstream);
		test_client_download_finished(tcreq);
		return;
	}

	i_assert(fstream != NULL);
	if (tset.read_client_partial == 0) {
		i_stream_ref(resp->payload);
		tcreq->payload = resp->payload;
		tcreq->file_in = fstream;
	} else {
		struct istream *payload = resp->payload;
		tcreq->payload = i_stream_create_limit(
			payload, tset.read_client_partial);
		tcreq->file_in = i_stream_create_limit(
			fstream, tset.read_client_partial);
		i_stream_unref(&fstream);
	}

	tcreq->io = io_add_istream(tcreq->payload,
		test_client_download_payload_input, tcreq);
	test_client_download_payload_input(tcreq);
}

static void test_client_download_continue(void)
{
	struct test_client_request *tcreq;
	struct http_client_request *hreq;
	const char *const *paths;
	unsigned int count;

	paths = array_get(&files, &count);
	i_assert(client_files_first <= count);
	i_assert(client_files_last <= count);

	i_assert(client_files_first <= client_files_last);
	for (; client_files_first < client_files_last &&
		paths[client_files_first] == NULL; client_files_first++)

	if (debug) {
		i_debug("test client: download: received until [%u]",
			client_files_first-1);
	}

	if (client_files_first >= count) {
		io_loop_stop(current_ioloop);
		return;
	}

	for (; (client_files_last < count &&
	        (client_files_last - client_files_first) < tset.max_pending);
	     client_files_last++) {
		struct http_client *http_client =
			http_clients[client_files_last % tset.parallel_clients];
		const char *path = paths[client_files_last];

		tcreq = test_client_request_new(http_client);
		tcreq->files_idx = client_files_last;

		if (debug) {
			i_debug("test client: download: retrieving %s [%u]",
				path, tcreq->files_idx);
		}
		hreq = tcreq->hreq = http_client_request(
			http_client, "GET", net_ip2addr(&bind_ip),
			t_strconcat("/download/", path, NULL),
			test_client_download_response, tcreq);
		http_client_request_set_port(hreq, bind_port);
		http_client_request_set_ssl(hreq, tset.ssl);
		http_client_request_set_destroy_callback(
			hreq, test_client_request_destroy, tcreq);
		http_client_request_submit(hreq);
	}
}

static void test_client_download(const struct http_client_settings *client_set)
{
	/* create client(s) */
	test_client_create_clients(client_set);

	/* start querying server */
	client_files_first = client_files_last = 0;
	test_client_download_continue();
}

/* echo */

static void test_client_echo_continue(void *context);

static void test_client_echo_finished(struct test_client_request *tcreq)
{
	unsigned int files_idx = tcreq->files_idx;
	const char **paths;
	unsigned int count;

	paths = array_get_modifiable(&files, &count);
	i_assert(files_idx < count);
	i_assert(client_files_first < count);
	i_assert(paths[files_idx] != NULL);

	if (tcreq->file_out != NULL)
		return;
	if (tcreq->file_in != NULL)
		return;

	if (debug) {
		i_debug("test client: echo: finished [%u]: %s",
			files_idx, paths[files_idx]);
	}

	paths[files_idx] = NULL;
	files_finished = TRUE;
	if (!running_continue && to_continue == NULL) {
		to_continue = timeout_add_short(0,
			test_client_echo_continue, NULL);
	}
}

static void test_client_echo_payload_input(struct test_client_request *tcreq)
{
	struct istream *payload = tcreq->payload;
	const unsigned char *pdata, *fdata;
	size_t psize, fsize, pleft;
	off_t ret;

	if (to_client_progress != NULL)
		timeout_reset(to_client_progress);

	/* read payload */
	while ((ret = i_stream_read_more(payload, &pdata, &psize)) > 0) {
		if (debug) {
			i_debug("test client: echo: "
				"got data for [%u] (size=%d)",
				tcreq->files_idx, (int)psize);
		}
		/* compare with file on disk */
		pleft = psize;
		while ((ret = i_stream_read_more(tcreq->file_in,
						 &fdata, &fsize)) > 0 &&
		       pleft > 0) {
			fsize = (fsize > pleft ? pleft : fsize);
			if (memcmp(pdata, fdata, fsize) != 0) {
				i_fatal("test client: echo: "
					"received data does not match file "
					"(%"PRIuUOFF_T":%"PRIuUOFF_T")",
					payload->v_offset,
					tcreq->file_in->v_offset);
			}
			i_stream_skip(tcreq->file_in, fsize);
			pleft -= fsize;
			pdata += fsize;
		}
		if (ret < 0 && tcreq->file_in->stream_errno != 0) {
			i_fatal("test client: echo: "
				"failed to read file: %s",
				i_stream_get_error(tcreq->file_in));
		}
		i_stream_skip(payload, psize);
	}

	if (ret == 0) {
		if (debug) {
			i_debug("test client: echo: "
				"need more data for [%u]",
				tcreq->files_idx);
		}
		/* we will be called again for this request */
	} else {
		(void)i_stream_read(tcreq->file_in);
		if (payload->stream_errno != 0) {
			i_fatal("test client: echo: "
				"failed to read request payload: %s",
				i_stream_get_error(payload));
		} if (i_stream_have_bytes_left(tcreq->file_in)) {
			if (i_stream_read_more(tcreq->file_in,
					       &fdata, &fsize) <= 0)
				fsize = 0;
			i_fatal("test client: echo: "
				"payload ended prematurely "
				"(at least %zu bytes left)", fsize);
		} else if (debug) {
			i_debug("test client: echo: "
				"finished request for [%u]",
				tcreq->files_idx);
		}

		/* finished */
		tcreq->payload = NULL;
		i_stream_unref(&tcreq->file_in);
		test_client_echo_finished(tcreq);

		/* dereference payload stream; finishes the request */
		io_remove(&tcreq->io); /* holds a reference too */
		i_stream_unref(&payload);
	}
}

static void
test_client_echo_response(const struct http_response *resp,
			  struct test_client_request *tcreq)
{
	const char **paths;
	const char *path;
	unsigned int count, status;
	struct istream *fstream;

	if (debug) {
		i_debug("test client: echo: got response for [%u]",
			tcreq->files_idx);
	}

	if (to_client_progress != NULL)
		timeout_reset(to_client_progress);

	paths = array_get_modifiable(&files, &count);
	i_assert(tcreq->files_idx < count);
	i_assert(client_files_first < count);
	path = paths[tcreq->files_idx];
	i_assert(path != NULL);

	if (debug) {
		i_debug("test client: echo: path for [%u]: %s",
			tcreq->files_idx, path);
	}

	if (resp->status / 100 != 2) {
		i_fatal("test client: echo: "
			"HTTP request for %s failed: %u %s",
			path, resp->status, resp->reason);
	}

	fstream = test_file_open(path, &status, NULL);
	if (fstream == NULL) {
		i_fatal("test client: echo: failed to open %s", path);
	}

	if (tset.unknown_size) {
		struct istream *ustream;

		ustream = i_stream_create_crlf(fstream);
		i_stream_unref(&fstream);
		fstream = ustream;
	}

	if (tset.read_server_partial > 0) {
		struct istream *partial =
			i_stream_create_limit(fstream, tset.read_server_partial);
		i_stream_unref(&fstream);
		fstream = partial;
	}

	if (resp->payload == NULL) {
		// FIXME: check file is empty
		if (debug) {
			i_debug("test client: echo: "
				"no payload for %s [%u]",
				path, tcreq->files_idx);
		}
		i_stream_unref(&fstream);
		test_client_echo_finished(tcreq);
		return;
	}

	i_assert(fstream != NULL);
	tcreq->file_in = fstream;

	i_stream_ref(resp->payload);
	tcreq->payload = resp->payload;
	tcreq->io = io_add_istream(resp->payload,
		test_client_echo_payload_input, tcreq);
	test_client_echo_payload_input(tcreq);
}

static void
test_client_echo_nonblocking(struct test_client_request *tcreq ATTR_UNUSED,
			     struct http_client_request *hreq,
			     struct istream *fstream)
{
	http_client_request_set_payload(hreq, fstream,
					tset.request_100_continue);
	http_client_request_submit(hreq);
}

static void
test_client_echo_blocking(struct test_client_request *tcreq,
			  struct http_client_request *hreq,
			  struct istream *fstream)
{
	const unsigned char *data;
	size_t size;
	int ret;

	test_client_request_ref(tcreq);
	tcreq->file_out = fstream;

	while ((ret = i_stream_read_more(fstream, &data, &size)) > 0) {
		ret = http_client_request_send_payload(&hreq, data, size);
		i_assert(ret <= 0);
		if (ret < 0)
			break;
		i_stream_skip(fstream, size);
	}
	i_assert(ret < 0);
	if (fstream->stream_errno != 0) {
		i_fatal("test client: echo: "
			"read(%s) failed: %s [%u]",
			i_stream_get_name(fstream),
			i_stream_get_error(fstream),
			tcreq->files_idx);
	} else if (i_stream_have_bytes_left(fstream)) {
		i_fatal("test client: echo: "
			"failed to send all blocking payload [%u]",
			tcreq->files_idx);
	}

	/* finish it */
	if (http_client_request_finish_payload(&hreq) < 0) {
		i_fatal("test client: echo: "
			"failed to finish blocking payload [%u]",
			tcreq->files_idx);
	}
	http_client_wait(tcreq->client);

	if (debug) {
		i_debug("test client: echo: "
			"sent all payload [%u]",
			tcreq->files_idx);
	}

	tcreq->file_out = NULL;
	test_client_echo_finished(tcreq);
	test_client_request_unref(&tcreq);
}

static void test_client_echo_continue(void *context ATTR_UNUSED)
{
	struct test_client_request *tcreq;
	struct http_client_request *hreq;
	const char **paths;
	unsigned int count, first_submitted;
	bool prev_files_finished = files_finished;

	running_continue = TRUE;
	files_finished = FALSE;
	timeout_remove(&to_continue);

	paths = array_get_modifiable(&files, &count);

	i_assert(client_files_first <= count);
	i_assert(client_files_last <= count);

	i_assert(client_files_first <= client_files_last);
	for (; client_files_first < client_files_last &&
		paths[client_files_first] == NULL; client_files_first++);

	if (debug) {
		i_debug("test client: echo: received until [%u/%u]",
			client_files_first-1, count);
	}

	if (debug && client_files_first < count) {
		const char *path = paths[client_files_first];
		i_debug("test client: echo: next blocking: %s [%d]",
			(path == NULL ? "none" : path), client_files_first);
	}

	if (client_files_first >= count || failure != NULL) {
		running_continue = FALSE;
		files_finished = prev_files_finished;
		io_loop_stop(current_ioloop);
		return;
	}

	first_submitted = client_files_last;
	for (; (client_files_last < count &&
	        (client_files_last - client_files_first) < tset.max_pending);
	     client_files_last++) {
		struct http_client *http_client =
			http_clients[client_files_last % tset.parallel_clients];
		struct istream *fstream;
		const char *path = paths[client_files_last];

		fstream = test_file_open(path, NULL, NULL);
		if (fstream == NULL) {
			paths[client_files_last] = NULL;
			if (debug) {
				i_debug("test client: echo: "
					"skipping %s [%u]",
					path, client_files_last);
			}
			continue;
		}

		if (debug) {
			i_debug("test client: echo: retrieving %s [%u]",
				path, client_files_last);
		}

		if (tset.unknown_size) {
			struct istream *ustream;

			ustream = i_stream_create_crlf(fstream);
			i_stream_unref(&fstream);
			fstream = ustream;
		}

		tcreq = test_client_request_new(http_client);
		tcreq->files_idx = client_files_last;

		hreq = tcreq->hreq = http_client_request(http_client,
			"PUT", net_ip2addr(&bind_ip),
			t_strconcat("/echo/", path, NULL),
			test_client_echo_response, tcreq);
		http_client_request_set_port(hreq, bind_port);
		http_client_request_set_ssl(hreq, tset.ssl);
		http_client_request_set_destroy_callback(hreq,
			test_client_request_destroy, tcreq);

		if (!tset.client_blocking)
			test_client_echo_nonblocking(tcreq, hreq, fstream);
		else
			test_client_echo_blocking(tcreq, hreq, fstream);
		i_stream_unref(&fstream);

		if (tset.client_blocking && paths[client_files_last] != NULL) {
			running_continue = FALSE;
			files_finished = prev_files_finished;
			return;
		}
	}

	if (files_finished && to_continue == NULL) {
		to_continue = timeout_add_short(
			0, test_client_echo_continue, NULL);
	}
	running_continue = FALSE;
	files_finished = prev_files_finished;

	/* run nested ioloop (if requested) if new requests cross a nesting
	   boundary */
	if (ioloop_nested != NULL) {
		unsigned int i;

		i_assert(ioloop_nested_first <= count);
		i_assert(ioloop_nested_last <= count);
		for (i = ioloop_nested_first; i < ioloop_nested_last; i++) {
			if (paths[i] != NULL) {
				if (debug) {
					i_debug("test client: "
						"not leaving ioloop [%u]", i);
				}
				break;
			}
		}

		if (i == ioloop_nested_last)
			io_loop_stop(ioloop_nested);
	} else if (tset.client_ioloop_nesting > 0 &&
		   ((client_files_last / tset.client_ioloop_nesting) !=
			(first_submitted / tset.client_ioloop_nesting))) {
		struct ioloop *prev_ioloop = current_ioloop;
		unsigned int i;

		ioloop_nested_first = first_submitted;
		ioloop_nested_last =
			first_submitted + tset.client_ioloop_nesting;
		if (ioloop_nested_last > client_files_last)
			ioloop_nested_last = client_files_last;

		if (debug) {
			i_debug("test client: "
				"echo: entering ioloop for %u...%u (depth=%u)",
				ioloop_nested_first, ioloop_nested_last,
				ioloop_nested_depth);
		}

		ioloop_nested_depth++;

		ioloop_nested = io_loop_create();
		for (i = 0; i < tset.parallel_clients; i++)
			http_client_switch_ioloop(http_clients[i]);
		test_client_switch_ioloop();

		io_loop_run(ioloop_nested);

		io_loop_set_current(prev_ioloop);
		for (i = 0; i < tset.parallel_clients; i++)
			http_client_switch_ioloop(http_clients[i]);
		test_client_switch_ioloop();
		io_loop_set_current(ioloop_nested);
		io_loop_destroy(&ioloop_nested);
		ioloop_nested = NULL;

		ioloop_nested_depth--;

		if (debug) {
			i_debug("test client: echo: leaving ioloop for %u...%u "
				"(depth=%u)", ioloop_nested_first,
				ioloop_nested_last, ioloop_nested_depth);
		}
		ioloop_nested_first = ioloop_nested_last = 0;

		if (client_files_first >= count || failure != NULL) {
			io_loop_stop(current_ioloop);
			return;
		}
	}
}

static void test_client_echo(const struct http_client_settings *client_set)
{
	/* create client */
	test_client_create_clients(client_set);

	/* start querying server */
	client_files_first = client_files_last = 0;

	i_assert(to_continue == NULL);
	to_continue = timeout_add_short(0, test_client_echo_continue, NULL);
}

/* cleanup */

static void test_client_deinit(void)
{
	unsigned int i;

	for (i = 0; i < tset.parallel_clients; i++)
		http_client_deinit(&http_clients[i]);
	i_free(http_clients);

	tset.parallel_clients = 1;

	timeout_remove(&to_continue);
	timeout_remove(&to_client_progress);
}

/*
 * Tests
 */

struct test_server_data {
	const struct http_server_settings *set;
};

static void test_open_server_fd(void)
{
	i_close_fd(&fd_listen);
	fd_listen = net_listen(&bind_ip, &bind_port, 128);
	if (fd_listen == -1) {
		i_fatal("listen(%s:%u) failed: %m",
			net_ip2addr(&bind_ip), bind_port);
	}
	net_set_nonblock(fd_listen, TRUE);
}

static int test_run_server(struct test_server_data *data)
{
	const struct http_server_settings *server_set = data->set;
	struct ioloop *ioloop;

	i_set_failure_prefix("SERVER: ");

	if (debug)
		i_debug("PID=%s", my_pid);

	ioloop_nested = NULL;
	ioloop_nested_depth = 0;
	ioloop = io_loop_create();
	test_server_init(server_set);
	io_loop_run(ioloop);
	test_server_deinit();
	io_loop_destroy(&ioloop);

	if (debug)
		i_debug("Terminated");

	i_close_fd(&fd_listen);
	test_files_deinit();
	main_deinit();
	return 0;
}

static void
test_run_client(
	const struct http_client_settings *client_set,
	void (*client_init)(const struct http_client_settings *client_set))
{
	struct ioloop *ioloop;

	i_set_failure_prefix("CLIENT: ");

	if (debug)
		i_debug("PID=%s", my_pid);

	ioloop_nested = NULL;
	ioloop_nested_depth = 0;
	ioloop = io_loop_create();
	client_init(client_set);
	io_loop_run(ioloop);
	test_client_deinit();
	io_loop_destroy(&ioloop);

	if (debug)
		i_debug("Terminated");
}

static void
test_run_client_server(
	const struct http_client_settings *client_set,
	const struct http_server_settings *server_set,
	void (*client_init)(const struct http_client_settings *client_set))
{
	struct test_server_data data;

	failure = NULL;

	test_files_init();

	i_zero(&data);
	data.set = server_set;

	/* Fork server */
	test_open_server_fd();
	test_subprocess_fork(test_run_server, &data, FALSE);
	i_close_fd(&fd_listen);

	/* Run client */
	test_run_client(client_set, client_init);

	i_unset_failure_prefix();
	test_subprocess_kill_all(SERVER_KILL_TIMEOUT_SECS);
	test_files_deinit();
}

static void
test_init_server_settings(struct http_server_settings *server_set_r)
{
	i_zero(server_set_r);
	server_set_r->request_limits.max_payload_size = UOFF_T_MAX;
	server_set_r->debug = debug;

	if (small_socket_buffers) {
		server_set_r->socket_send_buffer_size = 40960;
		server_set_r->socket_recv_buffer_size = 40960;
	}
}

static void
test_init_client_settings(struct http_client_settings *client_set_r)
{
	i_zero(client_set_r);
	client_set_r->max_redirects = 0;
	client_set_r->max_attempts = 1;
	client_set_r->max_idle_time_msecs =  5* 1000;
	client_set_r->debug = debug;

	if (small_socket_buffers) {
		client_set_r->socket_send_buffer_size = 40960;
		client_set_r->socket_recv_buffer_size = 40960;
		client_set_r->request_timeout_msecs = 20 * 60 * 1000;
		client_set_r->connect_timeout_msecs = 20 * 60 * 1000;
	}
}

static void
test_run_sequential(
	void (*client_init)(const struct http_client_settings *client_set))
{
	struct http_server_settings http_server_set;
	struct http_client_settings http_client_set;
	struct ssl_iostream_settings ssl_server_set, ssl_client_set;

	/* download files from blocking server */

	/* ssl settings */
	ssl_iostream_test_settings_server(&ssl_server_set);
	ssl_server_set.verbose = debug;
	ssl_iostream_test_settings_client(&ssl_client_set);
	ssl_client_set.verbose = debug;

	/* server settings */
	test_init_server_settings(&http_server_set);
	http_server_set.ssl = &ssl_server_set;
	http_server_set.max_pipelined_requests = 0;

	/* client settings */
	test_init_client_settings(&http_client_set);
	http_client_set.ssl = &ssl_client_set;
	http_client_set.max_parallel_connections = 1;
	http_client_set.max_pipelined_requests = 1;

	test_run_client_server(&http_client_set, &http_server_set, client_init);

	test_out_reason("sequential", (failure == NULL), failure);
}

static void
test_run_pipeline(
	void (*client_init)(const struct http_client_settings *client_set))
{
	struct http_server_settings http_server_set;
	struct http_client_settings http_client_set;
	struct ssl_iostream_settings ssl_server_set, ssl_client_set;

	/* download files from blocking server */

	/* ssl settings */
	ssl_iostream_test_settings_server(&ssl_server_set);
	ssl_server_set.verbose = debug;
	ssl_iostream_test_settings_client(&ssl_client_set);
	ssl_client_set.verbose = debug;

	/* server settings */
	test_init_server_settings(&http_server_set);
	http_server_set.ssl = &ssl_server_set;
	http_server_set.max_pipelined_requests = 4;

	/* client settings */
	test_init_client_settings(&http_client_set);
	http_client_set.ssl = &ssl_client_set;
	http_client_set.max_parallel_connections = 1;
	http_client_set.max_pipelined_requests = 8;

	test_run_client_server(&http_client_set, &http_server_set, client_init);

	test_out_reason("pipeline", (failure == NULL), failure);
}

static void
test_run_parallel(
	void (*client_init)(const struct http_client_settings *client_set))
{
	struct http_server_settings http_server_set;
	struct http_client_settings http_client_set;
	struct ssl_iostream_settings ssl_server_set, ssl_client_set;

	/* download files from blocking server */

	/* ssl settings */
	ssl_iostream_test_settings_server(&ssl_server_set);
	ssl_server_set.verbose = debug;
	ssl_iostream_test_settings_client(&ssl_client_set);
	ssl_client_set.verbose = debug;

	/* server settings */
	test_init_server_settings(&http_server_set);
	http_server_set.ssl = &ssl_server_set;
	http_server_set.max_pipelined_requests = 4;

	/* client settings */
	test_init_client_settings(&http_client_set);
	http_client_set.ssl = &ssl_client_set;
	http_client_set.max_parallel_connections = 40;
	http_client_set.max_pipelined_requests = 8;

	test_run_client_server(&http_client_set, &http_server_set, client_init);

	test_out_reason("parallel", (failure == NULL), failure);
}

static void test_download_server_nonblocking(void)
{
	test_begin("http payload download (server non-blocking)");
	test_init_defaults();
	test_run_sequential(test_client_download);
	test_run_pipeline(test_client_download);
	test_run_parallel(test_client_download);
	test_end();
}

static void test_download_server_blocking(void)
{
	test_begin("http payload download (server blocking)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	test_run_sequential(test_client_download);
	test_run_pipeline(test_client_download);
	test_run_parallel(test_client_download);
	test_end();
}

static void test_echo_server_nonblocking(void)
{
	test_begin("http payload echo "
		   "(server non-blocking)");
	test_init_defaults();
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; low-level)");
	test_init_defaults();
	tset.server_payload_handling = PAYLOAD_HANDLING_LOW_LEVEL;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; handler)");
	test_init_defaults();
	tset.server_payload_handling = PAYLOAD_HANDLING_HANDLER;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; size unknown)");
	test_init_defaults();
	tset.unknown_size = TRUE;
	tset.server_payload_handling = PAYLOAD_HANDLING_FORWARD;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; ostream)");
	test_init_defaults();
	tset.server_ostream = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; ostream; cork)");
	test_init_defaults();
	tset.server_ostream = TRUE;
	tset.server_cork = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
}

static void test_echo_server_blocking(void)
{
	test_begin("http payload echo (server blocking)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo (server blocking; ostream)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	tset.server_ostream = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo (server blocking; ostream; cork)");
	test_init_defaults();
	tset.server_ostream = TRUE;
	tset.server_blocking = TRUE;
	tset.server_cork = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
}

static void test_echo_server_nonblocking_sync(void)
{
	test_begin("http payload echo "
		   "(server non-blocking; 100-continue)");
	test_init_defaults();
	tset.request_100_continue = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; 100-continue; low-level)");
	test_init_defaults();
	tset.request_100_continue = TRUE;
	tset.server_payload_handling = PAYLOAD_HANDLING_LOW_LEVEL;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; 100-continue; handler)");
	test_init_defaults();
	tset.request_100_continue = TRUE;
	tset.server_payload_handling = PAYLOAD_HANDLING_HANDLER;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
}

static void test_echo_server_blocking_sync(void)
{
	test_begin("http payload echo (server blocking; 100-continue)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	tset.request_100_continue = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server blocking; ostream; 100-continue)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	tset.server_ostream = TRUE;
	tset.request_100_continue = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
}

static void test_echo_server_nonblocking_partial(void)
{
	test_begin("http payload echo "
		   "(server non-blocking; partial short)");
	test_init_defaults();
	tset.read_server_partial = 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
	test_begin("http payload echo "
		   "(server non-blocking; partial long)");
	test_init_defaults();
	tset.read_server_partial = IO_BLOCK_SIZE + 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo (server non-blocking; "
		   "partial short; low-level)");
	test_init_defaults();
	tset.server_payload_handling = PAYLOAD_HANDLING_LOW_LEVEL;
	tset.read_server_partial = 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
	test_begin("http payload echo "
		   "(server non-blocking; partial long; low-level)");
	test_init_defaults();
	tset.server_payload_handling = PAYLOAD_HANDLING_LOW_LEVEL;
	tset.read_server_partial = IO_BLOCK_SIZE + 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; partial short; handler)");
	test_init_defaults();
	tset.server_payload_handling = PAYLOAD_HANDLING_HANDLER;
	tset.read_server_partial = 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
	test_begin("http payload echo "
		   "(server non-blocking; partial long; handler)");
	test_init_defaults();
	tset.server_payload_handling = PAYLOAD_HANDLING_HANDLER;
	tset.read_server_partial = IO_BLOCK_SIZE + 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; partial short; ostream)");
	test_init_defaults();
	tset.server_ostream = TRUE;
	tset.read_server_partial = 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
	test_begin("http payload echo "
		   "(server non-blocking; partial long; ostream)");
	test_init_defaults();
	tset.server_ostream = TRUE;
	tset.read_server_partial = IO_BLOCK_SIZE + 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; partial short; ostream; corked)");
	test_init_defaults();
	tset.server_ostream = TRUE;
	tset.server_cork = TRUE;
	tset.read_server_partial = 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
	test_begin("http payload echo "
		   "(server non-blocking; partial long; ostream; corked)");
	test_init_defaults();
	tset.server_ostream = TRUE;
	tset.server_cork = TRUE;
	tset.read_server_partial = IO_BLOCK_SIZE + 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
}

static void test_echo_server_blocking_partial(void)
{
	test_begin("http payload echo (server blocking; partial short)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	tset.read_server_partial = 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
	test_begin("http payload echo (server blocking; partial long)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	tset.read_server_partial = IO_BLOCK_SIZE + 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server blocking; partial short; ostream; cork)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	tset.server_ostream = TRUE;
	tset.server_cork = TRUE;
	tset.read_server_partial = 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
	test_begin("http payload echo "
		   "(server blocking; partial long; ostream; cork)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	tset.server_ostream = TRUE;
	tset.server_cork = TRUE;
	tset.read_server_partial = IO_BLOCK_SIZE + 1024;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
}

static void test_download_client_partial(void)
{
	test_begin("http payload download (client partial)");
	test_init_defaults();
	tset.read_client_partial = 1024;
	test_run_sequential(test_client_download);
	test_run_pipeline(test_client_download);
	test_run_parallel(test_client_download);
	test_end();
	test_begin("http payload download (client partial long)");
	test_init_defaults();
	tset.read_client_partial = IO_BLOCK_SIZE + 1024;
	test_run_sequential(test_client_download);
	test_run_pipeline(test_client_download);
	test_run_parallel(test_client_download);
	test_end();
}

static void test_download_client_nested_ioloop(void)
{
	test_begin("http payload echo (client nested ioloop)");
	test_init_defaults();
	tset.client_ioloop_nesting = 10;
	test_run_parallel(test_client_echo);
	test_end();
}

static void test_echo_client_shared(void)
{
	test_begin("http payload download "
		   "(server non-blocking; client shared)");
	test_init_defaults();
	tset.parallel_clients = 4;
	test_run_sequential(test_client_download);
	tset.parallel_clients = 4;
	test_run_pipeline(test_client_download);
	tset.parallel_clients = 4;
	test_run_parallel(test_client_download);
	test_end();

	test_begin("http payload download "
		   "(server blocking; client shared)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	tset.parallel_clients = 4;
	test_run_sequential(test_client_download);
	tset.parallel_clients = 4;
	test_run_pipeline(test_client_download);
	tset.parallel_clients = 4;
	test_run_parallel(test_client_download);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; client shared)");
	test_init_defaults();
	tset.parallel_clients = 4;
	test_run_sequential(test_client_echo);
	tset.parallel_clients = 4;
	test_run_pipeline(test_client_echo);
	tset.parallel_clients = 4;
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server blocking; client shared)");
	test_init_defaults();
	tset.server_blocking = TRUE;
	tset.server_ostream = TRUE;
	tset.parallel_clients = 4;
	test_run_sequential(test_client_echo);
	tset.parallel_clients = 4;
	test_run_pipeline(test_client_echo);
	tset.parallel_clients = 4;
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo "
		   "(server non-blocking; client global)");
	test_init_defaults();
	tset.parallel_clients = 4;
	tset.parallel_clients_global = TRUE;
	test_run_sequential(test_client_echo);
	tset.parallel_clients = 4;
	tset.parallel_clients_global = TRUE;
	test_run_pipeline(test_client_echo);
	tset.parallel_clients = 4;
	tset.parallel_clients_global = TRUE;
	test_run_parallel(test_client_echo);
	test_end();
}

#ifdef HAVE_OPENSSL
static void test_echo_ssl(void)
{
	test_begin("http payload echo (ssl)");
	test_init_defaults();
	tset.ssl = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo (ssl; unknown size)");
	test_init_defaults();
	tset.unknown_size = TRUE;
	tset.ssl = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo (ssl; server ostream, cork)");
	test_init_defaults();
	tset.ssl = TRUE;
	tset.server_ostream = TRUE;
	tset.server_cork = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();
}
#endif

static void test_echo_client_blocking(void)
{
	test_begin("http payload echo (client blocking)");
	test_init_defaults();
	tset.client_blocking = TRUE;
	test_run_sequential(test_client_echo);
	test_run_pipeline(test_client_echo);
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo (client blocking; client shared)");
	test_init_defaults();
	tset.client_blocking = TRUE;
	tset.parallel_clients = 4;
	test_run_sequential(test_client_echo);
	tset.parallel_clients = 4;
	test_run_pipeline(test_client_echo);
	tset.parallel_clients = 4;
	test_run_parallel(test_client_echo);
	test_end();

	test_begin("http payload echo (client blocking; client global)");
	test_init_defaults();
	tset.client_blocking = TRUE;
	tset.parallel_clients = 4;
	tset.parallel_clients_global = TRUE;
	test_run_sequential(test_client_echo);
	tset.parallel_clients = 4;
	tset.parallel_clients_global = TRUE;
	test_run_pipeline(test_client_echo);
	tset.parallel_clients = 4;
	tset.parallel_clients_global = TRUE;
	test_run_parallel(test_client_echo);
	test_end();
}

static void (*const test_functions[])(void) = {
	test_download_server_nonblocking,
	test_download_server_blocking,
	test_echo_server_nonblocking,
	test_echo_server_blocking,
	test_echo_server_nonblocking_sync,
	test_echo_server_blocking_sync,
	test_echo_server_nonblocking_partial,
	test_echo_server_blocking_partial,
	test_download_client_partial,
	test_download_client_nested_ioloop,
	test_echo_client_shared,
#ifdef HAVE_OPENSSL
	test_echo_ssl,
#endif
	test_echo_client_blocking,
	NULL
};

/*
 * Main
 */

static void main_init(void)
{
#ifdef HAVE_OPENSSL
	ssl_iostream_openssl_init();
#endif
}

static void main_deinit(void)
{
	ssl_iostream_context_cache_free();
#ifdef HAVE_OPENSSL
	ssl_iostream_openssl_deinit();
#endif
}

int main(int argc, char *argv[])
{
	int c;
	int ret;

	lib_init();
	main_init();

	while ((c = getopt(argc, argv, "DS")) > 0) {
		switch (c) {
		case 'D':
			debug = TRUE;
			break;
		case 'S':
			small_socket_buffers = TRUE;
			break;
		default:
			i_fatal("Usage: %s [-D]", argv[0]);
		}
	}

	test_subprocesses_init(debug);

	/* listen on localhost */
	i_zero(&bind_ip);
	bind_ip.family = AF_INET;
	bind_ip.u.ip4.s_addr = htonl(INADDR_LOOPBACK);

	ret = test_run(test_functions);

	test_subprocesses_deinit();
	main_deinit();
	lib_deinit();
	return ret;
}