]> 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
old mode 100755 (executable)
new mode 100644 (file)
index ee5b651..87787f7
 #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 "global_statistics.h"
 #include "rrd.h"
 #include "rrd2json.h"
-
+#include "registry.h"
+#include "web_buffer_svg.h"
 #include "web_client.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;
@@ -69,14 +109,13 @@ struct web_client *web_client_create(int listener)
 
                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.");
-                       strncpy(w->client_ip,   "UNKNOWN", NI_MAXHOST);
-                       strncpy(w->client_port, "UNKNOWN", NI_MAXSERV);
+                       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;
@@ -86,7 +125,8 @@ struct web_client *web_client_create(int listener)
                                strcpy(w->client_ip, &w->client_ip[7]);
                                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);
                        }
-                       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);
+                       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:
@@ -95,7 +135,14 @@ struct web_client *web_client_create(int listener)
                }
 
                int 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);
+               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);
@@ -125,6 +172,7 @@ struct web_client *web_client_create(int listener)
                return NULL;
        }
 
+       w->origin[0] = '*';
        w->wait_receive = 1;
 
        if(web_clients) web_clients->prev = w;
@@ -136,43 +184,79 @@ 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);
 
-       long sent = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
+       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 = (long)w->response.zstream.total_out;
+               if(likely(w->response.zoutput)) sent = (size_t)w->response.zstream.total_out;
 #endif
 
-       long size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
-
-       if(likely(w->last_url[0]))
-               log_access("%llu: (sent/all = %ld/%ld 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
-               );
+               // --------------------------------------------------------------------
+               // global statistics
 
-       debug(D_WEB_CLIENT, "%llu: Reseting client.", w->id);
+               if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
+                       global_statistics_lock();
+
+               global_statistics.web_requests++;
+               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;
+
+
+               // --------------------------------------------------------------------
+               // 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) ? ((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)) {
-               debug(D_WEB_CLIENT, "%llu: Closing filecopy input file.", w->id);
-               close(w->ifd);
-               w->ifd = w->ofd;
+               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);
@@ -188,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;
@@ -201,22 +285,21 @@ 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);
 
        if(w->prev)     w->prev->next = w->next;
        if(w->next) w->next->prev = w->prev;
-
-       if(w == web_clients) web_clients = w->next;
-
        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);
-       close(w->ifd);
-       if(w->ofd != w->ifd) close(w->ofd);
+       if(w->ifd != -1) close(w->ifd);
+       if(w->ofd != -1 && w->ofd != w->ifd) close(w->ofd);
        free(w);
 
        global_statistics.connected_clients--;
@@ -224,23 +307,25 @@ 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;
 
        if(unlikely(!web_owner)) {
-               web_owner = config_get("global", "web files owner", NETDATA_USER);
+               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);
+                               error("User '%s' is not present. Ignoring option.", web_owner);
                                owner_uid = geteuid();
                        }
                        else {
-                               debug(D_WEB_CLIENT, "Web files owner set to %s.\n", web_owner);
+                               debug(D_WEB_CLIENT, "Web files owner set to %s.", web_owner);
                                owner_uid = pw->pw_uid;
                        }
                }
@@ -249,6 +334,33 @@ uid_t web_files_uid(void)
        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;
@@ -268,7 +380,7 @@ int mysendfile(struct web_client *w, char *filename)
        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'", *s);
+                       buffer_sprintf(w->response.data, "File '%s' cannot be served. Filename contains invalid character '%c'", filename, *s);
                        return 400;
                }
        }
@@ -282,31 +394,38 @@ int mysendfile(struct web_client *w, char *filename)
 
        // access the file
        char webfilename[FILENAME_MAX + 1];
-       snprintf(webfilename, FILENAME_MAX, "%s/%s", web_dir, filename);
+       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.", filename);
+               buffer_sprintf(w->response.data, "File '%s' does not exist, or is not accessible.", webfilename);
                return 404;
        }
 
-       // check if the file is owned by us
+       // check if the file is owned by expected user
        if(stat.st_uid != web_files_uid()) {
-               debug(D_WEB_CLIENT_ACCESS, "%llu: File '%s' is owned by user %d (I run as user %d). Access Denied.", w->id, webfilename, stat.st_uid, getuid());
-               buffer_sprintf(w->response.data, "Access to file '%s' is not permitted.", filename);
+               error("%llu: File '%s' is owned by user %d (expected user %d). 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 %d (expected group %d). 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) {
-               snprintf(webfilename, FILENAME_MAX+1, "%s/index.html", filename);
+               snprintfz(webfilename, FILENAME_MAX, "%s/index.html", filename);
                return mysendfile(w, webfilename);
        }
 
        if((stat.st_mode & S_IFMT) != S_IFREG) {
-               debug(D_WEB_CLIENT_ACCESS, "%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.", filename);
+               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;
        }
 
@@ -318,12 +437,12 @@ int mysendfile(struct web_client *w, char *filename)
                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.", 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'.", filename);
+                       buffer_sprintf(w->response.data, "Cannot open file '%s'.", webfilename);
                        return 404;
                }
        }
@@ -341,6 +460,13 @@ int mysendfile(struct web_client *w, char *filename)
        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);
@@ -357,13 +483,13 @@ int mysendfile(struct web_client *w, char *filename)
 
 
 #ifdef NETDATA_WITH_ZLIB
-void web_client_enable_deflate(struct web_client *w) {
-       if(w->response.zinitialized == 1) {
+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(w->response.sent) {
+       if(unlikely(w->response.sent)) {
                error("%llu: Cannot enable compression in the middle of a conversation.", w->id);
                return;
        }
@@ -390,7 +516,7 @@ void web_client_enable_deflate(struct web_client *w) {
 //     }
 
        // Select GZIP compression: windowbits = 15 + 16 = 31
-       if(deflateInit2(&w->response.zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 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;
        }
@@ -431,6 +557,8 @@ uint32_t web_client_api_request_v1_data_options(char *o)
                        ret |= RRDR_OPTION_OBJECTSROWS;
                else if(!strcmp(tok, "google_json"))
                        ret |= RRDR_OPTION_GOOGLE_JSON;
+               else if(!strcmp(tok, "percentage"))
+                       ret |= RRDR_OPTION_PERCENTAGE;
        }
 
        return ret;
@@ -468,6 +596,9 @@ uint32_t web_client_api_request_v1_data_format(char *name)
        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;
 }
 
@@ -555,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)
 {
@@ -591,7 +849,7 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
                if(!name || !*name) continue;
                if(!value || !*value) continue;
 
-               debug(D_WEB_CLIENT, "%llu: API v1 query param '%s' with value '%s'", w->id, name, value);
+               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
@@ -686,7 +944,7 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
 
        if(outFileName && *outFileName) {
                buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
-               error("generating outfilename header: '%s'", outFileName);
+               debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName);
        }
 
        if(format == DATASOURCE_DATATABLE_JSONP) {
@@ -731,40 +989,290 @@ cleanup:
        return ret;
 }
 
-int web_client_api_request_v1(struct web_client *w, char *url)
+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_sprintf(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
+                       return 400;
+       }
+
+       buffer_flush(w->response.data);
+       buffer_sprintf(w->response.data, "Invalid or no registry action.");
+       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;
+
+       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
        char *tok = mystrsep(&url, "/?&");
-       debug(D_WEB_CLIENT, "%llu: Searching for API v1 command '%s'.", w->id, tok);
+       if(tok && *tok) {
+               debug(D_WEB_CLIENT, "%llu: Searching for API v1 command '%s'.", w->id, tok);
+               uint32_t hash = simple_hash(tok);
 
-       if(strcmp(tok, "data") == 0)
-               return web_client_api_request_v1_data(w, url);
-       else if(strcmp(tok, "chart") == 0)
-               return web_client_api_request_v1_chart(w, url);
-       else if(strcmp(tok, "charts") == 0)
-               return web_client_api_request_v1_charts(w, url);
+               if(hash == hash_data && !strcmp(tok, "data"))
+                       return web_client_api_request_v1_data(w, url);
 
-       buffer_flush(w->response.data);
-       buffer_sprintf(w->response.data, "Unsupported v1 API command: %s", tok);
-       return 404;
+               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_v1_badge(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, "/?&");
-       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);
-
-       buffer_flush(w->response.data);
-       buffer_sprintf(w->response.data, "Unsupported API version: %s", tok);
-       return 404;
+       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_data_request(struct web_client *w, char *url, int datasource_type)
+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';
@@ -773,11 +1281,14 @@ int web_client_data_request(struct web_client *w, char *url, int datasource_type
 
        // get the name of the data to show
        char *tok = mystrsep(&url, "/");
-       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(tok && *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
@@ -804,34 +1315,36 @@ int web_client_data_request(struct web_client *w, char *url, int datasource_type
        if(url) {
                // parse the group count required
                tok = mystrsep(&url, "/");
-               if(tok) group_count = atoi(tok);
+               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(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(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) after = strtoul(tok, NULL, 10);
+               if(tok && *tok) after = strtoul(tok, NULL, 10);
                if(after < 0) after = 0;
        }
        if(url) {
                // parse before time
                tok = mystrsep(&url, "/");
-               if(tok) before = strtoul(tok, NULL, 10);
+               if(tok && *tok) before = strtoul(tok, NULL, 10);
                if(before < 0) before = 0;
        }
        if(url) {
                // parse nonzero
                tok = mystrsep(&url, "/");
-               if(tok && strcmp(tok, "nonzero") == 0) nonzero = 1;
+               if(tok && *tok && strcmp(tok, "nonzero") == 0) nonzero = 1;
        }
 
        w->response.data->contenttype = CT_APPLICATION_JSON;
@@ -850,9 +1363,9 @@ int web_client_data_request(struct web_client *w, char *url, int datasource_type
 
                while(args) {
                        tok = mystrsep(&args, "&");
-                       if(tok) {
+                       if(tok && *tok) {
                                char *name = mystrsep(&tok, "=");
-                               if(name && strcmp(name, "tqx") == 0) {
+                               if(name && *name && strcmp(name, "tqx") == 0) {
                                        char *key = mystrsep(&tok, ":");
                                        char *value = mystrsep(&tok, ";");
                                        if(key && value && *key && *value) {
@@ -920,380 +1433,484 @@ int web_client_data_request(struct web_client *w, char *url, int datasource_type
        return 200;
 }
 
-/*
-int web_client_parse_request(struct web_client *w) {
-       // protocol
-       // hostname
-       // path
-       // query string name-value
-       // http version
-       // method
-       // http request headers name-value
+const char *web_content_type_to_string(uint8_t contenttype) {
+       switch(contenttype) {
+               case CT_TEXT_HTML:
+                       return "text/html; charset=utf-8";
 
-       web_client_clean_request(w);
+               case CT_APPLICATION_XML:
+                       return "application/xml; charset=utf-8";
 
-       debug(D_WEB_DATA, "%llu: Processing data buffer of %d bytes: '%s'.", w->id, w->response.data->bytes, w->response.data->buffer);
+               case CT_APPLICATION_JSON:
+                       return "application/json; charset=utf-8";
 
-       char *buf = w->response.data->buffer;
-       char *line, *tok;
+               case CT_APPLICATION_X_JAVASCRIPT:
+                       return "application/x-javascript; charset=utf-8";
 
-       // ------------------------------------------------------------------------
-       // the first line
+               case CT_TEXT_CSS:
+                       return "text/css; charset=utf-8";
 
-       if(buf && (line = strsep(&buf, "\r\n"))) {
-               // method
-               if(line && (tok = strsep(&line, " "))) {
-                       w->request.protocol = strdup(tok);
-               }
-               else goto cleanup;
+               case CT_TEXT_XML:
+                       return "text/xml; charset=utf-8";
 
-               // url
-       }
-       else goto cleanup;
+               case CT_TEXT_XSL:
+                       return "text/xsl; charset=utf-8";
 
-       // ------------------------------------------------------------------------
-       // the rest of the lines
+               case CT_APPLICATION_OCTET_STREAM:
+                       return "application/octet-stream";
 
-       while(buf && (line = strsep(&buf, "\r\n"))) {
-               while(line && (tok = strsep(&line, ": "))) {
-               }
-       }
+               case CT_IMAGE_SVG_XML:
+                       return "image/svg+xml";
 
-       char *url = NULL;
+               case CT_APPLICATION_X_FONT_TRUETYPE:
+                       return "application/x-font-truetype";
 
+               case CT_APPLICATION_X_FONT_OPENTYPE:
+                       return "application/x-font-opentype";
 
-cleanup:
-       web_client_clean_request(w);
-       return 0;
-}
-*/
+               case CT_APPLICATION_FONT_WOFF:
+                       return "application/font-woff";
 
-void web_client_process(struct web_client *w) {
-       int code = 500;
-       ssize_t bytes;
-       int enable_gzip = 0;
+               case CT_APPLICATION_FONT_WOFF2:
+                       return "application/font-woff2";
 
-       w->wait_receive = 0;
+               case CT_APPLICATION_VND_MS_FONTOBJ:
+                       return "application/vnd.ms-fontobject";
 
-       // check if we have an empty line (end of HTTP header)
-       if(strstr(w->response.data->buffer, "\r\n\r\n")) {
-               global_statistics_lock();
-               global_statistics.web_requests++;
-               global_statistics_unlock();
+               case CT_IMAGE_PNG:
+                       return "image/png";
 
-               gettimeofday(&w->tv_in, NULL);
-               debug(D_WEB_DATA, "%llu: Processing data buffer of %d bytes: '%s'.", w->id, w->response.data->len, w->response.data->buffer);
+               case CT_IMAGE_JPG:
+                       return "image/jpeg";
 
-               // check if the client requested keep-alive HTTP
-               if(strcasestr(w->response.data->buffer, "Connection: keep-alive")) w->keepalive = 1;
-               else w->keepalive = 0;
+               case CT_IMAGE_GIF:
+                       return "image/gif";
 
-#ifdef NETDATA_WITH_ZLIB
-               // check if the client accepts deflate
-               if(web_enable_gzip && strstr(w->response.data->buffer, "gzip"))
-                       enable_gzip = 1;
-#endif // NETDATA_WITH_ZLIB
+               case CT_IMAGE_XICON:
+                       return "image/x-icon";
 
-               int datasource_type = DATASOURCE_DATATABLE_JSONP;
-               //if(strstr(w->response.data->buffer, "X-DataSource-Auth"))
-               //      datasource_type = DATASOURCE_GOOGLE_JSON;
+               case CT_IMAGE_BMP:
+                       return "image/bmp";
 
-               char *buf = (char *)buffer_tostring(w->response.data);
-               char *tok = strsep(&buf, " \r\n");
-               char *url = NULL;
-               char *pointer_to_free = NULL; // keep url_decode() allocated buffer
+               case CT_IMAGE_ICNS:
+                       return "image/icns";
 
-               w->mode = WEB_CLIENT_MODE_NORMAL;
+               default:
+               case CT_TEXT_PLAIN:
+                       return "text/plain; charset=utf-8";
+       }
+}
 
-               if(buf && strcmp(tok, "GET") == 0) {
-                       tok = strsep(&buf, " \r\n");
-                       pointer_to_free = url = url_decode(tok);
-                       debug(D_WEB_CLIENT, "%llu: Processing HTTP GET on url '%s'.", w->id, url);
-               }
-               else if(buf && strcmp(tok, "OPTIONS") == 0) {
-                       tok = strsep(&buf, " \r\n");
-                       pointer_to_free = url = url_decode(tok);
-                       debug(D_WEB_CLIENT, "%llu: Processing HTTP OPTIONS on url '%s'.", w->id, url);
-                       w->mode = WEB_CLIENT_MODE_OPTIONS;
-               }
-               else if (buf && strcmp(tok, "POST") == 0) {
-                       w->keepalive = 0;
-                       tok = strsep(&buf, " \r\n");
-                       pointer_to_free = url = url_decode(tok);
-                       debug(D_WEB_CLIENT, "%llu: I don't know how to handle POST with form data. Assuming it is a GET on url '%s'.", w->id, url);
-               }
 
-               w->last_url[0] = '\0';
+const char *web_response_code_to_string(int code) {
+       switch(code) {
+               case 200:
+                       return "OK";
 
-               if(w->mode == WEB_CLIENT_MODE_OPTIONS) {
-                       strncpy(w->last_url, url, URL_MAX);
-                       w->last_url[URL_MAX] = '\0';
+               case 307:
+                       return "Temporary Redirect";
 
-                       code = 200;
-                       w->response.data->contenttype = CT_TEXT_PLAIN;
-                       buffer_flush(w->response.data);
-                       buffer_strcat(w->response.data, "OK");
-               }
-               else if(url) {
-#ifdef NETDATA_WITH_ZLIB
-                       if(enable_gzip)
-                               web_client_enable_deflate(w);
-#endif
+               case 400:
+                       return "Bad Request";
 
-                       strncpy(w->last_url, url, URL_MAX);
-                       w->last_url[URL_MAX] = '\0';
+               case 403:
+                       return "Forbidden";
 
-                       tok = mystrsep(&url, "/?");
+               case 404:
+                       return "Not Found";
 
-                       debug(D_WEB_CLIENT, "%llu: Processing command '%s'.", w->id, tok);
+               case 412:
+                       return "Preconditions Failed";
 
-                       if(strcmp(tok, "api") == 0) {
-                               // the client is requesting api access
-                               datasource_type = DATASOURCE_JSON;
-                               code = web_client_api_request(w, url);
-                       }
-#ifdef NETDATA_INTERNAL_CHECKS
-                       else if(strcmp(tok, "exit") == 0) {
-                               netdata_exit = 1;
-                               code = 200;
-                               w->response.data->contenttype = CT_TEXT_PLAIN;
-                               buffer_flush(w->response.data);
-                               buffer_strcat(w->response.data, "will do");
-                       }
-#endif
-                       else if(strcmp(tok, WEB_PATH_DATA) == 0) { // "data"
-                               // the client is requesting rrd data
-                               datasource_type = DATASOURCE_JSON;
-                               code = web_client_data_request(w, url, datasource_type);
-                       }
-                       else if(strcmp(tok, WEB_PATH_DATASOURCE) == 0) { // "datasource"
-                               // the client is requesting google datasource
-                               code = web_client_data_request(w, url, datasource_type);
-                       }
-                       else if(strcmp(tok, WEB_PATH_GRAPH) == 0) { // "graph"
-                               // the client is requesting an rrd graph
-
-                               // get the name of the data to show
-                               tok = mystrsep(&url, "/?&");
-                               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);
-                               }
-                       }
-#ifdef NETDATA_INTERNAL_CHECKS
-                       else if(strcmp(tok, "debug") == 0) {
-                               buffer_flush(w->response.data);
+               default:
+                       if(code >= 100 && code < 200)
+                               return "Informational";
 
-                               // get the name of the data to show
-                               tok = mystrsep(&url, "/?&");
-                               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?0:1;
-                                       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 if(strcmp(tok, "mirror") == 0) {
-                               code = 200;
+                       if(code >= 200 && code < 300)
+                               return "Successful";
 
-                               debug(D_WEB_CLIENT_ACCESS, "%llu: Mirroring...", w->id);
+                       if(code >= 300 && code < 400)
+                               return "Redirection";
 
-                               // replace the zero bytes with spaces
-                               buffer_char_replace(w->response.data, '\0', ' ');
+                       if(code >= 400 && code < 500)
+                               return "Bad Request";
 
-                               // just leave the buffer as is
-                               // it will be copied back to the client
-                       }
-#endif
-                       else if(strcmp(tok, "list") == 0) {
-                               code = 200;
+                       if(code >= 500 && code < 600)
+                               return "Server Error";
 
-                               debug(D_WEB_CLIENT_ACCESS, "%llu: Sending list of RRD_STATS...", w->id);
+                       return "Undefined Error";
+       }
+}
 
-                               buffer_flush(w->response.data);
-                               RRDSET *st = rrdset_root;
+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;
 
-                               for ( ; st ; st = st->next )
-                                       buffer_sprintf(w->response.data, "%s\n", st->name);
-                       }
-                       else if(strcmp(tok, "all.json") == 0) {
-                               code = 200;
-                               debug(D_WEB_CLIENT_ACCESS, "%llu: Sending JSON list of all monitors of RRD_STATS...", w->id);
+       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");
+       }
 
-                               w->response.data->contenttype = CT_APPLICATION_JSON;
-                               buffer_flush(w->response.data);
-                               rrd_stats_all_json(w->response.data);
-                       }
-                       else if(strcmp(tok, "netdata.conf") == 0) {
-                               code = 200;
-                               debug(D_WEB_CLIENT_ACCESS, "%llu: Sending netdata.conf ...", w->id);
+       char *e = s;
 
-                               w->response.data->contenttype = CT_TEXT_PLAIN;
-                               buffer_flush(w->response.data);
-                               generate_config(w->response.data, 0);
-                       }
-                       else {
-                               char filename[FILENAME_MAX+1];
-                               url = filename;
-                               strncpy(filename, w->last_url, FILENAME_MAX);
-                               filename[FILENAME_MAX] = '\0';
-                               tok = mystrsep(&url, "?");
-                               buffer_flush(w->response.data);
-                               code = mysendfile(w, (tok && *tok)?tok:"/");
-                       }
-               }
-               else {
-                       strcpy(w->last_url, "not a valid response");
+       // find the :
+       while(*e && *e != ':') e++;
+       if(!*e) return e;
 
-                       if(buf) debug(D_WEB_CLIENT_ACCESS, "%llu: Cannot understand '%s'.", w->id, buf);
+       // get the name
+       *e = '\0';
 
-                       code = 500;
-                       buffer_flush(w->response.data);
-                       buffer_strcat(w->response.data, "I don't understand you...\r\n");
-               }
+       // find the value
+       char *v = e + 1, *ve;
+
+       // skip leading spaces from value
+       while(*v == ' ') v++;
+       ve = v;
 
-               // free url_decode() buffer
-               if(pointer_to_free) free(pointer_to_free);
+       // find the \r
+       while(*ve && *ve != '\r') ve++;
+       if(!*ve || ve[1] != '\n') {
+               *e = ':';
+               return ve;
        }
-       else if(w->response.data->len > 8192) {
-               strcpy(w->last_url, "too big request");
 
-               debug(D_WEB_CLIENT_ACCESS, "%llu: Received request is too big.", w->id);
+       // terminate the value
+       *ve = '\0';
 
-               code = 400;
-               buffer_flush(w->response.data);
-               buffer_strcat(w->response.data, "Received request is too big.\r\n");
+       // 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 {
-               // wait for more data
+               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;
+               return -2;
        }
 
-       gettimeofday(&w->tv_ready, NULL);
-       w->response.data->date = time(NULL);
-       w->response.sent = 0;
-       w->response.code = code;
+       // we have the end of encoded_url - remember it
+       char *ue = s;
 
-       // prepare the HTTP response header
-       debug(D_WEB_CLIENT, "%llu: Generating HTTP header with response %d.", w->id, code);
+       // make sure we have complete request
+       // complete requests contain: \r\n\r\n
+       while(*s) {
+               // find a line feed
+               while(*s && *s++ != '\r');
 
-       char *content_type_string;
-       switch(w->response.data->contenttype) {
-               case CT_TEXT_HTML:
-                       content_type_string = "text/html; charset=utf-8";
-                       break;
+               // did we reach the end?
+               if(unlikely(!*s)) break;
 
-               case CT_APPLICATION_XML:
-                       content_type_string = "application/xml; charset=utf-8";
-                       break;
+               // is it \r\n ?
+               if(likely(*s++ == '\n')) {
 
-               case CT_APPLICATION_JSON:
-                       content_type_string = "application/json; charset=utf-8";
-                       break;
+                       // is it again \r\n ? (header end)
+                       if(unlikely(*s == '\r' && s[1] == '\n')) {
+                               // a valid complete HTTP request found
 
-               case CT_APPLICATION_X_JAVASCRIPT:
-                       content_type_string = "application/x-javascript; charset=utf-8";
-                       break;
+                               *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);
 
-               case CT_TEXT_CSS:
-                       content_type_string = "text/css; charset=utf-8";
-                       break;
+                               w->wait_receive = 0;
+                               return 0;
+                       }
 
-               case CT_TEXT_XML:
-                       content_type_string = "text/xml; charset=utf-8";
-                       break;
+                       // another header line
+                       s = http_header_parse(w, s);
+               }
+       }
 
-               case CT_TEXT_XSL:
-                       content_type_string = "text/xsl; charset=utf-8";
-                       break;
+       // incomplete request
+       w->wait_receive = 1;
+       return -3;
+}
 
-               case CT_APPLICATION_OCTET_STREAM:
-                       content_type_string = "application/octet-stream";
-                       break;
+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;
+
+       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");
+       }
 
-               case CT_IMAGE_SVG_XML:
-                       content_type_string = "image/svg+xml";
-                       break;
+       int code = 500;
+       ssize_t bytes;
 
-               case CT_APPLICATION_X_FONT_TRUETYPE:
-                       content_type_string = "application/x-font-truetype";
-                       break;
+       int what_to_do = http_request_validate(w);
 
-               case CT_APPLICATION_X_FONT_OPENTYPE:
-                       content_type_string = "application/x-font-opentype";
-                       break;
+       // wait for more data
+       if(what_to_do < 0) {
+               if(w->response.data->len > TOO_BIG_REQUEST) {
+                       strcpy(w->last_url, "too big request");
 
-               case CT_APPLICATION_FONT_WOFF:
-                       content_type_string = "application/font-woff";
-                       break;
+                       debug(D_WEB_CLIENT_ACCESS, "%llu: Received request is too big (%zd bytes).", w->id, w->response.data->len);
 
-               case CT_APPLICATION_FONT_WOFF2:
-                       content_type_string = "application/font-woff2";
-                       break;
+                       code = 400;
+                       buffer_flush(w->response.data);
+                       buffer_sprintf(w->response.data, "Received request is too big  (%zd 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");
 
-               case CT_APPLICATION_VND_MS_FONTOBJ:
-                       content_type_string = "application/vnd.ms-fontobject";
-                       break;
+               debug(D_WEB_CLIENT_ACCESS, "%llu: Cannot understand '%s'.", w->id, w->response.data->buffer);
 
-               default:
-               case CT_TEXT_PLAIN:
-                       content_type_string = "text/plain; charset=utf-8";
-                       break;
+               code = 500;
+               buffer_flush(w->response.data);
+               buffer_strcat(w->response.data, "I don't understand you...\r\n");
        }
+       else { // what_to_do == 0
+               gettimeofday(&w->tv_in, NULL);
 
-       char *code_msg;
-       switch(code) {
-               case 200:
-                       code_msg = "OK";
-                       break;
+               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);
 
-               case 307:
-                       code_msg = "Temporary Redirect";
-                       break;
+                                       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;
 
-               case 400:
-                       code_msg = "Bad Request";
-                       break;
+                                       debug(D_WEB_CLIENT_ACCESS, "%llu: Sending list of RRD_STATS...", w->id);
 
-               case 403:
-                       code_msg = "Forbidden";
-                       break;
+                                       buffer_flush(w->response.data);
+                                       RRDSET *st = rrdset_root;
 
-               case 404:
-                       code_msg = "Not Found";
-                       break;
+                                       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);
 
-               default:
-                       code_msg = "Internal Server Error";
-                       break;
+                                       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");
+
+                                       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[100];
        struct tm tmbuf, *tm = gmtime_r(&w->response.data->date, &tmbuf);
        strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %Z", tm);
@@ -1302,30 +1919,72 @@ void web_client_process(struct web_client *w) {
                "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"
-               "Access-Control-Allow-Origin: *\r\n"
-               "Access-Control-Allow-Methods: GET, OPTIONS\r\n"
-               "Access-Control-Allow-Headers: accept, x-requested-with\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) {
+       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"
-                       "Access-Control-Max-Age: 0\r\n"
                        , date);
        }
-       else {
-               buffer_strcat(w->response.header_output, "Cache-Control: public\r\n");
-               buffer_strcat(w->response.header_output, "Access-Control-Max-Age: 3600\r\n");
+       else if(w->mode != WEB_CLIENT_MODE_OPTIONS) {
+               char edate[100];
+               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
@@ -1346,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
@@ -1358,21 +2012,21 @@ 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))
-               error("%llu: HTTP Header failed to be sent (I sent %d bytes but the system sent %d bytes)."
-                               , w->id
-                               , buffer_strlen(w->response.header_output)
-                               , bytes);
-       else {
-               global_statistics_lock();
-               global_statistics.bytes_sent += bytes;
-               global_statistics_unlock();
-       }
+       if(bytes != (ssize_t) buffer_strlen(w->response.header_output)) {
+               if(bytes > 0)
+                       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);
+               debug(D_WEB_CLIENT, "%llu: HTTP Header failed to be sent (I sent %d bytes but the system sent %d 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;
@@ -1399,8 +2053,10 @@ void web_client_process(struct web_client *w) {
                                // 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);
+                                       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);
                                }
                                */
                        }
@@ -1414,50 +2070,78 @@ void web_client_process(struct web_client *w) {
        }
 }
 
-long web_client_send_chunk_header(struct web_client *w, long len)
+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);
-       ssize_t bytes = send(w->ofd, buf, strlen(buf), MSG_DONTWAIT);
+       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 %d bytes.", w->id, bytes);
+               w->stats_sent_bytes += bytes;
+       }
 
-       if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk header %d bytes.", w->id, bytes);
-       else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk header to the client.", w->id);
-       else debug(D_DEFLATE, "%llu: Failed to send chunk header to client.", w->id);
+       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;
 }
 
-long web_client_send_chunk_close(struct web_client *w)
+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, MSG_DONTWAIT);
+       ssize_t bytes = send(w->ofd, "\r\n", 2, 0);
+       if(bytes > 0) {
+               debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
+               w->stats_sent_bytes += bytes;
+       }
 
-       if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
-       else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk suffix to the client.", w->id);
-       else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client.", w->id);
+       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;
 }
 
-long web_client_send_chunk_finalize(struct web_client *w)
+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, MSG_DONTWAIT);
+       ssize_t bytes = send(w->ofd, "\r\n0\r\n\r\n", 7, 0);
+       if(bytes > 0) {
+               debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
+               w->stats_sent_bytes += bytes;
+       }
 
-       if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
-       else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk suffix to the client.", w->id);
-       else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client.", w->id);
+       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
-long web_client_send_deflate(struct web_client *w)
+ssize_t web_client_send_deflate(struct web_client *w)
 {
-       long len = 0, t = 0;
+       ssize_t len = 0, t = 0;
 
        // when using compression,
        // w->response.sent is the amount of bytes passed through compression
@@ -1470,46 +2154,43 @@ long web_client_send_deflate(struct web_client *w)
                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);
-
-               // 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->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->ifd != w->ofd && w->response.rlen && w->response.rlen > w->response.data->len) {
+               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);
+                       return t;
                }
 
-               if(w->keepalive == 0) {
+               if(unlikely(!w->keepalive)) {
                        debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %ld bytes sent.", w->id, w->response.sent);
-                       errno = 0;
-                       return(-1);
+                       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. Waiting for next request on the same socket.", w->id);
-               return(0);
+               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(w->response.sent != 0) {
+                       t = web_client_send_chunk_close(w);
+                       if(t < 0) return t;
+               }
 
                debug(D_DEFLATE, "%llu: Compressing %d new bytes starting from %d (and %d 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) {
-#ifdef NETDATA_INTERNAL_CHECKS
-                       if((long)w->response.sent - (long)w->response.zstream.avail_in < 0)
-                               error("internal error: avail_in is corrupted.");
-#endif
                        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);
                }
@@ -1545,31 +2226,39 @@ long web_client_send_deflate(struct web_client *w)
                debug(D_DEFLATE, "%llu: Compression produced %d bytes.", w->id, w->response.zhave);
 
                // open a new chunk
-               t += web_client_send_chunk_header(w, w->response.zhave);
+               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 %d bytes of data (+%d 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;
-               if(t > 0) len += t;
+               len += t;
                debug(D_WEB_CLIENT, "%llu: Sent %d bytes.", w->id, len);
        }
-       else if(len == 0) debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client (zhave = %ld, zsent = %ld, need to send = %ld).", w->id, w->response.zhave, w->response.zsent, w->response.zhave - w->response.zsent);
-       else debug(D_WEB_CLIENT, "%llu: Failed to send data to client. Reason: %s", w->id, strerror(errno));
+       else if(len == 0) {
+               debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client (zhave = %ld, zsent = %ld, need to send = %ld).", 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
 
-long web_client_send(struct web_client *w)
-{
+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
 
-       long bytes;
+       ssize_t bytes;
 
        if(unlikely(w->response.data->len - w->response.sent == 0)) {
                // there is nothing to send
@@ -1580,42 +2269,49 @@ long web_client_send(struct web_client *w)
                // 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->ifd != w->ofd && w->response.rlen && w->response.rlen > w->response.data->len) {
+               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);
+                       return 0;
                }
 
-               if(unlikely(w->keepalive == 0)) {
+               if(unlikely(!w->keepalive)) {
                        debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %ld bytes sent.", w->id, w->response.sent);
-                       errno = 0;
-                       return(-1);
+                       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);
+               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 %d bytes.", w->id, bytes);
        }
-       else if(likely(bytes == 0)) debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client.", w->id);
-       else debug(D_WEB_CLIENT, "%llu: Failed to send data to client.", w->id);
+       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);
 }
 
-long web_client_receive(struct web_client *w)
+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);
 
-       long left = w->response.data->size - w->response.data->len;
-       long bytes;
+       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));
@@ -1623,6 +2319,9 @@ long web_client_receive(struct web_client *w)
                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';
@@ -1632,7 +2331,9 @@ long web_client_receive(struct web_client *w)
 
                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;
+
+                       if(w->response.rlen && w->response.data->len >= w->response.rlen)
+                               w->wait_receive = 0;
                }
        }
        else if(likely(bytes == 0)) {
@@ -1646,13 +2347,20 @@ long web_client_receive(struct web_client *w)
                        // we are copying data from ifd to ofd
                        // let it finish copying...
                        w->wait_receive = 0;
-                       debug(D_WEB_CLIENT, "%llu: Disabling input.", w->id);
+
+                       debug(D_WEB_CLIENT, "%llu: Read the whole file.", w->id);
+                       if(w->ifd != w->ofd) close(w->ifd);
+                       w->ifd = w->ofd;
                }
                else {
-                       bytes = -1;
-                       errno = 0;
+                       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);
 }
@@ -1674,97 +2382,125 @@ void *web_client_main(void *ptr)
        if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
                error("Cannot set pthread cancel state to ENABLE.");
 
-       struct timeval tv;
        struct web_client *w = ptr;
-       int retval;
-       fd_set ifds, ofds, efds;
-       int fdmax = 0;
+       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(;;) {
-               FD_ZERO (&ifds);
-               FD_ZERO (&ofds);
-               FD_ZERO (&efds);
-
-               FD_SET(w->ifd, &efds);
-
-               if(w->ifd != w->ofd)
-                       FD_SET(w->ofd, &efds);
-
-               if (w->wait_receive) {
-                       FD_SET(w->ifd, &ifds);
-                       if(w->ifd > fdmax) fdmax = w->ifd;
+               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.");
+                       break;
                }
 
-               if (w->wait_send) {
-                       FD_SET(w->ofd, &ofds);
-                       if(w->ofd > fdmax) fdmax = w->ofd;
+               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;
                }
 
-               tv.tv_sec = web_client_timeout;
-               tv.tv_usec = 0;
+               if(w->ifd == w->ofd) {
+                       fds[0].fd = w->ifd;
+                       fds[0].events = 0;
+                       fds[0].revents = 0;
 
-               debug(D_WEB_CLIENT, "%llu: Waiting socket async I/O for %s %s", w->id, w->wait_receive?"INPUT":"", w->wait_send?"OUTPUT":"");
-               retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
+                       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;
 
-               if(retval == -1) {
-                       debug(D_WEB_CLIENT_ACCESS, "%llu: LISTENER: select() failed.", w->id);
-                       continue;
+                       ifd = ofd = &fds[0];
+
+                       fdmax = 1;
                }
-               else if(!retval) {
-                       // timeout
-                       debug(D_WEB_CLIENT_ACCESS, "%llu: LISTENER: timeout.", w->id);
-                       break;
+               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;
                }
 
-               if(FD_ISSET(w->ifd, &efds)) {
-                       debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on input socket.", w->id);
+               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;
                }
-
-               if(FD_ISSET(w->ofd, &efds)) {
-                       debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on output socket.", w->id);
+               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;
                }
 
-               if(w->wait_send && FD_ISSET(w->ofd, &ofds)) {
-                       long bytes;
-                       if((bytes = web_client_send(w)) < 0) {
+               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);
-                               errno = 0;
                                break;
                        }
-
-                       global_statistics_lock();
-                       global_statistics.bytes_sent += bytes;
-                       global_statistics_unlock();
                }
 
-               if(w->wait_receive && FD_ISSET(w->ifd, &ifds)) {
-                       long bytes;
-                       if((bytes = web_client_receive(w)) < 0) {
+               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);
-                               errno = 0;
                                break;
                        }
 
-                       global_statistics_lock();
-                       global_statistics.bytes_received += bytes;
-                       global_statistics_unlock();
-
                        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);
 
-       web_client_reset(w);
+       // 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;
 }