Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_http_req.c
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
27
28#include "nilorea/n_http_req.h"
29#include "nilorea/n_log.h"
30
31#include <string.h>
32
33static int failures = 0;
34
35/* Parse a NUL-terminated request literal (its byte length is the wire length). */
36static n_http_req_status parse(const char* s, n_http_request* out) {
37 return n_http_request_parse(s, strlen(s), out);
38}
39
40#define WANT(expr, want, msg) \
41 do { \
42 if ((expr) != (want)) { \
43 n_log(LOG_ERR, "FAIL: %s", (msg)); \
44 failures++; \
45 } \
46 } while (0)
47
48int main(void) {
51
52 /* a valid GET */
53 WANT(parse("GET /v1/me?x=1 HTTP/1.1\r\nHost: e\r\n\r\n", &r), N_HTTP_REQ_OK,
54 "valid GET should parse");
55 WANT(r.method, N_HTTP_REQ_GET, "method GET");
56 if (strcmp(r.path, "/v1/me?x=1") != 0) {
57 n_log(LOG_ERR, "FAIL: GET path");
58 failures++;
59 }
60
61 /* a valid POST with a body + an Authorization header */
62 WANT(parse("POST /v1/send HTTP/1.1\r\nContent-Length: 5\r\n"
63 "Authorization: Bearer tok\r\n\r\nhello",
64 &r),
65 N_HTTP_REQ_OK, "valid POST should parse");
66 WANT(r.method, N_HTTP_REQ_POST, "method POST");
67 if (r.content_length != 5) {
68 n_log(LOG_ERR, "FAIL: content_length");
69 failures++;
70 }
71 if (strcmp(r.authorization, "Bearer tok") != 0) {
72 n_log(LOG_ERR, "FAIL: authorization capture");
73 failures++;
74 }
75
76 /* rejections */
77 WANT(parse("PUT / HTTP/1.1\r\n\r\n", &r), N_HTTP_REQ_ERROR,
78 "unsupported method rejected");
79 WANT(parse("GET http://e/x HTTP/1.1\r\n\r\n", &r), N_HTTP_REQ_ERROR,
80 "non-origin-form target rejected");
81 WANT(parse("GET /a#frag HTTP/1.1\r\n\r\n", &r), N_HTTP_REQ_ERROR,
82 "fragment in target rejected");
83 WANT(parse("GET / HTTP/2.0\r\n\r\n", &r), N_HTTP_REQ_ERROR,
84 "unsupported version rejected");
85 WANT(parse("POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n", &r),
86 N_HTTP_REQ_ERROR, "Transfer-Encoding rejected (smuggling)");
87 WANT(parse("POST / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 2\r\n"
88 "\r\n",
89 &r),
90 N_HTTP_REQ_ERROR, "duplicate Content-Length rejected (smuggling)");
91 WANT(parse("POST / HTTP/1.1\r\nContent-Length: abc\r\n\r\n", &r),
92 N_HTTP_REQ_ERROR, "non-numeric Content-Length rejected");
93
94 /* incomplete: no CRLFCRLF yet, still under the cap -> read more */
95 WANT(parse("GET / HTTP/1.1\r\nHost: e", &r), N_HTTP_REQ_INCOMPLETE,
96 "partial head is INCOMPLETE");
97 /* shorter than the CRLFCRLF terminator: still just "read more" */
99 "empty buffer is INCOMPLETE");
101 "buffer shorter than CRLFCRLF is INCOMPLETE");
102
103 /* NULL args */
104 WANT(n_http_request_parse(NULL, 0, &r), N_HTTP_REQ_ERROR, "NULL buf");
105
106 /* header values and names: control bytes and non-token names refused */
107 WANT(parse("GET / HTTP/1.1\r\nX-A: a\nb\r\n\r\n", &r), N_HTTP_REQ_ERROR,
108 "bare LF inside a header value rejected");
109 WANT(parse("GET / HTTP/1.1\r\nX-A: a\rb\r\n\r\n", &r), N_HTTP_REQ_ERROR,
110 "bare CR inside a header value rejected");
111 /* 28 bytes: the literal holds a NUL, so strlen() cannot measure it */
112 WANT(n_http_request_parse("GET / HTTP/1.1\r\nX-A: a\0b\r\n\r\n", 28, &r),
113 N_HTTP_REQ_ERROR, "NUL inside a header value rejected");
114 WANT(parse("GET / HTTP/1.1\r\nX(A): a\r\n\r\n", &r), N_HTTP_REQ_ERROR,
115 "separator in a header name rejected");
116 WANT(parse("GET / HTTP/1.1\r\n folded: a\r\n\r\n", &r), N_HTTP_REQ_ERROR,
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),
119 N_HTTP_REQ_OK, "obs-text and tabs inside a value accepted");
120
121 /* the default entry point keeps refusing HEAD (GET and POST only) */
122 WANT(parse("HEAD / HTTP/1.1\r\n\r\n", &r), N_HTTP_REQ_ERROR,
123 "HEAD refused by n_http_request_parse");
124
125 /* header table and lookup */
126 {
127 const char* req =
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";
130 const char* v;
131 size_t vlen = 99;
132 WANT(parse(req, &r), N_HTTP_REQ_OK, "header table request parses");
133 WANT(r.version_minor, 0, "HTTP/1.0 version_minor");
134 WANT(r.nb_headers, (size_t)4, "four headers recorded");
135 v = n_http_request_header(&r, req, "HOST", &vlen);
136 if (!v || vlen != 11 || memcmp(v, "www.example", 11) != 0) {
137 n_log(LOG_ERR, "FAIL: Host lookup");
138 failures++;
139 }
140 v = n_http_request_header(&r, req, "Cookie", &vlen);
141 if (!v || vlen != 3 || memcmp(v, "a=1", 3) != 0) {
142 n_log(LOG_ERR, "FAIL: first Cookie lookup");
143 failures++;
144 }
145 WANT(n_http_request_header_count(&r, req, "cookie"), (size_t)2,
146 "two Cookie lines counted");
147 v = n_http_request_header(&r, req, "x-empty", &vlen);
148 WANT(v != NULL && vlen == 0, 1, "empty header value found, length 0");
149 WANT(n_http_request_header(&r, req, "Referer", &vlen) == NULL, 1,
150 "absent header is NULL");
151 WANT(vlen, (size_t)0, "absent header length is 0");
152 WANT(n_http_request_header(&r, req, "Hos", NULL) == NULL, 1,
153 "a name prefix does not match");
154 WANT(n_http_request_header(NULL, req, "Host", NULL) == NULL, 1,
155 "NULL request");
156 WANT(n_http_request_header_count(&r, NULL, "Host"), (size_t)0,
157 "NULL buffer counts 0");
158 }
159
160 /* n_http_request_parse_ex: methods, body cap, Host rule */
161 {
166 lim.body_max = 22LL * 1024 * 1024;
167 lim.require_host = 1;
168#define PARSE_EX(s) n_http_request_parse_ex((s), strlen(s), &lim, &r)
169 WANT(PARSE_EX("HEAD /a HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_OK,
170 "HEAD accepted by parse_ex when allowed");
171 WANT(r.method, N_HTTP_REQ_HEAD, "method HEAD");
172 WANT(r.version_minor, 1, "HTTP/1.1 version_minor");
173 WANT(PARSE_EX("POST /u HTTP/1.1\r\nHost: e\r\nContent-Length: 23068672\r\n\r\n"),
174 N_HTTP_REQ_OK, "22 MiB body accepted under a 22 MiB cap");
175 WANT(r.content_length, 23068672LL, "22 MiB content_length");
176 WANT(PARSE_EX("POST /u HTTP/1.1\r\nHost: e\r\nContent-Length: 23068673\r\n\r\n"),
177 N_HTTP_REQ_ERROR, "body one byte over the cap rejected");
178 WANT(PARSE_EX("GET / HTTP/1.1\r\n\r\n"), N_HTTP_REQ_ERROR,
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"),
181 N_HTTP_REQ_ERROR, "two Host headers rejected when required");
182 WANT(PARSE_EX("GET / HTTP/1.0\r\n\r\n"), N_HTTP_REQ_OK,
183 "HTTP/1.0 without Host accepted");
184 WANT(PARSE_EX("PUT / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
185 "PUT still rejected by parse_ex");
187 WANT(PARSE_EX("POST / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
188 "method outside the allowed set rejected");
189 lim.methods = 0;
190 WANT(PARSE_EX("GET / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
191 "empty method set is an error");
194 WANT(PARSE_EX("GET / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
195 "body cap above the limit is an error");
196 lim.body_max = -1;
197 WANT(PARSE_EX("GET / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
198 "negative body cap is an error");
199#undef PARSE_EX
200 WANT(n_http_request_parse_ex("GET / HTTP/1.1\r\n\r\n", 18, NULL, &r),
201 N_HTTP_REQ_ERROR, "NULL limits");
202 }
203
204 if (failures) {
205 n_log(LOG_ERR, "ex_http_req: %d failure(s)", failures);
206 return 1;
207 }
208 n_log(LOG_NOTICE, "ex_http_req: all checks passed");
209 return 0;
210}
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
#define PARSE_EX(s)
static n_http_req_status parse(const char *s, n_http_request *out)
Definition ex_http_req.c:36
#define WANT(expr, want, msg)
Definition ex_http_req.c:40
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
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...
Definition n_http_req.h:105
long long body_max
largest accepted Content-Length, 0 to N_HTTP_REQ_BODY_LIMIT_MAX
Definition n_http_req.h:102
char authorization[256u+1]
Definition n_http_req.h:122
unsigned methods
accepted methods, an OR of N_HTTP_REQ_METHOD_BIT(); any other method is an error
Definition n_http_req.h:100
char path[2048u+1]
Definition n_http_req.h:119
size_t nb_headers
Definition n_http_req.h:124
long long content_length
Definition n_http_req.h:120
n_http_req_method method
Definition n_http_req.h:118
#define N_HTTP_REQ_METHOD_BIT(m)
Bit of a method in n_http_req_limits.methods.
Definition n_http_req.h:84
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
Definition n_http_req.c:110
n_http_req_status
Result of n_http_request_parse.
Definition n_http_req.h:130
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
Definition n_http_req.c:253
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)
Definition n_http_req.c:245
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
Definition n_http_req.c:271
#define N_HTTP_REQ_BODY_LIMIT_MAX
Highest body cap n_http_request_parse_ex() accepts (1 GiB).
Definition n_http_req.h:72
@ N_HTTP_REQ_INCOMPLETE
no CRLFCRLF yet, still under the cap
Definition n_http_req.h:132
@ N_HTTP_REQ_ERROR
malformed / oversized / unsupported
Definition n_http_req.h:133
@ N_HTTP_REQ_OK
a complete, valid head was parsed
Definition n_http_req.h:131
@ N_HTTP_REQ_POST
Definition n_http_req.h:79
@ N_HTTP_REQ_GET
Definition n_http_req.h:78
@ N_HTTP_REQ_HEAD
Definition n_http_req.h:80
Limits for n_http_request_parse_ex().
Definition n_http_req.h:97
A parsed request head.
Definition n_http_req.h:117
Pure, strict, bounded parser for an HTTP/1.1 request head.
Generic log system.