summaryrefslogtreecommitdiffstats
path: root/src/web_client.c
blob: 4036d4c813f49411788d4af9e751f6a7931b482f (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
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
#include "common.h"

#define INITIAL_WEB_DATA_LENGTH 16384
#define WEB_REQUEST_LENGTH 16384
#define TOO_BIG_REQUEST 16384

int web_client_timeout = DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS;
int web_donotrack_comply = 0;

#ifdef NETDATA_WITH_ZLIB
int web_enable_gzip = 1, web_gzip_level = 3, web_gzip_strategy = Z_DEFAULT_STRATEGY;
#endif /* NETDATA_WITH_ZLIB */

struct web_client *web_clients = NULL;
unsigned long long web_clients_count = 0;

inline int web_client_crock_socket(struct web_client *w) {
#ifdef TCP_CORK
    if(likely(!w->tcp_cork && w->ofd != -1)) {
        w->tcp_cork = 1;
        if(unlikely(setsockopt(w->ofd, IPPROTO_TCP, TCP_CORK, (char *) &w->tcp_cork, sizeof(int)) != 0)) {
            error("%llu: failed to enable TCP_CORK on socket.", w->id);
            w->tcp_cork = 0;
            return -1;
        }
    }
#endif /* TCP_CORK */

    return 0;
}

inline int web_client_uncrock_socket(struct web_client *w) {
#ifdef TCP_CORK
    if(likely(w->tcp_cork && w->ofd != -1)) {
        w->tcp_cork = 0;
        if(unlikely(setsockopt(w->ofd, IPPROTO_TCP, TCP_CORK, (char *) &w->tcp_cork, sizeof(int)) != 0)) {
            error("%llu: failed to disable TCP_CORK on socket.", w->id);
            w->tcp_cork = 1;
            return -1;
        }
    }
#endif /* TCP_CORK */

    return 0;
}

struct web_client *web_client_create(int listener)
{
    struct web_client *w;

    w = callocz(1, sizeof(struct web_client));
    w->id = ++web_clients_count;
    w->mode = WEB_CLIENT_MODE_NORMAL;

    {
        struct sockaddr *sadr;
        socklen_t addrlen;

        sadr = (struct sockaddr*) &w->clientaddr;
        addrlen = sizeof(w->clientaddr);

        w->ifd = accept4(listener, sadr, &addrlen, SOCK_NONBLOCK);
        if (w->ifd == -1) {
            error("%llu: Cannot accept new incoming connection.", w->id);
            freez(w);
            return NULL;
        }
        w->ofd = w->ifd;

        if(getnameinfo(sadr, addrlen, w->client_ip, NI_MAXHOST, w->client_port, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
            error("Cannot getnameinfo() on received client connection.");
            strncpyz(w->client_ip,   "UNKNOWN", NI_MAXHOST);
            strncpyz(w->client_port, "UNKNOWN", NI_MAXSERV);
        }
        w->client_ip[NI_MAXHOST]   = '\0';
        w->client_port[NI_MAXSERV] = '\0';

        switch(sadr->sa_family) {
        case AF_INET:
            debug(D_WEB_CLIENT_ACCESS, "%llu: New IPv4 web client from %s port %s on socket %d.", w->id, w->client_ip, w->client_port, w->ifd);
            break;

        case AF_INET6:
            if(strncmp(w->client_ip, "::ffff:", 7) == 0) {
                memmove(w->client_ip, &w->client_ip[7], strlen(&w->client_ip[7]) + 1);
                debug(D_WEB_CLIENT_ACCESS, "%llu: New IPv4 web client from %s port %s on socket %d.", w->id, w->client_ip, w->client_port, w->ifd);
            }
            else
                debug(D_WEB_CLIENT_ACCESS, "%llu: New IPv6 web client from %s port %s on socket %d.", w->id, w->client_ip, w->client_port, w->ifd);
            break;

        default:
            debug(D_WEB_CLIENT_ACCESS, "%llu: New UNKNOWN web client from %s port %s on socket %d.", w->id, w->client_ip, w->client_port, w->ifd);
            break;
        }

        int flag = 1;
        if(setsockopt(w->ofd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) != 0)
            error("%llu: failed to enable TCP_NODELAY on socket.", w->id);

        flag = 1;
        if(setsockopt(w->ifd, SOL_SOCKET, SO_KEEPALIVE, (char *) &flag, sizeof(int)) != 0)
            error("%llu: Cannot set SO_KEEPALIVE on socket.", w->id);
    }

    w->response.data = buffer_create(INITIAL_WEB_DATA_LENGTH);
    w->response.header = buffer_create(HTTP_RESPONSE_HEADER_SIZE);
    w->response.header_output = buffer_create(HTTP_RESPONSE_HEADER_SIZE);
    w->origin[0] = '*';
    w->wait_receive = 1;

    if(web_clients) web_clients->prev = w;
    w->next = web_clients;
    web_clients = w;

    web_client_connected();

    return(w);
}

void web_client_reset(struct web_client *w) {
    web_client_uncrock_socket(w);

    debug(D_WEB_CLIENT, "%llu: Reseting client.", w->id);

    if(likely(w->last_url[0])) {
        struct timeval tv;
        gettimeofday(&tv, NULL);

        size_t size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
        size_t sent = size;
#ifdef NETDATA_WITH_ZLIB
        if(likely(w->response.zoutput)) sent = (size_t)w->response.zstream.total_out;
#endif

        // --------------------------------------------------------------------
        // global statistics

        finished_web_request_statistics(usec_dt(&tv, &w->tv_in),
                                        w->stats_received_bytes,
                                        w->stats_sent_bytes,
                                        size,
                                        sent);

        w->stats_received_bytes = 0;
        w->stats_sent_bytes = 0;


        // --------------------------------------------------------------------
        // access log

        log_access("%llu: (sent/all = %zu/%zu bytes %0.0f%%, prep/sent/total = %0.2f/%0.2f/%0.2f ms) %s: %d '%s'",
                   w->id,
                   sent, size, -((size > 0) ? ((size - sent) / (double) size * 100.0) : 0.0),
                   usec_dt(&w->tv_ready, &w->tv_in) / 1000.0,
                   usec_dt(&tv, &w->tv_ready) / 1000.0,
                   usec_dt(&tv, &w->tv_in) / 1000.0,
                   (w->mode == WEB_CLIENT_MODE_FILECOPY) ? "filecopy" : ((w->mode == WEB_CLIENT_MODE_OPTIONS)
                                                                         ? "options" : "data"),
                   w->response.code,
                   w->last_url
        );
    }

    if(unlikely(w->mode == WEB_CLIENT_MODE_FILECOPY)) {
        if(w->ifd != w->ofd) {
            debug(D_WEB_CLIENT, "%llu: Closing filecopy input file descriptor %d.", w->id, w->ifd);
            if(w->ifd != -1) close(w->ifd);
            w->ifd = w->ofd;
        }
    }

    w->last_url[0] = '\0';
    w->cookie1[0] = '\0';
    w->cookie2[0] = '\0';
    w->origin[0] = '*';
    w->origin[1] = '\0';

    w->mode = WEB_CLIENT_MODE_NORMAL;

    w->tcp_cork = 0;
    w->donottrack = 0;
    w->tracking_required = 0;
    w->keepalive = 0;
    w->decoded_url[0] = '\0';

    buffer_reset(w->response.header_output);
    buffer_reset(w->response.header);
    buffer_reset(w->response.data);
    w->response.rlen = 0;
    w->response.sent = 0;
    w->response.code = 0;

    w->wait_receive = 1;
    w->wait_send = 0;

    w->response.zoutput = 0;

    // if we had enabled compression, release it
#ifdef NETDATA_WITH_ZLIB
    if(w->response.zinitialized) {
        debug(D_DEFLATE, "%llu: Freeing compression resources.", w->id);
        deflateEnd(&w->response.zstream);
        w->response.zsent = 0;
        w->response.zhave = 0;
        w->response.zstream.avail_in = 0;
        w->response.zstream.avail_out = 0;
        w->response.zstream.total_in = 0;
        w->response.zstream.total_out = 0;
        w->response.zinitialized = 0;
    }
#endif // NETDATA_WITH_ZLIB
}

struct web_client *web_client_free(struct web_client *w) {
    web_client_reset(w);

    struct web_client *n = w->next;
    if(w == web_clients) web_clients = n;

    debug(D_WEB_CLIENT_ACCESS, "%llu: Closing web client from %s port %s.", w->id, w->client_ip, w->client_port);

    if(w->prev) w->prev->next = w->next;
    if(w->next) w->next->prev = w->prev;
    if(w->response.header_output) buffer_free(w->response.header_output);
    if(w->response.header) buffer_free(w->response.header);
    if(w->response.data) buffer_free(w->response.data);
    if(w->ifd != -1) close(w->ifd);
    if(w->ofd != -1 && w->ofd != w->ifd) close(w->ofd);
    freez(w);

    web_client_disconnected();

    return(n);
}

uid_t web_files_uid(void) {
    static char *web_owner = NULL;
    static uid_t owner_uid = 0;

    if(unlikely(!web_owner)) {
        web_owner = config_get("global", "web files owner", config_get("global", "run as user", ""));
        if(!web_owner || !*web_owner)
            owner_uid = geteuid();
        else {
            // getpwnam() is not thread safe,
            // but we have called this function once
            // while single threaded
            struct passwd *pw = getpwnam(web_owner);
            if(!pw) {
                error("User '%s' is not present. Ignoring option.", web_owner);
                owner_uid = geteuid();
            }
            else {
                debug(D_WEB_CLIENT, "Web files owner set to %s.", web_owner);
                owner_uid = pw->pw_uid;
            }
        }
    }

    return(owner_uid);
}

gid_t web_files_gid(void) {
    static char *web_group = NULL;
    static gid_t owner_gid = 0;

    if(unlikely(!web_group)) {
        web_group = config_get("global", "web files group", config_get("global", "web files owner", ""));
        if(!web_group || !*web_group)
            owner_gid = getegid();
        else {
            // getgrnam() is not thread safe,
            // but we have called this function once
            // while single threaded
            struct group *gr = getgrnam(web_group);
            if(!gr) {
                error("Group '%s' is not present. Ignoring option.", web_group);
                owner_gid = getegid();
            }
            else {
                debug(D_WEB_CLIENT, "Web files group set to %s.", web_group);
                owner_gid = gr->gr_gid;
            }
        }
    }

    return(owner_gid);
}

int mysendfile(struct web_client *w, char *filename)
{
    static char *web_dir = NULL;

    // initialize our static data
    if(unlikely(!web_dir)) web_dir = config_get("global", "web files directory", WEB_DIR);

    debug(D_WEB_CLIENT, "%llu: Looking for file '%s/%s'", w->id, web_dir, filename);

    // skip leading slashes
    while (*filename == '/') filename++;

    // if the filename contain known paths, skip them
    if(strncmp(filename, WEB_PATH_FILE "/", strlen(WEB_PATH_FILE) + 1) == 0)
        filename = &filename[strlen(WEB_PATH_FILE) + 1];

    char *s;
    for(s = filename; *s ;s++) {
        if( !isalnum(*s) && *s != '/' && *s != '.' && *s != '-' && *s != '_') {
            debug(D_WEB_CLIENT_ACCESS, "%llu: File '%s' is not acceptable.", w->id, filename);
            buffer_sprintf(w->response.data, "File '%s' cannot be served. Filename contains invalid character '%c'", filename, *s);
            return 400;
        }
    }

    // if the filename contains a .. refuse to serve it
    if(strstr(filename, "..") != 0) {
        debug(D_WEB_CLIENT_ACCESS, "%llu: File '%s' is not acceptable.", w->id, filename);
        buffer_sprintf(w->response.data, "File '%s' cannot be served. Relative filenames with '..' in them are not supported.", filename);
        return 400;
    }

    // access the file
    char webfilename[FILENAME_MAX + 1];
    snprintfz(webfilename, FILENAME_MAX, "%s/%s", web_dir, filename);

    // check if the file exists
    struct stat stat;
    if(lstat(webfilename, &stat) != 0) {
        debug(D_WEB_CLIENT_ACCESS, "%llu: File '%s' is not found.", w->id, webfilename);
        buffer_sprintf(w->response.data, "File '%s' does not exist, or is not accessible.", webfilename);
        return 404;
    }

    // check if the file is owned by expected user
    if(stat.st_uid != web_files_uid()) {
        error("%llu: File '%s' is owned by user %u (expected user %u). Access Denied.", w->id, webfilename, stat.st_uid, web_files_uid());
        buffer_sprintf(w->response.data, "Access to file '%s' is not permitted.", webfilename);
        return 403;
    }

    // check if the file is owned by expected group
    if(stat.st_gid != web_files_gid()) {
        error("%llu: File '%s' is owned by group %u (expected group %u). Access Denied.", w->id, webfilename, stat.st_gid, web_files_gid());
        buffer_sprintf(w->response.data, "Access to file '%s' is not permitted.", webfilename);
        return 403;
    }

    if((stat.st_mode & S_IFMT) == S_IFDIR) {
        snprintfz(webfilename, FILENAME_MAX, "%s/index.html", filename);
        return mysendfile(w, webfilename);
    }

    if((stat.st_mode & S_IFMT) != S_IFREG) {
        error("%llu: File '%s' is not a regular file. Access Denied.", w->id, webfilename);
        buffer_sprintf(w->response.data, "Access to file '%s' is not permitted.", webfilename);
        return 403;
    }

    // open the file
    w->ifd = open(webfilename, O_NONBLOCK, O_RDONLY);
    if(w->ifd == -1) {
        w->ifd = w->ofd;

        if(errno == EBUSY || errno == EAGAIN) {
            error("%llu: File '%s' is busy, sending 307 Moved Temporarily to force retry.", w->id, webfilename);
            buffer_sprintf(w->response.header, "Location: /" WEB_PATH_FILE "/%s\r\n", filename);
            buffer_sprintf(w->response.data, "The file '%s' is currently busy. Please try again later.", webfilename);
            return 307;
        }
        else {
            error("%llu: Cannot open file '%s'.", w->id, webfilename);
            buffer_sprintf(w->response.data, "Cannot open file '%s'.", webfilename);
            return 404;
        }
    }
    if(fcntl(w->ifd, F_SETFL, O_NONBLOCK) < 0)
        error("%llu: Cannot set O_NONBLOCK on file '%s'.", w->id, webfilename);

    // pick a Content-Type for the file
         if(strstr(filename, ".html") != NULL)  w->response.data->contenttype = CT_TEXT_HTML;
    else if(strstr(filename, ".js")   != NULL)  w->response.data->contenttype = CT_APPLICATION_X_JAVASCRIPT;
    else if(strstr(filename, ".css")  != NULL)  w->response.data->contenttype = CT_TEXT_CSS;
    else if(strstr(filename, ".xml")  != NULL)  w->response.data->contenttype = CT_TEXT_XML;
    else if(strstr(filename, ".xsl")  != NULL)  w->response.data->contenttype = CT_TEXT_XSL;
    else if(strstr(filename, ".txt")  != NULL)  w->response.data->contenttype = CT_TEXT_PLAIN;
    else if(strstr(filename, ".svg")  != NULL)  w->response.data->contenttype = CT_IMAGE_SVG_XML;
    else if(strstr(filename, ".ttf")  != NULL)  w->response.data->contenttype = CT_APPLICATION_X_FONT_TRUETYPE;
    else if(strstr(filename, ".otf")  != NULL)  w->response.data->contenttype = CT_APPLICATION_X_FONT_OPENTYPE;
    else if(strstr(filename, ".woff2")!= NULL)  w->response.data->contenttype = CT_APPLICATION_FONT_WOFF2;
    else if(strstr(filename, ".woff") != NULL)  w->response.data->contenttype = CT_APPLICATION_FONT_WOFF;
    else if(strstr(filename, ".eot")  != NULL)  w->response.data->contenttype = CT_APPLICATION_VND_MS_FONTOBJ;
    else if(strstr(filename, ".png")  != NULL)  w->response.data->contenttype = CT_IMAGE_PNG;
    else if(strstr(filename, ".jpg")  != NULL)  w->response.data->contenttype = CT_IMAGE_JPG;
    else if(strstr(filename, ".jpeg") != NULL)  w->response.data->contenttype = CT_IMAGE_JPG;
    else if(strstr(filename, ".gif")  != NULL)  w->response.data->contenttype = CT_IMAGE_GIF;
    else if(strstr(filename, ".bmp")  != NULL)  w->response.data->contenttype = CT_IMAGE_BMP;
    else if(strstr(filename, ".ico")  != NULL)  w->response.data->contenttype = CT_IMAGE_XICON;
    else if(strstr(filename, ".icns") != NULL)  w->response.data->contenttype = CT_IMAGE_ICNS;
    else w->response.data->contenttype = CT_APPLICATION_OCTET_STREAM;

    debug(D_WEB_CLIENT_ACCESS, "%llu: Sending file '%s' (%ld bytes, ifd %d, ofd %d).", w->id, webfilename, stat.st_size, w->ifd, w->ofd);

    w->mode = WEB_CLIENT_MODE_FILECOPY;
    w->wait_receive = 1;
    w->wait_send = 0;
    buffer_flush(w->response.data);
    w->response.rlen = stat.st_size;
    w->response.data->date = stat.st_mtim.tv_sec;

    return 200;
}


#ifdef NETDATA_WITH_ZLIB
void web_client_enable_deflate(struct web_client *w, int gzip) {
    if(unlikely(w->response.zinitialized)) {
        error("%llu: Compression has already be initialized for this client.", w->id);
        return;
    }

    if(unlikely(w->response.sent)) {
        error("%llu: Cannot enable compression in the middle of a conversation.", w->id);
        return;
    }

    w->response.zstream.zalloc = Z_NULL;
    w->response.zstream.zfree = Z_NULL;
    w->response.zstream.opaque = Z_NULL;

    w->response.zstream.next_in = (Bytef *)w->response.data->buffer;
    w->response.zstream.avail_in = 0;
    w->response.zstream.total_in = 0;

    w->response.zstream.next_out = w->response.zbuffer;
    w->response.zstream.avail_out = 0;
    w->response.zstream.total_out = 0;

    w->response.zstream.zalloc = Z_NULL;
    w->response.zstream.zfree = Z_NULL;
    w->response.zstream.opaque = Z_NULL;

//  if(deflateInit(&w->response.zstream, Z_DEFAULT_COMPRESSION) != Z_OK) {
//      error("%llu: Failed to initialize zlib. Proceeding without compression.", w->id);
//      return;
//  }

    // Select GZIP compression: windowbits = 15 + 16 = 31
    if(deflateInit2(&w->response.zstream, web_gzip_level, Z_DEFLATED, 15 + ((gzip)?16:0), 8, web_gzip_strategy) != Z_OK) {
        error("%llu: Failed to initialize zlib. Proceeding without compression.", w->id);
        return;
    }

    w->response.zsent = 0;
    w->response.zoutput = 1;
    w->response.zinitialized = 1;

    debug(D_DEFLATE, "%llu: Initialized compression.", w->id);
}
#endif // NETDATA_WITH_ZLIB

void buffer_data_options2string(BUFFER *wb, uint32_t options) {
    int count = 0;

    if(options & RRDR_OPTION_NONZERO) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "nonzero");
    }

    if(options & RRDR_OPTION_REVERSED) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "flip");
    }

    if(options & RRDR_OPTION_JSON_WRAP) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "jsonwrap");
    }

    if(options & RRDR_OPTION_MIN2MAX) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "min2max");
    }

    if(options & RRDR_OPTION_MILLISECONDS) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "ms");
    }

    if(options & RRDR_OPTION_ABSOLUTE) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "abs");
    }

    if(options & RRDR_OPTION_SECONDS) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "seconds");
    }

    if(options & RRDR_OPTION_NULL2ZERO) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "null2zero");
    }

    if(options & RRDR_OPTION_OBJECTSROWS) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "objectrows");
    }

    if(options & RRDR_OPTION_GOOGLE_JSON) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "google_json");
    }

    if(options & RRDR_OPTION_PERCENTAGE) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "percentage");
    }

    if(options & RRDR_OPTION_NOT_ALIGNED) {
        if(count++) buffer_strcat(wb, " ");
        buffer_strcat(wb, "unaligned");
    }
}

uint32_t web_client_api_request_v1_data_options(char *o)
{
    uint32_t ret = 0x00000000;
    char *tok;

    while(o && *o && (tok = mystrsep(&o, ", |"))) {
        if(!*tok) continue;

        if(!strcmp(tok, "nonzero"))
            ret |= RRDR_OPTION_NONZERO;
        else if(!strcmp(tok, "flip") || !strcmp(tok, "reversed") || !strcmp(tok, "reverse"))
            ret |= RRDR_OPTION_REVERSED;
        else if(!strcmp(tok, "jsonwrap"))
            ret |= RRDR_OPTION_JSON_WRAP;
        else if(!strcmp(tok, "min2max"))
            ret |= RRDR_OPTION_MIN2MAX;
        else if(!strcmp(tok, "ms") || !strcmp(tok, "milliseconds"))
            ret |= RRDR_OPTION_MILLISECONDS;
        else if(!strcmp(tok, "abs") || !strcmp(tok, "absolute") || !strcmp(tok, "absolute_sum") || !strcmp(tok, "absolute-sum"))
            ret |= RRDR_OPTION_ABSOLUTE;
        else if(!strcmp(tok, "seconds"))
            ret |= RRDR_OPTION_SECONDS;
        else if(!strcmp(tok, "null2zero"))
            ret |= RRDR_OPTION_NULL2ZERO;
        else if(!strcmp(tok, "objectrows"))
            ret |= RRDR_OPTION_OBJECTSROWS;
        else if(!strcmp(tok, "google_json"))
            ret |= RRDR_OPTION_GOOGLE_JSON;
        else if(!strcmp(tok, "percentage"))
            ret |= RRDR_OPTION_PERCENTAGE;
        else if(!strcmp(tok, "unaligned"))
            ret |= RRDR_OPTION_NOT_ALIGNED;
    }

    return ret;
}

uint32_t web_client_api_request_v1_data_format(char *name)
{
    if(!strcmp(name, DATASOURCE_FORMAT_DATATABLE_JSON)) // datatable
        return DATASOURCE_DATATABLE_JSON;

    else if(!strcmp(name, DATASOURCE_FORMAT_DATATABLE_JSONP)) // datasource
        return DATASOURCE_DATATABLE_JSONP;

    else if(!strcmp(name, DATASOURCE_FORMAT_JSON)) // json
        return DATASOURCE_JSON;

    else if(!strcmp(name, DATASOURCE_FORMAT_JSONP)) // jsonp
        return DATASOURCE_JSONP;

    else if(!strcmp(name, DATASOURCE_FORMAT_SSV)) // ssv
        return DATASOURCE_SSV;

    else if(!strcmp(name, DATASOURCE_FORMAT_CSV)) // csv
        return DATASOURCE_CSV;

    else if(!strcmp(name, DATASOURCE_FORMAT_TSV) || !strcmp(name, "tsv-excel")) // tsv
        return DATASOURCE_TSV;

    else if(!strcmp(name, DATASOURCE_FORMAT_HTML)) // html
        return DATASOURCE_HTML;

    else if(!strcmp(name, DATASOURCE_FORMAT_JS_ARRAY)) // array
        return DATASOURCE_JS_ARRAY;

    else if(!strcmp(name, DATASOURCE_FORMAT_SSV_COMMA)) // ssvcomma
        return DATASOURCE_SSV_COMMA;

    else if(!strcmp(name, DATASOURCE_FORMAT_CSV_JSON_ARRAY)) // csvjsonarray
        return DATASOURCE_CSV_JSON_ARRAY;

    return DATASOURCE_JSON;
}

uint32_t web_client_api_request_v1_data_google_format(char *name)
{
    if(!strcmp(name, "json"))
        return DATASOURCE_DATATABLE_JSONP;

    else if(!strcmp(name, "html"))
        return DATASOURCE_HTML;

    else if(!strcmp(name, "csv"))
        return DATASOURCE_CSV;

    else if(!strcmp(name, "tsv-excel"))
        return DATASOURCE_TSV;

    return DATASOURCE_JSON;
}

const char *group_method2string(int group) {
    switch(group) {
        case GROUP_UNDEFINED:
            return "";

        case GROUP_AVERAGE:
            return "average";

        case GROUP_MIN:
            return "min";

        case GROUP_MAX:
            return "max";

        case GROUP_SUM:
            return "sum";

        case GROUP_INCREMENTAL_SUM:
            return "incremental-sum";

        default:
            return "unknown-group-method";
    }
}

int web_client_api_request_v1_data_group(char *name, int def)
{
    if(!strcmp(name, "average"))
        return GROUP_AVERAGE;

    else if(!strcmp(name, "min"))
        return GROUP_MIN;

    else if(!strcmp(name, "max"))
        return GROUP_MAX;

    else if(!strcmp(name, "sum"))
        return GROUP_SUM;

    else if(!strcmp(name, "incremental-sum"))
        return GROUP_INCREMENTAL_SUM;

    return def;
}

int web_client_api_request_v1_alarms(struct web_client *w, char *url)
{
    int all = 0;

    while(url) {
        char *value = mystrsep(&url, "?&[]");
        if (!value || !*value) continue;

        if(!strcmp(value, "all")) all = 1;
        else if(!strcmp(value, "active")) all = 0;
    }

    buffer_flush(w->response.data);
    w->response.data->contenttype = CT_APPLICATION_JSON;
    health_alarms2json(&localhost, w->response.data, all);
    return 200;
}

int web_client_api_request_v1_alarm_log(struct web_client *w, char *url)
{
    (void)url;

    buffer_flush(w->response.data);
    w->response.data->contenttype = CT_APPLICATION_JSON;
    health_alarm_log2json(&localhost, w->response.data);
    return 200;
}

int web_client_api_request_v1_charts(struct web_client *w, char *url)
{
    if(url) { ; }

    buffer_flush(w->response.data);
    w->response.data->contenttype = CT_APPLICATION_JSON;
    rrd_stats_api_v1_charts(w->response.data);
    return 200;
}

int web_client_api_request_v1_chart(struct web_client *w, char *url)
{
    int ret = 400;
    char *chart = NULL;

    buffer_flush(w->response.data);

    while(url) {
        char *value = mystrsep(&url, "?&[]");
        if(!value || !*value) continue;

        char *name = mystrsep(&value, "=");
        if(!name || !*name) continue;
        if(!value || !*value) continue;

        // name and value are now the parameters
        // they are not null and not empty

        if(!strcmp(name, "chart")) chart = value;
        //else {
        /// buffer_sprintf(w->response.data, "Unknown parameter '%s' in request.", name);
        //  goto cleanup;
        //}
    }

    if(!chart || !*chart) {
        buffer_sprintf(w->response.data, "No chart id is given at the request.");
        goto cleanup;
    }

    RRDSET *st = rrdset_find(chart);
    if(!st) st = rrdset_find_byname(chart);
    if(!st) {
        buffer_sprintf(w->response.data, "Chart '%s' is not found.", chart);
        ret = 404;
        goto cleanup;
    }

    w->response.data->contenttype = CT_APPLICATION_JSON;
    rrd_stats_api_v1_chart(st, w->response.data);
    return 200;

cleanup:
    return ret;
}

int web_client_api_request_v1_badge(struct web_client *w, char *url) {
    int ret = 400;
    buffer_flush(w->response.data);

    BUFFER *dimensions = NULL;
    
    const char *chart = NULL
            , *before_str = NULL
            , *after_str = NULL
            , *points_str = NULL
            , *multiply_str = NULL
            , *divide_str = NULL
            , *label = NULL
            , *units = NULL
            , *label_color = NULL
            , *value_color = NULL
            , *refresh_str = NULL
            , *precision_str = NULL
            , *alarm = NULL;

    int group = GROUP_AVERAGE;
    uint32_t options = 0x00000000;

    while(url) {
        char *value = mystrsep(&url, "/?&[]");
        if(!value || !*value) continue;

        char *name = mystrsep(&value, "=");
        if(!name || !*name) continue;
        if(!value || !*value) continue;

        debug(D_WEB_CLIENT, "%llu: API v1 badge.svg query param '%s' with value '%s'", w->id, name, value);

        // name and value are now the parameters
        // they are not null and not empty

        if(!strcmp(name, "chart")) chart = value;
        else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
            if(!dimensions)
                dimensions = buffer_create(100);

            buffer_strcat(dimensions, "|");
            buffer_strcat(dimensions, value);
        }
        else if(!strcmp(name, "after")) after_str = value;
        else if(!strcmp(name, "before")) before_str = value;
        else if(!strcmp(name, "points")) points_str = value;
        else if(!strcmp(name, "group")) {
            group = web_client_api_request_v1_data_group(value, GROUP_AVERAGE);
        }
        else if(!strcmp(name, "options")) {
            options |= web_client_api_request_v1_data_options(value);
        }
        else if(!strcmp(name, "label")) label = value;
        else if(!strcmp(name, "units")) units = value;
        else if(!strcmp(name, "label_color")) label_color = value;
        else if(!strcmp(name, "value_color")) value_color = value;
        else if(!strcmp(name, "multiply")) multiply_str = value;
        else if(!strcmp(name, "divide")) divide_str = value;
        else if(!strcmp(name, "refresh")) refresh_str = value;
        else if(!strcmp(name, "precision")) precision_str = value;
        else if(!strcmp(name, "alarm")) alarm = value;
    }

    if(!chart || !*chart) {
        buffer_sprintf(w->response.data, "No chart id is given at the request.");
        goto cleanup;
    }

    RRDSET *st = rrdset_find(chart);
    if(!st) st = rrdset_find_byname(chart);
    if(!st) {
        buffer_svg(w->response.data, "chart not found", 0, "", NULL, NULL, 1, -1);
        ret = 200;
        goto cleanup;
    }

    RRDCALC *rc = NULL;
    if(alarm) {
        rc = rrdcalc_find(st, alarm);
        if (!rc) {
            buffer_svg(w->response.data, "alarm not found", 0, "", NULL, NULL, 1, -1);
            ret = 200;
            goto cleanup;
        }
    }

    long long multiply  = (multiply_str  && *multiply_str )?atol(multiply_str):1;
    long long divide    = (divide_str    && *divide_str   )?atol(divide_str):1;
    long long before    = (before_str    && *before_str   )?atol(before_str):0;
    long long after     = (after_str     && *after_str    )?atol(after_str):-st->update_every;
    int       points    = (points_str    && *points_str   )?atoi(points_str):1;
    int       precision = (precision_str && *precision_str)?atoi(precision_str):-1;

    if(!multiply) multiply = 1;
    if(!divide) divide = 1;

    int refresh = 0;
    if(refresh_str && *refresh_str) {
        if(!strcmp(refresh_str, "auto")) {
            if(rc) refresh = rc->update_every;
            else if(options & RRDR_OPTION_NOT_ALIGNED)
                refresh = st->update_every;
            else {
                refresh = (before - after);
                if(refresh < 0) refresh = -refresh;
            }
        }
        else {
            refresh = atoi(refresh_str);
            if(refresh < 0) refresh = -refresh;
        }
    }

    if(!label) {
        if(alarm) {
            char *s = (char *)alarm;
            while(*s) {
                if(*s == '_') *s = ' ';
                s++;
            }
            label = alarm;
        }
        else if(dimensions) {
            const char *dim = buffer_tostring(dimensions);
            if(*dim == '|') dim++;
            label = dim;
        }
        else
            label = st->name;
    }
    if(!units) {
        if(alarm) {
            if(rc->units)
                units = rc->units;
            else
                units = "";
        }
        else if(options & RRDR_OPTION_PERCENTAGE)
            units = "%";
        else
            units = st->units;
    }

    debug(D_WEB_CLIENT, "%llu: API command 'badge.svg' for chart '%s', alarm '%s', dimensions '%s', after '%lld', before '%lld', points '%d', group '%d', options '0x%08x'"
            , w->id
            , chart
            , alarm?alarm:""
            , (dimensions)?buffer_tostring(dimensions):""
            , after
            , before
            , points
            , group
            , options
            );

    if(rc) {
        calculated_number n = rc->value;
        if(isnan(n) || isinf(n)) n = 0;

        if (refresh > 0)
            buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh);

        if(!value_color) {
            switch(rc->status) {
                case RRDCALC_STATUS_CRITICAL:
                    value_color = "red";
                    break;

                case RRDCALC_STATUS_WARNING:
                    value_color = "orange";
                    break;

                case RRDCALC_STATUS_CLEAR:
                    value_color = "brightgreen";
                    break;

                case RRDCALC_STATUS_UNDEFINED:
                    value_color = "lightgrey";
                    break;

                case RRDCALC_STATUS_UNINITIALIZED:
                    value_color = "#000";
                    break;

                default:
                    value_color = "grey";
                    break;
            }
        }

        buffer_svg(w->response.data, label, rc->value * multiply / divide, units, label_color, value_color, 0, precision);
        ret = 200;
    }
    else {
        time_t latest_timestamp = 0;
        int value_is_null = 1;
        calculated_number n = 0;
        ret = 500;

        // if the collected value is too old, don't calculate its value
        if (rrdset_last_entry_t(st) >= (time(NULL) - (st->update_every * st->gap_when_lost_iterations_above)))
            ret = rrd2value(st, w->response.data, &n, (dimensions) ? buffer_tostring(dimensions) : NULL, points, after,
                            before, group, options, NULL, &latest_timestamp, &value_is_null);

        // if the value cannot be calculated, show empty badge
        if (ret != 200) {
            value_is_null = 1;
            n = 0;
            ret = 200;
        }
        else if (refresh > 0)
            buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh);

        // render the badge
        buffer_svg(w->response.data, label, n * multiply / divide, units, label_color, value_color, value_is_null,
                   precision);
    }

cleanup:
    if(dimensions)
        buffer_free(dimensions);
    return ret;
}

// returns the HTTP code
int web_client_api_request_v1_data(struct web_client *w, char *url)
{
    debug(D_WEB_CLIENT, "%llu: API v1 data with URL '%s'", w->id, url);

    int ret = 400;
    BUFFER *dimensions = NULL;

    buffer_flush(w->response.data);

    char    *google_version = "0.6",
            *google_reqId = "0",
            *google_sig = "0",
            *google_out = "json",
            *responseHandler = NULL,
            *outFileName = NULL;

    time_t last_timestamp_in_data = 0, google_timestamp = 0;

    char *chart = NULL
            , *before_str = NULL
            , *after_str = NULL
            , *points_str = NULL;

    int group = GROUP_AVERAGE;
    uint32_t format = DATASOURCE_JSON;
    uint32_t options = 0x00000000;

    while(url) {
        char *value = mystrsep(&url, "?&[]");
        if(!value || !*value) continue;

        char *name = mystrsep(&value, "=");
        if(!name || !*name) continue;
        if(!value || !*value) continue;

        debug(D_WEB_CLIENT, "%llu: API v1 data query param '%s' with value '%s'", w->id, name, value);

        // name and value are now the parameters
        // they are not null and not empty

        if(!strcmp(name, "chart")) chart = value;
        else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
            if(!dimensions) dimensions = buffer_create(100);
            buffer_strcat(dimensions, "|");
            buffer_strcat(dimensions, value);
        }
        else if(!strcmp(name, "after")) after_str = value;
        else if(!strcmp(name, "before")) before_str = value;
        else if(!strcmp(name, "points")) points_str = value;
        else if(!strcmp(name, "group")) {
            group = web_client_api_request_v1_data_group(value, GROUP_AVERAGE);
        }
        else if(!strcmp(name, "format")) {
            format = web_client_api_request_v1_data_format(value);
        }
        else if(!strcmp(name, "options")) {
            options |= web_client_api_request_v1_data_options(value);
        }
        else if(!strcmp(name, "callback")) {
            responseHandler = value;
        }
        else if(!strcmp(name, "filename")) {
            outFileName = value;
        }
        else if(!strcmp(name, "tqx")) {
            // parse Google Visualization API options
            // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source
            char *tqx_name, *tqx_value;

            while(value) {
                tqx_value = mystrsep(&value, ";");
                if(!tqx_value || !*tqx_value) continue;

                tqx_name = mystrsep(&tqx_value, ":");
                if(!tqx_name || !*tqx_name) continue;
                if(!tqx_value || !*tqx_value) continue;

                if(!strcmp(tqx_name, "version"))
                    google_version = tqx_value;
                else if(!strcmp(tqx_name, "reqId"))
                    google_reqId = tqx_value;
                else if(!strcmp(tqx_name, "sig")) {
                    google_sig = tqx_value;
                    google_timestamp = strtoul(google_sig, NULL, 0);
                }
                else if(!strcmp(tqx_name, "out")) {
                    google_out = tqx_value;
                    format = web_client_api_request_v1_data_google_format(google_out);
                }
                else if(!strcmp(tqx_name, "responseHandler"))
                    responseHandler = tqx_value;
                else if(!strcmp(tqx_name, "outFileName"))
                    outFileName = tqx_value;
            }
        }
    }

    if(!chart || !*chart) {
        buffer_sprintf(w->response.data, "No chart id is given at the request.");
        goto cleanup;
    }

    RRDSET *st = rrdset_find(chart);
    if(!st) st = rrdset_find_byname(chart);
    if(!st) {
        buffer_sprintf(w->response.data, "Chart '%s' is not found.", chart);
        ret = 404;
        goto cleanup;
    }

    long long before = (before_str && *before_str)?atol(before_str):0;
    long long after  = (after_str  && *after_str) ?atol(after_str):0;
    int       points = (points_str && *points_str)?atoi(points_str):0;

    debug(D_WEB_CLIENT, "%llu: API command 'data' for chart '%s', dimensions '%s', after '%lld', before '%lld', points '%d', group '%d', format '%u', options '0x%08x'"
            , w->id
            , chart
            , (dimensions)?buffer_tostring(dimensions):""
            , after
            , before
            , points
            , group
            , format
            , options
            );

    if(outFileName && *outFileName) {
        buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
        debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName);
    }

    if(format == DATASOURCE_DATATABLE_JSONP) {
        if(responseHandler == NULL)
            responseHandler = "google.visualization.Query.setResponse";

        debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
                w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName
            );

        buffer_sprintf(w->response.data,
            "%s({version:'%s',reqId:'%s',status:'ok',sig:'%ld',table:",
            responseHandler, google_version, google_reqId, st->last_updated.tv_sec);
    }
    else if(format == DATASOURCE_JSONP) {
        if(responseHandler == NULL)
            responseHandler = "callback";

        buffer_strcat(w->response.data, responseHandler);
        buffer_strcat(w->response.data, "(");
    }

    ret = rrd2format(st, w->response.data, dimensions, format, points, after, before, group, options, &last_timestamp_in_data);

    if(format == DATASOURCE_DATATABLE_JSONP) {
        if(google_timestamp < last_timestamp_in_data)
            buffer_strcat(w->response.data, "});");

        else {
            // the client already has the latest data
            buffer_flush(w->response.data);
            buffer_sprintf(w->response.data,
                "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
                responseHandler, google_version, google_reqId);
        }
    }
    else if(format == DATASOURCE_JSONP)
        buffer_strcat(w->response.data, ");");

cleanup:
    if(dimensions) buffer_free(dimensions);
    return ret;
}

int web_client_api_request_v1_registry(struct web_client *w, char *url)
{
    static uint32_t hash_action = 0, hash_access = 0, hash_hello = 0, hash_delete = 0, hash_search = 0,
            hash_switch = 0, hash_machine = 0, hash_url = 0, hash_name = 0, hash_delete_url = 0, hash_for = 0,
            hash_to = 0 /*, hash_redirects = 0 */;

    if(unlikely(!hash_action)) {
        hash_action = simple_hash("action");
        hash_access = simple_hash("access");
        hash_hello = simple_hash("hello");
        hash_delete = simple_hash("delete");
        hash_search = simple_hash("search");
        hash_switch = simple_hash("switch");
        hash_machine = simple_hash("machine");
        hash_url = simple_hash("url");
        hash_name = simple_hash("name");
        hash_delete_url = simple_hash("delete_url");
        hash_for = simple_hash("for");
        hash_to = simple_hash("to");
/*
        hash_redirects = simple_hash("redirects");
*/
    }

    char person_guid[36 + 1] = "";

    debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url);

    // FIXME
    // The browser may send multiple cookies with our id
    
    char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "=");
    if(cookie)
        strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], 36);

    char action = '\0';
    char *machine_guid = NULL,
            *machine_url = NULL,
            *url_name = NULL,
            *search_machine_guid = NULL,
            *delete_url = NULL,
            *to_person_guid = NULL;
/*
    int redirects = 0;
*/

    while(url) {
        char *value = mystrsep(&url, "?&[]");
        if (!value || !*value) continue;

        char *name = mystrsep(&value, "=");
        if (!name || !*name) continue;
        if (!value || !*value) continue;

        debug(D_WEB_CLIENT, "%llu: API v1 registry query param '%s' with value '%s'", w->id, name, value);

        uint32_t hash = simple_hash(name);

        if(hash == hash_action && !strcmp(name, "action")) {
            uint32_t vhash = simple_hash(value);

            if(vhash == hash_access && !strcmp(value, "access")) action = 'A';
            else if(vhash == hash_hello && !strcmp(value, "hello")) action = 'H';
            else if(vhash == hash_delete && !strcmp(value, "delete")) action = 'D';
            else if(vhash == hash_search && !strcmp(value, "search")) action = 'S';
            else if(vhash == hash_switch && !strcmp(value, "switch")) action = 'W';
#ifdef NETDATA_INTERNAL_CHECKS
            else error("unknown registry action '%s'", value);
#endif /* NETDATA_INTERNAL_CHECKS */
        }
/*
        else if(hash == hash_redirects && !strcmp(name, "redirects"))
            redirects = atoi(value);
*/
        else if(hash == hash_machine && !strcmp(name, "machine"))
            machine_guid = value;

        else if(hash == hash_url && !strcmp(name, "url"))
            machine_url = value;

        else if(action == 'A') {
            if(hash == hash_name && !strcmp(name, "name"))
                url_name = value;
        }
        else if(action == 'D') {
            if(hash == hash_delete_url && !strcmp(name, "delete_url"))
                delete_url = value;
        }
        else if(action == 'S') {
            if(hash == hash_for && !strcmp(name, "for"))
                search_machine_guid = value;
        }
        else if(action == 'W') {
            if(hash == hash_to && !strcmp(name, "to"))
                to_person_guid = value;
        }
#ifdef NETDATA_INTERNAL_CHECKS
        else error("unused registry URL parameter '%s' with value '%s'", name, value);
#endif /* NETDATA_INTERNAL_CHECKS */
    }

    if(web_donotrack_comply && w->donottrack) {
        buffer_flush(w->response.data);
        buffer_sprintf(w->response.data, "Your web browser is sending 'DNT: 1' (Do Not Track). The registry requires persistent cookies on your browser to work.");
        return 400;
    }

    if(action == 'A' && (!machine_guid || !machine_url || !url_name)) {
        buffer_flush(w->response.data);
        buffer_sprintf(w->response.data, "Invalid registry request - access requires these parameters: machine ('%s'), url ('%s'), name ('%s')",
                       machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", url_name?url_name:"UNSET");
        return 400;
    }
    else if(action == 'D' && (!machine_guid || !machine_url || !delete_url)) {
        buffer_flush(w->response.data);
        buffer_sprintf(w->response.data, "Invalid registry request - delete requires these parameters: machine ('%s'), url ('%s'), delete_url ('%s')",
                       machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", delete_url?delete_url:"UNSET");
        return 400;
    }
    else if(action == 'S' && (!machine_guid || !machine_url || !search_machine_guid)) {
        buffer_flush(w->response.data);
        buffer_sprintf(w->response.data, "Invalid registry request - search requires these parameters: machine ('%s'), url ('%s'), for ('%s')",
                       machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", search_machine_guid?search_machine_guid:"UNSET");
        return 400;
    }
    else if(action == 'W' && (!machine_guid || !machine_url || !to_person_guid)) {
        buffer_flush(w->response.data);
        buffer_sprintf(w->response.data, "Invalid registry request - switching identity requires these parameters: machine ('%s'), url ('%s'), to ('%s')",
                       machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", to_person_guid?to_person_guid:"UNSET");
        return 400;
    }

    switch(action) {
        case 'A':
            w->tracking_required = 1;
            if(registry_verify_cookies_redirects() > 0 && (!cookie || !person_guid[0])) {
                buffer_flush(w->response.data);

                registry_set_cookie(w, "give-me-back-this-cookie-please");
                w->response.data->contenttype = CT_APPLICATION_JSON;
                buffer_sprintf(w->response.data, "{ \"status\": \"redirect\", \"registry\": \"%s\" }", registry_to_announce());
                return 200;

/*
 * it seems that web browsers are ignoring 307 (Moved Temporarily)
 * under certain conditions, when using CORS
 * so this is commented and we use application level redirects instead
 *
                redirects++;

                if(redirects > registry_verify_cookies_redirects()) {
                    buffer_flush(w->response.data);
                    buffer_sprintf(w->response.data, "Your browser does not support cookies");
                    return 400;
                }

                char *encoded_url = url_encode(machine_url);
                if(!encoded_url) {
                    error("%llu: Cannot URL encode string '%s'", w->id, machine_url);
                    return 500;
                }

                char *encoded_name = url_encode(url_name);
                if(!encoded_name) {
                    free(encoded_url);
                    error("%llu: Cannot URL encode string '%s'", w->id, url_name);
                    return 500;
                }

                char *encoded_guid = url_encode(machine_guid);
                if(!encoded_guid) {
                    free(encoded_url);
                    free(encoded_name);
                    error("%llu: Cannot URL encode string '%s'", w->id, machine_guid);
                    return 500;
                }

                buffer_sprintf(w->response.header, "Location: %s/api/v1/registry?action=access&machine=%s&name=%s&url=%s&redirects=%d\r\n",
                               registry_to_announce(), encoded_guid, encoded_name, encoded_url, redirects);

                free(encoded_guid);
                free(encoded_name);
                free(encoded_url);
                return 307
*/
            }
            return registry_request_access_json(w, person_guid, machine_guid, machine_url, url_name, time(NULL));

        case 'D':
            w->tracking_required = 1;
            return registry_request_delete_json(w, person_guid, machine_guid, machine_url, delete_url, time(NULL));

        case 'S':
            w->tracking_required = 1;
            return registry_request_search_json(w, person_guid, machine_guid, machine_url, search_machine_guid, time(NULL));

        case 'W':
            w->tracking_required = 1;
            return registry_request_switch_json(w, person_guid, machine_guid, machine_url, to_person_guid, time(NULL));

        case 'H':
            return registry_request_hello_json(w);

        default:
            buffer_flush(w->response.data);
            buffer_strcat(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
            return 400;
    }
}

int web_client_api_request_v1(struct web_client *w, char *url) {
    static uint32_t hash_data = 0, hash_chart = 0, hash_charts = 0, hash_registry = 0, hash_badge = 0, hash_alarms = 0, hash_alarm_log = 0;

    if(unlikely(hash_data == 0)) {
        hash_data = simple_hash("data");
        hash_chart = simple_hash("chart");
        hash_charts = simple_hash("charts");
        hash_registry = simple_hash("registry");
        hash_badge = simple_hash("badge.svg");
        hash_alarms = simple_hash("alarms");
        hash_alarm_log = simple_hash("alarm_log");
    }

    // get the command
    char *tok = mystrsep(&url, "/?&");
    if(tok && *tok) {
        debug(D_WEB_CLIENT, "%llu: Searching for API v1 command '%s'.", w->id, tok);
        uint32_t hash = simple_hash(tok);

        if(hash == hash_data && !strcmp(tok, "data"))
            return web_client_api_request_v1_data(w, url);

        else if(hash == hash_chart && !strcmp(tok, "chart"))
            return web_client_api_request_v1_chart(w, url);

        else if(hash == hash_charts && !strcmp(tok, "charts"))
            return web_client_api_request_v1_charts(w, url);

        else if(hash == hash_registry && !strcmp(tok, "registry"))
            return web_client_api_request_v1_registry(w, url);

        else if(hash == hash_badge && !strcmp(tok, "badge.svg"))
            return web_client_api_request_v1_badge(w, url);

        else if(hash == hash_alarms && !strcmp(tok, "alarms"))
            return web_client_api_request_v1_alarms(w, url);

        else if(hash == hash_alarm_log && !strcmp(tok, "alarm_log"))
            return web_client_api_request_v1_alarm_log(w, url);

        else {
            buffer_flush(w->response.data);
            buffer_sprintf(w->response.data, "Unsupported v1 API command: %s", tok);
            return 404;
        }
    }
    else {
        buffer_flush(w->response.data);
        buffer_sprintf(w->response.data, "API v1 command?");
        return 400;
    }
}

int web_client_api_request(struct web_client *w, char *url)
{
    // get the api version
    char *tok = mystrsep(&url, "/?&");
    if(tok && *tok) {
        debug(D_WEB_CLIENT, "%llu: Searching for API version '%s'.", w->id, tok);
        if(strcmp(tok, "v1") == 0)
            return web_client_api_request_v1(w, url);
        else {
            buffer_flush(w->response.data);
            buffer_sprintf(w->response.data, "Unsupported API version: %s", tok);
            return 404;
        }
    }
    else {
        buffer_flush(w->response.data);
        buffer_sprintf(w->response.data, "Which API version?");
        return 400;
    }
}

int web_client_api_old_data_request(struct web_client *w, char *url, int datasource_type)
{
    RRDSET *st = NULL;

    char *args = strchr(url, '?');
    if(args) {
        *args='\0';
        args = &args[1];
    }

    // get the name of the data to show
    char *tok = mystrsep(&url, "/");
    if(!tok) tok = "";

    // do we have such a data set?
    if(*tok) {
        debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
        st = rrdset_find_byname(tok);
        if(!st) st = rrdset_find(tok);
    }

    if(!st) {
        // we don't have it
        // try to send a file with that name
        buffer_flush(w->response.data);
        return(mysendfile(w, tok));
    }

    // we have it
    debug(D_WEB_CLIENT, "%llu: Found RRD data with name '%s'.", w->id, tok);

    // how many entries does the client want?
    int lines = rrd_default_history_entries;
    int group_count = 1;
    time_t after = 0, before = 0;
    int group_method = GROUP_AVERAGE;
    int nonzero = 0;

    if(url) {
        // parse the lines required
        tok = mystrsep(&url, "/");
        if(tok) lines = atoi(tok);
        if(lines < 1) lines = 1;
    }
    if(url) {
        // parse the group count required
        tok = mystrsep(&url, "/");
        if(tok && *tok) group_count = atoi(tok);
        if(group_count < 1) group_count = 1;
        //if(group_count > save_history / 20) group_count = save_history / 20;
    }
    if(url) {
        // parse the grouping method required
        tok = mystrsep(&url, "/");
        if(tok && *tok) {
            if(strcmp(tok, "max") == 0) group_method = GROUP_MAX;
            else if(strcmp(tok, "average") == 0) group_method = GROUP_AVERAGE;
            else if(strcmp(tok, "sum") == 0) group_method = GROUP_SUM;
            else debug(D_WEB_CLIENT, "%llu: Unknown group method '%s'", w->id, tok);
        }
    }
    if(url) {
        // parse after time
        tok = mystrsep(&url, "/");
        if(tok && *tok) after = strtoul(tok, NULL, 10);
        if(after < 0) after = 0;
    }
    if(url) {
        // parse before time
        tok = mystrsep(&url, "/");
        if(tok && *tok) before = strtoul(tok, NULL, 10);
        if(before < 0) before = 0;
    }
    if(url) {
        // parse nonzero
        tok = mystrsep(&url, "/");
        if(tok && *tok && strcmp(tok, "nonzero") == 0) nonzero = 1;
    }

    w->response.data->contenttype = CT_APPLICATION_JSON;
    buffer_flush(w->response.data);

    char *google_version = "0.6";
    char *google_reqId = "0";
    char *google_sig = "0";
    char *google_out = "json";
    char *google_responseHandler = "google.visualization.Query.setResponse";
    char *google_outFileName = NULL;
    time_t last_timestamp_in_data = 0;
    if(datasource_type == DATASOURCE_DATATABLE_JSON || datasource_type == DATASOURCE_DATATABLE_JSONP) {

        w->response.data->contenttype = CT_APPLICATION_X_JAVASCRIPT;

        while(args) {
            tok = mystrsep(&args, "&");
            if(tok && *tok) {
                char *name = mystrsep(&tok, "=");
                if(name && *name && strcmp(name, "tqx") == 0) {
                    char *key = mystrsep(&tok, ":");
                    char *value = mystrsep(&tok, ";");
                    if(key && value && *key && *value) {
                        if(strcmp(key, "version") == 0)
                            google_version = value;

                        else if(strcmp(key, "reqId") == 0)
                            google_reqId = value;

                        else if(strcmp(key, "sig") == 0)
                            google_sig = value;

                        else if(strcmp(key, "out") == 0)
                            google_out = value;

                        else if(strcmp(key, "responseHandler") == 0)
                            google_responseHandler = value;

                        else if(strcmp(key, "outFileName") == 0)
                            google_outFileName = value;
                    }
                }
            }
        }

        debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
            w->id, google_version, google_reqId, google_sig, google_out, google_responseHandler, google_outFileName
            );

        if(datasource_type == DATASOURCE_DATATABLE_JSONP) {
            last_timestamp_in_data = strtoul(google_sig, NULL, 0);

            // check the client wants json
            if(strcmp(google_out, "json") != 0) {
                buffer_sprintf(w->response.data,
                    "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'invalid_query',message:'output format is not supported',detailed_message:'the format %s requested is not supported by netdata.'}]});",
                    google_responseHandler, google_version, google_reqId, google_out);
                    return 200;
            }
        }
    }

    if(datasource_type == DATASOURCE_DATATABLE_JSONP) {
        buffer_sprintf(w->response.data,
            "%s({version:'%s',reqId:'%s',status:'ok',sig:'%ld',table:",
            google_responseHandler, google_version, google_reqId, st->last_updated.tv_sec);
    }

    debug(D_WEB_CLIENT_ACCESS, "%llu: Sending RRD data '%s' (id %s, %d lines, %d group, %d group_method, %ld after, %ld before).",
        w->id, st->name, st->id, lines, group_count, group_method, after, before);

    time_t timestamp_in_data = rrd_stats_json(datasource_type, st, w->response.data, lines, group_count, group_method, (unsigned long)after, (unsigned long)before, nonzero);

    if(datasource_type == DATASOURCE_DATATABLE_JSONP) {
        if(timestamp_in_data > last_timestamp_in_data)
            buffer_strcat(w->response.data, "});");

        else {
            // the client already has the latest data
            buffer_flush(w->response.data);
            buffer_sprintf(w->response.data,
                "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
                google_responseHandler, google_version, google_reqId);
        }
    }

    return 200;
}

const char *web_content_type_to_string(uint8_t contenttype) {
    switch(contenttype) {
        case CT_TEXT_HTML:
            return "text/html; charset=utf-8";

        case CT_APPLICATION_XML:
            return "application/xml; charset=utf-8";

        case CT_APPLICATION_JSON:
            return "application/json; charset=utf-8";

        case CT_APPLICATION_X_JAVASCRIPT:
            return "application/x-javascript; charset=utf-8";

        case CT_TEXT_CSS:
            return "text/css; charset=utf-8";

        case CT_TEXT_XML:
            return "text/xml; charset=utf-8";

        case CT_TEXT_XSL:
            return "text/xsl; charset=utf-8";

        case CT_APPLICATION_OCTET_STREAM:
            return "application/octet-stream";

        case CT_IMAGE_SVG_XML:
            return "image/svg+xml";

        case CT_APPLICATION_X_FONT_TRUETYPE:
            return "application/x-font-truetype";

        case CT_APPLICATION_X_FONT_OPENTYPE:
            return "application/x-font-opentype";

        case CT_APPLICATION_FONT_WOFF:
            return "application/font-woff";

        case CT_APPLICATION_FONT_WOFF2:
            return "application/font-woff2";

        case CT_APPLICATION_VND_MS_FONTOBJ:
            return "application/vnd.ms-fontobject";

        case CT_IMAGE_PNG:
            return "image/png";

        case CT_IMAGE_JPG:
            return "image/jpeg";

        case CT_IMAGE_GIF:
            return "image/gif";

        case CT_IMAGE_XICON:
            return "image/x-icon";

        case CT_IMAGE_BMP:
            return "image/bmp";

        case CT_IMAGE_ICNS:
            return "image/icns";

        default:
        case CT_TEXT_PLAIN:
            return "text/plain; charset=utf-8";
    }
}


const char *web_response_code_to_string(int code) {
    switch(code) {
        case 200:
            return "OK";

        case 307:
            return "Temporary Redirect";

        case 400:
            return "Bad Request";

        case 403:
            return "Forbidden";

        case 404:
            return "Not Found";

        case 412:
            return "Preconditions Failed";

        default:
            if(code >= 100 && code < 200)
                return "Informational";

            if(code >= 200 && code < 300)
                return "Successful";

            if(code >= 300 && code < 400)
                return "Redirection";

            if(code >= 400 && code < 500)
                return "Bad Request";

            if(code >= 500 && code < 600)
                return "Server Error";

            return "Undefined Error";
    }
}

static inline char *http_header_parse(struct web_client *w, char *s) {
    static uint32_t hash_origin = 0, hash_connection = 0, hash_accept_encoding = 0, hash_donottrack = 0;

    if(unlikely(!hash_origin)) {
        hash_origin = simple_uhash("Origin");
        hash_connection = simple_uhash("Connection");
        hash_accept_encoding = simple_uhash("Accept-Encoding");
        hash_donottrack = simple_uhash("DNT");
    }

    char *e = s;

    // find the :
    while(*e && *e != ':') e++;
    if(!*e) return e;

    // get the name
    *e = '\0';

    // find the value
    char *v = e + 1, *ve;

    // skip leading spaces from value
    while(*v == ' ') v++;
    ve = v;

    // find the \r
    while(*ve && *ve != '\r') ve++;
    if(!*ve || ve[1] != '\n') {
        *e = ':';
        return ve;
    }

    // terminate the value
    *ve = '\0';

    // fprintf(stderr, "HEADER: '%s' = '%s'\n", s, v);
    uint32_t hash = simple_uhash(s);

    if(hash == hash_origin && !strcasecmp(s, "Origin"))
        strncpyz(w->origin, v, ORIGIN_MAX);

    else if(hash == hash_connection && !strcasecmp(s, "Connection")) {
        if(strcasestr(v, "keep-alive"))
            w->keepalive = 1;
    }
    else if(web_donotrack_comply && hash == hash_donottrack && !strcasecmp(s, "DNT")) {
        if(*v == '0') w->donottrack = 0;
        else if(*v == '1') w->donottrack = 1;
    }
#ifdef NETDATA_WITH_ZLIB
    else if(hash == hash_accept_encoding && !strcasecmp(s, "Accept-Encoding")) {
        if(web_enable_gzip) {
            if(strcasestr(v, "gzip"))
                web_client_enable_deflate(w, 1);
            //
            // does not seem to work
            // else if(strcasestr(v, "deflate"))
            //  web_client_enable_deflate(w, 0);
        }
    }
#endif /* NETDATA_WITH_ZLIB */

    *e = ':';
    *ve = '\r';
    return ve;
}

// http_request_validate()
// returns:
// = 0 : all good, process the request
// > 0 : request is not supported
// < 0 : request is incomplete - wait for more data

static inline int http_request_validate(struct web_client *w) {
    char *s = w->response.data->buffer, *encoded_url = NULL;

    // is is a valid request?
    if(!strncmp(s, "GET ", 4)) {
        encoded_url = s = &s[4];
        w->mode = WEB_CLIENT_MODE_NORMAL;
    }
    else if(!strncmp(s, "OPTIONS ", 8)) {
        encoded_url = s = &s[8];
        w->mode = WEB_CLIENT_MODE_OPTIONS;
    }
    else {
        w->wait_receive = 0;
        return 1;
    }

    // find the SPACE + "HTTP/"
    while(*s) {
        // find the next space
        while (*s && *s != ' ') s++;

        // is it SPACE + "HTTP/" ?
        if(*s && !strncmp(s, " HTTP/", 6)) break;
        else s++;
    }

    // incomplete requests
    if(unlikely(!*s)) {
        w->wait_receive = 1;
        return -2;
    }

    // we have the end of encoded_url - remember it
    char *ue = s;

    // make sure we have complete request
    // complete requests contain: \r\n\r\n
    while(*s) {
        // find a line feed
        while(*s && *s++ != '\r');

        // did we reach the end?
        if(unlikely(!*s)) break;

        // is it \r\n ?
        if(likely(*s++ == '\n')) {

            // is it again \r\n ? (header end)
            if(unlikely(*s == '\r' && s[1] == '\n')) {
                // a valid complete HTTP request found

                *ue = '\0';
                url_decode_r(w->decoded_url, encoded_url, URL_MAX + 1);
                *ue = ' ';
                
                // copy the URL - we are going to overwrite parts of it
                // FIXME -- we should avoid it
                strncpyz(w->last_url, w->decoded_url, URL_MAX);

                w->wait_receive = 0;
                return 0;
            }

            // another header line
            s = http_header_parse(w, s);
        }
    }

    // incomplete request
    w->wait_receive = 1;
    return -3;
}

void web_client_process(struct web_client *w) {
    static uint32_t hash_api = 0, hash_netdata_conf = 0, hash_data = 0, hash_datasource = 0, hash_graph = 0,
            hash_list = 0, hash_all_json = 0, hash_exit = 0, hash_debug = 0, hash_mirror = 0;

    // start timing us
    gettimeofday(&w->tv_in, NULL);

    if(unlikely(!hash_api)) {
        hash_api = simple_hash("api");
        hash_netdata_conf = simple_hash("netdata.conf");
        hash_data = simple_hash(WEB_PATH_DATA);
        hash_datasource = simple_hash(WEB_PATH_DATASOURCE);
        hash_graph = simple_hash(WEB_PATH_GRAPH);
        hash_list = simple_hash("list");
        hash_all_json = simple_hash("all.json");
        hash_exit = simple_hash("exit");
        hash_debug = simple_hash("debug");
        hash_mirror = simple_hash("mirror");
    }

    int code = 500;
    ssize_t bytes;

    int what_to_do = http_request_validate(w);

    // wait for more data
    if(what_to_do < 0) {
        if(w->response.data->len > TOO_BIG_REQUEST) {
            strcpy(w->last_url, "too big request");

            debug(D_WEB_CLIENT_ACCESS, "%llu: Received request is too big (%zu bytes).", w->id, w->response.data->len);

            code = 400;
            buffer_flush(w->response.data);
            buffer_sprintf(w->response.data, "Received request is too big  (%zu bytes).\r\n", w->response.data->len);
        }
        else {
            // wait for more data
            return;
        }
    }
    else if(what_to_do > 0) {
        // strcpy(w->last_url, "not a valid request");

        debug(D_WEB_CLIENT_ACCESS, "%llu: Cannot understand '%s'.", w->id, w->response.data->buffer);

        code = 500;
        buffer_flush(w->response.data);
        buffer_strcat(w->response.data, "I don't understand you...\r\n");
    }
    else { // what_to_do == 0
        if(w->mode == WEB_CLIENT_MODE_OPTIONS) {
            code = 200;
            w->response.data->contenttype = CT_TEXT_PLAIN;
            buffer_flush(w->response.data);
            buffer_strcat(w->response.data, "OK");
        }
        else {
            char *url = w->decoded_url;
            char *tok = mystrsep(&url, "/?");
            if(tok && *tok) {
                uint32_t hash = simple_hash(tok);
                debug(D_WEB_CLIENT, "%llu: Processing command '%s'.", w->id, tok);

                if(hash == hash_api && strcmp(tok, "api") == 0) {
                    // the client is requesting api access
                    code = web_client_api_request(w, url);
                }
                else if(hash == hash_netdata_conf && strcmp(tok, "netdata.conf") == 0) {
                    code = 200;
                    debug(D_WEB_CLIENT_ACCESS, "%llu: Sending netdata.conf ...", w->id);

                    w->response.data->contenttype = CT_TEXT_PLAIN;
                    buffer_flush(w->response.data);
                    generate_config(w->response.data, 0);
                }
                else if(hash == hash_data && strcmp(tok, WEB_PATH_DATA) == 0) { // "data"
                    // the client is requesting rrd data -- OLD API
                    code = web_client_api_old_data_request(w, url, DATASOURCE_JSON);
                }
                else if(hash == hash_datasource && strcmp(tok, WEB_PATH_DATASOURCE) == 0) { // "datasource"
                    // the client is requesting google datasource -- OLD API
                    code = web_client_api_old_data_request(w, url, DATASOURCE_DATATABLE_JSONP);
                }
                else if(hash == hash_graph && strcmp(tok, WEB_PATH_GRAPH) == 0) { // "graph"
                    // the client is requesting an rrd graph -- OLD API

                    // get the name of the data to show
                    tok = mystrsep(&url, "/?&");
                    if(tok && *tok) {
                        debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);

                        // do we have such a data set?
                        RRDSET *st = rrdset_find_byname(tok);
                        if(!st) st = rrdset_find(tok);
                        if(!st) {
                            // we don't have it
                            // try to send a file with that name
                            buffer_flush(w->response.data);
                            code = mysendfile(w, tok);
                        }
                        else {
                            code = 200;
                            debug(D_WEB_CLIENT_ACCESS, "%llu: Sending %s.json of RRD_STATS...", w->id, st->name);
                            w->response.data->contenttype = CT_APPLICATION_JSON;
                            buffer_flush(w->response.data);
                            rrd_stats_graph_json(st, url, w->response.data);
                        }
                    }
                    else {
                        code = 400;
                        buffer_flush(w->response.data);
                        buffer_strcat(w->response.data, "Graph name?\r\n");
                    }
                }
                else if(hash == hash_list && strcmp(tok, "list") == 0) {
                    // OLD API
                    code = 200;

                    debug(D_WEB_CLIENT_ACCESS, "%llu: Sending list of RRD_STATS...", w->id);

                    buffer_flush(w->response.data);
                    RRDSET *st = localhost.rrdset_root;

                    for ( ; st ; st = st->next )
                        buffer_sprintf(w->response.data, "%s\n", st->name);
                }
                else if(hash == hash_all_json && strcmp(tok, "all.json") == 0) {
                    // OLD API
                    code = 200;
                    debug(D_WEB_CLIENT_ACCESS, "%llu: Sending JSON list of all monitors of RRD_STATS...", w->id);

                    w->response.data->contenttype = CT_APPLICATION_JSON;
                    buffer_flush(w->response.data);
                    rrd_stats_all_json(w->response.data);
                }
#ifdef NETDATA_INTERNAL_CHECKS
                else if(hash == hash_exit && strcmp(tok, "exit") == 0) {
                    code = 200;
                    w->response.data->contenttype = CT_TEXT_PLAIN;
                    buffer_flush(w->response.data);

                    if(!netdata_exit)
                        buffer_strcat(w->response.data, "ok, will do...");
                    else
                        buffer_strcat(w->response.data, "I am doing it already");

                    error("web request to exit received.");
                    netdata_cleanup_and_exit(0);
                    netdata_exit = 1;
                }
                else if(hash == hash_debug && strcmp(tok, "debug") == 0) {
                    buffer_flush(w->response.data);

                    // get the name of the data to show
                    tok = mystrsep(&url, "/?&");
                    if(tok && *tok) {
                        debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);

                        // do we have such a data set?
                        RRDSET *st = rrdset_find_byname(tok);
                        if(!st) st = rrdset_find(tok);
                        if(!st) {
                            code = 404;
                            buffer_sprintf(w->response.data, "Chart %s is not found.\r\n", tok);
                            debug(D_WEB_CLIENT_ACCESS, "%llu: %s is not found.", w->id, tok);
                        }
                        else {
                            code = 200;
                            debug_flags |= D_RRD_STATS;
                            st->debug = !st->debug;
                            buffer_sprintf(w->response.data, "Chart %s has now debug %s.\r\n", tok, st->debug?"enabled":"disabled");
                            debug(D_WEB_CLIENT_ACCESS, "%llu: debug for %s is %s.", w->id, tok, st->debug?"enabled":"disabled");
                        }
                    }
                    else {
                        code = 500;
                        buffer_flush(w->response.data);
                        buffer_strcat(w->response.data, "debug which chart?\r\n");
                    }
                }
                else if(hash == hash_mirror && strcmp(tok, "mirror") == 0) {
                    code = 200;

                    debug(D_WEB_CLIENT_ACCESS, "%llu: Mirroring...", w->id);

                    // replace the zero bytes with spaces
                    buffer_char_replace(w->response.data, '\0', ' ');

                    // just leave the buffer as is
                    // it will be copied back to the client
                }
#endif  /* NETDATA_INTERNAL_CHECKS */
                else {
                    char filename[FILENAME_MAX+1];
                    url = filename;
                    strncpyz(filename, w->last_url, FILENAME_MAX);
                    tok = mystrsep(&url, "?");
                    buffer_flush(w->response.data);
                    code = mysendfile(w, (tok && *tok)?tok:"/");
                }
            }
            else {
                char filename[FILENAME_MAX+1];
                url = filename;
                strncpyz(filename, w->last_url, FILENAME_MAX);
                tok = mystrsep(&url, "?");
                buffer_flush(w->response.data);
                code = mysendfile(w, (tok && *tok)?tok:"/");
            }
        }
    }

    gettimeofday(&w->tv_ready, NULL);
    w->response.data->date = time(NULL);
    w->response.sent = 0;
    w->response.code = code;

    // prepare the HTTP response header
    debug(D_WEB_CLIENT, "%llu: Generating HTTP header with response %d.", w->id, code);

    const char *content_type_string = web_content_type_to_string(w->response.data->contenttype);
    const char *code_msg = web_response_code_to_string(code);

    char date[32];
    struct tm tmbuf, *tm = gmtime_r(&w->response.data->date, &tmbuf);
    strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %Z", tm);

    buffer_sprintf(w->response.header_output,
        "HTTP/1.1 %d %s\r\n"
        "Connection: %s\r\n"
        "Server: NetData Embedded HTTP Server\r\n"
        "Access-Control-Allow-Origin: %s\r\n"
        "Access-Control-Allow-Credentials: true\r\n"
        "Content-Type: %s\r\n"
        "Date: %s\r\n"
        , code, code_msg
        , w->keepalive?"keep-alive":"close"
        , w->origin
        , content_type_string
        , date
        );

    if(w->cookie1[0] || w->cookie2[0]) {
        if(w->cookie1[0]) {
            buffer_sprintf(w->response.header_output,
               "Set-Cookie: %s\r\n",
               w->cookie1);
        }

        if(w->cookie2[0]) {
            buffer_sprintf(w->response.header_output,
               "Set-Cookie: %s\r\n",
               w->cookie2);
        }

        if(web_donotrack_comply)
            buffer_sprintf(w->response.header_output,
               "Tk: T;cookies\r\n");
    }
    else {
        if(web_donotrack_comply) {
            if(w->tracking_required)
                buffer_sprintf(w->response.header_output,
                   "Tk: T;cookies\r\n");
            else
                buffer_sprintf(w->response.header_output,
                   "Tk: N\r\n");
        }
    }

    if(w->mode == WEB_CLIENT_MODE_OPTIONS) {
        buffer_strcat(w->response.header_output,
            "Access-Control-Allow-Methods: GET, OPTIONS\r\n"
            "Access-Control-Allow-Headers: accept, x-requested-with, origin, content-type, cookie\r\n"
            "Access-Control-Max-Age: 1209600\r\n" // 86400 * 14
            );
    }

    if(buffer_strlen(w->response.header))
        buffer_strcat(w->response.header_output, buffer_tostring(w->response.header));

    if(w->mode == WEB_CLIENT_MODE_NORMAL && (w->response.data->options & WB_CONTENT_NO_CACHEABLE)) {
        buffer_sprintf(w->response.header_output,
            "Expires: %s\r\n"
            "Cache-Control: no-cache\r\n"
            , date);
    }
    else if(w->mode != WEB_CLIENT_MODE_OPTIONS) {
        char edate[32];
        time_t et = w->response.data->date + (86400 * 14);
        struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf);
        strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm);

        buffer_sprintf(w->response.header_output,
            "Expires: %s\r\n"
            "Cache-Control: public\r\n"
            , edate);
    }

    // if we know the content length, put it
    if(!w->response.zoutput && (w->response.data->len || w->response.rlen))
        buffer_sprintf(w->response.header_output,
            "Content-Length: %zu\r\n"
            , w->response.data->len? w->response.data->len: w->response.rlen
            );
    else if(!w->response.zoutput)
        w->keepalive = 0;   // content-length is required for keep-alive

    if(w->response.zoutput) {
        buffer_strcat(w->response.header_output,
            "Content-Encoding: gzip\r\n"
            "Transfer-Encoding: chunked\r\n"
            );
    }

    buffer_strcat(w->response.header_output, "\r\n");

    // sent the HTTP header
    debug(D_WEB_DATA, "%llu: Sending response HTTP header of size %zu: '%s'"
            , w->id
            , buffer_strlen(w->response.header_output)
            , buffer_tostring(w->response.header_output)
            );

    web_client_crock_socket(w);

    bytes = send(w->ofd, buffer_tostring(w->response.header_output), buffer_strlen(w->response.header_output), 0);
    if(bytes != (ssize_t) buffer_strlen(w->response.header_output)) {
        if(bytes > 0)
            w->stats_sent_bytes += bytes;

        debug(D_WEB_CLIENT, "%llu: HTTP Header failed to be sent (I sent %zu bytes but the system sent %zd bytes). Closing web client."
            , w->id
            , buffer_strlen(w->response.header_output)
            , bytes);

        WEB_CLIENT_IS_DEAD(w);
        return;
    }
    else 
        w->stats_sent_bytes += bytes;

    // enable sending immediately if we have data
    if(w->response.data->len) w->wait_send = 1;
    else w->wait_send = 0;

    // pretty logging
    switch(w->mode) {
        case WEB_CLIENT_MODE_OPTIONS:
            debug(D_WEB_CLIENT, "%llu: Done preparing the OPTIONS response. Sending data (%zu bytes) to client.", w->id, w->response.data->len);
            break;

        case WEB_CLIENT_MODE_NORMAL:
            debug(D_WEB_CLIENT, "%llu: Done preparing the response. Sending data (%zu bytes) to client.", w->id, w->response.data->len);
            break;

        case WEB_CLIENT_MODE_FILECOPY:
            if(w->response.rlen) {
                debug(D_WEB_CLIENT, "%llu: Done preparing the response. Will be sending data file of %zu bytes to client.", w->id, w->response.rlen);
                w->wait_receive = 1;

                /*
                // utilize the kernel sendfile() for copying the file to the socket.
                // this block of code can be commented, without anything missing.
                // when it is commented, the program will copy the data using async I/O.
                {
                    long len = sendfile(w->ofd, w->ifd, NULL, w->response.data->rbytes);
                    if(len != w->response.data->rbytes)
                        error("%llu: sendfile() should copy %ld bytes, but copied %ld. Falling back to manual copy.", w->id, w->response.data->rbytes, len);
                    else
                        web_client_reset(w);
                }
                */
            }
            else
                debug(D_WEB_CLIENT, "%llu: Done preparing the response. Will be sending an unknown amount of bytes to client.", w->id);
            break;

        default:
            fatal("%llu: Unknown client mode %d.", w->id, w->mode);
            break;
    }
}

ssize_t web_client_send_chunk_header(struct web_client *w, size_t len)
{
    debug(D_DEFLATE, "%llu: OPEN CHUNK of %zu bytes (hex: %zx).", w->id, len, len);
    char buf[24];
    sprintf(buf, "%zX\r\n", len);
    
    ssize_t bytes = send(w->ofd, buf, strlen(buf), 0);
    if(bytes > 0) {
        debug(D_DEFLATE, "%llu: Sent chunk header %zd bytes.", w->id, bytes);
        w->stats_sent_bytes += bytes;
    }

    else if(bytes == 0) {
        debug(D_WEB_CLIENT, "%llu: Did not send chunk header to the client.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }
    else {
        debug(D_WEB_CLIENT, "%llu: Failed to send chunk header to client.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }

    return bytes;
}

ssize_t web_client_send_chunk_close(struct web_client *w)
{
    //debug(D_DEFLATE, "%llu: CLOSE CHUNK.", w->id);

    ssize_t bytes = send(w->ofd, "\r\n", 2, 0);
    if(bytes > 0) {
        debug(D_DEFLATE, "%llu: Sent chunk suffix %zd bytes.", w->id, bytes);
        w->stats_sent_bytes += bytes;
    }

    else if(bytes == 0) {
        debug(D_WEB_CLIENT, "%llu: Did not send chunk suffix to the client.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }
    else {
        debug(D_WEB_CLIENT, "%llu: Failed to send chunk suffix to client.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }

    return bytes;
}

ssize_t web_client_send_chunk_finalize(struct web_client *w)
{
    //debug(D_DEFLATE, "%llu: FINALIZE CHUNK.", w->id);

    ssize_t bytes = send(w->ofd, "\r\n0\r\n\r\n", 7, 0);
    if(bytes > 0) {
        debug(D_DEFLATE, "%llu: Sent chunk suffix %zd bytes.", w->id, bytes);
        w->stats_sent_bytes += bytes;
    }

    else if(bytes == 0) {
        debug(D_WEB_CLIENT, "%llu: Did not send chunk finalize suffix to the client.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }
    else {
        debug(D_WEB_CLIENT, "%llu: Failed to send chunk finalize suffix to client.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }

    return bytes;
}

#ifdef NETDATA_WITH_ZLIB
ssize_t web_client_send_deflate(struct web_client *w)
{
    ssize_t len = 0, t = 0;

    // when using compression,
    // w->response.sent is the amount of bytes passed through compression

    debug(D_DEFLATE, "%llu: web_client_send_deflate(): w->response.data->len = %zu, w->response.sent = %zu, w->response.zhave = %zu, w->response.zsent = %zu, w->response.zstream.avail_in = %u, w->response.zstream.avail_out = %u, w->response.zstream.total_in = %lu, w->response.zstream.total_out = %lu.",
        w->id, w->response.data->len, w->response.sent, w->response.zhave, w->response.zsent, w->response.zstream.avail_in, w->response.zstream.avail_out, w->response.zstream.total_in, w->response.zstream.total_out);

    if(w->response.data->len - w->response.sent == 0 && w->response.zstream.avail_in == 0 && w->response.zhave == w->response.zsent && w->response.zstream.avail_out != 0) {
        // there is nothing to send

        debug(D_WEB_CLIENT, "%llu: Out of output data.", w->id);

        // finalize the chunk
        if(w->response.sent != 0) {
            t = web_client_send_chunk_finalize(w);
            if(t < 0) return t;
        }

        if(w->mode == WEB_CLIENT_MODE_FILECOPY && w->wait_receive && w->response.rlen && w->response.rlen > w->response.data->len) {
            // we have to wait, more data will come
            debug(D_WEB_CLIENT, "%llu: Waiting for more data to become available.", w->id);
            w->wait_send = 0;
            return t;
        }

        if(unlikely(!w->keepalive)) {
            debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %zu bytes sent.", w->id, w->response.sent);
            WEB_CLIENT_IS_DEAD(w);
            return t;
        }

        // reset the client
        web_client_reset(w);
        debug(D_WEB_CLIENT, "%llu: Done sending all data on socket.", w->id);
        return t;
    }

    if(w->response.zhave == w->response.zsent) {
        // compress more input data

        // close the previous open chunk
        if(w->response.sent != 0) {
            t = web_client_send_chunk_close(w);
            if(t < 0) return t;
        }

        debug(D_DEFLATE, "%llu: Compressing %zu new bytes starting from %zu (and %u left behind).", w->id, (w->response.data->len - w->response.sent), w->response.sent, w->response.zstream.avail_in);

        // give the compressor all the data not passed through the compressor yet
        if(w->response.data->len > w->response.sent) {
            w->response.zstream.next_in = (Bytef *)&w->response.data->buffer[w->response.sent - w->response.zstream.avail_in];
            w->response.zstream.avail_in += (uInt) (w->response.data->len - w->response.sent);
        }

        // reset the compressor output buffer
        w->response.zstream.next_out = w->response.zbuffer;
        w->response.zstream.avail_out = ZLIB_CHUNK;

        // ask for FINISH if we have all the input
        int flush = Z_SYNC_FLUSH;
        if(w->mode == WEB_CLIENT_MODE_NORMAL
            || (w->mode == WEB_CLIENT_MODE_FILECOPY && !w->wait_receive && w->response.data->len == w->response.rlen)) {
            flush = Z_FINISH;
            debug(D_DEFLATE, "%llu: Requesting Z_FINISH, if possible.", w->id);
        }
        else {
            debug(D_DEFLATE, "%llu: Requesting Z_SYNC_FLUSH.", w->id);
        }

        // compress
        if(deflate(&w->response.zstream, flush) == Z_STREAM_ERROR) {
            error("%llu: Compression failed. Closing down client.", w->id);
            web_client_reset(w);
            return(-1);
        }

        w->response.zhave = ZLIB_CHUNK - w->response.zstream.avail_out;
        w->response.zsent = 0;

        // keep track of the bytes passed through the compressor
        w->response.sent = w->response.data->len;

        debug(D_DEFLATE, "%llu: Compression produced %zu bytes.", w->id, w->response.zhave);

        // open a new chunk
        ssize_t t2 = web_client_send_chunk_header(w, w->response.zhave);
        if(t2 < 0) return t2;
        t += t2;
    }
    
    debug(D_WEB_CLIENT, "%llu: Sending %zu bytes of data (+%zd of chunk header).", w->id, w->response.zhave - w->response.zsent, t);

    len = send(w->ofd, &w->response.zbuffer[w->response.zsent], (size_t) (w->response.zhave - w->response.zsent), MSG_DONTWAIT);
    if(len > 0) {
        w->stats_sent_bytes += len;
        w->response.zsent += len;
        len += t;
        debug(D_WEB_CLIENT, "%llu: Sent %zd bytes.", w->id, len);
    }
    else if(len == 0) {
        debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client (zhave = %zu, zsent = %zu, need to send = %zu).",
            w->id, w->response.zhave, w->response.zsent, w->response.zhave - w->response.zsent);

        WEB_CLIENT_IS_DEAD(w);
    }
    else {
        debug(D_WEB_CLIENT, "%llu: Failed to send data to client.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }

    return(len);
}
#endif // NETDATA_WITH_ZLIB

ssize_t web_client_send(struct web_client *w) {
#ifdef NETDATA_WITH_ZLIB
    if(likely(w->response.zoutput)) return web_client_send_deflate(w);
#endif // NETDATA_WITH_ZLIB

    ssize_t bytes;

    if(unlikely(w->response.data->len - w->response.sent == 0)) {
        // there is nothing to send

        debug(D_WEB_CLIENT, "%llu: Out of output data.", w->id);

        // there can be two cases for this
        // A. we have done everything
        // B. we temporarily have nothing to send, waiting for the buffer to be filled by ifd

        if(w->mode == WEB_CLIENT_MODE_FILECOPY && w->wait_receive && w->response.rlen && w->response.rlen > w->response.data->len) {
            // we have to wait, more data will come
            debug(D_WEB_CLIENT, "%llu: Waiting for more data to become available.", w->id);
            w->wait_send = 0;
            return 0;
        }

        if(unlikely(!w->keepalive)) {
            debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %zu bytes sent.", w->id, w->response.sent);
            WEB_CLIENT_IS_DEAD(w);
            return 0;
        }

        web_client_reset(w);
        debug(D_WEB_CLIENT, "%llu: Done sending all data on socket. Waiting for next request on the same socket.", w->id);
        return 0;
    }

    bytes = send(w->ofd, &w->response.data->buffer[w->response.sent], w->response.data->len - w->response.sent, MSG_DONTWAIT);
    if(likely(bytes > 0)) {
        w->stats_sent_bytes += bytes;
        w->response.sent += bytes;
        debug(D_WEB_CLIENT, "%llu: Sent %zd bytes.", w->id, bytes);
    }
    else if(likely(bytes == 0)) {
        debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }
    else {
        debug(D_WEB_CLIENT, "%llu: Failed to send data to client.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }

    return(bytes);
}

ssize_t web_client_receive(struct web_client *w)
{
    // do we have any space for more data?
    buffer_need_bytes(w->response.data, WEB_REQUEST_LENGTH);

    ssize_t left = w->response.data->size - w->response.data->len;
    ssize_t bytes;

    if(unlikely(w->mode == WEB_CLIENT_MODE_FILECOPY))
        bytes = read(w->ifd, &w->response.data->buffer[w->response.data->len], (size_t) (left - 1));
    else
        bytes = recv(w->ifd, &w->response.data->buffer[w->response.data->len], (size_t) (left - 1), MSG_DONTWAIT);

    if(likely(bytes > 0)) {
        if(w->mode != WEB_CLIENT_MODE_FILECOPY)
            w->stats_received_bytes += bytes;

        size_t old = w->response.data->len;
        w->response.data->len += bytes;
        w->response.data->buffer[w->response.data->len] = '\0';

        debug(D_WEB_CLIENT, "%llu: Received %zd bytes.", w->id, bytes);
        debug(D_WEB_DATA, "%llu: Received data: '%s'.", w->id, &w->response.data->buffer[old]);

        if(w->mode == WEB_CLIENT_MODE_FILECOPY) {
            w->wait_send = 1;

            if(w->response.rlen && w->response.data->len >= w->response.rlen)
                w->wait_receive = 0;
        }
    }
    else if(likely(bytes == 0)) {
        debug(D_WEB_CLIENT, "%llu: Out of input data.", w->id);

        // if we cannot read, it means we have an error on input.
        // if however, we are copying a file from ifd to ofd, we should not return an error.
        // in this case, the error should be generated when the file has been sent to the client.

        if(w->mode == WEB_CLIENT_MODE_FILECOPY) {
            // we are copying data from ifd to ofd
            // let it finish copying...
            w->wait_receive = 0;

            debug(D_WEB_CLIENT, "%llu: Read the whole file.", w->id);
            if(w->ifd != w->ofd) close(w->ifd);
            w->ifd = w->ofd;
        }
        else {
            debug(D_WEB_CLIENT, "%llu: failed to receive data.", w->id);
            WEB_CLIENT_IS_DEAD(w);
        }
    }
    else {
        debug(D_WEB_CLIENT, "%llu: receive data failed.", w->id);
        WEB_CLIENT_IS_DEAD(w);
    }

    return(bytes);
}


// --------------------------------------------------------------------------------------
// the thread of a single client

// 1. waits for input and output, using async I/O
// 2. it processes HTTP requests
// 3. it generates HTTP responses
// 4. it copies data from input to output if mode is FILECOPY

void *web_client_main(void *ptr)
{
    if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
        error("Cannot set pthread cancel type to DEFERRED.");

    if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
        error("Cannot set pthread cancel state to ENABLE.");

    struct web_client *w = ptr;
    struct pollfd fds[2], *ifd, *ofd;
    int retval, fdmax = 0, timeout;

    log_access("%llu: %s port %s connected on thread task id %d", w->id, w->client_ip, w->client_port, gettid());

    for(;;) {
        if(unlikely(w->dead)) {
            debug(D_WEB_CLIENT, "%llu: client is dead.", w->id);
            break;
        }
        else if(unlikely(!w->wait_receive && !w->wait_send)) {
            debug(D_WEB_CLIENT, "%llu: client is not set for neither receiving nor sending data.", w->id);
            break;
        }

        if(unlikely(w->ifd < 0 || w->ofd < 0)) {
            error("%llu: invalid file descriptor, ifd = %d, ofd = %d (required 0 <= fd", w->id, w->ifd, w->ofd);
            break;
        }

        if(w->ifd == w->ofd) {
            fds[0].fd = w->ifd;
            fds[0].events = 0;
            fds[0].revents = 0;

            if(w->wait_receive) fds[0].events |= POLLIN;
            if(w->wait_send)    fds[0].events |= POLLOUT;

            fds[1].fd = -1;
            fds[1].events = 0;
            fds[1].revents = 0;

            ifd = ofd = &fds[0];

            fdmax = 1;
        }
        else {
            fds[0].fd = w->ifd;
            fds[0].events = 0;
            fds[0].revents = 0;
            if(w->wait_receive) fds[0].events |= POLLIN;
            ifd = &fds[0];

            fds[1].fd = w->ofd;
            fds[1].events = 0;
            fds[1].revents = 0;
            if(w->wait_send)    fds[1].events |= POLLOUT;
            ofd = &fds[1];

            fdmax = 2;
        }

        debug(D_WEB_CLIENT, "%llu: Waiting socket async I/O for %s %s", w->id, w->wait_receive?"INPUT":"", w->wait_send?"OUTPUT":"");
        errno = 0;
        timeout = web_client_timeout * 1000;
        retval = poll(fds, fdmax, timeout);

        if(unlikely(retval == -1)) {
            if(errno == EAGAIN || errno == EINTR) {
                debug(D_WEB_CLIENT, "%llu: EAGAIN received.", w->id);
                continue;
            }

            debug(D_WEB_CLIENT, "%llu: LISTENER: poll() failed (input fd = %d, output fd = %d). Closing client.", w->id, w->ifd, w->ofd);
            break;
        }
        else if(unlikely(!retval)) {
            debug(D_WEB_CLIENT, "%llu: Timeout while waiting socket async I/O for %s %s", w->id, w->wait_receive?"INPUT":"", w->wait_send?"OUTPUT":"");
            break;
        }

        int used = 0;
        if(w->wait_send && ofd->revents & POLLOUT) {
            used++;
            if(web_client_send(w) < 0) {
                debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client.", w->id);
                break;
            }
        }

        if(w->wait_receive && (ifd->revents & POLLIN || ifd->revents & POLLPRI)) {
            used++;
            if(web_client_receive(w) < 0) {
                debug(D_WEB_CLIENT, "%llu: Cannot receive data from client. Closing client.", w->id);
                break;
            }

            if(w->mode == WEB_CLIENT_MODE_NORMAL) {
                debug(D_WEB_CLIENT, "%llu: Attempting to process received data.", w->id);
                web_client_process(w);
            }
        }

        if(unlikely(!used)) {
            debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on socket.", w->id);
            break;
        }
    }

    web_client_reset(w);

    log_access("%llu: %s port %s disconnected from thread task id %d", w->id, w->client_ip, w->client_port, gettid());
    debug(D_WEB_CLIENT, "%llu: done...", w->id);

    // close the sockets/files now
    // to free file descriptors
    if(w->ifd == w->ofd) {
        if(w->ifd != -1) close(w->ifd);
    }
    else {
        if(w->ifd != -1) close(w->ifd);
        if(w->ofd != -1) close(w->ofd);
    }
    w->ifd = -1;
    w->ofd = -1;

    w->obsolete = 1;

    pthread_exit(NULL);
    return NULL;
}