-makefile for new test_stream_local (commented)
[oweals/gnunet.git] / src / transport / plugin_transport_http_server.c
index 10c8268b11ea078cfd3c58930cc9144a12585ded..634f51c27d1ebe605c69f85b2e61329a8c2d74bd 100644 (file)
@@ -62,7 +62,7 @@ server_log (void *arg, const char *fmt, va_list ap)
 
   vsnprintf (text, sizeof (text), fmt, ap);
   va_end (ap);
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: %s\n", text);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
 }
 
 /**
@@ -95,12 +95,13 @@ static char *
 server_load_file (const char *file)
 {
   struct GNUNET_DISK_FileHandle *gn_file;
-  struct stat fstat;
+  uint64_t fsize;
   char *text = NULL;
 
-  if (0 != STAT (file, &fstat))
+  if (GNUNET_OK != GNUNET_DISK_file_size (file,
+      &fsize, GNUNET_NO, GNUNET_YES))
     return NULL;
-  text = GNUNET_malloc (fstat.st_size + 1);
+  text = GNUNET_malloc (fsize + 1);
   gn_file =
       GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
                              GNUNET_DISK_PERM_USER_READ);
@@ -109,13 +110,13 @@ server_load_file (const char *file)
     GNUNET_free (text);
     return NULL;
   }
-  if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
+  if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
   {
     GNUNET_free (text);
     GNUNET_DISK_file_close (gn_file);
     return NULL;
   }
-  text[fstat.st_size] = '\0';
+  text[fsize] = '\0';
   GNUNET_DISK_file_close (gn_file);
   return text;
 }
@@ -156,11 +157,9 @@ server_load_certificate (struct Plugin *plugin)
   }
 
   /* read key & certificates from file */
-#if VERBOSE_SERVER
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
               key_file, cert_file);
-#endif
 
   plugin->key = server_load_file (key_file);
   plugin->cert = server_load_file (cert_file);
@@ -180,7 +179,7 @@ server_load_certificate (struct Plugin *plugin)
 #endif
     errno = 0;
     cert_creation =
-        GNUNET_OS_start_process (NULL, NULL,
+        GNUNET_OS_start_process (GNUNET_NO, NULL, NULL,
                                  "gnunet-transport-certificate-creation",
                                  "gnunet-transport-certificate-creation",
                                  key_file, cert_file, NULL);
@@ -280,10 +279,7 @@ server_receive_mst_cb (void *cls, void *client,
                        const struct GNUNET_MessageHeader *message)
 {
   struct Session *s = cls;
-
-#if VERBOSE_SERVER
   struct Plugin *plugin = s->plugin;
-#endif
   struct GNUNET_TIME_Relative delay;
 
   delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
@@ -293,13 +289,11 @@ server_receive_mst_cb (void *cls, void *client,
 
   if (delay.rel_value > 0)
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
                      GNUNET_i2s (&s->target),
                      http_plugin_address_to_string (NULL, s->addr, s->addrlen),
                      delay);
-#endif
   }
 }
 
@@ -316,10 +310,6 @@ server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
 {
   struct Session *s = cls;
 
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
-                   "Server: %X can sent maximum  %u \n", s, max);
-#endif
   struct HTTP_Message *msg;
   int bytes_read = 0;
 
@@ -351,12 +341,10 @@ server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
     }
   }
 
-#if VERBOSE_SERVER
   struct Plugin *plugin = s->plugin;
-
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: %X: sent %u bytes\n", s, bytes_read);
-#endif
+
   return bytes_read;
 }
 
@@ -379,7 +367,7 @@ server_lookup_session (struct Plugin *plugin,
   struct GNUNET_PeerIdentity target;
   int check = GNUNET_NO;
   uint32_t tag = 0;
-  int direction;
+  int direction = GNUNET_SYSERR;
 
   conn_info =
       MHD_get_connection_info (mhd_connection,
@@ -407,19 +395,22 @@ server_lookup_session (struct Plugin *plugin,
 
   if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
     direction = _RECEIVE;
-  if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
+  else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
     direction = _SEND;
+  else
+  {
+    GNUNET_break_op (0);
+    goto error;
+  }
+
 
   if (check == GNUNET_NO)
     goto error;
 
   plugin->cur_connections++;
-
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: New inbound connection from %s with tag %u\n",
                    GNUNET_i2s (&target), tag);
-#endif
   /* find duplicate session */
 
   t = plugin->head;
@@ -436,11 +427,9 @@ server_lookup_session (struct Plugin *plugin,
   }
   if (t != NULL)
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Duplicate session, dismissing new connection from peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
     goto error;
   }
 
@@ -460,20 +449,15 @@ server_lookup_session (struct Plugin *plugin,
 
   if (t == NULL)
     goto create;
-
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: Found existing semi-session for `%s'\n",
                    GNUNET_i2s (&target));
-#endif
 
   if ((direction == _SEND) && (t->server_send != NULL))
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
     goto error;
   }
   else
@@ -482,21 +466,17 @@ server_lookup_session (struct Plugin *plugin,
     GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
                                  plugin->server_semi_tail, s);
     GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Found matching semi-session, merging session for peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
 
     goto found;
   }
   if ((direction == _RECEIVE) && (t->server_recv != NULL))
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
     goto error;
   }
   else
@@ -505,23 +485,17 @@ server_lookup_session (struct Plugin *plugin,
     GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
                                  plugin->server_semi_tail, s);
     GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Found matching semi-session, merging session for peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
     goto found;
   }
 
 create:
 /* create new session */
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: Creating new session for peer `%s' \n",
                    GNUNET_i2s (&target));
-#endif
-
-
   switch (conn_info->client_addr->sa_family)
   {
   case (AF_INET):
@@ -559,10 +533,8 @@ create:
   goto found;
 
 error:
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: Invalid connection request\n");
-#endif
   return NULL;
 
 found:
@@ -578,10 +550,8 @@ found:
 #if MHD_VERSION >= 0x00090E00
   int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
 
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: Setting timeout for %X to %u sec.\n", sc, to);
-#endif
   MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
 
   struct MHD_Daemon *d = NULL;
@@ -642,14 +612,11 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
   /* connection is to be disconnected */
   if (sc->disconnect == GNUNET_YES)
   {
+    /* Sent HTTP/1.1: 200 OK as PUT Response\ */
     response =
         MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
                                        MHD_NO, MHD_NO);
     res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
-#if VERBOSE_SERVER
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Sent HTTP/1.1: 200 OK as PUT Response\n");
-#endif
     MHD_destroy_response (response);
     return MHD_YES;
   }
@@ -658,12 +625,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
   /* Check if both directions are connected */
   if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
   {
-#if VERBOSE_SERVER
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                     "Server: Delayed read from `%s' `%s' since not both semi-connections are connected\n",
-                     GNUNET_i2s (&s->target),
-                     http_plugin_address_to_string (NULL, s->addr, s->addrlen));
-#endif
+    /* Delayed read from since not both semi-connections are connected */
     return MHD_YES;
   }
 
@@ -672,7 +634,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
     response =
         MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
                                            s, NULL);
-    res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
+    MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
     MHD_destroy_response (response);
     return MHD_YES;
   }
@@ -680,41 +642,34 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
   {
     if (*upload_data_size == 0)
     {
-#if VERBOSE_SERVER
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                        "Server: Peer `%s' PUT on address `%s' connected\n",
                        GNUNET_i2s (&s->target),
                        http_plugin_address_to_string (NULL, s->addr,
                                                       s->addrlen));
-#endif
       return MHD_YES;
     }
 
     /* Receiving data */
     if ((*upload_data_size > 0))
     {
-#if VERBOSE_SERVER
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                        "Server: peer `%s' PUT on address `%s' received %u bytes\n",
                        GNUNET_i2s (&s->target),
                        http_plugin_address_to_string (NULL, s->addr,
                                                       s->addrlen),
                        *upload_data_size);
-#endif
       struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
 
       if ((s->next_receive.abs_value <= now.abs_value))
       {
-#if VERBOSE_SERVER
         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                          "Server: %X: PUT with %u bytes forwarded to MST\n", s,
                          *upload_data_size);
-#endif
         if (s->msg_tk == NULL)
         {
           s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
         }
-        res =
             GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
                                        *upload_data_size, GNUNET_NO, GNUNET_NO);
 
@@ -722,31 +677,24 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
         int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
         struct ServerConnection *t = NULL;
 
-#if VERBOSE_SERVER
         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                          "Server: Received %u bytes\n", *upload_data_size);
-#endif
-
         /* Setting timeouts for other connections */
         if (s->server_recv != NULL)
         {
           t = s->server_recv;
-#if VERBOSE_SERVER
           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                            "Server: Setting timeout for %X to %u sec.\n", t,
                            to);
-#endif
           MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
                                      to);
         }
         if (s->server_send != NULL)
         {
           t = s->server_send;
-#if VERBOSE_SERVER
           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                            "Server: Setting timeout for %X to %u sec.\n", t,
                            to);
-#endif
           MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
                                      to);
         }
@@ -762,11 +710,9 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
       }
       else
       {
-#if DEBUG_HTTP
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                     "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
                     s, now.abs_value - s->next_receive.abs_value);
-#endif
       }
       return MHD_YES;
     }
@@ -781,7 +727,7 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
                       void **httpSessionCache)
 {
   struct ServerConnection *sc = *httpSessionCache;
-  struct ServerConnection *tc = *httpSessionCache;
+  struct ServerConnection *tc = NULL;
   struct Session *s = NULL;
   struct Session *t = NULL;
   struct Plugin *plugin = NULL;
@@ -793,12 +739,12 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
   plugin = s->plugin;
   if (sc->direction == _SEND)
   {
-#if VERBOSE_SERVER
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: %X peer `%s' GET on address `%s' disconnected\n",
                      s->server_send, GNUNET_i2s (&s->target),
                      http_plugin_address_to_string (NULL, s->addr, s->addrlen));
-#endif
+
     s->server_send = NULL;
 
     if (s->server_recv != NULL)
@@ -813,12 +759,10 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
   }
   if (sc->direction == _RECEIVE)
   {
-#if VERBOSE_SERVER
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: %X peer `%s' PUT on address `%s' disconnected\n",
                      s->server_recv, GNUNET_i2s (&s->target),
                      http_plugin_address_to_string (NULL, s->addr, s->addrlen));
-#endif
     s->server_recv = NULL;
     if (s->server_send != NULL)
     {
@@ -861,12 +805,10 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
 
   if ((s->server_send == NULL) && (s->server_recv == NULL))
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: peer `%s' on address `%s' disconnected\n",
                      GNUNET_i2s (&s->target),
                      http_plugin_address_to_string (NULL, s->addr, s->addrlen));
-#endif
     if (s->msg_tk != NULL)
     {
       GNUNET_SERVER_mst_destroy (s->msg_tk);
@@ -938,8 +880,8 @@ server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+#if 0
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Running IPv4 server\n");
 #endif
   GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
@@ -966,8 +908,8 @@ server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+#if 0
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Running IPv6 server\n");
 #endif
   GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
@@ -1040,8 +982,8 @@ server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
       GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
       plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
     }
-#if VERBOSE_SERVER
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+#if 0
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Scheduling IPv4 server task in %llu ms\n", tv);
 #endif
     ret =
@@ -1056,8 +998,8 @@ server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
       GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
       plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
     }
-#if VERBOSE_SERVER
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+#if 0
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Scheduling IPv6 server task in %llu ms\n", tv);
 #endif
     ret =
@@ -1208,10 +1150,6 @@ server_stop (struct Plugin *plugin)
   struct Session *s = NULL;
   struct Session *t = NULL;
 
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "server_stop\n");
-#endif
-
   struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
 
   plugin->server_v4 = NULL;
@@ -1245,12 +1183,12 @@ server_stop (struct Plugin *plugin)
   while (s != NULL)
   {
 #if VERBOSE_SERVER
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Deleting semi-sessions %p\n", s);
 #endif
     t = s->next;
     struct HTTP_Message *msg = s->msg_head;
-    struct HTTP_Message *tmp = s->msg_head;
+    struct HTTP_Message *tmp = NULL;
 
     while (msg != NULL)
     {