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 *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 173 : offset = MHD_lookup_connection_value (connection,
48 : MHD_GET_ARGUMENT_KIND,
49 : "offset");
50 173 : if (NULL == offset)
51 17 : offset = MHD_lookup_connection_value (connection,
52 : MHD_GET_ARGUMENT_KIND,
53 : "start");
54 173 : ha->have_start = (NULL != offset);
55 173 : limit = MHD_lookup_connection_value (connection,
56 : MHD_GET_ARGUMENT_KIND,
57 : "limit");
58 173 : if (NULL == limit)
59 0 : limit = MHD_lookup_connection_value (connection,
60 : MHD_GET_ARGUMENT_KIND,
61 : "delta");
62 173 : long_poll_ms = MHD_lookup_connection_value (connection,
63 : MHD_GET_ARGUMENT_KIND,
64 : "long_poll_ms");
65 173 : lp_timeout = 0;
66 173 : if ( (NULL == limit) ||
67 173 : (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 0 : GNUNET_break_op (0);
77 : return (MHD_YES ==
78 0 : TALER_MHD_reply_with_error (connection,
79 : MHD_HTTP_BAD_REQUEST,
80 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
81 : "delta"))
82 : ? GNUNET_NO
83 0 : : GNUNET_SYSERR;
84 : }
85 173 : if ( (NULL != long_poll_ms) &&
86 0 : (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 0 : GNUNET_break_op (0);
96 : return (MHD_YES ==
97 0 : 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 0 : : GNUNET_SYSERR;
103 : }
104 173 : if ( (NULL != offset) &&
105 156 : (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 0 : GNUNET_break_op (0);
115 : return (MHD_YES ==
116 0 : TALER_MHD_reply_with_error (connection,
117 : MHD_HTTP_BAD_REQUEST,
118 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
119 : "start"))
120 : ? GNUNET_NO
121 0 : : GNUNET_SYSERR;
122 : }
123 173 : if (NULL == offset)
124 17 : ha->start_idx = (d > 0) ? 0 : UINT64_MAX;
125 : else
126 156 : ha->start_idx = (uint64_t) sval;
127 173 : ha->delta = (int64_t) d;
128 173 : if (0 == ha->delta)
129 : {
130 0 : GNUNET_break_op (0);
131 : return (MHD_YES ==
132 0 : TALER_MHD_reply_with_error (connection,
133 : MHD_HTTP_BAD_REQUEST,
134 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
135 : "limit"))
136 : ? GNUNET_NO
137 0 : : GNUNET_SYSERR;
138 : }
139 : ha->lp_timeout
140 173 : = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
141 : lp_timeout);
142 173 : 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 173 : return GNUNET_OK;
147 : }
|