GNUnet  0.10.x
plugin_rest_config.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2012-2018 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
27 #include "platform.h"
28 #include "gnunet_rest_plugin.h"
29 #include <gnunet_rest_lib.h>
30 #include <gnunet_util_lib.h>
31 #include <jansson.h>
32 
33 #define GNUNET_REST_API_NS_CONFIG "/config"
34 
38 struct Plugin
39 {
40  const struct GNUNET_CONFIGURATION_Handle *cfg;
41 };
42 
44 
45 struct RequestHandle
46 {
50  struct GNUNET_REST_RequestHandle *rest_handle;
51 
56 
60  void *proc_cls;
61 
65  int response_code;
66 
70  char *url;
71 };
72 
73 
79 static void
81 {
82  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
83  if (NULL != handle->url)
84  GNUNET_free (handle->url);
85  GNUNET_free (handle);
86 }
87 
88 
95 static void
96 do_error (void *cls)
97 {
98  struct RequestHandle *handle = cls;
99  struct MHD_Response *resp;
100 
101  resp = GNUNET_REST_create_response (NULL);
102  handle->proc (handle->proc_cls, resp, handle->response_code);
103  cleanup_handle (handle);
104 }
105 
106 
107 static void
108 add_sections (void *cls,
109  const char *section,
110  const char *option,
111  const char *value)
112 {
113  json_t *sections_obj = cls;
114  json_t *sec_obj;
115 
116  sec_obj = json_object_get (sections_obj, section);
117  if (NULL != sec_obj)
118  {
119  json_object_set_new (sec_obj, option, json_string (value));
120  return;
121  }
122  sec_obj = json_object ();
123  json_object_set_new (sec_obj, option, json_string (value));
124  json_object_set_new (sections_obj, section, sec_obj);
125 }
126 
127 static void
129  const char *section,
130  const char *option,
131  const char *value)
132 {
133  json_t *section_obj = cls;
134  json_object_set_new (section_obj, option, json_string (value));
135 }
136 
137 
143 static void
145  const char *url,
146  void *cls)
147 {
148  struct MHD_Response *resp;
149  struct RequestHandle *handle = cls;
150  const char *section;
151  char *response;
152  json_t *result;
153 
154  if (strlen (GNUNET_REST_API_NS_CONFIG) > strlen (handle->url))
155  {
156  handle->response_code = MHD_HTTP_BAD_REQUEST;
158  return;
159  }
160  if (strlen (GNUNET_REST_API_NS_CONFIG) == strlen (handle->url))
161  {
162  result = json_object ();
164  }
165  else
166  {
167  result = json_object ();
168  section = &handle->url[strlen (GNUNET_REST_API_NS_CONFIG) + 1];
170  section,
172  result);
173  }
174  response = json_dumps (result, 0);
175  resp = GNUNET_REST_create_response (response);
176  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
177  cleanup_handle (handle);
178  GNUNET_free (response);
179  json_decref (result);
180 }
181 
182 
188 static void
190  const char *url,
191  void *cls)
192 {
193  struct MHD_Response *resp;
194  struct RequestHandle *handle = cls;
195 
196  resp = GNUNET_REST_create_response (NULL);
197  MHD_add_response_header (resp,
198  "Access-Control-Allow-Methods",
199  MHD_HTTP_METHOD_GET);
200  handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
201  cleanup_handle (handle);
202 }
203 
204 
216 static void
219  void *proc_cls)
220 {
221  static const struct GNUNET_REST_RequestHandler handlers[] = {
222  {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_CONFIG, &get_cont},
223  {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_CONFIG, &options_cont},
225  struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
227 
228  handle->proc_cls = proc_cls;
229  handle->proc = proc;
230  handle->rest_handle = conndata_handle;
231  handle->url = GNUNET_strdup (conndata_handle->url);
232  if (handle->url[strlen (handle->url) - 1] == '/')
233  handle->url[strlen (handle->url) - 1] = '\0';
234 
235  if (GNUNET_NO ==
236  GNUNET_REST_handle_request (conndata_handle, handlers, &err, handle))
237  {
238  handle->response_code = err.error_code;
240  }
241 }
242 
243 
250 void *
252 {
253  static struct Plugin plugin;
254  cfg = cls;
255  struct GNUNET_REST_Plugin *api;
256 
257  if (NULL != plugin.cfg)
258  return NULL; /* can only initialize once! */
259  memset (&plugin, 0, sizeof (struct Plugin));
260  plugin.cfg = cfg;
261  api = GNUNET_new (struct GNUNET_REST_Plugin);
262  api->cls = &plugin;
265  GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("CONFIG REST API initialized\n"));
266  return api;
267 }
268 
269 
276 void *
278 {
279  struct GNUNET_REST_Plugin *api = cls;
280  struct Plugin *plugin = api->cls;
281 
282  plugin->cfg = NULL;
283  GNUNET_free (api);
284  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONFIG REST plugin is finished\n");
285  return NULL;
286 }
287 
288 /* end of plugin_rest_config.c */
void(* GNUNET_REST_ResultProcessor)(void *cls, struct MHD_Response *resp, int status)
Iterator called on obtained result for a REST result.
#define GNUNET_REST_API_NS_CONFIG
void GNUNET_CONFIGURATION_iterate_section_values(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
Iterate over values of a section in the configuration.
void * cls
The closure of the plugin.
void GNUNET_CONFIGURATION_iterate(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
Iterate over all options in the configuration.
GNUNET_REST_ResultProcessor proc
The plugin result processor.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct returned by the initialization function of the plugin
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void(* process_request)(struct GNUNET_REST_RequestHandle *handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function to process a REST call.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * proc_cls
The closure of the result processor.
#define GNUNET_REST_HANDLER_END
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
The request handle.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static char * section
Name of the section.
Definition: gnunet-config.c:33
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
static void rest_config_process_request(struct GNUNET_REST_RequestHandle *conndata_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function processing the REST call.
static char * value
Value of the record to add/remove.
const char * url
The url as string.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1273
char * name
Plugin name.
static char * option
Name of the option.
Definition: gnunet-config.c:38
static void do_error(void *cls)
Task run on shutdown.
static void get_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle rest request.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static int result
Global testing status.
int GNUNET_REST_handle_request(struct GNUNET_REST_RequestHandle *conn, const struct GNUNET_REST_RequestHandler *handlers, struct GNUNET_REST_RequestHandlerError *err, void *cls)
Definition: rest.c:76
static void add_section_contents(void *cls, const char *section, const char *option, const char *value)
struct GNUNET_REST_RequestHandle * rest_handle
Handle to rest request.
int response_code
HTTP response code.
void * libgnunet_plugin_rest_config_init(void *cls)
Entry point for the plugin.
configuration data
Definition: configuration.c:85
Handle for a plugin.
Definition: block.c:37
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
Definition: rest.c:56
#define GNUNET_log(kind,...)
static struct MHD_Response * response
Our canonical response.
static void add_sections(void *cls, const char *section, const char *option, const char *value)
void * libgnunet_plugin_rest_config_done(void *cls)
Exit point from the plugin.
static void cleanup_handle(struct RequestHandle *handle)
Cleanup request handle.
static void options_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle rest request.
#define GNUNET_free(ptr)
Wrapper around free.
char * url
The url.