prevent assertion failure
[oweals/gnunet.git] / src / transport / plugin_transport_http_server.c
index 2d6f40d58fb91c2141e8661b941b0412602ae9b2..6a9c1b0baba318e9002aede5072a3ebdc1264ee8 100644 (file)
@@ -92,6 +92,11 @@ struct ServerRequest
    */
   int connected;
 
+  /**
+   * Currently suspended
+   */
+  bool suspended;
+
 };
 
 
@@ -501,7 +506,9 @@ server_wake_up (void *cls)
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Session %p: Waking up PUT handle\n",
        s);
+  GNUNET_assert (s->server_recv->suspended);
   MHD_resume_connection (s->server_recv->mhd_conn);
+  s->server_recv->suspended = false;
 }
 
 
@@ -541,7 +548,11 @@ server_delete_session (struct GNUNET_ATS_Session *s)
     GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
     s->recv_wakeup_task = NULL;
     if (NULL != s->server_recv)
+    {
+      GNUNET_assert (s->server_recv->suspended);
+      s->server_recv->suspended = false;
       MHD_resume_connection (s->server_recv->mhd_conn);
+    }
   }
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
@@ -578,6 +589,11 @@ server_delete_session (struct GNUNET_ATS_Session *s)
     MHD_set_connection_option (s->server_send->mhd_conn,
                                MHD_CONNECTION_OPTION_TIMEOUT,
                                1 /* 0 = no timeout, so this is MIN */);
+    if (s->server_send->suspended)
+    {
+      s->server_send->suspended = false;
+      MHD_resume_connection (s->server_send->mhd_conn);
+    }
     server_reschedule (plugin,
                       s->server_send->mhd_daemon,
                       GNUNET_YES);
@@ -760,9 +776,16 @@ http_server_plugin_send (void *cls,
   GNUNET_free (stat_txt);
 
   if (NULL != session->server_send)
+  {
+    if (session->server_send->suspended)
+    {
+      MHD_resume_connection (session->server_send->mhd_conn);
+      session->server_send->suspended = false;
+    }
     server_reschedule (session->plugin,
                        session->server_send->mhd_daemon,
                        GNUNET_YES);
+  }
   return bytes_sent;
 }
 
@@ -1613,6 +1636,12 @@ server_send_callback (void *cls,
          s);
     return MHD_CONTENT_READER_END_OF_STREAM;
   }
+  else
+  {
+    MHD_suspend_connection (s->server_send->mhd_conn);
+    s->server_send->suspended = true;
+    return 0;
+  }
   return bytes_read;
 }
 
@@ -1622,7 +1651,7 @@ server_send_callback (void *cls,
  *
  * @param cls current session as closure
  * @param message the message to be forwarded to transport service
- * @return #GNUNET_OK
+ * @return #GNUNET_OK (all OK)
  */
 static int
 server_receive_mst_cb (void *cls,
@@ -1868,6 +1897,7 @@ server_access_cb (void *cls,
                                                            GNUNET_YES));
         GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
         MHD_suspend_connection (s->server_recv->mhd_conn);
+        s->server_recv->suspended = true;
         if (NULL == s->recv_wakeup_task)
           s->recv_wakeup_task
            = GNUNET_SCHEDULER_add_delayed (delay,
@@ -1946,6 +1976,37 @@ server_disconnect_cb (void *cls,
 }
 
 
+/**
+ * Callback from MHD when a connection starts/stops
+ *
+ * @param cls closure with the `struct HTTP_Server_Plugin *`
+ * @param connection connection handle
+ * @param socket_context socket-specific pointer
+ * @param toe reason for connection notification
+ * @see #MHD_OPTION_NOTIFY_CONNECTION
+ */
+static void
+server_connection_cb (void *cls,
+                      struct MHD_Connection *connection,
+                      void **socket_context,
+                      enum MHD_ConnectionNotificationCode toe)
+{
+  struct HTTP_Server_Plugin *plugin = cls;
+  const union MHD_ConnectionInfo *info;
+
+  if (MHD_CONNECTION_NOTIFY_STARTED == toe)
+    return;
+
+  /* Reschedule to remove closed socket from our select set */
+  info = MHD_get_connection_info (connection,
+                                  MHD_CONNECTION_INFO_DAEMON);
+  GNUNET_assert (NULL != info);
+  server_reschedule (plugin,
+                     info->daemon,
+                     GNUNET_YES);
+}
+
+
 /**
  * Check if incoming connection is accepted.
  *
@@ -2224,9 +2285,11 @@ run_mhd_start_daemon (struct HTTP_Server_Plugin *plugin,
                              timeout,
                              MHD_OPTION_CONNECTION_MEMORY_LIMIT,
                              (size_t) (2 *
-                                       GNUNET_SERVER_MAX_MESSAGE_SIZE),
+                                       GNUNET_MAX_MESSAGE_SIZE),
                              MHD_OPTION_NOTIFY_COMPLETED,
                              &server_disconnect_cb, plugin,
+                             MHD_OPTION_NOTIFY_CONNECTION,
+                             &server_connection_cb, plugin,
                              MHD_OPTION_EXTERNAL_LOGGER,
                              &server_log, NULL,
                              MHD_OPTION_END);