Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http_form.h File Reference

Strict percent-decoding, query strings and urlencoded forms. More...

#include "nilorea/n_str.h"
#include <stddef.h>
Include dependency graph for n_http_form.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  N_HTTP_FIELD
 one decoded name=value pair; both are NUL-terminated and hold no NUL More...
struct  N_HTTP_FORM
 decoded fields, in the order they were sent More...
struct  N_HTTP_FORM_MULTIPART
 a parsed multipart/form-data body More...
struct  N_HTTP_FORM_PART
 one part of a multipart/form-data body; the body is NOT copied More...

Macros

#define N_HTTP_FORM_BOUNDARY_MAX   70
 longest boundary RFC 2046 allows
#define N_HTTP_FORM_PART_FILENAME_MAX   255
 longest multipart filename accepted
#define N_HTTP_FORM_PART_HEAD_MAX   8192
 largest header block of one part
#define N_HTTP_FORM_PART_NAME_MAX   200
 longest multipart field name accepted
#define N_HTTP_FORM_PART_TYPE_MAX   127
 longest part Content-Type kept
#define N_HTTP_FORM_PLUS_SPACE   1
 decode '+' as a space (query strings and urlencoded forms, not paths)
#define N_HTTP_FORM_RAW_BYTES   2
 accept decoded bytes that are not well-formed UTF-8 (NUL stays refused)
#define N_HTTP_FORM_SKIP_INVALID   4
 n_http_form_parse(): drop a pair that fails to decode instead of failing

Functions

size_t n_http_form_count (const N_HTTP_FORM *form, const char *name)
 number of fields named name (exact, case-sensitive)
void n_http_form_free (N_HTTP_FORM **form)
 free a form and set *form to NULL
const char * n_http_form_get (const N_HTTP_FORM *form, const char *name, size_t *value_len)
 value of the LAST field named name, like PHP's $_GET / $_POST
int n_http_form_multipart_boundary (const char *content_type, size_t len, char *out)
 boundary of a multipart/form-data Content-Type, parsed strictly
size_t n_http_form_multipart_count (const N_HTTP_FORM_MULTIPART *mp, const char *name)
 number of parts named name
void n_http_form_multipart_free (N_HTTP_FORM_MULTIPART **mp)
 free a parsed body and set *mp to NULL
const N_HTTP_FORM_PART * n_http_form_multipart_get (const N_HTTP_FORM_MULTIPART *mp, const char *name)
 the LAST part named name (PHP semantics, like n_http_form_get())
N_HTTP_FORM_MULTIPART * n_http_form_multipart_parse (const char *buf, size_t len, const char *boundary, size_t max_parts)
 parse a multipart/form-data body strictly, without copying it
N_HTTP_FORM * n_http_form_parse (const char *s, size_t len, size_t max_fields, int flags)
 decode a query string or an urlencoded body into fields
int n_http_percent_decode (const char *s, size_t len, int flags, N_STR **out)
 percent-decode [s, s+len), appending to *out

Detailed Description

Strict percent-decoding, query strings and urlencoded forms.

Server-side decoding of what a browser sends: a percent-encoded path segment, a query string, or an application/x-www-form-urlencoded body. It faces hostile input, so it is strict where the classic decoders are lenient: a malformed escape ("%", "%4", "%zz") is an error, not a literal; a decoded NUL byte is always an error; decoded text must be well-formed UTF-8 unless the caller asks for raw bytes; and the number of fields is bounded by the caller before anything is allocated per field.

A query string from the wild often carries junk the page does not use (tracking parameters, truncated escapes). N_HTTP_FORM_SKIP_INVALID drops such pairs instead of refusing the whole string; a form the server itself rendered should be parsed without it.

Author
Castagnier Mickael
Version
1.0
Date
2026

Definition in file n_http_form.h.