GNUnet  0.11.x
plugin_rest_copying.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 
31 #define GNUNET_REST_API_NS_COPYING "/copying"
32 
33 #define GNUNET_REST_COPYING_TEXT \
34  "GNU Affero General Public License version 3 or later. See also: <http://www.gnu.org/licenses/>"
35 
39 struct Plugin
40 {
41  const struct GNUNET_CONFIGURATION_Handle *cfg;
42 };
43 
45 
46 struct RequestHandle
47 {
51  struct GNUNET_REST_RequestHandle *rest_handle;
52 
57 
61  void *proc_cls;
62 
66  int response_code;
67 };
68 
69 
75 static void
77 {
79  "Cleaning up\n");
80  GNUNET_free (handle);
81 }
82 
83 
90 static void
91 do_error (void *cls)
92 {
93  struct RequestHandle *handle = cls;
94  struct MHD_Response *resp;
95 
96  resp = GNUNET_REST_create_response (NULL);
97  handle->proc (handle->proc_cls, resp, handle->response_code);
98  cleanup_handle (handle);
99 }
100 
101 
107 static void
109  const char*url,
110  void *cls)
111 {
112  struct MHD_Response *resp;
113  struct RequestHandle *handle = cls;
114 
116  handle->proc (handle->proc_cls,
117  resp,
118  MHD_HTTP_OK);
119  cleanup_handle (handle);
120 }
121 
122 
128 static void
130  const char*url,
131  void *cls)
132 {
133  struct MHD_Response *resp;
134  struct RequestHandle *handle = cls;
135 
136  resp = GNUNET_REST_create_response (NULL);
137  MHD_add_response_header (resp,
138  "Access-Control-Allow-Methods",
139  MHD_HTTP_METHOD_GET);
140  handle->proc (handle->proc_cls,
141  resp,
142  MHD_HTTP_OK);
143  cleanup_handle (handle);
144 }
145 
146 
158 static void
161  void *proc_cls)
162 {
163  static const struct GNUNET_REST_RequestHandler handlers[] = {
164  { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_COPYING, &get_cont },
165  { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_COPYING, &options_cont },
167  };
168  struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
170 
171  handle->proc_cls = proc_cls;
172  handle->proc = proc;
173  handle->rest_handle = conndata_handle;
174 
175  if (GNUNET_NO == GNUNET_REST_handle_request (conndata_handle,
176  handlers,
177  &err,
178  handle))
179  {
180  handle->response_code = err.error_code;
182  }
183 }
184 
185 
192 void *
194 {
195  static struct Plugin plugin;
196 
197  cfg = cls;
198  struct GNUNET_REST_Plugin *api;
199 
200  if (NULL != plugin.cfg)
201  return NULL; /* can only initialize once! */
202  memset (&plugin, 0, sizeof(struct Plugin));
203  plugin.cfg = cfg;
204  api = GNUNET_new (struct GNUNET_REST_Plugin);
205  api->cls = &plugin;
209  _ ("COPYING REST API initialized\n"));
210  return api;
211 }
212 
213 
220 void *
222 {
223  struct GNUNET_REST_Plugin *api = cls;
224  struct Plugin *plugin = api->cls;
225 
226  plugin->cfg = NULL;
227  GNUNET_free (api);
229  "COPYING REST plugin is finished\n");
230  return NULL;
231 }
232 
233 
234 /* end of plugin_rest_copying.c */
void(* GNUNET_REST_ResultProcessor)(void *cls, struct MHD_Response *resp, int status)
Iterator called on obtained result for a REST result.
static void cleanup_handle(struct RequestHandle *handle)
Cleanup request handle.
#define GNUNET_REST_API_NS_COPYING
void * cls
The closure of the plugin.
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:78
static void do_error(void *cls)
Task run on shutdown.
#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.
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:181
static void rest_copying_process_request(struct GNUNET_REST_RequestHandle *conndata_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function processing the REST call.
#define GNUNET_REST_COPYING_TEXT
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:1280
char * name
Plugin name.
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.
void * libgnunet_plugin_rest_copying_done(void *cls)
Exit point from the plugin.
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:77
struct GNUNET_REST_RequestHandle * rest_handle
Rest connection.
int response_code
Response code.
static char * plugin
Solver plugin name as string.
configuration data
Definition: configuration.c:85
Handle for a plugin.
Definition: block.c:37
void * libgnunet_plugin_rest_copying_init(void *cls)
Entry point for the plugin.
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
Definition: rest.c:57
#define GNUNET_log(kind,...)
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.