]> arthur.barton.de Git - netdata.git/blobdiff - src/web_client.c
added configuration options for controlling web compression level and strategy and...
[netdata.git] / src / web_client.c
index 368a24b5c6bd846ed866e09066a43d56804e5409..3a3471ebd3042b297e43a45dcce0fd759bab9219 100644 (file)
@@ -16,6 +16,7 @@
 #include <pwd.h>
 #include <grp.h>
 #include <ctype.h>
+#include <poll.h>
 
 #include "common.h"
 #include "log.h"
 #include "registry.h"
 
 #include "web_client.h"
-#include "../config.h"
 
 #define INITIAL_WEB_DATA_LENGTH 16384
 #define WEB_REQUEST_LENGTH 16384
 #define TOO_BIG_REQUEST 16384
 
 int web_client_timeout = DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS;
-int web_enable_gzip = 1;
+
+#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;
@@ -80,7 +113,6 @@ struct web_client *web_client_create(int listener)
                w->client_port[NI_MAXSERV] = '\0';
 
                switch(sadr->sa_family) {
-
                case AF_INET:
                        debug(D_WEB_CLIENT_ACCESS, "%llu: New IPv4 web client from %s port %s on socket %d.", w->id, w->client_ip, w->client_port, w->ifd);
                        break;
@@ -90,7 +122,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:
@@ -99,7 +132,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);
@@ -141,14 +181,24 @@ struct web_client *web_client_create(int listener)
        return(w);
 }
 
-void web_client_reset(struct web_client *w)
-{
-       struct timeval tv;
-       gettimeofday(&tv, NULL);
+void web_client_reset(struct web_client *w) {
+       web_client_uncrock_socket(w);
 
        debug(D_WEB_CLIENT, "%llu: Reseting client.", w->id);
 
-       if(w->stats_received_bytes || w->stats_sent_bytes) {
+       if(likely(w->last_url[0])) {
+               struct timeval tv;
+               gettimeofday(&tv, NULL);
+
+               size_t size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
+               size_t sent = size;
+#ifdef NETDATA_WITH_ZLIB
+               if(likely(w->response.zoutput)) sent = (size_t)w->response.zstream.total_out;
+#endif
+
+               // --------------------------------------------------------------------
+               // global statistics
+
                if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
                        global_statistics_lock();
 
@@ -156,37 +206,36 @@ void web_client_reset(struct web_client *w)
                global_statistics.web_usec += usecdiff(&tv, &w->tv_in);
                global_statistics.bytes_received += w->stats_received_bytes;
                global_statistics.bytes_sent += w->stats_sent_bytes;
+               global_statistics.content_size += size;
+               global_statistics.compressed_content_size += sent;
 
                if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
                        global_statistics_unlock();
-       }
-       w->stats_received_bytes = 0;
-       w->stats_sent_bytes = 0;
 
-       size_t sent = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
+               w->stats_received_bytes = 0;
+               w->stats_sent_bytes = 0;
 
-#ifdef NETDATA_WITH_ZLIB
-       if(likely(w->response.zoutput)) sent = (size_t)w->response.zstream.total_out;
-#endif
 
-       size_t size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->response.rlen:w->response.data->len;
+               // --------------------------------------------------------------------
+               // access log
 
-       if(likely(w->last_url[0]))
                log_access("%llu: (sent/all = %zu/%zu bytes %0.0f%%, prep/sent/total = %0.2f/%0.2f/%0.2f ms) %s: %d '%s'",
-                       w->id,
-                       sent, size, -((size>0)?((float)(size-sent)/(float)size * 100.0):0.0),
-                       (float)usecdiff(&w->tv_ready, &w->tv_in) / 1000.0,
-                       (float)usecdiff(&tv, &w->tv_ready) / 1000.0,
-                       (float)usecdiff(&tv, &w->tv_in) / 1000.0,
-                       (w->mode == WEB_CLIENT_MODE_FILECOPY)?"filecopy":((w->mode == WEB_CLIENT_MODE_OPTIONS)?"options":"data"),
-                       w->response.code,
-                       w->last_url
+                                  w->id,
+                                  sent, size, -((size > 0) ? ((float) (size - sent) / (float) size * 100.0) : 0.0),
+                                  (float) usecdiff(&w->tv_ready, &w->tv_in) / 1000.0,
+                                  (float) usecdiff(&tv, &w->tv_ready) / 1000.0,
+                                  (float) usecdiff(&tv, &w->tv_in) / 1000.0,
+                                  (w->mode == WEB_CLIENT_MODE_FILECOPY) ? "filecopy" : ((w->mode == WEB_CLIENT_MODE_OPTIONS)
+                                                                                                                                                ? "options" : "data"),
+                                  w->response.code,
+                                  w->last_url
                );
+       }
 
        if(unlikely(w->mode == WEB_CLIENT_MODE_FILECOPY)) {
                if(w->ifd != w->ofd) {
                        debug(D_WEB_CLIENT, "%llu: Closing filecopy input file descriptor %d.", w->id, w->ifd);
-                       close(w->ifd);
+                       if(w->ifd != -1) close(w->ifd);
                        w->ifd = w->ofd;
                }
        }
@@ -217,7 +266,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;
@@ -230,8 +279,9 @@ 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;
 
@@ -251,8 +301,7 @@ struct web_client *web_client_free(struct web_client *w)
        return(n);
 }
 
-uid_t web_files_uid(void)
-{
+uid_t web_files_uid(void) {
        static char *web_owner = NULL;
        static uid_t owner_uid = 0;
 
@@ -266,11 +315,11 @@ uid_t web_files_uid(void)
                        // 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;
                        }
                }
@@ -279,8 +328,7 @@ uid_t web_files_uid(void)
        return(owner_uid);
 }
 
-gid_t web_files_gid(void)
-{
+gid_t web_files_gid(void) {
        static char *web_group = NULL;
        static gid_t owner_gid = 0;
 
@@ -294,11 +342,11 @@ gid_t web_files_gid(void)
                        // while single threaded
                        struct group *gr = getgrnam(web_group);
                        if(!gr) {
-                               error("Group %s is not present. Ignoring option.", web_group);
+                               error("Group '%s' is not present. Ignoring option.", web_group);
                                owner_gid = getegid();
                        }
                        else {
-                               debug(D_WEB_CLIENT, "Web files group set to %s.\n", web_group);
+                               debug(D_WEB_CLIENT, "Web files group set to %s.", web_group);
                                owner_gid = gr->gr_gid;
                        }
                }
@@ -430,12 +478,12 @@ int mysendfile(struct web_client *w, char *filename)
 
 #ifdef NETDATA_WITH_ZLIB
 void web_client_enable_deflate(struct web_client *w, int gzip) {
-       if(w->response.zinitialized) {
+       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;
        }
@@ -462,7 +510,7 @@ void web_client_enable_deflate(struct web_client *w, int gzip) {
 //     }
 
        // Select GZIP compression: windowbits = 15 + 16 = 31
-       if(deflateInit2(&w->response.zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + ((gzip)?16:0), 8, Z_DEFAULT_STRATEGY) != Z_OK) {
+       if(deflateInit2(&w->response.zstream, web_gzip_level, Z_DEFLATED, 15 + ((gzip)?16:0), 8, web_gzip_strategy) != Z_OK) {
                error("%llu: Failed to initialize zlib. Proceeding without compression.", w->id);
                return;
        }
@@ -763,7 +811,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) {
@@ -1720,11 +1768,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
@@ -1732,12 +1775,14 @@ void web_client_process(struct web_client *w) {
                        , buffer_tostring(w->response.header_output)
                        );
 
+       web_client_crock_socket(w);
+
        bytes = send(w->ofd, buffer_tostring(w->response.header_output), buffer_strlen(w->response.header_output), 0);
        if(bytes != (ssize_t) buffer_strlen(w->response.header_output)) {
                if(bytes > 0)
                        w->stats_sent_bytes += bytes;
 
-               error("%llu: HTTP Header failed to be sent (I sent %d bytes but the system sent %d bytes). Closing web client.", 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);
@@ -1746,10 +1791,6 @@ void web_client_process(struct web_client *w) {
        else 
                w->stats_sent_bytes += bytes;
 
-/*     // enable TCP_NODELAY, to send all data immediately at the next send()
-       flag = 1;
-       if(setsockopt(w->ofd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) != 0) error("%llu: failed to enable TCP_NODELAY on socket.", w->id);
-*/
        // enable sending immediately if we have data
        if(w->response.data->len) w->wait_send = 1;
        else w->wait_send = 0;
@@ -1775,8 +1816,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);
                                }
                                */
                        }
@@ -1803,11 +1846,11 @@ ssize_t web_client_send_chunk_header(struct web_client *w, size_t len)
        }
 
        else if(bytes == 0) {
-               error("%llu: Did not send chunk header to the client.", w->id);
+               debug(D_WEB_CLIENT, "%llu: Did not send chunk header to the client.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
        else {
-               error("%llu: Failed to send chunk header to client.", w->id);
+               debug(D_WEB_CLIENT, "%llu: Failed to send chunk header to client.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
 
@@ -1825,11 +1868,11 @@ ssize_t web_client_send_chunk_close(struct web_client *w)
        }
 
        else if(bytes == 0) {
-               error("%llu: Did not send chunk suffix to the client.", w->id);
+               debug(D_WEB_CLIENT, "%llu: Did not send chunk suffix to the client.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
        else {
-               error("%llu: Failed to send chunk suffix to client.", w->id);
+               debug(D_WEB_CLIENT, "%llu: Failed to send chunk suffix to client.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
 
@@ -1847,11 +1890,11 @@ ssize_t web_client_send_chunk_finalize(struct web_client *w)
        }
 
        else if(bytes == 0) {
-               error("%llu: Did not send chunk suffix to the client.", w->id);
+               debug(D_WEB_CLIENT, "%llu: Did not send chunk finalize suffix to the client.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
        else {
-               error("%llu: Failed to send chunk suffix to client.", w->id);
+               debug(D_WEB_CLIENT, "%llu: Failed to send chunk finalize suffix to client.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
 
@@ -1961,11 +2004,11 @@ ssize_t web_client_send_deflate(struct web_client *w)
                debug(D_WEB_CLIENT, "%llu: Sent %d bytes.", w->id, len);
        }
        else if(len == 0) {
-               error("%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);
+               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 {
-               error("%llu: Failed to send data to client.", w->id);
+               debug(D_WEB_CLIENT, "%llu: Failed to send data to client.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
 
@@ -2014,11 +2057,11 @@ ssize_t web_client_send(struct web_client *w) {
                debug(D_WEB_CLIENT, "%llu: Sent %d bytes.", w->id, bytes);
        }
        else if(likely(bytes == 0)) {
-               error("%llu: Did not send any bytes to the client.", w->id);
+               debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
        else {
-               error("%llu: Failed to send data to client.", w->id);
+               debug(D_WEB_CLIENT, "%llu: Failed to send data to client.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
 
@@ -2073,12 +2116,12 @@ ssize_t web_client_receive(struct web_client *w)
                        w->ifd = w->ofd;
                }
                else {
-                       error("%llu: failed to receive data.", w->id);
+                       debug(D_WEB_CLIENT, "%llu: failed to receive data.", w->id);
                        WEB_CLIENT_IS_DEAD(w);
                }
        }
        else {
-               error("%llu: receive data failed.", w->id);
+               debug(D_WEB_CLIENT, "%llu: receive data failed.", w->id);
                WEB_CLIENT_IS_DEAD(w);
        }
 
@@ -2103,78 +2146,88 @@ void *web_client_main(void *ptr)
                error("Cannot set pthread cancel state to ENABLE.");
 
        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(;;) {
                if(unlikely(w->dead)) {
-                       error("%llu: client is dead.", w->id);
+                       debug(D_WEB_CLIENT, "%llu: client is dead.", w->id);
                        break;
                }
                else if(unlikely(!w->wait_receive && !w->wait_send)) {
-                       error("%llu: client is not set for neither receiving nor sending data.");
+                       debug(D_WEB_CLIENT, "%llu: client is not set for neither receiving nor sending data.");
                        break;
                }
 
-               FD_ZERO (&ifds);
-               FD_ZERO (&ofds);
-               FD_ZERO (&efds);
-
-               if(w->ifd < 0 || w->ifd >= FD_SETSIZE || w->ofd < 0 || w->ofd >= FD_SETSIZE) {
-                       error("%llu: invalid file descriptor, ifd = %d, ofd = %d (required 0 <= fd < FD_SETSIZE (%d)", w->id, w->ifd, w->ofd, FD_SETSIZE);
+               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;
                }
 
-               FD_SET(w->ifd, &efds);
+               if(w->ifd == w->ofd) {
+                       fds[0].fd = w->ifd;
+                       fds[0].events = 0;
+                       fds[0].revents = 0;
 
-               if(w->ifd != w->ofd)
-                       FD_SET(w->ofd, &efds);
+                       if(w->wait_receive) fds[0].events |= POLLIN;
+                       if(w->wait_send)    fds[0].events |= POLLOUT;
 
-               if (w->wait_receive) {
-                       FD_SET(w->ifd, &ifds);
-                       if(w->ifd > fdmax) fdmax = w->ifd;
-               }
+                       fds[1].fd = -1;
+                       fds[1].events = 0;
+                       fds[1].revents = 0;
 
-               if (w->wait_send) {
-                       FD_SET(w->ofd, &ofds);
-                       if(w->ofd > fdmax) fdmax = w->ofd;
-               }
+                       ifd = ofd = &fds[0];
 
-               debug(D_WEB_CLIENT, "%llu: Waiting socket async I/O for %s %s", w->id, w->wait_receive?"INPUT":"", w->wait_send?"OUTPUT":"");
-               struct timeval tv = { .tv_sec = web_client_timeout, .tv_usec = 0 };
-               retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
-
-               if(retval == -1) {
-                       error("%llu: LISTENER: select() failed (input fd = %d, output fd = %d). Closing client.", w->id, w->ifd, w->ofd);
-                       break;
+                       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)) {
+               int used = 0;
+               if(w->wait_send && ofd->revents & POLLOUT) {
+                       used++;
                        if(web_client_send(w) < 0) {
                                debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client.", w->id);
                                break;
                        }
                }
 
-               if(w->wait_receive && FD_ISSET(w->ifd, &ifds)) {
+               if(w->wait_receive && (ifd->revents & POLLIN || ifd->revents & POLLPRI)) {
+                       used++;
                        if(web_client_receive(w) < 0) {
                                debug(D_WEB_CLIENT, "%llu: Cannot receive data from client. Closing client.", w->id);
                                break;
@@ -2185,13 +2238,18 @@ void *web_client_main(void *ptr)
                                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) {
@@ -2199,7 +2257,7 @@ void *web_client_main(void *ptr)
        }
        else {
                if(w->ifd != -1) close(w->ifd);
-               if(w->ofd != -1) close(w->ifd);
+               if(w->ofd != -1) close(w->ofd);
        }
        w->ifd = -1;
        w->ofd = -1;