]> arthur.barton.de Git - netdata.git/blobdiff - src/web_client.c
added support for generating SVG badges with chart data
[netdata.git] / src / web_client.c
index a6831870d8ad4b65545dab74171f1a600557bc5c..87787f785aaac23f8518302bd99f9210f0c9e06c 100644 (file)
 #include <pthread.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <netinet/tcp.h>
 #include <malloc.h>
 #include <pwd.h>
 #include <grp.h>
 #include <ctype.h>
 #include <poll.h>
 
+// TCP_CORK
+#include <netinet/tcp.h>
+
 #include "common.h"
 #include "log.h"
 #include "appconfig.h"
 #include "rrd.h"
 #include "rrd2json.h"
 #include "registry.h"
-
+#include "web_buffer_svg.h"
 #include "web_client.h"
-#include "../config.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_enable_gzip = 1;
+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 */
 
 extern int netdata_exit;
 
 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;
@@ -81,7 +116,6 @@ struct web_client *web_client_create(int listener)
                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;
@@ -101,8 +135,14 @@ struct web_client *web_client_create(int listener)
                }
 
                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);
@@ -144,14 +184,24 @@ struct web_client *web_client_create(int listener)
        return(w);
 }
 
-void web_client_reset(struct web_client *w)
-{
-       struct timeval tv;
-       gettimeofday(&tv, NULL);
+void web_client_reset(struct web_client *w) {
+       web_client_uncrock_socket(w);
 
        debug(D_WEB_CLIENT, "%llu: Reseting client.", w->id);
 
-       if(w->stats_received_bytes || w->stats_sent_bytes) {
+       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
+
                if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
                        global_statistics_lock();
 
@@ -159,37 +209,36 @@ void web_client_reset(struct web_client *w)
                global_statistics.web_usec += usecdiff(&tv, &w->tv_in);
                global_statistics.bytes_received += w->stats_received_bytes;
                global_statistics.bytes_sent += w->stats_sent_bytes;
+               global_statistics.content_size += size;
+               global_statistics.compressed_content_size += sent;
 
                if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
                        global_statistics_unlock();
-       }
-       w->stats_received_bytes = 0;
-       w->stats_sent_bytes = 0;
 
-       size_t sent = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
+               w->stats_received_bytes = 0;
+               w->stats_sent_bytes = 0;
 
-#ifdef NETDATA_WITH_ZLIB
-       if(likely(w->response.zoutput)) sent = (size_t)w->response.zstream.total_out;
-#endif
 
-       size_t size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
+               // --------------------------------------------------------------------
+               // access log
 
-       if(likely(w->last_url[0]))
                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)?((float)(size-sent)/(float)size * 100.0):0.0),
-                       (float)usecdiff(&w->tv_ready, &w->tv_in) / 1000.0,
-                       (float)usecdiff(&tv, &w->tv_ready) / 1000.0,
-                       (float)usecdiff(&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
+                                  w->id,
+                                  sent, size, -((size > 0) ? ((float) (size - sent) / (float) size * 100.0) : 0.0),
+                                  (float) usecdiff(&w->tv_ready, &w->tv_in) / 1000.0,
+                                  (float) usecdiff(&tv, &w->tv_ready) / 1000.0,
+                                  (float) usecdiff(&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);
-                       close(w->ifd);
+                       if(w->ifd != -1) close(w->ifd);
                        w->ifd = w->ofd;
                }
        }
@@ -202,6 +251,9 @@ void web_client_reset(struct web_client *w)
 
        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';
 
@@ -220,7 +272,7 @@ void web_client_reset(struct web_client *w)
        // if we had enabled compression, release it
 #ifdef NETDATA_WITH_ZLIB
        if(w->response.zinitialized) {
-               debug(D_DEFLATE, "%llu: Reseting compression.", w->id);
+               debug(D_DEFLATE, "%llu: Freeing compression resources.", w->id);
                deflateEnd(&w->response.zstream);
                w->response.zsent = 0;
                w->response.zhave = 0;
@@ -233,19 +285,14 @@ void web_client_reset(struct web_client *w)
 #endif // NETDATA_WITH_ZLIB
 }
 
-struct web_client *web_client_free(struct web_client *w)
-{
+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);
 
-#ifdef NETDATA_WITH_ZLIB
-       if(w->response.zinitialized) {
-               deflateEnd(&w->response.zstream);
-       }
-#endif // NETDATA_WITH_ZLIB
-               
        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);
@@ -260,8 +307,7 @@ struct web_client *web_client_free(struct web_client *w)
        return(n);
 }
 
-uid_t web_files_uid(void)
-{
+uid_t web_files_uid(void) {
        static char *web_owner = NULL;
        static uid_t owner_uid = 0;
 
@@ -288,8 +334,7 @@ uid_t web_files_uid(void)
        return(owner_uid);
 }
 
-gid_t web_files_gid(void)
-{
+gid_t web_files_gid(void) {
        static char *web_group = NULL;
        static gid_t owner_gid = 0;
 
@@ -471,7 +516,7 @@ void web_client_enable_deflate(struct web_client *w, int gzip) {
 //     }
 
        // Select GZIP compression: windowbits = 15 + 16 = 31
-       if(deflateInit2(&w->response.zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + ((gzip)?16:0), 8, Z_DEFAULT_STRATEGY) != Z_OK) {
+       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;
        }
@@ -641,6 +686,133 @@ cleanup:
        return ret;
 }
 
+int web_client_api_v1_badge(struct web_client *w, char *url) {
+       // chart
+       // dimensions
+       // before
+       // after
+       // points
+
+       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;
+
+       int group = GROUP_MAX;
+       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 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(strlen(value));
+                       if(dimensions) {
+                               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);
+               }
+               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, "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;
+       }
+
+       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 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):0;
+       int       points   = (points_str   && *points_str  )?atoi(points_str):0;
+
+       if(!label) {
+               if(dimensions) {
+                       const char *dim = buffer_tostring(dimensions);
+                       if(*dim == '|') dim++;
+                       label = dim;
+               }
+               else
+                       label = st->name;
+       }
+       if(!units) {
+               if(options & RRDR_OPTION_PERCENTAGE)
+                       units="%";
+               else
+                       units = st->units;
+       }
+
+       debug(D_WEB_CLIENT, "%llu: API command 'badge.svg' for chart '%s', dimensions '%s', after '%lld', before '%lld', points '%d', group '%u', format '%u', options '0x%08x'"
+                       , w->id
+                       , chart
+                       , (dimensions)?buffer_tostring(dimensions):""
+                       , after
+                       , before
+                       , points
+                       , group
+                       , format
+                       , options
+                       );
+
+       time_t latest_timestamp = 0;
+       int value_is_null = 1;
+       calculated_number n = 0;
+       ret = rrd2value(st, w->response.data, &n, dimensions, points, after, before, group, options, &latest_timestamp, &value_is_null);
+       buffer_svg(w->response.data, label, n * multiply / divide, units, label_color, value_color, value_is_null);
+       return ret;
+
+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)
 {
@@ -821,7 +993,7 @@ 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_to = 0 /*, hash_redirects = 0 */;
 
        if(unlikely(!hash_action)) {
                hash_action = simple_hash("action");
@@ -836,6 +1008,9 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
                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] = "";
@@ -845,9 +1020,9 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
        // FIXME
        // The browser may send multiple cookies with our id
        
-       char *cookie = strstr(w->response.data->buffer, " " NETDATA_REGISTRY_COOKIE_NAME "=");
+       char *cookie = strstr(w->response.data->buffer, NETDATA_REGISTRY_COOKIE_NAME "=");
        if(cookie)
-               strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME) + 1], 36);
+               strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME)], 36);
 
        char action = '\0';
        char *machine_guid = NULL,
@@ -856,6 +1031,9 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
                        *search_machine_guid = NULL,
                        *delete_url = NULL,
                        *to_person_guid = NULL;
+/*
+       int redirects = 0;
+*/
 
        while(url) {
                char *value = mystrsep(&url, "?&[]");
@@ -877,7 +1055,14 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
                        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;
 
@@ -900,6 +1085,15 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
                        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)) {
@@ -929,15 +1123,70 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
 
        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':
@@ -954,15 +1203,15 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
        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;
+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;
 
        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");
        }
 
        // get the command
@@ -983,6 +1232,9 @@ int web_client_api_request_v1(struct web_client *w, char *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_v1_badge(w, url);
+
                else {
                        buffer_flush(w->response.data);
                        buffer_sprintf(w->response.data, "Unsupported v1 API command: %s", tok);
@@ -1291,26 +1543,30 @@ const char *web_response_code_to_string(int code) {
 }
 
 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;
+       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 || e[1] != ' ') return e;
+       if(!*e) return e;
 
        // get the name
        *e = '\0';
 
        // find the value
-       char *v, *ve;
-       v = ve = e + 2;
+       char *v = e + 1, *ve;
+
+       // skip leading spaces from value
+       while(*v == ' ') v++;
+       ve = v;
 
        // find the \r
        while(*ve && *ve != '\r') ve++;
@@ -1332,6 +1588,10 @@ static inline char *http_header_parse(struct web_client *w, char *s) {
                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) {
@@ -1670,16 +1930,32 @@ void web_client_process(struct web_client *w) {
                , date
                );
 
-       if(w->cookie1[0]) {
-               buffer_sprintf(w->response.header_output,
-                  "Set-Cookie: %s\r\n",
-                  w->cookie1);
-       }
+       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(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) {
@@ -1729,11 +2005,6 @@ void web_client_process(struct web_client *w) {
 
        buffer_strcat(w->response.header_output, "\r\n");
 
-/*     // disable TCP_NODELAY, to buffer the header
-       int flag = 0;
-       if(setsockopt(w->ofd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) != 0)
-               error("%llu: failed to disable TCP_NODELAY on socket.", w->id);
-*/
        // sent the HTTP header
        debug(D_WEB_DATA, "%llu: Sending response HTTP header of size %d: '%s'"
                        , w->id
@@ -1741,6 +2012,8 @@ void web_client_process(struct web_client *w) {
                        , 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)
@@ -1755,11 +2028,6 @@ void web_client_process(struct web_client *w) {
        else 
                w->stats_sent_bytes += bytes;
 
-/*     // enable TCP_NODELAY, to send all data immediately at the next send()
-       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);
-*/
        // enable sending immediately if we have data
        if(w->response.data->len) w->wait_send = 1;
        else w->wait_send = 0;
@@ -1806,7 +2074,7 @@ ssize_t web_client_send_chunk_header(struct web_client *w, size_t len)
 {
        debug(D_DEFLATE, "%llu: OPEN CHUNK of %d bytes (hex: %x).", w->id, len, len);
        char buf[1024];
-       sprintf(buf, "%lX\r\n", len);
+       sprintf(buf, "%zX\r\n", len);
        
        ssize_t bytes = send(w->ofd, buf, strlen(buf), 0);
        if(bytes > 0) {
@@ -2214,6 +2482,8 @@ void *web_client_main(void *ptr)
                }
        }
 
+       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);