GNUnet  0.11.x
rest.c
Go to the documentation of this file.
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  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 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_rest_lib.h"
30 #include "microhttpd.h"
31 
43 int
44 GNUNET_REST_namespace_match (const char *url, const char *namespace)
45 {
46  return 0 == strncmp (namespace, url, strlen (namespace));
47 }
48 
49 
56 struct MHD_Response*
58 {
59  struct MHD_Response *resp;
60  size_t len;
61 
62  if (NULL == data)
63  {
64  len = 0;
65  data = "";
66  }
67  else
68  len = strlen (data);
69  resp = MHD_create_response_from_buffer (len,
70  (void*) data,
71  MHD_RESPMEM_MUST_COPY);
72  return resp;
73 }
74 
75 
76 int
80  void *cls)
81 {
82  int count;
83  int i;
84  char *url;
85 
86  count = 0;
87  while (NULL != handlers[count].method)
88  count++;
89 
90  GNUNET_asprintf (&url, "%s", conn->url);
91  if (url[strlen (url) - 1] == '/')
92  url[strlen (url) - 1] = '\0';
93  for (i = 0; i < count; i++)
94  {
95  if (0 != strcasecmp (conn->method, handlers[i].method))
96  continue;
97  if (strlen (url) < strlen (handlers[i].namespace))
98  continue;
99  if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
100  continue;
101  // Match
102  handlers[i].proc (conn, (const char*) url, cls);
103  GNUNET_free (url);
104  return GNUNET_YES;
105  }
106  GNUNET_free (url);
107  err->error_code = MHD_HTTP_BAD_REQUEST;
108  return GNUNET_NO;
109 }
110 
111 
112 /* end of rest.c */
const char * method
Http method to handle.
#define GNUNET_NO
Definition: gnunet_common.h:78
void(* proc)(struct GNUNET_REST_RequestHandle *handle, const char *url, void *cls)
Namespace to handle.
int GNUNET_REST_namespace_match(const char *url, const char *namespace)
REST Utilities.
Definition: rest.c:44
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
const char * url
The url as string.
const char * method
The HTTP method as MHD value (see microhttpd.h)
int GNUNET_REST_handle_request(struct GNUNET_REST_RequestHandle *conn, const struct GNUNET_REST_RequestHandler *handlers, struct GNUNET_REST_RequestHandlerError *err, void *cls)
Definition: rest.c:77
struct MHD_Response * GNUNET_REST_create_response(const char *data)
Create MHD response.
Definition: rest.c:57
#define GNUNET_YES
Definition: gnunet_common.h:77
uint32_t data
The data value.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...