LCOV - code coverage report
Current view: top level - mhd - mhd_parsing.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 38 77 49.4 %
Date: 2021-08-30 06:43:37 Functions: 5 6 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014--2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Affero General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Affero General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file mhd_parsing.c
      18             :  * @brief functions to parse incoming requests (MHD arguments and JSON snippets)
      19             :  * @author Florian Dold
      20             :  * @author Benedikt Mueller
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include <gnunet/gnunet_json_lib.h>
      26             : #include "taler_json_lib.h"
      27             : #include "taler_mhd_lib.h"
      28             : 
      29             : 
      30             : /**
      31             :  * Maximum POST request size.
      32             :  */
      33             : #define REQUEST_BUFFER_MAX (1024 * 1024)
      34             : 
      35             : 
      36             : /**
      37             :  * Process a POST request containing a JSON object.  This function
      38             :  * realizes an MHD POST processor that will (incrementally) process
      39             :  * JSON data uploaded to the HTTP server.  It will store the required
      40             :  * state in the @a con_cls, which must be cleaned up using
      41             :  * #TALER_MHD_parse_post_cleanup_callback().
      42             :  *
      43             :  * @param connection the MHD connection
      44             :  * @param con_cls the closure (points to a `struct Buffer *`)
      45             :  * @param upload_data the POST data
      46             :  * @param upload_data_size number of bytes in @a upload_data
      47             :  * @param json the JSON object for a completed request
      48             :  * @return
      49             :  *    #GNUNET_YES if json object was parsed or at least
      50             :  *               may be parsed in the future (call again);
      51             :  *               `*json` will be NULL if we need to be called again,
      52             :  *                and non-NULL if we are done.
      53             :  *    #GNUNET_NO is request incomplete or invalid
      54             :  *               (error message was generated)
      55             :  *    #GNUNET_SYSERR on internal error
      56             :  *               (we could not even queue an error message,
      57             :  *                close HTTP session with MHD_NO)
      58             :  */
      59             : enum GNUNET_GenericReturnValue
      60         576 : TALER_MHD_parse_post_json (struct MHD_Connection *connection,
      61             :                            void **con_cls,
      62             :                            const char *upload_data,
      63             :                            size_t *upload_data_size,
      64             :                            json_t **json)
      65             : {
      66             :   enum GNUNET_JSON_PostResult pr;
      67             : 
      68         576 :   pr = GNUNET_JSON_post_parser (REQUEST_BUFFER_MAX,
      69             :                                 connection,
      70             :                                 con_cls,
      71             :                                 upload_data,
      72             :                                 upload_data_size,
      73             :                                 json);
      74         576 :   switch (pr)
      75             :   {
      76           0 :   case GNUNET_JSON_PR_OUT_OF_MEMORY:
      77           0 :     GNUNET_break (NULL == *json);
      78             :     return (MHD_NO ==
      79           0 :             TALER_MHD_reply_with_error (
      80             :               connection,
      81             :               MHD_HTTP_INTERNAL_SERVER_ERROR,
      82             :               TALER_EC_GENERIC_PARSER_OUT_OF_MEMORY,
      83           0 :               NULL)) ? GNUNET_SYSERR : GNUNET_NO;
      84             : 
      85         384 :   case GNUNET_JSON_PR_CONTINUE:
      86         384 :     GNUNET_break (NULL == *json);
      87         384 :     return GNUNET_YES;
      88           0 :   case GNUNET_JSON_PR_REQUEST_TOO_LARGE:
      89           0 :     GNUNET_break (NULL == *json);
      90             :     return (MHD_NO ==
      91           0 :             TALER_MHD_reply_request_too_large
      92           0 :               (connection)) ? GNUNET_SYSERR : GNUNET_NO;
      93           0 :   case GNUNET_JSON_PR_JSON_INVALID:
      94           0 :     GNUNET_break (NULL == *json);
      95             :     return (MHD_YES ==
      96           0 :             TALER_MHD_reply_with_error (connection,
      97             :                                         MHD_HTTP_BAD_REQUEST,
      98             :                                         TALER_EC_GENERIC_JSON_INVALID,
      99             :                                         NULL))
     100           0 :            ? GNUNET_NO : GNUNET_SYSERR;
     101         192 :   case GNUNET_JSON_PR_SUCCESS:
     102         192 :     GNUNET_break (NULL != *json);
     103         192 :     return GNUNET_YES;
     104             :   }
     105             :   /* this should never happen */
     106           0 :   GNUNET_break (0);
     107           0 :   return GNUNET_SYSERR;
     108             : }
     109             : 
     110             : 
     111             : /**
     112             :  * Function called whenever we are done with a request
     113             :  * to clean up our state.
     114             :  *
     115             :  * @param con_cls value as it was left by
     116             :  *        #TALER_MHD_parse_post_json(), to be cleaned up
     117             :  */
     118             : void
     119         287 : TALER_MHD_parse_post_cleanup_callback (void *con_cls)
     120             : {
     121         287 :   GNUNET_JSON_post_parser_cleanup (con_cls);
     122         287 : }
     123             : 
     124             : 
     125             : /**
     126             :  * Extract base32crockford encoded data from request.
     127             :  *
     128             :  * Queues an error response to the connection if the parameter is
     129             :  * missing or invalid.
     130             :  *
     131             :  * @param connection the MHD connection
     132             :  * @param param_name the name of the parameter with the key
     133             :  * @param[out] out_data pointer to store the result
     134             :  * @param out_size expected size of data
     135             :  * @return
     136             :  *   #GNUNET_YES if the the argument is present
     137             :  *   #GNUNET_NO if the argument is absent or malformed
     138             :  *   #GNUNET_SYSERR on internal error (error response could not be sent)
     139             :  */
     140             : int
     141           3 : TALER_MHD_parse_request_arg_data (struct MHD_Connection *connection,
     142             :                                   const char *param_name,
     143             :                                   void *out_data,
     144             :                                   size_t out_size)
     145             : {
     146             :   const char *str;
     147             : 
     148           3 :   str = MHD_lookup_connection_value (connection,
     149             :                                      MHD_GET_ARGUMENT_KIND,
     150             :                                      param_name);
     151           3 :   if (NULL == str)
     152             :   {
     153             :     return (MHD_NO ==
     154           0 :             TALER_MHD_reply_with_error (connection,
     155             :                                         MHD_HTTP_BAD_REQUEST,
     156             :                                         TALER_EC_GENERIC_PARAMETER_MISSING,
     157             :                                         param_name))
     158           0 :            ? GNUNET_SYSERR : GNUNET_NO;
     159             :   }
     160           3 :   if (GNUNET_OK !=
     161           3 :       GNUNET_STRINGS_string_to_data (str,
     162             :                                      strlen (str),
     163             :                                      out_data,
     164             :                                      out_size))
     165             :     return (MHD_NO ==
     166           0 :             TALER_MHD_reply_with_error (connection,
     167             :                                         MHD_HTTP_BAD_REQUEST,
     168             :                                         TALER_EC_GENERIC_PARAMETER_MALFORMED,
     169             :                                         param_name))
     170           0 :            ? GNUNET_SYSERR : GNUNET_NO;
     171           3 :   return GNUNET_OK;
     172             : }
     173             : 
     174             : 
     175             : /**
     176             :  * Parse JSON object into components based on the given field
     177             :  * specification.  Generates error response on parse errors.
     178             :  *
     179             :  * @param connection the connection to send an error response to
     180             :  * @param root the JSON node to start the navigation at.
     181             :  * @param[in,out] spec field specification for the parser
     182             :  * @return
     183             :  *    #GNUNET_YES if navigation was successful (caller is responsible
     184             :  *                for freeing allocated variable-size data using
     185             :  *                GNUNET_JSON_parse_free() when done)
     186             :  *    #GNUNET_NO if json is malformed, error response was generated
     187             :  *    #GNUNET_SYSERR on internal error
     188             :  */
     189             : enum GNUNET_GenericReturnValue
     190         390 : TALER_MHD_parse_json_data (struct MHD_Connection *connection,
     191             :                            const json_t *root,
     192             :                            struct GNUNET_JSON_Specification *spec)
     193             : {
     194             :   enum GNUNET_GenericReturnValue ret;
     195             :   const char *error_json_name;
     196             :   unsigned int error_line;
     197             : 
     198         390 :   ret = GNUNET_JSON_parse (root,
     199             :                            spec,
     200             :                            &error_json_name,
     201             :                            &error_line);
     202         390 :   if (GNUNET_SYSERR == ret)
     203             :   {
     204           1 :     if (NULL == error_json_name)
     205           0 :       error_json_name = "<no field>";
     206           1 :     ret = (MHD_YES ==
     207           1 :            TALER_MHD_REPLY_JSON_PACK (
     208             :              connection,
     209             :              MHD_HTTP_BAD_REQUEST,
     210             :              GNUNET_JSON_pack_string ("hint",
     211             :                                       TALER_ErrorCode_get_hint (
     212             :                                         TALER_EC_GENERIC_JSON_INVALID)),
     213             :              GNUNET_JSON_pack_uint64 ("code",
     214             :                                       TALER_EC_GENERIC_JSON_INVALID),
     215             :              GNUNET_JSON_pack_string ("field",
     216             :                                       error_json_name),
     217             :              GNUNET_JSON_pack_uint64 ("line",
     218             :                                       error_line)))
     219           1 :           ? GNUNET_NO : GNUNET_SYSERR;
     220           1 :     return ret;
     221             :   }
     222         389 :   return GNUNET_YES;
     223             : }
     224             : 
     225             : 
     226             : /**
     227             :  * Parse JSON object that we (the server!) generated into components based on
     228             :  * the given field specification.  The difference to
     229             :  * #TALER_MHD_parse_json_data() is that this function will fail
     230             :  * with an HTTP failure of 500 (internal server error) in case
     231             :  * parsing fails, instead of blaming it on the client with a
     232             :  * 400 (#MHD_HTTP_BAD_REQUEST).
     233             :  *
     234             :  * @param connection the connection to send an error response to
     235             :  * @param root the JSON node to start the navigation at.
     236             :  * @param spec field specification for the parser
     237             :  * @return
     238             :  *    #GNUNET_YES if navigation was successful (caller is responsible
     239             :  *                for freeing allocated variable-size data using
     240             :  *                GNUNET_JSON_parse_free() when done)
     241             :  *    #GNUNET_NO if json is malformed, error response was generated
     242             :  *    #GNUNET_SYSERR on internal error
     243             :  */
     244             : enum GNUNET_GenericReturnValue
     245           0 : TALER_MHD_parse_internal_json_data (struct MHD_Connection *connection,
     246             :                                     const json_t *root,
     247             :                                     struct GNUNET_JSON_Specification *spec)
     248             : {
     249             :   enum GNUNET_GenericReturnValue ret;
     250             :   const char *error_json_name;
     251             :   unsigned int error_line;
     252             : 
     253           0 :   ret = GNUNET_JSON_parse (root,
     254             :                            spec,
     255             :                            &error_json_name,
     256             :                            &error_line);
     257           0 :   if (GNUNET_SYSERR == ret)
     258             :   {
     259           0 :     if (NULL == error_json_name)
     260           0 :       error_json_name = "<no field>";
     261           0 :     ret = (MHD_YES ==
     262           0 :            TALER_MHD_REPLY_JSON_PACK (
     263             :              connection,
     264             :              MHD_HTTP_INTERNAL_SERVER_ERROR,
     265             :              GNUNET_JSON_pack_string ("hint",
     266             :                                       TALER_ErrorCode_get_hint (
     267             :                                         TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE)),
     268             :              GNUNET_JSON_pack_uint64 ("code",
     269             :                                       TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE),
     270             :              GNUNET_JSON_pack_string ("field",
     271             :                                       error_json_name),
     272             :              GNUNET_JSON_pack_uint64 ("line",
     273             :                                       error_line)))
     274           0 :           ? GNUNET_NO : GNUNET_SYSERR;
     275           0 :     return ret;
     276             :   }
     277           0 :   return GNUNET_YES;
     278             : }
     279             : 
     280             : 
     281             : /**
     282             :  * Parse JSON array into components based on the given field
     283             :  * specification.  Generates error response on parse errors.
     284             :  *
     285             :  * @param connection the connection to send an error response to
     286             :  * @param root the JSON node to start the navigation at.
     287             :  * @param[in,out] spec field specification for the parser
     288             :  * @param ... -1-terminated list of array offsets of type 'int'
     289             :  * @return
     290             :  *    #GNUNET_YES if navigation was successful (caller is responsible
     291             :  *                for freeing allocated variable-size data using
     292             :  *                GNUNET_JSON_parse_free() when done)
     293             :  *    #GNUNET_NO if json is malformed, error response was generated
     294             :  *    #GNUNET_SYSERR on internal error
     295             :  */
     296             : enum GNUNET_GenericReturnValue
     297          85 : TALER_MHD_parse_json_array (struct MHD_Connection *connection,
     298             :                             const json_t *root,
     299             :                             struct GNUNET_JSON_Specification *spec,
     300             :                             ...)
     301             : {
     302             :   enum GNUNET_GenericReturnValue ret;
     303             :   const char *error_json_name;
     304             :   unsigned int error_line;
     305             :   va_list ap;
     306             :   json_int_t dim;
     307             : 
     308          85 :   va_start (ap, spec);
     309          85 :   dim = 0;
     310         170 :   while ( (-1 != (ret = va_arg (ap, int))) &&
     311             :           (NULL != root) )
     312             :   {
     313          85 :     dim++;
     314          85 :     root = json_array_get (root, ret);
     315             :   }
     316          85 :   va_end (ap);
     317          85 :   if (NULL == root)
     318             :   {
     319           0 :     ret = (MHD_YES ==
     320           0 :            TALER_MHD_REPLY_JSON_PACK (
     321             :              connection,
     322             :              MHD_HTTP_BAD_REQUEST,
     323             :              GNUNET_JSON_pack_string ("hint",
     324             :                                       TALER_ErrorCode_get_hint (
     325             :                                         TALER_EC_GENERIC_JSON_INVALID)),
     326             :              GNUNET_JSON_pack_uint64 ("code",
     327             :                                       TALER_EC_GENERIC_JSON_INVALID),
     328             :              GNUNET_JSON_pack_string ("detail",
     329             :                                       "expected array"),
     330             :              GNUNET_JSON_pack_uint64 ("dimension",
     331             :                                       dim)))
     332           0 :           ? GNUNET_NO : GNUNET_SYSERR;
     333           0 :     return ret;
     334             :   }
     335          85 :   ret = GNUNET_JSON_parse (root,
     336             :                            spec,
     337             :                            &error_json_name,
     338             :                            &error_line);
     339          85 :   if (GNUNET_SYSERR == ret)
     340             :   {
     341           0 :     if (NULL == error_json_name)
     342           0 :       error_json_name = "<no field>";
     343           0 :     ret = (MHD_YES ==
     344           0 :            TALER_MHD_REPLY_JSON_PACK (
     345             :              connection,
     346             :              MHD_HTTP_BAD_REQUEST,
     347             :              GNUNET_JSON_pack_string ("detail",
     348             :                                       error_json_name),
     349             :              GNUNET_JSON_pack_string ("hint",
     350             :                                       TALER_ErrorCode_get_hint (
     351             :                                         TALER_EC_GENERIC_JSON_INVALID)),
     352             :              GNUNET_JSON_pack_uint64 ("code",
     353             :                                       TALER_EC_GENERIC_JSON_INVALID),
     354             :              GNUNET_JSON_pack_uint64 ("line",
     355             :                                       error_line)))
     356           0 :           ? GNUNET_NO : GNUNET_SYSERR;
     357           0 :     return ret;
     358             :   }
     359          85 :   return GNUNET_YES;
     360             : }
     361             : 
     362             : 
     363             : /* end of mhd_parsing.c */

Generated by: LCOV version 1.14