aboutsummaryrefslogtreecommitdiff
path: root/src/rest/plugin_rest_config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rest/plugin_rest_config.c')
-rw-r--r--src/rest/plugin_rest_config.c339
1 files changed, 170 insertions, 169 deletions
diff --git a/src/rest/plugin_rest_config.c b/src/rest/plugin_rest_config.c
index 7f1d0eedf..55797f543 100644
--- a/src/rest/plugin_rest_config.c
+++ b/src/rest/plugin_rest_config.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20/** 20/**
21 * @author Martin Schanzenbach 21 * @author Martin Schanzenbach
22 * @file gns/plugin_rest_config.c 22 * @file gns/plugin_rest_config.c
@@ -35,15 +35,13 @@
35/** 35/**
36 * @brief struct returned by the initialization function of the plugin 36 * @brief struct returned by the initialization function of the plugin
37 */ 37 */
38struct Plugin 38struct Plugin {
39{
40 const struct GNUNET_CONFIGURATION_Handle *cfg; 39 const struct GNUNET_CONFIGURATION_Handle *cfg;
41}; 40};
42 41
43const struct GNUNET_CONFIGURATION_Handle *cfg; 42const struct GNUNET_CONFIGURATION_Handle *cfg;
44 43
45struct RequestHandle 44struct RequestHandle {
46{
47 /** 45 /**
48 * Handle to rest request 46 * Handle to rest request
49 */ 47 */
@@ -77,12 +75,12 @@ struct RequestHandle
77 * @param handle Handle to clean up 75 * @param handle Handle to clean up
78 */ 76 */
79static void 77static void
80cleanup_handle (struct RequestHandle *handle) 78cleanup_handle(struct RequestHandle *handle)
81{ 79{
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n"); 80 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
83 if (NULL != handle->url) 81 if (NULL != handle->url)
84 GNUNET_free (handle->url); 82 GNUNET_free(handle->url);
85 GNUNET_free (handle); 83 GNUNET_free(handle);
86} 84}
87 85
88 86
@@ -93,45 +91,46 @@ cleanup_handle (struct RequestHandle *handle)
93 * @param tc scheduler context 91 * @param tc scheduler context
94 */ 92 */
95static void 93static void
96do_error (void *cls) 94do_error(void *cls)
97{ 95{
98 struct RequestHandle *handle = cls; 96 struct RequestHandle *handle = cls;
99 struct MHD_Response *resp; 97 struct MHD_Response *resp;
100 98
101 resp = GNUNET_REST_create_response (NULL); 99 resp = GNUNET_REST_create_response(NULL);
102 handle->proc (handle->proc_cls, resp, handle->response_code); 100 handle->proc(handle->proc_cls, resp, handle->response_code);
103 cleanup_handle (handle); 101 cleanup_handle(handle);
104} 102}
105 103
106 104
107static void 105static void
108add_sections (void *cls, 106add_sections(void *cls,
109 const char *section, 107 const char *section,
110 const char *option, 108 const char *option,
111 const char *value) 109 const char *value)
112{ 110{
113 json_t *sections_obj = cls; 111 json_t *sections_obj = cls;
114 json_t *sec_obj; 112 json_t *sec_obj;
115 113
116 sec_obj = json_object_get (sections_obj, section); 114 sec_obj = json_object_get(sections_obj, section);
117 if (NULL != sec_obj) 115 if (NULL != sec_obj)
118 { 116 {
119 json_object_set_new (sec_obj, option, json_string (value)); 117 json_object_set_new(sec_obj, option, json_string(value));
120 return; 118 return;
121 } 119 }
122 sec_obj = json_object (); 120 sec_obj = json_object();
123 json_object_set_new (sec_obj, option, json_string (value)); 121 json_object_set_new(sec_obj, option, json_string(value));
124 json_object_set_new (sections_obj, section, sec_obj); 122 json_object_set_new(sections_obj, section, sec_obj);
125} 123}
126 124
127static void 125static void
128add_section_contents (void *cls, 126add_section_contents(void *cls,
129 const char *section, 127 const char *section,
130 const char *option, 128 const char *option,
131 const char *value) 129 const char *value)
132{ 130{
133 json_t *section_obj = cls; 131 json_t *section_obj = cls;
134 json_object_set_new (section_obj, option, json_string (value)); 132
133 json_object_set_new(section_obj, option, json_string(value));
135} 134}
136 135
137/** 136/**
@@ -140,9 +139,9 @@ add_section_contents (void *cls,
140 * @param handle the lookup handle 139 * @param handle the lookup handle
141 */ 140 */
142static void 141static void
143get_cont (struct GNUNET_REST_RequestHandle *con_handle, 142get_cont(struct GNUNET_REST_RequestHandle *con_handle,
144 const char *url, 143 const char *url,
145 void *cls) 144 void *cls)
146{ 145{
147 struct MHD_Response *resp; 146 struct MHD_Response *resp;
148 struct RequestHandle *handle = cls; 147 struct RequestHandle *handle = cls;
@@ -150,50 +149,50 @@ get_cont (struct GNUNET_REST_RequestHandle *con_handle,
150 char *response; 149 char *response;
151 json_t *result; 150 json_t *result;
152 151
153 if (strlen (GNUNET_REST_API_NS_CONFIG) > strlen (handle->url)) 152 if (strlen(GNUNET_REST_API_NS_CONFIG) > strlen(handle->url))
154 { 153 {
155 handle->response_code = MHD_HTTP_BAD_REQUEST; 154 handle->response_code = MHD_HTTP_BAD_REQUEST;
156 GNUNET_SCHEDULER_add_now (&do_error, handle); 155 GNUNET_SCHEDULER_add_now(&do_error, handle);
157 return; 156 return;
158 } 157 }
159 if (strlen (GNUNET_REST_API_NS_CONFIG) == strlen (handle->url)) 158 if (strlen(GNUNET_REST_API_NS_CONFIG) == strlen(handle->url))
160 { 159 {
161 result = json_object (); 160 result = json_object();
162 GNUNET_CONFIGURATION_iterate (cfg, &add_sections, result); 161 GNUNET_CONFIGURATION_iterate(cfg, &add_sections, result);
163 } 162 }
164 else 163 else
165 { 164 {
166 result = json_object (); 165 result = json_object();
167 section = &handle->url[strlen (GNUNET_REST_API_NS_CONFIG) + 1]; 166 section = &handle->url[strlen(GNUNET_REST_API_NS_CONFIG) + 1];
168 GNUNET_CONFIGURATION_iterate_section_values (cfg, 167 GNUNET_CONFIGURATION_iterate_section_values(cfg,
169 section, 168 section,
170 &add_section_contents, 169 &add_section_contents,
171 result); 170 result);
172 } 171 }
173 response = json_dumps (result, 0); 172 response = json_dumps(result, 0);
174 resp = GNUNET_REST_create_response (response); 173 resp = GNUNET_REST_create_response(response);
175 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 174 handle->proc(handle->proc_cls, resp, MHD_HTTP_OK);
176 cleanup_handle (handle); 175 cleanup_handle(handle);
177 GNUNET_free (response); 176 GNUNET_free(response);
178 json_decref (result); 177 json_decref(result);
179} 178}
180 179
181struct GNUNET_CONFIGURATION_Handle * 180struct GNUNET_CONFIGURATION_Handle *
182set_value (struct GNUNET_CONFIGURATION_Handle *config, 181set_value(struct GNUNET_CONFIGURATION_Handle *config,
183 const char *section, 182 const char *section,
184 const char *option, 183 const char *option,
185 json_t *value) 184 json_t *value)
186{ 185{
187 if (json_is_string (value)) 186 if (json_is_string(value))
188 GNUNET_CONFIGURATION_set_value_string (config, section, option, json_string_value (value)); 187 GNUNET_CONFIGURATION_set_value_string(config, section, option, json_string_value(value));
189 else if (json_is_number (value)) 188 else if (json_is_number(value))
190 GNUNET_CONFIGURATION_set_value_number (config, section, option, json_integer_value (value)); 189 GNUNET_CONFIGURATION_set_value_number(config, section, option, json_integer_value(value));
191 else if (json_is_null (value)) 190 else if (json_is_null(value))
192 GNUNET_CONFIGURATION_set_value_string (config, section, option, NULL); 191 GNUNET_CONFIGURATION_set_value_string(config, section, option, NULL);
193 else if (json_is_true (value)) 192 else if (json_is_true(value))
194 GNUNET_CONFIGURATION_set_value_string (config, section, option, "yes"); 193 GNUNET_CONFIGURATION_set_value_string(config, section, option, "yes");
195 else if (json_is_false (value)) 194 else if (json_is_false(value))
196 GNUNET_CONFIGURATION_set_value_string (config, section, option, "no"); 195 GNUNET_CONFIGURATION_set_value_string(config, section, option, "no");
197 else 196 else
198 return NULL; 197 return NULL;
199 return config; // for error handling (0 -> success, 1 -> error) 198 return config; // for error handling (0 -> success, 1 -> error)
@@ -205,13 +204,13 @@ set_value (struct GNUNET_CONFIGURATION_Handle *config,
205 * @param handle the lookup handle 204 * @param handle the lookup handle
206 */ 205 */
207static void 206static void
208set_cont (struct GNUNET_REST_RequestHandle *con_handle, 207set_cont(struct GNUNET_REST_RequestHandle *con_handle,
209 const char *url, 208 const char *url,
210 void *cls) 209 void *cls)
211{ 210{
212 struct RequestHandle *handle = cls; 211 struct RequestHandle *handle = cls;
213 char term_data[handle->rest_handle->data_size + 1]; 212 char term_data[handle->rest_handle->data_size + 1];
214 struct GNUNET_CONFIGURATION_Handle *out = GNUNET_CONFIGURATION_dup (cfg); 213 struct GNUNET_CONFIGURATION_Handle *out = GNUNET_CONFIGURATION_dup(cfg);
215 214
216 json_error_t err; 215 json_error_t err;
217 json_t *data_json; 216 json_t *data_json;
@@ -220,89 +219,89 @@ set_cont (struct GNUNET_REST_RequestHandle *con_handle,
220 json_t *sec_obj; 219 json_t *sec_obj;
221 json_t *value; 220 json_t *value;
222 char *cfg_fn; 221 char *cfg_fn;
223 222
224 // invalid url 223 // invalid url
225 if (strlen (GNUNET_REST_API_NS_CONFIG) > strlen (handle->url)) 224 if (strlen(GNUNET_REST_API_NS_CONFIG) > strlen(handle->url))
226 { 225 {
227 handle->response_code = MHD_HTTP_BAD_REQUEST; 226 handle->response_code = MHD_HTTP_BAD_REQUEST;
228 GNUNET_SCHEDULER_add_now (&do_error, handle); 227 GNUNET_SCHEDULER_add_now(&do_error, handle);
229 return; 228 return;
230 } 229 }
231 230
232 // extract data from handle 231 // extract data from handle
233 term_data[handle->rest_handle->data_size] = '\0'; 232 term_data[handle->rest_handle->data_size] = '\0';
234 GNUNET_memcpy (term_data, 233 GNUNET_memcpy(term_data,
235 handle->rest_handle->data, 234 handle->rest_handle->data,
236 handle->rest_handle->data_size); 235 handle->rest_handle->data_size);
237 data_json = json_loads (term_data, JSON_DECODE_ANY, &err); 236 data_json = json_loads(term_data, JSON_DECODE_ANY, &err);
238 237
239 if (NULL == data_json) 238 if (NULL == data_json)
240 { 239 {
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 240 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
242 "Unable to parse JSON Object from %s\n", 241 "Unable to parse JSON Object from %s\n",
243 term_data); 242 term_data);
244 GNUNET_SCHEDULER_add_now (&do_error, handle); 243 GNUNET_SCHEDULER_add_now(&do_error, handle);
245 return; 244 return;
246 } 245 }
247 246
248 // POST /config => {<section> : {<option> : <value>}} 247 // POST /config => {<section> : {<option> : <value>}}
249 if (strlen (GNUNET_REST_API_NS_CONFIG) == strlen (handle->url)) // POST /config 248 if (strlen(GNUNET_REST_API_NS_CONFIG) == strlen(handle->url)) // POST /config
250 { 249 {
251 // iterate over sections 250 // iterate over sections
252 json_object_foreach (data_json, section, sec_obj) 251 json_object_foreach(data_json, section, sec_obj)
253 {
254 // iterate over options
255 json_object_foreach (sec_obj, option, value)
256 { 252 {
257 out = set_value (out, section, option, value); 253 // iterate over options
258 if (NULL == out) 254 json_object_foreach(sec_obj, option, value)
259 { 255 {
260 handle->response_code = MHD_HTTP_BAD_REQUEST; 256 out = set_value(out, section, option, value);
261 GNUNET_SCHEDULER_add_now (&do_error, handle); 257 if (NULL == out)
262 json_decref (data_json); 258 {
263 return; 259 handle->response_code = MHD_HTTP_BAD_REQUEST;
260 GNUNET_SCHEDULER_add_now(&do_error, handle);
261 json_decref(data_json);
262 return;
263 }
264 } 264 }
265 } 265 }
266 } 266 }
267 }
268 else // POST /config/<section> => {<option> : <value>} 267 else // POST /config/<section> => {<option> : <value>}
269 {
270 // extract the "<section>" part from the url
271 section = &handle->url[strlen (GNUNET_REST_API_NS_CONFIG) + 1];
272 // iterate over options
273 json_object_foreach (data_json, option, value)
274 { 268 {
275 out = set_value (out, section, option, value); 269 // extract the "<section>" part from the url
276 if (NULL == out) 270 section = &handle->url[strlen(GNUNET_REST_API_NS_CONFIG) + 1];
271 // iterate over options
272 json_object_foreach(data_json, option, value)
277 { 273 {
278 handle->response_code = MHD_HTTP_BAD_REQUEST; 274 out = set_value(out, section, option, value);
279 GNUNET_SCHEDULER_add_now (&do_error, handle); 275 if (NULL == out)
280 json_decref (data_json); 276 {
281 return; 277 handle->response_code = MHD_HTTP_BAD_REQUEST;
278 GNUNET_SCHEDULER_add_now(&do_error, handle);
279 json_decref(data_json);
280 return;
281 }
282 } 282 }
283 } 283 }
284 } 284 json_decref(data_json);
285 json_decref (data_json);
286 285
287 286
288 // get cfg file path 287 // get cfg file path
289 cfg_fn = NULL; 288 cfg_fn = NULL;
290 const char *xdg = getenv ("XDG_CONFIG_HOME"); 289 const char *xdg = getenv("XDG_CONFIG_HOME");
291 if (NULL != xdg) 290 if (NULL != xdg)
292 GNUNET_asprintf (&cfg_fn, 291 GNUNET_asprintf(&cfg_fn,
293 "%s%s%s", 292 "%s%s%s",
294 xdg, 293 xdg,
295 DIR_SEPARATOR_STR, 294 DIR_SEPARATOR_STR,
296 GNUNET_OS_project_data_get ()->config_file); 295 GNUNET_OS_project_data_get()->config_file);
297 else 296 else
298 cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file); 297 cfg_fn = GNUNET_strdup(GNUNET_OS_project_data_get()->user_config_file);
299 298
300 GNUNET_CONFIGURATION_write (out, cfg_fn); 299 GNUNET_CONFIGURATION_write(out, cfg_fn);
301 cfg = out; 300 cfg = out;
302 handle->proc (handle->proc_cls, 301 handle->proc(handle->proc_cls,
303 GNUNET_REST_create_response (NULL), 302 GNUNET_REST_create_response(NULL),
304 MHD_HTTP_OK); 303 MHD_HTTP_OK);
305 cleanup_handle (handle); 304 cleanup_handle(handle);
306} 305}
307 306
308/** 307/**
@@ -311,19 +310,19 @@ set_cont (struct GNUNET_REST_RequestHandle *con_handle,
311 * @param handle the lookup handle 310 * @param handle the lookup handle
312 */ 311 */
313static void 312static void
314options_cont (struct GNUNET_REST_RequestHandle *con_handle, 313options_cont(struct GNUNET_REST_RequestHandle *con_handle,
315 const char *url, 314 const char *url,
316 void *cls) 315 void *cls)
317{ 316{
318 struct MHD_Response *resp; 317 struct MHD_Response *resp;
319 struct RequestHandle *handle = cls; 318 struct RequestHandle *handle = cls;
320 319
321 resp = GNUNET_REST_create_response (NULL); 320 resp = GNUNET_REST_create_response(NULL);
322 MHD_add_response_header (resp, 321 MHD_add_response_header(resp,
323 "Access-Control-Allow-Methods", 322 "Access-Control-Allow-Methods",
324 MHD_HTTP_METHOD_GET); 323 MHD_HTTP_METHOD_GET);
325 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 324 handle->proc(handle->proc_cls, resp, MHD_HTTP_OK);
326 cleanup_handle (handle); 325 cleanup_handle(handle);
327} 326}
328 327
329 328
@@ -339,31 +338,32 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle,
339 * @return #GNUNET_OK if request accepted 338 * @return #GNUNET_OK if request accepted
340 */ 339 */
341static void 340static void
342rest_config_process_request (struct GNUNET_REST_RequestHandle *conndata_handle, 341rest_config_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
343 GNUNET_REST_ResultProcessor proc, 342 GNUNET_REST_ResultProcessor proc,
344 void *proc_cls) 343 void *proc_cls)
345{ 344{
346 static const struct GNUNET_REST_RequestHandler handlers[] = { 345 static const struct GNUNET_REST_RequestHandler handlers[] = {
347 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_CONFIG, &get_cont}, 346 { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_CONFIG, &get_cont },
348 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_CONFIG, &set_cont}, 347 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_CONFIG, &set_cont },
349 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_CONFIG, &options_cont}, 348 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_CONFIG, &options_cont },
350 GNUNET_REST_HANDLER_END}; 349 GNUNET_REST_HANDLER_END
351 struct RequestHandle *handle = GNUNET_new (struct RequestHandle); 350 };
351 struct RequestHandle *handle = GNUNET_new(struct RequestHandle);
352 struct GNUNET_REST_RequestHandlerError err; 352 struct GNUNET_REST_RequestHandlerError err;
353 353
354 handle->proc_cls = proc_cls; 354 handle->proc_cls = proc_cls;
355 handle->proc = proc; 355 handle->proc = proc;
356 handle->rest_handle = conndata_handle; 356 handle->rest_handle = conndata_handle;
357 handle->url = GNUNET_strdup (conndata_handle->url); 357 handle->url = GNUNET_strdup(conndata_handle->url);
358 if (handle->url[strlen (handle->url) - 1] == '/') 358 if (handle->url[strlen(handle->url) - 1] == '/')
359 handle->url[strlen (handle->url) - 1] = '\0'; 359 handle->url[strlen(handle->url) - 1] = '\0';
360 360
361 if (GNUNET_NO == 361 if (GNUNET_NO ==
362 GNUNET_REST_handle_request (conndata_handle, handlers, &err, handle)) 362 GNUNET_REST_handle_request(conndata_handle, handlers, &err, handle))
363 { 363 {
364 handle->response_code = err.error_code; 364 handle->response_code = err.error_code;
365 GNUNET_SCHEDULER_add_now (&do_error, handle); 365 GNUNET_SCHEDULER_add_now(&do_error, handle);
366 } 366 }
367} 367}
368 368
369 369
@@ -374,21 +374,22 @@ rest_config_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
374 * @return NULL on error, otherwise the plugin context 374 * @return NULL on error, otherwise the plugin context
375 */ 375 */
376void * 376void *
377libgnunet_plugin_rest_config_init (void *cls) 377libgnunet_plugin_rest_config_init(void *cls)
378{ 378{
379 static struct Plugin plugin; 379 static struct Plugin plugin;
380
380 cfg = cls; 381 cfg = cls;
381 struct GNUNET_REST_Plugin *api; 382 struct GNUNET_REST_Plugin *api;
382 383
383 if (NULL != plugin.cfg) 384 if (NULL != plugin.cfg)
384 return NULL; /* can only initialize once! */ 385 return NULL; /* can only initialize once! */
385 memset (&plugin, 0, sizeof (struct Plugin)); 386 memset(&plugin, 0, sizeof(struct Plugin));
386 plugin.cfg = cfg; 387 plugin.cfg = cfg;
387 api = GNUNET_new (struct GNUNET_REST_Plugin); 388 api = GNUNET_new(struct GNUNET_REST_Plugin);
388 api->cls = &plugin; 389 api->cls = &plugin;
389 api->name = GNUNET_REST_API_NS_CONFIG; 390 api->name = GNUNET_REST_API_NS_CONFIG;
390 api->process_request = &rest_config_process_request; 391 api->process_request = &rest_config_process_request;
391 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("CONFIG REST API initialized\n")); 392 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("CONFIG REST API initialized\n"));
392 return api; 393 return api;
393} 394}
394 395
@@ -400,14 +401,14 @@ libgnunet_plugin_rest_config_init (void *cls)
400 * @return always NULL 401 * @return always NULL
401 */ 402 */
402void * 403void *
403libgnunet_plugin_rest_config_done (void *cls) 404libgnunet_plugin_rest_config_done(void *cls)
404{ 405{
405 struct GNUNET_REST_Plugin *api = cls; 406 struct GNUNET_REST_Plugin *api = cls;
406 struct Plugin *plugin = api->cls; 407 struct Plugin *plugin = api->cls;
407 408
408 plugin->cfg = NULL; 409 plugin->cfg = NULL;
409 GNUNET_free (api); 410 GNUNET_free(api);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONFIG REST plugin is finished\n"); 411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CONFIG REST plugin is finished\n");
411 return NULL; 412 return NULL;
412} 413}
413 414