diff options
author | Alexia Pagkopoulou <a.pagkopoulou@tum.de> | 2019-08-19 17:36:48 +0200 |
---|---|---|
committer | Alexia Pagkopoulou <a.pagkopoulou@tum.de> | 2019-08-19 17:36:48 +0200 |
commit | 7c484d98416b489072cb728f614e17186a12c81c (patch) | |
tree | 81d27270a03348c4b53c7d00861921e1531e978f /src | |
parent | b7d497cb169f8de8973d40391f1b4f40f03db389 (diff) | |
download | gnunet-7c484d98416b489072cb728f614e17186a12c81c.tar.gz gnunet-7c484d98416b489072cb728f614e17186a12c81c.zip |
made the rest config editable
Diffstat (limited to 'src')
-rw-r--r-- | src/rest/plugin_rest_config.c | 128 |
1 files changed, 127 insertions, 1 deletions
diff --git a/src/rest/plugin_rest_config.c b/src/rest/plugin_rest_config.c index fe8d9bf7f..7f1d0eedf 100644 --- a/src/rest/plugin_rest_config.c +++ b/src/rest/plugin_rest_config.c | |||
@@ -134,7 +134,6 @@ add_section_contents (void *cls, | |||
134 | json_object_set_new (section_obj, option, json_string (value)); | 134 | json_object_set_new (section_obj, option, json_string (value)); |
135 | } | 135 | } |
136 | 136 | ||
137 | |||
138 | /** | 137 | /** |
139 | * Handle rest request | 138 | * Handle rest request |
140 | * | 139 | * |
@@ -179,6 +178,132 @@ get_cont (struct GNUNET_REST_RequestHandle *con_handle, | |||
179 | json_decref (result); | 178 | json_decref (result); |
180 | } | 179 | } |
181 | 180 | ||
181 | struct GNUNET_CONFIGURATION_Handle * | ||
182 | set_value (struct GNUNET_CONFIGURATION_Handle *config, | ||
183 | const char *section, | ||
184 | const char *option, | ||
185 | json_t *value) | ||
186 | { | ||
187 | if (json_is_string (value)) | ||
188 | GNUNET_CONFIGURATION_set_value_string (config, section, option, json_string_value (value)); | ||
189 | else if (json_is_number (value)) | ||
190 | GNUNET_CONFIGURATION_set_value_number (config, section, option, json_integer_value (value)); | ||
191 | else if (json_is_null (value)) | ||
192 | GNUNET_CONFIGURATION_set_value_string (config, section, option, NULL); | ||
193 | else if (json_is_true (value)) | ||
194 | GNUNET_CONFIGURATION_set_value_string (config, section, option, "yes"); | ||
195 | else if (json_is_false (value)) | ||
196 | GNUNET_CONFIGURATION_set_value_string (config, section, option, "no"); | ||
197 | else | ||
198 | return NULL; | ||
199 | return config; // for error handling (0 -> success, 1 -> error) | ||
200 | } | ||
201 | |||
202 | /** | ||
203 | * Handle REST POST request | ||
204 | * | ||
205 | * @param handle the lookup handle | ||
206 | */ | ||
207 | static void | ||
208 | set_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
209 | const char *url, | ||
210 | void *cls) | ||
211 | { | ||
212 | struct RequestHandle *handle = cls; | ||
213 | char term_data[handle->rest_handle->data_size + 1]; | ||
214 | struct GNUNET_CONFIGURATION_Handle *out = GNUNET_CONFIGURATION_dup (cfg); | ||
215 | |||
216 | json_error_t err; | ||
217 | json_t *data_json; | ||
218 | const char *section; | ||
219 | const char *option; | ||
220 | json_t *sec_obj; | ||
221 | json_t *value; | ||
222 | char *cfg_fn; | ||
223 | |||
224 | // invalid url | ||
225 | if (strlen (GNUNET_REST_API_NS_CONFIG) > strlen (handle->url)) | ||
226 | { | ||
227 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
228 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
229 | return; | ||
230 | } | ||
231 | |||
232 | // extract data from handle | ||
233 | term_data[handle->rest_handle->data_size] = '\0'; | ||
234 | GNUNET_memcpy (term_data, | ||
235 | handle->rest_handle->data, | ||
236 | handle->rest_handle->data_size); | ||
237 | data_json = json_loads (term_data, JSON_DECODE_ANY, &err); | ||
238 | |||
239 | if (NULL == data_json) | ||
240 | { | ||
241 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
242 | "Unable to parse JSON Object from %s\n", | ||
243 | term_data); | ||
244 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
245 | return; | ||
246 | } | ||
247 | |||
248 | // POST /config => {<section> : {<option> : <value>}} | ||
249 | if (strlen (GNUNET_REST_API_NS_CONFIG) == strlen (handle->url)) // POST /config | ||
250 | { | ||
251 | // iterate over sections | ||
252 | json_object_foreach (data_json, section, sec_obj) | ||
253 | { | ||
254 | // iterate over options | ||
255 | json_object_foreach (sec_obj, option, value) | ||
256 | { | ||
257 | out = set_value (out, section, option, value); | ||
258 | if (NULL == out) | ||
259 | { | ||
260 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
261 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
262 | json_decref (data_json); | ||
263 | return; | ||
264 | } | ||
265 | } | ||
266 | } | ||
267 | } | ||
268 | 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 | { | ||
275 | out = set_value (out, section, option, value); | ||
276 | if (NULL == out) | ||
277 | { | ||
278 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
279 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
280 | json_decref (data_json); | ||
281 | return; | ||
282 | } | ||
283 | } | ||
284 | } | ||
285 | json_decref (data_json); | ||
286 | |||
287 | |||
288 | // get cfg file path | ||
289 | cfg_fn = NULL; | ||
290 | const char *xdg = getenv ("XDG_CONFIG_HOME"); | ||
291 | if (NULL != xdg) | ||
292 | GNUNET_asprintf (&cfg_fn, | ||
293 | "%s%s%s", | ||
294 | xdg, | ||
295 | DIR_SEPARATOR_STR, | ||
296 | GNUNET_OS_project_data_get ()->config_file); | ||
297 | else | ||
298 | cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file); | ||
299 | |||
300 | GNUNET_CONFIGURATION_write (out, cfg_fn); | ||
301 | cfg = out; | ||
302 | handle->proc (handle->proc_cls, | ||
303 | GNUNET_REST_create_response (NULL), | ||
304 | MHD_HTTP_OK); | ||
305 | cleanup_handle (handle); | ||
306 | } | ||
182 | 307 | ||
183 | /** | 308 | /** |
184 | * Handle rest request | 309 | * Handle rest request |
@@ -220,6 +345,7 @@ rest_config_process_request (struct GNUNET_REST_RequestHandle *conndata_handle, | |||
220 | { | 345 | { |
221 | static const struct GNUNET_REST_RequestHandler handlers[] = { | 346 | static const struct GNUNET_REST_RequestHandler handlers[] = { |
222 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_CONFIG, &get_cont}, | 347 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_CONFIG, &get_cont}, |
348 | {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_CONFIG, &set_cont}, | ||
223 | {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_CONFIG, &options_cont}, | 349 | {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_CONFIG, &options_cont}, |
224 | GNUNET_REST_HANDLER_END}; | 350 | GNUNET_REST_HANDLER_END}; |
225 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | 351 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); |