(no commit message)
[oweals/gnunet.git] / src / transport / plugin_transport_http.c
index 7e11cbae10c4ffddfcce0fd0720803fb50b70a5e..7984529343dd4381b8a54a5b311ed3c0b18eb5f6 100644 (file)
@@ -38,6 +38,8 @@
 #include "microhttpd.h"
 #include <curl/curl.h>
 
+
+#define DEBUG_CURL GNUNET_CURL
 #define DEBUG_HTTP GNUNET_NO
 
 /**
  */
 #define HTTP_CONNECT_TIMEOUT 30
 
+/**
+ * Timeout for a http connect
+ */
+#define HTTP_MESSAGE_INITIAL_BUFFERSIZE GNUNET_SERVER_MAX_MESSAGE_SIZE
+
 
 /**
  * Encapsulation of all of the state of the plugin.
@@ -131,6 +138,20 @@ struct HTTP_Message
    * amount of data to sent
    */
   size_t len;
+
+  char * dest_url;
+
+  /**
+   * Continuation function to call once the transmission buffer
+   * has again space available.  NULL if there is no
+   * continuation to call.
+   */
+  GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
+
+  /**
+   * Closure for transmit_cont.
+   */
+  void *transmit_cont_cls;
 };
 
 
@@ -150,18 +171,6 @@ struct Session
    */
   struct Plugin *plugin;
 
-  /**
-   * Continuation function to call once the transmission buffer
-   * has again space available.  NULL if there is no
-   * continuation to call.
-   */
-  GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
-
-  /**
-   * Closure for transmit_cont.
-   */
-  void *transmit_cont_cls;
-
   /**
    * To whom are we talking to (set to our identity
    * if we are still waiting for the welcome message)
@@ -181,7 +190,12 @@ struct Session
   /**
    * Sender's ip address to distinguish between incoming connections
    */
-  struct sockaddr_in * addr;
+  struct sockaddr_in * addr_inbound;
+
+  /**
+   * Sender's ip address recieved by transport
+   */
+  struct sockaddr_in * addr_outbound;
 
   /**
    * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
@@ -215,6 +229,11 @@ struct Session
    */
   unsigned int is_put_in_progress;
 
+  /**
+   * Is there a HTTP/PUT in progress?
+   */
+  unsigned int is_bad_request;
+
   /**
    * Encoded hash
    */
@@ -222,6 +241,8 @@ struct Session
 
   struct HTTP_Message * pending_outbound_msg;;
 
+  struct HTTP_Message * pending_inbound_msg;
+
   CURL *curl_handle;
 };
 
@@ -350,37 +371,58 @@ static struct Session * find_session_by_curlhandle( CURL* handle )
 /**
  * Create a new session
  *
- * @param address address the peer is using
+ * @param address address the peer is using inbound
+ * @param address address the peer is using outbound
  * @param peer identity
  * @return created session object
  */
-static struct Session * create_session (struct sockaddr_in *address, const struct GNUNET_PeerIdentity *peer)
+static struct Session * create_session (struct sockaddr_in *addr_in, struct sockaddr_in *addr_out, const struct GNUNET_PeerIdentity *peer)
 {
   struct sockaddr_in  *addrin;
   struct sockaddr_in6 *addrin6;
-
   struct Session * ses = GNUNET_malloc ( sizeof( struct Session) );
-  ses->addr = GNUNET_malloc ( sizeof (struct sockaddr_in) );
+
+  ses->addr_outbound = GNUNET_malloc ( sizeof (struct sockaddr_in) );
+
 
   ses->next = NULL;
   ses->plugin = plugin;
-
-  memcpy(ses->addr, address, sizeof (struct sockaddr_in));
-  if ( AF_INET == address->sin_family)
+  if (NULL != addr_in)
   {
-    ses->ip = GNUNET_malloc (INET_ADDRSTRLEN);
-    addrin = address;
-    inet_ntop(addrin->sin_family,&(addrin->sin_addr),ses->ip,INET_ADDRSTRLEN);
+    ses->addr_inbound  = GNUNET_malloc ( sizeof (struct sockaddr_in) );
+    memcpy(ses->addr_inbound, addr_in, sizeof (struct sockaddr_in));
+    if ( AF_INET == addr_in->sin_family)
+    {
+      ses->ip = GNUNET_malloc (INET_ADDRSTRLEN);
+      addrin = addr_in;
+      inet_ntop(addrin->sin_family,&(addrin->sin_addr),ses->ip,INET_ADDRSTRLEN);
+    }
+    if ( AF_INET6 == addr_in->sin_family)
+    {
+      ses->ip = GNUNET_malloc (INET6_ADDRSTRLEN);
+      addrin6 = (struct sockaddr_in6 *) addr_in;
+      inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr) ,ses->ip,INET6_ADDRSTRLEN);
+    }
   }
-  if ( AF_INET6 == address->sin_family)
+  else
+    ses->addr_inbound = NULL;
+
+  if (NULL != addr_out)
   {
-    ses->ip = GNUNET_malloc (INET6_ADDRSTRLEN);
-    addrin6 = (struct sockaddr_in6 *) address;
-    inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr) ,ses->ip,INET6_ADDRSTRLEN);
+    ses->addr_outbound  = GNUNET_malloc ( sizeof (struct sockaddr_in) );
+    memcpy(ses->addr_outbound, addr_out, sizeof (struct sockaddr_in));
   }
+  else
+    ses->addr_outbound = NULL;
+
   memcpy(&ses->sender, peer, sizeof (struct GNUNET_PeerIdentity));
   GNUNET_CRYPTO_hash_to_enc(&ses->sender.hashPubKey,&(ses->hash));
   ses->is_active = GNUNET_NO;
+  ses->pending_inbound_msg = GNUNET_malloc( sizeof (struct HTTP_Message));
+  ses->pending_inbound_msg->buf = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE);
+  ses->pending_inbound_msg->len = GNUNET_SERVER_MAX_MESSAGE_SIZE;
+  ses->pending_inbound_msg->pos = 0;
+
 
   return ses;
 }
@@ -415,6 +457,8 @@ acceptPolicyCallback (void *cls,
 }
 
 
+int serror;
+
 /**
  * Process GET or PUT request received via MHD.  For
  * GET, queue response that will send back our pending
@@ -440,12 +484,12 @@ accessHandlerCallback (void *cls,
   char * address = NULL;
   struct GNUNET_PeerIdentity pi_in;
   int res = GNUNET_NO;
-  size_t bytes_recv;
   struct GNUNET_MessageHeader *gn_msg;
   int send_error_to_client;
 
   gn_msg = NULL;
   send_error_to_client = GNUNET_NO;
+
   if ( NULL == *httpSessionCache)
   {
     /* check url for peer identity */
@@ -504,7 +548,7 @@ accessHandlerCallback (void *cls,
     if (cs == NULL )
     {
       /* create new session object */
-      cs = create_session(conn_info->client_addr, &pi_in);
+      cs = create_session(conn_info->client_addr, NULL, &pi_in);
 
       /* Insert session into linked list */
       if ( plugin->sessions == NULL)
@@ -529,7 +573,7 @@ accessHandlerCallback (void *cls,
     {
       *httpSessionCache = cs;
     }
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`[%s]:%u')\n",method, GNUNET_i2s(&cs->sender),cs->ip,cs->addr->sin_port);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`[%s]:%u')\n",method, GNUNET_i2s(&cs->sender),cs->ip,cs->addr_inbound->sin_port);
   }
   else
   {
@@ -541,63 +585,94 @@ accessHandlerCallback (void *cls,
     /* New  */
     if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_NO))
     {
+      if (cs->pending_inbound_msg->pos !=0 )
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                    _("Incoming message from peer `%s', while existing message with %u bytes was not forwarded to transport'\n"),
+                    GNUNET_i2s(&cs->sender), cs->pending_inbound_msg->pos);
+        cs->pending_inbound_msg->pos = 0;
+      }
       /* not yet ready */
       cs->is_put_in_progress = GNUNET_YES;
+      cs->is_bad_request = GNUNET_NO;
       cs->is_active = GNUNET_YES;
       return MHD_YES;
     }
-    if ( *upload_data_size > 0 )
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"PUT URL: `%s'\n",url);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"PUT Request: %lu bytes: `%s' \n", (*upload_data_size), upload_data);
-      /* No data left */
-      bytes_recv = *upload_data_size ;
-      *upload_data_size = 0;
 
-      /* checking size */
-      if (bytes_recv < sizeof (struct GNUNET_MessageHeader))
+    if ((*upload_data_size > 0) && (cs->is_bad_request != GNUNET_YES))
+    {
+      if ((*upload_data_size + cs->pending_inbound_msg->pos < cs->pending_inbound_msg->len) && (*upload_data_size + cs->pending_inbound_msg->pos <= GNUNET_SERVER_MAX_MESSAGE_SIZE))
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Message too small, is %u bytes, has to be at least %u '\n",bytes_recv, sizeof(struct GNUNET_MessageHeader));
-        send_error_to_client = GNUNET_YES;
+        /* copy uploaded data to buffer */
+        memcpy(&cs->pending_inbound_msg->buf[cs->pending_inbound_msg->pos],upload_data,*upload_data_size);
+        cs->pending_inbound_msg->pos += *upload_data_size;
+        *upload_data_size = 0;
+        return MHD_YES;
       }
-
-      if ( bytes_recv > GNUNET_SERVER_MAX_MESSAGE_SIZE)
+      else
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Message too big, is %u bytes, maximum %u '\n",bytes_recv, GNUNET_SERVER_MAX_MESSAGE_SIZE);
-        send_error_to_client = GNUNET_YES;
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"%u bytes not added to message of %u bytes, message to big\n",*upload_data_size, cs->pending_inbound_msg->pos);
+        cs->is_bad_request = GNUNET_YES;
+        /* (*upload_data_size) bytes not processed */
+        return MHD_YES;
       }
+    }
 
-      struct GNUNET_MessageHeader * gn_msg = GNUNET_malloc (bytes_recv);
-      memcpy (gn_msg,upload_data,bytes_recv);
+    if ((cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_YES))
+    {
+      *upload_data_size = 0;
+      response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
+      res = MHD_queue_response (session, MHD_HTTP_REQUEST_ENTITY_TOO_LARGE, response);
+      if (res == MHD_YES)
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 413 ENTITY TOO LARGE as PUT Response\n");
+        cs->is_bad_request = GNUNET_NO;
+        cs->is_put_in_progress =GNUNET_NO;
+      }
+      MHD_destroy_response (response);
+      return MHD_YES;
+    }
 
-      if ( ntohs(gn_msg->size) != bytes_recv )
+    if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_NO))
+    {
+      send_error_to_client = GNUNET_YES;
+      struct GNUNET_MessageHeader * gn_msg = NULL;
+      /*check message and forward here */
+      /* checking size */
+      if (cs->pending_inbound_msg->pos >= sizeof (struct GNUNET_MessageHeader))
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Message has incorrect size, is %u bytes vs %u recieved'\n",ntohs(gn_msg->size) , bytes_recv);
-        send_error_to_client = GNUNET_YES;
+        gn_msg = GNUNET_malloc (cs->pending_inbound_msg->pos);
+        memcpy (gn_msg,cs->pending_inbound_msg->buf,cs->pending_inbound_msg->pos);
+
+        if ((ntohs(gn_msg->size) == cs->pending_inbound_msg->pos))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved GNUnet message type %u size %u and payload %u \n",ntohs (gn_msg->type), ntohs (gn_msg->size), ntohs (gn_msg->size)-sizeof(struct GNUNET_MessageHeader));
+          /* forwarding message to transport */
+          plugin->env->receive(plugin->env, &(cs->sender), gn_msg, 1, cs , cs->ip, strlen(cs->ip) );
+          send_error_to_client = GNUNET_NO;
+        }
       }
 
-      if ( GNUNET_YES == send_error_to_client)
+      if (send_error_to_client == GNUNET_NO)
       {
+        response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
+        res = MHD_queue_response (session, MHD_HTTP_OK, response);
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
+        MHD_destroy_response (response);
+      }
+      else
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved malformed message with %u bytes\n", cs->pending_inbound_msg->pos);
         response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
         res = MHD_queue_response (session, MHD_HTTP_BAD_REQUEST, response);
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 400 BAD REQUEST as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
         MHD_destroy_response (response);
-        GNUNET_free (gn_msg);
-        return MHD_NO;
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 400 BAD REQUEST as PUT Response\n");
       }
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved GNUnet message type %u size %u and payload %u \n",ntohs (gn_msg->type), ntohs (gn_msg->size), ntohs (gn_msg->size)-sizeof(struct GNUNET_MessageHeader));
 
-      /* forwarding message to transport */
-      plugin->env->receive(plugin->env, &(cs->sender), gn_msg, 1, cs , cs->ip, strlen(cs->ip) );
-      return MHD_YES;
-    }
-    if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_YES))
-    {
+      GNUNET_free_non_null (gn_msg);
       cs->is_put_in_progress = GNUNET_NO;
-      response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
-      res = MHD_queue_response (session, MHD_HTTP_OK, response);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
-      MHD_destroy_response (response);
+      cs->is_bad_request = GNUNET_NO;
+      cs->pending_inbound_msg->pos = 0;
       return res;
     }
   }
@@ -722,6 +797,7 @@ static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
   {
     ses->pending_outbound_msg = cur->next;
     GNUNET_free (cur->buf);
+    GNUNET_free (cur->dest_url);
     GNUNET_free (cur);
     cur = NULL;
     return GNUNET_OK;
@@ -737,12 +813,51 @@ static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
 
   cur->next = cur->next->next;
   GNUNET_free (cur->next->buf);
+  GNUNET_free (cur->next->dest_url);
   GNUNET_free (cur->next);
   cur->next = NULL;
   return GNUNET_OK;
 }
 
 
+static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
+{
+  char * tmp;
+  unsigned int len = size * nmemb;
+
+  tmp = GNUNET_malloc (  len+1 );
+  memcpy(tmp,ptr,len);
+  if (tmp[len-2] == 13)
+    tmp[len-2]= '\0';
+#if DEBUG_CURL
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
+#endif
+  /*
+  if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
+  {
+    res->http_result_code=100;
+  }
+  if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
+  {
+    res->http_result_code=200;
+  }
+  if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
+  {
+    res->http_result_code=400;
+  }
+  if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
+  {
+    res->http_result_code=404;
+  }
+  if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
+  {
+    res->http_result_code=413;
+  }
+   */
+  GNUNET_free (tmp);
+  return size * nmemb;
+}
+
 /**
  * Callback method used with libcurl
  * Method is called when libcurl needs to read data during sending
@@ -806,23 +921,29 @@ static size_t send_prepare(struct Session* session );
  * @param ses session to send data to
  * @return bytes sent to peer
  */
-static ssize_t send_select_init (struct Session* ses  )
+static ssize_t send_select_init (struct Session* ses )
 {
-  char * url;
   int bytes_sent = 0;
   CURLMcode mret;
   struct HTTP_Message * msg;
 
-  /* FIFO selection, send oldest msg first, perhaps priority here?  */
+  if ( NULL == ses->curl_handle)
+    ses->curl_handle = curl_easy_init();
+  if( NULL == ses->curl_handle)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
+    return -1;
+  }
   msg = ses->pending_outbound_msg;
 
-  url = GNUNET_malloc( 7 + strlen(ses->ip) + 7 + strlen ((char *) &(ses->hash)) + 1);
-  /* FIXME: use correct port number */
-  GNUNET_asprintf(&url,"http://%s:%u/%s",ses->ip,12389, (char *) &(ses->hash));
 
-  /* curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L); */
-  curl_easy_setopt(ses->curl_handle, CURLOPT_URL, url);
+
+#if DEBUG_CURL
+  curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L);
+#endif
+  curl_easy_setopt(ses->curl_handle, CURLOPT_URL, msg->dest_url);
   curl_easy_setopt(ses->curl_handle, CURLOPT_PUT, 1L);
+  curl_easy_setopt(ses->curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
   curl_easy_setopt(ses->curl_handle, CURLOPT_READFUNCTION, send_read_callback);
   curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
   curl_easy_setopt(ses->curl_handle, CURLOPT_WRITEFUNCTION, send_write_callback);
@@ -841,7 +962,6 @@ static ssize_t send_select_init (struct Session* ses  )
     return -1;
   }
   bytes_sent = send_prepare (ses );
-  GNUNET_free ( url );
   return bytes_sent;
 }
 
@@ -871,6 +991,7 @@ static void send_execute (void *cls,
               if (msg == NULL)
                 break;
               /* get session for affected curl handle */
+              GNUNET_assert ( msg->easy_handle != NULL );
               cs = find_session_by_curlhandle (msg->easy_handle);
               GNUNET_assert ( cs != NULL );
               switch (msg->msg)
@@ -889,11 +1010,11 @@ static void send_execute (void *cls,
                                __LINE__,
                                curl_easy_strerror (msg->data.result));
                     /* sending msg failed*/
-                    if ( NULL != cs->transmit_cont)
-                      cs->transmit_cont (NULL,&cs->sender,GNUNET_SYSERR);
+                    if ( NULL != cs->pending_outbound_msg->transmit_cont)
+                      cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_SYSERR);
                     }
                   else
-                    {
+                  {
                     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                                 "Send to %s completed.\n", cs->ip);
                     if (GNUNET_OK != remove_http_message(cs, cs->pending_outbound_msg))
@@ -902,14 +1023,17 @@ static void send_execute (void *cls,
                     curl_easy_cleanup(cs->curl_handle);
                     cs->curl_handle=NULL;
 
+                    /* Calling transmit continuation  */
+                    if ( NULL != cs->pending_outbound_msg->transmit_cont)
+                      cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_OK);
+
+
                     /* send pending messages */
                     if (cs->pending_outbound_msg != NULL)
+                    {
                       send_select_init (cs);
-
-                    /* Calling transmit continuation  */
-                    if ( NULL != cs->transmit_cont)
-                      cs->transmit_cont (NULL,&cs->sender,GNUNET_OK);
                     }
+                  }
                   return;
                 default:
                   break;
@@ -1022,14 +1146,13 @@ http_plugin_send (void *cls,
                       GNUNET_TRANSPORT_TransmitContinuation cont,
                       void *cont_cls)
 {
+  char * address;
   struct Session* ses;
   struct Session* ses_temp;
   struct HTTP_Message * msg;
   struct HTTP_Message * tmp;
   int bytes_sent = 0;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport told plugin to send to peer `%s'\n",GNUNET_i2s(target));
-
   /* find session for peer */
   ses = find_session_by_pi (target);
   if (NULL != ses )
@@ -1039,9 +1162,8 @@ http_plugin_send (void *cls,
     /* create new session object */
 
     /*FIXME: what is const void * really? Assuming struct sockaddr_in * ! */
-    ses = create_session((struct sockaddr_in *) addr, target);
+    ses = create_session(NULL, (struct sockaddr_in *) addr, target);
     ses->is_active = GNUNET_YES;
-    ses->transmit_cont = cont;
 
     /* Insert session into linked list */
     if ( plugin->sessions == NULL)
@@ -1062,15 +1184,33 @@ http_plugin_send (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", GNUNET_i2s(target), plugin->session_count);
   }
 
-  ses->curl_handle = curl_easy_init();
-  if( NULL == ses->curl_handle)
+  GNUNET_assert (addr!=NULL);
+  unsigned int port;
+
+  /* setting url to send to */
+  if (force_address == GNUNET_YES)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
-    return -1;
+    if (addrlen == (sizeof (struct IPv4HttpAddress)))
+    {
+      address = GNUNET_malloc(INET_ADDRSTRLEN + 14 + strlen ((const char *) (&ses->hash)));
+      inet_ntop(AF_INET,&((struct IPv4HttpAddress *) addr)->ipv4_addr,address,INET_ADDRSTRLEN);
+      port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
+      GNUNET_asprintf(&address,"http://%s:%u/%s",address,port, (char *) (&ses->hash));
+    }
+    else if (addrlen == (sizeof (struct IPv6HttpAddress)))
+    {
+      address = GNUNET_malloc(INET6_ADDRSTRLEN + 14 + strlen ((const char *) (&ses->hash)));
+      inet_ntop(AF_INET6, &((struct IPv6HttpAddress *) addr)->ipv6_addr,address,INET6_ADDRSTRLEN);
+      port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
+      GNUNET_asprintf(&address,"http://%s:%u/%s",address,port,(char *) (&ses->hash));
+    }
+    else
+      {
+        GNUNET_break (0);
+        return -1;
+    }
   }
 
-  if ( NULL != cont)
-    ses->transmit_cont = cont;
   timeout = to;
   /* setting up message */
   msg = GNUNET_malloc (sizeof (struct HTTP_Message));
@@ -1078,10 +1218,12 @@ http_plugin_send (void *cls,
   msg->len = msgbuf_size;
   msg->pos = 0;
   msg->buf = GNUNET_malloc (msgbuf_size);
+  msg->dest_url = address;
+  msg->transmit_cont = cont;
+  msg->transmit_cont_cls = cont_cls;
   memcpy (msg->buf,msgbuf, msgbuf_size);
 
   /* insert created message in list of pending messages */
-
   if (ses->pending_outbound_msg == NULL)
   {
     ses->pending_outbound_msg = msg;
@@ -1092,11 +1234,16 @@ http_plugin_send (void *cls,
     tmp = tmp->next;
   }
   if ( tmp != msg)
+  {
     tmp->next = msg;
+  }
 
-  bytes_sent = send_select_init (ses);
-  return bytes_sent;
-
+  if (msg == ses->pending_outbound_msg)
+  {
+    bytes_sent = send_select_init (ses);
+    return bytes_sent;
+  }
+  return msgbuf_size;
 }
 
 
@@ -1201,6 +1348,7 @@ http_plugin_address_suggested (void *cls,
 {
   struct IPv4HttpAddress *v4;
   struct IPv6HttpAddress *v6;
+  unsigned int port;
 
   if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
       (addrlen != sizeof (struct IPv6HttpAddress)))
@@ -1212,8 +1360,8 @@ http_plugin_address_suggested (void *cls,
     {
       v4 = (struct IPv4HttpAddress *) addr;
 
-      v4->u_port = ntohs (v4->u_port);
-      if (v4->u_port != plugin->port_inbound)
+      port = ntohs (v4->u_port);
+      if (port != plugin->port_inbound)
       {
         GNUNET_break_op (0);
         return GNUNET_SYSERR;
@@ -1227,8 +1375,8 @@ http_plugin_address_suggested (void *cls,
           GNUNET_break_op (0);
           return GNUNET_SYSERR;
         }
-      v6->u6_port = ntohs (v6->u6_port);
-      if (v6->u6_port != plugin->port_inbound)
+      port = ntohs (v6->u6_port);
+      if (port != plugin->port_inbound)
       {
         GNUNET_break_op (0);
         return GNUNET_SYSERR;
@@ -1426,9 +1574,11 @@ libgnunet_plugin_transport_http_done (void *cls)
          GNUNET_free (cur);
          cur = tmp;
       }
-
+      GNUNET_free (cs->pending_inbound_msg->buf);
+      GNUNET_free (cs->pending_inbound_msg);
       GNUNET_free (cs->ip);
-      GNUNET_free (cs->addr);
+      GNUNET_free (cs->addr_inbound);
+      GNUNET_free_non_null (cs->addr_outbound);
       GNUNET_free (cs);
       plugin->session_count--;
       cs = cs_next;