Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http_form.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
41
42#ifndef __NILOREA_HTTP_FORM_HEADER
43#define __NILOREA_HTTP_FORM_HEADER
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
53
54#include "nilorea/n_str.h"
55
56#include <stddef.h>
57
59#define N_HTTP_FORM_PLUS_SPACE 1
61#define N_HTTP_FORM_RAW_BYTES 2
63#define N_HTTP_FORM_SKIP_INVALID 4
64
66typedef struct N_HTTP_FIELD {
68 char* name;
70 size_t name_len;
72 char* value;
74 size_t value_len;
76
84
96int n_http_percent_decode(const char* s, size_t len, int flags, N_STR** out);
97
109N_HTTP_FORM* n_http_form_parse(const char* s, size_t len, size_t max_fields, int flags);
110
120const char* n_http_form_get(const N_HTTP_FORM* form, const char* name, size_t* value_len);
121
126size_t n_http_form_count(const N_HTTP_FORM* form, const char* name);
127
130void n_http_form_free(N_HTTP_FORM** form);
131
133#define N_HTTP_FORM_BOUNDARY_MAX 70
135#define N_HTTP_FORM_PART_NAME_MAX 200
137#define N_HTTP_FORM_PART_FILENAME_MAX 255
139#define N_HTTP_FORM_PART_TYPE_MAX 127
141#define N_HTTP_FORM_PART_HEAD_MAX 8192
142
159
167
178int n_http_form_multipart_boundary(const char* content_type, size_t len, char* out);
179
196N_HTTP_FORM_MULTIPART* n_http_form_multipart_parse(const char* buf, size_t len, const char* boundary, size_t max_parts);
197
202const N_HTTP_FORM_PART* n_http_form_multipart_get(const N_HTTP_FORM_MULTIPART* mp, const char* name);
203
208size_t n_http_form_multipart_count(const N_HTTP_FORM_MULTIPART* mp, const char* name);
209
213
217
218#ifdef __cplusplus
219}
220#endif
221
222#endif /* __NILOREA_HTTP_FORM_HEADER */
size_t nb_parts
number of parts
size_t body_len
body length
size_t value_len
length of value
Definition n_http_form.h:74
char content_type[127+1]
the part's Content-Type as sent, "" when absent: untrusted
char * value
decoded value, "" for "name" or "name="
Definition n_http_form.h:72
int has_filename
non-zero when the part carried a filename parameter (a file field)
char * name
decoded name
Definition n_http_form.h:68
size_t name_len
length of name
Definition n_http_form.h:70
size_t body_off
body offset in the parsed buffer
N_HTTP_FIELD * fields
the fields, nb_fields of them
Definition n_http_form.h:80
char name[200+1]
field name (non-empty, UTF-8, no control byte)
char filename[255+1]
the filename as sent (UTF-8, no control byte, may be ""): untrusted, never use it as a path
N_HTTP_FORM_PART * parts
the parts, in order
size_t nb_fields
number of fields
Definition n_http_form.h:82
#define N_HTTP_FORM_PART_TYPE_MAX
longest part Content-Type kept
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
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
void n_http_form_multipart_free(N_HTTP_FORM_MULTIPART **mp)
free a parsed body and set *mp to NULL
size_t n_http_form_count(const N_HTTP_FORM *form, const char *name)
number of fields named name (exact, case-sensitive)
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())
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
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
size_t n_http_form_multipart_count(const N_HTTP_FORM_MULTIPART *mp, const char *name)
number of parts named name
#define N_HTTP_FORM_PART_NAME_MAX
longest multipart field name accepted
#define N_HTTP_FORM_PART_FILENAME_MAX
longest multipart filename accepted
void n_http_form_free(N_HTTP_FORM **form)
free a form and set *form to NULL
one decoded name=value pair; both are NUL-terminated and hold no NUL
Definition n_http_form.h:66
decoded fields, in the order they were sent
Definition n_http_form.h:78
a parsed multipart/form-data body
one part of a multipart/form-data body; the body is NOT copied
A box including a string and his lenght.
Definition n_str.h:61
N_STR and string function declaration.