cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
multipart.c File Reference

RFC 7578 multipart/form-data parser - wrapper around multipart-parser-c. More...

#include <cwist/net/http/multipart.h>
#include <cwist/core/mem/alloc.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <ctype.h>
#include "multipart_parser.h"
Include dependency graph for multipart.c:

Go to the source code of this file.

Functions

cwist_multipart_result * cwist_multipart_parse (const char *body, size_t body_len, const char *boundary)
 Parse a multipart/form-data body into a field list.
 
void cwist_multipart_result_destroy (cwist_multipart_result *result)
 Destroy a parse result and everything it owns.
 
char * cwist_multipart_extract_boundary (const char *content_type)
 Extract the boundary parameter from a Content-Type header value.
 

Detailed Description

RFC 7578 multipart/form-data parser - wrapper around multipart-parser-c.

Definition in file multipart.c.

Function Documentation

◆ cwist_multipart_extract_boundary()

char * cwist_multipart_extract_boundary ( const char *  content_type)

Extract the boundary parameter from a Content-Type header value.

Extract the boundary string from a Content-Type header value.

Scans semicolon-separated parameters of content_type, skipping optional whitespace, and accepts both quoted and unquoted boundary values.

Parameters
content_typeContent-Type header value, e.g. from the request; may be NULL.
Returns
Newly allocated NUL-terminated boundary string (caller frees with cwist_free), or NULL if no boundary parameter is present or allocation failed.

Definition at line 363 of file multipart.c.

References cap, and cwist_alloc().

◆ cwist_multipart_parse()

cwist_multipart_result * cwist_multipart_parse ( const char *  body,
size_t  body_len,
const char *  boundary 
)

Parse a multipart/form-data body into a field list.

Parse a multipart/form-data body.

Runs the multipart-parser-c state machine over body with boundary (the boundary without leading dashes). On malformed or truncated input the parser stops early and NULL is returned, with all intermediate state freed.

Parameters
bodyRaw body bytes; must not be NULL.
body_lenLength of body in bytes.
boundaryMIME boundary string, without the leading "--".
Returns
Newly allocated cwist_multipart_result on success (caller owns it, release with cwist_multipart_result_destroy), NULL on invalid arguments, malformed input, or allocation failure.

Definition at line 269 of file multipart.c.

References cap, cwist_alloc(), cwist_free(), cwist_multipart_result_destroy(), CWIST_SCRATCH_DEFER, and cwist_multipart_result::fields.

◆ cwist_multipart_result_destroy()

void cwist_multipart_result_destroy ( cwist_multipart_result *  result)

Destroy a parse result and everything it owns.

Free all memory associated with a multipart parse result.

Frees every field's name, filename, content_type, and data, then the field nodes and the result itself. Safe to call with NULL.

Parameters
resultResult returned by cwist_multipart_parse, or NULL.

Definition at line 336 of file multipart.c.

References cap, cwist_free(), cwist_multipart_result::fields, and cwist_multipart_field::next.

Referenced by cwist_multipart_parse().