Sfoglia il codice sorgente

'Upgrade' connections: simplify daemon options

Evgeny Grin (Karlson2k) 9 anni fa
parent
commit
cbd9376691

+ 5 - 8
src/include/microhttpd.h

@@ -126,7 +126,7 @@ typedef intptr_t ssize_t;
  * Current version of the library.
  * 0x01093001 = 1.9.30-1.
  */
-#define MHD_VERSION 0x00095204
+#define MHD_VERSION 0x00095205
 
 /**
  * MHD-internal return code for "YES".
@@ -686,14 +686,11 @@ enum MHD_FLAG
   MHD_USE_TCP_FASTOPEN = 16384,
 
   /**
-   * You need to set this option if you want to use epoll() in
-   * combination with HTTPS connections and switching protocols via
-   * connection upgrades (via #MHD_create_response_for_upgrade()).
-   * This flag is required as under these circumstances we need to
-   * open up an extra file descriptor, which we do not want to do
-   * unless necessary.
+   * You need to set this option if you want to use HTTP "Upgrade".
+   * "Upgrade" may require usage of additional internal resources,
+   * which we do not want to use unless necessary.
    */
-  MHD_USE_TLS_EPOLL_UPGRADE = 32768 | MHD_USE_SUSPEND_RESUME | MHD_USE_EPOLL | MHD_USE_TLS
+  MHD_ALLOW_UPGRADE = 32768
 
 };
 

+ 5 - 17
src/microhttpd/connection.c

@@ -3437,33 +3437,21 @@ MHD_queue_response (struct MHD_Connection *connection,
 	 (MHD_CONNECTION_FOOTERS_RECEIVED != connection->state) ) )
     return MHD_NO;
   daemon = connection->daemon;
-  if ( (MHD_HTTP_SWITCHING_PROTOCOLS != status_code) &&
-       (NULL != response->upgrade_handler) )
-    {
-#ifdef HAVE_MESSAGES
-      MHD_DLOG (daemon,
-                _("Application used invalid status code for 'upgrade' response!\n"));
-#endif
-      return MHD_NO;
-    }
   if ( (NULL != response->upgrade_handler) &&
-       (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
-       (0 == (daemon->options & MHD_USE_SUSPEND_RESUME)) )
+       (0 == (daemon->options & MHD_ALLOW_UPGRADE)) )
     {
 #ifdef HAVE_MESSAGES
       MHD_DLOG (daemon,
-                _("Application attempted 'upgrade' without setting MHD_USE_SUSPEND_RESUME!\n"));
+                _("Attempted 'upgrade' connection on daemon without MHD_ALLOW_UPGRADE option!\n"));
 #endif
       return MHD_NO;
     }
-  if ( (NULL != response->upgrade_handler) &&
-       (0 != (MHD_USE_EPOLL & daemon->options)) &&
-       (0 != (MHD_USE_TLS & daemon->options)) &&
-       (MHD_USE_TLS_EPOLL_UPGRADE != (MHD_USE_TLS_EPOLL_UPGRADE & daemon->options)) )
+  if ( (MHD_HTTP_SWITCHING_PROTOCOLS != status_code) &&
+       (NULL != response->upgrade_handler) )
     {
 #ifdef HAVE_MESSAGES
       MHD_DLOG (daemon,
-                _("Application attempted 'upgrade' HTTPS connection in epoll mode without setting MHD_USE_HTTPS_EPOLL_UPGRADE!\n"));
+                _("Application used invalid status code for 'upgrade' response!\n"));
 #endif
       return MHD_NO;
     }

+ 6 - 1
src/microhttpd/daemon.c

@@ -4604,7 +4604,7 @@ setup_epoll_to_listen (struct MHD_Daemon *daemon)
   if (-1 == daemon->epoll_fd)
     return MHD_NO;
 #if HTTPS_SUPPORT
-  if (MHD_USE_TLS_EPOLL_UPGRADE == (MHD_USE_TLS_EPOLL_UPGRADE & daemon->options))
+  if (0 != (MHD_ALLOW_UPGRADE & daemon->options))
     {
        daemon->epoll_upgrade_fd = setup_epoll_fd (daemon);
        if (MHD_INVALID_SOCKET == daemon->epoll_upgrade_fd)
@@ -4751,6 +4751,11 @@ MHD_start_daemon_va (unsigned int flags,
   daemon->custom_error_log = (MHD_LogCallback) &vfprintf;
   daemon->custom_error_log_cls = stderr;
 #endif
+  if (0 != (daemon->options & MHD_ALLOW_UPGRADE))
+    {
+      daemon->options |= MHD_USE_SUSPEND_RESUME;
+      flags |= MHD_USE_SUSPEND_RESUME;
+    }
 #ifdef HAVE_LISTEN_SHUTDOWN
   use_itc = (0 != (daemon->options & (MHD_USE_NO_LISTEN_SOCKET | MHD_USE_ITC)));
 #else

+ 3 - 0
src/microhttpd/response.c

@@ -691,6 +691,9 @@ MHD_response_execute_upgrade_ (struct MHD_Response *response,
   struct MHD_UpgradeResponseHandle *urh;
   size_t rbo;
 
+  if (0 == (daemon->options & MHD_ALLOW_UPGRADE))
+    return MHD_NO;
+
   if (NULL ==
       MHD_get_response_header (response,
                                MHD_HTTP_HEADER_UPGRADE))

+ 1 - 1
src/microhttpd/test_upgrade.c

@@ -61,7 +61,7 @@ test_upgrade (int flags,
 
   done = 0;
 
-  d = MHD_start_daemon (flags | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
+  d = MHD_start_daemon (flags | MHD_USE_DEBUG | MHD_ALLOW_UPGRADE,
                         1080,
                         NULL, NULL,
                         &ahc_upgrade, NULL,

+ 4 - 4
src/microhttpd/test_upgrade_ssl.c

@@ -139,7 +139,7 @@ test_upgrade (int flags,
 
   done = 0;
 
-  d = MHD_start_daemon (flags | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME |MHD_USE_TLS,
+  d = MHD_start_daemon (flags | MHD_USE_DEBUG | MHD_ALLOW_UPGRADE | MHD_USE_TLS,
                         1080,
                         NULL, NULL,
                         &ahc_upgrade, NULL,
@@ -198,7 +198,7 @@ main (int argc,
   error_count += test_upgrade (0,
                                0);
 #ifdef EPOLL_SUPPORT
-  error_count += test_upgrade (MHD_USE_TLS_EPOLL_UPGRADE,
+  error_count += test_upgrade (MHD_USE_EPOLL | MHD_USE_TLS,
                                0);
 #endif
 
@@ -221,10 +221,10 @@ main (int argc,
 #endif
 #ifdef EPOLL_SUPPORT
   error_count += test_upgrade (MHD_USE_EPOLL_INTERNALLY |
-                               MHD_USE_TLS_EPOLL_UPGRADE,
+                               MHD_USE_TLS,
                                0);
   error_count += test_upgrade (MHD_USE_EPOLL_INTERNALLY |
-                               MHD_USE_TLS_EPOLL_UPGRADE,
+                               MHD_USE_TLS,
                                2);
 #endif
   /* report result */