fakebank_common_parser.c (5283B)
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/platform.h" 25 #include "taler/taler_fakebank_lib.h" 26 #include "taler/taler_bank_service.h" 27 #include "taler/taler_mhd_lib.h" 28 #include <gnunet/gnunet_mhd_compat.h> 29 #include "fakebank.h" 30 #include "fakebank_common_parser.h" 31 32 33 enum GNUNET_GenericReturnValue 34 TALER_FAKEBANK_common_parse_history_args ( 35 const struct TALER_FAKEBANK_Handle *h, 36 struct MHD_Connection *connection, 37 struct HistoryArgs *ha) 38 { 39 const char *offset; 40 const char *limit; 41 const char *long_poll_ms; 42 unsigned long long lp_timeout; 43 unsigned long long sval; 44 long long d; 45 char dummy; 46 47 offset = MHD_lookup_connection_value (connection, 48 MHD_GET_ARGUMENT_KIND, 49 "offset"); 50 if (NULL == offset) 51 offset = MHD_lookup_connection_value (connection, 52 MHD_GET_ARGUMENT_KIND, 53 "start"); 54 ha->have_start = (NULL != offset); 55 limit = MHD_lookup_connection_value (connection, 56 MHD_GET_ARGUMENT_KIND, 57 "limit"); 58 if (NULL == limit) 59 limit = MHD_lookup_connection_value (connection, 60 MHD_GET_ARGUMENT_KIND, 61 "delta"); 62 long_poll_ms = MHD_lookup_connection_value (connection, 63 MHD_GET_ARGUMENT_KIND, 64 "long_poll_ms"); 65 lp_timeout = 0; 66 if ( (NULL == limit) || 67 (1 != sscanf (limit, 68 "%lld%c", 69 &d, 70 &dummy)) ) 71 { 72 /* Fail if one of the above failed. */ 73 /* Invalid request, given that this is fakebank we impolitely 74 * just kill the connection instead of returning a nice error. 75 */ 76 GNUNET_break_op (0); 77 return (MHD_YES == 78 TALER_MHD_reply_with_error (connection, 79 MHD_HTTP_BAD_REQUEST, 80 TALER_EC_GENERIC_PARAMETER_MALFORMED, 81 "delta")) 82 ? GNUNET_NO 83 : GNUNET_SYSERR; 84 } 85 if ( (NULL != long_poll_ms) && 86 (1 != sscanf (long_poll_ms, 87 "%llu%c", 88 &lp_timeout, 89 &dummy)) ) 90 { 91 /* Fail if one of the above failed. */ 92 /* Invalid request, given that this is fakebank we impolitely 93 * just kill the connection instead of returning a nice error. 94 */ 95 GNUNET_break_op (0); 96 return (MHD_YES == 97 TALER_MHD_reply_with_error (connection, 98 MHD_HTTP_BAD_REQUEST, 99 TALER_EC_GENERIC_PARAMETER_MALFORMED, 100 "long_poll_ms")) 101 ? GNUNET_NO 102 : GNUNET_SYSERR; 103 } 104 if ( (NULL != offset) && 105 (1 != sscanf (offset, 106 "%llu%c", 107 &sval, 108 &dummy)) ) 109 { 110 /* Fail if one of the above failed. */ 111 /* Invalid request, given that this is fakebank we impolitely 112 * just kill the connection instead of returning a nice error. 113 */ 114 GNUNET_break_op (0); 115 return (MHD_YES == 116 TALER_MHD_reply_with_error (connection, 117 MHD_HTTP_BAD_REQUEST, 118 TALER_EC_GENERIC_PARAMETER_MALFORMED, 119 "start")) 120 ? GNUNET_NO 121 : GNUNET_SYSERR; 122 } 123 if (NULL == offset) 124 ha->start_idx = (d > 0) ? 0 : UINT64_MAX; 125 else 126 ha->start_idx = (uint64_t) sval; 127 ha->delta = (int64_t) d; 128 if (0 == ha->delta) 129 { 130 GNUNET_break_op (0); 131 return (MHD_YES == 132 TALER_MHD_reply_with_error (connection, 133 MHD_HTTP_BAD_REQUEST, 134 TALER_EC_GENERIC_PARAMETER_MALFORMED, 135 "limit")) 136 ? GNUNET_NO 137 : GNUNET_SYSERR; 138 } 139 ha->lp_timeout 140 = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 141 lp_timeout); 142 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 143 "Request for %lld records from %llu\n", 144 (long long) ha->delta, 145 (unsigned long long) ha->start_idx); 146 return GNUNET_OK; 147 }