gnunet-svn
[Top][All Lists]
Advanced

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

[libmicrohttpd] branch master updated (8df85c22 -> db2ab3a5)


From: gnunet
Subject: [libmicrohttpd] branch master updated (8df85c22 -> db2ab3a5)
Date: Sun, 01 Aug 2021 12:28:03 +0200

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

karlson2k pushed a change to branch master
in repository libmicrohttpd.

    from 8df85c22 Updated some copyrights
     new aadbe635 connection cleanup: streamlined cleanup process
     new 497a3546 Added MHD_uint64_to_str() internal function
     new db2ab3a5 response: do not allow "Connection: keep-alive" header

The 3 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/microhttpd/connection.c            |  17 +++-
 src/microhttpd/internal.h              |   3 +-
 src/microhttpd/mhd_str.c               |  42 +++++++++
 src/microhttpd/mhd_str.h               |  21 +++++
 src/microhttpd/response.c              |  12 ++-
 src/microhttpd/test_response_entries.c | 163 +++++++++++++++++++++++++++++++++
 src/microhttpd/test_str.c              | 140 ++++++++++++++++++++++++++++
 7 files changed, 390 insertions(+), 8 deletions(-)

diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index 21340001..a59e3d60 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -677,8 +677,6 @@ MHD_connection_mark_closed_ (struct MHD_Connection 
*connection)
 {
   const struct MHD_Daemon *daemon = connection->daemon;
 
-  connection->state = MHD_CONNECTION_CLOSED;
-  connection->event_loop_info = MHD_EVENT_LOOP_INFO_CLEANUP;
   if (0 == (daemon->options & MHD_USE_TURBO))
   {
 #ifdef HTTPS_SUPPORT
@@ -698,6 +696,8 @@ MHD_connection_mark_closed_ (struct MHD_Connection 
*connection)
     shutdown (connection->socket_fd,
               SHUT_WR);
   }
+  connection->state = MHD_CONNECTION_CLOSED;
+  connection->event_loop_info = MHD_EVENT_LOOP_INFO_CLEANUP;
 }
 
 
@@ -733,6 +733,11 @@ MHD_connection_close_ (struct MHD_Connection *connection,
     connection->response = NULL;
     MHD_destroy_response (resp);
   }
+  if (NULL != connection->pool)
+  {
+    MHD_pool_destroy (connection->pool);
+    connection->pool = NULL;
+  }
 
   MHD_connection_mark_closed_ (connection);
 }
@@ -3743,14 +3748,16 @@ connection_reset (struct MHD_Connection *connection,
   if (! reuse)
   {
     /* Next function will destroy response, notify client,
-     * and set state "CLOSED" */
+     * destroy memory pool, and set connection state to "CLOSED" */
     MHD_connection_close_ (connection,
                            MHD_REQUEST_TERMINATED_COMPLETED_OK);
-    MHD_pool_destroy (connection->pool);
-    c->pool = NULL;
     c->read_buffer = NULL;
     c->read_buffer_size = 0;
     c->read_buffer_offset = 0;
+    c->write_buffer = NULL;
+    c->write_buffer_size = 0;
+    c->write_buffer_send_offset = 0;
+    c->write_buffer_append_offset = 0;
   }
   else
   {
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h
index d1f3b9ff..ad60502e 100644
--- a/src/microhttpd/internal.h
+++ b/src/microhttpd/internal.h
@@ -1182,8 +1182,7 @@ struct MHD_Connection
   bool in_idle;
 
   /**
-   * Are we currently inside the "idle" handler (to avoid recursively
-   * invoking it).
+   * Connection is in the cleanup DL-linked list.
    */
   bool in_cleanup;
 
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
index 1d4b9257..b106859d 100644
--- a/src/microhttpd/mhd_str.c
+++ b/src/microhttpd/mhd_str.c
@@ -1216,6 +1216,7 @@ MHD_uint32_to_strx (uint32_t val,
 }
 
 
+#ifndef MHD_FAVOR_SMALL_CODE
 size_t
 MHD_uint16_to_str (uint16_t val,
                    char *buf,
@@ -1252,3 +1253,44 @@ MHD_uint16_to_str (uint16_t val,
   }
   return 0; /* The buffer is too small */
 }
+
+
+#endif /* !MHD_FAVOR_SMALL_CODE */
+
+
+size_t
+MHD_uint64_to_str (uint64_t val,
+                   char *buf,
+                   size_t buf_size)
+{
+  char *chr;  /**< pointer to the current printed digit */
+  /* The biggest printable number is 18446744073709551615 */
+  uint64_t divisor = UINT64_C (10000000000000000000);
+  int digit;
+
+  chr = buf;
+  digit = (int) (val / divisor);
+  mhd_assert (digit < 10);
+
+  /* Do not print leading zeros */
+  while ((0 == digit) && (1 < divisor))
+  {
+    divisor /= 10;
+    digit = (int) (val / divisor);
+    mhd_assert (digit < 10);
+  }
+
+  while (0 != buf_size)
+  {
+    *chr = (char) digit + '0';
+    chr++;
+    buf_size--;
+    if (1 == divisor)
+      return (size_t) (chr - buf);
+    val %= divisor;
+    divisor /= 10;
+    digit = (int) (val / divisor);
+    mhd_assert (digit < 10);
+  }
+  return 0; /* The buffer is too small */
+}
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h
index 91d5f46e..9cc4bb19 100644
--- a/src/microhttpd/mhd_str.h
+++ b/src/microhttpd/mhd_str.h
@@ -389,6 +389,7 @@ MHD_uint32_to_strx (uint32_t val,
                     size_t buf_size);
 
 
+#ifndef MHD_FAVOR_SMALL_CODE
 /**
  * Convert uint16_t value to decimal US-ASCII string.
  * @note: result is NOT zero-terminated.
@@ -403,4 +404,24 @@ MHD_uint16_to_str (uint16_t val,
                    char *buf,
                    size_t buf_size);
 
+#else  /* MHD_FAVOR_SMALL_CODE */
+#define MHD_uint16_to_str(v,b,s) MHD_uint64_to_str(v,b,s)
+#endif /* MHD_FAVOR_SMALL_CODE */
+
+
+/**
+ * Convert uint64_t value to decimal US-ASCII string.
+ * @note: result is NOT zero-terminated.
+ * @param val the value to convert
+ * @param buf the buffer to result to
+ * @param buf_size size of the @a buffer
+ * @return number of charters has been put to the @a buf,
+ *         zero if buffer is too small (buffer may be modified).
+ */
+size_t
+MHD_uint64_to_str (uint64_t val,
+                   char *buf,
+                   size_t buf_size);
+
+
 #endif /* MHD_STR_H */
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index ec09d14a..3833990c 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -251,6 +251,8 @@ add_response_header_connection (struct MHD_Response 
*response,
     old_value_len = 0;
 
   value_len = strlen (value);
+  if (value_len >= SSIZE_MAX)
+    return MHD_NO;
   /* Additional space for normalisation and zero-termination*/
   norm_len = (ssize_t) (value_len + value_len / 2 + 1);
   buf_size = old_value_len + (size_t) norm_len;
@@ -258,7 +260,7 @@ add_response_header_connection (struct MHD_Response 
*response,
   buf = malloc (buf_size);
   if (NULL == buf)
     return MHD_NO;
-  /* Move "close" token (if any) to the front */
+  /* Remove "close" token (if any), it will be moved to the front */
   value_has_close = MHD_str_remove_token_caseless_ (value, value_len, "close",
                                                     MHD_STATICSTR_LEN_ ( \
                                                       "close"),
@@ -267,6 +269,14 @@ add_response_header_connection (struct MHD_Response 
*response,
   mhd_assert (0 <= norm_len);
   if (0 > norm_len)
     norm_len = 0; /* Must never happen */
+  if (0 != norm_len)
+  {
+    size_t len = norm_len;
+    MHD_str_remove_tokens_caseless_ (buf + old_value_len, &len,
+                                     "keep-alive",
+                                     MHD_STATICSTR_LEN_ ("keep-alive"));
+    norm_len = (ssize_t) len;
+  }
   if (0 == norm_len)
   { /* New value is empty after normalisation */
     if (! value_has_close)
diff --git a/src/microhttpd/test_response_entries.c 
b/src/microhttpd/test_response_entries.c
index 5a21b025..c4bf293d 100644
--- a/src/microhttpd/test_response_entries.c
+++ b/src/microhttpd/test_response_entries.c
@@ -612,6 +612,169 @@ main (int argc,
     return 3;
   }
 
+  if (MHD_NO != MHD_add_response_header (r, "Connection", "keep-Alive"))
+  {
+    fprintf (stderr,
+             "Successfully added \"Connection\" header with 
\"keep-Alive\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"), NULL))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_YES != MHD_add_response_header (r, "Connection", "keep-Alive, 
Close"))
+  {
+    fprintf (stderr,
+             "Cannot add \"Connection\" header with \"keep-Alive, Close\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"), "close"))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_NO != MHD_add_response_header (r, "Connection", "keep-Alive"))
+  {
+    fprintf (stderr,
+             "Successfully added \"Connection\" header with 
\"keep-Alive\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_YES != MHD_add_response_header (r, "Connection", "keep-Alive, 
Close"))
+  {
+    fprintf (stderr,
+             "Cannot add \"Connection\" header with \"keep-Alive, Close\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"), "close"))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_YES != MHD_add_response_header (r, "Connection",
+                                          "close, additional-token"))
+  {
+    fprintf (stderr, "Cannot add \"Connection\" header with "
+             "\"close, additional-token\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"),
+                    "close, additional-token"))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_NO != MHD_add_response_header (r, "Connection", "keep-Alive"))
+  {
+    fprintf (stderr,
+             "Successfully added \"Connection\" header with 
\"keep-Alive\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"),
+                    "close, additional-token"))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_YES != MHD_del_response_header (r, "Connection",
+                                          "additional-token,close"))
+  {
+    fprintf (stderr, "Cannot remove tokens from \"Connection\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"), NULL))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+
+  if (MHD_YES != MHD_add_response_header (r, "Connection",
+                                          "Keep-aLive, token-1"))
+  {
+    fprintf (stderr,
+             "Cannot add \"Connection\" header with \"Keep-aLive, 
token-1\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"), "token-1"))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_YES != MHD_add_response_header (r, "Connection",
+                                          "Keep-aLive, token-2"))
+  {
+    fprintf (stderr,
+             "Cannot add \"Connection\" header with \"Keep-aLive, 
token-2\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"),
+                    "token-1, token-2"))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_YES != MHD_add_response_header (r, "Connection",
+                                          "Keep-aLive, token-3, close"))
+  {
+    fprintf (stderr,
+             "Cannot add \"Connection\" header with \"Keep-aLive, token-3, 
close\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"),
+                    "close, token-1, token-2, token-3"))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_YES != MHD_del_response_header (r, "Connection",
+                                          "close"))
+  {
+    fprintf (stderr, "Cannot remove \"close\" tokens from \"Connection\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"),
+                    "token-1, token-2, token-3"))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_YES != MHD_add_response_header (r, "Connection", "Keep-aLive, 
close"))
+  {
+    fprintf (stderr,
+             "Cannot add \"Connection\" header with \"Keep-aLive, token-3, 
close\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"),
+                    "close, token-1, token-2, token-3"))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (MHD_YES != MHD_del_response_header (r, "Connection",
+                                          "close, token-1, Keep-Alive, 
token-2, token-3"))
+  {
+    fprintf (stderr, "Cannot remove \"close\" tokens from \"Connection\".\n");
+    MHD_destroy_response (r);
+    return 4;
+  }
+  if (! expect_str (MHD_get_response_header (r, "Connection"), NULL))
+  {
+    MHD_destroy_response (r);
+    return 4;
+  }
+
   MHD_destroy_response (r);
   printf ("All tests has been successfully passed.\n");
   return 0;
diff --git a/src/microhttpd/test_str.c b/src/microhttpd/test_str.c
index 81ba378f..a65cb61d 100644
--- a/src/microhttpd/test_str.c
+++ b/src/microhttpd/test_str.c
@@ -3438,6 +3438,128 @@ check_str_from_uint16 (void)
 }
 
 
+int
+check_str_from_uint64 (void)
+{
+  size_t t_failed = 0;
+  size_t i, j;
+  char buf[70];
+  const char *erase =
+    "-@=sd#+&(pdirenDSFGSe#@C&S!DAS*!AOIPUQWESAdFzxcvSs*()&#$%KH`"
+    "32452d098poiden45SADFFDA3S4D3SDFdfgsdfgsSADFzxdvs$*()&#2342`"
+    "adsf##$$@&*^%*^&56qwe#3C@S!DAScFAOIP$#%#$Ad1zs3v1$*()&#1228`";
+  static const size_t n_checks = sizeof(dstrs_w_values)
+                                 / sizeof(dstrs_w_values[0]);
+  int c_failed[n_checks];
+
+  memset (c_failed, 0, sizeof(c_failed));
+
+  for (j = 0; j < locale_name_count; j++)
+  {
+    set_test_locale (j);  /* setlocale() can be slow! */
+    for (i = 0; i < n_checks; i++)
+    {
+      const struct str_with_value *const t = dstrs_w_values + i;
+      size_t b_size;
+      size_t rs;
+
+      if (c_failed[i])
+        continue;     /* skip already failed checks */
+
+      if (t->str.len < t->num_of_digt)
+      {
+        fprintf (stderr,
+                 "ERROR: dstrs_w_values[%u] has wrong num_of_digt (%u): 
num_of_digt is expected"
+                 " to be less or equal to str.len (%u).\n",
+                 (unsigned int) i, (unsigned int) t->num_of_digt, (unsigned
+                                                                   int) t->str.
+                 len);
+        return -1;
+      }
+      if ('0' == t->str.str[0])
+        continue;  /* Skip strings prefixed with zeros */
+      if (t->num_of_digt != t->str.len)
+        continue;  /* Skip strings with suffixes */
+      if (sizeof(buf) < t->str.len + 1)
+      {
+        fprintf (stderr,
+                 "ERROR: dstrs_w_values[%u] has too long (%u) string, "
+                 "size of 'buf' should be increased.\n",
+                 (unsigned int) i, (unsigned int) t->str.len);
+        return -1;
+      }
+      for (b_size = 0; b_size <= t->str.len + 1; ++b_size)
+      {
+        /* fill buffer with pseudo-random values */
+        memcpy (buf, erase, sizeof(buf));
+
+        rs = MHD_uint64_to_str (t->val, buf, b_size);
+
+        if (t->num_of_digt > b_size)
+        {
+          /* Must fail, buffer is too small for result */
+          if (0 != rs)
+          {
+            if (0 == c_failed[i])
+              t_failed++;
+            c_failed[i] = ! 0;
+            fprintf (stderr,
+                     "FAILED: MHD_uint64_to_str(%" PRIu64 ", -> buf,"
+                     " %d) returned %" PRIuPTR
+                     ", while expecting 0."
+                     " Locale: %s\n", t->val, (int) b_size, (intptr_t) rs,
+                     get_current_locale_str ());
+          }
+        }
+        else
+        {
+          if (t->num_of_digt != rs)
+          {
+            if (0 == c_failed[i])
+              t_failed++;
+            c_failed[i] = ! 0;
+            fprintf (stderr,
+                     "FAILED: MHD_uint64_to_str(%" PRIu64 ", -> buf,"
+                     " %d) returned %" PRIuPTR
+                     ", while expecting %d."
+                     " Locale: %s\n", t->val, (int) b_size, (intptr_t) rs,
+                     (int) t->num_of_digt, get_current_locale_str ());
+          }
+          else if (0 != memcmp (buf, t->str.str, t->num_of_digt))
+          {
+            if (0 == c_failed[i])
+              t_failed++;
+            c_failed[i] = ! 0;
+            fprintf (stderr,
+                     "FAILED: MHD_uint64_to_str(%" PRIu64 ", -> \"%.*s\","
+                     " %d) returned %" PRIuPTR "."
+                     " Locale: %s\n", t->val, (int) rs, buf, (int) b_size,
+                     (intptr_t) rs,  get_current_locale_str ());
+          }
+          else if (0 != memcmp (buf + rs, erase + rs, sizeof(buf) - rs))
+          {
+            if (0 == c_failed[i])
+              t_failed++;
+            c_failed[i] = ! 0;
+            fprintf (stderr,
+                     "FAILED: MHD_uint64_to_str(%" PRIu64 ", -> \"%.*s\","
+                     " %d) returned %" PRIuPTR
+                     " and touched data after the resulting string."
+                     " Locale: %s\n", t->val, (int) rs, buf, (int) b_size,
+                     (intptr_t) rs,  get_current_locale_str ());
+          }
+        }
+      }
+      if ((verbose > 1) && (j == locale_name_count - 1) && ! c_failed[i])
+        printf ("PASSED: MHD_uint64_to_str(%" PRIu64 ", -> \"%.*s\", %d) "
+                "== %" PRIuPTR "\n",
+                t->val, (int) rs, buf, (int) b_size - 1, (intptr_t) rs);
+    }
+  }
+  return t_failed;
+}
+
+
 int
 check_strx_from_uint32 (void)
 {
@@ -3567,6 +3689,7 @@ int
 run_str_from_X_tests (void)
 {
   int str_from_uint16;
+  int str_from_uint64;
   int strx_from_uint32;
   int failures;
 
@@ -3589,6 +3712,23 @@ run_str_from_X_tests (void)
     printf (
       "PASSED: testcase check_str_from_uint16() successfully passed.\n\n");
 
+  str_from_uint64 = check_str_from_uint64 ();
+  if (str_from_uint64 != 0)
+  {
+    if (str_from_uint64 < 0)
+    {
+      fprintf (stderr,
+               "ERROR: test internal error in check_str_from_uint16().\n");
+      return 99;
+    }
+    fprintf (stderr,
+             "FAILED: testcase check_str_from_uint16() failed.\n\n");
+    failures += str_from_uint64;
+  }
+  else if (verbose > 1)
+    printf (
+      "PASSED: testcase check_str_from_uint16() successfully passed.\n\n");
+
   strx_from_uint32 = check_strx_from_uint32 ();
   if (strx_from_uint32 != 0)
   {

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

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