GNUnet  0.10.x
json_mhd.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2014, 2015, 2016 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 */
27 #include "platform.h"
28 #include "gnunet_json_lib.h"
29 #include <zlib.h>
30 
31 
37 #define REQUEST_BUFFER_INITIAL (2 * 1024)
38 
39 
43 struct Buffer
44 {
48  char *data;
49 
53  size_t fill;
54 
58  size_t alloc;
59 
63  size_t max;
64 };
65 
66 
77 static int
79  const void *data,
80  size_t data_size,
81  size_t alloc_size,
82  size_t max_size)
83 {
84  if ((data_size > max_size) || (alloc_size > max_size))
85  return GNUNET_SYSERR;
86  if (data_size > alloc_size)
87  alloc_size = data_size;
88  buf->data = GNUNET_malloc (alloc_size);
89  buf->alloc = alloc_size;
90  GNUNET_memcpy (buf->data, data, data_size);
91  buf->fill = data_size;
92  buf->max = max_size;
93  return GNUNET_OK;
94 }
95 
96 
103 static void
105 {
106  GNUNET_free (buf->data);
107  buf->data = NULL;
108 }
109 
110 
121 static int
123  const void *data,
124  size_t data_size,
125  size_t max_size)
126 {
127  if (buf->fill + data_size > max_size)
128  return GNUNET_NO;
129  if (buf->fill + data_size > buf->alloc)
130  {
131  char *new_buf;
132  size_t new_size = buf->alloc;
133  while (new_size < buf->fill + data_size)
134  new_size += 2;
135  if (new_size > max_size)
136  return GNUNET_NO;
137  new_buf = GNUNET_malloc (new_size);
138  GNUNET_memcpy (new_buf, buf->data, buf->fill);
139  GNUNET_free (buf->data);
140  buf->data = new_buf;
141  buf->alloc = new_size;
142  }
143  GNUNET_memcpy (buf->data + buf->fill, data, data_size);
144  buf->fill += data_size;
145  return GNUNET_OK;
146 }
147 
148 
155 static enum GNUNET_JSON_PostResult
157 {
158  z_stream z;
159  char *tmp;
160  size_t tmp_size;
161  int ret;
162 
163  memset (&z, 0, sizeof (z));
164  z.next_in = (Bytef *) buf->data;
165  z.avail_in = buf->fill;
166  tmp_size = GNUNET_MIN (buf->max, buf->fill * 4);
167  tmp = GNUNET_malloc (tmp_size);
168  z.next_out = (Bytef *) tmp;
169  z.avail_out = tmp_size;
170  ret = inflateInit (&z);
171  switch (ret)
172  {
173  case Z_MEM_ERROR:
174  GNUNET_break (0);
176  case Z_STREAM_ERROR:
177  GNUNET_break_op (0);
179  case Z_OK:
180  break;
181  }
182  while (1)
183  {
184  ret = inflate (&z, 0);
185  switch (ret)
186  {
187  case Z_MEM_ERROR:
188  GNUNET_break (0);
189  GNUNET_break (Z_OK == inflateEnd (&z));
190  GNUNET_free (tmp);
192  case Z_DATA_ERROR:
193  GNUNET_break (0);
194  GNUNET_break (Z_OK == inflateEnd (&z));
195  GNUNET_free (tmp);
197  case Z_NEED_DICT:
198  GNUNET_break (0);
199  GNUNET_break (Z_OK == inflateEnd (&z));
200  GNUNET_free (tmp);
202  case Z_OK:
203  if ((0 < z.avail_out) && (0 == z.avail_in))
204  {
205  /* truncated input stream */
206  GNUNET_break (0);
207  GNUNET_break (Z_OK == inflateEnd (&z));
208  GNUNET_free (tmp);
210  }
211  if (0 < z.avail_out)
212  continue; /* just call it again */
213  /* output buffer full, can we grow it? */
214  if (tmp_size == buf->max)
215  {
216  /* already at max */
217  GNUNET_break (0);
218  GNUNET_break (Z_OK == inflateEnd (&z));
219  GNUNET_free (tmp);
221  }
222  if (tmp_size * 2 < tmp_size)
223  tmp_size = buf->max;
224  else
225  tmp_size = GNUNET_MIN (buf->max, tmp_size * 2);
226  tmp = GNUNET_realloc (tmp, tmp_size);
227  z.next_out = (Bytef *) &tmp[z.total_out];
228  continue;
229  case Z_STREAM_END:
230  /* decompression successful, make 'tmp' the new 'data' */
231  GNUNET_free (buf->data);
232  buf->data = tmp;
233  buf->alloc = tmp_size;
234  buf->fill = z.total_out;
235  GNUNET_break (Z_OK == inflateEnd (&z));
236  return GNUNET_JSON_PR_SUCCESS; /* at least for now */
237  }
238  } /* while (1) */
239 }
240 
241 
258 GNUNET_JSON_post_parser (size_t buffer_max,
259  struct MHD_Connection *connection,
260  void **con_cls,
261  const char *upload_data,
262  size_t *upload_data_size,
263  json_t **json)
264 {
265  struct Buffer *r = *con_cls;
266  const char *ce;
267  int ret;
268 
269  *json = NULL;
270  if (NULL == *con_cls)
271  {
272 
273  /* We are seeing a fresh POST request. */
274  r = GNUNET_new (struct Buffer);
275  if (GNUNET_OK != buffer_init (r,
276  upload_data,
277  *upload_data_size,
279  buffer_max))
280  {
281  *con_cls = NULL;
282  buffer_deinit (r);
283  GNUNET_free (r);
285  }
286  /* everything OK, wait for more POST data */
287  *upload_data_size = 0;
288  *con_cls = r;
290  }
291  if (0 != *upload_data_size)
292  {
293  /* We are seeing an old request with more data available. */
294 
295  if (GNUNET_OK !=
296  buffer_append (r, upload_data, *upload_data_size, buffer_max))
297  {
298  /* Request too long */
299  *con_cls = NULL;
300  buffer_deinit (r);
301  GNUNET_free (r);
303  }
304  /* everything OK, wait for more POST data */
305  *upload_data_size = 0;
307  }
308 
309  /* We have seen the whole request. */
310  ce = MHD_lookup_connection_value (connection,
311  MHD_HEADER_KIND,
312  MHD_HTTP_HEADER_CONTENT_ENCODING);
313  if ((NULL != ce) && (0 == strcasecmp ("deflate", ce)))
314  {
315  ret = inflate_data (r);
316  if (GNUNET_JSON_PR_SUCCESS != ret)
317  {
318  buffer_deinit (r);
319  GNUNET_free (r);
320  *con_cls = NULL;
321  return ret;
322  }
323  }
324 
325  *json = json_loadb (r->data, r->fill, 0, NULL);
326  if (NULL == *json)
327  {
329  "Failed to parse JSON request body\n");
330  buffer_deinit (r);
331  GNUNET_free (r);
332  *con_cls = NULL;
334  }
335  buffer_deinit (r);
336  GNUNET_free (r);
337  *con_cls = NULL;
338 
339  return GNUNET_JSON_PR_SUCCESS;
340 }
341 
342 
350 void
352 {
353  struct Buffer *r = con_cls;
354 
355  if (NULL != r)
356  {
357  buffer_deinit (r);
358  GNUNET_free (r);
359  }
360 }
361 
362 /* end of mhd_json.c */
JSON parsing failed.
static enum GNUNET_JSON_PostResult inflate_data(struct Buffer *buf)
Decompress data in buf.
Definition: json_mhd.c:156
functions to parse JSON objects into GNUnet objects
size_t alloc
Number of allocated bytes in buffer.
Definition: json_mhd.c:58
Buffer for POST requests.
Definition: json_mhd.c:43
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int ret
Final status code.
Definition: gnunet-arm.c:89
size_t fill
Number of valid bytes in buffer.
Definition: json_mhd.c:53
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
Parsing successful, JSON result is in *json.
static int buffer_init(struct Buffer *buf, const void *data, size_t data_size, size_t alloc_size, size_t max_size)
Initialize a buffer.
Definition: json_mhd.c:78
#define GNUNET_memcpy(dst, src, n)
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:83
static char buf[2048]
Sorry, memory allocation (malloc()) failed.
Request size exceeded buffer_max argument.
static int buffer_append(struct Buffer *buf, const void *data, size_t data_size, size_t max_size)
Append data to a buffer, growing the buffer if necessary.
Definition: json_mhd.c:122
size_t max
Maximum buffer size allowed.
Definition: json_mhd.c:63
char * data
Allocated memory.
Definition: json_mhd.c:48
#define REQUEST_BUFFER_INITIAL
Initial size for POST request buffers.
Definition: json_mhd.c:37
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static size_t max_size
The maximal size the extended sampler elements should grow to.
GNUNET_JSON_PostResult
Return codes from GNUNET_JSON_post_parser().
Parsing continues, call again soon!
#define GNUNET_log(kind,...)
void GNUNET_JSON_post_parser_cleanup(void *con_cls)
Function called whenever we are done with a request to clean up our state.
Definition: json_mhd.c:351
enum GNUNET_JSON_PostResult GNUNET_JSON_post_parser(size_t buffer_max, struct MHD_Connection *connection, void **con_cls, const char *upload_data, size_t *upload_data_size, json_t **json)
Process a POST request containing a JSON object.
Definition: json_mhd.c:258
static void buffer_deinit(struct Buffer *buf)
Free the data in a buffer.
Definition: json_mhd.c:104
static size_t data_size
Number of bytes in data.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.