Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as
7 : published by the Free Software Foundation; either version 2.1,
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 Lesser General Public License for more details.
14 :
15 : You should have received a copy of the GNU Lesser General
16 : Public License along with TALER; see the file COPYING.LGPL.
17 : If not, see <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file merchant_api_patch_instance.c
21 : * @brief Implementation of the PATCH /instances/$ID request
22 : * of the merchant's HTTP API
23 : * @author Christian Grothoff
24 : */
25 : #include "platform.h"
26 : #include <curl/curl.h>
27 : #include <jansson.h>
28 : #include <microhttpd.h> /* just for HTTP status codes */
29 : #include <gnunet/gnunet_util_lib.h>
30 : #include "taler_merchant_service.h"
31 : #include "merchant_api_curl_defaults.h"
32 : #include "merchant_api_common.h"
33 : #include <taler/taler_json_lib.h>
34 : #include <taler/taler_kyclogic_lib.h>
35 : #include <taler/taler_curl_lib.h>
36 :
37 :
38 : /**
39 : * Handle for a PATCH /instances/$ID operation.
40 : */
41 : struct TALER_MERCHANT_InstancePatchHandle
42 : {
43 :
44 : /**
45 : * The url for this request.
46 : */
47 : char *url;
48 :
49 : /**
50 : * Handle for the request.
51 : */
52 : struct GNUNET_CURL_Job *job;
53 :
54 : /**
55 : * Function to call with the result.
56 : */
57 : TALER_MERCHANT_InstancePatchCallback cb;
58 :
59 : /**
60 : * Closure for @a cb.
61 : */
62 : void *cb_cls;
63 :
64 : /**
65 : * Reference to the execution context.
66 : */
67 : struct GNUNET_CURL_Context *ctx;
68 :
69 : /**
70 : * Minor context that holds body and headers.
71 : */
72 : struct TALER_CURL_PostContext post_ctx;
73 :
74 : };
75 :
76 :
77 : /**
78 : * Function called when we're done processing the
79 : * HTTP PATCH /instances/$ID request.
80 : *
81 : * @param cls the `struct TALER_MERCHANT_InstancePatchHandle`
82 : * @param response_code HTTP response code, 0 on error
83 : * @param response response body, NULL if not in JSON
84 : */
85 : static void
86 4 : handle_patch_instance_finished (void *cls,
87 : long response_code,
88 : const void *response)
89 : {
90 4 : struct TALER_MERCHANT_InstancePatchHandle *iph = cls;
91 4 : const json_t *json = response;
92 4 : struct TALER_MERCHANT_HttpResponse hr = {
93 4 : .http_status = (unsigned int) response_code,
94 : .reply = json
95 : };
96 :
97 4 : iph->job = NULL;
98 4 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
99 : "PATCH /instances/$ID completed with response code %u\n",
100 : (unsigned int) response_code);
101 4 : switch (response_code)
102 : {
103 0 : case 0:
104 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
105 0 : break;
106 4 : case MHD_HTTP_NO_CONTENT:
107 4 : break;
108 0 : case MHD_HTTP_BAD_REQUEST:
109 0 : hr.ec = TALER_JSON_get_error_code (json);
110 0 : hr.hint = TALER_JSON_get_error_hint (json);
111 0 : break;
112 0 : case MHD_HTTP_UNAUTHORIZED:
113 0 : hr.ec = TALER_JSON_get_error_code (json);
114 0 : hr.hint = TALER_JSON_get_error_hint (json);
115 : /* Nothing really to verify, merchant says we need to authenticate. */
116 0 : break;
117 0 : case MHD_HTTP_FORBIDDEN:
118 0 : hr.ec = TALER_JSON_get_error_code (json);
119 0 : hr.hint = TALER_JSON_get_error_hint (json);
120 : /* Nothing really to verify, merchant says we tried to abort the payment
121 : * after it was successful. We should pass the JSON reply to the
122 : * application */
123 0 : break;
124 0 : case MHD_HTTP_NOT_FOUND:
125 0 : hr.ec = TALER_JSON_get_error_code (json);
126 0 : hr.hint = TALER_JSON_get_error_hint (json);
127 0 : break;
128 0 : case MHD_HTTP_CONFLICT:
129 0 : hr.ec = TALER_JSON_get_error_code (json);
130 0 : hr.hint = TALER_JSON_get_error_hint (json);
131 0 : break;
132 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
133 0 : hr.ec = TALER_JSON_get_error_code (json);
134 0 : hr.hint = TALER_JSON_get_error_hint (json);
135 : /* Server had an internal issue; we should retry,
136 : but this API leaves this to the application */
137 0 : break;
138 0 : default:
139 0 : TALER_MERCHANT_parse_error_details_ (json,
140 : response_code,
141 : &hr);
142 : /* unexpected response code */
143 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
144 : "Unexpected response code %u/%d\n",
145 : (unsigned int) response_code,
146 : (int) hr.ec);
147 0 : GNUNET_break_op (0);
148 0 : break;
149 : }
150 4 : iph->cb (iph->cb_cls,
151 : &hr);
152 4 : TALER_MERCHANT_instance_patch_cancel (iph);
153 4 : }
154 :
155 :
156 : struct TALER_MERCHANT_InstancePatchHandle *
157 4 : TALER_MERCHANT_instance_patch (
158 : struct GNUNET_CURL_Context *ctx,
159 : const char *backend_url,
160 : const char *instance_id,
161 : const char *name,
162 : const json_t *address,
163 : const json_t *jurisdiction,
164 : bool use_stefan,
165 : struct GNUNET_TIME_Relative default_wire_transfer_delay,
166 : struct GNUNET_TIME_Relative default_pay_delay,
167 : TALER_MERCHANT_InstancePatchCallback cb,
168 : void *cb_cls)
169 : {
170 : struct TALER_MERCHANT_InstancePatchHandle *iph;
171 : json_t *req_obj;
172 :
173 4 : req_obj = GNUNET_JSON_PACK (
174 : GNUNET_JSON_pack_string ("name",
175 : name),
176 : GNUNET_JSON_pack_object_incref ("address",
177 : (json_t *) address),
178 : GNUNET_JSON_pack_object_incref ("jurisdiction",
179 : (json_t *) jurisdiction),
180 : GNUNET_JSON_pack_bool ("use_stefan",
181 : use_stefan),
182 : GNUNET_JSON_pack_time_rel ("default_wire_transfer_delay",
183 : default_wire_transfer_delay),
184 : GNUNET_JSON_pack_time_rel ("default_pay_delay",
185 : default_pay_delay));
186 4 : iph = GNUNET_new (struct TALER_MERCHANT_InstancePatchHandle);
187 4 : iph->ctx = ctx;
188 4 : iph->cb = cb;
189 4 : iph->cb_cls = cb_cls;
190 4 : if (NULL != instance_id)
191 : {
192 : char *path;
193 :
194 4 : GNUNET_asprintf (&path,
195 : "management/instances/%s",
196 : instance_id);
197 4 : iph->url = TALER_url_join (backend_url,
198 : path,
199 : NULL);
200 4 : GNUNET_free (path);
201 : }
202 : else
203 : {
204 : /* backend_url is already identifying the instance */
205 0 : iph->url = TALER_url_join (backend_url,
206 : "private",
207 : NULL);
208 : }
209 4 : if (NULL == iph->url)
210 : {
211 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 : "Could not construct request URL.\n");
213 0 : json_decref (req_obj);
214 0 : GNUNET_free (iph);
215 0 : return NULL;
216 : }
217 : {
218 : CURL *eh;
219 :
220 4 : eh = TALER_MERCHANT_curl_easy_get_ (iph->url);
221 4 : if (GNUNET_OK !=
222 4 : TALER_curl_easy_post (&iph->post_ctx,
223 : eh,
224 : req_obj))
225 : {
226 0 : GNUNET_break (0);
227 0 : curl_easy_cleanup (eh);
228 0 : json_decref (req_obj);
229 0 : GNUNET_free (iph->url);
230 0 : GNUNET_free (iph);
231 0 : return NULL;
232 : }
233 4 : json_decref (req_obj);
234 4 : GNUNET_assert (CURLE_OK ==
235 : curl_easy_setopt (eh,
236 : CURLOPT_CUSTOMREQUEST,
237 : MHD_HTTP_METHOD_PATCH));
238 8 : iph->job = GNUNET_CURL_job_add2 (ctx,
239 : eh,
240 4 : iph->post_ctx.headers,
241 : &handle_patch_instance_finished,
242 : iph);
243 : }
244 4 : return iph;
245 : }
246 :
247 :
248 : void
249 4 : TALER_MERCHANT_instance_patch_cancel (
250 : struct TALER_MERCHANT_InstancePatchHandle *iph)
251 : {
252 4 : if (NULL != iph->job)
253 : {
254 0 : GNUNET_CURL_job_cancel (iph->job);
255 0 : iph->job = NULL;
256 : }
257 4 : TALER_curl_easy_post_finished (&iph->post_ctx);
258 4 : GNUNET_free (iph->url);
259 4 : GNUNET_free (iph);
260 4 : }
261 :
262 :
263 : /* end of merchant_api_patch_instance.c */
|