xref: /unit/src/test/nxt_http_parse_test.c (revision 1709:1fe93c17d23f)
1384Szelenkov@nginx.com 
2384Szelenkov@nginx.com /*
3384Szelenkov@nginx.com  * Copyright (C) NGINX, Inc.
4384Szelenkov@nginx.com  * Copyright (C) Valentin V. Bartenev
5384Szelenkov@nginx.com  */
6384Szelenkov@nginx.com 
7384Szelenkov@nginx.com #include <nxt_main.h>
8384Szelenkov@nginx.com #include "nxt_tests.h"
9384Szelenkov@nginx.com 
10384Szelenkov@nginx.com 
11384Szelenkov@nginx.com typedef struct {
12384Szelenkov@nginx.com     nxt_str_t  method;
13384Szelenkov@nginx.com     nxt_str_t  target;
14384Szelenkov@nginx.com     nxt_str_t  args;
15384Szelenkov@nginx.com     u_char     version[8];
16384Szelenkov@nginx.com 
17384Szelenkov@nginx.com     /* target with "/." */
18384Szelenkov@nginx.com     unsigned   complex_target:1;
19384Szelenkov@nginx.com     /* target with "%" */
20384Szelenkov@nginx.com     unsigned   quoted_target:1;
21384Szelenkov@nginx.com     /* target with " " */
22384Szelenkov@nginx.com     unsigned   space_in_target:1;
23384Szelenkov@nginx.com } nxt_http_parse_test_request_line_t;
24384Szelenkov@nginx.com 
25384Szelenkov@nginx.com 
26*1709Svbart@nginx.com typedef struct {
27*1709Svbart@nginx.com     nxt_int_t  result;
28*1709Svbart@nginx.com     unsigned   discard_unsafe_fields:1;
29*1709Svbart@nginx.com } nxt_http_parse_test_fields_t;
30*1709Svbart@nginx.com 
31*1709Svbart@nginx.com 
32384Szelenkov@nginx.com typedef union {
33384Szelenkov@nginx.com     void                                *pointer;
34*1709Svbart@nginx.com     nxt_http_parse_test_fields_t        fields;
35384Szelenkov@nginx.com     nxt_http_parse_test_request_line_t  request_line;
36384Szelenkov@nginx.com } nxt_http_parse_test_data_t;
37384Szelenkov@nginx.com 
38384Szelenkov@nginx.com 
39384Szelenkov@nginx.com typedef struct {
40384Szelenkov@nginx.com     nxt_str_t  request;
41384Szelenkov@nginx.com     nxt_int_t  result;
42384Szelenkov@nginx.com     nxt_int_t  (*handler)(nxt_http_request_parse_t *rp,
43384Szelenkov@nginx.com                           nxt_http_parse_test_data_t *data,
44384Szelenkov@nginx.com                           nxt_str_t *request, nxt_log_t *log);
45384Szelenkov@nginx.com 
46384Szelenkov@nginx.com     nxt_http_parse_test_data_t  data;
47384Szelenkov@nginx.com } nxt_http_parse_test_case_t;
48384Szelenkov@nginx.com 
49384Szelenkov@nginx.com 
50384Szelenkov@nginx.com static nxt_int_t nxt_http_parse_test_run(nxt_http_request_parse_t *rp,
51384Szelenkov@nginx.com     nxt_str_t *request);
52384Szelenkov@nginx.com static nxt_int_t nxt_http_parse_test_bench(nxt_thread_t *thr,
53417Svbart@nginx.com     nxt_str_t *request, nxt_lvlhsh_t *hash, const char *name, nxt_uint_t n);
54384Szelenkov@nginx.com static nxt_int_t nxt_http_parse_test_request_line(nxt_http_request_parse_t *rp,
55384Szelenkov@nginx.com     nxt_http_parse_test_data_t *data,
56384Szelenkov@nginx.com     nxt_str_t *request, nxt_log_t *log);
57384Szelenkov@nginx.com static nxt_int_t nxt_http_parse_test_fields(nxt_http_request_parse_t *rp,
58384Szelenkov@nginx.com     nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log);
59384Szelenkov@nginx.com 
60384Szelenkov@nginx.com 
61384Szelenkov@nginx.com static nxt_int_t nxt_http_test_header_return(void *ctx, nxt_http_field_t *field,
62417Svbart@nginx.com     uintptr_t data);
63384Szelenkov@nginx.com 
64384Szelenkov@nginx.com 
65384Szelenkov@nginx.com static nxt_http_parse_test_case_t  nxt_http_test_cases[] = {
66384Szelenkov@nginx.com     {
67384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.0\r\n\r\n"),
68384Szelenkov@nginx.com         NXT_DONE,
69384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
70384Szelenkov@nginx.com         { .request_line = {
71384Szelenkov@nginx.com             nxt_string("GET"),
72384Szelenkov@nginx.com             nxt_string("/"),
73384Szelenkov@nginx.com             nxt_null_string,
74384Szelenkov@nginx.com             "HTTP/1.0",
751170Svbart@nginx.com             0, 0, 0
76384Szelenkov@nginx.com         }}
77384Szelenkov@nginx.com     },
78384Szelenkov@nginx.com     {
79384Szelenkov@nginx.com         nxt_string("XXX-METHOD    /d.ir/fi+le.ext?key=val    HTTP/1.2\n\n"),
80384Szelenkov@nginx.com         NXT_DONE,
81384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
82384Szelenkov@nginx.com         { .request_line = {
83384Szelenkov@nginx.com             nxt_string("XXX-METHOD"),
84384Szelenkov@nginx.com             nxt_string("/d.ir/fi+le.ext?key=val"),
85384Szelenkov@nginx.com             nxt_string("key=val"),
86384Szelenkov@nginx.com             "HTTP/1.2",
871170Svbart@nginx.com             0, 0, 0
88384Szelenkov@nginx.com         }}
89384Szelenkov@nginx.com     },
90384Szelenkov@nginx.com     {
91384Szelenkov@nginx.com         nxt_string("GET /di.r/? HTTP/1.0\r\n\r\n"),
92384Szelenkov@nginx.com         NXT_DONE,
93384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
94384Szelenkov@nginx.com         { .request_line = {
95384Szelenkov@nginx.com             nxt_string("GET"),
96384Szelenkov@nginx.com             nxt_string("/di.r/?"),
97384Szelenkov@nginx.com             nxt_string(""),
98384Szelenkov@nginx.com             "HTTP/1.0",
991170Svbart@nginx.com             0, 0, 0
100384Szelenkov@nginx.com         }}
101384Szelenkov@nginx.com     },
102384Szelenkov@nginx.com     {
103384Szelenkov@nginx.com         nxt_string("GEt / HTTP/1.0\r\n\r\n"),
104480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
105384Szelenkov@nginx.com         NULL, { NULL }
106384Szelenkov@nginx.com     },
107384Szelenkov@nginx.com     {
108384Szelenkov@nginx.com         nxt_string("GET /\0 HTTP/1.0\r\n\r\n"),
109480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
110384Szelenkov@nginx.com         NULL, { NULL }
111384Szelenkov@nginx.com     },
112384Szelenkov@nginx.com     {
113384Szelenkov@nginx.com         nxt_string("GET /\r HTTP/1.0\r\n\r\n"),
114480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
115384Szelenkov@nginx.com         NULL, { NULL }
116384Szelenkov@nginx.com     },
117384Szelenkov@nginx.com     {
118384Szelenkov@nginx.com         nxt_string("GET /\n HTTP/1.0\r\n\r\n"),
119480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
120384Szelenkov@nginx.com         NULL, { NULL }
121384Szelenkov@nginx.com     },
122384Szelenkov@nginx.com     {
123384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.0\r\r\n"),
124480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
125384Szelenkov@nginx.com         NULL, { NULL }
126384Szelenkov@nginx.com     },
127384Szelenkov@nginx.com     {
128482Svbart@nginx.com         nxt_string("GET / HTTP/2.0\r\n"),
129482Svbart@nginx.com         NXT_HTTP_PARSE_UNSUPPORTED_VERSION,
130482Svbart@nginx.com         NULL, { NULL }
131482Svbart@nginx.com     },
132482Svbart@nginx.com     {
133384Szelenkov@nginx.com         nxt_string("GET /. HTTP/1.0\r\n\r\n"),
134384Szelenkov@nginx.com         NXT_DONE,
135384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
136384Szelenkov@nginx.com         { .request_line = {
137384Szelenkov@nginx.com             nxt_string("GET"),
138384Szelenkov@nginx.com             nxt_string("/."),
139384Szelenkov@nginx.com             nxt_null_string,
140384Szelenkov@nginx.com             "HTTP/1.0",
1411170Svbart@nginx.com             1, 0, 0
142384Szelenkov@nginx.com         }}
143384Szelenkov@nginx.com     },
144384Szelenkov@nginx.com     {
145384Szelenkov@nginx.com         nxt_string("GET /# HTTP/1.0\r\n\r\n"),
146384Szelenkov@nginx.com         NXT_DONE,
147384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
148384Szelenkov@nginx.com         { .request_line = {
149384Szelenkov@nginx.com             nxt_string("GET"),
150384Szelenkov@nginx.com             nxt_string("/#"),
151384Szelenkov@nginx.com             nxt_null_string,
152384Szelenkov@nginx.com             "HTTP/1.0",
1531170Svbart@nginx.com             1, 0, 0
154384Szelenkov@nginx.com         }}
155384Szelenkov@nginx.com     },
156384Szelenkov@nginx.com     {
157384Szelenkov@nginx.com         nxt_string("GET /?# HTTP/1.0\r\n\r\n"),
158384Szelenkov@nginx.com         NXT_DONE,
159384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
160384Szelenkov@nginx.com         { .request_line = {
161384Szelenkov@nginx.com             nxt_string("GET"),
162384Szelenkov@nginx.com             nxt_string("/?#"),
1631168Svbart@nginx.com             nxt_string(""),
164384Szelenkov@nginx.com             "HTTP/1.0",
1651170Svbart@nginx.com             1, 0, 0
166384Szelenkov@nginx.com         }}
167384Szelenkov@nginx.com     },
168384Szelenkov@nginx.com     {
169384Szelenkov@nginx.com         nxt_string("GET // HTTP/1.0\r\n\r\n"),
170384Szelenkov@nginx.com         NXT_DONE,
171384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
172384Szelenkov@nginx.com         { .request_line = {
173384Szelenkov@nginx.com             nxt_string("GET"),
174384Szelenkov@nginx.com             nxt_string("//"),
175384Szelenkov@nginx.com             nxt_null_string,
176384Szelenkov@nginx.com             "HTTP/1.0",
1771170Svbart@nginx.com             1, 0, 0
178384Szelenkov@nginx.com         }}
179384Szelenkov@nginx.com     },
180384Szelenkov@nginx.com     {
181384Szelenkov@nginx.com         nxt_string("GET /%20 HTTP/1.0\r\n\r\n"),
182384Szelenkov@nginx.com         NXT_DONE,
183384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
184384Szelenkov@nginx.com         { .request_line = {
185384Szelenkov@nginx.com             nxt_string("GET"),
186384Szelenkov@nginx.com             nxt_string("/%20"),
187384Szelenkov@nginx.com             nxt_null_string,
188384Szelenkov@nginx.com             "HTTP/1.0",
1891170Svbart@nginx.com             0, 1, 0
190384Szelenkov@nginx.com         }}
191384Szelenkov@nginx.com     },
192384Szelenkov@nginx.com     {
193384Szelenkov@nginx.com         nxt_string("GET / a HTTP/1.0\r\n\r\n"),
194384Szelenkov@nginx.com         NXT_DONE,
195384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
196384Szelenkov@nginx.com         { .request_line = {
197384Szelenkov@nginx.com             nxt_string("GET"),
198384Szelenkov@nginx.com             nxt_string("/ a"),
199384Szelenkov@nginx.com             nxt_null_string,
200384Szelenkov@nginx.com             "HTTP/1.0",
2011170Svbart@nginx.com             0, 0, 1
202384Szelenkov@nginx.com         }}
203384Szelenkov@nginx.com     },
204384Szelenkov@nginx.com     {
2051171Svbart@nginx.com         nxt_string("GET /na %20me.ext?args HTTP/1.0\r\n\r\n"),
2061171Svbart@nginx.com         NXT_DONE,
2071171Svbart@nginx.com         &nxt_http_parse_test_request_line,
2081171Svbart@nginx.com         { .request_line = {
2091171Svbart@nginx.com             nxt_string("GET"),
2101171Svbart@nginx.com             nxt_string("/na %20me.ext?args"),
2111171Svbart@nginx.com             nxt_string("args"),
2121171Svbart@nginx.com             "HTTP/1.0",
2131171Svbart@nginx.com             0, 1, 1
2141171Svbart@nginx.com         }}
2151171Svbart@nginx.com     },
2161171Svbart@nginx.com     {
217384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.0 HTTP/1.1\r\n\r\n"),
218384Szelenkov@nginx.com         NXT_DONE,
219384Szelenkov@nginx.com         &nxt_http_parse_test_request_line,
220384Szelenkov@nginx.com         { .request_line = {
221384Szelenkov@nginx.com             nxt_string("GET"),
222384Szelenkov@nginx.com             nxt_string("/ HTTP/1.0"),
223384Szelenkov@nginx.com             nxt_null_string,
224384Szelenkov@nginx.com             "HTTP/1.1",
2251170Svbart@nginx.com             0, 0, 1
226384Szelenkov@nginx.com         }}
227384Szelenkov@nginx.com     },
228384Szelenkov@nginx.com     {
229384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
230384Szelenkov@nginx.com                    "Host: example.com\r\n\r\n"),
231384Szelenkov@nginx.com         NXT_DONE,
232384Szelenkov@nginx.com         NULL, { NULL }
233384Szelenkov@nginx.com     },
234384Szelenkov@nginx.com     {
235384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
236574Svbart@nginx.com                    "Host:example.com \r\n\r\n"),
237574Svbart@nginx.com         NXT_DONE,
238574Svbart@nginx.com         NULL, { NULL }
239574Svbart@nginx.com     },
240574Svbart@nginx.com     {
241574Svbart@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
242384Szelenkov@nginx.com                    "Host:\r\n\r\n"),
243384Szelenkov@nginx.com         NXT_DONE,
244384Szelenkov@nginx.com         NULL, { NULL }
245384Szelenkov@nginx.com     },
246384Szelenkov@nginx.com     {
247384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
248384Szelenkov@nginx.com                    "Host example.com\r\n\r\n"),
249480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
250384Szelenkov@nginx.com         NULL, { NULL }
251384Szelenkov@nginx.com     },
252384Szelenkov@nginx.com     {
253384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
254384Szelenkov@nginx.com                    ":Host: example.com\r\n\r\n"),
255480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
256384Szelenkov@nginx.com         NULL, { NULL }
257384Szelenkov@nginx.com     },
258384Szelenkov@nginx.com     {
259384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
260384Szelenkov@nginx.com                    "Ho_st: example.com\r\n\r\n"),
261454Svbart@nginx.com         NXT_DONE,
262384Szelenkov@nginx.com         NULL, { NULL }
263384Szelenkov@nginx.com     },
264384Szelenkov@nginx.com     {
265384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
266384Szelenkov@nginx.com                    "Ho\0st: example.com\r\n\r\n"),
267480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
268384Szelenkov@nginx.com         NULL, { NULL }
269384Szelenkov@nginx.com     },
270384Szelenkov@nginx.com     {
271384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
272384Szelenkov@nginx.com                    "Ho\rst: example.com\r\n\r\n"),
273480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
274384Szelenkov@nginx.com         NULL, { NULL }
275384Szelenkov@nginx.com     },
276384Szelenkov@nginx.com     {
277384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
278576Svbart@nginx.com                    "Ho\nst: example.com\r\n\r\n"),
279576Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
280576Svbart@nginx.com         NULL, { NULL }
281576Svbart@nginx.com     },
282576Svbart@nginx.com     {
283576Svbart@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
284576Svbart@nginx.com                    "Host : example.com\r\n\r\n"),
285576Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
286576Svbart@nginx.com         NULL, { NULL }
287576Svbart@nginx.com     },
288576Svbart@nginx.com     {
289576Svbart@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
290384Szelenkov@nginx.com                    "Host: exa\0mple.com\r\n\r\n"),
291480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
292384Szelenkov@nginx.com         NULL, { NULL }
293384Szelenkov@nginx.com     },
294384Szelenkov@nginx.com     {
295384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
296384Szelenkov@nginx.com                    "Host: exa\rmple.com\r\n\r\n"),
297480Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
298384Szelenkov@nginx.com         NULL, { NULL }
299384Szelenkov@nginx.com     },
300384Szelenkov@nginx.com     {
301384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
302575Svbart@nginx.com                    "Host: exa\bmple.com\r\n\r\n"),
303575Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
304575Svbart@nginx.com         NULL, { NULL }
305575Svbart@nginx.com     },
306575Svbart@nginx.com     {
307575Svbart@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
308575Svbart@nginx.com                    "Host: пример.испытание\r\n\r\n"),
309712Svbart@nginx.com         NXT_DONE,
310575Svbart@nginx.com         NULL, { NULL }
311575Svbart@nginx.com     },
312575Svbart@nginx.com     {
313575Svbart@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
314575Svbart@nginx.com                    "Host: xn--e1afmkfd.xn--80akhbyknj4f\r\n\r\n"),
315575Svbart@nginx.com         NXT_DONE,
316575Svbart@nginx.com         NULL, { NULL }
317575Svbart@nginx.com     },
318575Svbart@nginx.com     {
319575Svbart@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
320576Svbart@nginx.com                    "Host: exa\nmple.com\r\n\r\n"),
321576Svbart@nginx.com         NXT_HTTP_PARSE_INVALID,
322576Svbart@nginx.com         NULL, { NULL }
323576Svbart@nginx.com     },
324576Svbart@nginx.com     {
325576Svbart@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
326576Svbart@nginx.com                    "Host: exa\tmple.com\r\n\r\n"),
327576Svbart@nginx.com         NXT_DONE,
328576Svbart@nginx.com         NULL, { NULL }
329576Svbart@nginx.com     },
330576Svbart@nginx.com     {
331576Svbart@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
332384Szelenkov@nginx.com                    "X-Unknown-Header: value\r\n"
333*1709Svbart@nginx.com                    "X-Good-Header: value\r\n"
334*1709Svbart@nginx.com                    "!#$%&'*+.^_`|~: skipped\r\n\r\n"),
335384Szelenkov@nginx.com         NXT_DONE,
336384Szelenkov@nginx.com         &nxt_http_parse_test_fields,
337*1709Svbart@nginx.com         { .fields = { NXT_OK, 1 } }
338384Szelenkov@nginx.com     },
339384Szelenkov@nginx.com     {
340384Szelenkov@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
341384Szelenkov@nginx.com                    "X-Good-Header: value\r\n"
342384Szelenkov@nginx.com                    "X-Unknown-Header: value\r\n"
343384Szelenkov@nginx.com                    "X-Bad-Header: value\r\n\r\n"),
344384Szelenkov@nginx.com         NXT_DONE,
345384Szelenkov@nginx.com         &nxt_http_parse_test_fields,
346*1709Svbart@nginx.com         { .fields = { NXT_ERROR, 1 } }
347*1709Svbart@nginx.com     },
348*1709Svbart@nginx.com     {
349*1709Svbart@nginx.com         nxt_string("GET / HTTP/1.1\r\n"
350*1709Svbart@nginx.com                    "!#$%&'*+.^_`|~: allowed\r\n\r\n"),
351*1709Svbart@nginx.com         NXT_DONE,
352*1709Svbart@nginx.com         &nxt_http_parse_test_fields,
353*1709Svbart@nginx.com         { .fields = { NXT_ERROR, 0 } }
354384Szelenkov@nginx.com     },
355384Szelenkov@nginx.com };
356384Szelenkov@nginx.com 
357384Szelenkov@nginx.com 
358417Svbart@nginx.com static nxt_http_field_proc_t  nxt_http_test_fields[] = {
359384Szelenkov@nginx.com     { nxt_string("X-Bad-Header"),
360384Szelenkov@nginx.com       &nxt_http_test_header_return,
361417Svbart@nginx.com       NXT_ERROR },
362384Szelenkov@nginx.com 
363384Szelenkov@nginx.com     { nxt_string("X-Good-Header"),
364384Szelenkov@nginx.com       &nxt_http_test_header_return,
365417Svbart@nginx.com       NXT_OK },
366*1709Svbart@nginx.com 
367*1709Svbart@nginx.com     { nxt_string("!#$%&'*+.^_`|~"),
368*1709Svbart@nginx.com       &nxt_http_test_header_return,
369*1709Svbart@nginx.com       NXT_ERROR },
370384Szelenkov@nginx.com };
371384Szelenkov@nginx.com 
372384Szelenkov@nginx.com 
373417Svbart@nginx.com static nxt_lvlhsh_t  nxt_http_test_fields_hash;
374417Svbart@nginx.com 
375417Svbart@nginx.com 
376417Svbart@nginx.com static nxt_http_field_proc_t  nxt_http_test_bench_fields[] = {
377384Szelenkov@nginx.com     { nxt_string("Host"),
378384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
379384Szelenkov@nginx.com     { nxt_string("User-Agent"),
380384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
381417Svbart@nginx.com     { nxt_string("Accept"),
382417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
383384Szelenkov@nginx.com     { nxt_string("Accept-Encoding"),
384384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
385384Szelenkov@nginx.com     { nxt_string("Accept-Language"),
386384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
387384Szelenkov@nginx.com     { nxt_string("Connection"),
388384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
389384Szelenkov@nginx.com     { nxt_string("Content-Length"),
390384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
391417Svbart@nginx.com     { nxt_string("Content-Range"),
392417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
393384Szelenkov@nginx.com     { nxt_string("Content-Type"),
394384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
395417Svbart@nginx.com     { nxt_string("Cookie"),
396417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
397417Svbart@nginx.com     { nxt_string("Range"),
398417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
399417Svbart@nginx.com     { nxt_string("If-Range"),
400417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
401417Svbart@nginx.com     { nxt_string("Transfer-Encoding"),
402417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
403417Svbart@nginx.com     { nxt_string("Expect"),
404417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
405417Svbart@nginx.com     { nxt_string("Via"),
406417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
407384Szelenkov@nginx.com     { nxt_string("If-Modified-Since"),
408384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
409417Svbart@nginx.com     { nxt_string("If-Unmodified-Since"),
410417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
411384Szelenkov@nginx.com     { nxt_string("If-Match"),
412384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
413417Svbart@nginx.com     { nxt_string("If-None-Match"),
414417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
415417Svbart@nginx.com     { nxt_string("Referer"),
416417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
417384Szelenkov@nginx.com     { nxt_string("Date"),
418384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
419384Szelenkov@nginx.com     { nxt_string("Upgrade"),
420384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
421417Svbart@nginx.com     { nxt_string("Authorization"),
422417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
423417Svbart@nginx.com     { nxt_string("Keep-Alive"),
424417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
425384Szelenkov@nginx.com     { nxt_string("X-Forwarded-For"),
426384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
427417Svbart@nginx.com     { nxt_string("X-Forwarded-Host"),
428417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
429417Svbart@nginx.com     { nxt_string("X-Forwarded-Proto"),
430417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
431417Svbart@nginx.com     { nxt_string("X-Http-Method-Override"),
432417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
433417Svbart@nginx.com     { nxt_string("X-Real-IP"),
434417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
435384Szelenkov@nginx.com     { nxt_string("X-Request-ID"),
436384Szelenkov@nginx.com       &nxt_http_test_header_return, NXT_OK },
437417Svbart@nginx.com     { nxt_string("TE"),
438417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
439417Svbart@nginx.com     { nxt_string("Pragma"),
440417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
441417Svbart@nginx.com     { nxt_string("Cache-Control"),
442417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
443417Svbart@nginx.com     { nxt_string("Origin"),
444417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
445417Svbart@nginx.com     { nxt_string("Upgrade-Insecure-Requests"),
446417Svbart@nginx.com       &nxt_http_test_header_return, NXT_OK },
447384Szelenkov@nginx.com };
448384Szelenkov@nginx.com 
449384Szelenkov@nginx.com 
450384Szelenkov@nginx.com static nxt_str_t nxt_http_test_simple_request = nxt_string(
451384Szelenkov@nginx.com     "GET /page HTTP/1.1\r\n"
452384Szelenkov@nginx.com     "Host: example.com\r\n\r\n"
453384Szelenkov@nginx.com );
454384Szelenkov@nginx.com 
455384Szelenkov@nginx.com 
456384Szelenkov@nginx.com static nxt_str_t nxt_http_test_big_request = nxt_string(
457384Szelenkov@nginx.com     "POST /path/to/very/interesting/article/on.this.site?arg1=value&arg2=value"
458384Szelenkov@nginx.com         "2&very_big_arg=even_bigger_value HTTP/1.1\r\n"
459384Szelenkov@nginx.com     "Host: www.example.com\r\n"
460384Szelenkov@nginx.com     "User-Agent: Mozilla/5.0 (X11; Gentoo Linux x86_64; rv:42.0) Firefox/42.0"
461384Szelenkov@nginx.com         "\r\n"
462384Szelenkov@nginx.com     "Accept: text/html,application/json,application/xml;q=0.9,*/*;q=0.8\r\n"
463384Szelenkov@nginx.com     "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n"
464384Szelenkov@nginx.com     "Accept-Encoding: gzip, deflate, br\r\n"
465384Szelenkov@nginx.com     "If-Modified-Since: Wed, 31 Dec 1986 16:00:00 GMT\r\n"
466384Szelenkov@nginx.com     "Referer: https://example.org/path/to/not-interesting/article.html\r\n"
467384Szelenkov@nginx.com     "Cookie: name=value; name2=value2; some_big_cookie=iVBORw0KGgoAAAANSUhEUgA"
468384Szelenkov@nginx.com         "AAEAAAABACAMAAACdt4HsAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAABmelRY"
469384Szelenkov@nginx.com         "dFJhdyBwcm9maWxlIHR5cGUgZXhpZgAAeNptitsJgEAMBP9ThSWsZy6PcvKhcB1YvjEni"
470384Szelenkov@nginx.com         "ODAwjAs7ec4aCmkEXc1cREk7OwtUgyTFRA3BU+vFPjS7gUI/p46Q0u2fP/1B7oA1Scbwk"
471384Szelenkov@nginx.com         "nkf9gAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACfUExURQwMDDw8PFBQUAICAhQUFAcHBxs"
472384Szelenkov@nginx.com         "bGxEREQkJCTk5OTU1NSAgIFRUVB8fH0xMTCUlJVtbW0pKSikpKS8vL0BAQEZGRjMzM2Bg"
473384Szelenkov@nginx.com         "YL6+vsDAwLS0tF1dXXJycrGxsWVlZWhoaKenp29vb6urq8TExHp6epSUlLu7u66urqOjo"
474384Szelenkov@nginx.com         "5ycnH9/f4CAgJOTk5qamo6OjoWFhYiIiHd3d8nJyc/Pz9LS0ojXP1QAAAihSURBVFjDZV"
475384Szelenkov@nginx.com         "eHdqM6EBUYEEh0EM3gCu41+/7/294dCSfZsxOHeM8yV3f6iGVGYohNEtJPGEjPiSLpMTz"
476384Szelenkov@nginx.com         "zokg8DmGOCOm/P0I6MTPaBGDPCGEYV3kEzchjzPOSPIkk8BzuM8fSCOFfALER+6MdpnaV"
477384Szelenkov@nginx.com         "55FMoOP7UliioK8QzpiT0Qv0Fl4lDJvFPwChETuHFjhw7vhRVcGAXDqcfhhnRaZUWeJTW"
478384Szelenkov@nginx.com         "pYVCBEYAJihtCsUpIhyq6win3ueDCoRBIknJRwACtz3AJhDYBhESsmyEjhaKv0MRJIIFR"
479384Szelenkov@nginx.com         "d4XyYqC1RWwQFeBF2CcApCmEFI2KwHTRIrsMq8UnYcRUkehKtlaGeq8BjowKHEQf7oEgH"
480384Szelenkov@nginx.com         "JcKRWpSeZpTIrs5dKlGX9fF7GfrtdWqDAuce1IyOtLbWyRKRYIIIPBo63gswO07q20/p6"
481384Szelenkov@nginx.com         "2txvj+flvUZUZeQ4IODBGDoYivoReREzugaAJKuX637dP0/DbnMGwuWyTTNlBYX0ItL3E"
482384Szelenkov@nginx.com         "q2ptUmYZi9+ANLt9r2+nrqmORKD1/W9Xi3hirisEumQOz+qRv5hUL/H1bg7tG0znKbHCy"
483384Szelenkov@nginx.com         "Zs16u6TgmiQH5rLW2Ltslhf6kjO1bjOJ4PTfu1PwDgeR0BsF6BBCBQIThee+P78QvAQNS"
484384Szelenkov@nginx.com         "X17mD/tfXYaMBejAAhWWahqoiB5q8dmYQ9rc+AF7Trmn2BLC7vy4XQ0ADpHZmJRQPznVO"
485384Szelenkov@nginx.com         "0YcABJRnBwBg+Tofm3a//2q7zYREIAAyAQRQQKqAJ/ksH4CPC4wJy9uma2eA2+syjtsVn"
486384Szelenkov@nginx.com         "LicKzDTRYaqMgi/AQyHQNSPY0uyb7vdHVEcezDQBhAHJXLPqLOZxN8+CLJVehmapoUX2u"
487384Szelenkov@nginx.com         "54okzsIXACucAOYyunov62AUDiN0IQd69+dyAf7PfdsLlRGAGwXekowIgySRzoMzZzcAj"
488384Szelenkov@nginx.com         "gpxIs9Ti+TsTghLMvV1Lfbvt+vbTR9ZAJtlWoXxSIwaxuohCUt8Pp3LTd+XHt01KF9XZL"
489384Szelenkov@nginx.com         "iRhXkSwKCzYg7X2NwGYYJsRvCHU6nndNO3SH4TauV9v3OK7rUKHnUJaiTxRl4XODwD8mC"
490384Szelenkov@nginx.com         "Gptn0Q8j1e4oOmmfi0iZY/naRuWaIyiNI1bxDljs/7M4Hcxlta9fzTd/qubrrdYpNZ2GL"
491384Szelenkov@nginx.com         "ZxgJboFkmFVhGLLPE/6ubPp5nNTphOAGj/QHavtZ292t3KLouiQocqbXhRKOlr+/9hoA0"
492384Szelenkov@nginx.com         "og/d+dzi0/+2b7nTr60vXbtZhJkQZx2GaLsNMxZ8ozk5gphN/M4i79nBo/uwHdJPn1Db7"
493384Szelenkov@nginx.com         "c40aUgoDRVdTmhn3awbsXxOs4PZfc2i+vrrTNCEe+/0JnTmkoZOiJcT2co4i5z9hnHu6Z"
494384Szelenkov@nginx.com         "bxoT7sWAM3mfp9O7Vd7rnUV6E8ap2lk/MdmJzD2eyRohKrf4+DmON2ej6HZ31epnnqpLg"
495384Szelenkov@nginx.com         "ZV8dmFMw6fB0vww0Gs903ToJaviOifdnrXS6SxhgjjxNEF9BH6VlUVMKqf+STqPTLpeHr"
496384Szelenkov@nginx.com         "0l2HYHaYeHohVZiOIYUYjhjHfx0cLAHI96Qrzi4BXeYxiRi94PjeH4/k8xshgO8u0HYoI"
497384Szelenkov@nginx.com         "EIDvQgzEPOJIaGAlSSQQye54nzbH3Wb3wFSJ9SJAi0XAZ33NwXUXC5dJFIRHvZo7n0Z3J"
498384Szelenkov@nginx.com         "oDNaYef0zVd2bFZJjDzEmhByWfQ8bi/gDDpuz7NCa4RidhivT90w7B51tfXpV+F2CVEqd"
499384Szelenkov@nginx.com         "eamC+gj5cYznSYawCYwSPvEIbP3ArqXXdeXze3MUUNBJbSAGHgGuOZ7maazAfAoXnnaP8"
500384Szelenkov@nginx.com         "yN9kdj8fhjPY8TNt6FWchDTbsVB4s196jANI3XwNQPPXM9LSLmZ/Ae0f8nuGC2lhPK5md"
501384Szelenkov@nginx.com         "++zbh76B8V0Wmaz0aOB7epHy5XA4b3ZIgt1puvYYrCkaQZyhCrjZ1ehw+B//An2skMYLh"
502384Szelenkov@nginx.com         "GDCXB3b43Q6dhSL+7NHQ0YZYW3yyVfgyUwoOI1WABje3IkkBRMHRPmmPWxupyM4nF/jek"
503384Szelenkov@nginx.com         "mrp8pSSSqap++aSADA1ZuTtsLTewPgKmfadx2q8YwNZVwhDzJVZnbGfEcDOB8A/Y1wDAV"
504384Szelenkov@nginx.com         "iRxtHVLF321EiTJf3u0b+osLgglyTximcUQr6NJ2ZvwDAxwa9ejg8l7wcDsOAZLptwzgr"
505384Szelenkov@nginx.com         "LUXLdOC5nF5yPi6giFAYsbTwbwQHcRCejFCHA/lwwoZFZRBjvZlbGJ4mGylj8E27giJDo"
506384Szelenkov@nginx.com         "SQCsvJyR702xwGz8X5dp7qSMuy7lGcmhBrB13XxC8Asw7zIueBJ/brvEINHvzRLeSmS3C"
507384Szelenkov@nginx.com         "SfTgHDwaXKIOd5c4/RoYzrRHiOtbpOm8391dNuhXW3rECBzwC+qWQS+IAZABSBE+VoJzV"
508384Szelenkov@nginx.com         "6P+e5Wl9u9wlZRJtNjEXTLq1INwHdhvxZH9GkcFI8HFqAsWDLhYw5k0W8Hl8Y0fUSFxBs"
509384Szelenkov@nginx.com         "9CquLGFKQBfcDODPrQGPnPpRlADAiZEMCVb1/r0lAkjD0kq9xSJnmj/7NoEiYUxAElOOA"
510384Szelenkov@nginx.com         "SMoFgwAUhbKpnmANhTTFSXD+x6jEjJm+CaUXIdfJhFuN3RLy3GbcBcqYjJPKH8QwGWdod"
511384Szelenkov@nginx.com         "nbEgqOMQD6xpXQJ/fjelXlgKU9vghk4S0KwZIC15YSvXjZ15awslAHzP00008iUEE7oC4"
512384Szelenkov@nginx.com         "r7nKHerJAl18gGRGPAMwzez2GVpmFFhEAAKOe5CN6ZL6v0znPpVcluBMyj2ZDHhWLhciT"
513384Szelenkov@nginx.com         "Ctq4UKb9uIIfV3ChqzvJpxvpWBIeAOheSXQ8ZEEig2DhyjyqSqVoJ9j2W0y2knLW16dCd"
514384Szelenkov@nginx.com         "6EjyQ0a/E23IDDwowJ5IFJsMzJaRAEoxOFy1S+tXDAAcMdlxoP4w7UtnABQe0nhUa1HES"
515384Szelenkov@nginx.com         "5kVennooC/WWEpANRLK4mYjplkcy/ViU+n627I8gjXIJ9L5APiCDYiqFD7IIYLWKoKySj"
516384Szelenkov@nginx.com         "lUXleNM9TzcSfdxRGqlKijGALtTVJA7bgi0RVRaByyhjqP1S73BxPyjoeM47LPRqvVInU"
517384Szelenkov@nginx.com         "cvGoCit3GRpZ5VC0XZ1zpg6pb1AqLAhDD8L/AcHH1p8sEFAHAAAAAElFTkSuQmCC\r\n"
518384Szelenkov@nginx.com     "Connection: keep-alive\r\n"
519384Szelenkov@nginx.com     "Content-Length: 0\r\n"
520384Szelenkov@nginx.com     "Upgrade-Insecure-Requests: 1\r\n"
521384Szelenkov@nginx.com     "Pragma: no-cache\r\n"
522384Szelenkov@nginx.com     "Cache-Control: no-cache\r\n"
523384Szelenkov@nginx.com     "X-Forwarded-For: 192.0.2.0, 198.51.100.0, 203.0.113.0\r\n"
524384Szelenkov@nginx.com     "\r\n"
525384Szelenkov@nginx.com );
526384Szelenkov@nginx.com 
527384Szelenkov@nginx.com 
528384Szelenkov@nginx.com nxt_int_t
nxt_http_parse_test(nxt_thread_t * thr)529384Szelenkov@nginx.com nxt_http_parse_test(nxt_thread_t *thr)
530384Szelenkov@nginx.com {
5311459Smax.romanov@nginx.com     nxt_mp_t                    *mp_temp;
532384Szelenkov@nginx.com     nxt_int_t                   rc;
533417Svbart@nginx.com     nxt_uint_t                  i, colls, lvl_colls;
534417Svbart@nginx.com     nxt_lvlhsh_t                hash;
535384Szelenkov@nginx.com     nxt_http_request_parse_t    rp;
536384Szelenkov@nginx.com     nxt_http_parse_test_case_t  *test;
537384Szelenkov@nginx.com 
538384Szelenkov@nginx.com     nxt_thread_time_update(thr);
539384Szelenkov@nginx.com 
5401459Smax.romanov@nginx.com     rc = nxt_http_fields_hash(&nxt_http_test_fields_hash,
541417Svbart@nginx.com                               nxt_http_test_fields,
542417Svbart@nginx.com                               nxt_nitems(nxt_http_test_fields));
543417Svbart@nginx.com     if (rc != NXT_OK) {
544384Szelenkov@nginx.com         return NXT_ERROR;
545384Szelenkov@nginx.com     }
546384Szelenkov@nginx.com 
547384Szelenkov@nginx.com     for (i = 0; i < nxt_nitems(nxt_http_test_cases); i++) {
548384Szelenkov@nginx.com         test = &nxt_http_test_cases[i];
549384Szelenkov@nginx.com 
550384Szelenkov@nginx.com         nxt_memzero(&rp, sizeof(nxt_http_request_parse_t));
551384Szelenkov@nginx.com 
552384Szelenkov@nginx.com         mp_temp = nxt_mp_create(1024, 128, 256, 32);
553384Szelenkov@nginx.com         if (mp_temp == NULL) {
554384Szelenkov@nginx.com             return NXT_ERROR;
555384Szelenkov@nginx.com         }
556384Szelenkov@nginx.com 
557384Szelenkov@nginx.com         if (nxt_http_parse_request_init(&rp, mp_temp) != NXT_OK) {
558384Szelenkov@nginx.com             return NXT_ERROR;
559384Szelenkov@nginx.com         }
560384Szelenkov@nginx.com 
561*1709Svbart@nginx.com         if (test->handler == &nxt_http_parse_test_fields) {
562*1709Svbart@nginx.com             rp.discard_unsafe_fields = test->data.fields.discard_unsafe_fields;
563*1709Svbart@nginx.com         }
564*1709Svbart@nginx.com 
565384Szelenkov@nginx.com         rc = nxt_http_parse_test_run(&rp, &test->request);
566384Szelenkov@nginx.com 
567384Szelenkov@nginx.com         if (rc != test->result) {
568384Szelenkov@nginx.com             nxt_log_alert(thr->log, "http parse test case failed:\n"
569384Szelenkov@nginx.com                                     " - request:\n\"%V\"\n"
570384Szelenkov@nginx.com                                     " - result: %i (expected: %i)",
571384Szelenkov@nginx.com                                     &test->request, rc, test->result);
572384Szelenkov@nginx.com             return NXT_ERROR;
573384Szelenkov@nginx.com         }
574384Szelenkov@nginx.com 
575384Szelenkov@nginx.com         if (test->handler != NULL
576384Szelenkov@nginx.com             && test->handler(&rp, &test->data, &test->request, thr->log)
577384Szelenkov@nginx.com                != NXT_OK)
578384Szelenkov@nginx.com         {
579384Szelenkov@nginx.com             return NXT_ERROR;
580384Szelenkov@nginx.com         }
581384Szelenkov@nginx.com 
582384Szelenkov@nginx.com         nxt_mp_destroy(mp_temp);
583384Szelenkov@nginx.com     }
584384Szelenkov@nginx.com 
585384Szelenkov@nginx.com     nxt_log_error(NXT_LOG_NOTICE, thr->log, "http parse test passed");
586384Szelenkov@nginx.com 
587417Svbart@nginx.com     nxt_memzero(&hash, sizeof(nxt_lvlhsh_t));
588417Svbart@nginx.com 
5891459Smax.romanov@nginx.com     colls = nxt_http_fields_hash_collisions(&hash,
590417Svbart@nginx.com                                         nxt_http_test_bench_fields,
591417Svbart@nginx.com                                         nxt_nitems(nxt_http_test_bench_fields),
592417Svbart@nginx.com                                         0);
593417Svbart@nginx.com 
594417Svbart@nginx.com     nxt_memzero(&hash, sizeof(nxt_lvlhsh_t));
595417Svbart@nginx.com 
5961459Smax.romanov@nginx.com     lvl_colls = nxt_http_fields_hash_collisions(&hash,
597417Svbart@nginx.com                                         nxt_http_test_bench_fields,
598417Svbart@nginx.com                                         nxt_nitems(nxt_http_test_bench_fields),
599417Svbart@nginx.com                                         1);
600417Svbart@nginx.com 
601417Svbart@nginx.com     nxt_log_error(NXT_LOG_NOTICE, thr->log,
602494Spluknet@nginx.com                   "http parse test hash collisions %ui out of %uz, level: %ui",
603417Svbart@nginx.com                   colls, nxt_nitems(nxt_http_test_bench_fields), lvl_colls);
604417Svbart@nginx.com 
605417Svbart@nginx.com     nxt_memzero(&hash, sizeof(nxt_lvlhsh_t));
606417Svbart@nginx.com 
6071459Smax.romanov@nginx.com     rc = nxt_http_fields_hash(&hash, nxt_http_test_bench_fields,
608417Svbart@nginx.com                               nxt_nitems(nxt_http_test_bench_fields));
609417Svbart@nginx.com     if (rc != NXT_OK) {
610384Szelenkov@nginx.com         return NXT_ERROR;
611384Szelenkov@nginx.com     }
612384Szelenkov@nginx.com 
613384Szelenkov@nginx.com     if (nxt_http_parse_test_bench(thr, &nxt_http_test_simple_request,
614417Svbart@nginx.com                                   &hash, "simple", 1000000)
615384Szelenkov@nginx.com         != NXT_OK)
616384Szelenkov@nginx.com     {
617384Szelenkov@nginx.com         return NXT_ERROR;
618384Szelenkov@nginx.com     }
619384Szelenkov@nginx.com 
620384Szelenkov@nginx.com     if (nxt_http_parse_test_bench(thr, &nxt_http_test_big_request,
621417Svbart@nginx.com                                   &hash, "big", 100000)
622384Szelenkov@nginx.com         != NXT_OK)
623384Szelenkov@nginx.com     {
624384Szelenkov@nginx.com         return NXT_ERROR;
625384Szelenkov@nginx.com     }
626384Szelenkov@nginx.com 
627384Szelenkov@nginx.com     return NXT_OK;
628384Szelenkov@nginx.com }
629384Szelenkov@nginx.com 
630384Szelenkov@nginx.com 
631384Szelenkov@nginx.com static nxt_int_t
nxt_http_parse_test_run(nxt_http_request_parse_t * rp,nxt_str_t * request)632384Szelenkov@nginx.com nxt_http_parse_test_run(nxt_http_request_parse_t *rp, nxt_str_t *request)
633384Szelenkov@nginx.com {
634384Szelenkov@nginx.com     nxt_int_t      rc;
635384Szelenkov@nginx.com     nxt_buf_mem_t  buf;
636384Szelenkov@nginx.com 
637384Szelenkov@nginx.com     buf.start = request->start;
638384Szelenkov@nginx.com     buf.end = request->start + request->length;
639384Szelenkov@nginx.com 
640384Szelenkov@nginx.com     buf.pos = buf.start;
641384Szelenkov@nginx.com     buf.free = buf.pos + 1;
642384Szelenkov@nginx.com 
643384Szelenkov@nginx.com     do {
644384Szelenkov@nginx.com         buf.free++;
645384Szelenkov@nginx.com         rc = nxt_http_parse_request(rp, &buf);
646384Szelenkov@nginx.com     } while (buf.free < buf.end && rc == NXT_AGAIN);
647384Szelenkov@nginx.com 
648384Szelenkov@nginx.com     return rc;
649384Szelenkov@nginx.com }
650384Szelenkov@nginx.com 
651384Szelenkov@nginx.com 
652384Szelenkov@nginx.com static nxt_int_t
nxt_http_parse_test_bench(nxt_thread_t * thr,nxt_str_t * request,nxt_lvlhsh_t * hash,const char * name,nxt_uint_t n)653384Szelenkov@nginx.com nxt_http_parse_test_bench(nxt_thread_t *thr, nxt_str_t *request,
654417Svbart@nginx.com     nxt_lvlhsh_t *hash, const char *name, nxt_uint_t n)
655384Szelenkov@nginx.com {
656384Szelenkov@nginx.com     nxt_mp_t                  *mp;
657384Szelenkov@nginx.com     nxt_nsec_t                start, end;
658384Szelenkov@nginx.com     nxt_uint_t                i;
659384Szelenkov@nginx.com     nxt_buf_mem_t             buf;
660384Szelenkov@nginx.com     nxt_http_request_parse_t  rp;
661384Szelenkov@nginx.com 
662384Szelenkov@nginx.com     nxt_log_error(NXT_LOG_NOTICE, thr->log,
663384Szelenkov@nginx.com                   "http parse %s request bench started: %uz bytes, %ui runs",
664384Szelenkov@nginx.com                   name, request->length, n);
665384Szelenkov@nginx.com 
666384Szelenkov@nginx.com     buf.start = request->start;
667384Szelenkov@nginx.com     buf.end = request->start + request->length;
668384Szelenkov@nginx.com 
669384Szelenkov@nginx.com     nxt_thread_time_update(thr);
670384Szelenkov@nginx.com     start = nxt_thread_monotonic_time(thr);
671384Szelenkov@nginx.com 
672384Szelenkov@nginx.com     for (i = 0; nxt_fast_path(i < n); i++) {
673384Szelenkov@nginx.com         nxt_memzero(&rp, sizeof(nxt_http_request_parse_t));
674384Szelenkov@nginx.com 
675384Szelenkov@nginx.com         mp = nxt_mp_create(1024, 128, 256, 32);
676384Szelenkov@nginx.com         if (nxt_slow_path(mp == NULL)) {
677384Szelenkov@nginx.com             return NXT_ERROR;
678384Szelenkov@nginx.com         }
679384Szelenkov@nginx.com 
680384Szelenkov@nginx.com         if (nxt_slow_path(nxt_http_parse_request_init(&rp, mp) != NXT_OK)) {
681384Szelenkov@nginx.com             return NXT_ERROR;
682384Szelenkov@nginx.com         }
683384Szelenkov@nginx.com 
684384Szelenkov@nginx.com         buf.pos = buf.start;
685384Szelenkov@nginx.com         buf.free = buf.end;
686384Szelenkov@nginx.com 
687384Szelenkov@nginx.com         if (nxt_slow_path(nxt_http_parse_request(&rp, &buf) != NXT_DONE)) {
688384Szelenkov@nginx.com             nxt_log_alert(thr->log, "http parse %s request bench failed "
689384Szelenkov@nginx.com                                     "while parsing", name);
690384Szelenkov@nginx.com             return NXT_ERROR;
691384Szelenkov@nginx.com         }
692384Szelenkov@nginx.com 
693417Svbart@nginx.com         if (nxt_slow_path(nxt_http_fields_process(rp.fields, hash, NULL)
694384Szelenkov@nginx.com                           != NXT_OK))
695384Szelenkov@nginx.com         {
696384Szelenkov@nginx.com             nxt_log_alert(thr->log, "http parse %s request bench failed "
697384Szelenkov@nginx.com                                     "while fields processing", name);
698384Szelenkov@nginx.com             return NXT_ERROR;
699384Szelenkov@nginx.com         }
700384Szelenkov@nginx.com 
701384Szelenkov@nginx.com         nxt_mp_destroy(mp);
702384Szelenkov@nginx.com     }
703384Szelenkov@nginx.com 
704384Szelenkov@nginx.com     nxt_thread_time_update(thr);
705384Szelenkov@nginx.com     end = nxt_thread_monotonic_time(thr);
706384Szelenkov@nginx.com 
707384Szelenkov@nginx.com     nxt_log_error(NXT_LOG_NOTICE, thr->log,
708384Szelenkov@nginx.com                   "http parse %s request bench: %0.3fs",
709384Szelenkov@nginx.com                   name, (end - start) / 1000000000.0);
710384Szelenkov@nginx.com 
711384Szelenkov@nginx.com     return NXT_OK;
712384Szelenkov@nginx.com }
713384Szelenkov@nginx.com 
714384Szelenkov@nginx.com 
715384Szelenkov@nginx.com static nxt_int_t
nxt_http_parse_test_request_line(nxt_http_request_parse_t * rp,nxt_http_parse_test_data_t * data,nxt_str_t * request,nxt_log_t * log)716384Szelenkov@nginx.com nxt_http_parse_test_request_line(nxt_http_request_parse_t *rp,
717384Szelenkov@nginx.com     nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log)
718384Szelenkov@nginx.com {
719384Szelenkov@nginx.com     nxt_str_t  str;
720384Szelenkov@nginx.com 
721384Szelenkov@nginx.com     nxt_http_parse_test_request_line_t  *test = &data->request_line;
722384Szelenkov@nginx.com 
723384Szelenkov@nginx.com     if (rp->method.start != test->method.start
724384Szelenkov@nginx.com         && !nxt_strstr_eq(&rp->method, &test->method))
725384Szelenkov@nginx.com     {
726384Szelenkov@nginx.com         nxt_log_alert(log, "http parse test case failed:\n"
727384Szelenkov@nginx.com                            " - request:\n\"%V\"\n"
728384Szelenkov@nginx.com                            " - method: \"%V\" (expected: \"%V\")",
729384Szelenkov@nginx.com                            request, &rp->method, &test->method);
730384Szelenkov@nginx.com         return NXT_ERROR;
731384Szelenkov@nginx.com     }
732384Szelenkov@nginx.com 
733384Szelenkov@nginx.com     str.length = rp->target_end - rp->target_start;
734384Szelenkov@nginx.com     str.start = rp->target_start;
735384Szelenkov@nginx.com 
736384Szelenkov@nginx.com     if (str.start != test->target.start
737384Szelenkov@nginx.com         && !nxt_strstr_eq(&str, &test->target))
738384Szelenkov@nginx.com     {
739384Szelenkov@nginx.com         nxt_log_alert(log, "http parse test case failed:\n"
740384Szelenkov@nginx.com                             " - request:\n\"%V\"\n"
741384Szelenkov@nginx.com                             " - target: \"%V\" (expected: \"%V\")",
742384Szelenkov@nginx.com                             request, &str, &test->target);
743384Szelenkov@nginx.com         return NXT_ERROR;
744384Szelenkov@nginx.com     }
745384Szelenkov@nginx.com 
7461168Svbart@nginx.com     if (rp->args.start != test->args.start
7471168Svbart@nginx.com         && !nxt_strstr_eq(&rp->args, &test->args))
748384Szelenkov@nginx.com     {
749384Szelenkov@nginx.com         nxt_log_alert(log, "http parse test case failed:\n"
750384Szelenkov@nginx.com                            " - request:\n\"%V\"\n"
751384Szelenkov@nginx.com                            " - args: \"%V\" (expected: \"%V\")",
7521168Svbart@nginx.com                            request, &rp->args, &test->args);
753384Szelenkov@nginx.com         return NXT_ERROR;
754384Szelenkov@nginx.com     }
755384Szelenkov@nginx.com 
756384Szelenkov@nginx.com     if (nxt_memcmp(rp->version.str, test->version, 8) != 0) {
757384Szelenkov@nginx.com         nxt_log_alert(log, "http parse test case failed:\n"
758384Szelenkov@nginx.com                            " - request:\n\"%V\"\n"
759493Spluknet@nginx.com                            " - version: \"%*s\" (expected: \"%*s\")", request,
760493Spluknet@nginx.com                            (size_t) 8, rp->version.str,
761493Spluknet@nginx.com                            (size_t) 8, test->version);
762384Szelenkov@nginx.com         return NXT_ERROR;
763384Szelenkov@nginx.com     }
764384Szelenkov@nginx.com 
765*1709Svbart@nginx.com     if (rp->complex_target != (test->complex_target | test->quoted_target)) {
766384Szelenkov@nginx.com         nxt_log_alert(log, "http parse test case failed:\n"
767384Szelenkov@nginx.com                            " - request:\n\"%V\"\n"
768384Szelenkov@nginx.com                            " - complex_target: %d (expected: %d)",
769384Szelenkov@nginx.com                            request, rp->complex_target, test->complex_target);
770384Szelenkov@nginx.com         return NXT_ERROR;
771384Szelenkov@nginx.com     }
772384Szelenkov@nginx.com 
773*1709Svbart@nginx.com #if 0
774384Szelenkov@nginx.com     if (rp->quoted_target != test->quoted_target) {
775384Szelenkov@nginx.com         nxt_log_alert(log, "http parse test case failed:\n"
776384Szelenkov@nginx.com                            " - request:\n\"%V\"\n"
777384Szelenkov@nginx.com                            " - quoted_target: %d (expected: %d)",
778384Szelenkov@nginx.com                            request, rp->quoted_target, test->quoted_target);
779384Szelenkov@nginx.com         return NXT_ERROR;
780384Szelenkov@nginx.com     }
781384Szelenkov@nginx.com 
782384Szelenkov@nginx.com     if (rp->space_in_target != test->space_in_target) {
783384Szelenkov@nginx.com         nxt_log_alert(log, "http parse test case failed:\n"
784384Szelenkov@nginx.com                            " - request:\n\"%V\"\n"
785384Szelenkov@nginx.com                            " - space_in_target: %d (expected: %d)",
786384Szelenkov@nginx.com                            request, rp->space_in_target, test->space_in_target);
787384Szelenkov@nginx.com         return NXT_ERROR;
788384Szelenkov@nginx.com     }
789*1709Svbart@nginx.com #endif
790384Szelenkov@nginx.com 
791384Szelenkov@nginx.com     return NXT_OK;
792384Szelenkov@nginx.com }
793384Szelenkov@nginx.com 
794384Szelenkov@nginx.com 
795384Szelenkov@nginx.com static nxt_int_t
nxt_http_parse_test_fields(nxt_http_request_parse_t * rp,nxt_http_parse_test_data_t * data,nxt_str_t * request,nxt_log_t * log)796384Szelenkov@nginx.com nxt_http_parse_test_fields(nxt_http_request_parse_t *rp,
797384Szelenkov@nginx.com     nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log)
798384Szelenkov@nginx.com {
799384Szelenkov@nginx.com     nxt_int_t  rc;
800384Szelenkov@nginx.com 
801417Svbart@nginx.com     rc = nxt_http_fields_process(rp->fields, &nxt_http_test_fields_hash, NULL);
802384Szelenkov@nginx.com 
803*1709Svbart@nginx.com     if (rc != data->fields.result) {
804384Szelenkov@nginx.com         nxt_log_alert(log, "http parse test hash failed:\n"
805384Szelenkov@nginx.com                            " - request:\n\"%V\"\n"
806384Szelenkov@nginx.com                            " - result: %i (expected: %i)",
807*1709Svbart@nginx.com                            request, rc, data->fields.result);
808384Szelenkov@nginx.com         return NXT_ERROR;
809384Szelenkov@nginx.com     }
810384Szelenkov@nginx.com 
811384Szelenkov@nginx.com     return NXT_OK;
812384Szelenkov@nginx.com }
813384Szelenkov@nginx.com 
814384Szelenkov@nginx.com 
815384Szelenkov@nginx.com static nxt_int_t
nxt_http_test_header_return(void * ctx,nxt_http_field_t * field,uintptr_t data)816417Svbart@nginx.com nxt_http_test_header_return(void *ctx, nxt_http_field_t *field, uintptr_t data)
817384Szelenkov@nginx.com {
818417Svbart@nginx.com     return data;
819384Szelenkov@nginx.com }
820