1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
This file is part of TALER
(C) 2020 Taler Systems SA
TALER is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation; either version 3,
or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with TALER; see the file COPYING. If not,
see <http://www.gnu.org/licenses/>
*/
/**
* @file taler-merchant-httpd_private-patch-orders-ID-forget.c
* @brief implementing PATCH /orders/$ORDER_ID/forget request handling
* @author Jonathan Buchanan
*/
#include "platform.h"
#include "taler-merchant-httpd_private-patch-instances-ID.h"
#include <taler/taler_json_lib.h>
/**
* How often do we retry the UPDATE database transaction?
*/
#define MAX_RETRIES 3
/**
* Forget part of the contract terms.
*
* @param cls pointer to the result of the forget operation.
* @param object_id name of the object to forget.
* @param parent parent of the object at @e object_id.
*/
static void
forget (void *cls,
const char *object_id,
json_t *parent)
{
int *res = cls;
if (GNUNET_OK !=
TALER_JSON_contract_part_forget (parent,
object_id))
*res = GNUNET_SYSERR;
}
/**
* Forget fields of an order's contract terms.
*
* @param rh context of the handler
* @param connection the MHD connection to handle
* @param[in,out] hc context with further information about the request
* @return MHD result code
*/
MHD_RESULT
TMH_private_patch_orders_ID_forget (const struct TMH_RequestHandler *rh,
struct MHD_Connection *connection,
struct TMH_HandlerContext *hc)
{
const char *order_id = hc->infix;
enum GNUNET_DB_QueryStatus qs;
uint64_t order_serial;
for (unsigned int i = 0; i<MAX_RETRIES; i++)
{
json_t *fields;
json_t *contract_terms;
if (GNUNET_OK !=
TMH_db->start (TMH_db->cls,
"forget order"))
{
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_START_FAILED,
NULL);
}
qs = TMH_db->lookup_contract_terms (TMH_db->cls,
hc->instance->settings.id,
order_id,
&contract_terms,
&order_serial);
switch (qs)
{
case GNUNET_DB_STATUS_HARD_ERROR:
TMH_db->rollback (TMH_db->cls);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_FETCH_FAILED,
"contract terms");
case GNUNET_DB_STATUS_SOFT_ERROR:
TMH_db->rollback (TMH_db->cls);
continue;
case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
TMH_db->rollback (TMH_db->cls);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_NOT_FOUND,
TALER_EC_MERCHANT_GENERIC_ORDER_UNKNOWN,
order_id);
case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
GNUNET_assert (NULL != contract_terms);
break;
}
{
struct GNUNET_JSON_Specification spec[] = {
GNUNET_JSON_spec_json ("fields",
&fields),
GNUNET_JSON_spec_end ()
};
enum GNUNET_GenericReturnValue res;
res = TALER_MHD_parse_json_data (connection,
hc->request_body,
spec);
if (GNUNET_OK != res)
{
TMH_db->rollback (TMH_db->cls);
json_decref (contract_terms);
return (GNUNET_NO == res)
? MHD_YES
: MHD_NO;
}
}
if (! (json_is_array (fields)))
{
TMH_db->rollback (TMH_db->cls);
json_decref (contract_terms);
json_decref (fields);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_GENERIC_PARAMETER_MALFORMED,
"fields");
}
{
size_t index;
json_t *value;
json_array_foreach (fields, index, value) {
int forget_status = GNUNET_OK;
int expand_status;
if (! (json_is_string (value)))
{
TMH_db->rollback (TMH_db->cls);
json_decref (contract_terms);
json_decref (fields);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_MERCHANT_PRIVATE_PATCH_ORDERS_ID_FORGET_PATH_SYNTAX_INCORRECT,
"field is not a string");
}
expand_status = TALER_JSON_expand_path (contract_terms,
json_string_value (value),
&forget,
&forget_status);
if (GNUNET_SYSERR == forget_status)
{
/* We tried to forget a field that isn't forgettable */
TMH_db->rollback (TMH_db->cls);
json_decref (contract_terms);
json_decref (fields);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_CONFLICT,
TALER_EC_MERCHANT_PRIVATE_PATCH_ORDERS_ID_FORGET_PATH_NOT_FORGETTABLE,
json_string_value (value));
}
if (GNUNET_SYSERR == expand_status)
{
/* One of the paths was malformed and couldn't be expanded */
TMH_db->rollback (TMH_db->cls);
json_decref (contract_terms);
json_decref (fields);
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_MERCHANT_PRIVATE_PATCH_ORDERS_ID_FORGET_PATH_SYNTAX_INCORRECT,
json_string_value (value));
}
}
}
qs = TMH_db->update_contract_terms (TMH_db->cls,
hc->instance->settings.id,
order_id,
contract_terms);
json_decref (contract_terms);
json_decref (fields);
if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
{
TMH_db->rollback (TMH_db->cls);
if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
break;
}
else
{
qs = TMH_db->commit (TMH_db->cls);
if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
break;
}
}
if (0 > qs)
{
return TALER_MHD_reply_with_error (connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
TALER_EC_GENERIC_DB_COMMIT_FAILED,
NULL);
}
return TALER_MHD_reply_static (connection,
MHD_HTTP_OK,
NULL,
NULL,
0);
}
|