GNUnet 0.24.1-15-gab6ed22f1
gnunet_mhd_lib.h File Reference

functions to parse HTTP uploads with MHD More...

#include "gnunet_util_lib.h"
#include <microhttpd.h>
#include <jansson.h>
Include dependency graph for gnunet_mhd_lib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  GNUNET_MHD_PostResult {
  GNUNET_MHD_PR_SUCCESS , GNUNET_MHD_PR_CONTINUE , GNUNET_MHD_PR_OUT_OF_MEMORY , GNUNET_MHD_PR_REQUEST_TOO_LARGE ,
  GNUNET_MHD_PR_JSON_INVALID
}
 Return codes from GNUNET_MHD_post_parser(). More...
 

Functions

enum GNUNET_MHD_PostResult GNUNET_MHD_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_MHD_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 HTTP uploads with MHD

Author
Christian Grothoff

Definition in file gnunet_mhd_lib.h.

Enumeration Type Documentation

◆ GNUNET_MHD_PostResult

Return codes from GNUNET_MHD_post_parser().

Enumerator
GNUNET_MHD_PR_SUCCESS 

Parsing successful, JSON result is in *json.

GNUNET_MHD_PR_CONTINUE 

Parsing continues, call again soon!

GNUNET_MHD_PR_OUT_OF_MEMORY 

Sorry, memory allocation (malloc()) failed.

GNUNET_MHD_PR_REQUEST_TOO_LARGE 

Request size exceeded buffer_max argument.

GNUNET_MHD_PR_JSON_INVALID 

JSON parsing failed.

This was not a JSON upload.

Definition at line 36 of file gnunet_mhd_lib.h.

37{
42
47
52
57
62};
@ GNUNET_MHD_PR_CONTINUE
Parsing continues, call again soon!
@ GNUNET_MHD_PR_SUCCESS
Parsing successful, JSON result is in *json.
@ GNUNET_MHD_PR_OUT_OF_MEMORY
Sorry, memory allocation (malloc()) failed.
@ GNUNET_MHD_PR_REQUEST_TOO_LARGE
Request size exceeded buffer_max argument.
@ GNUNET_MHD_PR_JSON_INVALID
JSON parsing failed.

Function Documentation

◆ GNUNET_MHD_post_parser()

enum GNUNET_MHD_PostResult GNUNET_MHD_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_MHD_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 266 of file mhd_upload.c.

272{
273 struct Buffer *r = *con_cls;
274 const char *ce;
275 int ret;
276
277 *json = NULL;
278 if (NULL == *con_cls)
279 {
280 /* We are seeing a fresh POST request. */
281 r = GNUNET_new (struct Buffer);
282 if (GNUNET_OK != buffer_init (r,
283 upload_data,
284 *upload_data_size,
286 buffer_max))
287 {
288 *con_cls = NULL;
289 buffer_deinit (r);
290 GNUNET_free (r);
292 }
293 /* everything OK, wait for more POST data */
294 *upload_data_size = 0;
295 *con_cls = r;
297 }
298 if (0 != *upload_data_size)
299 {
300 /* We are seeing an old request with more data available. */
301
302 if (GNUNET_OK !=
303 buffer_append (r, upload_data, *upload_data_size, buffer_max))
304 {
305 /* Request too long */
306 *con_cls = NULL;
307 buffer_deinit (r);
308 GNUNET_free (r);
310 }
311 /* everything OK, wait for more POST data */
312 *upload_data_size = 0;
314 }
315
316 /* We have seen the whole request. */
317 ce = MHD_lookup_connection_value (connection,
318 MHD_HEADER_KIND,
319 MHD_HTTP_HEADER_CONTENT_ENCODING);
320 if ((NULL != ce) && (0 == strcasecmp ("deflate", ce)))
321 {
322 ret = inflate_data (r);
324 {
325 buffer_deinit (r);
326 GNUNET_free (r);
327 *con_cls = NULL;
328 return ret;
329 }
330 }
331
332 {
333 json_error_t err;
334
335 *json = json_loadb (r->data,
336 r->fill,
337 0,
338 &err);
339 if (NULL == *json)
340 {
342 "Failed to parse JSON request body of %u byte at offset %d: %s\n",
343 (unsigned int) r->fill,
344 err.position,
345 err.text);
346 buffer_deinit (r);
347 GNUNET_free (r);
348 *con_cls = NULL;
350 }
351 }
352 buffer_deinit (r);
353 GNUNET_free (r);
354 *con_cls = NULL;
355
357}
static int ret
Final status code.
Definition: gnunet-arm.c:93
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_ERROR_TYPE_WARNING
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
static enum GNUNET_MHD_PostResult inflate_data(struct Buffer *buf)
Decompress data in buf.
Definition: mhd_upload.c:157
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: mhd_upload.c:79
#define REQUEST_BUFFER_INITIAL
Initial size for POST request buffers.
Definition: mhd_upload.c:38
static void buffer_deinit(struct Buffer *buf)
Free the data in a buffer.
Definition: mhd_upload.c:105
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: mhd_upload.c:123
Buffer for POST requests.
Definition: mhd_upload.c:45
size_t fill
Number of valid bytes in buffer.
Definition: mhd_upload.c:54
char * data
Allocated memory.
Definition: mhd_upload.c:49

References buffer_append(), buffer_deinit(), buffer_init(), Buffer::data, Buffer::fill, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_MHD_PR_CONTINUE, GNUNET_MHD_PR_JSON_INVALID, GNUNET_MHD_PR_OUT_OF_MEMORY, GNUNET_MHD_PR_REQUEST_TOO_LARGE, GNUNET_MHD_PR_SUCCESS, GNUNET_new, GNUNET_OK, inflate_data(), REQUEST_BUFFER_INITIAL, and ret.

Here is the call graph for this function:

◆ GNUNET_MHD_post_parser_cleanup()

void GNUNET_MHD_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_MHD_post_parser(), to be cleaned up

Definition at line 368 of file mhd_upload.c.

369{
370 struct Buffer *r = con_cls;
371
372 if (NULL != r)
373 {
374 buffer_deinit (r);
375 GNUNET_free (r);
376 }
377}

References buffer_deinit(), and GNUNET_free.

Here is the call graph for this function: