Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2018, 2020 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Lesser General Public License as published by the Free Software
7 : Foundation; either version 2.1, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 :
13 : You should have received a copy of the GNU Lesser General Public License along with
14 : TALER; see the file COPYING.LGPL. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file merchant_api_get_config.c
19 : * @brief Implementation of the /config request of the merchant's HTTP API
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <curl/curl.h>
24 : #include <jansson.h>
25 : #include <microhttpd.h> /* just for HTTP status codes */
26 : #include <gnunet/gnunet_util_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_merchant_service.h"
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 : #include <taler/taler_signatures.h>
32 :
33 : /**
34 : * Which version of the Taler protocol is implemented
35 : * by this library? Used to determine compatibility.
36 : */
37 : #define MERCHANT_PROTOCOL_CURRENT 3
38 :
39 : /**
40 : * How many configs are we backwards compatible with?
41 : */
42 : #define MERCHANT_PROTOCOL_AGE 1
43 :
44 :
45 : /**
46 : * @brief A handle for /config operations
47 : */
48 : struct TALER_MERCHANT_ConfigGetHandle
49 : {
50 : /**
51 : * The url for this request.
52 : */
53 : char *url;
54 :
55 : /**
56 : * Handle for the request.
57 : */
58 : struct GNUNET_CURL_Job *job;
59 :
60 : /**
61 : * Function to call with the result.
62 : */
63 : TALER_MERCHANT_ConfigCallback cb;
64 :
65 : /**
66 : * Closure for @a cb.
67 : */
68 : void *cb_cls;
69 :
70 : /**
71 : * Reference to the execution context.
72 : */
73 : struct GNUNET_CURL_Context *ctx;
74 :
75 : };
76 :
77 :
78 : /**
79 : * Function called when we're done processing the
80 : * HTTP /config request.
81 : *
82 : * @param cls the `struct TALER_MERCHANT_ConfigGetHandle`
83 : * @param response_code HTTP response code, 0 on error
84 : * @param response response body, NULL if not in JSON
85 : */
86 : static void
87 0 : handle_config_finished (void *cls,
88 : long response_code,
89 : const void *response)
90 : {
91 0 : struct TALER_MERCHANT_ConfigGetHandle *vgh = cls;
92 0 : const json_t *json = response;
93 0 : struct TALER_MERCHANT_HttpResponse hr = {
94 0 : .http_status = (unsigned int) response_code,
95 : .reply = json
96 : };
97 :
98 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 : "Got /config response with status code %u\n",
100 : (unsigned int) response_code);
101 :
102 0 : vgh->job = NULL;
103 0 : switch (response_code)
104 : {
105 0 : case MHD_HTTP_OK:
106 : {
107 : struct TALER_MERCHANT_ConfigInformation vi;
108 0 : enum TALER_MERCHANT_VersionCompatibility vc =
109 : TALER_MERCHANT_VC_PROTOCOL_ERROR;
110 : struct GNUNET_JSON_Specification spec[] = {
111 0 : GNUNET_JSON_spec_string ("currency",
112 : &vi.currency),
113 0 : GNUNET_JSON_spec_string ("version",
114 : &vi.version),
115 0 : GNUNET_JSON_spec_end ()
116 : };
117 :
118 0 : if (GNUNET_OK !=
119 0 : GNUNET_JSON_parse (json,
120 : spec,
121 : NULL, NULL))
122 : {
123 0 : hr.http_status = 0;
124 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
125 : }
126 : else
127 : {
128 : unsigned int age;
129 : unsigned int revision;
130 : unsigned int current;
131 :
132 0 : if (3 != sscanf (vi.version,
133 : "%u:%u:%u",
134 : ¤t,
135 : &revision,
136 : &age))
137 : {
138 0 : hr.http_status = 0;
139 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
140 : }
141 : else
142 : {
143 0 : vc = TALER_MERCHANT_VC_MATCH;
144 0 : if (MERCHANT_PROTOCOL_CURRENT < current)
145 : {
146 0 : vc |= TALER_MERCHANT_VC_NEWER;
147 0 : if (MERCHANT_PROTOCOL_CURRENT < current - age)
148 0 : vc |= TALER_MERCHANT_VC_INCOMPATIBLE;
149 : }
150 0 : if (MERCHANT_PROTOCOL_CURRENT > current)
151 : {
152 0 : vc |= TALER_MERCHANT_VC_OLDER;
153 0 : if (MERCHANT_PROTOCOL_CURRENT - MERCHANT_PROTOCOL_AGE > current)
154 0 : vc |= TALER_MERCHANT_VC_INCOMPATIBLE;
155 : }
156 : }
157 : }
158 0 : vgh->cb (vgh->cb_cls,
159 : &hr,
160 : &vi,
161 : vc);
162 0 : TALER_MERCHANT_config_get_cancel (vgh);
163 0 : return;
164 : }
165 0 : default:
166 : /* unexpected response code */
167 0 : hr.ec = TALER_JSON_get_error_code (json);
168 0 : hr.hint = TALER_JSON_get_error_hint (json);
169 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170 : "Unexpected response code %u/%d\n",
171 : (unsigned int) response_code,
172 : (int) hr.ec);
173 0 : vgh->cb (vgh->cb_cls,
174 : &hr,
175 : NULL,
176 : TALER_MERCHANT_VC_PROTOCOL_ERROR);
177 0 : break;
178 : }
179 0 : TALER_MERCHANT_config_get_cancel (vgh);
180 : }
181 :
182 :
183 : struct TALER_MERCHANT_ConfigGetHandle *
184 0 : TALER_MERCHANT_config_get (struct GNUNET_CURL_Context *ctx,
185 : const char *backend_url,
186 : TALER_MERCHANT_ConfigCallback config_cb,
187 : void *config_cb_cls)
188 : {
189 : struct TALER_MERCHANT_ConfigGetHandle *vgh;
190 : CURL *eh;
191 :
192 0 : vgh = GNUNET_new (struct TALER_MERCHANT_ConfigGetHandle);
193 0 : vgh->ctx = ctx;
194 0 : vgh->cb = config_cb;
195 0 : vgh->cb_cls = config_cb_cls;
196 0 : vgh->url = TALER_url_join (backend_url,
197 : "config",
198 : NULL);
199 0 : if (NULL == vgh->url)
200 : {
201 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 : "Could not construct request URL.\n");
203 0 : GNUNET_free (vgh);
204 0 : return NULL;
205 : }
206 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 : "Requesting URL '%s'\n",
208 : vgh->url);
209 0 : eh = TALER_MERCHANT_curl_easy_get_ (vgh->url);
210 0 : vgh->job = GNUNET_CURL_job_add (ctx,
211 : eh,
212 : &handle_config_finished,
213 : vgh);
214 0 : return vgh;
215 : }
216 :
217 :
218 : void
219 0 : TALER_MERCHANT_config_get_cancel (struct TALER_MERCHANT_ConfigGetHandle *vgh)
220 : {
221 0 : if (NULL != vgh->job)
222 : {
223 0 : GNUNET_CURL_job_cancel (vgh->job);
224 0 : vgh->job = NULL;
225 : }
226 0 : GNUNET_free (vgh->url);
227 0 : GNUNET_free (vgh);
228 0 : }
229 :
230 :
231 : /* end of merchant_api_config_get.c */
|