n_http_req regression: valid requests parse, and the smuggling / malformed / oversized cases are rejected.
n_http_req regression: valid requests parse, and the smuggling / malformed / oversized cases are rejected.
#include <string.h>
}
#define WANT(expr, want, msg) \
do { \
if ((expr) != (want)) { \
n_log(LOG_ERR, "FAIL: %s", (msg)); \
failures++; \
} \
} while (0)
"valid GET should parse");
if (strcmp(r.
path,
"/v1/me?x=1") != 0) {
}
WANT(
parse(
"POST /v1/send HTTP/1.1\r\nContent-Length: 5\r\n"
"Authorization: Bearer tok\r\n\r\nhello",
&r),
}
}
"unsupported method rejected");
"non-origin-form target rejected");
"fragment in target rejected");
"unsupported version rejected");
WANT(
parse(
"POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n", &r),
WANT(
parse(
"POST / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 2\r\n"
"\r\n",
&r),
WANT(
parse(
"POST / HTTP/1.1\r\nContent-Length: abc\r\n\r\n", &r),
"partial head is INCOMPLETE");
"empty buffer is INCOMPLETE");
"buffer shorter than CRLFCRLF is INCOMPLETE");
"bare LF inside a header value rejected");
"bare CR inside a header value rejected");
"separator in a header name rejected");
"obs-fold continuation line rejected");
WANT(
parse(
"GET / HTTP/1.1\r\nX-A: caf\xc3\xa9\t ok\r\n\r\n", &r),
"HEAD refused by n_http_request_parse");
{
const char* req =
"GET /x HTTP/1.0\r\nHost: www.example\r\ncookie: a=1\r\n"
"X-Empty:\r\nCOOKIE: b=2 \r\n\r\n";
const char* v;
size_t vlen = 99;
if (!v || vlen != 11 || memcmp(v, "www.example", 11) != 0) {
}
if (!v || vlen != 3 || memcmp(v, "a=1", 3) != 0) {
}
"two Cookie lines counted");
WANT(v != NULL && vlen == 0, 1,
"empty header value found, length 0");
"absent header is NULL");
WANT(vlen, (
size_t)0,
"absent header length is 0");
"a name prefix does not match");
"NULL request");
"NULL buffer counts 0");
}
{
#define PARSE_EX(s) n_http_request_parse_ex((s), strlen(s), &lim, &r)
"HEAD accepted by parse_ex when allowed");
WANT(
PARSE_EX(
"POST /u HTTP/1.1\r\nHost: e\r\nContent-Length: 23068672\r\n\r\n"),
WANT(
PARSE_EX(
"POST /u HTTP/1.1\r\nHost: e\r\nContent-Length: 23068673\r\n\r\n"),
"HTTP/1.1 without Host rejected when required");
WANT(
PARSE_EX(
"GET / HTTP/1.1\r\nHost: a\r\nHost: b\r\n\r\n"),
"HTTP/1.0 without Host accepted");
"PUT still rejected by parse_ex");
"method outside the allowed set rejected");
"empty method set is an error");
"body cap above the limit is an error");
"negative body cap is an error");
#undef PARSE_EX
}
return 1;
}
return 0;
}
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.