glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / rest / rest.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2010-2015 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
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 /**
28  * REST Utilities
29  */
30
31   /**
32    * Check if namespace is in URL.
33    *
34    * @param url URL to check
35    * @param namespace namespace to check against
36    * @retun GNUNET_YES if namespace matches
37    */
38 int
39 GNUNET_REST_namespace_match (const char *url, const char *namespace)
40 {
41   return 0 == strncmp (namespace, url, strlen (namespace));
42 }
43
44 /**
45  * Create MHD response
46  *
47  * @param data result
48  * @retun MHD response
49  */
50 struct MHD_Response*
51 GNUNET_REST_create_response (const char *data)
52 {
53   struct MHD_Response *resp;
54   size_t len;
55
56   if (NULL == data)
57   {
58     len = 0;
59     data = "";
60   }
61   else
62     len = strlen (data);
63   resp = MHD_create_response_from_buffer (len,
64                                           (void*)data,
65                                           MHD_RESPMEM_MUST_COPY);
66   return resp;
67
68 }
69
70 int
71 GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn,
72                             const struct GNUNET_REST_RequestHandler *handlers,
73                             struct GNUNET_REST_RequestHandlerError *err,
74                             void *cls)
75 {
76   int count;
77   int i;
78   char *url;
79
80   count = 0;
81   while (NULL != handlers[count].method)
82     count++;
83
84   GNUNET_asprintf (&url, "%s", conn->url);
85   if (url[strlen (url)-1] == '/')
86     url[strlen (url)-1] = '\0';
87   for (i = 0; i < count; i++)
88   {
89     if (0 != strcasecmp (conn->method, handlers[i].method))
90       continue;
91     if (strlen (url) < strlen (handlers[i].namespace))
92       continue;
93     if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
94       continue;
95     //Match
96     handlers[i].proc (conn, (const char*)url, cls);
97     GNUNET_free (url);
98     return GNUNET_YES;
99   }
100   GNUNET_free (url);
101   err->error_code = MHD_HTTP_BAD_REQUEST;
102   return GNUNET_NO;
103 }
104
105 /* end of rest.c */