GNUnet  0.20.0
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 
62 
67 
71  void *proc_cls;
72 
76  int response_code;
77 };
78 
83 
88 
94 static void
96 {
98  "Cleaning up\n");
101  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);
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  GNUNET_assert (MHD_NO != 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);
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;
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)
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 */
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
struct TestcasePlugin * plugin
The process handle to the testbed service.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
API for helper library to parse/create REST.
GNUnet service REST plugin header.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create REST MHD response.
Definition: rest.c:44
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:64
void(* GNUNET_REST_ResultProcessor)(void *cls, struct MHD_Response *resp, int status)
Iterator called on obtained result for a REST result.
#define GNUNET_REST_HANDLER_END
@ MHD_HTTP_OK
OK [RFC7231, Section 6.3.1].
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
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.
#define GNUNET_REST_COPYING_TEXT
void * libgnunet_plugin_rest_copying_done(void *cls)
Exit point from the plugin.
const struct GNUNET_CONFIGURATION_Handle * cfg
Handle to our configuration.
static struct RequestHandle * requests_head
DLL.
static void options_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle rest request.
void * libgnunet_plugin_rest_copying_init(void *cls)
Entry point for the plugin.
static void cleanup_handle(struct RequestHandle *handle)
Cleanup request handle.
#define GNUNET_REST_API_NS_COPYING
static struct RequestHandle * requests_tail
DLL.
static void get_cont(struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls)
Handle rest request.
void * cls
Closure for all of the callbacks.
struct returned by the initialization function of the plugin
char * name
Plugin name.
void * cls
The closure of the plugin.
enum GNUNET_GenericReturnValue(* process_request)(struct GNUNET_REST_RequestHandle *handle, GNUNET_REST_ResultProcessor proc, void *proc_cls)
Function to process a REST call.
Handle for a plugin.
Definition: block.c:38
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
The request handle.
struct RequestHandle * prev
DLL.
int response_code
Response code.
void * proc_cls
The closure of the result processor.
GNUNET_REST_ResultProcessor proc
The plugin result processor.
struct RequestHandle * next
DLL.
struct GNUNET_REST_RequestHandle * rest_handle
Rest connection.
char * url
The url.