plugin_rest_copying.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012-2018 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Martin Schanzenbach
  18. * @file gns/plugin_rest_copying.c
  19. * @brief REST plugin that serves licensing information.
  20. *
  21. */
  22. #include "platform.h"
  23. #include "gnunet_rest_plugin.h"
  24. #include <gnunet_rest_lib.h>
  25. #define GNUNET_REST_API_NS_COPYING "/copying"
  26. #define GNUNET_REST_COPYING_TEXT \
  27. "GNU Affero General Public License version 3 or later. See also: <http://www.gnu.org/licenses/>"
  28. /**
  29. * @brief struct returned by the initialization function of the plugin
  30. */
  31. struct Plugin
  32. {
  33. const struct GNUNET_CONFIGURATION_Handle *cfg;
  34. };
  35. const struct GNUNET_CONFIGURATION_Handle *cfg;
  36. struct RequestHandle
  37. {
  38. /**
  39. * DLL
  40. */
  41. struct RequestHandle *next;
  42. /**
  43. * DLL
  44. */
  45. struct RequestHandle *prev;
  46. /**
  47. * Handle to rest request
  48. */
  49. struct GNUNET_REST_RequestHandle *rest_handle;
  50. /**
  51. * The plugin result processor
  52. */
  53. GNUNET_REST_ResultProcessor proc;
  54. /**
  55. * The closure of the result processor
  56. */
  57. void *proc_cls;
  58. /**
  59. * HTTP response code
  60. */
  61. int response_code;
  62. };
  63. /**
  64. * DLL
  65. */
  66. static struct RequestHandle *requests_head;
  67. /**
  68. * DLL
  69. */
  70. static struct RequestHandle *requests_tail;
  71. /**
  72. * Cleanup request handle.
  73. *
  74. * @param handle Handle to clean up
  75. */
  76. static void
  77. cleanup_handle (struct RequestHandle *handle)
  78. {
  79. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  80. "Cleaning up\n");
  81. GNUNET_CONTAINER_DLL_remove (requests_head,
  82. requests_tail,
  83. handle);
  84. GNUNET_free (handle);
  85. }
  86. /**
  87. * Handle rest request
  88. *
  89. * @param handle the lookup handle
  90. */
  91. static void
  92. get_cont (struct GNUNET_REST_RequestHandle *con_handle,
  93. const char*url,
  94. void *cls)
  95. {
  96. struct MHD_Response *resp;
  97. struct RequestHandle *handle = cls;
  98. resp = GNUNET_REST_create_response (GNUNET_REST_COPYING_TEXT);
  99. handle->proc (handle->proc_cls,
  100. resp,
  101. MHD_HTTP_OK);
  102. cleanup_handle (handle);
  103. }
  104. /**
  105. * Handle rest request
  106. *
  107. * @param handle the lookup handle
  108. */
  109. static void
  110. options_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. resp = GNUNET_REST_create_response (NULL);
  117. MHD_add_response_header (resp,
  118. "Access-Control-Allow-Methods",
  119. MHD_HTTP_METHOD_GET);
  120. handle->proc (handle->proc_cls,
  121. resp,
  122. MHD_HTTP_OK);
  123. cleanup_handle (handle);
  124. }
  125. /**
  126. * Function processing the REST call
  127. *
  128. * @param method HTTP method
  129. * @param url URL of the HTTP request
  130. * @param data body of the HTTP request (optional)
  131. * @param data_size length of the body
  132. * @param proc callback function for the result
  133. * @param proc_cls closure for @a proc
  134. * @return #GNUNET_OK if request accepted
  135. */
  136. static enum GNUNET_GenericReturnValue
  137. rest_copying_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
  138. GNUNET_REST_ResultProcessor proc,
  139. void *proc_cls)
  140. {
  141. static const struct GNUNET_REST_RequestHandler handlers[] = {
  142. { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_COPYING, &get_cont },
  143. { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_COPYING, &options_cont },
  144. GNUNET_REST_HANDLER_END
  145. };
  146. struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
  147. struct GNUNET_REST_RequestHandlerError err;
  148. handle->proc_cls = proc_cls;
  149. handle->proc = proc;
  150. handle->rest_handle = conndata_handle;
  151. GNUNET_CONTAINER_DLL_insert (requests_head,
  152. requests_tail,
  153. handle);
  154. return GNUNET_REST_handle_request (conndata_handle,
  155. handlers,
  156. &err,
  157. handle);
  158. }
  159. /**
  160. * Entry point for the plugin.
  161. *
  162. * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
  163. * @return NULL on error, otherwise the plugin context
  164. */
  165. void *
  166. libgnunet_plugin_rest_copying_init (void *cls)
  167. {
  168. static struct Plugin plugin;
  169. cfg = cls;
  170. struct GNUNET_REST_Plugin *api;
  171. if (NULL != plugin.cfg)
  172. return NULL; /* can only initialize once! */
  173. memset (&plugin, 0, sizeof(struct Plugin));
  174. plugin.cfg = cfg;
  175. api = GNUNET_new (struct GNUNET_REST_Plugin);
  176. api->cls = &plugin;
  177. api->name = GNUNET_REST_API_NS_COPYING;
  178. api->process_request = &rest_copying_process_request;
  179. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  180. _ ("COPYING REST API initialized\n"));
  181. return api;
  182. }
  183. /**
  184. * Exit point from the plugin.
  185. *
  186. * @param cls the plugin context (as returned by "init")
  187. * @return always NULL
  188. */
  189. void *
  190. libgnunet_plugin_rest_copying_done (void *cls)
  191. {
  192. struct GNUNET_REST_Plugin *api = cls;
  193. struct Plugin *plugin = api->cls;
  194. while (NULL != requests_head)
  195. cleanup_handle (requests_head);
  196. plugin->cfg = NULL;
  197. GNUNET_free (api);
  198. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  199. "COPYING REST plugin is finished\n");
  200. return NULL;
  201. }
  202. /* end of plugin_rest_copying.c */