Discard large metadata items first
[oweals/gnunet.git] / src / gns / gnunet-gns-proxy.c
index 44fef89c2863580369250432098722bae0193717..7b783787b7586967d616dace262be000140f9a13 100644 (file)
      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA 02111-1307, USA.
 */
-
+/**
+ * @author Martin Schanzenbach
+ * @file src/gns/gnunet-gns-proxy.c
+ * @brief HTTP(S) proxy that rewrites URIs and fakes certificats to make GADS work
+ *        with legacy browsers
+ */
 #include "platform.h"
-#include <gnunet_util_lib.h>
-#include <gnunet_gns_service.h>
 #include <microhttpd.h>
 #include <curl/curl.h>
-#include <regex.h>
-#include "gns_proxy_proto.h"
-#include "gns.h"
-
-/** SSL **/
 #include <gnutls/gnutls.h>
 #include <gnutls/x509.h>
 #include <gnutls/abstract.h>
 #include <gnutls/crypto.h>
-#include <time.h>
+#include <regex.h>
+#include "gnunet_util_lib.h"
+#include "gnunet_gns_service.h"
+#include "gns_proxy_proto.h"
+#include "gns.h"
 
-#define HAVE_MHD_NO_LISTEN_SOCKET MHD_VERSION >= 0x00091401
+#define HAVE_MHD_NO_LISTEN_SOCKET (MHD_VERSION >= 0x00091401)
 
 #define GNUNET_GNS_PROXY_PORT 7777
 #define MHD_MAX_CONNECTIONS 300
 #define POSTBUFFERSIZE 4096
 
 /* MHD/cURL defines */
-#define BUF_WAIT_FOR_CURL 0
-#define BUF_WAIT_FOR_MHD 1
-#define BUF_WAIT_FOR_PP 2
+enum BufferStatus
+  {
+    BUF_WAIT_FOR_CURL,
+    BUF_WAIT_FOR_MHD,
+    BUF_WAIT_FOR_PP 
+  };
+
 #define HTML_HDR_CONTENT "Content-Type: text/html"
 
 /* buffer padding for proper RE matching */
@@ -215,29 +221,29 @@ struct ProxyCurlTask
   /* Write pos in the buffer */
   char *buffer_write_ptr;
 
-  /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
-  int buf_status;
+  /* connection */
+  struct MHD_Connection *connection;
 
-  /* Number of bytes in buffer */
-  unsigned int bytes_in_buffer;
+  /*put*/
+  size_t put_read_offset;
+  size_t put_read_size;
 
-  /* Indicates wheather the download is in progress */
-  int download_in_progress;
+  /*post*/
+  struct MHD_PostProcessor *post_handler;
 
-  /* Indicates wheather the download was successful */
-  int download_is_finished;
+  /* post data */
+  struct ProxyUploadData *upload_data_head;
+  struct ProxyUploadData *upload_data_tail;
 
-  /* Indicates wheather the download failed */
-  int download_error;
+  /* the type of POST encoding */
+  char* post_type;
 
-  /* Indicates wheather we need to parse HTML */
-  int parse_content;
+  struct curl_httppost *httppost;
 
-  /* Indicates wheather we are postprocessing the HTML right now */
-  int is_postprocessing;
+  struct curl_httppost *httppost_last;
 
-  /* Indicates wheather postprocessing has finished */
-  int pp_finished;
+  /* Number of bytes in buffer */
+  unsigned int bytes_in_buffer;
 
   /* PP task */
   GNUNET_SCHEDULER_TaskIdentifier pp_task;
@@ -248,18 +254,6 @@ struct ProxyCurlTask
   /* PP match list */
   struct ProxyREMatch *pp_match_tail;
 
-  /* The authority of the corresponding host (site of origin) */
-  char authority[256];
-
-  /* The hostname (Host header field) */
-  char host[256];
-
-  /* The port */
-  uint16_t port;
-
-  /* The LEgacy HOstname (can be empty) */
-  char leho[256];
-
   /* The associated daemon list entry */
   struct MhdHttpList *mhd;
 
@@ -272,30 +266,54 @@ struct ProxyCurlTask
   /* Cookies to set */
   struct ProxySetCookieHeader *set_cookies_tail;
 
+  /* The authority of the corresponding host (site of origin) */
+  char authority[256];
+
+  /* The hostname (Host header field) */
+  char host[256];
+
+  /* The LEgacy HOstname (can be empty) */
+  char leho[256];
+
+  /* The port */
+  uint16_t port;
+
+  /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
+  enum BufferStatus buf_status;
+
   /* connection status */
   int ready_to_queue;
+
+  /* is curl running? */
+  int curl_running;
   
   /* are we done */
   int fin;
 
-  /* connection */
-  struct MHD_Connection *connection;
+  /* Already accepted */
+  int accepted;
 
-  /*put*/
-  size_t put_read_offset;
-  size_t put_read_size;
+  /* Indicates wheather the download is in progress */
+  int download_in_progress;
 
-  /*post*/
-  struct MHD_PostProcessor *post_handler;
+  /* Indicates wheather the download was successful */
+  int download_is_finished;
 
-  /* post data */
-  struct ProxyPostData *post_data_head;
-  struct ProxyPostData *post_data_tail;
+  /* Indicates wheather the download failed */
+  int download_error;
+
+  /* Indicates wheather we need to parse HTML */
+  int parse_content;
+
+  /* Indicates wheather we are postprocessing the HTML right now */
+  int is_postprocessing;
+
+  /* Indicates wheather postprocessing has finished */
+  int pp_finished;
 
   int post_done;
 
-  /* the type of POST encoding */
-  char* post_type;
+  int is_httppost;
   
 };
 
@@ -310,6 +328,15 @@ struct ProxyREMatch
   /* DLL */
   struct ProxyREMatch *prev;
 
+  /* start of match in buffer */
+  char* start;
+
+  /* end of match in buffer */
+  char* end;
+
+  /* associated proxycurltask */
+  struct ProxyCurlTask *ctask;
+
   /* hostname found */
   char hostname[255];
 
@@ -322,14 +349,9 @@ struct ProxyREMatch
   /* are we done */
   int done;
 
-  /* start of match in buffer */
-  char* start;
-
-  /* end of match in buffer */
-  char* end;
+  /* is SSL */
+  int is_ssl;
 
-  /* associated proxycurltask */
-  struct ProxyCurlTask *ctask;
 };
 
 /**
@@ -350,13 +372,21 @@ struct ProxySetCookieHeader
 /**
  * Post data structure
  */
-struct ProxyPostData
+struct ProxyUploadData
 {
   /* DLL */
-  struct ProxyPostData *next;
+  struct ProxyUploadData *next;
 
   /* DLL */
-  struct ProxyPostData *prev;
+  struct ProxyUploadData *prev;
+
+  char *key;
+
+  char *filename;
+
+  char *content_type;
+
+  size_t content_length;
   
   /* value */
   char *value;
@@ -379,10 +409,10 @@ static char* cafile_opt;
 static struct GNUNET_NETWORK_Handle *lsock;
 
 /* The listen task ID */
-GNUNET_SCHEDULER_TaskIdentifier ltask;
+static GNUNET_SCHEDULER_TaskIdentifier ltask;
 
 /* The cURL download task */
-GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
+static GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
 
 /* The non SSL httpd daemon handle */
 static struct MHD_Daemon *httpd;
@@ -424,82 +454,35 @@ static struct GNUNET_CRYPTO_ShortHashCode *local_shorten_zone;
 static struct ProxyCA proxy_ca;
 
 /* UNIX domain socket for mhd */
-struct GNUNET_NETWORK_Handle *mhd_unix_socket;
+#if !HAVE_MHD_NO_LISTEN_SOCKET
+static struct GNUNET_NETWORK_Handle *mhd_unix_socket;
+#endif
 
 /* Shorten zone private key */
-struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
+static struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
+
 
 /**
  * Checks if name is in tld
  *
- * @param name the name to check
- * @param tld the TLD to check for
+ * @param name the name to check 
+ * @param tld the TLD to check for (must NOT begin with ".")
  * @return GNUNET_YES or GNUNET_NO
  */
-int
-is_tld(const char* name, const char* tld)
-{
-  size_t offset;
-
-  if (strlen(name) <= strlen(tld))  
-    return GNUNET_NO;  
-
-  offset = strlen(name) - strlen(tld);
-  if (0 != strcmp (name+offset, tld))
-  {
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-               "%s is not in .%s TLD\n", name, tld);
-    return GNUNET_NO;
-  }
-
-  return GNUNET_YES;
-}
-
-/**
- * convert integer to string representation
- *
- * @param i integer
- * @return the character
- */
-char i_to_hexchar (char i)
+static int
+is_tld (const char* name, const char* tld)
 {
-  static char hexmap[] = "0123456789abcdef";
-  GNUNET_assert (sizeof (hexmap) > (i & 15));
-  return hexmap[i & 15];
+  size_t name_len = strlen (name);
+  size_t tld_len = strlen (tld);
+
+  GNUNET_break ('.' != tld[0]);
+  return ( (tld_len < name_len) &&
+          ( ('.' == name[name_len - tld_len - 1]) || (name_len == tld_len) ) &&
+          (0 == memcmp (tld,
+                        name + (name_len - tld_len),
+                        tld_len)) );
 }
 
-/**
- * Escape giben 0-terminated string
- *
- * @param to_esc string to escapse
- * @return allocated new escaped string (MUST free!)
- */
-static char*
-escape_to_urlenc (const char *to_esc)
-{
-  char *pos = (char*)to_esc;
-  char *res = GNUNET_malloc (strlen (to_esc) * 3 + 1);
-  char *rpos = res;
-
-  while ('\0' != *pos)
-  {
-    if (isalnum (*pos) ||
-        ('-' == *pos) || ('_' == *pos) ||
-        ('.' == *pos) || ('~' == *pos))
-        *rpos++ = *pos;
-    else if (' ' == *pos)
-      *rpos++ = '+';
-    else
-    {
-      *rpos++ = '%';
-      *rpos++ = i_to_hexchar (*pos >> 4);
-      *rpos++ = i_to_hexchar (*pos >> 15);
-    }
-    pos++;
-  }
-  *rpos = '\0';
-  return res;
-}
 
 static int
 con_post_data_iter (void *cls,
@@ -513,12 +496,55 @@ con_post_data_iter (void *cls,
                   size_t size)
 {
   struct ProxyCurlTask* ctask = cls;
-  struct ProxyPostData* pdata;
+  struct ProxyUploadData* pdata;
   char* enc;
+  char* new_value;
   
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Got POST data: '%s : %s' at offset %llu size %lld\n",
-              key, data, off, size);
+              "Got POST data (file: %s, content type: %s): '%s=%.*s' at offset %llu size %llu\n",
+             filename, content_type,
+              key, (int) size, data, 
+             (unsigned long long) off, 
+             (unsigned long long) size);
+  GNUNET_assert (NULL != ctask->post_type);
+
+  if (0 == strcasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
+                       ctask->post_type))
+  {
+    ctask->is_httppost = GNUNET_YES;
+    /* new part */
+    if (0 == off)
+    {
+      pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
+      pdata->key = GNUNET_strdup (key);
+
+      if (NULL != filename)
+        pdata->filename = GNUNET_strdup (filename);
+      if (NULL != content_type)
+        pdata->content_type = GNUNET_strdup (content_type);
+      pdata->value = GNUNET_malloc (size);
+      pdata->total_bytes = size;
+      memcpy (pdata->value, data, size);
+      GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
+                                        ctask->upload_data_tail,
+                                        pdata);
+
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Copied %llu bytes of POST Data\n", 
+                 (unsigned long long) size);
+      return MHD_YES;
+    }
+    
+    pdata = ctask->upload_data_tail;
+    new_value = GNUNET_malloc (size + pdata->total_bytes);
+    memcpy (new_value, pdata->value, pdata->total_bytes);
+    memcpy (new_value+off, data, size);
+    GNUNET_free (pdata->value);
+    pdata->value = new_value;
+    pdata->total_bytes += size;
+
+    return MHD_YES;
+  }
 
   if (0 != strcasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
                        ctask->post_type))
@@ -526,13 +552,23 @@ con_post_data_iter (void *cls,
     return MHD_NO;
   }
 
+  ctask->is_httppost = GNUNET_NO;
+  
+  if (NULL != ctask->curl)
+    curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
+
   if (0 == off)
   {
+    enc = curl_easy_escape (ctask->curl, key, 0);
+    if (NULL == enc)
+      {
+       GNUNET_break (0);
+       return MHD_NO;
+      }
     /* a key */
-    pdata = GNUNET_malloc (sizeof (struct ProxyPostData));
-    enc = escape_to_urlenc (key);
+    pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
     pdata->value = GNUNET_malloc (strlen (enc) + 3);
-    if (NULL != ctask->post_data_head)
+    if (NULL != ctask->upload_data_head)
     {
       pdata->value[0] = '&';
       memcpy (pdata->value+1, enc, strlen (enc));
@@ -542,52 +578,42 @@ con_post_data_iter (void *cls,
     pdata->value[strlen (pdata->value)] = '=';
     pdata->bytes_left = strlen (pdata->value);
     pdata->total_bytes = pdata->bytes_left;
-    GNUNET_free (enc);
+    curl_free (enc);
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Escaped POST key: '%s'\n",
                 pdata->value);
 
-    GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
-                                      ctask->post_data_tail,
+    GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
+                                      ctask->upload_data_tail,
                                       pdata);
   }
 
   /* a value */
-  pdata = GNUNET_malloc (sizeof (struct ProxyPostData));
-  enc = escape_to_urlenc (data);
+  enc = curl_easy_escape (ctask->curl, data, 0);
+  if (NULL == enc)
+    {
+      GNUNET_break (0);
+      return MHD_NO;
+    }
+  pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
   pdata->value = GNUNET_malloc (strlen (enc) + 1);
   memcpy (pdata->value, enc, strlen (enc));
   pdata->bytes_left = strlen (pdata->value);
   pdata->total_bytes = pdata->bytes_left;
-  GNUNET_free (enc);
+  curl_free (enc);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Escaped POST value: '%s'\n",
               pdata->value);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
-                                    ctask->post_data_tail,
+  GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
+                                    ctask->upload_data_tail,
                                     pdata);
   return MHD_YES;
 }
 
 
-static int
-get_uri_val_iter (void *cls,
-                  enum MHD_ValueKind kind,
-                  const char *key,
-                  const char *value)
-{
-  char* buf = cls;
-  
-  if (strlen (buf) + strlen (value) + 3 > MAX_HTTP_URI_LENGTH)
-    return MHD_NO;
-  sprintf (buf+strlen (buf), "?%s=%s", key, value);
-
-  return MHD_YES;
-}
-
 /**
  * Read HTTP request header field 'Host'
  *
@@ -612,7 +638,7 @@ con_val_iter (void *cls,
 
   if (0 == strcmp ("Host", key))
   {
-    port = strstr (value, ":");
+    port = strchr (value, ':');
     if (NULL != port)
     {
       strncpy (buf, value, port-value);
@@ -630,19 +656,21 @@ con_val_iter (void *cls,
     return MHD_YES;
   }
 
-  if (0 == strcmp ("Accept-Encoding", key))
+  if (0 == strcmp (MHD_HTTP_HEADER_ACCEPT_ENCODING, key))
     hdr_val = "";
   else
     hdr_val = value;
 
-  if (0 == strcmp (MHD_HTTP_HEADER_CONTENT_TYPE,
+  if (0 == strcasecmp (MHD_HTTP_HEADER_CONTENT_TYPE,
                    key))
   {
-    if (0 == strcmp (value,
-                     MHD_HTTP_POST_ENCODING_FORM_URLENCODED))
+    if (0 == strncasecmp (value,
+                     MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
+                     strlen (MHD_HTTP_POST_ENCODING_FORM_URLENCODED)))
       ctask->post_type = MHD_HTTP_POST_ENCODING_FORM_URLENCODED;
-    else if (0 == strcmp (value,
-                          MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))
+    else if (0 == strncasecmp (value,
+                          MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
+                          strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)))
       ctask->post_type = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
     else
       ctask->post_type = NULL;
@@ -678,6 +706,7 @@ mhd_content_cb (void *cls,
                 char* buf,
                 size_t max);
 
+
 /**
  * Check HTTP response header for mime
  *
@@ -707,6 +736,7 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
   char* hdr_val;
   int delta_cdomain;
   size_t offset = 0;
+  char cors_hdr[strlen (ctask->leho) + strlen ("https://")];
   
   if (NULL == ctask->response)
   {
@@ -718,8 +748,30 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
                                                         &mhd_content_cb,
                                                         ctask,
                                                         NULL);
+
+    /* if we have a leho add a CORS header */
+    if (0 != strcmp ("", ctask->leho))
+    {
+      /* We could also allow ssl and http here */
+      if (ctask->mhd->is_ssl)
+        sprintf (cors_hdr, "https://%s", ctask->leho);
+      else
+        sprintf (cors_hdr, "http://%s", ctask->leho);
+
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "MHD: Adding CORS header field %s\n",
+                  cors_hdr);
+
+      if (GNUNET_NO == MHD_add_response_header (ctask->response,
+                                              "Access-Control-Allow-Origin",
+                                              cors_hdr))
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "MHD: Error adding CORS header field %s\n",
+                  cors_hdr);
+      }
+    }
     ctask->ready_to_queue = GNUNET_YES;
-    
   }
   
   if (html_mime_len <= bytes)
@@ -751,14 +803,11 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
                    MHD_HTTP_HEADER_SET_COOKIE,
                    cookie_hdr_len))
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Looking for cookie in: `%s'\n", hdr_generic);    
     ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
     memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
-    tok = strtok (ndup, ";");
-
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                "Looking for cookie in : %s\n", hdr_generic);
-    
-    for (; tok != NULL; tok = strtok (NULL, ";"))
+    for (tok = strtok (ndup, ";"); tok != NULL; tok = strtok (NULL, ";"))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                   "Got Cookie token: %s\n", tok);
@@ -880,6 +929,7 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
   return bytes;
 }
 
+
 /**
  * schedule mhd
  *
@@ -896,6 +946,7 @@ run_httpd (struct MhdHttpList *hd);
 static void
 run_httpds (void);
 
+
 /**
  * Task run whenever HTTP server operations are pending.
  *
@@ -906,6 +957,7 @@ static void
 do_httpd (void *cls,
           const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+
 static void
 run_mhd_now (struct MhdHttpList *hd)
 {
@@ -920,11 +972,13 @@ run_mhd_now (struct MhdHttpList *hd)
   hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
 }
 
+
 /**
  * Ask cURL for the select sets and schedule download
  */
 static void
-curl_download_prepare ();
+curl_download_prepare (void);
+
 
 /**
  * Callback to free content
@@ -937,8 +991,9 @@ mhd_content_free (void *cls,
                   const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct ProxyCurlTask *ctask = cls;
-  GNUNET_assert (NULL == ctask->pp_match_head);
+  struct ProxyUploadData *pdata;
 
+  GNUNET_assert (NULL == ctask->pp_match_head);
   if (NULL != ctask->headers)
     curl_slist_free_all (ctask->headers);
 
@@ -951,7 +1006,20 @@ mhd_content_free (void *cls,
   if (NULL != ctask->post_handler)
     MHD_destroy_post_processor (ctask->post_handler);
 
+  if (GNUNET_SCHEDULER_NO_TASK != ctask->pp_task)
+    GNUNET_SCHEDULER_cancel (ctask->pp_task);
 
+  for (pdata = ctask->upload_data_head; NULL != pdata; pdata = ctask->upload_data_head)
+  {
+    GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
+                                 ctask->upload_data_tail,
+                                 pdata);
+    GNUNET_free_non_null (pdata->filename);
+    GNUNET_free_non_null (pdata->content_type);
+    GNUNET_free_non_null (pdata->key);
+    GNUNET_free_non_null (pdata->value);
+    GNUNET_free (pdata);
+  }
   GNUNET_free (ctask);
 }
 
@@ -972,19 +1040,16 @@ mhd_content_cb (void *cls,
                 size_t max)
 {
   struct ProxyCurlTask *ctask = cls;
-  struct ProxyREMatch *re_match = ctask->pp_match_head;
+  struct ProxyREMatch *re_match;
   ssize_t copied = 0;
-  long long int bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
+  size_t bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "MHD: content cb for %s. To copy: %lld\n",
-              ctask->url, bytes_to_copy);
-  GNUNET_assert (bytes_to_copy >= 0);
-
+              "MHD: content cb for %s. To copy: %u\n",
+              ctask->url, (unsigned int) bytes_to_copy);
   if ((GNUNET_YES == ctask->download_is_finished) &&
       (GNUNET_NO == ctask->download_error) &&
-      (0 == bytes_to_copy)) /* &&
-      (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
+      (0 == bytes_to_copy))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 "MHD: sending response for %s\n", ctask->url);
@@ -997,8 +1062,7 @@ mhd_content_cb (void *cls,
   
   if ((GNUNET_YES == ctask->download_error) &&
       (GNUNET_YES == ctask->download_is_finished) &&
-      (0 == bytes_to_copy)) /* &&
-      (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
+      (0 == bytes_to_copy))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 "MHD: sending error response\n");
@@ -1013,32 +1077,30 @@ mhd_content_cb (void *cls,
     return 0;
   
   copied = 0;
-  for (; NULL != re_match; re_match = ctask->pp_match_head)
+  for (re_match = ctask->pp_match_head; NULL != re_match; re_match = ctask->pp_match_head)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "MHD: Processing PP %s\n",
                 re_match->hostname);
     bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
-    GNUNET_assert (bytes_to_copy >= 0);
-
     if (bytes_to_copy+copied > max)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "MHD: buffer in response too small for %d. Using available space (%d). (%s)\n",
-             bytes_to_copy,
-             max,
-             ctask->url);
+                 "MHD: buffer in response too small for %u. Using available space (%d). (%s)\n",
+                 (unsigned int) bytes_to_copy,
+                 max,
+                 ctask->url);
       memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
       ctask->buffer_read_ptr += max-copied;
       copied = max;
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "MHD: copied %d bytes\n", copied);
+                 "MHD: copied %d bytes\n", (int) copied);
       return copied;
     }
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "MHD: copying %d bytes to mhd response at offset %d\n",
-                bytes_to_copy, ctask->buffer_read_ptr);
+                "MHD: copying %u bytes to mhd response at offset %d\n",
+                (unsigned int) bytes_to_copy, ctask->buffer_read_ptr);
     memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
     copied += bytes_to_copy;
 
@@ -1047,7 +1109,7 @@ mhd_content_cb (void *cls,
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "MHD: Waiting for PP of %s\n", re_match->hostname);
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "MHD: copied %d bytes\n", copied);
+                 "MHD: copied %d bytes\n", (int) copied);
       ctask->buffer_read_ptr += bytes_to_copy;
       return copied;
     }
@@ -1060,7 +1122,7 @@ mhd_content_cb (void *cls,
                   re_match->result,
                   ctask->url);
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "MHD: copied %d bytes\n", copied);
+                 "MHD: copied %d bytes\n", (int) copied);
       ctask->buffer_read_ptr += bytes_to_copy;
       return copied;
     }
@@ -1068,7 +1130,7 @@ mhd_content_cb (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "MHD: Adding PP result %s to buffer\n",
                 re_match->result);
-    memcpy (buf+copied, re_match->result, strlen (re_match->result));
+    memcpy (buf + copied, re_match->result, strlen (re_match->result));
     copied += strlen (re_match->result);
     ctask->buffer_read_ptr = re_match->end;
     GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
@@ -1080,12 +1142,10 @@ mhd_content_cb (void *cls,
   bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "MHD: copied: %d left: %d, space left in buf: %d\n",
+              "MHD: copied: %d left: %u, space left in buf: %d\n",
               copied,
-              bytes_to_copy, max-copied);
+              (unsigned int) bytes_to_copy, (int) (max - copied));
   
-  GNUNET_assert (0 <= bytes_to_copy);
-
   if (GNUNET_NO == ctask->download_is_finished)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1096,12 +1156,8 @@ mhd_content_cb (void *cls,
     ctask->buffer[bytes_to_copy] = '\0';
   }
   
-  if (bytes_to_copy+copied > max)
-    bytes_to_copy = max-copied;
-
-  if (0 > bytes_to_copy)
-    bytes_to_copy = 0;
-  
+  if (bytes_to_copy + copied > max)
+    bytes_to_copy = max - copied;
   memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
   ctask->buffer_read_ptr += bytes_to_copy;
   copied += bytes_to_copy;
@@ -1111,11 +1167,12 @@ mhd_content_cb (void *cls,
     curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "MHD: copied %d bytes\n", copied);
+              "MHD: copied %d bytes\n", (int) copied);
   run_mhd_now (ctask->mhd);
   return copied;
 }
 
+
 /**
  * Shorten result callback
  *
@@ -1150,7 +1207,7 @@ process_shorten (void* cls, const char* short_name)
               re_match->hostname,
               result);
   
-  if (re_match->ctask->mhd->is_ssl)
+  if (re_match->is_ssl)
     sprintf (re_match->result, "href=\"https://%s", result);
   else
     sprintf (re_match->result, "href=\"http://%s", result);
@@ -1199,12 +1256,19 @@ postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
     memset (re_hostname, 0, sizeof (re_hostname));
     memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
+    
 
     re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
     re_match->start = re_ptr + m[0].rm_so;
     re_match->end = re_ptr + m[3].rm_eo;
     re_match->done = GNUNET_NO;
     re_match->ctask = ctask;
+    
+    if ('s' == *(re_ptr+m[1].rm_so-strlen("://")-1)) //FIXME strcmp
+      re_match->is_ssl = GNUNET_YES;
+    else
+      re_match->is_ssl = GNUNET_NO;
+      
     strcpy (re_match->hostname, re_hostname);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "PP: Got hostname %s\n", re_hostname);
@@ -1236,6 +1300,7 @@ postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   run_mhd_now (ctask->mhd);
 }
 
+
 /**
  * Handle data from cURL
  *
@@ -1289,14 +1354,68 @@ curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
   return total;
 }
 
+
+/**
+ * cURL callback for put data
+ */
+static size_t
+put_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
+{
+  struct ProxyCurlTask *ctask = cls;
+  struct ProxyUploadData *pdata = ctask->upload_data_head;
+  size_t len = size * nmemb;
+  size_t to_copy;
+  char* pos;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "CURL: put read callback\n");
+
+  if (NULL == pdata)
+    return CURL_READFUNC_PAUSE;
+  
+  //fin
+  if (NULL == pdata->value)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "CURL: Terminating PUT\n");
+
+    GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
+                                 ctask->upload_data_tail,
+                                 pdata);
+    GNUNET_free (pdata);
+    return 0;
+  }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "CURL: read callback value %s\n", pdata->value); 
+  
+  to_copy = pdata->bytes_left;
+  if (to_copy > len)
+    to_copy = len;
+  
+  pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
+  memcpy (buf, pos, to_copy);
+  pdata->bytes_left -= to_copy;
+  if (pdata->bytes_left <= 0)
+  {
+    GNUNET_free (pdata->value);
+    GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
+                                 ctask->upload_data_tail,
+                                 pdata);
+    GNUNET_free (pdata);
+  }
+  return to_copy;
+}
+
+
 /**
  * cURL callback for post data
  */
 static size_t
-read_callback (void *buf, size_t size, size_t nmemb, void *cls)
+post_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
 {
   struct ProxyCurlTask *ctask = cls;
-  struct ProxyPostData *pdata = ctask->post_data_head;
+  struct ProxyUploadData *pdata = ctask->upload_data_head;
   size_t len = size * nmemb;
   size_t to_copy;
   char* pos;
@@ -1305,7 +1424,7 @@ read_callback (void *buf, size_t size, size_t nmemb, void *cls)
               "CURL: read callback\n");
 
   if (NULL == pdata)
-    return 0;
+    return CURL_READFUNC_PAUSE;
   
   //fin
   if (NULL == pdata->value)
@@ -1313,8 +1432,8 @@ read_callback (void *buf, size_t size, size_t nmemb, void *cls)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "CURL: Terminating POST data\n");
 
-    GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
-                                 ctask->post_data_tail,
+    GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
+                                 ctask->upload_data_tail,
                                  pdata);
     GNUNET_free (pdata);
     return 0;
@@ -1333,14 +1452,15 @@ read_callback (void *buf, size_t size, size_t nmemb, void *cls)
   if (pdata->bytes_left <= 0)
   {
     GNUNET_free (pdata->value);
-    GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
-                                 ctask->post_data_tail,
+    GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
+                                 ctask->upload_data_tail,
                                  pdata);
     GNUNET_free (pdata);
   }
   return to_copy;
 }
 
+
 /**
  * Task that is run when we are ready to receive more data
  * from curl
@@ -1351,6 +1471,7 @@ read_callback (void *buf, size_t size, size_t nmemb, void *cls)
 static void
 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+
 /**
  * Ask cURL for the select sets and schedule download
  */
@@ -1371,9 +1492,7 @@ curl_download_prepare ()
   FD_ZERO (&rs);
   FD_ZERO (&ws);
   FD_ZERO (&es);
-  mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
-
-  if (mret != CURLM_OK)
+  if (CURLM_OK != (mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "%s failed at %s:%d: `%s'\n",
@@ -1382,23 +1501,20 @@ curl_download_prepare ()
     //TODO cleanup here?
     return;
   }
-
-  mret = curl_multi_timeout (curl_multi, &to);
-  rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
-
+  to = -1;
+  GNUNET_break (CURLM_OK == curl_multi_timeout (curl_multi, &to));
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "cURL multi fds: max=%d timeout=%llu\n", max, to);
-
+              "cURL multi fds: max=%d timeout=%lld\n", max, (long long) to);
+  if (-1 == to)
+    rtime = GNUNET_TIME_UNIT_FOREVER_REL;
+  else
+    rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
   grs = GNUNET_NETWORK_fdset_create ();
   gws = GNUNET_NETWORK_fdset_create ();
   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Scheduling task cURL\n");
-
   if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
-    GNUNET_SCHEDULER_cancel (curl_download_task);
-  
+    GNUNET_SCHEDULER_cancel (curl_download_task);  
   if (-1 != max)
   {
     curl_download_task =
@@ -1436,7 +1552,6 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct ProxyCurlTask *ctask;
   int num_ctasks;
   long resp_code;
-
   struct ProxyCurlTask *clean_head = NULL;
   struct ProxyCurlTask *clean_tail = NULL;
 
@@ -1446,8 +1561,8 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Shutdown requested while trying to download\n");
-  //TODO cleanup
-  return;
+    //TODO cleanup
+    return;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Ready to dl\n");
@@ -1462,8 +1577,7 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Running curl tasks: %d\n", running);
 
-    ctask = ctasks_head;
-    for (; ctask != NULL; ctask = ctask->next)
+    for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "CTask: %s\n", ctask->url);
@@ -1478,7 +1592,7 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     
     do
     {
-      ctask = ctasks_head;
+      
       msg = curl_multi_info_read (curl_multi, &msgnum);
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Messages left: %d\n", msgnum);
@@ -1494,7 +1608,7 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
            GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                        "Download curl failed");
             
-           for (; ctask != NULL; ctask = ctask->next)
+           for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
            {
              if (NULL == ctask->curl)
                continue;
@@ -1528,7 +1642,7 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                        "CURL: download completed.\n");
 
-           for (; ctask != NULL; ctask = ctask->next)
+           for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
            {
              if (NULL == ctask->curl)
                continue;
@@ -1574,7 +1688,7 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       }
     } while (msgnum > 0);
 
-    for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
+    for (ctask=clean_head; NULL != ctask; ctask = ctask->next)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "CURL: Removing task %s.\n", ctask->url);
@@ -1584,10 +1698,8 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     }
     
     num_ctasks=0;
-    for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
-    {
-      num_ctasks++;
-    }
+    for (ctask=ctasks_head; NULL != ctask; ctask = ctask->next)    
+      num_ctasks++; 
     
     if (num_ctasks != running)
     {
@@ -1608,6 +1720,7 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   curl_download_prepare();
 }
 
+
 /**
  * Process LEHO lookup
  *
@@ -1706,6 +1819,7 @@ process_leho_lookup (void *cls,
 
 }
 
+
 /**
  * Initialize download and trigger curl
  *
@@ -1743,6 +1857,17 @@ process_get_authority (void *cls,
 }
 
 
+static void*
+mhd_log_callback (void* cls, const char* url)
+{
+  struct ProxyCurlTask *ctask;
+
+  ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
+  strcpy (ctask->url, url);
+  return ctask;
+}
+
+
 /**
  * Main MHD callback for handling requests.
  *
@@ -1777,14 +1902,16 @@ create_response (void *cls,
                  void **con_cls)
 {
   struct MhdHttpList* hd = cls;
-  const char* page = "<html><head><title>gnoxy</title>"\
+  const char* page = "<html><head><title>gnunet-gns-proxy</title>"
                       "</head><body>cURL fail</body></html>";
   
   char curlurl[MAX_HTTP_URI_LENGTH]; // buffer overflow!
   int ret = MHD_YES;
-
-  struct ProxyCurlTask *ctask;
-  struct ProxyPostData *fin_post;
+  int i;
+  struct ProxyCurlTask *ctask = *con_cls;
+  struct ProxyUploadData *fin_post;
+  struct curl_forms forms[5];
+  struct ProxyUploadData *upload_data_iter;
   
   //FIXME handle
   if ((0 != strcasecmp (meth, MHD_HTTP_METHOD_GET)) &&
@@ -1798,16 +1925,14 @@ create_response (void *cls,
   }
 
 
-  if (NULL == *con_cls)
+  if (GNUNET_NO == ctask->accepted)
   {
 
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 "Got %s request for %s\n", meth, url);
-    ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
     ctask->mhd = hd;
-    *con_cls = ctask;
-    
     ctask->curl = curl_easy_init();
+    ctask->curl_running = GNUNET_NO;
     if (NULL == ctask->curl)
     {
       ctask->response = MHD_create_response_from_buffer (strlen (page),
@@ -1820,14 +1945,7 @@ create_response (void *cls,
       GNUNET_free (ctask);
       return ret;
     }
-  
-    ctask->download_in_progress = GNUNET_YES;
-    ctask->buf_status = BUF_WAIT_FOR_CURL;
-    ctask->connection = con;
-    ctask->curl_response_code = MHD_HTTP_OK;
-    ctask->buffer_read_ptr = ctask->buffer;
-    ctask->buffer_write_ptr = ctask->buffer;
-    ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
+    
     if (ctask->mhd->is_ssl)
       ctask->port = HTTPS_PORT;
     else
@@ -1836,44 +1954,62 @@ create_response (void *cls,
     MHD_get_connection_values (con,
                                MHD_HEADER_KIND,
                                &con_val_iter, ctask);
+    
+    curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
+    curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
+    curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
+    curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
+    curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
+    curl_easy_setopt (ctask->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
+
+    if (GNUNET_NO == ctask->mhd->is_ssl)
+    {
+      sprintf (curlurl, "http://%s:%d%s", ctask->host, ctask->port, ctask->url);
+      curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
+    }
+    
+
+    curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
+    curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
+    curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
+    
+    /* Add GNS header */
+    ctask->headers = curl_slist_append (ctask->headers,
+                                          "GNS: YES");
+    ctask->accepted = GNUNET_YES;
+    ctask->download_in_progress = GNUNET_YES;
+    ctask->buf_status = BUF_WAIT_FOR_CURL;
+    ctask->connection = con;
+    ctask->curl_response_code = MHD_HTTP_OK;
+    ctask->buffer_read_ptr = ctask->buffer;
+    ctask->buffer_write_ptr = ctask->buffer;
+    ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
+    
 
     if (0 == strcasecmp (meth, MHD_HTTP_METHOD_PUT))
     {
-      //FIXME: this prolly works like POST?
-      if (0 == *upload_data_size)
-      {
-        curl_easy_cleanup (ctask->curl);
-        GNUNET_free (ctask);
-        return MHD_NO;
-      }
-      ctask->put_read_offset = 0;
-      ctask->put_read_size = *upload_data_size;
-      curl_easy_setopt (ctask->curl, CURLOPT_UPLOAD, 1);
-      curl_easy_setopt (ctask->curl, CURLOPT_READDATA, upload_data);
-      //curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION, &curl_read_cb);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Setting up PUT\n");
       
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                  "Got PUT data: %s\n", upload_data);
-      curl_easy_cleanup (ctask->curl);
-      GNUNET_free (ctask);
-      return MHD_NO;
+      curl_easy_setopt (ctask->curl, CURLOPT_UPLOAD, 1);
+      curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
+      curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION, &put_read_callback);
+      ctask->headers = curl_slist_append (ctask->headers,
+                                          "Transfer-Encoding: chunked");
     }
 
     if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
     {
       //FIXME handle multipart
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Setting up POST processor\n");
       ctask->post_handler = MHD_create_post_processor (con,
                                  POSTBUFFERSIZE,
                                  &con_post_data_iter,
                                  ctask);
-      curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
-      curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION,
-                        &read_callback);
-      curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
       ctask->headers = curl_slist_append (ctask->headers,
-                                          "Transfer-Encoding: chunked");
+                                         "Transfer-Encoding: chunked");
+      return MHD_YES;
     }
 
     if (0 == strcasecmp (meth, MHD_HTTP_METHOD_HEAD))
@@ -1883,31 +2019,9 @@ create_response (void *cls,
       curl_easy_setopt (ctask->curl, CURLOPT_NOBODY, 1);
     }
 
-    curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
-    curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
-    curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
-    curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
-    curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
-    curl_easy_setopt (ctask->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
     
-    if (GNUNET_NO == ctask->mhd->is_ssl)
-    {
-      sprintf (curlurl, "http://%s:%d%s", ctask->host, ctask->port, url);
-      MHD_get_connection_values (con,
-                                 MHD_GET_ARGUMENT_KIND,
-                                 &get_uri_val_iter, curlurl);
-      curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
-    }
-    strcpy (ctask->url, url);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "MHD: Adding new curl task for %s%s\n", ctask->host, url);
-    MHD_get_connection_values (con,
-                               MHD_GET_ARGUMENT_KIND,
-                               &get_uri_val_iter, ctask->url);
-  
-    curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
-    curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
-    curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
+                  "MHD: Adding new curl task for %s\n", ctask->host);
 
     GNUNET_GNS_get_authority (gns_handle,
                               ctask->host,
@@ -1915,6 +2029,7 @@ create_response (void *cls,
                               ctask);
     ctask->ready_to_queue = GNUNET_NO;
     ctask->fin = GNUNET_NO;
+    ctask->curl_running = GNUNET_YES;
     return MHD_YES;
   }
 
@@ -1923,18 +2038,95 @@ create_response (void *cls,
   {
     if (0 != *upload_data_size)
     {
+      
       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                   "Invoking POST processor\n");
       MHD_post_process (ctask->post_handler,
                         upload_data, *upload_data_size);
       *upload_data_size = 0;
+      if ((GNUNET_NO == ctask->is_httppost) &&
+          (GNUNET_NO == ctask->curl_running))
+      {
+        curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
+        curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION,
+                          &post_read_callback);
+        curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
+        
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "MHD: Adding new curl task for %s\n", ctask->host);
+
+        GNUNET_GNS_get_authority (gns_handle,
+                                  ctask->host,
+                                  &process_get_authority,
+                                  ctask);
+        ctask->ready_to_queue = GNUNET_NO;
+        ctask->fin = GNUNET_NO;
+        ctask->curl_running = GNUNET_YES;
+      }
       return MHD_YES;
     }
     else if (GNUNET_NO == ctask->post_done)
     {
-      fin_post = GNUNET_malloc (sizeof (struct ProxyPostData));
-      GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
-                                        ctask->post_data_tail,
+      if (GNUNET_YES == ctask->is_httppost)
+      {
+        for (upload_data_iter = ctask->upload_data_head;
+             NULL != upload_data_iter;
+             upload_data_iter = upload_data_iter->next)
+        {
+          i = 0;
+          if (NULL != upload_data_iter->filename)
+          {
+            forms[i].option = CURLFORM_FILENAME;
+            forms[i].value = upload_data_iter->filename;
+            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                        "Adding filename %s\n",
+                        forms[i].value);
+            i++;
+          }
+          if (NULL != upload_data_iter->content_type)
+          {
+            forms[i].option = CURLFORM_CONTENTTYPE;
+            forms[i].value = upload_data_iter->content_type;
+            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                        "Adding content type %s\n",
+                        forms[i].value);
+            i++;
+          }
+          forms[i].option = CURLFORM_PTRCONTENTS;
+          forms[i].value = upload_data_iter->value;
+          forms[i+1].option = CURLFORM_END;
+
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "Adding formdata for %s (len=%lld)\n",
+                      upload_data_iter->key,
+                      upload_data_iter->total_bytes);
+
+          curl_formadd(&ctask->httppost, &ctask->httppost_last,
+                       CURLFORM_COPYNAME, upload_data_iter->key,
+                       CURLFORM_CONTENTSLENGTH, upload_data_iter->total_bytes,
+                       CURLFORM_ARRAY, forms,
+                       CURLFORM_END);
+        }
+        curl_easy_setopt (ctask->curl, CURLOPT_HTTPPOST,
+                          ctask->httppost);
+
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "MHD: Adding new curl task for %s\n", ctask->host);
+
+        GNUNET_GNS_get_authority (gns_handle,
+                                  ctask->host,
+                                  &process_get_authority,
+                                  ctask);
+        ctask->ready_to_queue = GNUNET_YES;
+        ctask->fin = GNUNET_NO;
+        ctask->curl_running = GNUNET_YES;
+        ctask->post_done = GNUNET_YES;
+        return MHD_YES;
+      }
+
+      fin_post = GNUNET_malloc (sizeof (struct ProxyUploadData));
+      GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
+                                        ctask->upload_data_tail,
                                         fin_post);
       ctask->post_done = GNUNET_YES;
       return MHD_YES;
@@ -1969,6 +2161,10 @@ run_httpds ()
 
 }
 
+
+#define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
+
+
 /**
  * schedule mhd
  *
@@ -1985,7 +2181,7 @@ run_httpd (struct MhdHttpList *hd)
   struct GNUNET_NETWORK_FDSet *wes;
   int max;
   int haveto;
-  unsigned MHD_LONG_LONG timeout;
+  UNSIGNED_MHD_LONG_LONG timeout;
   struct GNUNET_TIME_Relative tv;
 
   FD_ZERO (&rs);
@@ -2043,7 +2239,6 @@ do_httpd (void *cls,
 }
 
 
-
 /**
  * Read data from socket
  *
@@ -2053,6 +2248,7 @@ do_httpd (void *cls,
 static void
 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+
 /**
  * Read from remote end
  *
@@ -2062,6 +2258,7 @@ do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 static void
 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+
 /**
  * Write data to remote socket
  *
@@ -2120,8 +2317,7 @@ cleanup_s5r (struct Socks5Request *s5r)
   if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdwtask)
     GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
   if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
-    GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
-  
+    GNUNET_SCHEDULER_cancel (s5r->fwdrtask);  
   if (NULL != s5r->remote_sock)
     GNUNET_NETWORK_socket_close (s5r->remote_sock);
   if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
@@ -2130,6 +2326,7 @@ cleanup_s5r (struct Socks5Request *s5r)
   GNUNET_free(s5r);
 }
 
+
 /**
  * Write data to socket
  *
@@ -2154,13 +2351,11 @@ do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                 len);
   }
   else
-  {
-    
+  {    
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
     s5r->cleanup = GNUNET_YES;
     s5r->cleanup_sock = GNUNET_YES;
-    cleanup_s5r (s5r);
-    
+    cleanup_s5r (s5r); 
     return;
   }
 
@@ -2178,6 +2373,7 @@ do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                      &do_read_remote, s5r);
 }
 
+
 /**
  * Read from remote end
  *
@@ -2190,8 +2386,6 @@ do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct Socks5Request *s5r = cls;
   
   s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
-
-
   if ((NULL != tc->write_ready) &&
       (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
       (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
@@ -2203,7 +2397,7 @@ do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   else
   {
-    if (s5r->wbuf_len == 0)
+    if (0 == s5r->wbuf_len)
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "0 bytes received from remote... graceful shutdown!\n");
     if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
@@ -2221,8 +2415,7 @@ do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   
   s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                                s5r->sock,
-                                               &do_write, s5r);
-  
+                                               &do_write, s5r);  
 }
 
 
@@ -2247,18 +2440,19 @@ add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
   return MHD_add_connection (daemon, fd, addr, len);
 }
 
+
 /**
  * Read file in filename
  *
  * @param filename file to read
  * @param size pointer where filesize is stored
- * @return data
+ * @return NULL on error
  */
-static char*
+static void*
 load_file (const char* filename, 
           unsigned int* size)
 {
-  char *buffer;
+  void *buffer;
   uint64_t fsize;
 
   if (GNUNET_OK !=
@@ -2291,7 +2485,7 @@ load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
   gnutls_datum_t key_data;
   int ret;
 
-  key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
+  key_data.data = load_file (keyfile, &key_data.size);
   ret = gnutls_x509_privkey_import (key, &key_data,
                                     GNUTLS_X509_FMT_PEM);
   if (GNUTLS_E_SUCCESS != ret)
@@ -2317,10 +2511,9 @@ static int
 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
 {
   gnutls_datum_t cert_data;
-  cert_data.data = NULL;
   int ret;
 
-  cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
+  cert_data.data = load_file (certfile, &cert_data.size);
   ret = gnutls_x509_crt_import (crt, &cert_data,
                                 GNUTLS_X509_FMT_PEM);
   if (GNUTLS_E_SUCCESS != ret)
@@ -2343,7 +2536,6 @@ load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
 static struct ProxyGNSCertificate *
 generate_gns_certificate (const char *name)
 {
-
   int ret;
   unsigned int serial;
   size_t key_buf_size;
@@ -2359,12 +2551,7 @@ generate_gns_certificate (const char *name)
     GNUNET_break (0);
   }
 
-  ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
-
-  if (GNUTLS_E_SUCCESS != ret)
-  {
-    GNUNET_break (0);
-  }
+  GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_key (request, proxy_ca.key));
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
 
@@ -2375,14 +2562,11 @@ generate_gns_certificate (const char *name)
   
   gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
                                  0, "DE", 2);
-
   gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
-                                 0, "GNUnet", 6);
-
+                                 0, "GADS", 4);
   gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
                                  0, name, strlen (name));
-
-  ret = gnutls_x509_crt_set_version (request, 3);
+  GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_version (request, 3));
 
   ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
 
@@ -2430,13 +2614,13 @@ generate_gns_certificate (const char *name)
 }
 
 
-/*
+/**
  * Accept policy for mhdaemons
  *
  * @param cls NULL
  * @param addr the sockaddr
  * @param addrlen the sockaddr length
- * @return MHD_NO if sockaddr is wrong or #conns too high
+ * @return MHD_NO if sockaddr is wrong or number of connections is too high
  */
 static int
 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
@@ -2469,7 +2653,7 @@ accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
 static int
 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
 {
-  struct MhdHttpList *hd = NULL;
+  struct MhdHttpList *hd;
   struct ProxyGNSCertificate *pgc;
   struct NetworkHandleList *nh;
 
@@ -2490,27 +2674,36 @@ add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "No previous SSL instance found... starting new one for %s\n",
                 domain);
-    hd->daemon = MHD_start_daemon (MHD_USE_DEBUG
-                                   | MHD_USE_SSL
 #if HAVE_MHD_NO_LISTEN_SOCKET
-                                   | MHD_USE_NO_LISTEN_SOCKET,
+    hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL | MHD_USE_NO_LISTEN_SOCKET,
                                    0,
+                                   &accept_cb, NULL,
+                                   &create_response, hd,
+                                   MHD_OPTION_CONNECTION_LIMIT,
+                                   MHD_MAX_CONNECTIONS,
+                                  MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+                                  MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
+                                  MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
+                                  MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
+                                  MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
+                                  NULL,
+                                  MHD_OPTION_END);
 #else
-                                   , 4444, //Dummy
-#endif
+    hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL,
+                                   4444 /* dummy port */,
                                    &accept_cb, NULL,
                                    &create_response, hd,
-#if !HAVE_MHD_NO_LISTEN_SOCKET
-            MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
-#endif
+                                  MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
                                    MHD_OPTION_CONNECTION_LIMIT,
                                    MHD_MAX_CONNECTIONS,
-                               MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
-                               MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
-                               MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
-                               MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
-                               MHD_OPTION_END);
-
+                                  MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+                                  MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
+                                  MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
+                                  MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
+                                  MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
+                                  NULL,
+                                  MHD_OPTION_END);
+#endif
     GNUNET_assert (hd->daemon != NULL);
     hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
     
@@ -2528,7 +2721,6 @@ add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
 }
 
 
-
 /**
  * Read data from incoming connection
  *
@@ -2543,7 +2735,6 @@ do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct socks5_server_hello *s_hello;
   struct socks5_client_request *c_req;
   struct socks5_server_response *s_resp;
-
   int ret;
   char domain[256];
   uint8_t dom_len;
@@ -2552,7 +2743,6 @@ do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   uint32_t remote_ip;
   struct sockaddr_in remote_addr;
   struct in_addr *r_sin_addr;
-
   struct NetworkHandleList *nh;
 
   s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
@@ -2805,10 +2995,7 @@ do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                        s5r->remote_sock,
                                        &do_read_remote, s5r);
     }
-
-
   }
-
 }
 
 
@@ -2865,19 +3052,16 @@ static void
 do_shutdown (void *cls,
              const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-
   struct MhdHttpList *hd;
   struct MhdHttpList *tmp_hd;
   struct NetworkHandleList *nh;
   struct NetworkHandleList *tmp_nh;
   struct ProxyCurlTask *ctask;
   struct ProxyCurlTask *ctask_tmp;
+  struct ProxyUploadData *pdata;
   
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Shutting down...\n");
-
-  gnutls_global_deinit ();
   if (NULL != local_gns_zone)
     GNUNET_free (local_gns_zone); 
   if (NULL != local_private_zone)
@@ -2906,13 +3090,11 @@ do_shutdown (void *cls,
       GNUNET_SCHEDULER_cancel (hd->httpd_task);
       hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
     }
-
     if (NULL != hd->daemon)
     {
       MHD_stop_daemon (hd->daemon);
       hd->daemon = NULL;
     }
-
     for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
     {
       tmp_nh = nh->next;
@@ -2950,12 +3132,25 @@ do_shutdown (void *cls,
     if (NULL != ctask->response)
       MHD_destroy_response (ctask->response);
 
+    pdata = ctask->upload_data_head;
 
+    //FIXME free pdata here
+    for (; pdata != NULL; pdata = ctask->upload_data_head)
+    {
+      GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
+                                   ctask->upload_data_tail,
+                                   pdata);
+      GNUNET_free_non_null (pdata->filename);
+      GNUNET_free_non_null (pdata->content_type);
+      GNUNET_free_non_null (pdata->key);
+      GNUNET_free_non_null (pdata->value);
+      GNUNET_free (pdata);
+    }
     GNUNET_free (ctask);
   }
   curl_multi_cleanup (curl_multi);
-
   GNUNET_GNS_disconnect (gns_handle);
+  gnutls_global_deinit ();
 }
 
 
@@ -2993,9 +3188,9 @@ static int
 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   char *keyfile;
-  struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
+  struct GNUNET_CRYPTO_RsaPrivateKey *key;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
-  struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
+  struct GNUNET_CRYPTO_ShortHashCode *zone;
   struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
 
   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
@@ -3088,6 +3283,7 @@ load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
   return GNUNET_YES;
 }
 
+
 /**
  * Main function that will be run
  *
@@ -3110,9 +3306,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   struct sockaddr_un mhd_unix_sock_addr;
 #endif
 
-  curl_multi = curl_multi_init ();
-
-  if (NULL == curl_multi)
+  if (NULL == (curl_multi = curl_multi_init ()))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Failed to create cURL multo handle!\n");
@@ -3125,8 +3319,8 @@ run (void *cls, char *const *args, const char *cfgfile,
   if (NULL == cafile)
   {
     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
-                                                          "PROXY_CACERT",
-                                                          &cafile_cfg))
+                                                             "PROXY_CACERT",
+                                                             &cafile_cfg))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                   "Unable to load proxy CA config value!\n");
@@ -3157,19 +3351,16 @@ run (void *cls, char *const *args, const char *cfgfile,
   
   compile_regex (&re_dotplus, (char*) RE_A_HREF);
 
-  gns_handle = GNUNET_GNS_connect (cfg);
-
-  if (GNUNET_NO == load_local_zone_key (cfg))
+  if (NULL == (gns_handle = GNUNET_GNS_connect (cfg)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Unable to load zone!\n");
+                "Unable to connect to GNS!\n");
     return;
   }
-
-  if (NULL == gns_handle)
+  if (GNUNET_NO == load_local_zone_key (cfg))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Unable to connect to GNS!\n");
+                "Unable to load zone!\n");
     return;
   }
 
@@ -3204,13 +3395,9 @@ run (void *cls, char *const *args, const char *cfgfile,
                 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
     return;
   }
-
   ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                          lsock, &do_accept, NULL);
 
-  ctasks_head = NULL;
-  ctasks_tail = NULL;
-
   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -3221,11 +3408,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Proxy listens on port %u\n",
               port);
-
-  mhd_httpd_head = NULL;
-  mhd_httpd_tail = NULL;
-  total_mhd_connections = 0;
-#ifndef HAVE_MHD_NO_LISTEN_SOCKET
+#if ! HAVE_MHD_NO_LISTEN_SOCKET
   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
                                                             "PROXY_UNIXPATH",
                                                             &proxy_sockfile))
@@ -3234,12 +3417,9 @@ run (void *cls, char *const *args, const char *cfgfile,
                 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
     return;
   }
-
-  mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
-                                                SOCK_STREAM,
-                                                0);
-
-  if (NULL == mhd_unix_socket)
+  if (NULL == (mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
+                                                              SOCK_STREAM,
+                                                              0)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Unable to create unix domain socket!\n");
@@ -3257,7 +3437,6 @@ run (void *cls, char *const *args, const char *cfgfile,
 #endif
 
   len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
-
   GNUNET_free (proxy_sockfile);
 
   if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
@@ -3281,35 +3460,38 @@ run (void *cls, char *const *args, const char *cfgfile,
   hd = GNUNET_malloc (sizeof (struct MhdHttpList));
   hd->is_ssl = GNUNET_NO;
   strcpy (hd->domain, "");
-  httpd = MHD_start_daemon (MHD_USE_DEBUG
+
 #if HAVE_MHD_NO_LISTEN_SOCKET
-                            | MHD_USE_NO_LISTEN_SOCKET,
+  httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
                             0,
+                           &accept_cb, NULL,
+                           &create_response, hd,
+                           MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
+                           MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+                           MHD_OPTION_NOTIFY_COMPLETED,
+                           NULL, NULL,
+                           MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
+                           MHD_OPTION_END);
 #else
-                            , 4444, //Dummy port
+  httpd = MHD_start_daemon (MHD_USE_DEBUG,
+                           4444 /* Dummy port */,
+                           &accept_cb, NULL,
+                           &create_response, hd,
+                           MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
+                           MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
+                           MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+                           MHD_OPTION_NOTIFY_COMPLETED,
+                           NULL, NULL,
+                           MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
+                           MHD_OPTION_END);
 #endif
-            &accept_cb, NULL,
-            &create_response, hd,
-#if !HAVE_MHD_NO_LISTEN_SOCKET
-            MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
-#endif
-            MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
-            MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
-            MHD_OPTION_NOTIFY_COMPLETED,
-            NULL, NULL,
-            MHD_OPTION_END);
-
-  GNUNET_assert (httpd != NULL);
+  GNUNET_break (httpd != NULL);
   hd->daemon = httpd;
   hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
-
   GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
-
   run_httpds ();
-
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                 &do_shutdown, NULL);
-
 }
 
 
@@ -3325,20 +3507,17 @@ main (int argc, char *const *argv)
 {
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'p', "port", NULL,
-     gettext_noop ("listen on specified port"), 1,
-     &GNUNET_GETOPT_set_string, &port},
+     gettext_noop ("listen on specified port (default: 7777)"), 1,
+     &GNUNET_GETOPT_set_ulong, &port},
     {'a', "authority", NULL,
       gettext_noop ("pem file to use as CA"), 1,
       &GNUNET_GETOPT_set_string, &cafile_opt},
     GNUNET_GETOPT_OPTION_END
   };
-
   int ret;
 
   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
     return 2;
-
-
   GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
   ret =
       (GNUNET_OK ==
@@ -3346,11 +3525,8 @@ main (int argc, char *const *argv)
                            _("GNUnet GNS proxy"),
                            options,
                            &run, NULL)) ? 0 : 1;
-  GNUNET_free_non_null ((char*)argv);
-
+  GNUNET_free_non_null ((char *) argv);
   return ret;
 }
 
-
-
-
+/* end of gnunet-gns-proxy.c */