40#define WANT(expr, want, msg) \
42 if ((expr) != (want)) { \
43 n_log(LOG_ERR, "FAIL: %s", (msg)); \
54 "valid GET should parse");
56 if (strcmp(r.
path,
"/v1/me?x=1") != 0) {
62 WANT(
parse(
"POST /v1/send HTTP/1.1\r\nContent-Length: 5\r\n"
63 "Authorization: Bearer tok\r\n\r\nhello",
78 "unsupported method rejected");
80 "non-origin-form target rejected");
82 "fragment in target rejected");
84 "unsupported version rejected");
85 WANT(
parse(
"POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n", &r),
87 WANT(
parse(
"POST / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 2\r\n"
91 WANT(
parse(
"POST / HTTP/1.1\r\nContent-Length: abc\r\n\r\n", &r),
96 "partial head is INCOMPLETE");
99 "empty buffer is INCOMPLETE");
101 "buffer shorter than CRLFCRLF is INCOMPLETE");
108 "bare LF inside a header value rejected");
110 "bare CR inside a header value rejected");
115 "separator in a header name rejected");
117 "obs-fold continuation line rejected");
118 WANT(
parse(
"GET / HTTP/1.1\r\nX-A: caf\xc3\xa9\t ok\r\n\r\n", &r),
123 "HEAD refused by n_http_request_parse");
128 "GET /x HTTP/1.0\r\nHost: www.example\r\ncookie: a=1\r\n"
129 "X-Empty:\r\nCOOKIE: b=2 \r\n\r\n";
136 if (!v || vlen != 11 || memcmp(v,
"www.example", 11) != 0) {
141 if (!v || vlen != 3 || memcmp(v,
"a=1", 3) != 0) {
146 "two Cookie lines counted");
148 WANT(v != NULL && vlen == 0, 1,
"empty header value found, length 0");
150 "absent header is NULL");
151 WANT(vlen, (
size_t)0,
"absent header length is 0");
153 "a name prefix does not match");
157 "NULL buffer counts 0");
168#define PARSE_EX(s) n_http_request_parse_ex((s), strlen(s), &lim, &r)
170 "HEAD accepted by parse_ex when allowed");
173 WANT(
PARSE_EX(
"POST /u HTTP/1.1\r\nHost: e\r\nContent-Length: 23068672\r\n\r\n"),
176 WANT(
PARSE_EX(
"POST /u HTTP/1.1\r\nHost: e\r\nContent-Length: 23068673\r\n\r\n"),
179 "HTTP/1.1 without Host rejected when required");
180 WANT(
PARSE_EX(
"GET / HTTP/1.1\r\nHost: a\r\nHost: b\r\n\r\n"),
183 "HTTP/1.0 without Host accepted");
185 "PUT still rejected by parse_ex");
188 "method outside the allowed set rejected");
191 "empty method set is an error");
195 "body cap above the limit is an error");
198 "negative body cap is an error");
static n_http_req_status parse(const char *s, n_http_request *out)
#define WANT(expr, want, msg)
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_NOTICE
normal but significant condition
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
#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
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).
@ 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().
Pure, strict, bounded parser for an HTTP/1.1 request head.