X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Ftransport%2Fplugin_transport_http_server.c;h=8ac301341a966e6f0f8ade82f85ee79722b32c63;hb=56389a7d277b05c9c2968b7ebd529a12f8be15eb;hp=58c8fd2158727cd99ee701105b6879632a7e53c8;hpb=cec8e7eee09c84d32e7e8c6ae18d03a43eaa3e13;p=oweals%2Fgnunet.git diff --git a/src/transport/plugin_transport_http_server.c b/src/transport/plugin_transport_http_server.c index 58c8fd215..8ac301341 100644 --- a/src/transport/plugin_transport_http_server.c +++ b/src/transport/plugin_transport_http_server.c @@ -163,10 +163,8 @@ server_load_certificate (struct Plugin *plugin) GNUNET_free_non_null (plugin->cert); plugin->cert = NULL; -#if VERBOSE_SERVER GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate\n"); -#endif errno = 0; cert_creation = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, @@ -230,12 +228,17 @@ server_load_certificate (struct Plugin *plugin) * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait * until timeout */ - static void server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now) { if ((server == plugin->server_v4) && (plugin->server_v4 != NULL)) { + if (GNUNET_YES == plugin->server_v4_immediately) + return; /* No rescheduling, server will run asap */ + + if (GNUNET_YES == now) + plugin->server_v4_immediately = GNUNET_YES; + if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK) { GNUNET_SCHEDULER_cancel (plugin->server_v4_task); @@ -246,6 +249,12 @@ server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now) if ((server == plugin->server_v6) && (plugin->server_v6 != NULL)) { + if (GNUNET_YES == plugin->server_v6_immediately) + return; /* No rescheduling, server will run asap */ + + if (GNUNET_YES == now) + plugin->server_v6_immediately = GNUNET_YES; + if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK) { GNUNET_SCHEDULER_cancel (plugin->server_v6_task); @@ -290,6 +299,7 @@ server_receive_mst_cb (void *cls, void *client, return GNUNET_OK; } + /** * Callback called by MHD when it needs data to send * @param cls current session @@ -302,50 +312,54 @@ static ssize_t server_send_callback (void *cls, uint64_t pos, char *buf, size_t max) { struct Session *s = cls; + ssize_t bytes_read = 0; + struct HTTP_Message *msg; + GNUNET_assert (NULL != p); if (GNUNET_NO == exist_session(p, s)) return 0; - - struct HTTP_Message *msg; - int bytes_read = 0; - - //static int c = 0; msg = s->msg_head; - if (msg != NULL) + if (NULL != msg) { /* sending */ - if ((msg->size - msg->pos) <= max) - { - memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos)); - bytes_read = msg->size - msg->pos; - msg->pos += (msg->size - msg->pos); - } - else - { - memcpy (buf, &msg->buf[msg->pos], max); - msg->pos += max; - bytes_read = max; - } + bytes_read = GNUNET_MIN (msg->size - msg->pos, + max); + memcpy (buf, &msg->buf[msg->pos], bytes_read); + msg->pos += bytes_read; /* removing message */ if (msg->pos == msg->size) { + GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg); if (NULL != msg->transmit_cont) msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK); - GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg); GNUNET_free (msg); } } + GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name, + "Server: %p: sent %u bytes\n", s, bytes_read); + return bytes_read; +} - struct Plugin *plugin = s->plugin; - GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, - "Server: %X: sent %u bytes\n", s, bytes_read); - return bytes_read; +static struct Session * +server_lookup_session (struct Plugin *plugin, + struct ServerConnection * sc) +{ + struct Session *s; + + for (s = plugin->head; NULL != s; s = s->next) + if ((s->server_recv == sc) || (s->server_send == sc)) + return s; + for (s = plugin->server_semi_head; NULL != s; s = s->next) + if ((s->server_recv == sc) || (s->server_send == sc)) + return s; + return NULL; } + static struct ServerConnection * -server_lookup_session (struct Plugin *plugin, +server_lookup_serverconnection (struct Plugin *plugin, struct MHD_Connection *mhd_connection, const char *url, const char *method) { @@ -531,6 +545,9 @@ create: s->inbound = GNUNET_YES; s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS; s->tag = tag; + s->server_recv = NULL; + s->server_send = NULL; + GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s); goto found; @@ -554,7 +571,7 @@ found: int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000); GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, - "Server: Setting timeout for %X to %u sec.\n", sc, to); + "Server: Setting timeout for %p to %u sec.\n", sc, to); MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to); struct MHD_Daemon *d = NULL; @@ -589,10 +606,10 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection, int res = MHD_YES; GNUNET_assert (cls != NULL); - /* new connection */ if (sc == NULL) { - sc = server_lookup_session (plugin, mhd_connection, url, method); + /* new connection */ + sc = server_lookup_serverconnection (plugin, mhd_connection, url, method); if (sc != NULL) (*httpSessionCache) = sc; else @@ -605,6 +622,15 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection, return res; } } + else + { + /* 'old' connection */ + if (NULL == server_lookup_session (plugin, sc)) + { + /* Session was already disconnected */ + return MHD_NO; + } + } /* existing connection */ sc = (*httpSessionCache); @@ -635,8 +661,10 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection, if (sc->direction == _SEND) { response = - MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback, - s, NULL); + MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, + 32 * 1024, + &server_send_callback, s, + NULL); MHD_queue_response (mhd_connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return MHD_YES; @@ -667,7 +695,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection, if ((s->next_receive.abs_value <= now.abs_value)) { GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, - "Server: %X: PUT with %u bytes forwarded to MST\n", s, + "Server: %p: PUT with %u bytes forwarded to MST\n", s, *upload_data_size); if (s->msg_tk == NULL) { @@ -687,7 +715,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection, { t = s->server_recv; GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, - "Server: Setting timeout for %X to %u sec.\n", t, + "Server: Setting timeout for %p to %u sec.\n", t, to); MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, to); @@ -696,7 +724,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection, { t = s->server_send; GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, - "Server: Setting timeout for %X to %u sec.\n", t, + "Server: Setting timeout for %p to %u sec.\n", t, to); MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, to); @@ -714,7 +742,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection, else { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n", + "Server: %p no inbound bandwidth available! Next read was delayed by %llu ms\n", s, now.abs_value - s->next_receive.abs_value); } return MHD_YES; @@ -738,8 +766,9 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection, if (sc == NULL) return; - s = sc->session; - GNUNET_assert (NULL != s); + if (NULL == (s = server_lookup_session (p, sc))) + return; + GNUNET_assert (NULL != p); if (GNUNET_NO == exist_session(p, s)) return; @@ -749,17 +778,16 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection, { GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, - "Server: %X peer `%s' GET on address `%s' disconnected\n", + "Server: %p 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)); - GNUNET_free (s->server_send); s->server_send = NULL; - if (s->server_recv != NULL) + if (NULL != (tc = s->server_recv)) { - tc = s->server_recv; tc->disconnect = GNUNET_YES; + GNUNET_assert (NULL != tc->mhd_conn); #if MHD_VERSION >= 0x00090E00 - MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, + MHD_set_connection_option (tc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1); #endif } @@ -767,17 +795,16 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection, if (sc->direction == _RECEIVE) { GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, - "Server: %X peer `%s' PUT on address `%s' disconnected\n", + "Server: %p 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)); - GNUNET_free (s->server_recv); s->server_recv = NULL; - if (s->server_send != NULL) + if (NULL != (tc = s->server_send)) { - tc = s->server_send; tc->disconnect = GNUNET_YES; + GNUNET_assert (NULL != tc->mhd_conn); #if MHD_VERSION >= 0x00090E00 - MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, + MHD_set_connection_option (tc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1); #endif } @@ -787,6 +814,7 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection, s->msg_tk = NULL; } } + GNUNET_free (sc); t = plugin->server_semi_head; @@ -851,7 +879,7 @@ server_disconnect (struct Session *s) int server_send (struct Session *s, struct HTTP_Message *msg) { - GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg); + GNUNET_CONTAINER_DLL_insert_tail (s->msg_head, s->msg_tail, msg); if (s->addrlen == sizeof (struct IPv4HttpAddress)) { @@ -882,17 +910,15 @@ server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_assert (cls != NULL); plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK; - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) return; #if 0 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Running IPv4 server\n"); #endif + plugin->server_v4_immediately = GNUNET_NO; GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4)); - if (plugin->server_v4 != NULL) - plugin->server_v4_task = - server_schedule (plugin, plugin->server_v4, GNUNET_NO); + server_reschedule (plugin, plugin->server_v4, GNUNET_NO); } @@ -908,19 +934,16 @@ server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) struct Plugin *plugin = cls; GNUNET_assert (cls != NULL); - plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK; - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) return; #if 0 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Running IPv6 server\n"); #endif + plugin->server_v6_immediately = GNUNET_NO; GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6)); - if (plugin->server_v6 != NULL) - plugin->server_v6_task = - server_schedule (plugin, plugin->server_v6, GNUNET_NO); + server_reschedule (plugin, plugin->server_v6, GNUNET_NO); } /** @@ -1153,8 +1176,6 @@ server_stop (struct Plugin *plugin) struct Session *s = NULL; struct Session *t = NULL; - p = NULL; - struct MHD_Daemon *server_v4_tmp = plugin->server_v4; plugin->server_v4 = NULL; @@ -1212,6 +1233,8 @@ server_stop (struct Plugin *plugin) s = t; } + p = NULL; + #if BUILD_HTTPS GNUNET_free_non_null (plugin->crypto_init); GNUNET_free_non_null (plugin->cert);