gnunet-svn
[Top][All Lists]
Advanced

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

[taler-exchange] 136/152: add TALER_MHD_parse_internal_json_data()


From: gnunet
Subject: [taler-exchange] 136/152: add TALER_MHD_parse_internal_json_data()
Date: Sun, 05 Jul 2020 20:48:12 +0200

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

grothoff pushed a commit to branch master
in repository exchange.

commit 25ecc97717490a0ee149cc0af5cec07e764d1acb
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Sun Jun 21 23:05:51 2020 +0200

    add TALER_MHD_parse_internal_json_data()
---
 src/include/taler_mhd_lib.h | 27 ++++++++++++++++++-
 src/mhd/mhd_parsing.c       | 64 ++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 84 insertions(+), 7 deletions(-)

diff --git a/src/include/taler_mhd_lib.h b/src/include/taler_mhd_lib.h
index 71006e8b..8cfb8072 100644
--- a/src/include/taler_mhd_lib.h
+++ b/src/include/taler_mhd_lib.h
@@ -264,7 +264,8 @@ TALER_MHD_parse_post_cleanup_callback (void *con_cls);
 
 /**
  * Parse JSON object into components based on the given field
- * specification.
+ * specification.  If parsing fails, we return an HTTP
+ * status code of 400 (#MHD_HTTP_BAD_REQUEST).
  *
  * @param connection the connection to send an error response to
  * @param root the JSON node to start the navigation at.
@@ -282,6 +283,30 @@ TALER_MHD_parse_json_data (struct MHD_Connection 
*connection,
                            struct GNUNET_JSON_Specification *spec);
 
 
+/**
+ * Parse JSON object that we (the server!) generated into components based on
+ * the given field specification.  The difference to
+ * #TALER_MHD_parse_json_data() is that this function will fail
+ * with an HTTP failure of 500 (internal server error) in case
+ * parsing fails, instead of blaming it on the client with a
+ * 400 (#MHD_HTTP_BAD_REQUEST).
+ *
+ * @param connection the connection to send an error response to
+ * @param root the JSON node to start the navigation at.
+ * @param spec field specification for the parser
+ * @return
+ *    #GNUNET_YES if navigation was successful (caller is responsible
+ *                for freeing allocated variable-size data using
+ *                GNUNET_JSON_parse_free() when done)
+ *    #GNUNET_NO if json is malformed, error response was generated
+ *    #GNUNET_SYSERR on internal error
+ */
+enum GNUNET_GenericReturnValue
+TALER_MHD_parse_internal_json_data (struct MHD_Connection *connection,
+                                    const json_t *root,
+                                    struct GNUNET_JSON_Specification *spec);
+
+
 /**
  * Parse JSON array into components based on the given field
  * specification.  Generates error response on parse errors.
diff --git a/src/mhd/mhd_parsing.c b/src/mhd/mhd_parsing.c
index fca54f3f..2a086e11 100644
--- a/src/mhd/mhd_parsing.c
+++ b/src/mhd/mhd_parsing.c
@@ -1,6 +1,6 @@
 /*
   This file is part of TALER
-  Copyright (C) 2014--2019 Taler Systems SA
+  Copyright (C) 2014--2020 Taler Systems SA
 
   TALER is free software; you can redistribute it and/or modify it under the
   terms of the GNU Affero General Public License as published by the Free 
Software
@@ -56,7 +56,7 @@
  *               (we could not even queue an error message,
  *                close HTTP session with MHD_NO)
  */
-int
+enum GNUNET_GenericReturnValue
 TALER_MHD_parse_post_json (struct MHD_Connection *connection,
                            void **con_cls,
                            const char *upload_data,
@@ -186,12 +186,12 @@ TALER_MHD_parse_request_arg_data (struct MHD_Connection 
*connection,
  *    #GNUNET_NO if json is malformed, error response was generated
  *    #GNUNET_SYSERR on internal error
  */
-int
+enum GNUNET_GenericReturnValue
 TALER_MHD_parse_json_data (struct MHD_Connection *connection,
                            const json_t *root,
                            struct GNUNET_JSON_Specification *spec)
 {
-  int ret;
+  enum GNUNET_GenericReturnValue ret;
   const char *error_json_name;
   unsigned int error_line;
 
@@ -220,6 +220,58 @@ TALER_MHD_parse_json_data (struct MHD_Connection 
*connection,
 }
 
 
+/**
+ * Parse JSON object that we (the server!) generated into components based on
+ * the given field specification.  The difference to
+ * #TALER_MHD_parse_json_data() is that this function will fail
+ * with an HTTP failure of 500 (internal server error) in case
+ * parsing fails, instead of blaming it on the client with a
+ * 400 (#MHD_HTTP_BAD_REQUEST).
+ *
+ * @param connection the connection to send an error response to
+ * @param root the JSON node to start the navigation at.
+ * @param spec field specification for the parser
+ * @return
+ *    #GNUNET_YES if navigation was successful (caller is responsible
+ *                for freeing allocated variable-size data using
+ *                GNUNET_JSON_parse_free() when done)
+ *    #GNUNET_NO if json is malformed, error response was generated
+ *    #GNUNET_SYSERR on internal error
+ */
+enum GNUNET_GenericReturnValue
+TALER_MHD_parse_internal_json_data (struct MHD_Connection *connection,
+                                    const json_t *root,
+                                    struct GNUNET_JSON_Specification *spec)
+{
+  enum GNUNET_GenericReturnValue ret;
+  const char *error_json_name;
+  unsigned int error_line;
+
+  ret = GNUNET_JSON_parse (root,
+                           spec,
+                           &error_json_name,
+                           &error_line);
+  if (GNUNET_SYSERR == ret)
+  {
+    if (NULL == error_json_name)
+      error_json_name = "<no field>";
+    ret = (MHD_YES ==
+           TALER_MHD_reply_json_pack (connection,
+                                      MHD_HTTP_INTERNAL_SERVER_ERROR,
+                                      "{s:s, s:I, s:s, s:I}",
+                                      "hint", "JSON parse error",
+                                      "code",
+                                      (json_int_t)
+                                      TALER_EC_INTERNAL_INVARIANT_FAILURE,
+                                      "field", error_json_name,
+                                      "line", (json_int_t) error_line))
+          ? GNUNET_NO : GNUNET_SYSERR;
+    return ret;
+  }
+  return GNUNET_YES;
+}
+
+
 /**
  * Parse JSON array into components based on the given field
  * specification.  Generates error response on parse errors.
@@ -235,13 +287,13 @@ TALER_MHD_parse_json_data (struct MHD_Connection 
*connection,
  *    #GNUNET_NO if json is malformed, error response was generated
  *    #GNUNET_SYSERR on internal error
  */
-int
+enum GNUNET_GenericReturnValue
 TALER_MHD_parse_json_array (struct MHD_Connection *connection,
                             const json_t *root,
                             struct GNUNET_JSON_Specification *spec,
                             ...)
 {
-  int ret;
+  enum GNUNET_GenericReturnValue ret;
   const char *error_json_name;
   unsigned int error_line;
   va_list ap;

-- 
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]