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