exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

fakebank_common_parser.c (5255B)


      1 /*
      2   This file is part of TALER
      3   (C) 2016-2023 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or
      6   modify it under the terms of the GNU General Public License
      7   as published by the Free Software Foundation; either version 3,
      8   or (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful,
     11   but WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with TALER; see the file COPYING.  If not,
     17   see <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file bank-lib/fakebank_common_parser.c
     21  * @brief functions to help parse REST requests
     22  * @author Christian Grothoff <christian@grothoff.org>
     23  */
     24 #include "taler/taler_fakebank_lib.h"
     25 #include "taler/taler_bank_service.h"
     26 #include "taler/taler_mhd_lib.h"
     27 #include <gnunet/gnunet_mhd_compat.h>
     28 #include "fakebank.h"
     29 #include "fakebank_common_parser.h"
     30 
     31 
     32 enum GNUNET_GenericReturnValue
     33 TALER_FAKEBANK_common_parse_history_args (
     34   const struct TALER_FAKEBANK_Handle *h,
     35   struct MHD_Connection *connection,
     36   struct HistoryArgs *ha)
     37 {
     38   const char *offset;
     39   const char *limit;
     40   const char *long_poll_ms;
     41   unsigned long long lp_timeout;
     42   unsigned long long sval;
     43   long long d;
     44   char dummy;
     45 
     46   offset = MHD_lookup_connection_value (connection,
     47                                         MHD_GET_ARGUMENT_KIND,
     48                                         "offset");
     49   if (NULL == offset)
     50     offset = MHD_lookup_connection_value (connection,
     51                                           MHD_GET_ARGUMENT_KIND,
     52                                           "start");
     53   ha->have_start = (NULL != offset);
     54   limit = MHD_lookup_connection_value (connection,
     55                                        MHD_GET_ARGUMENT_KIND,
     56                                        "limit");
     57   if (NULL == limit)
     58     limit = MHD_lookup_connection_value (connection,
     59                                          MHD_GET_ARGUMENT_KIND,
     60                                          "delta");
     61   long_poll_ms = MHD_lookup_connection_value (connection,
     62                                               MHD_GET_ARGUMENT_KIND,
     63                                               "long_poll_ms");
     64   lp_timeout = 0;
     65   if ( (NULL == limit) ||
     66        (1 != sscanf (limit,
     67                      "%lld%c",
     68                      &d,
     69                      &dummy)) )
     70   {
     71     /* Fail if one of the above failed.  */
     72     /* Invalid request, given that this is fakebank we impolitely
     73      * just kill the connection instead of returning a nice error.
     74      */
     75     GNUNET_break_op (0);
     76     return (MHD_YES ==
     77             TALER_MHD_reply_with_error (connection,
     78                                         MHD_HTTP_BAD_REQUEST,
     79                                         TALER_EC_GENERIC_PARAMETER_MALFORMED,
     80                                         "delta"))
     81            ? GNUNET_NO
     82            : GNUNET_SYSERR;
     83   }
     84   if ( (NULL != long_poll_ms) &&
     85        (1 != sscanf (long_poll_ms,
     86                      "%llu%c",
     87                      &lp_timeout,
     88                      &dummy)) )
     89   {
     90     /* Fail if one of the above failed.  */
     91     /* Invalid request, given that this is fakebank we impolitely
     92      * just kill the connection instead of returning a nice error.
     93      */
     94     GNUNET_break_op (0);
     95     return (MHD_YES ==
     96             TALER_MHD_reply_with_error (connection,
     97                                         MHD_HTTP_BAD_REQUEST,
     98                                         TALER_EC_GENERIC_PARAMETER_MALFORMED,
     99                                         "long_poll_ms"))
    100            ? GNUNET_NO
    101            : GNUNET_SYSERR;
    102   }
    103   if ( (NULL != offset) &&
    104        (1 != sscanf (offset,
    105                      "%llu%c",
    106                      &sval,
    107                      &dummy)) )
    108   {
    109     /* Fail if one of the above failed.  */
    110     /* Invalid request, given that this is fakebank we impolitely
    111      * just kill the connection instead of returning a nice error.
    112      */
    113     GNUNET_break_op (0);
    114     return (MHD_YES ==
    115             TALER_MHD_reply_with_error (connection,
    116                                         MHD_HTTP_BAD_REQUEST,
    117                                         TALER_EC_GENERIC_PARAMETER_MALFORMED,
    118                                         "start"))
    119            ? GNUNET_NO
    120            : GNUNET_SYSERR;
    121   }
    122   if (NULL == offset)
    123     ha->start_idx = (d > 0) ? 0 : UINT64_MAX;
    124   else
    125     ha->start_idx = (uint64_t) sval;
    126   ha->delta = (int64_t) d;
    127   if (0 == ha->delta)
    128   {
    129     GNUNET_break_op (0);
    130     return (MHD_YES ==
    131             TALER_MHD_reply_with_error (connection,
    132                                         MHD_HTTP_BAD_REQUEST,
    133                                         TALER_EC_GENERIC_PARAMETER_MALFORMED,
    134                                         "limit"))
    135            ? GNUNET_NO
    136            : GNUNET_SYSERR;
    137   }
    138   ha->lp_timeout
    139     = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
    140                                      lp_timeout);
    141   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    142               "Request for %lld records from %llu\n",
    143               (long long) ha->delta,
    144               (unsigned long long) ha->start_idx);
    145   return GNUNET_OK;
    146 }