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