GNUnet  0.11.x
Data Structures | Macros | Functions
json_mhd.c File Reference

functions to parse JSON snippets we receive via MHD More...

#include "platform.h"
#include "gnunet_json_lib.h"
#include <zlib.h>
Include dependency graph for json_mhd.c:

Go to the source code of this file.

Data Structures

struct  Buffer
 Buffer for POST requests. More...
 

Macros

#define REQUEST_BUFFER_INITIAL   (2 * 1024)
 Initial size for POST request buffers. More...
 

Functions

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. More...
 
static void buffer_deinit (struct Buffer *buf)
 Free the data in a buffer. More...
 
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. More...
 
static enum GNUNET_JSON_PostResult inflate_data (struct Buffer *buf)
 Decompress data in buf. More...
 
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. More...
 
void GNUNET_JSON_post_parser_cleanup (void *con_cls)
 Function called whenever we are done with a request to clean up our state. More...
 

Detailed Description

functions to parse JSON snippets we receive via MHD

Author
Florian Dold
Benedikt Mueller
Christian Grothoff

Definition in file json_mhd.c.

Macro Definition Documentation

◆ REQUEST_BUFFER_INITIAL

#define REQUEST_BUFFER_INITIAL   (2 * 1024)

Initial size for POST request buffers.

Should be big enough to usually not require a reallocation, but not so big that it hurts in terms of memory use.

Definition at line 37 of file json_mhd.c.

Referenced by GNUNET_JSON_post_parser().

Function Documentation

◆ buffer_init()

static int buffer_init ( struct Buffer buf,
const void *  data,
size_t  data_size,
size_t  alloc_size,
size_t  max_size 
)
static

Initialize a buffer.

Parameters
bufthe buffer to initialize
datathe initial data
data_sizesize of the initial data
alloc_sizesize of the buffer
max_sizemaximum size that the buffer can grow to
Returns
a GNUnet result code

Definition at line 78 of file json_mhd.c.

References Buffer::alloc, Buffer::data, data_size, Buffer::fill, GNUNET_malloc, GNUNET_memcpy, GNUNET_OK, GNUNET_SYSERR, Buffer::max, and max_size.

Referenced by GNUNET_JSON_post_parser().

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;
91  buf->fill = data_size;
92  buf->max = max_size;
93  return GNUNET_OK;
94 }
size_t alloc
Number of allocated bytes in buffer.
Definition: json_mhd.c:58
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
size_t fill
Number of valid bytes in buffer.
Definition: json_mhd.c:53
size_t max
Maximum buffer size allowed.
Definition: json_mhd.c:63
char * data
Allocated memory.
Definition: json_mhd.c:48
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static size_t max_size
The maximal size the extended sampler elements should grow to.
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the caller graph for this function:

◆ buffer_deinit()

static void buffer_deinit ( struct Buffer buf)
static

Free the data in a buffer.

Does not free the buffer object itself.

Parameters
bufbuffer to de-initialize

Definition at line 104 of file json_mhd.c.

References Buffer::data, and GNUNET_free.

Referenced by GNUNET_JSON_post_parser(), and GNUNET_JSON_post_parser_cleanup().

105 {
106  GNUNET_free (buf->data);
107  buf->data = NULL;
108 }
char * data
Allocated memory.
Definition: json_mhd.c:48
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ buffer_append()

static int buffer_append ( struct Buffer buf,
const void *  data,
size_t  data_size,
size_t  max_size 
)
static

Append data to a buffer, growing the buffer if necessary.

Parameters
bufthe buffer to append to
datathe data to append
data_sizethe size of data
max_sizemaximum size that the buffer can grow to
Returns
GNUNET_OK on success, GNUNET_NO if the buffer can't accomodate for the new data

Definition at line 122 of file json_mhd.c.

References Buffer::alloc, Buffer::data, data_size, Buffer::fill, GNUNET_free, GNUNET_malloc, GNUNET_memcpy, GNUNET_NO, and GNUNET_OK.

Referenced by GNUNET_JSON_post_parser().

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 }
size_t alloc
Number of allocated bytes in buffer.
Definition: json_mhd.c:58
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
size_t fill
Number of valid bytes in buffer.
Definition: json_mhd.c:53
char * data
Allocated memory.
Definition: json_mhd.c:48
static size_t max_size
The maximal size the extended sampler elements should grow to.
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ inflate_data()

static enum GNUNET_JSON_PostResult inflate_data ( struct Buffer buf)
static

Decompress data in buf.

Parameters
bufinput data to inflate
Returns
result code indicating the status of the operation

Definition at line 156 of file json_mhd.c.

References Buffer::alloc, Buffer::data, Buffer::fill, GNUNET_break, GNUNET_break_op, GNUNET_free, GNUNET_JSON_PR_JSON_INVALID, GNUNET_JSON_PR_OUT_OF_MEMORY, GNUNET_JSON_PR_SUCCESS, GNUNET_malloc, GNUNET_MIN, GNUNET_realloc, Buffer::max, and ret.

Referenced by GNUNET_JSON_post_parser().

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 
177  case Z_STREAM_ERROR:
178  GNUNET_break_op (0);
180 
181  case Z_OK:
182  break;
183  }
184  while (1)
185  {
186  ret = inflate (&z, 0);
187  switch (ret)
188  {
189  case Z_MEM_ERROR:
190  GNUNET_break (0);
191  GNUNET_break (Z_OK == inflateEnd (&z));
192  GNUNET_free (tmp);
194 
195  case Z_DATA_ERROR:
196  GNUNET_break (0);
197  GNUNET_break (Z_OK == inflateEnd (&z));
198  GNUNET_free (tmp);
200 
201  case Z_NEED_DICT:
202  GNUNET_break (0);
203  GNUNET_break (Z_OK == inflateEnd (&z));
204  GNUNET_free (tmp);
206 
207  case Z_OK:
208  if ((0 < z.avail_out) && (0 == z.avail_in))
209  {
210  /* truncated input stream */
211  GNUNET_break (0);
212  GNUNET_break (Z_OK == inflateEnd (&z));
213  GNUNET_free (tmp);
215  }
216  if (0 < z.avail_out)
217  continue; /* just call it again */
218  /* output buffer full, can we grow it? */
219  if (tmp_size == buf->max)
220  {
221  /* already at max */
222  GNUNET_break (0);
223  GNUNET_break (Z_OK == inflateEnd (&z));
224  GNUNET_free (tmp);
226  }
227  if (tmp_size * 2 < tmp_size)
228  tmp_size = buf->max;
229  else
230  tmp_size = GNUNET_MIN (buf->max, tmp_size * 2);
231  tmp = GNUNET_realloc (tmp, tmp_size);
232  z.next_out = (Bytef *) &tmp[z.total_out];
233  continue;
234 
235  case Z_STREAM_END:
236  /* decompression successful, make 'tmp' the new 'data' */
237  GNUNET_free (buf->data);
238  buf->data = tmp;
239  buf->alloc = tmp_size;
240  buf->fill = z.total_out;
241  GNUNET_break (Z_OK == inflateEnd (&z));
242  return GNUNET_JSON_PR_SUCCESS; /* at least for now */
243  }
244  } /* while (1) */
245 }
JSON parsing failed.
size_t alloc
Number of allocated bytes in buffer.
Definition: json_mhd.c:58
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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.
#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:80
Sorry, memory allocation (malloc()) failed.
size_t max
Maximum buffer size allowed.
Definition: json_mhd.c:63
char * data
Allocated memory.
Definition: json_mhd.c:48
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GNUNET_JSON_post_parser()

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.

This function realizes an MHD POST processor that will (incrementally) process JSON data uploaded to the HTTP server. It will store the required state in the con_cls, which must be cleaned up using #GNUNET_JSON_post_parser_callback().

Parameters
buffer_maxmaximum allowed size for the buffer
connectionMHD connection handle (for meta data about the upload)
con_clsthe closure (will point to a struct Buffer *)
upload_datathe POST data
upload_data_sizenumber of bytes in upload_data
jsonthe JSON object for a completed request
Returns
result code indicating the status of the operation

Definition at line 264 of file json_mhd.c.

References buffer_append(), buffer_deinit(), buffer_init(), Buffer::data, Buffer::fill, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_JSON_PR_CONTINUE, GNUNET_JSON_PR_JSON_INVALID, GNUNET_JSON_PR_OUT_OF_MEMORY, GNUNET_JSON_PR_REQUEST_TOO_LARGE, GNUNET_JSON_PR_SUCCESS, GNUNET_log, GNUNET_new, GNUNET_OK, inflate_data(), REQUEST_BUFFER_INITIAL, and ret.

270 {
271  struct Buffer *r = *con_cls;
272  const char *ce;
273  int ret;
274 
275  *json = NULL;
276  if (NULL == *con_cls)
277  {
278  /* We are seeing a fresh POST request. */
279  r = GNUNET_new (struct Buffer);
280  if (GNUNET_OK != buffer_init (r,
281  upload_data,
282  *upload_data_size,
284  buffer_max))
285  {
286  *con_cls = NULL;
287  buffer_deinit (r);
288  GNUNET_free (r);
290  }
291  /* everything OK, wait for more POST data */
292  *upload_data_size = 0;
293  *con_cls = r;
295  }
296  if (0 != *upload_data_size)
297  {
298  /* We are seeing an old request with more data available. */
299 
300  if (GNUNET_OK !=
301  buffer_append (r, upload_data, *upload_data_size, buffer_max))
302  {
303  /* Request too long */
304  *con_cls = NULL;
305  buffer_deinit (r);
306  GNUNET_free (r);
308  }
309  /* everything OK, wait for more POST data */
310  *upload_data_size = 0;
312  }
313 
314  /* We have seen the whole request. */
315  ce = MHD_lookup_connection_value (connection,
316  MHD_HEADER_KIND,
317  MHD_HTTP_HEADER_CONTENT_ENCODING);
318  if ((NULL != ce) && (0 == strcasecmp ("deflate", ce)))
319  {
320  ret = inflate_data (r);
321  if (GNUNET_JSON_PR_SUCCESS != ret)
322  {
323  buffer_deinit (r);
324  GNUNET_free (r);
325  *con_cls = NULL;
326  return ret;
327  }
328  }
329 
330  *json = json_loadb (r->data, r->fill, 0, NULL);
331  if (NULL == *json)
332  {
334  "Failed to parse JSON request body\n");
335  buffer_deinit (r);
336  GNUNET_free (r);
337  *con_cls = NULL;
339  }
340  buffer_deinit (r);
341  GNUNET_free (r);
342  *con_cls = NULL;
343 
344  return GNUNET_JSON_PR_SUCCESS;
345 }
JSON parsing failed.
static enum GNUNET_JSON_PostResult inflate_data(struct Buffer *buf)
Decompress data in buf.
Definition: json_mhd.c:156
Buffer for POST requests.
Definition: json_mhd.c:43
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
size_t fill
Number of valid bytes in buffer.
Definition: json_mhd.c:53
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
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
char * data
Allocated memory.
Definition: json_mhd.c:48
#define REQUEST_BUFFER_INITIAL
Initial size for POST request buffers.
Definition: json_mhd.c:37
Parsing continues, call again soon!
#define GNUNET_log(kind,...)
static void buffer_deinit(struct Buffer *buf)
Free the data in a buffer.
Definition: json_mhd.c:104
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:

◆ GNUNET_JSON_post_parser_cleanup()

void GNUNET_JSON_post_parser_cleanup ( void *  con_cls)

Function called whenever we are done with a request to clean up our state.

Parameters
con_clsvalue as it was left by GNUNET_JSON_post_parser(), to be cleaned up

Definition at line 356 of file json_mhd.c.

References buffer_deinit(), and GNUNET_free.

357 {
358  struct Buffer *r = con_cls;
359 
360  if (NULL != r)
361  {
362  buffer_deinit (r);
363  GNUNET_free (r);
364  }
365 }
Buffer for POST requests.
Definition: json_mhd.c:43
static void buffer_deinit(struct Buffer *buf)
Free the data in a buffer.
Definition: json_mhd.c:104
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function: