32 return (c >=
'A' && c <=
'Z') ? c -
'A' +
'a' : c;
37static int name_eq(
const char* s,
size_t n,
const char* lit) {
39 for (i = 0; i < n; i++)
40 if (lit[i] ==
'\0' ||
lc((
unsigned char)s[i]) != lit[i])
42 return lit[n] ==
'\0';
46static int name_ieq(
const char* a,
const char* b,
size_t n) {
48 for (i = 0; i < n; i++)
49 if (
lc((
unsigned char)a[i]) !=
lc((
unsigned char)b[i]))
62 for (i = 0; i < n; i++) {
63 unsigned char c = (
unsigned char)s[i];
64 if ((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || (c >=
'0' && c <=
'9'))
66 if (!strchr(
"!#$%&'*+-.^_`|~", c) || c ==
'\0')
77 for (i = 0; i < n; i++) {
78 unsigned char c = (
unsigned char)s[i];
79 if ((c < 0x20 && c !=
'\t') || c == 0x7f)
88static int parse_body_len(
const char* s,
size_t n,
long long max,
long long* out) {
89 unsigned long long v = 0;
93 for (i = 0; i < n; i++) {
94 if (s[i] <
'0' || s[i] >
'9')
96 v = v * 10u + (unsigned)(s[i] -
'0');
97 if (v > (
unsigned long long)max)
107 return c > 0x20 && c < 0x7f && c !=
'#';
111 size_t scan, i, sl, p, tstart, tend, line_start, nheaders = 0, nhost = 0;
114 if (!buf || !out || !limits)
116 memset(out, 0,
sizeof(*out));
132 for (i = 0; i + 3 < scan; i++)
133 if (buf[i] ==
'\r' && buf[i + 1] ==
'\n' && buf[i + 2] ==
'\r' &&
134 buf[i + 3] ==
'\n') {
144 while (sl + 1 < out->
head_len && !(buf[sl] ==
'\r' && buf[sl + 1] ==
'\n'))
146 if (sl >= 4 && memcmp(buf,
"GET ", 4) == 0) {
149 }
else if (sl >= 5 && memcmp(buf,
"POST ", 5) == 0) {
152 }
else if (sl >= 5 && memcmp(buf,
"HEAD ", 5) == 0) {
162 while (p < sl && buf[p] !=
' ') {
168 if (tend == tstart || buf[tstart] !=
'/' ||
172 if (p >= sl || buf[p] !=
' ')
175 if (sl - p != 8 || memcmp(buf + p,
"HTTP/1.", 7) != 0 ||
176 (buf[p + 7] !=
'0' && buf[p + 7] !=
'1'))
179 memcpy(out->
path, buf + tstart, tend - tstart);
180 out->
path[tend - tstart] =
'\0';
184 while (line_start + 1 < out->
head_len) {
185 size_t le = line_start, colon, vs, vlen, nlen;
186 const char *name, *val;
188 !(buf[
le] ==
'\r' && buf[
le + 1] ==
'\n'))
190 if (
le == line_start)
195 while (colon <
le && buf[colon] !=
':')
199 name = buf + line_start;
200 nlen = colon - line_start;
204 while (vs <
le && (buf[vs] ==
' ' || buf[vs] ==
'\t'))
208 while (vlen > 0 && (val[vlen - 1] ==
' ' || val[vlen - 1] ==
'\t'))
217 if (
name_eq(name, nlen,
"host"))
219 if (
name_eq(name, nlen,
"transfer-encoding"))
221 if (
name_eq(name, nlen,
"content-length")) {
229 }
else if (
name_eq(name, nlen,
"authorization")) {
257 if (!req || !buf || !name)
262 if (
h->name_len == n &&
name_ieq(buf +
h->name_off, name, n)) {
264 *value_len =
h->value_len;
265 return buf +
h->value_off;
272 size_t i, n, count = 0;
273 if (!req || !buf || !name)
278 if (
h->name_len == n &&
name_ieq(buf +
h->name_off, name, n))
static int h(void *ctx, const N_ROUTE_MATCH *m)
a handler that does nothing (the test only routes)
int require_host
non-zero: an HTTP/1.1 request must carry exactly one Host header and an HTTP/1.0 one at most one (RFC...
long long body_max
largest accepted Content-Length, 0 to N_HTTP_REQ_BODY_LIMIT_MAX
char authorization[256u+1]
unsigned methods
accepted methods, an OR of N_HTTP_REQ_METHOD_BIT(); any other method is an error
n_http_req_header headers[64u]
#define N_HTTP_REQ_METHOD_BIT(m)
Bit of a method in n_http_req_limits.methods.
n_http_req_status n_http_request_parse_ex(const char *buf, size_t len, const n_http_req_limits *limits, n_http_request *out)
parse an HTTP/1.1 request head with caller-chosen limits
#define N_HTTP_REQ_HEAD_MAX
Largest request head (request line + headers, through CRLFCRLF) accepted.
n_http_req_status
Result of n_http_request_parse.
const char * n_http_request_header(const n_http_request *req, const char *buf, const char *name, size_t *value_len)
find a header of a parsed request
n_http_req_status n_http_request_parse(const char *buf, size_t len, n_http_request *out)
parse an HTTP/1.1 request head from [buf, buf+len)
size_t n_http_request_header_count(const n_http_request *req, const char *buf, const char *name)
count the headers of a parsed request that carry a name
#define N_HTTP_REQ_BODY_LIMIT_MAX
Highest body cap n_http_request_parse_ex() accepts (1 GiB).
#define N_HTTP_REQ_AUTH_MAX
Longest captured Authorization header value.
#define N_HTTP_REQ_BODY_MAX
Maximum declared Content-Length (body size), in bytes, for n_http_request_parse().
#define N_HTTP_REQ_HEADERS_MAX
Maximum number of header lines.
#define N_HTTP_REQ_PATH_MAX
Longest origin-form request target ('/'-prefixed, query included).
@ N_HTTP_REQ_INCOMPLETE
no CRLFCRLF yet, still under the cap
@ N_HTTP_REQ_ERROR
malformed / oversized / unsupported
@ N_HTTP_REQ_OK
a complete, valid head was parsed
Limits for n_http_request_parse_ex().
static int value_ok(const char *s, size_t n)
static int target_ok(unsigned char c)
static int name_eq(const char *s, size_t n, const char *lit)
static int name_token_ok(const char *s, size_t n)
static int parse_body_len(const char *s, size_t n, long long max, long long *out)
static int name_ieq(const char *a, const char *b, size_t n)
Pure, strict, bounded parser for an HTTP/1.1 request head.
static uint64_t le(const unsigned char *p, int n)
read a little endian unsigned value