rest.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2010-2015 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. * @file rest/rest.c
  18. * @brief helper library to create JSON REST Objects and handle REST
  19. * responses/requests.
  20. * @author Martin Schanzenbach
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_rest_lib.h"
  25. #include "microhttpd.h"
  26. /**
  27. * REST Utilities
  28. */
  29. /**
  30. * Check if namespace is in URL.
  31. *
  32. * @param url URL to check
  33. * @param namespace namespace to check against
  34. * @retun GNUNET_YES if namespace matches
  35. */
  36. int
  37. GNUNET_REST_namespace_match (const char *url, const char *namespace)
  38. {
  39. return 0 == strncmp (namespace, url, strlen (namespace));
  40. }
  41. /**
  42. * Create MHD response
  43. *
  44. * @param data result
  45. * @retun MHD response
  46. */
  47. struct MHD_Response*
  48. GNUNET_REST_create_response (const char *data)
  49. {
  50. struct MHD_Response *resp;
  51. size_t len;
  52. if (NULL == data)
  53. {
  54. len = 0;
  55. data = "";
  56. }
  57. else
  58. len = strlen (data);
  59. resp = MHD_create_response_from_buffer (len,
  60. (void*)data,
  61. MHD_RESPMEM_MUST_COPY);
  62. return resp;
  63. }
  64. int
  65. GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn,
  66. const struct GNUNET_REST_RequestHandler *handlers,
  67. struct GNUNET_REST_RequestHandlerError *err,
  68. void *cls)
  69. {
  70. int count;
  71. int i;
  72. char *url;
  73. count = 0;
  74. while (NULL != handlers[count].method)
  75. count++;
  76. GNUNET_asprintf (&url, "%s", conn->url);
  77. if (url[strlen (url)-1] == '/')
  78. url[strlen (url)-1] = '\0';
  79. for (i = 0; i < count; i++)
  80. {
  81. if (0 != strcasecmp (conn->method, handlers[i].method))
  82. continue;
  83. if (strlen (url) < strlen (handlers[i].namespace))
  84. continue;
  85. if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
  86. continue;
  87. //Match
  88. handlers[i].proc (conn, (const char*)url, cls);
  89. GNUNET_free (url);
  90. return GNUNET_YES;
  91. }
  92. GNUNET_free (url);
  93. err->error_code = MHD_HTTP_BAD_REQUEST;
  94. return GNUNET_NO;
  95. }
  96. /* end of rest.c */