uncrustify as demanded.
[oweals/gnunet.git] / src / rest / plugin_rest_copying.c
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  */
20 /**
21  * @author Martin Schanzenbach
22  * @file gns/plugin_rest_copying.c
23  * @brief REST plugin that serves licensing information.
24  *
25  */
26
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 "GNU Affero General Public License version 3 or later. See also: <http://www.gnu.org/licenses/>"
34
35 /**
36  * @brief struct returned by the initialization function of the plugin
37  */
38 struct Plugin {
39   const struct GNUNET_CONFIGURATION_Handle *cfg;
40 };
41
42 const struct GNUNET_CONFIGURATION_Handle *cfg;
43
44 struct RequestHandle {
45   /**
46    * Handle to rest request
47    */
48   struct GNUNET_REST_RequestHandle *rest_handle;
49
50   /**
51    * The plugin result processor
52    */
53   GNUNET_REST_ResultProcessor proc;
54
55   /**
56    * The closure of the result processor
57    */
58   void *proc_cls;
59
60   /**
61    * HTTP response code
62    */
63   int response_code;
64 };
65
66
67 /**
68  * Cleanup request handle.
69  *
70  * @param handle Handle to clean up
71  */
72 static void
73 cleanup_handle(struct RequestHandle *handle)
74 {
75   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
76              "Cleaning up\n");
77   GNUNET_free(handle);
78 }
79
80
81 /**
82  * Task run on shutdown.  Cleans up everything.
83  *
84  * @param cls unused
85  * @param tc scheduler context
86  */
87 static void
88 do_error(void *cls)
89 {
90   struct RequestHandle *handle = cls;
91   struct MHD_Response *resp;
92
93   resp = GNUNET_REST_create_response(NULL);
94   handle->proc(handle->proc_cls, resp, handle->response_code);
95   cleanup_handle(handle);
96 }
97
98
99 /**
100  * Handle rest request
101  *
102  * @param handle the lookup handle
103  */
104 static void
105 get_cont(struct GNUNET_REST_RequestHandle *con_handle,
106          const char* url,
107          void *cls)
108 {
109   struct MHD_Response *resp;
110   struct RequestHandle *handle = cls;
111
112   resp = GNUNET_REST_create_response(GNUNET_REST_COPYING_TEXT);
113   handle->proc(handle->proc_cls,
114                resp,
115                MHD_HTTP_OK);
116   cleanup_handle(handle);
117 }
118
119
120
121 /**
122  * Handle rest request
123  *
124  * @param handle the lookup handle
125  */
126 static void
127 options_cont(struct GNUNET_REST_RequestHandle *con_handle,
128              const char* url,
129              void *cls)
130 {
131   struct MHD_Response *resp;
132   struct RequestHandle *handle = cls;
133
134   resp = GNUNET_REST_create_response(NULL);
135   MHD_add_response_header(resp,
136                           "Access-Control-Allow-Methods",
137                           MHD_HTTP_METHOD_GET);
138   handle->proc(handle->proc_cls,
139                resp,
140                MHD_HTTP_OK);
141   cleanup_handle(handle);
142 }
143
144
145 /**
146  * Function processing the REST call
147  *
148  * @param method HTTP method
149  * @param url URL of the HTTP request
150  * @param data body of the HTTP request (optional)
151  * @param data_size length of the body
152  * @param proc callback function for the result
153  * @param proc_cls closure for @a proc
154  * @return #GNUNET_OK if request accepted
155  */
156 static void
157 rest_copying_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
158                              GNUNET_REST_ResultProcessor proc,
159                              void *proc_cls)
160 {
161   static const struct GNUNET_REST_RequestHandler handlers[] = {
162     { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_COPYING, &get_cont },
163     { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_COPYING, &options_cont },
164     GNUNET_REST_HANDLER_END
165   };
166   struct RequestHandle *handle = GNUNET_new(struct RequestHandle);
167   struct GNUNET_REST_RequestHandlerError err;
168
169   handle->proc_cls = proc_cls;
170   handle->proc = proc;
171   handle->rest_handle = conndata_handle;
172
173   if (GNUNET_NO == GNUNET_REST_handle_request(conndata_handle,
174                                               handlers,
175                                               &err,
176                                               handle))
177     {
178       handle->response_code = err.error_code;
179       GNUNET_SCHEDULER_add_now(&do_error, handle);
180     }
181 }
182
183
184 /**
185  * Entry point for the plugin.
186  *
187  * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
188  * @return NULL on error, otherwise the plugin context
189  */
190 void *
191 libgnunet_plugin_rest_copying_init(void *cls)
192 {
193   static struct Plugin plugin;
194
195   cfg = cls;
196   struct GNUNET_REST_Plugin *api;
197
198   if (NULL != plugin.cfg)
199     return NULL;                /* can only initialize once! */
200   memset(&plugin, 0, sizeof(struct Plugin));
201   plugin.cfg = cfg;
202   api = GNUNET_new(struct GNUNET_REST_Plugin);
203   api->cls = &plugin;
204   api->name = GNUNET_REST_API_NS_COPYING;
205   api->process_request = &rest_copying_process_request;
206   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
207              _("COPYING REST API initialized\n"));
208   return api;
209 }
210
211
212 /**
213  * Exit point from the plugin.
214  *
215  * @param cls the plugin context (as returned by "init")
216  * @return always NULL
217  */
218 void *
219 libgnunet_plugin_rest_copying_done(void *cls)
220 {
221   struct GNUNET_REST_Plugin *api = cls;
222   struct Plugin *plugin = api->cls;
223
224   plugin->cfg = NULL;
225   GNUNET_free(api);
226   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
227              "COPYING REST plugin is finished\n");
228   return NULL;
229 }
230
231 /* end of plugin_rest_copying.c */