gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [gnunet] branch master updated (4444fb802 -> ba33155b4)


From: gnunet
Subject: [GNUnet-SVN] [gnunet] branch master updated (4444fb802 -> ba33155b4)
Date: Fri, 03 May 2019 16:19:37 +0200

This is an automated email from the git hooks/post-receive script.

grothoff pushed a change to branch master
in repository gnunet.

    from 4444fb802 add test for json_mhd.c logic
     new 782565417 support compressed JSON uploads
     new ba33155b4 reindentation

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/core/Makefile.am                   |   2 +-
 src/core/gnunet-service-core_typemap.c |  49 +++---
 src/curl/curl.c                        | 267 +++++++++++++--------------------
 src/json/.gitignore                    |   1 +
 src/json/Makefile.am                   |   5 +-
 src/json/json_mhd.c                    | 122 +++++++++++++++
 src/json/test_json_mhd.c               |  47 +++++-
 7 files changed, 301 insertions(+), 192 deletions(-)

diff --git a/src/core/Makefile.am b/src/core/Makefile.am
index 2723151c5..70ec91fbc 100644
--- a/src/core/Makefile.am
+++ b/src/core/Makefile.am
@@ -58,7 +58,7 @@ gnunet_core_LDADD = \
 
 if HAVE_TESTING
   TESTING_TESTS = \
-    test_core_api_send_to_self 
+    test_core_api_send_to_self
 endif
 
 check_PROGRAMS = \
diff --git a/src/core/gnunet-service-core_typemap.c 
b/src/core/gnunet-service-core_typemap.c
index b73267544..9dd3c71c8 100644
--- a/src/core/gnunet-service-core_typemap.c
+++ b/src/core/gnunet-service-core_typemap.c
@@ -11,7 +11,7 @@
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.
-    
+
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
@@ -80,12 +80,9 @@ rehash_typemap ()
  * @param hc where to store the hash code
  */
 void
-GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm,
-                  struct GNUNET_HashCode *hc)
+GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm, struct GNUNET_HashCode *hc)
 {
-  GNUNET_CRYPTO_hash (tm,
-                      sizeof (struct GSC_TypeMap),
-                      hc);
+  GNUNET_CRYPTO_hash (tm, sizeof (struct GSC_TypeMap), hc);
 }
 
 
@@ -100,12 +97,12 @@ GSC_TYPEMAP_check_hash (const struct GNUNET_HashCode *hc)
 {
   if (GNUNET_NO == hash_current)
   {
-    GSC_TYPEMAP_hash (&my_type_map,
-                      &my_tm_hash);
+    GSC_TYPEMAP_hash (&my_type_map, &my_tm_hash);
     hash_current = GNUNET_YES;
   }
   return (0 == memcmp (hc, &my_tm_hash, sizeof (struct GNUNET_HashCode)))
-    ? GNUNET_YES : GNUNET_NO;
+           ? GNUNET_YES
+           : GNUNET_NO;
 }
 
 
@@ -130,9 +127,12 @@ GSC_TYPEMAP_compute_type_map_message ()
 #endif
   hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
   tmp = (char *) &hdr[1];
-  if ((Z_OK !=
-       compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map,
-                  sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
+  if ((Z_OK != compress2 ((Bytef *) tmp,
+                          &dlen,
+                          (const Bytef *) &my_type_map,
+                          sizeof (my_type_map),
+                          9)) ||
+      (dlen >= sizeof (my_type_map)))
   {
     /* compression failed, use uncompressed map */
     dlen = sizeof (my_type_map);
@@ -166,8 +166,10 @@ GSC_TYPEMAP_get_from_message (const struct 
GNUNET_MessageHeader *msg)
   switch (ntohs (msg->type))
   {
   case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
-    GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# type maps received"),
-                              1, GNUNET_NO);
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop ("# type maps received"),
+                              1,
+                              GNUNET_NO);
     if (size != sizeof (struct GSC_TypeMap))
     {
       GNUNET_break_op (0);
@@ -183,9 +185,11 @@ GSC_TYPEMAP_get_from_message (const struct 
GNUNET_MessageHeader *msg)
                               GNUNET_NO);
     ret = GNUNET_new (struct GSC_TypeMap);
     dlen = sizeof (struct GSC_TypeMap);
-    if ((Z_OK !=
-         uncompress ((Bytef *) ret, &dlen, (const Bytef *) &msg[1],
-                     (uLong) size)) || (dlen != sizeof (struct GSC_TypeMap)))
+    if ((Z_OK != uncompress ((Bytef *) ret,
+                             &dlen,
+                             (const Bytef *) &msg[1],
+                             (uLong) size)) ||
+        (dlen != sizeof (struct GSC_TypeMap)))
     {
       GNUNET_break_op (0);
       GNUNET_free (ret);
@@ -224,8 +228,7 @@ broadcast_my_type_map ()
  * @param tlen number of entries in @a types
  */
 void
-GSC_TYPEMAP_add (const uint16_t *types,
-                 unsigned int tlen)
+GSC_TYPEMAP_add (const uint16_t *types, unsigned int tlen)
 {
   unsigned int i;
   int changed;
@@ -241,8 +244,7 @@ GSC_TYPEMAP_add (const uint16_t *types,
   }
   if (GNUNET_YES == changed)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Typemap changed, broadcasting!\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Typemap changed, broadcasting!\n");
     rehash_typemap ();
     broadcast_my_type_map ();
   }
@@ -256,8 +258,7 @@ GSC_TYPEMAP_add (const uint16_t *types,
  * @param tlen length of the @a types array
  */
 void
-GSC_TYPEMAP_remove (const uint16_t *types,
-                    unsigned int tlen)
+GSC_TYPEMAP_remove (const uint16_t *types, unsigned int tlen)
 {
   int changed;
 
@@ -295,7 +296,7 @@ GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap,
   if (NULL == tmap)
     return GNUNET_NO;
   if (0 == tcnt)
-    return GNUNET_YES;          /* matches all */
+    return GNUNET_YES; /* matches all */
   for (unsigned int i = 0; i < tcnt; i++)
     if (0 != (tmap->bits[types[i] / 32] & (1 << (types[i] % 32))))
       return GNUNET_YES;
diff --git a/src/curl/curl.c b/src/curl/curl.c
index 1c352f195..e13c1478f 100644
--- a/src/curl/curl.c
+++ b/src/curl/curl.c
@@ -39,18 +39,24 @@
  * @param function which function failed to run
  * @param code what was the curl error code
  */
-#define CURL_STRERROR(type, function, code)      \
- GNUNET_log (type,                               \
-             "Curl function `%s' has failed at `%s:%d' with error: %s\n", \
-             function, __FILE__, __LINE__, curl_easy_strerror (code));
+#define CURL_STRERROR(type, function, code)                                \
+  GNUNET_log (type,                                                        \
+              "Curl function `%s' has failed at `%s:%d' with error: %s\n", \
+              function,                                                    \
+              __FILE__,                                                    \
+              __LINE__,                                                    \
+              curl_easy_strerror (code));
 
 /**
  * Print JSON parsing related error information
  */
-#define JSON_WARN(error)                                                \
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,                              \
-                "JSON parsing failed at %s:%u: %s (%s)\n",              \
-                __FILE__, __LINE__, error.text, error.source)
+#define JSON_WARN(error)                                 \
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,                 \
+              "JSON parsing failed at %s:%u: %s (%s)\n", \
+              __FILE__,                                  \
+              __LINE__,                                  \
+              error.text,                                \
+              error.source)
 
 
 /**
@@ -99,7 +105,6 @@ struct GNUNET_CURL_Job
    * Buffer for response received from CURL.
    */
   struct GNUNET_CURL_DownloadBuffer db;
-
 };
 
 
@@ -156,8 +161,7 @@ struct GNUNET_CURL_Context
  * @return library context
  */
 struct GNUNET_CURL_Context *
-GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
-                  void *cb_cls)
+GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb, void *cb_cls)
 {
   struct GNUNET_CURL_Context *ctx;
   CURLM *multi;
@@ -165,8 +169,7 @@ GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
 
   if (curl_fail)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Curl was not initialised properly\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Curl was not initialised 
properly\n");
     return NULL;
   }
   if (NULL == (multi = curl_multi_init ()))
@@ -186,9 +189,9 @@ GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
   ctx->cb_cls = cb_cls;
   ctx->multi = multi;
   ctx->share = share;
-  GNUNET_assert (NULL != (ctx->json_header =
-                          curl_slist_append (NULL,
-                                             "Content-Type: 
application/json")));
+  GNUNET_assert (
+    NULL != (ctx->json_header =
+               curl_slist_append (NULL, "Content-Type: application/json")));
   return ctx;
 }
 
@@ -207,10 +210,7 @@ GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
  * @return number of bytes processed from @a bufptr
  */
 static size_t
-download_cb (char *bufptr,
-             size_t size,
-             size_t nitems,
-             void *cls)
+download_cb (char *bufptr, size_t size, size_t nitems, void *cls)
 {
   struct GNUNET_CURL_DownloadBuffer *db = cls;
   size_t msize;
@@ -222,13 +222,12 @@ download_cb (char *bufptr,
     return 0;
   }
   msize = size * nitems;
-  if ( (msize + db->buf_size) >= GNUNET_MAX_MALLOC_CHECKED)
+  if ((msize + db->buf_size) >= GNUNET_MAX_MALLOC_CHECKED)
   {
     db->eno = ENOMEM;
     return 0; /* signals an error to curl */
   }
-  db->buf = GNUNET_realloc (db->buf,
-                            db->buf_size + msize);
+  db->buf = GNUNET_realloc (db->buf, db->buf_size + msize);
   buf = db->buf + db->buf_size;
   GNUNET_memcpy (buf, bufptr, msize);
   db->buf_size += msize;
@@ -262,10 +261,7 @@ GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
   struct GNUNET_CURL_Job *job;
 
   if (GNUNET_YES == add_json)
-    if (CURLE_OK !=
-        curl_easy_setopt (eh,
-                          CURLOPT_HTTPHEADER,
-                          ctx->json_header))
+    if (CURLE_OK != curl_easy_setopt (eh, CURLOPT_HTTPHEADER, 
ctx->json_header))
     {
       GNUNET_break (0);
       curl_easy_cleanup (eh);
@@ -273,25 +269,12 @@ GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
     }
 
   job = GNUNET_new (struct GNUNET_CURL_Job);
-  if ( (CURLE_OK !=
-        curl_easy_setopt (eh,
-                          CURLOPT_PRIVATE,
-                          job)) ||
-       (CURLE_OK !=
-        curl_easy_setopt (eh,
-                          CURLOPT_WRITEFUNCTION,
-                          &download_cb)) ||
-       (CURLE_OK !=
-        curl_easy_setopt (eh,
-                          CURLOPT_WRITEDATA,
-                          &job->db)) ||
-       (CURLE_OK !=
-        curl_easy_setopt (eh,
-                          CURLOPT_SHARE,
-                          ctx->share)) ||
-       (CURLM_OK !=
-        curl_multi_add_handle (ctx->multi,
-                               eh)) )
+  if ((CURLE_OK != curl_easy_setopt (eh, CURLOPT_PRIVATE, job)) ||
+      (CURLE_OK !=
+       curl_easy_setopt (eh, CURLOPT_WRITEFUNCTION, &download_cb)) ||
+      (CURLE_OK != curl_easy_setopt (eh, CURLOPT_WRITEDATA, &job->db)) ||
+      (CURLE_OK != curl_easy_setopt (eh, CURLOPT_SHARE, ctx->share)) ||
+      (CURLM_OK != curl_multi_add_handle (ctx->multi, eh)))
   {
     GNUNET_break (0);
     GNUNET_free (job);
@@ -303,9 +286,7 @@ GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
   job->ctx = ctx;
   job->jcc = jcc;
   job->jcc_cls = jcc_cls;
-  GNUNET_CONTAINER_DLL_insert (ctx->jobs_head,
-                               ctx->jobs_tail,
-                               job);
+  GNUNET_CONTAINER_DLL_insert (ctx->jobs_head, ctx->jobs_tail, job);
   ctx->cb (ctx->cb_cls);
   return job;
 }
@@ -322,12 +303,9 @@ GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job)
 {
   struct GNUNET_CURL_Context *ctx = job->ctx;
 
-  GNUNET_CONTAINER_DLL_remove (ctx->jobs_head,
-                               ctx->jobs_tail,
-                               job);
+  GNUNET_CONTAINER_DLL_remove (ctx->jobs_head, ctx->jobs_tail, job);
   GNUNET_break (CURLM_OK ==
-                curl_multi_remove_handle (ctx->multi,
-                                          job->easy_handle));
+                curl_multi_remove_handle (ctx->multi, job->easy_handle));
   curl_easy_cleanup (job->easy_handle);
   GNUNET_free_non_null (job->db.buf);
   GNUNET_free (job);
@@ -366,20 +344,13 @@ download_get_result (struct GNUNET_CURL_DownloadBuffer 
*db,
               (int) db->buf_size,
               (char *) db->buf);
 
-  if ( (CURLE_OK !=
-        curl_easy_getinfo (eh,
-                           CURLINFO_CONTENT_TYPE,
-                           &ct)) ||
-       (NULL == ct) ||
-       (0 != strcasecmp (ct,
-                         "application/json")) )
+  if ((CURLE_OK != curl_easy_getinfo (eh, CURLINFO_CONTENT_TYPE, &ct)) ||
+      (NULL == ct) || (0 != strcasecmp (ct, "application/json")))
   {
     /* No content type or explicitly not JSON, refuse to parse
        (but keep response code) */
     if (CURLE_OK !=
-        curl_easy_getinfo (eh,
-                           CURLINFO_RESPONSE_CODE,
-                           response_code))
+        curl_easy_getinfo (eh, CURLINFO_RESPONSE_CODE, response_code))
     {
       /* unexpected error... */
       GNUNET_break (0);
@@ -409,9 +380,7 @@ download_get_result (struct GNUNET_CURL_DownloadBuffer *db,
   if (NULL != json)
   {
     if (CURLE_OK !=
-        curl_easy_getinfo (eh,
-                           CURLINFO_RESPONSE_CODE,
-                           response_code))
+        curl_easy_getinfo (eh, CURLINFO_RESPONSE_CODE, response_code))
     {
       /* unexpected error... */
       GNUNET_break (0);
@@ -430,11 +399,9 @@ download_get_result (struct GNUNET_CURL_DownloadBuffer *db,
  * @return #GNUNET_OK if no errors occurred, #GNUNET_SYSERR otherwise.
  */
 int
-GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
-                           const char *header)
+GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx, const char *header)
 {
-  ctx->json_header = curl_slist_append (ctx->json_header,
-                                        header);
+  ctx->json_header = curl_slist_append (ctx->json_header, header);
   if (NULL == ctx->json_header)
     return GNUNET_SYSERR;
 
@@ -462,96 +429,83 @@ GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
   long response_code;
   void *response;
 
-  (void) curl_multi_perform (ctx->multi,
-                             &n_running);
-  while (NULL != (cmsg = curl_multi_info_read (ctx->multi,
-                                               &n_completed)))
+  (void) curl_multi_perform (ctx->multi, &n_running);
+  while (NULL != (cmsg = curl_multi_info_read (ctx->multi, &n_completed)))
   {
     /* Only documented return value is CURLMSG_DONE */
     GNUNET_break (CURLMSG_DONE == cmsg->msg);
-    GNUNET_assert (CURLE_OK ==
-                   curl_easy_getinfo (cmsg->easy_handle,
-                                      CURLINFO_PRIVATE,
-                                      (char **) &job));
+    GNUNET_assert (CURLE_OK == curl_easy_getinfo (cmsg->easy_handle,
+                                                  CURLINFO_PRIVATE,
+                                                  (char **) &job));
     GNUNET_assert (job->ctx == ctx);
-    response_code = 0 ;
-    response = rp (&job->db,
-                   job->easy_handle,
-                   &response_code);
+    response_code = 0;
+    response = rp (&job->db, job->easy_handle, &response_code);
 #if ENABLE_BENCHMARK
-  {
-    char *url = NULL;
-    double total_as_double = 0;
-    struct GNUNET_TIME_Relative total;
-    struct UrlRequestData *urd;
-    /* Some care required, as curl is using data types (long vs curl_off_t vs
+    {
+      char *url = NULL;
+      double total_as_double = 0;
+      struct GNUNET_TIME_Relative total;
+      struct UrlRequestData *urd;
+      /* Some care required, as curl is using data types (long vs curl_off_t vs
      * double) inconsistently to store byte count. */
-    curl_off_t size_curl = 0;
-    long size_long = 0;
-    uint64_t bytes_sent = 0;
-    uint64_t bytes_received = 0;
-
-    GNUNET_break (CURLE_OK ==
-                  curl_easy_getinfo (cmsg->easy_handle,
-                                     CURLINFO_TOTAL_TIME,
-                                     &total_as_double));
-    total.rel_value_us = total_as_double * 1000 * 1000;
-
-    GNUNET_break (CURLE_OK ==
-                  curl_easy_getinfo (cmsg->easy_handle,
-                                     CURLINFO_EFFECTIVE_URL,
-                                     &url));
-
-    /* HEADER_SIZE + SIZE_DOWNLOAD_T is hopefully the total
+      curl_off_t size_curl = 0;
+      long size_long = 0;
+      uint64_t bytes_sent = 0;
+      uint64_t bytes_received = 0;
+
+      GNUNET_break (CURLE_OK == curl_easy_getinfo (cmsg->easy_handle,
+                                                   CURLINFO_TOTAL_TIME,
+                                                   &total_as_double));
+      total.rel_value_us = total_as_double * 1000 * 1000;
+
+      GNUNET_break (CURLE_OK == curl_easy_getinfo (cmsg->easy_handle,
+                                                   CURLINFO_EFFECTIVE_URL,
+                                                   &url));
+
+      /* HEADER_SIZE + SIZE_DOWNLOAD_T is hopefully the total
        number of bytes received, not clear from curl docs. */
 
-    GNUNET_break (CURLE_OK ==
-                  curl_easy_getinfo (cmsg->easy_handle,
-                                     CURLINFO_HEADER_SIZE,
-                                     &size_long));
-    bytes_received += size_long;
+      GNUNET_break (CURLE_OK == curl_easy_getinfo (cmsg->easy_handle,
+                                                   CURLINFO_HEADER_SIZE,
+                                                   &size_long));
+      bytes_received += size_long;
 
-    GNUNET_break (CURLE_OK ==
-                  curl_easy_getinfo (cmsg->easy_handle,
-                                     CURLINFO_SIZE_DOWNLOAD_T,
-                                     &size_curl));
-    bytes_received += size_curl;
+      GNUNET_break (CURLE_OK == curl_easy_getinfo (cmsg->easy_handle,
+                                                   CURLINFO_SIZE_DOWNLOAD_T,
+                                                   &size_curl));
+      bytes_received += size_curl;
 
-    /* REQUEST_SIZE + SIZE_UPLOAD_T is hopefully the total number of bytes
+      /* REQUEST_SIZE + SIZE_UPLOAD_T is hopefully the total number of bytes
        sent, again docs are not completely clear. */
 
-    GNUNET_break (CURLE_OK ==
-                  curl_easy_getinfo (cmsg->easy_handle,
-                                     CURLINFO_REQUEST_SIZE,
-                                     &size_long));
-    bytes_sent += size_long;
+      GNUNET_break (CURLE_OK == curl_easy_getinfo (cmsg->easy_handle,
+                                                   CURLINFO_REQUEST_SIZE,
+                                                   &size_long));
+      bytes_sent += size_long;
 
-    /* We obtain this value to check an invariant, but never use it otherwise. 
*/
-    GNUNET_break (CURLE_OK ==
-                  curl_easy_getinfo (cmsg->easy_handle,
-                                     CURLINFO_SIZE_UPLOAD_T,
-                                     &size_curl));
+      /* We obtain this value to check an invariant, but never use it 
otherwise. */
+      GNUNET_break (CURLE_OK == curl_easy_getinfo (cmsg->easy_handle,
+                                                   CURLINFO_SIZE_UPLOAD_T,
+                                                   &size_curl));
 
-    /* CURLINFO_SIZE_UPLOAD_T <= CURLINFO_REQUEST_SIZE should
+      /* CURLINFO_SIZE_UPLOAD_T <= CURLINFO_REQUEST_SIZE should
        be an invariant.
        As verified with
          curl -w "foo%{size_request} -XPOST --data "ABC" $URL
       the CURLINFO_REQUEST_SIZE should be the whole size of the request
       including headers and body.
      */
-    GNUNET_break (size_curl <= size_long);
-
-    urd = get_url_benchmark_data (url, (unsigned int) response_code);
-    urd->count++;
-    urd->time = GNUNET_TIME_relative_add (urd->time, total);
-    urd->time_max = GNUNET_TIME_relative_max (total, urd->time_max);
-    urd->bytes_sent += bytes_sent;
-    urd->bytes_received += bytes_received;
-  }
+      GNUNET_break (size_curl <= size_long);
+
+      urd = get_url_benchmark_data (url, (unsigned int) response_code);
+      urd->count++;
+      urd->time = GNUNET_TIME_relative_add (urd->time, total);
+      urd->time_max = GNUNET_TIME_relative_max (total, urd->time_max);
+      urd->bytes_sent += bytes_sent;
+      urd->bytes_received += bytes_received;
+    }
 #endif
-    job->jcc (job->jcc_cls,
-              response_code,
-              response);
+    job->jcc (job->jcc_cls, response_code, response);
     rc (response);
     GNUNET_CURL_job_cancel (job);
   }
@@ -566,7 +520,7 @@ GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
 void
 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx)
 {
-  
+
   GNUNET_CURL_perform2 (ctx,
                         download_get_result,
                         (GNUNET_CURL_ResponseCleaner) &json_decref);
@@ -613,25 +567,20 @@ GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context 
*ctx,
   int m;
 
   m = -1;
-  GNUNET_assert (CURLM_OK ==
-                 curl_multi_fdset (ctx->multi,
-                                   read_fd_set,
-                                   write_fd_set,
-                                   except_fd_set,
-                                   &m));
+  GNUNET_assert (CURLM_OK == curl_multi_fdset (ctx->multi,
+                                               read_fd_set,
+                                               write_fd_set,
+                                               except_fd_set,
+                                               &m));
   to = *timeout;
   *max_fd = GNUNET_MAX (m, *max_fd);
-  GNUNET_assert (CURLM_OK ==
-                 curl_multi_timeout (ctx->multi,
-                                     &to));
+  GNUNET_assert (CURLM_OK == curl_multi_timeout (ctx->multi, &to));
 
   /* Only if what we got back from curl is smaller than what we
      already had (-1 == infinity!), then update timeout */
-  if ( (to < *timeout) &&
-       (-1 != to) )
+  if ((to < *timeout) && (-1 != to))
     *timeout = to;
-  if ( (-1 == (*timeout)) &&
-       (NULL != ctx->jobs_head) )
+  if ((-1 == (*timeout)) && (NULL != ctx->jobs_head))
     *timeout = to;
 }
 
@@ -658,17 +607,14 @@ GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx)
 /**
  * Initial global setup logic, specifically runs the Curl setup.
  */
-__attribute__ ((constructor))
-void
+__attribute__ ((constructor)) void
 GNUNET_CURL_constructor__ (void)
 {
   CURLcode ret;
 
   if (CURLE_OK != (ret = curl_global_init (CURL_GLOBAL_DEFAULT)))
   {
-    CURL_STRERROR (GNUNET_ERROR_TYPE_ERROR,
-                   "curl_global_init",
-                   ret);
+    CURL_STRERROR (GNUNET_ERROR_TYPE_ERROR, "curl_global_init", ret);
     curl_fail = 1;
   }
 }
@@ -677,8 +623,7 @@ GNUNET_CURL_constructor__ (void)
 /**
  * Cleans up after us, specifically runs the Curl cleanup.
  */
-__attribute__ ((destructor))
-void
+__attribute__ ((destructor)) void
 GNUNET_CURL_destructor__ (void)
 {
   if (curl_fail)
diff --git a/src/json/.gitignore b/src/json/.gitignore
index 6709c749d..347bffd7b 100644
--- a/src/json/.gitignore
+++ b/src/json/.gitignore
@@ -1 +1,2 @@
 test_json
+test_json_mhd
diff --git a/src/json/Makefile.am b/src/json/Makefile.am
index f030c3016..dfe185d5e 100644
--- a/src/json/Makefile.am
+++ b/src/json/Makefile.am
@@ -22,7 +22,9 @@ libgnunetjson_la_LIBADD = \
   $(top_builddir)/src/util/libgnunetutil.la \
   $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \
   -ljansson \
-  $(XLIB)
+  -lmicrohttpd \
+  $(XLIB) \
+  $(Z_LIBS)
 
 check_PROGRAMS = \
   test_json \
@@ -57,6 +59,7 @@ test_json_mhd_LDADD = \
   $(top_builddir)/src/util/libgnunetutil.la \
   -ljansson \
   -lmicrohttpd \
+  $(Z_LIBS) \
   $(LIB_GNURL)
 test_json_mhd_CPPFLAGS = \
  $(CPP_GNURL) $(AM_CPPFLAGS)
diff --git a/src/json/json_mhd.c b/src/json/json_mhd.c
index 30b29b88e..b6ab2d116 100644
--- a/src/json/json_mhd.c
+++ b/src/json/json_mhd.c
@@ -26,6 +26,7 @@
  */
 #include "platform.h"
 #include "gnunet_json_lib.h"
+#include <zlib.h>
 
 
 /**
@@ -55,6 +56,11 @@ struct Buffer
    * Number of allocated bytes in buffer.
    */
   size_t alloc;
+
+  /**
+   * Maximum buffer size allowed.
+   */
+  size_t max;
 };
 
 
@@ -80,7 +86,10 @@ buffer_init (struct Buffer *buf,
   if (data_size > alloc_size)
     alloc_size = data_size;
   buf->data = GNUNET_malloc (alloc_size);
+  buf->alloc = alloc_size;
   GNUNET_memcpy (buf->data, data, data_size);
+  buf->fill = data_size;
+  buf->max = max_size;
   return GNUNET_OK;
 }
 
@@ -137,6 +146,99 @@ buffer_append (struct Buffer *buf,
 }
 
 
+/**
+ * Decompress data in @a buf.
+ *
+ * @param buf input data to inflate
+ * @return result code indicating the status of the operation
+ */
+static enum GNUNET_JSON_PostResult
+inflate_data (struct Buffer *buf)
+{
+  z_stream z;
+  char *tmp;
+  size_t tmp_size;
+  int ret;
+
+  memset (&z, 0, sizeof (z));
+  z.next_in = (Bytef *) buf->data;
+  z.avail_in = buf->fill;
+  tmp_size = GNUNET_MIN (buf->max, buf->fill * 4);
+  tmp = GNUNET_malloc (tmp_size);
+  z.next_out = (Bytef *) tmp;
+  z.avail_out = tmp_size;
+  ret = inflateInit (&z);
+  switch (ret)
+  {
+  case Z_MEM_ERROR:
+    GNUNET_break (0);
+    return GNUNET_JSON_PR_OUT_OF_MEMORY;
+  case Z_STREAM_ERROR:
+    GNUNET_break_op (0);
+    return GNUNET_JSON_PR_JSON_INVALID;
+  case Z_OK:
+    break;
+  }
+  while (1)
+  {
+    ret = inflate (&z, 0);
+    switch (ret)
+    {
+    case Z_MEM_ERROR:
+      GNUNET_break (0);
+      GNUNET_break (Z_OK == inflateEnd (&z));
+      GNUNET_free (tmp);
+      return GNUNET_JSON_PR_OUT_OF_MEMORY;
+    case Z_DATA_ERROR:
+      GNUNET_break (0);
+      GNUNET_break (Z_OK == inflateEnd (&z));
+      GNUNET_free (tmp);
+      return GNUNET_JSON_PR_JSON_INVALID;
+    case Z_NEED_DICT:
+      GNUNET_break (0);
+      GNUNET_break (Z_OK == inflateEnd (&z));
+      GNUNET_free (tmp);
+      return GNUNET_JSON_PR_JSON_INVALID;
+    case Z_OK:
+      if ((0 < z.avail_out) && (0 == z.avail_in))
+      {
+        /* truncated input stream */
+        GNUNET_break (0);
+        GNUNET_break (Z_OK == inflateEnd (&z));
+        GNUNET_free (tmp);
+        return GNUNET_JSON_PR_JSON_INVALID;
+      }
+      if (0 < z.avail_out)
+        continue; /* just call it again */
+      /* output buffer full, can we grow it? */
+      if (tmp_size == buf->max)
+      {
+        /* already at max */
+        GNUNET_break (0);
+        GNUNET_break (Z_OK == inflateEnd (&z));
+        GNUNET_free (tmp);
+        return GNUNET_JSON_PR_OUT_OF_MEMORY;
+      }
+      if (tmp_size * 2 < tmp_size)
+        tmp_size = buf->max;
+      else
+        tmp_size = GNUNET_MIN (buf->max, tmp_size * 2);
+      tmp = GNUNET_realloc (tmp, tmp_size);
+      z.next_out = (Bytef *) &tmp[z.total_out];
+      continue;
+    case Z_STREAM_END:
+      /* decompression successful, make 'tmp' the new 'data' */
+      GNUNET_free (buf->data);
+      buf->data = tmp;
+      buf->alloc = tmp_size;
+      buf->fill = z.total_out;
+      GNUNET_break (Z_OK == inflateEnd (&z));
+      return GNUNET_JSON_PR_SUCCESS; /* at least for now */
+    }
+  } /* while (1) */
+}
+
+
 /**
  * Process a POST request containing a JSON object.  This function
  * realizes an MHD POST processor that will (incrementally) process
@@ -161,10 +263,13 @@ GNUNET_JSON_post_parser (size_t buffer_max,
                          json_t **json)
 {
   struct Buffer *r = *con_cls;
+  const char *ce;
+  int ret;
 
   *json = NULL;
   if (NULL == *con_cls)
   {
+
     /* We are seeing a fresh POST request. */
     r = GNUNET_new (struct Buffer);
     if (GNUNET_OK != buffer_init (r,
@@ -202,12 +307,29 @@ GNUNET_JSON_post_parser (size_t buffer_max,
   }
 
   /* We have seen the whole request. */
+  ce = MHD_lookup_connection_value (connection,
+                                    MHD_HEADER_KIND,
+                                    MHD_HTTP_HEADER_CONTENT_ENCODING);
+  if ((NULL != ce) && (0 == strcasecmp ("deflate", ce)))
+  {
+    ret = inflate_data (r);
+    if (GNUNET_JSON_PR_SUCCESS != ret)
+    {
+      buffer_deinit (r);
+      GNUNET_free (r);
+      *con_cls = NULL;
+      return ret;
+    }
+  }
 
   *json = json_loadb (r->data, r->fill, 0, NULL);
   if (NULL == *json)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 "Failed to parse JSON request body\n");
+    buffer_deinit (r);
+    GNUNET_free (r);
+    *con_cls = NULL;
     return GNUNET_JSON_PR_JSON_INVALID;
   }
   buffer_deinit (r);
diff --git a/src/json/test_json_mhd.c b/src/json/test_json_mhd.c
index 665fd140e..13338b32c 100644
--- a/src/json/test_json_mhd.c
+++ b/src/json/test_json_mhd.c
@@ -27,6 +27,7 @@
 #include "gnunet_util_lib.h"
 #include "gnunet_json_lib.h"
 #include "gnunet_curl_lib.h"
+#include <zlib.h>
 
 #define MAX_SIZE 1024 * 1024
 
@@ -69,7 +70,7 @@ access_handler_cb (void *cls,
       global_ret = 6;
     }
     json_decref (json);
-    resp = MHD_create_response_from_buffer (2, "OK", MHD_RESPMEM_PERSISTENT);
+    resp = MHD_create_response_from_buffer (3, "OK\n", MHD_RESPMEM_PERSISTENT);
     ret = MHD_queue_response (connection, MHD_HTTP_OK, resp);
     MHD_destroy_response (resp);
     return ret;
@@ -100,8 +101,12 @@ main (int argc, const char *const argv[])
   uint16_t port;
   CURL *easy;
   char *url;
+  char *str;
+  size_t slen;
   long post_data_size;
   void *post_data;
+  uLongf dlen;
+  struct curl_slist *json_header;
 
   GNUNET_log_setup ("test-json-mhd", "WARNING", NULL);
   global_ret = 2;
@@ -123,28 +128,60 @@ main (int argc, const char *const argv[])
     GNUNET_snprintf (tmp, sizeof (tmp), "%u", i);
     json_object_set_new (bigj, tmp, json_string (tmp));
   }
-  post_data = json_dumps (bigj, JSON_INDENT (2));
-  post_data_size = strlen (post_data);
-
+  str = json_dumps (bigj, JSON_INDENT (2));
+  slen = strlen (str);
+
+#ifdef compressBound
+  dlen = compressBound (slen);
+#else
+  dlen = slen + slen / 100 + 20;
+  /* documentation says 100.1% oldSize + 12 bytes, but we
+   * should be able to overshoot by more to be safe */
+#endif
+  post_data = GNUNET_malloc (dlen);
+  if (Z_OK !=
+      compress2 ((Bytef *) post_data, &dlen, (const Bytef *) str, slen, 9))
+  {
+    GNUNET_break (0);
+    MHD_stop_daemon (daemon);
+    GNUNET_free (url);
+    json_decref (bigj);
+    GNUNET_free (post_data);
+    GNUNET_free (str);
+    return 1;
+  }
+  post_data_size = (long) dlen;
   port = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT)->port;
   easy = curl_easy_init ();
   GNUNET_asprintf (&url, "http://localhost:%u/";, (unsigned int) port);
-  curl_easy_setopt (easy, CURLOPT_VERBOSE, 1);
+  curl_easy_setopt (easy, CURLOPT_VERBOSE, 0);
   curl_easy_setopt (easy, CURLOPT_URL, url);
   curl_easy_setopt (easy, CURLOPT_POST, 1);
   curl_easy_setopt (easy, CURLOPT_POSTFIELDS, post_data);
   curl_easy_setopt (easy, CURLOPT_POSTFIELDSIZE, post_data_size);
+
+  json_header = curl_slist_append (NULL, "Content-Type: application/json");
+  json_header = curl_slist_append (json_header, "Content-Encoding: deflate");
+  curl_easy_setopt (easy, CURLOPT_HTTPHEADER, json_header);
   if (0 != curl_easy_perform (easy))
   {
     GNUNET_break (0);
     MHD_stop_daemon (daemon);
     GNUNET_free (url);
     json_decref (bigj);
+    GNUNET_free (post_data);
+    GNUNET_free (str);
+    curl_slist_free_all (json_header);
+    curl_easy_cleanup (easy);
     return 1;
   }
   MHD_stop_daemon (daemon);
   GNUNET_free (url);
   json_decref (bigj);
+  GNUNET_free (post_data);
+  GNUNET_free (str);
+  curl_slist_free_all (json_header);
+  curl_easy_cleanup (easy);
   return global_ret;
 }
 

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]