mhd_h2_request_get_values_cb() reported the length of the field value
as the length of the field name, so the application got the name
followed by its terminating zero and a part of the value, or a
truncated name.

MHDT_server_reply_check_header() now also finds the header with
MHD_request_get_values_cb() and checks the length of its name, so the
custom header case of test_http2 covers the iterator.
---
 src/mhd2/h2/h2_req_get_items.c                |  2 +-
 src/tests/client_server/libtest.h             |  3 +-
 .../libtest_convenience_server_reply.c        | 61 +++++++++++++++++++
 3 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/src/mhd2/h2/h2_req_get_items.c b/src/mhd2/h2/h2_req_get_items.c
index 8e0a899..183175c 100644
--- a/src/mhd2/h2/h2_req_get_items.c
+++ b/src/mhd2/h2/h2_req_get_items.c
@@ -235,7 +235,7 @@ mhd_h2_request_get_values_cb (struct MHD_Request *r,
       struct MHD_NameAndValue nv;

       nv.name.cstr = buff + itm->offset;
-      nv.name.len = (size_t)itm->val_len;
+      nv.name.len = (size_t)itm->name_len;
       if (mhd_H2_RIK_URI_PARAM_NV != itm->kind)
       {
         nv.value.cstr = buff + itm->offset + itm->name_len + 1u;
diff --git a/src/tests/client_server/libtest.h
b/src/tests/client_server/libtest.h
index 81952e5..b180b04 100644
--- a/src/tests/client_server/libtest.h
+++ b/src/tests/client_server/libtest.h
@@ -649,7 +649,8 @@ MHDT_server_reply_check_query (

 /**
  * Checks that the client request includes the given
- * custom header.  If so, returns #MHD_HTTP_STATUS_NO_CONTENT.
+ * custom header, both by its name and by iterating over
+ * the headers.  If so, returns #MHD_HTTP_STATUS_NO_CONTENT.
  *
  * @param cls expected header with "$NAME:$VALUE" format.
  * @param request the request object
diff --git a/src/tests/client_server/libtest_convenience_server_reply.c
b/src/tests/client_server/libtest_convenience_server_reply.c
index 5a88b26..6021cc7 100644
--- a/src/tests/client_server/libtest_convenience_server_reply.c
+++ b/src/tests/client_server/libtest_convenience_server_reply.c
@@ -50,6 +50,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <errno.h>
+#include <strings.h>
 #include <curl/curl.h>
 #include <assert.h>
 #include "mhdt_tmpfile.h"
@@ -319,6 +320,51 @@ MHDT_server_reply_check_query (
 }


+/**
+ * Closure for #find_header_name().
+ */
+struct HeaderName
+{
+  /**
+   * The name of the header to find.
+   */
+  const char *name;
+
+  /**
+   * Set to the length of the name given by the iterator,
+   * zero if the header has not been found.
+   */
+  size_t len;
+};
+
+
+/**
+ * Records the length of the name that the iterator gives
+ * for the header to find.
+ *
+ * @param cls the `struct HeaderName`
+ * @param kind the kind of the element
+ * @param nv the name and the value of the element
+ * @return #MHD_NO if the header has been found,
+ *         #MHD_YES to continue iterating
+ */
+static enum MHD_Bool
+find_header_name (
+  void *cls,
+  enum MHD_ValueKind kind,
+  const struct MHD_NameAndValue *nv)
+{
+  struct HeaderName *hn = cls;
+
+  (void)kind;                                     /* Unused */
+  if (0 != strcasecmp (hn->name,
+                       nv->name.cstr))
+    return MHD_YES;
+  hn->len = nv->name.len;
+  return MHD_NO;
+}
+
+
 const struct MHD_Action *
 MHDT_server_reply_check_header (
   void *cls,
@@ -333,6 +379,7 @@ MHDT_server_reply_check_header (
   const char *colon = strchr (want, ':');
   struct MHD_StringNullable have;
   const char *value;
+  struct HeaderName hn;

   (void)path;
   (void)method;
@@ -392,6 +439,20 @@ MHDT_server_reply_check_header (
       return MHD_action_abort_request (request);
     }
   }
+  hn.name = key;
+  hn.len = 0;
+  MHD_request_get_values_cb (request,
+                             MHD_VK_HEADER,
+                             &find_header_name,
+                             &hn);
+  if (strlen (key) != hn.len)
+  {
+    fprintf (stderr,
+             "Client header `%s' has name length %u in the iterator\n",
+             key,
+             (unsigned int)hn.len);
+    return MHD_action_abort_request (request);
+  }
   return MHD_action_from_response (
     request,
     MHD_response_from_empty (
-- 
2.43.0

Reply via email to