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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @author Martin Schanzenbach
22 * @file gns/plugin_rest_copying.c
23 * @brief REST plugin that serves licensing information.
28 #include "gnunet_rest_plugin.h"
29 #include <gnunet_rest_lib.h>
31 #define GNUNET_REST_API_NS_COPYING "/copying"
33 #define GNUNET_REST_COPYING_TEXT \
34 "GNU Affero General Public License version 3 or later. See also: <http://www.gnu.org/licenses/>"
37 * @brief struct returned by the initialization function of the plugin
41 const struct GNUNET_CONFIGURATION_Handle *cfg;
44 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Handle to rest request
51 struct GNUNET_REST_RequestHandle *rest_handle;
54 * The plugin result processor
56 GNUNET_REST_ResultProcessor proc;
59 * The closure of the result processor
71 * Cleanup request handle.
73 * @param handle Handle to clean up
76 cleanup_handle (struct RequestHandle *handle)
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 * Task run on shutdown. Cleans up everything.
88 * @param tc scheduler context
93 struct RequestHandle *handle = cls;
94 struct MHD_Response *resp;
96 resp = GNUNET_REST_create_response (NULL);
97 handle->proc (handle->proc_cls, resp, handle->response_code);
98 cleanup_handle (handle);
103 * Handle rest request
105 * @param handle the lookup handle
108 get_cont (struct GNUNET_REST_RequestHandle *con_handle,
112 struct MHD_Response *resp;
113 struct RequestHandle *handle = cls;
115 resp = GNUNET_REST_create_response (GNUNET_REST_COPYING_TEXT);
116 handle->proc (handle->proc_cls,
119 cleanup_handle (handle);
124 * Handle rest request
126 * @param handle the lookup handle
129 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
133 struct MHD_Response *resp;
134 struct RequestHandle *handle = cls;
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,
143 cleanup_handle (handle);
148 * Function processing the REST call
150 * @param method HTTP method
151 * @param url URL of the HTTP request
152 * @param data body of the HTTP request (optional)
153 * @param data_size length of the body
154 * @param proc callback function for the result
155 * @param proc_cls closure for @a proc
156 * @return #GNUNET_OK if request accepted
159 rest_copying_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
160 GNUNET_REST_ResultProcessor proc,
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 },
166 GNUNET_REST_HANDLER_END
168 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
169 struct GNUNET_REST_RequestHandlerError err;
171 handle->proc_cls = proc_cls;
173 handle->rest_handle = conndata_handle;
175 if (GNUNET_NO == GNUNET_REST_handle_request (conndata_handle,
180 handle->response_code = err.error_code;
181 GNUNET_SCHEDULER_add_now (&do_error, handle);
187 * Entry point for the plugin.
189 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
190 * @return NULL on error, otherwise the plugin context
193 libgnunet_plugin_rest_copying_init (void *cls)
195 static struct Plugin plugin;
198 struct GNUNET_REST_Plugin *api;
200 if (NULL != plugin.cfg)
201 return NULL; /* can only initialize once! */
202 memset (&plugin, 0, sizeof(struct Plugin));
204 api = GNUNET_new (struct GNUNET_REST_Plugin);
206 api->name = GNUNET_REST_API_NS_COPYING;
207 api->process_request = &rest_copying_process_request;
208 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
209 _ ("COPYING REST API initialized\n"));
215 * Exit point from the plugin.
217 * @param cls the plugin context (as returned by "init")
218 * @return always NULL
221 libgnunet_plugin_rest_copying_done (void *cls)
223 struct GNUNET_REST_Plugin *api = cls;
224 struct Plugin *plugin = api->cls;
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "COPYING REST plugin is finished\n");
234 /* end of plugin_rest_copying.c */