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 RequestHandle *next;
52 
56  struct RequestHandle *prev;
57 
61  struct GNUNET_REST_RequestHandle *rest_handle;
62 
67 
71  void *proc_cls;
72 
76  int response_code;
77 };
78 
83 
88 
94 static void
96 {
98  "Cleaning up\n");
99  GNUNET_CONTAINER_DLL_remove (requests_head,
100  requests_tail,
101  handle);
102  GNUNET_free (handle);
103 }
104 
105 
111 static void
113  const char*url,
114  void *cls)
115 {
116  struct MHD_Response *resp;
117  struct RequestHandle *handle = cls;
118 
120  handle->proc (handle->proc_cls,
121  resp,
122  MHD_HTTP_OK);
123  cleanup_handle (handle);
124 }
125 
126 
132 static void
134  const char*url,
135  void *cls)
136 {
137  struct MHD_Response *resp;
138  struct RequestHandle *handle = cls;
139 
140  resp = GNUNET_REST_create_response (NULL);
141  MHD_add_response_header (resp,
142  "Access-Control-Allow-Methods",
143  MHD_HTTP_METHOD_GET);
144  handle->proc (handle->proc_cls,
145  resp,
146  MHD_HTTP_OK);
147  cleanup_handle (handle);
148 }
149 
150 
162 static enum GNUNET_GenericReturnValue
165  void *proc_cls)
166 {
167  static const struct GNUNET_REST_RequestHandler handlers[] = {
168  { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_COPYING, &get_cont },
169  { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_COPYING, &options_cont },
171  };
172  struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
174 
175  handle->proc_cls = proc_cls;
176  handle->proc = proc;
177  handle->rest_handle = conndata_handle;
178  GNUNET_CONTAINER_DLL_insert (requests_head,
179  requests_tail,
180  handle);
181  return GNUNET_REST_handle_request (conndata_handle,
182  handlers,
183  &err,
184  handle);
185 }
186 
187 
194 void *
196 {
197  static struct Plugin plugin;
198 
199  cfg = cls;
200  struct GNUNET_REST_Plugin *api;
201 
202  if (NULL != plugin.cfg)
203  return NULL; /* can only initialize once! */
204  memset (&plugin, 0, sizeof(struct Plugin));
205  plugin.cfg = cfg;
206  api = GNUNET_new (struct GNUNET_REST_Plugin);
207  api->cls = &plugin;
211  _ ("COPYING REST API initialized\n"));
212  return api;
213 }
214 
215 
222 void *
224 {
225  struct GNUNET_REST_Plugin *api = cls;
226  struct Plugin *plugin = api->cls;
227 
228  while (NULL != requests_head)
229  cleanup_handle (requests_head);
230  plugin->cfg = NULL;
231  GNUNET_free (api);
233  "COPYING REST plugin is finished\n");
234  return NULL;
235 }
236 
237 
238 /* end of plugin_rest_copying.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
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_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_REST_API_NS_COPYING
void * cls
The closure of the plugin.
GNUNET_REST_ResultProcessor proc
The plugin result processor.
static enum GNUNET_GenericReturnValue rest_copying_process_request(struct GNUNET_REST_RequestHandle *conndata_handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function processing the REST call.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct returned by the initialization function of the plugin
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:83
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static struct RequestHandle * requests_head
DLL.
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:184
#define GNUNET_REST_COPYING_TEXT
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.
static char * plugin
Solver plugin name as string.
struct RequestHandle * next
DLL.
static struct RequestHandle * requests_tail
DLL.
struct RequestHandle * prev
DLL.
configuration data
Definition: configuration.c:84
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,...)
enum GNUNET_GenericReturnValue(* process_request)(struct GNUNET_REST_RequestHandle *handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function to process a REST call.
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.