gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r37819 - libmicrohttpd/src/microhttpd


From: gnunet
Subject: [GNUnet-SVN] r37819 - libmicrohttpd/src/microhttpd
Date: Sun, 28 Aug 2016 12:49:19 +0200

Author: grothoff
Date: 2016-08-28 12:49:19 +0200 (Sun, 28 Aug 2016)
New Revision: 37819

Modified:
   libmicrohttpd/src/microhttpd/connection.c
   libmicrohttpd/src/microhttpd/connection_https.c
   libmicrohttpd/src/microhttpd/daemon.c
Log:
more work on upgrade implementation

Modified: libmicrohttpd/src/microhttpd/connection.c
===================================================================
--- libmicrohttpd/src/microhttpd/connection.c   2016-08-28 09:21:39 UTC (rev 
37818)
+++ libmicrohttpd/src/microhttpd/connection.c   2016-08-28 10:49:19 UTC (rev 
37819)
@@ -1834,8 +1834,8 @@
 
 
 /**
- * Try reading data from the socket into the
- * read buffer of the connection.
+ * Try reading data from the socket into the read buffer of the
+ * connection.
  *
  * @param connection connection we're processing
  * @return #MHD_YES if something changed,

Modified: libmicrohttpd/src/microhttpd/connection_https.c
===================================================================
--- libmicrohttpd/src/microhttpd/connection_https.c     2016-08-28 09:21:39 UTC 
(rev 37818)
+++ libmicrohttpd/src/microhttpd/connection_https.c     2016-08-28 10:49:19 UTC 
(rev 37819)
@@ -49,7 +49,7 @@
   int ret;
 
   connection->last_activity = MHD_monotonic_sec_counter();
-  if (connection->state == MHD_TLS_CONNECTION_INIT)
+  if (MHD_TLS_CONNECTION_INIT == connection->state)
     {
       ret = gnutls_handshake (connection->tls_session);
       if (ret == GNUTLS_E_SUCCESS)
@@ -58,8 +58,8 @@
          connection->state = MHD_CONNECTION_INIT;
          return MHD_YES;
        }
-      if ( (ret == GNUTLS_E_AGAIN) ||
-          (ret == GNUTLS_E_INTERRUPTED) )
+      if ( (GNUTLS_E_AGAIN == ret) ||
+          (GNUTLS_E_INTERRUPTED == ret) )
        {
          /* handshake not done */
          return MHD_YES;
@@ -141,7 +141,8 @@
             MHD_state_to_string (connection->state));
 #endif
   timeout = connection->connection_timeout;
-  if ( (timeout != 0) && (timeout <= (MHD_monotonic_sec_counter() - 
connection->last_activity)))
+  if ( (timeout != 0) &&
+       (timeout <= (MHD_monotonic_sec_counter() - connection->last_activity)))
     MHD_connection_close_ (connection,
                            MHD_REQUEST_TERMINATED_TIMEOUT_REACHED);
   switch (connection->state)
@@ -151,7 +152,8 @@
       break;
       /* close connection if necessary */
     case MHD_CONNECTION_CLOSED:
-      gnutls_bye (connection->tls_session, GNUTLS_SHUT_RDWR);
+      gnutls_bye (connection->tls_session,
+                  GNUTLS_SHUT_RDWR);
       return MHD_connection_handle_idle (connection);
     default:
       if ( (0 != gnutls_record_check_pending (connection->tls_session)) &&

Modified: libmicrohttpd/src/microhttpd/daemon.c
===================================================================
--- libmicrohttpd/src/microhttpd/daemon.c       2016-08-28 09:21:39 UTC (rev 
37818)
+++ libmicrohttpd/src/microhttpd/daemon.c       2016-08-28 10:49:19 UTC (rev 
37819)
@@ -123,7 +123,7 @@
 MHD_PanicCallback mhd_panic;
 
 /**
- * Closure argument for "mhd_panic".
+ * Closure argument for #mhd_panic.
  */
 void *mhd_panic_cls;
 
@@ -401,7 +401,9 @@
  * @return number of bytes actually received
  */
 static ssize_t
-recv_tls_adapter (struct MHD_Connection *connection, void *other, size_t i)
+recv_tls_adapter (struct MHD_Connection *connection,
+                  void *other,
+                  size_t i)
 {
   ssize_t res;
 
@@ -410,7 +412,9 @@
       connection->daemon->num_tls_read_ready--;
       connection->tls_read_ready = MHD_NO;
     }
-  res = gnutls_record_recv (connection->tls_session, other, i);
+  res = gnutls_record_recv (connection->tls_session,
+                            other,
+                            i);
   if ( (GNUTLS_E_AGAIN == res) ||
        (GNUTLS_E_INTERRUPTED == res) )
     {
@@ -447,11 +451,14 @@
  */
 static ssize_t
 send_tls_adapter (struct MHD_Connection *connection,
-                  const void *other, size_t i)
+                  const void *other,
+                  size_t i)
 {
   int res;
 
-  res = gnutls_record_send (connection->tls_session, other, i);
+  res = gnutls_record_send (connection->tls_session,
+                            other,
+                            i);
   if ( (GNUTLS_E_AGAIN == res) ||
        (GNUTLS_E_INTERRUPTED == res) )
     {
@@ -2157,7 +2164,124 @@
 }
 
 
+#if HTTPS_SUPPORT
 /**
+ * Performs bi-directional forwarding on upgraded HTTPS connections
+ * based on the readyness state stored in the @a urh handle.
+ *
+ * @param urh handle to process
+ */
+static void
+process_urh (struct MHD_UpgradeResponseHandle *urh)
+{
+#if FIXME_BUFFERS
+  // FIXME: we need buffer/buffer_size/buffer_off for
+  // both directions to be somehow stored within urh.
+  // (Note that despite using the same variable names
+  // below, we need actually different buffers for each
+  // direction.)
+
+  /* handle reading from HTTPS client and writing to application */
+  if ( (0 != (MHD_EPOLL_STATE_READ_READY & urh->celi_client)) &&
+       (buffer_off < buffer_size) )
+    {
+      ssize_t res;
+
+      res = gnutls_record_recv (uri->connection->tls_session,
+                                &buffer[buffer_off],
+                                buffer_size - buffer_off);
+      if ( (GNUTLS_E_AGAIN == res) ||
+           (GNUTLS_E_INTERRUPTED == res) )
+        {
+          urh->celi_client &= ~MHD_EPOLL_STATE_READ_READY;
+        }
+      else if (res > 0)
+        {
+          buffer_off += res;
+        }
+    }
+  if ( (0 != (MHD_EPOLL_STATE_WRITE_READY & urh->celi_mhd)) &&
+       (buffer_off > 0) )
+    {
+      size_t res;
+
+      res = write (urh->mhd_socket,
+                   buffer,
+                   buffer_off);
+      if (-1 == res)
+        {
+          /* FIXME: differenciate by errno? */
+          urh->celi_mhd &= ~MHD_EPOLL_STATE_WRITE_READY;
+        }
+      else
+        {
+          if (buffer_off != res)
+            {
+              memmove (buffer,
+                       &buffer[res],
+                       buffer_off - res);
+              buffer_off -= res;
+            }
+          else
+            {
+              buffer_off = 0;
+            }
+        }
+    }
+
+  /* handle reading from application and writing to HTTPS client */
+  if ( (0 != (MHD_EPOLL_STATE_READ_READY & urh->celi_mhd)) &&
+       (buffer_off < buffer_size) )
+    {
+      size_t res;
+
+      res = read (urh->mhd_socket,
+                  &buffer[buffer_off],
+                  buffer_size - buffer_off);
+      if (-1 == res)
+        {
+          /* FIXME: differenciate by errno? */
+          urh->celi_mhd &= ~MHD_EPOLL_STATE_READ_READY;
+        }
+      else
+        {
+          buffer_off += res;
+        }
+    }
+  if ( (0 != (MHD_EPOLL_STATE_WRITE_READY & urh->celi_client)) &&
+       (buffer_off > 0) )
+    {
+      ssize_t res;
+
+      res = gnutls_record_send (uri->connection->tls_session,
+                                buffer,
+                                buffer_off);
+      if ( (GNUTLS_E_AGAIN == res) ||
+           (GNUTLS_E_INTERRUPTED == res) )
+        {
+          urh->celi_client &= ~MHD_EPOLL_STATE_WRITE_READY;
+        }
+      else if (res > 0)
+        {
+          if (buffer_off != res)
+            {
+              memmove (buffer,
+                       &buffer[res],
+                       buffer_off - res);
+              buffer_off -= res;
+            }
+          else
+            {
+              buffer_off = 0;
+            }
+        }
+    }
+#endif
+}
+#endif
+
+
+/**
  * Run webserver operations. This method should be called by clients
  * in combination with #MHD_get_fdset if the client-controlled select
  * method is used.
@@ -2185,7 +2309,9 @@
   MHD_socket ds;
   struct MHD_Connection *pos;
   struct MHD_Connection *next;
+#if HTTPS_SUPPORT
   struct MHD_UpgradeResponseHandle *urh;
+#endif
   unsigned int mask = MHD_USE_SUSPEND_RESUME | MHD_USE_EPOLL_INTERNALLY |
     MHD_USE_SELECT_INTERNALLY | MHD_USE_POLL_INTERNALLY | 
MHD_USE_THREAD_PER_CONNECTION;
 
@@ -2239,8 +2365,17 @@
 #if HTTPS_SUPPORT
   for (urh = daemon->urh_head; NULL != urh; urh = urh->next)
     {
-      // if ( (0 != (MHD_EPOLL_STATE_READ_READY & urh->celi_mhd)) &&
-      // (0 != (MHD_EPOLL_STATE_WRITE_READY & urh->celi_client)) )
+      /* update urh state based on select() output */
+      if (FD_ISSET (urh->connection->socket_fd, read_fd_set))
+        urh->celi_client |= MHD_EPOLL_STATE_READ_READY;
+      if (FD_ISSET (urh->connection->socket_fd, write_fd_set))
+        urh->celi_client |= MHD_EPOLL_STATE_WRITE_READY;
+      if (FD_ISSET (urh->mhd_socket, read_fd_set))
+        urh->celi_mhd |= MHD_EPOLL_STATE_READ_READY;
+      if (FD_ISSET (urh->mhd_socket, write_fd_set))
+        urh->celi_mhd |= MHD_EPOLL_STATE_WRITE_READY;
+      /* call generic forwarding function for passing data */
+      process_urh (urh);
     }
 #endif
   MHD_cleanup_connections (daemon);
@@ -2298,10 +2433,10 @@
     {
       /* accept only, have one thread per connection */
       if ( (MHD_INVALID_SOCKET != daemon->socket_fd) &&
-           (!MHD_add_to_fd_set_ (daemon->socket_fd,
-                                 &rs,
-                                 &maxsock,
-                                 FD_SETSIZE)) )
+           (! MHD_add_to_fd_set_ (daemon->socket_fd,
+                                  &rs,
+                                  &maxsock,
+                                  FD_SETSIZE)) )
         {
 #ifdef HAVE_MESSAGES
           MHD_DLOG (daemon, "Could not add listen socket to fdset");
@@ -2310,10 +2445,10 @@
         }
     }
   if ( (MHD_INVALID_PIPE_ != daemon->wpipe[0]) &&
-       (!MHD_add_to_fd_set_ (daemon->wpipe[0],
-                             &rs,
-                             &maxsock,
-                             FD_SETSIZE)) )
+       (! MHD_add_to_fd_set_ (daemon->wpipe[0],
+                              &rs,
+                              &maxsock,
+                              FD_SETSIZE)) )
     {
 #if defined(MHD_WINSOCK_SOCKETS)
       /* fdset limit reached, new connections
@@ -2322,10 +2457,10 @@
       if (MHD_INVALID_SOCKET != daemon->socket_fd)
         {
           FD_CLR (daemon->socket_fd, &rs);
-          if (!MHD_add_to_fd_set_ (daemon->wpipe[0],
-                                   &rs,
-                                   &maxsock,
-                                   FD_SETSIZE))
+          if (! MHD_add_to_fd_set_ (daemon->wpipe[0],
+                                    &rs,
+                                    &maxsock,
+                                    FD_SETSIZE))
             {
 #endif /* MHD_WINSOCK_SOCKETS */
 #ifdef HAVE_MESSAGES
@@ -2410,6 +2545,10 @@
   unsigned int num_connections;
   struct MHD_Connection *pos;
   struct MHD_Connection *next;
+#if HTTPS_SUPPORT
+  struct MHD_UpgradeResponseHandle *urh;
+  struct MHD_UpgradeResponseHandle *urhn;
+#endif
 
   if ( (MHD_USE_SUSPEND_RESUME == (daemon->options & MHD_USE_SUSPEND_RESUME)) 
&&
        (MHD_YES == resume_suspended_connections (daemon)) )
@@ -2419,6 +2558,10 @@
   num_connections = 0;
   for (pos = daemon->connections_head; NULL != pos; pos = pos->next)
     num_connections++;
+#if HTTPS_SUPPORT
+  for (urh = daemon->urh_head; NULL != urh; urh = urh->next)
+    num_connections += 2;
+#endif
   {
     MHD_UNSIGNED_LONG_LONG ltimeout;
     unsigned int i;
@@ -2428,7 +2571,7 @@
     int poll_pipe;
     struct pollfd *p;
 
-    p = malloc(sizeof (struct pollfd) * (2 + num_connections));
+    p = malloc (sizeof (struct pollfd) * (2 + num_connections));
     if (NULL == p)
       {
 #ifdef HAVE_MESSAGES
@@ -2493,6 +2636,23 @@
          }
        i++;
       }
+#if HTTPS_SUPPORT
+    for (urh = daemon->urh_head; NULL != urh; urh = urh->next)
+      {
+        p[poll_server+i].fd = urh->connection->socket_fd;
+        if (0 == (MHD_EPOLL_STATE_READ_READY & urh->celi_client))
+          p[poll_server+i].events |= POLLIN;
+        if (0 == (MHD_EPOLL_STATE_WRITE_READY & urh->celi_client))
+          p[poll_server+i].events |= POLLOUT;
+        i++;
+        p[poll_server+i].fd = urh->mhd_socket;
+        if (0 == (MHD_EPOLL_STATE_READ_READY & urh->celi_mhd))
+          p[poll_server+i].events |= POLLIN;
+        if (0 == (MHD_EPOLL_STATE_WRITE_READY & urh->celi_mhd))
+          p[poll_server+i].events |= POLLOUT;
+        i++;
+      }
+#endif
     if (0 == poll_server + num_connections)
       {
         free(p);
@@ -2542,6 +2702,33 @@
                        0 != (p[poll_server+i].revents & POLLOUT),
                        MHD_NO);
       }
+#if HTTPS_SUPPORT
+    for (urh = daemon->urh_head; NULL != urh; urh = urhn)
+      {
+        urhn = urh->next;
+        if (p[poll_server+i].fd != urh->connection->socket_fd)
+          continue; /* fd mismatch, something else happened, retry later ... */
+        if (0 != (p[poll_server+i].revents & POLLIN))
+          urh->celi_client |= MHD_EPOLL_STATE_READ_READY;
+        if (0 != (p[poll_server+i].revents & POLLOUT))
+          urh->celi_client |= MHD_EPOLL_STATE_WRITE_READY;
+        i++;
+        if (p[poll_server+i].fd != urh->mhd_socket)
+          {
+            /* fd mismatch, something else happened, retry later ... */
+            /* may still be able to do something based on updates
+               to socket_fd availability */
+            process_urh (urh);
+            continue;
+          }
+        if (0 != (p[poll_server+i].revents & POLLIN))
+          urh->celi_mhd |= MHD_EPOLL_STATE_READ_READY;
+        if (0 != (p[poll_server+i].revents & POLLOUT))
+          urh->celi_mhd |= MHD_EPOLL_STATE_WRITE_READY;
+        i++;
+        process_urh (urh);
+      }
+#endif
     /* handle 'listen' FD */
     if ( (-1 != poll_listen) &&
         (0 != (p[poll_listen].revents & POLLIN)) )
@@ -3081,9 +3268,10 @@
  * @param format format string
  * @param va arguments to the format string (fprintf-style)
  */
-typedef void (*VfprintfFunctionPointerType)(void *cls,
-                                           const char *format,
-                                           va_list va);
+typedef void
+(*VfprintfFunctionPointerType)(void *cls,
+                               const char *format,
+                               va_list va);
 
 
 /**
@@ -3687,7 +3875,7 @@
   }
   if ( (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL))) &&
        (1 == use_pipe) &&
-       (!MHD_SCKT_FD_FITS_FDSET_(daemon->wpipe[0], NULL)) )
+       (! MHD_SCKT_FD_FITS_FDSET_(daemon->wpipe[0], NULL)) )
     {
 #ifdef HAVE_MESSAGES
       MHD_DLOG (daemon,




reply via email to

[Prev in Thread] Current Thread [Next in Thread]