xref: /unit/src/nxt_http_request.c (revision 2746:2adebf2bf59c)
1431Sigor@sysoev.ru 
2431Sigor@sysoev.ru /*
3431Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
4431Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
5431Sigor@sysoev.ru  */
6431Sigor@sysoev.ru 
7431Sigor@sysoev.ru #include <nxt_router.h>
8431Sigor@sysoev.ru #include <nxt_http.h>
9431Sigor@sysoev.ru 
10431Sigor@sysoev.ru 
11945Svbart@nginx.com static nxt_int_t nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp);
12431Sigor@sysoev.ru static void nxt_http_request_start(nxt_task_t *task, void *obj, void *data);
132133Sz.hong@f5.com static nxt_int_t nxt_http_request_forward(nxt_task_t *task,
142133Sz.hong@f5.com     nxt_http_request_t *r, nxt_http_forward_t *forward);
152133Sz.hong@f5.com static void nxt_http_request_forward_client_ip(nxt_http_request_t *r,
162133Sz.hong@f5.com     nxt_http_forward_t *forward, nxt_array_t *fields);
171936So.canty@f5.com static nxt_sockaddr_t *nxt_http_request_client_ip_sockaddr(
181936So.canty@f5.com     nxt_http_request_t *r, u_char *start, size_t len);
192133Sz.hong@f5.com static void nxt_http_request_forward_protocol(nxt_http_request_t *r,
202133Sz.hong@f5.com     nxt_http_field_t *field);
211563Svbart@nginx.com static void nxt_http_request_ready(nxt_task_t *task, void *obj, void *data);
221011Smax.romanov@nginx.com static void nxt_http_request_proto_info(nxt_task_t *task,
231011Smax.romanov@nginx.com     nxt_http_request_t *r);
24608Sigor@sysoev.ru static void nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj,
25608Sigor@sysoev.ru     void *data);
26431Sigor@sysoev.ru static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data);
272625Sz.hong@f5.com static nxt_int_t nxt_http_request_access_log(nxt_task_t *task,
282625Sz.hong@f5.com     nxt_http_request_t *r, nxt_router_conf_t *rtcf);
29431Sigor@sysoev.ru 
301183Svbart@nginx.com static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now,
311183Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
32543Svbart@nginx.com 
332104Sz.hong@f5.com static nxt_http_name_value_t *nxt_http_argument(nxt_array_t *array,
342104Sz.hong@f5.com     u_char *name, size_t name_length, uint32_t hash, u_char *start,
352139Sandrew@digital-domain.net     const u_char *end);
362104Sz.hong@f5.com static nxt_int_t nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start,
372139Sandrew@digital-domain.net     const u_char *end);
382104Sz.hong@f5.com static nxt_http_name_value_t *nxt_http_cookie(nxt_array_t *array, u_char *name,
392139Sandrew@digital-domain.net     size_t name_length, u_char *start, const u_char *end);
402104Sz.hong@f5.com 
412104Sz.hong@f5.com 
422104Sz.hong@f5.com #define NXT_HTTP_COOKIE_HASH                                                  \
432104Sz.hong@f5.com     (nxt_http_field_hash_end(                                                 \
442104Sz.hong@f5.com      nxt_http_field_hash_char(                                                \
452104Sz.hong@f5.com      nxt_http_field_hash_char(                                                \
462104Sz.hong@f5.com      nxt_http_field_hash_char(                                                \
472104Sz.hong@f5.com      nxt_http_field_hash_char(                                                \
482104Sz.hong@f5.com      nxt_http_field_hash_char(                                                \
492104Sz.hong@f5.com      nxt_http_field_hash_char(NXT_HTTP_FIELD_HASH_INIT,                       \
502104Sz.hong@f5.com         'c'), 'o'), 'o'), 'k'), 'i'), 'e')) & 0xFFFF)
512104Sz.hong@f5.com 
52431Sigor@sysoev.ru 
53431Sigor@sysoev.ru static const nxt_http_request_state_t  nxt_http_request_init_state;
54431Sigor@sysoev.ru static const nxt_http_request_state_t  nxt_http_request_body_state;
55431Sigor@sysoev.ru 
56431Sigor@sysoev.ru 
57740Sigor@sysoev.ru nxt_time_string_t  nxt_http_date_cache = {
58740Sigor@sysoev.ru     (nxt_atomic_uint_t) -1,
591183Svbart@nginx.com     nxt_http_date_cache_handler,
601183Svbart@nginx.com     NULL,
611183Svbart@nginx.com     NXT_HTTP_DATE_LEN,
62740Sigor@sysoev.ru     NXT_THREAD_TIME_GMT,
63740Sigor@sysoev.ru     NXT_THREAD_TIME_SEC,
64740Sigor@sysoev.ru };
65740Sigor@sysoev.ru 
66740Sigor@sysoev.ru 
67431Sigor@sysoev.ru nxt_int_t
nxt_http_init(nxt_task_t * task)681459Smax.romanov@nginx.com nxt_http_init(nxt_task_t *task)
69431Sigor@sysoev.ru {
70431Sigor@sysoev.ru     nxt_int_t  ret;
71431Sigor@sysoev.ru 
721459Smax.romanov@nginx.com     ret = nxt_h1p_init(task);
73431Sigor@sysoev.ru 
74431Sigor@sysoev.ru     if (ret != NXT_OK) {
75431Sigor@sysoev.ru         return ret;
76431Sigor@sysoev.ru     }
77431Sigor@sysoev.ru 
781459Smax.romanov@nginx.com     return nxt_http_response_hash_init(task);
79431Sigor@sysoev.ru }
80431Sigor@sysoev.ru 
81431Sigor@sysoev.ru 
82431Sigor@sysoev.ru nxt_int_t
nxt_http_request_host(void * ctx,nxt_http_field_t * field,uintptr_t data)83431Sigor@sysoev.ru nxt_http_request_host(void *ctx, nxt_http_field_t *field, uintptr_t data)
84431Sigor@sysoev.ru {
85945Svbart@nginx.com     nxt_int_t           ret;
86945Svbart@nginx.com     nxt_str_t           host;
87431Sigor@sysoev.ru     nxt_http_request_t  *r;
88431Sigor@sysoev.ru 
89431Sigor@sysoev.ru     r = ctx;
90431Sigor@sysoev.ru 
91945Svbart@nginx.com     if (nxt_slow_path(r->host.start != NULL)) {
92945Svbart@nginx.com         return NXT_HTTP_BAD_REQUEST;
93945Svbart@nginx.com     }
94945Svbart@nginx.com 
95945Svbart@nginx.com     host.length = field->value_length;
96945Svbart@nginx.com     host.start = field->value;
97945Svbart@nginx.com 
98945Svbart@nginx.com     ret = nxt_http_validate_host(&host, r->mem_pool);
99945Svbart@nginx.com 
100945Svbart@nginx.com     if (nxt_fast_path(ret == NXT_OK)) {
101945Svbart@nginx.com         r->host = host;
102945Svbart@nginx.com     }
103945Svbart@nginx.com 
104945Svbart@nginx.com     return ret;
105945Svbart@nginx.com }
106945Svbart@nginx.com 
107945Svbart@nginx.com 
108945Svbart@nginx.com static nxt_int_t
nxt_http_validate_host(nxt_str_t * host,nxt_mp_t * mp)109945Svbart@nginx.com nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp)
110945Svbart@nginx.com {
111945Svbart@nginx.com     u_char      *h, ch;
112945Svbart@nginx.com     size_t      i, dot_pos, host_length;
113945Svbart@nginx.com     nxt_bool_t  lowcase;
114945Svbart@nginx.com 
115945Svbart@nginx.com     enum {
116945Svbart@nginx.com         sw_usual,
117945Svbart@nginx.com         sw_literal,
118945Svbart@nginx.com         sw_rest
119945Svbart@nginx.com     } state;
120945Svbart@nginx.com 
121945Svbart@nginx.com     dot_pos = host->length;
122945Svbart@nginx.com     host_length = host->length;
123945Svbart@nginx.com 
124945Svbart@nginx.com     h = host->start;
125945Svbart@nginx.com 
126945Svbart@nginx.com     lowcase = 0;
127945Svbart@nginx.com     state = sw_usual;
128945Svbart@nginx.com 
129945Svbart@nginx.com     for (i = 0; i < host->length; i++) {
130945Svbart@nginx.com         ch = h[i];
131945Svbart@nginx.com 
132945Svbart@nginx.com         if (ch > ']') {
133945Svbart@nginx.com             /* Short path. */
134945Svbart@nginx.com             continue;
135945Svbart@nginx.com         }
136945Svbart@nginx.com 
137945Svbart@nginx.com         switch (ch) {
138945Svbart@nginx.com 
139945Svbart@nginx.com         case '.':
140945Svbart@nginx.com             if (dot_pos == i - 1) {
141945Svbart@nginx.com                 return NXT_HTTP_BAD_REQUEST;
142945Svbart@nginx.com             }
143431Sigor@sysoev.ru 
144945Svbart@nginx.com             dot_pos = i;
145945Svbart@nginx.com             break;
146945Svbart@nginx.com 
147945Svbart@nginx.com         case ':':
148945Svbart@nginx.com             if (state == sw_usual) {
149945Svbart@nginx.com                 host_length = i;
150945Svbart@nginx.com                 state = sw_rest;
151945Svbart@nginx.com             }
152945Svbart@nginx.com 
153945Svbart@nginx.com             break;
154945Svbart@nginx.com 
155945Svbart@nginx.com         case '[':
156945Svbart@nginx.com             if (i == 0) {
157945Svbart@nginx.com                 state = sw_literal;
158945Svbart@nginx.com             }
159945Svbart@nginx.com 
160945Svbart@nginx.com             break;
161945Svbart@nginx.com 
162945Svbart@nginx.com         case ']':
163945Svbart@nginx.com             if (state == sw_literal) {
164945Svbart@nginx.com                 host_length = i + 1;
165945Svbart@nginx.com                 state = sw_rest;
166945Svbart@nginx.com             }
167945Svbart@nginx.com 
168945Svbart@nginx.com             break;
169945Svbart@nginx.com 
170945Svbart@nginx.com         case '/':
171945Svbart@nginx.com             return NXT_HTTP_BAD_REQUEST;
172945Svbart@nginx.com 
173945Svbart@nginx.com         default:
174945Svbart@nginx.com             if (ch >= 'A' && ch <= 'Z') {
175945Svbart@nginx.com                 lowcase = 1;
176945Svbart@nginx.com             }
177945Svbart@nginx.com 
178945Svbart@nginx.com             break;
179945Svbart@nginx.com         }
180945Svbart@nginx.com     }
181945Svbart@nginx.com 
182945Svbart@nginx.com     if (dot_pos == host_length - 1) {
183945Svbart@nginx.com         host_length--;
184945Svbart@nginx.com     }
185945Svbart@nginx.com 
186945Svbart@nginx.com     host->length = host_length;
187945Svbart@nginx.com 
188945Svbart@nginx.com     if (lowcase) {
189945Svbart@nginx.com         host->start = nxt_mp_nget(mp, host_length);
190945Svbart@nginx.com         if (nxt_slow_path(host->start == NULL)) {
191945Svbart@nginx.com             return NXT_HTTP_INTERNAL_SERVER_ERROR;
192945Svbart@nginx.com         }
193945Svbart@nginx.com 
194945Svbart@nginx.com         nxt_memcpy_lowcase(host->start, h, host_length);
195945Svbart@nginx.com     }
196431Sigor@sysoev.ru 
197431Sigor@sysoev.ru     return NXT_OK;
198431Sigor@sysoev.ru }
199431Sigor@sysoev.ru 
200431Sigor@sysoev.ru 
201431Sigor@sysoev.ru nxt_int_t
nxt_http_request_field(void * ctx,nxt_http_field_t * field,uintptr_t offset)202431Sigor@sysoev.ru nxt_http_request_field(void *ctx, nxt_http_field_t *field, uintptr_t offset)
203431Sigor@sysoev.ru {
204431Sigor@sysoev.ru     nxt_http_request_t  *r;
205431Sigor@sysoev.ru 
206431Sigor@sysoev.ru     r = ctx;
207431Sigor@sysoev.ru 
208431Sigor@sysoev.ru     nxt_value_at(nxt_http_field_t *, r, offset) = field;
209431Sigor@sysoev.ru 
210431Sigor@sysoev.ru     return NXT_OK;
211431Sigor@sysoev.ru }
212431Sigor@sysoev.ru 
213431Sigor@sysoev.ru 
214431Sigor@sysoev.ru nxt_int_t
nxt_http_request_content_length(void * ctx,nxt_http_field_t * field,uintptr_t data)215431Sigor@sysoev.ru nxt_http_request_content_length(void *ctx, nxt_http_field_t *field,
216431Sigor@sysoev.ru     uintptr_t data)
217431Sigor@sysoev.ru {
2181401Smax.romanov@nginx.com     nxt_off_t           n, max_body_size;
219431Sigor@sysoev.ru     nxt_http_request_t  *r;
220431Sigor@sysoev.ru 
221431Sigor@sysoev.ru     r = ctx;
222431Sigor@sysoev.ru 
223942Svbart@nginx.com     if (nxt_fast_path(r->content_length == NULL)) {
224942Svbart@nginx.com         r->content_length = field;
225431Sigor@sysoev.ru 
226942Svbart@nginx.com         n = nxt_off_t_parse(field->value, field->value_length);
227942Svbart@nginx.com 
228942Svbart@nginx.com         if (nxt_fast_path(n >= 0)) {
229942Svbart@nginx.com             r->content_length_n = n;
2301401Smax.romanov@nginx.com 
2311401Smax.romanov@nginx.com             max_body_size = r->conf->socket_conf->max_body_size;
2321401Smax.romanov@nginx.com 
2331401Smax.romanov@nginx.com             if (nxt_slow_path(n > max_body_size)) {
2341401Smax.romanov@nginx.com                 return NXT_HTTP_PAYLOAD_TOO_LARGE;
2351401Smax.romanov@nginx.com             }
2361401Smax.romanov@nginx.com 
237942Svbart@nginx.com             return NXT_OK;
238942Svbart@nginx.com         }
239918Svbart@nginx.com     }
240918Svbart@nginx.com 
241945Svbart@nginx.com     return NXT_HTTP_BAD_REQUEST;
242431Sigor@sysoev.ru }
243431Sigor@sysoev.ru 
244431Sigor@sysoev.ru 
245431Sigor@sysoev.ru nxt_http_request_t *
nxt_http_request_create(nxt_task_t * task)246431Sigor@sysoev.ru nxt_http_request_create(nxt_task_t *task)
247431Sigor@sysoev.ru {
248431Sigor@sysoev.ru     nxt_mp_t            *mp;
249608Sigor@sysoev.ru     nxt_buf_t           *last;
250431Sigor@sysoev.ru     nxt_http_request_t  *r;
251431Sigor@sysoev.ru 
2521656Svbart@nginx.com     mp = nxt_mp_create(4096, 128, 512, 32);
253431Sigor@sysoev.ru     if (nxt_slow_path(mp == NULL)) {
254431Sigor@sysoev.ru         return NULL;
255431Sigor@sysoev.ru     }
256431Sigor@sysoev.ru 
257431Sigor@sysoev.ru     r = nxt_mp_zget(mp, sizeof(nxt_http_request_t));
258431Sigor@sysoev.ru     if (nxt_slow_path(r == NULL)) {
259431Sigor@sysoev.ru         goto fail;
260431Sigor@sysoev.ru     }
261431Sigor@sysoev.ru 
262431Sigor@sysoev.ru     r->resp.fields = nxt_list_create(mp, 8, sizeof(nxt_http_field_t));
263451Sigor@sysoev.ru     if (nxt_slow_path(r->resp.fields == NULL)) {
264431Sigor@sysoev.ru         goto fail;
265431Sigor@sysoev.ru     }
266431Sigor@sysoev.ru 
267608Sigor@sysoev.ru     last = nxt_mp_zget(mp, NXT_BUF_SYNC_SIZE);
268608Sigor@sysoev.ru     if (nxt_slow_path(last == NULL)) {
269608Sigor@sysoev.ru         goto fail;
270608Sigor@sysoev.ru     }
271608Sigor@sysoev.ru 
272608Sigor@sysoev.ru     nxt_buf_set_sync(last);
273608Sigor@sysoev.ru     nxt_buf_set_last(last);
274608Sigor@sysoev.ru     last->completion_handler = nxt_http_request_done;
275608Sigor@sysoev.ru     last->parent = r;
276608Sigor@sysoev.ru     r->last = last;
277608Sigor@sysoev.ru 
278431Sigor@sysoev.ru     r->mem_pool = mp;
279431Sigor@sysoev.ru     r->content_length_n = -1;
280431Sigor@sysoev.ru     r->resp.content_length_n = -1;
281431Sigor@sysoev.ru     r->state = &nxt_http_request_init_state;
282431Sigor@sysoev.ru 
2832214Sz.hong@f5.com     r->start_time = nxt_thread_monotonic_time(task->thread);
2842214Sz.hong@f5.com 
2852186Sz.hong@f5.com     task->thread->engine->requests_cnt++;
2862186Sz.hong@f5.com 
2872247Sz.hong@f5.com     r->tstr_cache.var.pool = mp;
2882246Sz.hong@f5.com 
289431Sigor@sysoev.ru     return r;
290431Sigor@sysoev.ru 
291431Sigor@sysoev.ru fail:
292431Sigor@sysoev.ru 
293431Sigor@sysoev.ru     nxt_mp_release(mp);
294608Sigor@sysoev.ru 
295431Sigor@sysoev.ru     return NULL;
296431Sigor@sysoev.ru }
297431Sigor@sysoev.ru 
298431Sigor@sysoev.ru 
299431Sigor@sysoev.ru static const nxt_http_request_state_t  nxt_http_request_init_state
300431Sigor@sysoev.ru     nxt_aligned(64) =
301431Sigor@sysoev.ru {
302431Sigor@sysoev.ru     .ready_handler = nxt_http_request_start,
303431Sigor@sysoev.ru     .error_handler = nxt_http_request_close_handler,
304431Sigor@sysoev.ru };
305431Sigor@sysoev.ru 
306431Sigor@sysoev.ru 
307431Sigor@sysoev.ru static void
nxt_http_request_start(nxt_task_t * task,void * obj,void * data)308431Sigor@sysoev.ru nxt_http_request_start(nxt_task_t *task, void *obj, void *data)
309431Sigor@sysoev.ru {
3101936So.canty@f5.com     nxt_int_t           ret;
3112133Sz.hong@f5.com     nxt_socket_conf_t   *skcf;
312431Sigor@sysoev.ru     nxt_http_request_t  *r;
313431Sigor@sysoev.ru 
314431Sigor@sysoev.ru     r = obj;
315431Sigor@sysoev.ru 
316431Sigor@sysoev.ru     r->state = &nxt_http_request_body_state;
317431Sigor@sysoev.ru 
3182133Sz.hong@f5.com     skcf = r->conf->socket_conf;
3192133Sz.hong@f5.com 
3202133Sz.hong@f5.com     if (skcf->forwarded != NULL) {
3212133Sz.hong@f5.com         ret = nxt_http_request_forward(task, r, skcf->forwarded);
3222133Sz.hong@f5.com         if (nxt_slow_path(ret != NXT_OK)) {
3232133Sz.hong@f5.com             goto fail;
3242133Sz.hong@f5.com         }
3252133Sz.hong@f5.com     }
3262133Sz.hong@f5.com 
3272133Sz.hong@f5.com     if (skcf->client_ip != NULL) {
3282133Sz.hong@f5.com         ret = nxt_http_request_forward(task, r, skcf->client_ip);
3292133Sz.hong@f5.com         if (nxt_slow_path(ret != NXT_OK)) {
3302133Sz.hong@f5.com             goto fail;
3312133Sz.hong@f5.com         }
3321936So.canty@f5.com     }
3331936So.canty@f5.com 
334431Sigor@sysoev.ru     nxt_http_request_read_body(task, r);
3352133Sz.hong@f5.com 
3362133Sz.hong@f5.com     return;
3372133Sz.hong@f5.com 
3382133Sz.hong@f5.com fail:
3392133Sz.hong@f5.com     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
340431Sigor@sysoev.ru }
341431Sigor@sysoev.ru 
342431Sigor@sysoev.ru 
3431936So.canty@f5.com static nxt_int_t
nxt_http_request_forward(nxt_task_t * task,nxt_http_request_t * r,nxt_http_forward_t * forward)3442133Sz.hong@f5.com nxt_http_request_forward(nxt_task_t *task, nxt_http_request_t *r,
3452133Sz.hong@f5.com     nxt_http_forward_t *forward)
3461936So.canty@f5.com {
3472133Sz.hong@f5.com     nxt_int_t                  ret;
3482133Sz.hong@f5.com     nxt_array_t                *client_ip_fields;
3492133Sz.hong@f5.com     nxt_http_field_t           *f, **fields, *protocol_field;
3502133Sz.hong@f5.com     nxt_http_forward_header_t  *client_ip, *protocol;
3511936So.canty@f5.com 
3522132Sz.hong@f5.com     ret = nxt_http_route_addr_rule(r, forward->source, r->remote);
3531936So.canty@f5.com     if (ret <= 0) {
3541936So.canty@f5.com         return NXT_OK;
3551936So.canty@f5.com     }
3561936So.canty@f5.com 
3572132Sz.hong@f5.com     client_ip = &forward->client_ip;
3582133Sz.hong@f5.com     protocol = &forward->protocol;
3591936So.canty@f5.com 
3602133Sz.hong@f5.com     if (client_ip->header != NULL) {
3612133Sz.hong@f5.com         client_ip_fields = nxt_array_create(r->mem_pool, 1,
3622133Sz.hong@f5.com                                             sizeof(nxt_http_field_t *));
3632133Sz.hong@f5.com         if (nxt_slow_path(client_ip_fields == NULL)) {
3642133Sz.hong@f5.com             return NXT_ERROR;
3652133Sz.hong@f5.com         }
3662133Sz.hong@f5.com 
3672133Sz.hong@f5.com     } else {
3682133Sz.hong@f5.com         client_ip_fields = NULL;
3691936So.canty@f5.com     }
3701936So.canty@f5.com 
3712133Sz.hong@f5.com     protocol_field = NULL;
3722133Sz.hong@f5.com 
3731936So.canty@f5.com     nxt_list_each(f, r->fields) {
3742133Sz.hong@f5.com         if (client_ip_fields != NULL
3752133Sz.hong@f5.com             && f->hash == client_ip->header_hash
3761936So.canty@f5.com             && f->value_length > 0
3772133Sz.hong@f5.com             && f->name_length == client_ip->header->length
3782133Sz.hong@f5.com             && nxt_memcasecmp(f->name, client_ip->header->start,
3792133Sz.hong@f5.com                               client_ip->header->length) == 0)
3801936So.canty@f5.com         {
3812133Sz.hong@f5.com             fields = nxt_array_add(client_ip_fields);
3821936So.canty@f5.com             if (nxt_slow_path(fields == NULL)) {
3831936So.canty@f5.com                 return NXT_ERROR;
3841936So.canty@f5.com             }
3851936So.canty@f5.com 
3861936So.canty@f5.com             *fields = f;
3871936So.canty@f5.com         }
3882133Sz.hong@f5.com 
3892133Sz.hong@f5.com         if (protocol->header != NULL
3902133Sz.hong@f5.com             && protocol_field == NULL
3912133Sz.hong@f5.com             && f->hash == protocol->header_hash
3922133Sz.hong@f5.com             && f->value_length > 0
3932133Sz.hong@f5.com             && f->name_length == protocol->header->length
3942133Sz.hong@f5.com             && nxt_memcasecmp(f->name, protocol->header->start,
3952133Sz.hong@f5.com                               protocol->header->length) == 0)
3962133Sz.hong@f5.com         {
3972133Sz.hong@f5.com             protocol_field = f;
3982133Sz.hong@f5.com         }
3991936So.canty@f5.com     } nxt_list_loop;
4001936So.canty@f5.com 
4012133Sz.hong@f5.com     if (client_ip_fields != NULL) {
4022133Sz.hong@f5.com         nxt_http_request_forward_client_ip(r, forward, client_ip_fields);
4032133Sz.hong@f5.com     }
4042133Sz.hong@f5.com 
4052133Sz.hong@f5.com     if (protocol_field != NULL) {
4062133Sz.hong@f5.com         nxt_http_request_forward_protocol(r, protocol_field);
4072133Sz.hong@f5.com     }
4082133Sz.hong@f5.com 
4092133Sz.hong@f5.com     return NXT_OK;
4102133Sz.hong@f5.com }
4112133Sz.hong@f5.com 
4122133Sz.hong@f5.com 
4132133Sz.hong@f5.com static void
nxt_http_request_forward_client_ip(nxt_http_request_t * r,nxt_http_forward_t * forward,nxt_array_t * fields)4142133Sz.hong@f5.com nxt_http_request_forward_client_ip(nxt_http_request_t *r,
4152133Sz.hong@f5.com     nxt_http_forward_t *forward, nxt_array_t *fields)
4162133Sz.hong@f5.com {
4172133Sz.hong@f5.com     u_char            *start, *p;
4182133Sz.hong@f5.com     nxt_int_t         ret, i, len;
4192133Sz.hong@f5.com     nxt_sockaddr_t    *sa, *prev_sa;
4202133Sz.hong@f5.com     nxt_http_field_t  **f;
4212133Sz.hong@f5.com 
4221936So.canty@f5.com     prev_sa = r->remote;
4232133Sz.hong@f5.com     f = (nxt_http_field_t **) fields->elts;
4241936So.canty@f5.com 
4252133Sz.hong@f5.com     i = fields->nelts;
4261936So.canty@f5.com 
4271936So.canty@f5.com     while (i-- > 0) {
4282133Sz.hong@f5.com         start = f[i]->value;
4292133Sz.hong@f5.com         len = f[i]->value_length;
4301936So.canty@f5.com 
4311936So.canty@f5.com         do {
4321936So.canty@f5.com             for (p = start + len - 1; p > start; p--, len--) {
4331936So.canty@f5.com                 if (*p != ' ' && *p != ',') {
4341936So.canty@f5.com                     break;
4351936So.canty@f5.com                 }
4361936So.canty@f5.com             }
4371936So.canty@f5.com 
4381936So.canty@f5.com             for (/* void */; p > start; p--) {
4391936So.canty@f5.com                 if (*p == ' ' || *p == ',') {
4401936So.canty@f5.com                     p++;
4411936So.canty@f5.com                     break;
4421936So.canty@f5.com                 }
4431936So.canty@f5.com             }
4441936So.canty@f5.com 
4451936So.canty@f5.com             sa = nxt_http_request_client_ip_sockaddr(r, p, len - (p - start));
4461936So.canty@f5.com             if (nxt_slow_path(sa == NULL)) {
4471936So.canty@f5.com                 if (prev_sa != NULL) {
4481936So.canty@f5.com                     r->remote = prev_sa;
4491936So.canty@f5.com                 }
4501936So.canty@f5.com 
4512133Sz.hong@f5.com                 return;
4521936So.canty@f5.com             }
4531936So.canty@f5.com 
4542132Sz.hong@f5.com             if (!forward->recursive) {
4551936So.canty@f5.com                 r->remote = sa;
4562133Sz.hong@f5.com                 return;
4571936So.canty@f5.com             }
4581936So.canty@f5.com 
4592132Sz.hong@f5.com             ret = nxt_http_route_addr_rule(r, forward->source, sa);
4601936So.canty@f5.com             if (ret <= 0 || (i == 0 && p == start)) {
4611936So.canty@f5.com                 r->remote = sa;
4622133Sz.hong@f5.com                 return;
4631936So.canty@f5.com             }
4641936So.canty@f5.com 
4651936So.canty@f5.com             prev_sa = sa;
4661936So.canty@f5.com             len = p - 1 - start;
4671936So.canty@f5.com 
4681936So.canty@f5.com         } while (len > 0);
4691936So.canty@f5.com     }
4701936So.canty@f5.com }
4711936So.canty@f5.com 
4721936So.canty@f5.com 
4731936So.canty@f5.com static nxt_sockaddr_t *
nxt_http_request_client_ip_sockaddr(nxt_http_request_t * r,u_char * start,size_t len)4741936So.canty@f5.com nxt_http_request_client_ip_sockaddr(nxt_http_request_t *r, u_char *start,
4751936So.canty@f5.com     size_t len)
4761936So.canty@f5.com {
4771936So.canty@f5.com     nxt_str_t       addr;
4781936So.canty@f5.com     nxt_sockaddr_t  *sa;
4791936So.canty@f5.com 
4801936So.canty@f5.com     addr.start = start;
4811936So.canty@f5.com     addr.length = len;
4821936So.canty@f5.com 
4831936So.canty@f5.com     sa = nxt_sockaddr_parse_optport(r->mem_pool, &addr);
4841936So.canty@f5.com     if (nxt_slow_path(sa == NULL)) {
4851936So.canty@f5.com         return NULL;
4861936So.canty@f5.com     }
4871936So.canty@f5.com 
4881936So.canty@f5.com     switch (sa->u.sockaddr.sa_family) {
4891936So.canty@f5.com         case AF_INET:
4901936So.canty@f5.com             if (sa->u.sockaddr_in.sin_addr.s_addr == INADDR_ANY) {
4911936So.canty@f5.com                 return NULL;
4921936So.canty@f5.com             }
4931936So.canty@f5.com 
4941936So.canty@f5.com             break;
4951936So.canty@f5.com 
4961936So.canty@f5.com #if (NXT_INET6)
4971936So.canty@f5.com         case AF_INET6:
4981936So.canty@f5.com             if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sockaddr_in6.sin6_addr)) {
4991936So.canty@f5.com                 return NULL;
5001936So.canty@f5.com             }
5011936So.canty@f5.com 
5021936So.canty@f5.com             break;
5031936So.canty@f5.com #endif /* NXT_INET6 */
5041936So.canty@f5.com 
5051936So.canty@f5.com         default:
5061936So.canty@f5.com             return NULL;
5071936So.canty@f5.com     }
5081936So.canty@f5.com 
5091936So.canty@f5.com     return sa;
5101936So.canty@f5.com }
5111936So.canty@f5.com 
5121936So.canty@f5.com 
5132133Sz.hong@f5.com static void
nxt_http_request_forward_protocol(nxt_http_request_t * r,nxt_http_field_t * field)5142133Sz.hong@f5.com nxt_http_request_forward_protocol(nxt_http_request_t *r,
5152133Sz.hong@f5.com     nxt_http_field_t *field)
5162133Sz.hong@f5.com {
5172133Sz.hong@f5.com     if (field->value_length == 4) {
5182133Sz.hong@f5.com         if (nxt_memcasecmp(field->value, "http", 4) == 0) {
5192133Sz.hong@f5.com             r->tls = 0;
5202133Sz.hong@f5.com         }
5212133Sz.hong@f5.com 
5222133Sz.hong@f5.com     } else if (field->value_length == 5) {
5232133Sz.hong@f5.com         if (nxt_memcasecmp(field->value, "https", 5) == 0) {
5242133Sz.hong@f5.com             r->tls = 1;
5252133Sz.hong@f5.com         }
5262133Sz.hong@f5.com 
5272133Sz.hong@f5.com     } else if (field->value_length == 2) {
5282133Sz.hong@f5.com         if (nxt_memcasecmp(field->value, "on", 2) == 0) {
5292133Sz.hong@f5.com             r->tls = 1;
5302133Sz.hong@f5.com         }
5312133Sz.hong@f5.com     }
5322133Sz.hong@f5.com }
5332133Sz.hong@f5.com 
5342133Sz.hong@f5.com 
535431Sigor@sysoev.ru static const nxt_http_request_state_t  nxt_http_request_body_state
536431Sigor@sysoev.ru     nxt_aligned(64) =
537431Sigor@sysoev.ru {
5381563Svbart@nginx.com     .ready_handler = nxt_http_request_ready,
539431Sigor@sysoev.ru     .error_handler = nxt_http_request_close_handler,
540431Sigor@sysoev.ru };
541431Sigor@sysoev.ru 
542431Sigor@sysoev.ru 
543431Sigor@sysoev.ru static void
nxt_http_request_ready(nxt_task_t * task,void * obj,void * data)5441563Svbart@nginx.com nxt_http_request_ready(nxt_task_t *task, void *obj, void *data)
545964Sigor@sysoev.ru {
5461264Sigor@sysoev.ru     nxt_http_action_t   *action;
547964Sigor@sysoev.ru     nxt_http_request_t  *r;
548964Sigor@sysoev.ru 
549964Sigor@sysoev.ru     r = obj;
5501264Sigor@sysoev.ru     action = r->conf->socket_conf->action;
551964Sigor@sysoev.ru 
5521563Svbart@nginx.com     nxt_http_request_action(task, r, action);
5531563Svbart@nginx.com }
5541563Svbart@nginx.com 
5551563Svbart@nginx.com 
5561563Svbart@nginx.com void
nxt_http_request_action(nxt_task_t * task,nxt_http_request_t * r,nxt_http_action_t * action)5571563Svbart@nginx.com nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r,
5581563Svbart@nginx.com     nxt_http_action_t *action)
5591563Svbart@nginx.com {
5602448Sz.hong@f5.com     nxt_int_t  ret;
5612448Sz.hong@f5.com 
5621264Sigor@sysoev.ru     if (nxt_fast_path(action != NULL)) {
563964Sigor@sysoev.ru 
5641060Sigor@sysoev.ru         do {
5652511Sz.hong@f5.com             ret = nxt_http_rewrite(task, r);
5662511Sz.hong@f5.com             if (nxt_slow_path(ret != NXT_OK)) {
5672511Sz.hong@f5.com                 break;
5682448Sz.hong@f5.com             }
5692448Sz.hong@f5.com 
5701264Sigor@sysoev.ru             action = action->handler(task, r, action);
571964Sigor@sysoev.ru 
5721264Sigor@sysoev.ru             if (action == NULL) {
5731060Sigor@sysoev.ru                 return;
5741060Sigor@sysoev.ru             }
575964Sigor@sysoev.ru 
5761264Sigor@sysoev.ru             if (action == NXT_HTTP_ACTION_ERROR) {
5771060Sigor@sysoev.ru                 break;
5781060Sigor@sysoev.ru             }
5791060Sigor@sysoev.ru 
5801060Sigor@sysoev.ru         } while (r->pass_count++ < 255);
581964Sigor@sysoev.ru     }
582964Sigor@sysoev.ru 
583964Sigor@sysoev.ru     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
584964Sigor@sysoev.ru }
585964Sigor@sysoev.ru 
586964Sigor@sysoev.ru 
5871264Sigor@sysoev.ru nxt_http_action_t *
nxt_http_application_handler(nxt_task_t * task,nxt_http_request_t * r,nxt_http_action_t * action)5881264Sigor@sysoev.ru nxt_http_application_handler(nxt_task_t *task, nxt_http_request_t *r,
5891264Sigor@sysoev.ru     nxt_http_action_t *action)
590431Sigor@sysoev.ru {
5911264Sigor@sysoev.ru     nxt_debug(task, "http application handler");
592431Sigor@sysoev.ru 
593431Sigor@sysoev.ru     /*
594431Sigor@sysoev.ru      * TODO: need an application flag to get local address
595431Sigor@sysoev.ru      * required by "SERVER_ADDR" in Pyhton and PHP. Not used in Go.
596431Sigor@sysoev.ru      */
5971011Smax.romanov@nginx.com     nxt_http_request_proto_info(task, r);
598431Sigor@sysoev.ru 
599967Svbart@nginx.com     if (r->host.length != 0) {
6001007Salexander.borisov@nginx.com         r->server_name = r->host;
601967Svbart@nginx.com 
602967Svbart@nginx.com     } else {
6031007Salexander.borisov@nginx.com         nxt_str_set(&r->server_name, "localhost");
604431Sigor@sysoev.ru     }
605431Sigor@sysoev.ru 
6061925Sz.hong@f5.com     nxt_router_process_http_request(task, r, action);
607964Sigor@sysoev.ru 
608964Sigor@sysoev.ru     return NULL;
609431Sigor@sysoev.ru }
610431Sigor@sysoev.ru 
611431Sigor@sysoev.ru 
6121011Smax.romanov@nginx.com static void
nxt_http_request_proto_info(nxt_task_t * task,nxt_http_request_t * r)6131011Smax.romanov@nginx.com nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r)
6141011Smax.romanov@nginx.com {
6151112Sigor@sysoev.ru     if (nxt_fast_path(r->proto.any != NULL)) {
6161112Sigor@sysoev.ru         nxt_http_proto[r->protocol].local_addr(task, r);
6171011Smax.romanov@nginx.com     }
6181011Smax.romanov@nginx.com }
6191011Smax.romanov@nginx.com 
6201011Smax.romanov@nginx.com 
621431Sigor@sysoev.ru void
nxt_http_request_read_body(nxt_task_t * task,nxt_http_request_t * r)622431Sigor@sysoev.ru nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r)
623431Sigor@sysoev.ru {
6241112Sigor@sysoev.ru     if (nxt_fast_path(r->proto.any != NULL)) {
6251112Sigor@sysoev.ru         nxt_http_proto[r->protocol].body_read(task, r);
626431Sigor@sysoev.ru     }
627431Sigor@sysoev.ru }
628431Sigor@sysoev.ru 
629431Sigor@sysoev.ru 
630431Sigor@sysoev.ru void
nxt_http_request_header_send(nxt_task_t * task,nxt_http_request_t * r,nxt_work_handler_t body_handler,void * data)6311148Sigor@sysoev.ru nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
6321270Sigor@sysoev.ru     nxt_work_handler_t body_handler, void *data)
633431Sigor@sysoev.ru {
6342437Sa.clayton@nginx.com     u_char             *p, *end, *server_string;
6352512Sz.hong@f5.com     nxt_int_t          ret;
6362437Sa.clayton@nginx.com     nxt_http_field_t   *server, *date, *content_length;
6372437Sa.clayton@nginx.com     nxt_socket_conf_t  *skcf;
638543Svbart@nginx.com 
6392512Sz.hong@f5.com     ret = nxt_http_set_headers(r);
6402512Sz.hong@f5.com     if (nxt_slow_path(ret != NXT_OK)) {
6412512Sz.hong@f5.com         goto fail;
6422512Sz.hong@f5.com     }
6432512Sz.hong@f5.com 
644431Sigor@sysoev.ru     /*
645543Svbart@nginx.com      * TODO: "Server", "Date", and "Content-Length" processing should be moved
646431Sigor@sysoev.ru      * to the last header filter.
647431Sigor@sysoev.ru      */
648431Sigor@sysoev.ru 
649431Sigor@sysoev.ru     server = nxt_list_zero_add(r->resp.fields);
650431Sigor@sysoev.ru     if (nxt_slow_path(server == NULL)) {
651431Sigor@sysoev.ru         goto fail;
652431Sigor@sysoev.ru     }
653431Sigor@sysoev.ru 
6542437Sa.clayton@nginx.com     skcf = r->conf->socket_conf;
6552437Sa.clayton@nginx.com     server_string = (u_char *) (skcf->server_version ? NXT_SERVER : NXT_NAME);
6562437Sa.clayton@nginx.com 
6572437Sa.clayton@nginx.com     nxt_http_field_name_set(server, "Server");
6582437Sa.clayton@nginx.com     server->value = server_string;
6592437Sa.clayton@nginx.com     server->value_length = nxt_strlen(server_string);
660431Sigor@sysoev.ru 
661543Svbart@nginx.com     if (r->resp.date == NULL) {
662543Svbart@nginx.com         date = nxt_list_zero_add(r->resp.fields);
663543Svbart@nginx.com         if (nxt_slow_path(date == NULL)) {
664543Svbart@nginx.com             goto fail;
665543Svbart@nginx.com         }
666543Svbart@nginx.com 
667543Svbart@nginx.com         nxt_http_field_name_set(date, "Date");
668543Svbart@nginx.com 
669740Sigor@sysoev.ru         p = nxt_mp_nget(r->mem_pool, nxt_http_date_cache.size);
670543Svbart@nginx.com         if (nxt_slow_path(p == NULL)) {
671543Svbart@nginx.com             goto fail;
672543Svbart@nginx.com         }
673543Svbart@nginx.com 
674740Sigor@sysoev.ru         (void) nxt_thread_time_string(task->thread, &nxt_http_date_cache, p);
675543Svbart@nginx.com 
676543Svbart@nginx.com         date->value = p;
677740Sigor@sysoev.ru         date->value_length = nxt_http_date_cache.size;
678543Svbart@nginx.com 
679543Svbart@nginx.com         r->resp.date = date;
680543Svbart@nginx.com     }
681543Svbart@nginx.com 
682431Sigor@sysoev.ru     if (r->resp.content_length_n != -1
683431Sigor@sysoev.ru         && (r->resp.content_length == NULL || r->resp.content_length->skip))
684431Sigor@sysoev.ru     {
685431Sigor@sysoev.ru         content_length = nxt_list_zero_add(r->resp.fields);
686431Sigor@sysoev.ru         if (nxt_slow_path(content_length == NULL)) {
687431Sigor@sysoev.ru             goto fail;
688431Sigor@sysoev.ru         }
689431Sigor@sysoev.ru 
690431Sigor@sysoev.ru         nxt_http_field_name_set(content_length, "Content-Length");
691431Sigor@sysoev.ru 
692431Sigor@sysoev.ru         p = nxt_mp_nget(r->mem_pool, NXT_OFF_T_LEN);
693431Sigor@sysoev.ru         if (nxt_slow_path(p == NULL)) {
694431Sigor@sysoev.ru             goto fail;
695431Sigor@sysoev.ru         }
696431Sigor@sysoev.ru 
697431Sigor@sysoev.ru         content_length->value = p;
698431Sigor@sysoev.ru         end = nxt_sprintf(p, p + NXT_OFF_T_LEN, "%O", r->resp.content_length_n);
699431Sigor@sysoev.ru         content_length->value_length = end - p;
700431Sigor@sysoev.ru 
701431Sigor@sysoev.ru         r->resp.content_length = content_length;
702431Sigor@sysoev.ru     }
703431Sigor@sysoev.ru 
7041112Sigor@sysoev.ru     if (nxt_fast_path(r->proto.any != NULL)) {
7051270Sigor@sysoev.ru         nxt_http_proto[r->protocol].header_send(task, r, body_handler, data);
706431Sigor@sysoev.ru     }
707431Sigor@sysoev.ru 
708431Sigor@sysoev.ru     return;
709431Sigor@sysoev.ru 
710431Sigor@sysoev.ru fail:
711431Sigor@sysoev.ru 
712431Sigor@sysoev.ru     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
713431Sigor@sysoev.ru }
714431Sigor@sysoev.ru 
715431Sigor@sysoev.ru 
716431Sigor@sysoev.ru void
nxt_http_request_ws_frame_start(nxt_task_t * task,nxt_http_request_t * r,nxt_buf_t * ws_frame)7171131Smax.romanov@nginx.com nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r,
7181131Smax.romanov@nginx.com     nxt_buf_t *ws_frame)
7191131Smax.romanov@nginx.com {
7201131Smax.romanov@nginx.com     if (r->proto.any != NULL) {
7211131Smax.romanov@nginx.com         nxt_http_proto[r->protocol].ws_frame_start(task, r, ws_frame);
7221131Smax.romanov@nginx.com     }
7231131Smax.romanov@nginx.com }
7241131Smax.romanov@nginx.com 
7251131Smax.romanov@nginx.com 
7261131Smax.romanov@nginx.com void
nxt_http_request_send(nxt_task_t * task,nxt_http_request_t * r,nxt_buf_t * out)727431Sigor@sysoev.ru nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out)
728431Sigor@sysoev.ru {
7291112Sigor@sysoev.ru     if (nxt_fast_path(r->proto.any != NULL)) {
7301112Sigor@sysoev.ru         nxt_http_proto[r->protocol].send(task, r, out);
731431Sigor@sysoev.ru     }
732431Sigor@sysoev.ru }
733431Sigor@sysoev.ru 
734431Sigor@sysoev.ru 
735431Sigor@sysoev.ru nxt_buf_t *
nxt_http_buf_mem(nxt_task_t * task,nxt_http_request_t * r,size_t size)736608Sigor@sysoev.ru nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, size_t size)
737431Sigor@sysoev.ru {
738431Sigor@sysoev.ru     nxt_buf_t  *b;
739431Sigor@sysoev.ru 
740608Sigor@sysoev.ru     b = nxt_buf_mem_alloc(r->mem_pool, size, 0);
741431Sigor@sysoev.ru     if (nxt_fast_path(b != NULL)) {
742608Sigor@sysoev.ru         b->completion_handler = nxt_http_request_mem_buf_completion;
743431Sigor@sysoev.ru         b->parent = r;
744608Sigor@sysoev.ru         nxt_mp_retain(r->mem_pool);
745431Sigor@sysoev.ru 
746431Sigor@sysoev.ru     } else {
747608Sigor@sysoev.ru         nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
748431Sigor@sysoev.ru     }
749431Sigor@sysoev.ru 
750431Sigor@sysoev.ru     return b;
751431Sigor@sysoev.ru }
752431Sigor@sysoev.ru 
753431Sigor@sysoev.ru 
754431Sigor@sysoev.ru static void
nxt_http_request_mem_buf_completion(nxt_task_t * task,void * obj,void * data)755608Sigor@sysoev.ru nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, void *data)
756608Sigor@sysoev.ru {
7571269Sigor@sysoev.ru     nxt_buf_t           *b, *next;
758608Sigor@sysoev.ru     nxt_http_request_t  *r;
759608Sigor@sysoev.ru 
760608Sigor@sysoev.ru     b = obj;
761608Sigor@sysoev.ru     r = data;
762608Sigor@sysoev.ru 
7631269Sigor@sysoev.ru     do {
7641269Sigor@sysoev.ru         next = b->next;
765608Sigor@sysoev.ru 
7661269Sigor@sysoev.ru         nxt_mp_free(r->mem_pool, b);
7671269Sigor@sysoev.ru         nxt_mp_release(r->mem_pool);
7681269Sigor@sysoev.ru 
7691269Sigor@sysoev.ru         b = next;
7701269Sigor@sysoev.ru     } while (b != NULL);
771608Sigor@sysoev.ru }
772608Sigor@sysoev.ru 
773608Sigor@sysoev.ru 
774608Sigor@sysoev.ru nxt_buf_t *
nxt_http_buf_last(nxt_http_request_t * r)775608Sigor@sysoev.ru nxt_http_buf_last(nxt_http_request_t *r)
776608Sigor@sysoev.ru {
777608Sigor@sysoev.ru     nxt_buf_t  *last;
778608Sigor@sysoev.ru 
779608Sigor@sysoev.ru     last = r->last;
780608Sigor@sysoev.ru     r->last = NULL;
781608Sigor@sysoev.ru 
782608Sigor@sysoev.ru     return last;
783608Sigor@sysoev.ru }
784608Sigor@sysoev.ru 
785608Sigor@sysoev.ru 
786608Sigor@sysoev.ru static void
nxt_http_request_done(nxt_task_t * task,void * obj,void * data)787431Sigor@sysoev.ru nxt_http_request_done(nxt_task_t *task, void *obj, void *data)
788431Sigor@sysoev.ru {
789431Sigor@sysoev.ru     nxt_http_request_t  *r;
790431Sigor@sysoev.ru 
791431Sigor@sysoev.ru     r = data;
792431Sigor@sysoev.ru 
793431Sigor@sysoev.ru     nxt_debug(task, "http request done");
794431Sigor@sysoev.ru 
795431Sigor@sysoev.ru     nxt_http_request_close_handler(task, r, r->proto.any);
796431Sigor@sysoev.ru }
797431Sigor@sysoev.ru 
798431Sigor@sysoev.ru 
799431Sigor@sysoev.ru void
nxt_http_request_error_handler(nxt_task_t * task,void * obj,void * data)800608Sigor@sysoev.ru nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
801431Sigor@sysoev.ru {
802608Sigor@sysoev.ru     nxt_http_proto_t    proto;
803608Sigor@sysoev.ru     nxt_http_request_t  *r;
804608Sigor@sysoev.ru 
805608Sigor@sysoev.ru     r = obj;
806608Sigor@sysoev.ru     proto.any = data;
807431Sigor@sysoev.ru 
808608Sigor@sysoev.ru     nxt_debug(task, "http request error handler");
809608Sigor@sysoev.ru 
8101009Smax.romanov@nginx.com     r->error = 1;
8111009Smax.romanov@nginx.com 
8121112Sigor@sysoev.ru     if (nxt_fast_path(proto.any != NULL)) {
8131112Sigor@sysoev.ru         nxt_http_proto[r->protocol].discard(task, r, nxt_http_buf_last(r));
814608Sigor@sysoev.ru     }
815431Sigor@sysoev.ru }
816431Sigor@sysoev.ru 
817431Sigor@sysoev.ru 
8181131Smax.romanov@nginx.com void
nxt_http_request_close_handler(nxt_task_t * task,void * obj,void * data)819431Sigor@sysoev.ru nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
820431Sigor@sysoev.ru {
8212625Sz.hong@f5.com     nxt_int_t                ret;
822630Svbart@nginx.com     nxt_http_proto_t         proto;
8232624Sz.hong@f5.com     nxt_router_conf_t        *rtcf;
824630Svbart@nginx.com     nxt_http_request_t       *r;
8251112Sigor@sysoev.ru     nxt_http_protocol_t      protocol;
826683Sigor@sysoev.ru     nxt_socket_conf_joint_t  *conf;
827431Sigor@sysoev.ru 
828431Sigor@sysoev.ru     r = obj;
829431Sigor@sysoev.ru     proto.any = data;
830431Sigor@sysoev.ru 
831683Sigor@sysoev.ru     conf = r->conf;
8322624Sz.hong@f5.com     rtcf = conf->socket_conf->router_conf;
833683Sigor@sysoev.ru 
834431Sigor@sysoev.ru     if (!r->logged) {
835431Sigor@sysoev.ru         r->logged = 1;
836630Svbart@nginx.com 
8372624Sz.hong@f5.com         if (rtcf->access_log != NULL) {
8382625Sz.hong@f5.com             ret = nxt_http_request_access_log(task, r, rtcf);
8392625Sz.hong@f5.com             if (ret == NXT_OK) {
8402625Sz.hong@f5.com                 return;
8412625Sz.hong@f5.com             }
842630Svbart@nginx.com         }
843431Sigor@sysoev.ru     }
844431Sigor@sysoev.ru 
8452166Sz.hong@f5.com     nxt_debug(task, "http request close handler");
8462166Sz.hong@f5.com 
847431Sigor@sysoev.ru     r->proto.any = NULL;
848431Sigor@sysoev.ru 
8491403Smax.romanov@nginx.com     if (r->body != NULL && nxt_buf_is_file(r->body)
8501403Smax.romanov@nginx.com         && r->body->file->fd != -1)
8511403Smax.romanov@nginx.com     {
8521403Smax.romanov@nginx.com         nxt_fd_close(r->body->file->fd);
8531403Smax.romanov@nginx.com 
8541403Smax.romanov@nginx.com         r->body->file->fd = -1;
8551403Smax.romanov@nginx.com     }
8561403Smax.romanov@nginx.com 
8572318Sz.hong@f5.com     if (r->tstr_query != NULL) {
8582318Sz.hong@f5.com         nxt_tstr_query_release(r->tstr_query);
8592318Sz.hong@f5.com     }
8602318Sz.hong@f5.com 
8611112Sigor@sysoev.ru     if (nxt_fast_path(proto.any != NULL)) {
8621131Smax.romanov@nginx.com         protocol = r->protocol;
8631131Smax.romanov@nginx.com 
8641265Sigor@sysoev.ru         nxt_http_proto[protocol].close(task, proto, conf);
8651131Smax.romanov@nginx.com 
8661265Sigor@sysoev.ru         nxt_mp_release(r->mem_pool);
867431Sigor@sysoev.ru     }
868431Sigor@sysoev.ru }
869543Svbart@nginx.com 
870543Svbart@nginx.com 
8712625Sz.hong@f5.com static nxt_int_t
nxt_http_request_access_log(nxt_task_t * task,nxt_http_request_t * r,nxt_router_conf_t * rtcf)8722624Sz.hong@f5.com nxt_http_request_access_log(nxt_task_t *task, nxt_http_request_t *r,
8732624Sz.hong@f5.com     nxt_router_conf_t *rtcf)
8742624Sz.hong@f5.com {
8752625Sz.hong@f5.com     nxt_int_t                ret;
8762625Sz.hong@f5.com     nxt_str_t                str;
8772625Sz.hong@f5.com     nxt_bool_t               expr;
8782624Sz.hong@f5.com     nxt_router_access_log_t  *access_log;
8792624Sz.hong@f5.com 
8802624Sz.hong@f5.com     access_log = rtcf->access_log;
8812624Sz.hong@f5.com 
8822625Sz.hong@f5.com     expr = 1;
8832625Sz.hong@f5.com 
8842625Sz.hong@f5.com     if (rtcf->log_expr != NULL) {
8852625Sz.hong@f5.com 
8862625Sz.hong@f5.com         if (nxt_tstr_is_const(rtcf->log_expr)) {
8872625Sz.hong@f5.com             nxt_tstr_str(rtcf->log_expr, &str);
8882625Sz.hong@f5.com 
8892625Sz.hong@f5.com         } else {
8902625Sz.hong@f5.com             ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state,
8912625Sz.hong@f5.com                                       &r->tstr_cache, r, r->mem_pool);
8922625Sz.hong@f5.com             if (nxt_slow_path(ret != NXT_OK)) {
8932625Sz.hong@f5.com                 return NXT_DECLINED;
8942625Sz.hong@f5.com             }
8952625Sz.hong@f5.com 
8962625Sz.hong@f5.com             nxt_tstr_query(task, r->tstr_query, rtcf->log_expr, &str);
8972625Sz.hong@f5.com 
8982625Sz.hong@f5.com             if (nxt_slow_path(nxt_tstr_query_failed(r->tstr_query))) {
8992625Sz.hong@f5.com                 return NXT_DECLINED;
9002625Sz.hong@f5.com             }
9012625Sz.hong@f5.com         }
9022625Sz.hong@f5.com 
9032625Sz.hong@f5.com         if (str.length == 0
9042625Sz.hong@f5.com             || nxt_str_eq(&str, "0", 1)
9052625Sz.hong@f5.com             || nxt_str_eq(&str, "false", 5)
9062625Sz.hong@f5.com             || nxt_str_eq(&str, "null", 4)
9072625Sz.hong@f5.com             || nxt_str_eq(&str, "undefined", 9))
9082625Sz.hong@f5.com         {
9092625Sz.hong@f5.com             expr = 0;
9102625Sz.hong@f5.com         }
9112625Sz.hong@f5.com     }
9122625Sz.hong@f5.com 
9132625Sz.hong@f5.com     if (rtcf->log_negate ^ expr) {
9142625Sz.hong@f5.com         access_log->handler(task, r, access_log, rtcf->log_format);
9152625Sz.hong@f5.com         return NXT_OK;
9162625Sz.hong@f5.com     }
9172625Sz.hong@f5.com 
9182625Sz.hong@f5.com     return NXT_DECLINED;
9192624Sz.hong@f5.com }
9202624Sz.hong@f5.com 
9212624Sz.hong@f5.com 
922543Svbart@nginx.com static u_char *
nxt_http_date_cache_handler(u_char * buf,nxt_realtime_t * now,struct tm * tm,size_t size,const char * format)9231183Svbart@nginx.com nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, struct tm *tm,
9241183Svbart@nginx.com     size_t size, const char *format)
925543Svbart@nginx.com {
9261183Svbart@nginx.com     return nxt_http_date(buf, tm);
927543Svbart@nginx.com }
9282104Sz.hong@f5.com 
9292104Sz.hong@f5.com 
9302104Sz.hong@f5.com nxt_array_t *
nxt_http_arguments_parse(nxt_http_request_t * r)9312104Sz.hong@f5.com nxt_http_arguments_parse(nxt_http_request_t *r)
9322104Sz.hong@f5.com {
9332104Sz.hong@f5.com     size_t                 name_length;
9342104Sz.hong@f5.com     u_char                 *p, *dst, *dst_start, *start, *end, *name;
9352104Sz.hong@f5.com     uint8_t                d0, d1;
9362104Sz.hong@f5.com     uint32_t               hash;
9372104Sz.hong@f5.com     nxt_array_t            *args;
9382104Sz.hong@f5.com     nxt_http_name_value_t  *nv;
9392104Sz.hong@f5.com 
9402104Sz.hong@f5.com     if (r->arguments != NULL) {
9412104Sz.hong@f5.com         return r->arguments;
9422104Sz.hong@f5.com     }
9432104Sz.hong@f5.com 
9442104Sz.hong@f5.com     args = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
9452104Sz.hong@f5.com     if (nxt_slow_path(args == NULL)) {
9462104Sz.hong@f5.com         return NULL;
9472104Sz.hong@f5.com     }
9482104Sz.hong@f5.com 
949*2746Szelenkov@nginx.com     hash = NXT_HTTP_FIELD_HASH_INIT;
950*2746Szelenkov@nginx.com     name = NULL;
951*2746Szelenkov@nginx.com     name_length = 0;
952*2746Szelenkov@nginx.com 
9532104Sz.hong@f5.com     dst_start = nxt_mp_nget(r->mem_pool, r->args->length);
9542104Sz.hong@f5.com     if (nxt_slow_path(dst_start == NULL)) {
9552104Sz.hong@f5.com         return NULL;
9562104Sz.hong@f5.com     }
9572104Sz.hong@f5.com 
9582104Sz.hong@f5.com     r->args_decoded.start = dst_start;
9592104Sz.hong@f5.com 
9602104Sz.hong@f5.com     start = r->args->start;
9612104Sz.hong@f5.com     end = start + r->args->length;
9622104Sz.hong@f5.com 
9632104Sz.hong@f5.com     for (p = start, dst = dst_start; p < end; p++, dst++) {
9642104Sz.hong@f5.com         *dst = *p;
9652104Sz.hong@f5.com 
9662104Sz.hong@f5.com         switch (*p) {
9672104Sz.hong@f5.com         case '=':
9682104Sz.hong@f5.com             if (name == NULL) {
9692104Sz.hong@f5.com                 name_length = dst - dst_start;
9702104Sz.hong@f5.com                 name = dst_start;
9712104Sz.hong@f5.com                 dst_start = dst + 1;
9722104Sz.hong@f5.com             }
9732104Sz.hong@f5.com 
9742104Sz.hong@f5.com             continue;
9752104Sz.hong@f5.com 
9762104Sz.hong@f5.com         case '&':
9772104Sz.hong@f5.com             if (name_length != 0 || dst != dst_start) {
9782104Sz.hong@f5.com                 nv = nxt_http_argument(args, name, name_length, hash, dst_start,
9792104Sz.hong@f5.com                                        dst);
9802104Sz.hong@f5.com                 if (nxt_slow_path(nv == NULL)) {
9812104Sz.hong@f5.com                     return NULL;
9822104Sz.hong@f5.com                 }
9832104Sz.hong@f5.com             }
9842104Sz.hong@f5.com 
9852104Sz.hong@f5.com             hash = NXT_HTTP_FIELD_HASH_INIT;
9862104Sz.hong@f5.com             name_length = 0;
9872104Sz.hong@f5.com             name = NULL;
9882104Sz.hong@f5.com             dst_start = dst + 1;
9892104Sz.hong@f5.com 
9902104Sz.hong@f5.com             continue;
9912104Sz.hong@f5.com 
9922104Sz.hong@f5.com         case '+':
9932104Sz.hong@f5.com             *dst = ' ';
9942104Sz.hong@f5.com 
9952104Sz.hong@f5.com             break;
9962104Sz.hong@f5.com 
9972104Sz.hong@f5.com         case '%':
9982104Sz.hong@f5.com             if (nxt_slow_path(end - p <= 2)) {
9992104Sz.hong@f5.com                 break;
10002104Sz.hong@f5.com             }
10012104Sz.hong@f5.com 
10022104Sz.hong@f5.com             d0 = nxt_hex2int[p[1]];
10032104Sz.hong@f5.com             d1 = nxt_hex2int[p[2]];
10042104Sz.hong@f5.com 
10052104Sz.hong@f5.com             if (nxt_slow_path((d0 | d1) >= 16)) {
10062104Sz.hong@f5.com                 break;
10072104Sz.hong@f5.com             }
10082104Sz.hong@f5.com 
10092104Sz.hong@f5.com             p += 2;
10102104Sz.hong@f5.com             *dst = (d0 << 4) + d1;
10112104Sz.hong@f5.com 
10122104Sz.hong@f5.com             break;
10132104Sz.hong@f5.com         }
10142104Sz.hong@f5.com 
10152104Sz.hong@f5.com         if (name == NULL) {
10162104Sz.hong@f5.com             hash = nxt_http_field_hash_char(hash, *dst);
10172104Sz.hong@f5.com         }
10182104Sz.hong@f5.com     }
10192104Sz.hong@f5.com 
10202104Sz.hong@f5.com     r->args_decoded.length = dst - r->args_decoded.start;
10212104Sz.hong@f5.com 
10222104Sz.hong@f5.com     if (name_length != 0 || dst != dst_start) {
10232104Sz.hong@f5.com         nv = nxt_http_argument(args, name, name_length, hash, dst_start, dst);
10242104Sz.hong@f5.com         if (nxt_slow_path(nv == NULL)) {
10252104Sz.hong@f5.com             return NULL;
10262104Sz.hong@f5.com         }
10272104Sz.hong@f5.com     }
10282104Sz.hong@f5.com 
10292104Sz.hong@f5.com     r->arguments = args;
10302104Sz.hong@f5.com 
10312104Sz.hong@f5.com     return args;
10322104Sz.hong@f5.com }
1033*2746Szelenkov@nginx.com 
1034*2746Szelenkov@nginx.com 
10352104Sz.hong@f5.com static nxt_http_name_value_t *
nxt_http_argument(nxt_array_t * array,u_char * name,size_t name_length,uint32_t hash,u_char * start,const u_char * end)10362104Sz.hong@f5.com nxt_http_argument(nxt_array_t *array, u_char *name, size_t name_length,
10372104Sz.hong@f5.com     uint32_t hash, u_char *start, const u_char *end)
10382104Sz.hong@f5.com {
10392104Sz.hong@f5.com     size_t                 length;
10402104Sz.hong@f5.com     nxt_http_name_value_t  *nv;
10412104Sz.hong@f5.com 
10422104Sz.hong@f5.com     nv = nxt_array_add(array);
10432139Sandrew@digital-domain.net     if (nxt_slow_path(nv == NULL)) {
10442104Sz.hong@f5.com         return NULL;
10452104Sz.hong@f5.com     }
10462104Sz.hong@f5.com 
10472104Sz.hong@f5.com     nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
10482104Sz.hong@f5.com 
10492104Sz.hong@f5.com     length = end - start;
10502104Sz.hong@f5.com 
10512104Sz.hong@f5.com     if (name == NULL) {
10522104Sz.hong@f5.com         name_length = length;
10532104Sz.hong@f5.com         name = start;
10542104Sz.hong@f5.com         length = 0;
10552104Sz.hong@f5.com     }
10562104Sz.hong@f5.com 
10572104Sz.hong@f5.com     nv->name_length = name_length;
10582104Sz.hong@f5.com     nv->value_length = length;
10592104Sz.hong@f5.com     nv->name = name;
10602104Sz.hong@f5.com     nv->value = start;
10612104Sz.hong@f5.com 
10622104Sz.hong@f5.com     return nv;
10632104Sz.hong@f5.com }
10642104Sz.hong@f5.com 
10652104Sz.hong@f5.com 
10662104Sz.hong@f5.com nxt_array_t *
nxt_http_cookies_parse(nxt_http_request_t * r)10672104Sz.hong@f5.com nxt_http_cookies_parse(nxt_http_request_t *r)
10682104Sz.hong@f5.com {
10692104Sz.hong@f5.com     nxt_int_t         ret;
10702104Sz.hong@f5.com     nxt_array_t       *cookies;
10712104Sz.hong@f5.com     nxt_http_field_t  *f;
10722104Sz.hong@f5.com 
10732104Sz.hong@f5.com     if (r->cookies != NULL) {
10742104Sz.hong@f5.com         return r->cookies;
10752104Sz.hong@f5.com     }
10762104Sz.hong@f5.com 
10772104Sz.hong@f5.com     cookies = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
10782104Sz.hong@f5.com     if (nxt_slow_path(cookies == NULL)) {
10792104Sz.hong@f5.com         return NULL;
10802104Sz.hong@f5.com     }
10812104Sz.hong@f5.com 
10822104Sz.hong@f5.com     nxt_list_each(f, r->fields) {
10832104Sz.hong@f5.com 
10842104Sz.hong@f5.com         if (f->hash != NXT_HTTP_COOKIE_HASH
10852104Sz.hong@f5.com             || f->name_length != 6
10862104Sz.hong@f5.com             || nxt_strncasecmp(f->name, (u_char *) "Cookie", 6) != 0)
10872104Sz.hong@f5.com         {
10882104Sz.hong@f5.com             continue;
10892104Sz.hong@f5.com         }
10902104Sz.hong@f5.com 
10912104Sz.hong@f5.com         ret = nxt_http_cookie_parse(cookies, f->value,
10922104Sz.hong@f5.com                                     f->value + f->value_length);
10932104Sz.hong@f5.com         if (ret != NXT_OK) {
10942104Sz.hong@f5.com             return NULL;
10952104Sz.hong@f5.com         }
10962104Sz.hong@f5.com 
10972104Sz.hong@f5.com     } nxt_list_loop;
10982104Sz.hong@f5.com 
10992104Sz.hong@f5.com     r->cookies = cookies;
11002104Sz.hong@f5.com 
11012104Sz.hong@f5.com     return cookies;
11022104Sz.hong@f5.com }
11032104Sz.hong@f5.com 
11042104Sz.hong@f5.com 
11052104Sz.hong@f5.com static nxt_int_t
nxt_http_cookie_parse(nxt_array_t * cookies,u_char * start,const u_char * end)11062104Sz.hong@f5.com nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, const u_char *end)
11072104Sz.hong@f5.com {
11082104Sz.hong@f5.com     size_t                 name_length;
11092104Sz.hong@f5.com     u_char                 c, *p, *name;
11102104Sz.hong@f5.com     nxt_http_name_value_t  *nv;
11112104Sz.hong@f5.com 
11122139Sandrew@digital-domain.net     name = NULL;
11132104Sz.hong@f5.com     name_length = 0;
11142104Sz.hong@f5.com 
11152104Sz.hong@f5.com     for (p = start; p < end; p++) {
11162104Sz.hong@f5.com         c = *p;
11172104Sz.hong@f5.com 
11182104Sz.hong@f5.com         if (c == '=' && name == NULL) {
11192104Sz.hong@f5.com             while (start[0] == ' ') { start++; }
11202104Sz.hong@f5.com 
11212104Sz.hong@f5.com             name_length = p - start;
11222104Sz.hong@f5.com             name = start;
11232104Sz.hong@f5.com 
11242203Sz.hong@f5.com             start = p + 1;
11252104Sz.hong@f5.com 
11262104Sz.hong@f5.com         } else if (c == ';') {
11272104Sz.hong@f5.com             if (name != NULL) {
11282203Sz.hong@f5.com                 nv = nxt_http_cookie(cookies, name, name_length, start, p);
11292104Sz.hong@f5.com                 if (nxt_slow_path(nv == NULL)) {
11302104Sz.hong@f5.com                     return NXT_ERROR;
11312104Sz.hong@f5.com                 }
11322104Sz.hong@f5.com             }
11332104Sz.hong@f5.com 
11342104Sz.hong@f5.com             name = NULL;
11352104Sz.hong@f5.com             start = p + 1;
11362104Sz.hong@f5.com          }
11372104Sz.hong@f5.com     }
11382104Sz.hong@f5.com 
11392104Sz.hong@f5.com     if (name != NULL) {
11402104Sz.hong@f5.com         nv = nxt_http_cookie(cookies, name, name_length, start, p);
11412104Sz.hong@f5.com         if (nxt_slow_path(nv == NULL)) {
11422104Sz.hong@f5.com             return NXT_ERROR;
11432104Sz.hong@f5.com         }
11442104Sz.hong@f5.com     }
11452104Sz.hong@f5.com 
11462104Sz.hong@f5.com     return NXT_OK;
11472104Sz.hong@f5.com }
11482104Sz.hong@f5.com 
11492104Sz.hong@f5.com 
11502104Sz.hong@f5.com static nxt_http_name_value_t *
nxt_http_cookie(nxt_array_t * array,u_char * name,size_t name_length,u_char * start,const u_char * end)11512104Sz.hong@f5.com nxt_http_cookie(nxt_array_t *array, u_char *name, size_t name_length,
11522104Sz.hong@f5.com     u_char *start, const u_char *end)
11532104Sz.hong@f5.com {
11542104Sz.hong@f5.com     u_char                 c, *p;
11552104Sz.hong@f5.com     uint32_t               hash;
11562104Sz.hong@f5.com     nxt_http_name_value_t  *nv;
11572104Sz.hong@f5.com 
11582139Sandrew@digital-domain.net     nv = nxt_array_add(array);
11592104Sz.hong@f5.com     if (nxt_slow_path(nv == NULL)) {
11602104Sz.hong@f5.com         return NULL;
11612104Sz.hong@f5.com     }
11622104Sz.hong@f5.com 
11632104Sz.hong@f5.com     nv->name_length = name_length;
11642104Sz.hong@f5.com     nv->name = name;
11652104Sz.hong@f5.com 
11662104Sz.hong@f5.com     hash = NXT_HTTP_FIELD_HASH_INIT;
11672104Sz.hong@f5.com 
11682104Sz.hong@f5.com     for (p = name; p < name + name_length; p++) {
11692104Sz.hong@f5.com         c = *p;
11702104Sz.hong@f5.com         hash = nxt_http_field_hash_char(hash, c);
11712104Sz.hong@f5.com     }
11722104Sz.hong@f5.com 
11732104Sz.hong@f5.com     nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
11742104Sz.hong@f5.com 
11752104Sz.hong@f5.com     while (start < end && end[-1] == ' ') { end--; }
11762104Sz.hong@f5.com 
11772104Sz.hong@f5.com     nv->value_length = end - start;
11782104Sz.hong@f5.com     nv->value = start;
11792104Sz.hong@f5.com 
11802104Sz.hong@f5.com     return nv;
11812104Sz.hong@f5.com }
11822104Sz.hong@f5.com 
11832104Sz.hong@f5.com 
11842104Sz.hong@f5.com int64_t
nxt_http_field_hash(nxt_mp_t * mp,nxt_str_t * name,nxt_bool_t case_sensitive,uint8_t encoding)11852104Sz.hong@f5.com nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive,
11862104Sz.hong@f5.com     uint8_t encoding)
11872104Sz.hong@f5.com {
11882123Sz.hong@f5.com     u_char      c, *p, *src, *start, *end, plus;
11892123Sz.hong@f5.com     uint8_t     d0, d1;
11902123Sz.hong@f5.com     uint32_t    hash;
11912123Sz.hong@f5.com     nxt_str_t   str;
11922123Sz.hong@f5.com     nxt_uint_t  i;
11932123Sz.hong@f5.com 
11942123Sz.hong@f5.com     str.length = name->length;
11952123Sz.hong@f5.com 
11962123Sz.hong@f5.com     str.start = nxt_mp_nget(mp, str.length);
11972123Sz.hong@f5.com     if (nxt_slow_path(str.start == NULL)) {
11982123Sz.hong@f5.com         return -1;
11992123Sz.hong@f5.com     }
12002123Sz.hong@f5.com 
12012123Sz.hong@f5.com     p = str.start;
12022123Sz.hong@f5.com 
12032123Sz.hong@f5.com     hash = NXT_HTTP_FIELD_HASH_INIT;
12042123Sz.hong@f5.com 
12052123Sz.hong@f5.com     if (encoding == NXT_HTTP_URI_ENCODING_NONE) {
12062123Sz.hong@f5.com         for (i = 0; i < name->length; i++) {
12072123Sz.hong@f5.com             c = name->start[i];
12082123Sz.hong@f5.com             *p++ = c;
12092123Sz.hong@f5.com 
12102123Sz.hong@f5.com             c = case_sensitive ? c : nxt_lowcase(c);
12112123Sz.hong@f5.com             hash = nxt_http_field_hash_char(hash, c);
12122123Sz.hong@f5.com         }
12132123Sz.hong@f5.com 
12142123Sz.hong@f5.com         goto end;
12152123Sz.hong@f5.com     }
12162123Sz.hong@f5.com 
12172123Sz.hong@f5.com     plus = (encoding == NXT_HTTP_URI_ENCODING_PLUS) ? ' ' : '+';
12182123Sz.hong@f5.com 
12192123Sz.hong@f5.com     start = name->start;
12202123Sz.hong@f5.com     end = start + name->length;
12212123Sz.hong@f5.com 
12222123Sz.hong@f5.com     for (src = start; src < end; src++) {
12232123Sz.hong@f5.com         c = *src;
12242123Sz.hong@f5.com 
12252123Sz.hong@f5.com         switch (c) {
12262123Sz.hong@f5.com         case '%':
12272123Sz.hong@f5.com             if (nxt_slow_path(end - src <= 2)) {
12282123Sz.hong@f5.com                 return -1;
12292123Sz.hong@f5.com             }
12302123Sz.hong@f5.com 
12312123Sz.hong@f5.com             d0 = nxt_hex2int[src[1]];
12322123Sz.hong@f5.com             d1 = nxt_hex2int[src[2]];
12332123Sz.hong@f5.com             src += 2;
12342123Sz.hong@f5.com 
12352123Sz.hong@f5.com             if (nxt_slow_path((d0 | d1) >= 16)) {
12362123Sz.hong@f5.com                 return -1;
12372123Sz.hong@f5.com             }
12382123Sz.hong@f5.com 
12392123Sz.hong@f5.com             c = (d0 << 4) + d1;
12402123Sz.hong@f5.com             *p++ = c;
12412123Sz.hong@f5.com             break;
12422123Sz.hong@f5.com 
12432123Sz.hong@f5.com         case '+':
12442123Sz.hong@f5.com             c = plus;
12452123Sz.hong@f5.com             *p++ = c;
12462123Sz.hong@f5.com             break;
12472123Sz.hong@f5.com 
12482123Sz.hong@f5.com         default:
12492123Sz.hong@f5.com             *p++ = c;
12502123Sz.hong@f5.com             break;
12512123Sz.hong@f5.com         }
12522123Sz.hong@f5.com 
12532123Sz.hong@f5.com         c = case_sensitive ? c : nxt_lowcase(c);
12542123Sz.hong@f5.com         hash = nxt_http_field_hash_char(hash, c);
12552123Sz.hong@f5.com     }
12562123Sz.hong@f5.com 
12572123Sz.hong@f5.com     str.length = p - str.start;
12582123Sz.hong@f5.com 
12592123Sz.hong@f5.com end:
12602123Sz.hong@f5.com 
12612123Sz.hong@f5.com     *name = str;
12622123Sz.hong@f5.com 
12632123Sz.hong@f5.com     return nxt_http_field_hash_end(hash) & 0xFFFF;
12642123Sz.hong@f5.com }
12652123Sz.hong@f5.com 
12662123Sz.hong@f5.com 
12672123Sz.hong@f5.com int64_t
nxt_http_argument_hash(nxt_mp_t * mp,nxt_str_t * name)12682123Sz.hong@f5.com nxt_http_argument_hash(nxt_mp_t *mp, nxt_str_t *name)
12692123Sz.hong@f5.com {
12702123Sz.hong@f5.com     return nxt_http_field_hash(mp, name, 1, NXT_HTTP_URI_ENCODING_PLUS);
12712147Sz.hong@f5.com }
12722147Sz.hong@f5.com 
12732147Sz.hong@f5.com 
12742147Sz.hong@f5.com int64_t
nxt_http_header_hash(nxt_mp_t * mp,nxt_str_t * name)12752147Sz.hong@f5.com nxt_http_header_hash(nxt_mp_t *mp, nxt_str_t *name)
12762147Sz.hong@f5.com {
12772147Sz.hong@f5.com     u_char     c, *p;
12782147Sz.hong@f5.com     uint32_t   i, hash;
12792147Sz.hong@f5.com     nxt_str_t  str;
12802147Sz.hong@f5.com 
12812147Sz.hong@f5.com     str.length = name->length;
12822147Sz.hong@f5.com 
12832147Sz.hong@f5.com     str.start = nxt_mp_nget(mp, str.length);
12842147Sz.hong@f5.com     if (nxt_slow_path(str.start == NULL)) {
12852147Sz.hong@f5.com         return -1;
12862147Sz.hong@f5.com     }
12872147Sz.hong@f5.com 
12882147Sz.hong@f5.com     p = str.start;
12892147Sz.hong@f5.com     hash = NXT_HTTP_FIELD_HASH_INIT;
12902147Sz.hong@f5.com 
12912147Sz.hong@f5.com     for (i = 0; i < name->length; i++) {
12922147Sz.hong@f5.com         c = name->start[i];
12932147Sz.hong@f5.com 
12942147Sz.hong@f5.com         if (c >= 'A' && c <= 'Z') {
12952147Sz.hong@f5.com             *p = c | 0x20;
12962147Sz.hong@f5.com 
12972147Sz.hong@f5.com         } else if (c == '_') {
12982147Sz.hong@f5.com             *p = '-';
12992147Sz.hong@f5.com 
13002147Sz.hong@f5.com         } else {
13012147Sz.hong@f5.com             *p = c;
13022147Sz.hong@f5.com         }
13032147Sz.hong@f5.com 
13042147Sz.hong@f5.com         hash = nxt_http_field_hash_char(hash, *p);
13052147Sz.hong@f5.com         p++;
13062147Sz.hong@f5.com     }
13072147Sz.hong@f5.com 
13082147Sz.hong@f5.com     *name = str;
13092147Sz.hong@f5.com 
13102147Sz.hong@f5.com     return nxt_http_field_hash_end(hash) & 0xFFFF;
13112147Sz.hong@f5.com }
13122147Sz.hong@f5.com 
13132147Sz.hong@f5.com 
13142147Sz.hong@f5.com int64_t
nxt_http_cookie_hash(nxt_mp_t * mp,nxt_str_t * name)13152147Sz.hong@f5.com nxt_http_cookie_hash(nxt_mp_t *mp, nxt_str_t *name)
13162147Sz.hong@f5.com {
13172147Sz.hong@f5.com     return nxt_http_field_hash(mp, name, 1, NXT_HTTP_URI_ENCODING_NONE);
13182147Sz.hong@f5.com }
13192147Sz.hong@f5.com