Line data Source code
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 173 : 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 *start;
40 : const char *delta;
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 173 : start = MHD_lookup_connection_value (connection,
48 : MHD_GET_ARGUMENT_KIND,
49 : "start");
50 173 : ha->have_start = (NULL != start);
51 173 : delta = MHD_lookup_connection_value (connection,
52 : MHD_GET_ARGUMENT_KIND,
53 : "delta");
54 173 : long_poll_ms = MHD_lookup_connection_value (connection,
55 : MHD_GET_ARGUMENT_KIND,
56 : "long_poll_ms");
57 173 : lp_timeout = 0;
58 173 : if ( (NULL == delta) ||
59 173 : (1 != sscanf (delta,
60 : "%lld%c",
61 : &d,
62 : &dummy)) )
63 : {
64 : /* Fail if one of the above failed. */
65 : /* Invalid request, given that this is fakebank we impolitely
66 : * just kill the connection instead of returning a nice error.
67 : */
68 0 : GNUNET_break_op (0);
69 : return (MHD_YES ==
70 0 : TALER_MHD_reply_with_error (connection,
71 : MHD_HTTP_BAD_REQUEST,
72 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
73 : "delta"))
74 : ? GNUNET_NO
75 0 : : GNUNET_SYSERR;
76 : }
77 173 : if ( (NULL != long_poll_ms) &&
78 0 : (1 != sscanf (long_poll_ms,
79 : "%llu%c",
80 : &lp_timeout,
81 : &dummy)) )
82 : {
83 : /* Fail if one of the above failed. */
84 : /* Invalid request, given that this is fakebank we impolitely
85 : * just kill the connection instead of returning a nice error.
86 : */
87 0 : GNUNET_break_op (0);
88 : return (MHD_YES ==
89 0 : TALER_MHD_reply_with_error (connection,
90 : MHD_HTTP_BAD_REQUEST,
91 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
92 : "long_poll_ms"))
93 : ? GNUNET_NO
94 0 : : GNUNET_SYSERR;
95 : }
96 173 : if ( (NULL != start) &&
97 156 : (1 != sscanf (start,
98 : "%llu%c",
99 : &sval,
100 : &dummy)) )
101 : {
102 : /* Fail if one of the above failed. */
103 : /* Invalid request, given that this is fakebank we impolitely
104 : * just kill the connection instead of returning a nice error.
105 : */
106 0 : GNUNET_break_op (0);
107 : return (MHD_YES ==
108 0 : TALER_MHD_reply_with_error (connection,
109 : MHD_HTTP_BAD_REQUEST,
110 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
111 : "start"))
112 : ? GNUNET_NO
113 0 : : GNUNET_SYSERR;
114 : }
115 173 : if (NULL == start)
116 17 : ha->start_idx = (d > 0) ? 0 : UINT64_MAX;
117 : else
118 156 : ha->start_idx = (uint64_t) sval;
119 173 : ha->delta = (int64_t) d;
120 173 : if (0 == ha->delta)
121 : {
122 0 : GNUNET_break_op (0);
123 : return (MHD_YES ==
124 0 : TALER_MHD_reply_with_error (connection,
125 : MHD_HTTP_BAD_REQUEST,
126 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
127 : "delta"))
128 : ? GNUNET_NO
129 0 : : GNUNET_SYSERR;
130 : }
131 : ha->lp_timeout
132 173 : = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
133 : lp_timeout);
134 173 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
135 : "Request for %lld records from %llu\n",
136 : (long long) ha->delta,
137 : (unsigned long long) ha->start_idx);
138 173 : return GNUNET_OK;
139 : }
|