2 This file is part of GNUnet.
3 Copyright (C) 2012-2018 GNUnet e.V.
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.
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.
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/>.
19 * @author Martin Schanzenbach
20 * @file gns/plugin_rest_copying.c
21 * @brief REST plugin that serves licensing information.
26 #include "gnunet_rest_plugin.h"
27 #include <gnunet_rest_lib.h>
29 #define GNUNET_REST_API_NS_COPYING "/copying"
31 #define GNUNET_REST_COPYING_TEXT "GNU Affero General Public License version 3 or later. See also: <http://www.gnu.org/licenses/>"
34 * @brief struct returned by the initialization function of the plugin
38 const struct GNUNET_CONFIGURATION_Handle *cfg;
41 const struct GNUNET_CONFIGURATION_Handle *cfg;
46 * Handle to rest request
48 struct GNUNET_REST_RequestHandle *rest_handle;
51 * The plugin result processor
53 GNUNET_REST_ResultProcessor proc;
56 * The closure of the result processor
69 * Cleanup request handle.
71 * @param handle Handle to clean up
74 cleanup_handle (struct RequestHandle *handle)
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83 * Task run on shutdown. Cleans up everything.
86 * @param tc scheduler context
91 struct RequestHandle *handle = cls;
92 struct MHD_Response *resp;
94 resp = GNUNET_REST_create_response (NULL);
95 handle->proc (handle->proc_cls, resp, handle->response_code);
96 cleanup_handle (handle);
101 * Handle rest request
103 * @param handle the lookup handle
106 get_cont (struct GNUNET_REST_RequestHandle *con_handle,
110 struct MHD_Response *resp;
111 struct RequestHandle *handle = cls;
113 resp = GNUNET_REST_create_response (GNUNET_REST_COPYING_TEXT);
114 handle->proc (handle->proc_cls,
117 cleanup_handle (handle);
123 * Handle rest request
125 * @param handle the lookup handle
128 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
132 struct MHD_Response *resp;
133 struct RequestHandle *handle = cls;
135 resp = GNUNET_REST_create_response (NULL);
136 MHD_add_response_header (resp,
137 "Access-Control-Allow-Methods",
138 MHD_HTTP_METHOD_GET);
139 handle->proc (handle->proc_cls,
142 cleanup_handle (handle);
147 * Function processing the REST call
149 * @param method HTTP method
150 * @param url URL of the HTTP request
151 * @param data body of the HTTP request (optional)
152 * @param data_size length of the body
153 * @param proc callback function for the result
154 * @param proc_cls closure for @a proc
155 * @return #GNUNET_OK if request accepted
158 rest_copying_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
159 GNUNET_REST_ResultProcessor proc,
162 static const struct GNUNET_REST_RequestHandler handlers[] = {
163 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_COPYING, &get_cont},
164 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_COPYING, &options_cont},
165 GNUNET_REST_HANDLER_END
167 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
168 struct GNUNET_REST_RequestHandlerError err;
170 handle->proc_cls = proc_cls;
172 handle->rest_handle = conndata_handle;
174 if (GNUNET_NO == GNUNET_REST_handle_request (conndata_handle,
179 handle->response_code = err.error_code;
180 GNUNET_SCHEDULER_add_now (&do_error, handle);
186 * Entry point for the plugin.
188 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
189 * @return NULL on error, otherwise the plugin context
192 libgnunet_plugin_rest_copying_init (void *cls)
194 static struct Plugin plugin;
196 struct GNUNET_REST_Plugin *api;
198 if (NULL != plugin.cfg)
199 return NULL; /* can only initialize once! */
200 memset (&plugin, 0, sizeof (struct Plugin));
202 api = GNUNET_new (struct GNUNET_REST_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"));
213 * Exit point from the plugin.
215 * @param cls the plugin context (as returned by "init")
216 * @return always NULL
219 libgnunet_plugin_rest_copying_done (void *cls)
221 struct GNUNET_REST_Plugin *api = cls;
222 struct Plugin *plugin = api->cls;
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "COPYING REST plugin is finished\n");
231 /* end of plugin_rest_copying.c */