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

Pure, strict, bounded parser for an HTTP/1.1 request head. More...

#include <stddef.h>
Include dependency graph for n_http_req.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_req_header
 One header line, as offsets into the parsed buffer: the name is [name_off, name_off + name_len), the value (surrounding spaces and tabs trimmed) is [value_off, value_off + value_len). More...
struct  n_http_req_limits
 Limits for n_http_request_parse_ex(). More...
struct  n_http_request
 A parsed request head. More...

Macros

#define N_HTTP_REQ_AUTH_MAX   256u
 Longest captured Authorization header value.
#define N_HTTP_REQ_BODY_LIMIT_MAX   (1024LL * 1024LL * 1024LL)
 Highest body cap n_http_request_parse_ex() accepts (1 GiB).
#define N_HTTP_REQ_BODY_MAX   (1024u * 1024u)
 Maximum declared Content-Length (body size), in bytes, for n_http_request_parse().
#define N_HTTP_REQ_HEAD_MAX   16384u
 Largest request head (request line + headers, through CRLFCRLF) accepted.
#define N_HTTP_REQ_HEADERS_MAX   64u
 Maximum number of header lines.
#define N_HTTP_REQ_METHOD_BIT(m)
 Bit of a method in n_http_req_limits.methods.
#define N_HTTP_REQ_PATH_MAX   2048u
 Longest origin-form request target ('/'-prefixed, query included).

Enumerations

enum  n_http_req_method { N_HTTP_REQ_GET = 0 , N_HTTP_REQ_POST = 1 , N_HTTP_REQ_HEAD = 2 }
 The methods accepted. More...
enum  n_http_req_status { N_HTTP_REQ_OK = 0 , N_HTTP_REQ_INCOMPLETE = 1 , N_HTTP_REQ_ERROR = 2 }
 Result of n_http_request_parse. More...

Functions

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
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
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)
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

Detailed Description

Pure, strict, bounded parser for an HTTP/1.1 request head.

A small, hardened scanner, not a general HTTP implementation, for the request line + headers a browser / REST client sends, meant for a server that sniffed a "GET " / "POST " on a connection and needs the head before routing.

It faces hostile network input, so it is deliberately narrow: only GET and POST (plus HEAD through n_http_request_parse_ex()), only origin-form targets ("/path?query"), only HTTP/1.0 or 1.1, Content-Length only, no chunked transfer encoding, with the target, header count and declared body all bounded before use, and a duplicate Content-Length or any Transfer-Encoding header rejected to foreclose request smuggling. Header names must be RFC 9110 tokens and header values may not hold control bytes (a bare CR, LF or NUL inside a value is a smuggling and injection vector). No sockets, no allocation, never over-reads its buffer. The OK / INCOMPLETE / ERROR split lets a transport read incrementally.

Every header line is recorded as offsets into the caller's buffer, so a server reads Host, Cookie, Content-Type... with n_http_request_header() without any copy. Those offsets are only meaningful while the buffer that was parsed is unchanged.

Author
Castagnier Mickael
Version
1.0

Definition in file n_http_req.h.