httpd: process client I/O before polling
authorRémi Denis-Courmont <remi@remlab.net>
Sat, 6 Jun 2020 17:21:10 +0000 (20:21 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Mon, 8 Jun 2020 13:50:39 +0000 (16:50 +0300)
In case of TLS, the TCP socket poll state does not necessarily match
the TLS stream state: data may be in the library-side TLS buffers.
So we need to try to read/write regardless of the socket events.

Refs #24824.

src/network/httpd.c

index ae19f81..6e1aed8 100644 (file)
@@ -1727,6 +1727,21 @@ static void httpdLoop(httpd_host_t *host)
 
     int canc = vlc_savecancel();
     vlc_list_foreach(cl, &host->clients, node) {
+        int val = -1;
+
+        switch (cl->i_state) {
+            case HTTPD_CLIENT_RECEIVING:
+                val = httpd_ClientRecv(cl);
+                break;
+            case HTTPD_CLIENT_SENDING:
+                val = httpd_ClientSend(cl);
+                break;
+            case HTTPD_CLIENT_TLS_HS_IN:
+            case HTTPD_CLIENT_TLS_HS_OUT:
+                httpd_ClientTlsHandshake(host, cl);
+                break;
+        }
+
         if (cl->i_state == HTTPD_CLIENT_DEAD
          || (cl->i_activity_timeout > 0
           && cl->i_activity_date + cl->i_activity_timeout < now)) {
@@ -1735,6 +1750,9 @@ static void httpdLoop(httpd_host_t *host)
             continue;
         }
 
+        if (val == 0)
+            cl->i_activity_date = now;
+
         struct pollfd *pufd = ufd + nfd;
         assert (pufd < ufd + ARRAY_SIZE (ufd));
 
@@ -1984,35 +2002,6 @@ static void httpdLoop(httpd_host_t *host)
     now = vlc_tick_now();
     nfd = host->nfd;
 
-    vlc_list_foreach(cl, &host->clients, node) {
-        const struct pollfd *pufd = &ufd[nfd];
-        int val = -1;
-
-        assert(pufd < &ufd[ARRAY_SIZE(ufd)]);
-
-        if (vlc_tls_GetFD(cl->sock) != pufd->fd)
-            continue; // we were not waiting for this client
-        ++nfd;
-        if (pufd->revents == 0)
-            continue; // no event received
-
-        switch (cl->i_state) {
-            case HTTPD_CLIENT_RECEIVING:
-                val = httpd_ClientRecv(cl);
-                break;
-            case HTTPD_CLIENT_SENDING:
-                val = httpd_ClientSend(cl);
-                break;
-            case HTTPD_CLIENT_TLS_HS_IN:
-            case HTTPD_CLIENT_TLS_HS_OUT:
-                httpd_ClientTlsHandshake(host, cl);
-                break;
-        }
-
-        if (val == 0)
-            cl->i_activity_date = now;
-    }
-
     /* Handle server sockets (accept new connections) */
     for (nfd = 0; nfd < host->nfd; nfd++) {
         int fd = ufd[nfd].fd;