LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_parsing.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 27 58 46.6 %
Date: 2017-11-25 11:31:41 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
       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             : /**
      18             :  * @file taler-exchange-httpd_parsing.c
      19             :  * @brief functions to parse incoming requests (MHD arguments and JSON snippets)
      20             :  * @author Florian Dold
      21             :  * @author Benedikt Mueller
      22             :  * @author Christian Grothoff
      23             :  */
      24             : 
      25             : #include "platform.h"
      26             : #include <gnunet/gnunet_util_lib.h>
      27             : #include <gnunet/gnunet_json_lib.h>
      28             : #include "taler_json_lib.h"
      29             : #include "taler-exchange-httpd_parsing.h"
      30             : #include "taler-exchange-httpd_responses.h"
      31             : 
      32             : 
      33             : /**
      34             :  * Maximum POST request size.
      35             :  */
      36             : #define REQUEST_BUFFER_MAX (1024*1024)
      37             : 
      38             : 
      39             : 
      40             : /**
      41             :  * Process a POST request containing a JSON object.  This function
      42             :  * realizes an MHD POST processor that will (incrementally) process
      43             :  * JSON data uploaded to the HTTP server.  It will store the required
      44             :  * state in the @a con_cls, which must be cleaned up using
      45             :  * #TEH_PARSE_post_cleanup_callback().
      46             :  *
      47             :  * @param connection the MHD connection
      48             :  * @param con_cls the closure (points to a `struct Buffer *`)
      49             :  * @param upload_data the POST data
      50             :  * @param upload_data_size number of bytes in @a upload_data
      51             :  * @param json the JSON object for a completed request
      52             :  * @return
      53             :  *    #GNUNET_YES if json object was parsed or at least
      54             :  *               may be parsed in the future (call again);
      55             :  *               `*json` will be NULL if we need to be called again,
      56             :  *                and non-NULL if we are done.
      57             :  *    #GNUNET_NO is request incomplete or invalid
      58             :  *               (error message was generated)
      59             :  *    #GNUNET_SYSERR on internal error
      60             :  *               (we could not even queue an error message,
      61             :  *                close HTTP session with MHD_NO)
      62             :  */
      63             : int
      64         116 : TEH_PARSE_post_json (struct MHD_Connection *connection,
      65             :                      void **con_cls,
      66             :                      const char *upload_data,
      67             :                      size_t *upload_data_size,
      68             :                      json_t **json)
      69             : {
      70             :   enum GNUNET_JSON_PostResult pr;
      71             : 
      72         116 :   pr = GNUNET_JSON_post_parser (REQUEST_BUFFER_MAX,
      73             :                                 con_cls,
      74             :                                 upload_data,
      75             :                                 upload_data_size,
      76             :                                 json);
      77         116 :   switch (pr)
      78             :   {
      79             :   case GNUNET_JSON_PR_OUT_OF_MEMORY:
      80             :     return (MHD_NO ==
      81           0 :             TEH_RESPONSE_reply_internal_error (connection,
      82             :                                                TALER_EC_PARSER_OUT_OF_MEMORY,
      83             :                                                "out of memory"))
      84           0 :       ? GNUNET_SYSERR : GNUNET_NO;
      85             :   case GNUNET_JSON_PR_CONTINUE:
      86          78 :     return GNUNET_YES;
      87             :   case GNUNET_JSON_PR_REQUEST_TOO_LARGE:
      88             :     return (MHD_NO ==
      89           0 :             TEH_RESPONSE_reply_request_too_large (connection))
      90           0 :       ? GNUNET_SYSERR : GNUNET_NO;
      91             :   case GNUNET_JSON_PR_JSON_INVALID:
      92             :     return (MHD_YES ==
      93           0 :             TEH_RESPONSE_reply_invalid_json (connection))
      94           0 :       ? GNUNET_NO : GNUNET_SYSERR;
      95             :   case GNUNET_JSON_PR_SUCCESS:
      96          38 :     GNUNET_break (NULL != *json);
      97          38 :     return GNUNET_YES;
      98             :   }
      99             :   /* this should never happen */
     100           0 :   GNUNET_break (0);
     101           0 :   return GNUNET_SYSERR;
     102             : }
     103             : 
     104             : 
     105             : /**
     106             :  * Function called whenever we are done with a request
     107             :  * to clean up our state.
     108             :  *
     109             :  * @param con_cls value as it was left by
     110             :  *        #TEH_PARSE_post_json(), to be cleaned up
     111             :  */
     112             : void
     113           0 : TEH_PARSE_post_cleanup_callback (void *con_cls)
     114             : {
     115           0 :   GNUNET_JSON_post_parser_cleanup (con_cls);
     116           0 : }
     117             : 
     118             : 
     119             : /**
     120             :  * Extract base32crockford encoded data from request.
     121             :  *
     122             :  * Queues an error response to the connection if the parameter is
     123             :  * missing or invalid.
     124             :  *
     125             :  * @param connection the MHD connection
     126             :  * @param param_name the name of the parameter with the key
     127             :  * @param[out] out_data pointer to store the result
     128             :  * @param out_size expected size of data
     129             :  * @return
     130             :  *   #GNUNET_YES if the the argument is present
     131             :  *   #GNUNET_NO if the argument is absent or malformed
     132             :  *   #GNUNET_SYSERR on internal error (error response could not be sent)
     133             :  */
     134             : int
     135           6 : TEH_PARSE_mhd_request_arg_data (struct MHD_Connection *connection,
     136             :                                 const char *param_name,
     137             :                                 void *out_data,
     138             :                                 size_t out_size)
     139             : {
     140             :   const char *str;
     141             : 
     142           6 :   str = MHD_lookup_connection_value (connection,
     143             :                                      MHD_GET_ARGUMENT_KIND,
     144             :                                      param_name);
     145           6 :   if (NULL == str)
     146             :   {
     147             :     return (MHD_NO ==
     148           0 :             TEH_RESPONSE_reply_arg_missing (connection,
     149             :                                             TALER_EC_PARAMETER_MISSING,
     150             :                                             param_name))
     151           0 :       ? GNUNET_SYSERR : GNUNET_NO;
     152             :   }
     153           6 :   if (GNUNET_OK !=
     154           6 :       GNUNET_STRINGS_string_to_data (str,
     155             :                                      strlen (str),
     156             :                                      out_data,
     157             :                                      out_size))
     158             :     return (MHD_NO ==
     159           0 :             TEH_RESPONSE_reply_arg_invalid (connection,
     160             :                                             TALER_EC_PARAMETER_MALFORMED,
     161             :                                             param_name))
     162           0 :       ? GNUNET_SYSERR : GNUNET_NO;
     163           6 :   return GNUNET_OK;
     164             : }
     165             : 
     166             : 
     167             : /**
     168             :  * Parse JSON object into components based on the given field
     169             :  * specification.  Generates error response on parse errors.
     170             :  *
     171             :  * @param connection the connection to send an error response to
     172             :  * @param root the JSON node to start the navigation at.
     173             :  * @param[in,out] spec field specification for the parser
     174             :  * @return
     175             :  *    #GNUNET_YES if navigation was successful (caller is responsible
     176             :  *                for freeing allocated variable-size data using
     177             :  *                GNUNET_JSON_parse_free() when done)
     178             :  *    #GNUNET_NO if json is malformed, error response was generated
     179             :  *    #GNUNET_SYSERR on internal error
     180             :  */
     181             : int
     182          40 : TEH_PARSE_json_data (struct MHD_Connection *connection,
     183             :                      const json_t *root,
     184             :                      struct GNUNET_JSON_Specification *spec)
     185             : {
     186             :   int ret;
     187             :   const char *error_json_name;
     188             :   unsigned int error_line;
     189             : 
     190          40 :   ret = GNUNET_JSON_parse (root,
     191             :                            spec,
     192             :                            &error_json_name,
     193             :                            &error_line);
     194          40 :   if (GNUNET_SYSERR == ret)
     195             :   {
     196           0 :     if (NULL == error_json_name)
     197           0 :       error_json_name = "<no field>";
     198           0 :     ret = (MHD_YES ==
     199           0 :            TEH_RESPONSE_reply_json_pack (connection,
     200             :                                          MHD_HTTP_BAD_REQUEST,
     201             :                                          "{s:s, s:I, s:s, s:I}",
     202             :                                          "error", "parse error",
     203             :                                          "code", (json_int_t) TALER_EC_JSON_INVALID_WITH_DETAILS,
     204             :                                          "field", error_json_name,
     205             :                                          "line", (json_int_t) error_line))
     206           0 :       ? GNUNET_NO : GNUNET_SYSERR;
     207           0 :     return ret;
     208             :   }
     209          40 :   return GNUNET_YES;
     210             : }
     211             : 
     212             : 
     213             : /**
     214             :  * Parse JSON array into components based on the given field
     215             :  * specification.  Generates error response on parse errors.
     216             :  *
     217             :  * @param connection the connection to send an error response to
     218             :  * @param root the JSON node to start the navigation at.
     219             :  * @param[in,out] spec field specification for the parser
     220             :  * @param ... -1-terminated list of array offsets of type 'int'
     221             :  * @return
     222             :  *    #GNUNET_YES if navigation was successful (caller is responsible
     223             :  *                for freeing allocated variable-size data using
     224             :  *                GNUNET_JSON_parse_free() when done)
     225             :  *    #GNUNET_NO if json is malformed, error response was generated
     226             :  *    #GNUNET_SYSERR on internal error
     227             :  */
     228             : int
     229         146 : TEH_PARSE_json_array (struct MHD_Connection *connection,
     230             :                       const json_t *root,
     231             :                       struct GNUNET_JSON_Specification *spec,
     232             :                       ...)
     233             : {
     234             :   int ret;
     235             :   const char *error_json_name;
     236             :   unsigned int error_line;
     237             :   va_list ap;
     238             :   json_int_t dim;
     239             : 
     240         146 :   va_start (ap, spec);
     241         146 :   dim = 0;
     242         540 :   while ( (-1 != (ret = va_arg (ap, int))) &&
     243             :           (NULL != root) )
     244             :   {
     245         248 :     dim++;
     246         248 :     root = json_array_get (root, ret);
     247             :   }
     248         146 :   va_end (ap);
     249         146 :   if (NULL == root)
     250             :   {
     251           0 :     ret = (MHD_YES ==
     252           0 :            TEH_RESPONSE_reply_json_pack (connection,
     253             :                                          MHD_HTTP_BAD_REQUEST,
     254             :                                          "{s:s, s:I}",
     255             :                                          "error", "parse error",
     256             :                                          "dimension", dim))
     257           0 :       ? GNUNET_NO : GNUNET_SYSERR;
     258           0 :     return ret;
     259             :   }
     260         146 :   ret = GNUNET_JSON_parse (root,
     261             :                            spec,
     262             :                            &error_json_name,
     263             :                            &error_line);
     264         146 :   if (GNUNET_SYSERR == ret)
     265             :   {
     266           0 :     if (NULL == error_json_name)
     267           0 :       error_json_name = "<no field>";
     268           0 :     ret = (MHD_YES ==
     269           0 :            TEH_RESPONSE_reply_json_pack (connection,
     270             :                                          MHD_HTTP_BAD_REQUEST,
     271             :                                          "{s:s, s:s, s:I}",
     272             :                                          "error", "parse error",
     273             :                                          "field", error_json_name,
     274             :                                          "line", (json_int_t) error_line))
     275           0 :       ? GNUNET_NO : GNUNET_SYSERR;
     276           0 :     return ret;
     277             :   }
     278         146 :   return GNUNET_YES;
     279             : }
     280             : 
     281             : 
     282             : /* end of taler-exchange-httpd_parsing.c */

Generated by: LCOV version 1.13