xref: /unit/src/nxt_http_request.c (revision 2247:baa6b9879267)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_router.h>
8 #include <nxt_http.h>
9 
10 
11 static nxt_int_t nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp);
12 static void nxt_http_request_start(nxt_task_t *task, void *obj, void *data);
13 static nxt_int_t nxt_http_request_forward(nxt_task_t *task,
14     nxt_http_request_t *r, nxt_http_forward_t *forward);
15 static void nxt_http_request_forward_client_ip(nxt_http_request_t *r,
16     nxt_http_forward_t *forward, nxt_array_t *fields);
17 static nxt_sockaddr_t *nxt_http_request_client_ip_sockaddr(
18     nxt_http_request_t *r, u_char *start, size_t len);
19 static void nxt_http_request_forward_protocol(nxt_http_request_t *r,
20     nxt_http_field_t *field);
21 static void nxt_http_request_ready(nxt_task_t *task, void *obj, void *data);
22 static void nxt_http_request_proto_info(nxt_task_t *task,
23     nxt_http_request_t *r);
24 static void nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj,
25     void *data);
26 static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data);
27 
28 static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now,
29     struct tm *tm, size_t size, const char *format);
30 
31 static nxt_http_name_value_t *nxt_http_argument(nxt_array_t *array,
32     u_char *name, size_t name_length, uint32_t hash, u_char *start,
33     const u_char *end);
34 static nxt_int_t nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start,
35     const u_char *end);
36 static nxt_http_name_value_t *nxt_http_cookie(nxt_array_t *array, u_char *name,
37     size_t name_length, u_char *start, const u_char *end);
38 
39 
40 #define NXT_HTTP_COOKIE_HASH                                                  \
41     (nxt_http_field_hash_end(                                                 \
42      nxt_http_field_hash_char(                                                \
43      nxt_http_field_hash_char(                                                \
44      nxt_http_field_hash_char(                                                \
45      nxt_http_field_hash_char(                                                \
46      nxt_http_field_hash_char(                                                \
47      nxt_http_field_hash_char(NXT_HTTP_FIELD_HASH_INIT,                       \
48         'c'), 'o'), 'o'), 'k'), 'i'), 'e')) & 0xFFFF)
49 
50 
51 static const nxt_http_request_state_t  nxt_http_request_init_state;
52 static const nxt_http_request_state_t  nxt_http_request_body_state;
53 
54 
55 nxt_time_string_t  nxt_http_date_cache = {
56     (nxt_atomic_uint_t) -1,
57     nxt_http_date_cache_handler,
58     NULL,
59     NXT_HTTP_DATE_LEN,
60     NXT_THREAD_TIME_GMT,
61     NXT_THREAD_TIME_SEC,
62 };
63 
64 
65 nxt_int_t
nxt_http_init(nxt_task_t * task)66 nxt_http_init(nxt_task_t *task)
67 {
68     nxt_int_t  ret;
69 
70     ret = nxt_h1p_init(task);
71 
72     if (ret != NXT_OK) {
73         return ret;
74     }
75 
76     return nxt_http_response_hash_init(task);
77 }
78 
79 
80 nxt_int_t
nxt_http_request_host(void * ctx,nxt_http_field_t * field,uintptr_t data)81 nxt_http_request_host(void *ctx, nxt_http_field_t *field, uintptr_t data)
82 {
83     nxt_int_t           ret;
84     nxt_str_t           host;
85     nxt_http_request_t  *r;
86 
87     r = ctx;
88 
89     if (nxt_slow_path(r->host.start != NULL)) {
90         return NXT_HTTP_BAD_REQUEST;
91     }
92 
93     host.length = field->value_length;
94     host.start = field->value;
95 
96     ret = nxt_http_validate_host(&host, r->mem_pool);
97 
98     if (nxt_fast_path(ret == NXT_OK)) {
99         r->host = host;
100     }
101 
102     return ret;
103 }
104 
105 
106 static nxt_int_t
nxt_http_validate_host(nxt_str_t * host,nxt_mp_t * mp)107 nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp)
108 {
109     u_char      *h, ch;
110     size_t      i, dot_pos, host_length;
111     nxt_bool_t  lowcase;
112 
113     enum {
114         sw_usual,
115         sw_literal,
116         sw_rest
117     } state;
118 
119     dot_pos = host->length;
120     host_length = host->length;
121 
122     h = host->start;
123 
124     lowcase = 0;
125     state = sw_usual;
126 
127     for (i = 0; i < host->length; i++) {
128         ch = h[i];
129 
130         if (ch > ']') {
131             /* Short path. */
132             continue;
133         }
134 
135         switch (ch) {
136 
137         case '.':
138             if (dot_pos == i - 1) {
139                 return NXT_HTTP_BAD_REQUEST;
140             }
141 
142             dot_pos = i;
143             break;
144 
145         case ':':
146             if (state == sw_usual) {
147                 host_length = i;
148                 state = sw_rest;
149             }
150 
151             break;
152 
153         case '[':
154             if (i == 0) {
155                 state = sw_literal;
156             }
157 
158             break;
159 
160         case ']':
161             if (state == sw_literal) {
162                 host_length = i + 1;
163                 state = sw_rest;
164             }
165 
166             break;
167 
168         case '/':
169             return NXT_HTTP_BAD_REQUEST;
170 
171         default:
172             if (ch >= 'A' && ch <= 'Z') {
173                 lowcase = 1;
174             }
175 
176             break;
177         }
178     }
179 
180     if (dot_pos == host_length - 1) {
181         host_length--;
182     }
183 
184     host->length = host_length;
185 
186     if (lowcase) {
187         host->start = nxt_mp_nget(mp, host_length);
188         if (nxt_slow_path(host->start == NULL)) {
189             return NXT_HTTP_INTERNAL_SERVER_ERROR;
190         }
191 
192         nxt_memcpy_lowcase(host->start, h, host_length);
193     }
194 
195     return NXT_OK;
196 }
197 
198 
199 nxt_int_t
nxt_http_request_field(void * ctx,nxt_http_field_t * field,uintptr_t offset)200 nxt_http_request_field(void *ctx, nxt_http_field_t *field, uintptr_t offset)
201 {
202     nxt_http_request_t  *r;
203 
204     r = ctx;
205 
206     nxt_value_at(nxt_http_field_t *, r, offset) = field;
207 
208     return NXT_OK;
209 }
210 
211 
212 nxt_int_t
nxt_http_request_content_length(void * ctx,nxt_http_field_t * field,uintptr_t data)213 nxt_http_request_content_length(void *ctx, nxt_http_field_t *field,
214     uintptr_t data)
215 {
216     nxt_off_t           n, max_body_size;
217     nxt_http_request_t  *r;
218 
219     r = ctx;
220 
221     if (nxt_fast_path(r->content_length == NULL)) {
222         r->content_length = field;
223 
224         n = nxt_off_t_parse(field->value, field->value_length);
225 
226         if (nxt_fast_path(n >= 0)) {
227             r->content_length_n = n;
228 
229             max_body_size = r->conf->socket_conf->max_body_size;
230 
231             if (nxt_slow_path(n > max_body_size)) {
232                 return NXT_HTTP_PAYLOAD_TOO_LARGE;
233             }
234 
235             return NXT_OK;
236         }
237     }
238 
239     return NXT_HTTP_BAD_REQUEST;
240 }
241 
242 
243 nxt_http_request_t *
nxt_http_request_create(nxt_task_t * task)244 nxt_http_request_create(nxt_task_t *task)
245 {
246     nxt_mp_t            *mp;
247     nxt_buf_t           *last;
248     nxt_http_request_t  *r;
249 
250     mp = nxt_mp_create(4096, 128, 512, 32);
251     if (nxt_slow_path(mp == NULL)) {
252         return NULL;
253     }
254 
255     r = nxt_mp_zget(mp, sizeof(nxt_http_request_t));
256     if (nxt_slow_path(r == NULL)) {
257         goto fail;
258     }
259 
260     r->resp.fields = nxt_list_create(mp, 8, sizeof(nxt_http_field_t));
261     if (nxt_slow_path(r->resp.fields == NULL)) {
262         goto fail;
263     }
264 
265     last = nxt_mp_zget(mp, NXT_BUF_SYNC_SIZE);
266     if (nxt_slow_path(last == NULL)) {
267         goto fail;
268     }
269 
270     nxt_buf_set_sync(last);
271     nxt_buf_set_last(last);
272     last->completion_handler = nxt_http_request_done;
273     last->parent = r;
274     r->last = last;
275 
276     r->mem_pool = mp;
277     r->content_length_n = -1;
278     r->resp.content_length_n = -1;
279     r->state = &nxt_http_request_init_state;
280 
281     r->start_time = nxt_thread_monotonic_time(task->thread);
282 
283     task->thread->engine->requests_cnt++;
284 
285     r->tstr_cache.var.pool = mp;
286 
287     return r;
288 
289 fail:
290 
291     nxt_mp_release(mp);
292 
293     return NULL;
294 }
295 
296 
297 static const nxt_http_request_state_t  nxt_http_request_init_state
298     nxt_aligned(64) =
299 {
300     .ready_handler = nxt_http_request_start,
301     .error_handler = nxt_http_request_close_handler,
302 };
303 
304 
305 static void
nxt_http_request_start(nxt_task_t * task,void * obj,void * data)306 nxt_http_request_start(nxt_task_t *task, void *obj, void *data)
307 {
308     nxt_int_t           ret;
309     nxt_socket_conf_t   *skcf;
310     nxt_http_request_t  *r;
311 
312     r = obj;
313 
314     r->state = &nxt_http_request_body_state;
315 
316     skcf = r->conf->socket_conf;
317 
318     if (skcf->forwarded != NULL) {
319         ret = nxt_http_request_forward(task, r, skcf->forwarded);
320         if (nxt_slow_path(ret != NXT_OK)) {
321             goto fail;
322         }
323     }
324 
325     if (skcf->client_ip != NULL) {
326         ret = nxt_http_request_forward(task, r, skcf->client_ip);
327         if (nxt_slow_path(ret != NXT_OK)) {
328             goto fail;
329         }
330     }
331 
332     nxt_http_request_read_body(task, r);
333 
334     return;
335 
336 fail:
337     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
338 }
339 
340 
341 static nxt_int_t
nxt_http_request_forward(nxt_task_t * task,nxt_http_request_t * r,nxt_http_forward_t * forward)342 nxt_http_request_forward(nxt_task_t *task, nxt_http_request_t *r,
343     nxt_http_forward_t *forward)
344 {
345     nxt_int_t                  ret;
346     nxt_array_t                *client_ip_fields;
347     nxt_http_field_t           *f, **fields, *protocol_field;
348     nxt_http_forward_header_t  *client_ip, *protocol;
349 
350     ret = nxt_http_route_addr_rule(r, forward->source, r->remote);
351     if (ret <= 0) {
352         return NXT_OK;
353     }
354 
355     client_ip = &forward->client_ip;
356     protocol = &forward->protocol;
357 
358     if (client_ip->header != NULL) {
359         client_ip_fields = nxt_array_create(r->mem_pool, 1,
360                                             sizeof(nxt_http_field_t *));
361         if (nxt_slow_path(client_ip_fields == NULL)) {
362             return NXT_ERROR;
363         }
364 
365     } else {
366         client_ip_fields = NULL;
367     }
368 
369     protocol_field = NULL;
370 
371     nxt_list_each(f, r->fields) {
372         if (client_ip_fields != NULL
373             && f->hash == client_ip->header_hash
374             && f->value_length > 0
375             && f->name_length == client_ip->header->length
376             && nxt_memcasecmp(f->name, client_ip->header->start,
377                               client_ip->header->length) == 0)
378         {
379             fields = nxt_array_add(client_ip_fields);
380             if (nxt_slow_path(fields == NULL)) {
381                 return NXT_ERROR;
382             }
383 
384             *fields = f;
385         }
386 
387         if (protocol->header != NULL
388             && protocol_field == NULL
389             && f->hash == protocol->header_hash
390             && f->value_length > 0
391             && f->name_length == protocol->header->length
392             && nxt_memcasecmp(f->name, protocol->header->start,
393                               protocol->header->length) == 0)
394         {
395             protocol_field = f;
396         }
397     } nxt_list_loop;
398 
399     if (client_ip_fields != NULL) {
400         nxt_http_request_forward_client_ip(r, forward, client_ip_fields);
401     }
402 
403     if (protocol_field != NULL) {
404         nxt_http_request_forward_protocol(r, protocol_field);
405     }
406 
407     return NXT_OK;
408 }
409 
410 
411 static void
nxt_http_request_forward_client_ip(nxt_http_request_t * r,nxt_http_forward_t * forward,nxt_array_t * fields)412 nxt_http_request_forward_client_ip(nxt_http_request_t *r,
413     nxt_http_forward_t *forward, nxt_array_t *fields)
414 {
415     u_char            *start, *p;
416     nxt_int_t         ret, i, len;
417     nxt_sockaddr_t    *sa, *prev_sa;
418     nxt_http_field_t  **f;
419 
420     prev_sa = r->remote;
421     f = (nxt_http_field_t **) fields->elts;
422 
423     i = fields->nelts;
424 
425     while (i-- > 0) {
426         start = f[i]->value;
427         len = f[i]->value_length;
428 
429         do {
430             for (p = start + len - 1; p > start; p--, len--) {
431                 if (*p != ' ' && *p != ',') {
432                     break;
433                 }
434             }
435 
436             for (/* void */; p > start; p--) {
437                 if (*p == ' ' || *p == ',') {
438                     p++;
439                     break;
440                 }
441             }
442 
443             sa = nxt_http_request_client_ip_sockaddr(r, p, len - (p - start));
444             if (nxt_slow_path(sa == NULL)) {
445                 if (prev_sa != NULL) {
446                     r->remote = prev_sa;
447                 }
448 
449                 return;
450             }
451 
452             if (!forward->recursive) {
453                 r->remote = sa;
454                 return;
455             }
456 
457             ret = nxt_http_route_addr_rule(r, forward->source, sa);
458             if (ret <= 0 || (i == 0 && p == start)) {
459                 r->remote = sa;
460                 return;
461             }
462 
463             prev_sa = sa;
464             len = p - 1 - start;
465 
466         } while (len > 0);
467     }
468 }
469 
470 
471 static nxt_sockaddr_t *
nxt_http_request_client_ip_sockaddr(nxt_http_request_t * r,u_char * start,size_t len)472 nxt_http_request_client_ip_sockaddr(nxt_http_request_t *r, u_char *start,
473     size_t len)
474 {
475     nxt_str_t       addr;
476     nxt_sockaddr_t  *sa;
477 
478     addr.start = start;
479     addr.length = len;
480 
481     sa = nxt_sockaddr_parse_optport(r->mem_pool, &addr);
482     if (nxt_slow_path(sa == NULL)) {
483         return NULL;
484     }
485 
486     switch (sa->u.sockaddr.sa_family) {
487         case AF_INET:
488             if (sa->u.sockaddr_in.sin_addr.s_addr == INADDR_ANY) {
489                 return NULL;
490             }
491 
492             break;
493 
494 #if (NXT_INET6)
495         case AF_INET6:
496             if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sockaddr_in6.sin6_addr)) {
497                 return NULL;
498             }
499 
500             break;
501 #endif /* NXT_INET6 */
502 
503         default:
504             return NULL;
505     }
506 
507     return sa;
508 }
509 
510 
511 static void
nxt_http_request_forward_protocol(nxt_http_request_t * r,nxt_http_field_t * field)512 nxt_http_request_forward_protocol(nxt_http_request_t *r,
513     nxt_http_field_t *field)
514 {
515     if (field->value_length == 4) {
516         if (nxt_memcasecmp(field->value, "http", 4) == 0) {
517             r->tls = 0;
518         }
519 
520     } else if (field->value_length == 5) {
521         if (nxt_memcasecmp(field->value, "https", 5) == 0) {
522             r->tls = 1;
523         }
524 
525     } else if (field->value_length == 2) {
526         if (nxt_memcasecmp(field->value, "on", 2) == 0) {
527             r->tls = 1;
528         }
529     }
530 }
531 
532 
533 static const nxt_http_request_state_t  nxt_http_request_body_state
534     nxt_aligned(64) =
535 {
536     .ready_handler = nxt_http_request_ready,
537     .error_handler = nxt_http_request_close_handler,
538 };
539 
540 
541 static void
nxt_http_request_ready(nxt_task_t * task,void * obj,void * data)542 nxt_http_request_ready(nxt_task_t *task, void *obj, void *data)
543 {
544     nxt_http_action_t   *action;
545     nxt_http_request_t  *r;
546 
547     r = obj;
548     action = r->conf->socket_conf->action;
549 
550     nxt_http_request_action(task, r, action);
551 }
552 
553 
554 void
nxt_http_request_action(nxt_task_t * task,nxt_http_request_t * r,nxt_http_action_t * action)555 nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r,
556     nxt_http_action_t *action)
557 {
558     if (nxt_fast_path(action != NULL)) {
559 
560         do {
561             action = action->handler(task, r, action);
562 
563             if (action == NULL) {
564                 return;
565             }
566 
567             if (action == NXT_HTTP_ACTION_ERROR) {
568                 break;
569             }
570 
571         } while (r->pass_count++ < 255);
572     }
573 
574     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
575 }
576 
577 
578 nxt_http_action_t *
nxt_http_application_handler(nxt_task_t * task,nxt_http_request_t * r,nxt_http_action_t * action)579 nxt_http_application_handler(nxt_task_t *task, nxt_http_request_t *r,
580     nxt_http_action_t *action)
581 {
582     nxt_debug(task, "http application handler");
583 
584     /*
585      * TODO: need an application flag to get local address
586      * required by "SERVER_ADDR" in Pyhton and PHP. Not used in Go.
587      */
588     nxt_http_request_proto_info(task, r);
589 
590     if (r->host.length != 0) {
591         r->server_name = r->host;
592 
593     } else {
594         nxt_str_set(&r->server_name, "localhost");
595     }
596 
597     nxt_router_process_http_request(task, r, action);
598 
599     return NULL;
600 }
601 
602 
603 static void
nxt_http_request_proto_info(nxt_task_t * task,nxt_http_request_t * r)604 nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r)
605 {
606     if (nxt_fast_path(r->proto.any != NULL)) {
607         nxt_http_proto[r->protocol].local_addr(task, r);
608     }
609 }
610 
611 
612 void
nxt_http_request_read_body(nxt_task_t * task,nxt_http_request_t * r)613 nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r)
614 {
615     if (nxt_fast_path(r->proto.any != NULL)) {
616         nxt_http_proto[r->protocol].body_read(task, r);
617     }
618 }
619 
620 
621 void
nxt_http_request_header_send(nxt_task_t * task,nxt_http_request_t * r,nxt_work_handler_t body_handler,void * data)622 nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
623     nxt_work_handler_t body_handler, void *data)
624 {
625     u_char            *p, *end;
626     nxt_http_field_t  *server, *date, *content_length;
627 
628     /*
629      * TODO: "Server", "Date", and "Content-Length" processing should be moved
630      * to the last header filter.
631      */
632 
633     server = nxt_list_zero_add(r->resp.fields);
634     if (nxt_slow_path(server == NULL)) {
635         goto fail;
636     }
637 
638     nxt_http_field_set(server, "Server", NXT_SERVER);
639 
640     if (r->resp.date == NULL) {
641         date = nxt_list_zero_add(r->resp.fields);
642         if (nxt_slow_path(date == NULL)) {
643             goto fail;
644         }
645 
646         nxt_http_field_name_set(date, "Date");
647 
648         p = nxt_mp_nget(r->mem_pool, nxt_http_date_cache.size);
649         if (nxt_slow_path(p == NULL)) {
650             goto fail;
651         }
652 
653         (void) nxt_thread_time_string(task->thread, &nxt_http_date_cache, p);
654 
655         date->value = p;
656         date->value_length = nxt_http_date_cache.size;
657 
658         r->resp.date = date;
659     }
660 
661     if (r->resp.content_length_n != -1
662         && (r->resp.content_length == NULL || r->resp.content_length->skip))
663     {
664         content_length = nxt_list_zero_add(r->resp.fields);
665         if (nxt_slow_path(content_length == NULL)) {
666             goto fail;
667         }
668 
669         nxt_http_field_name_set(content_length, "Content-Length");
670 
671         p = nxt_mp_nget(r->mem_pool, NXT_OFF_T_LEN);
672         if (nxt_slow_path(p == NULL)) {
673             goto fail;
674         }
675 
676         content_length->value = p;
677         end = nxt_sprintf(p, p + NXT_OFF_T_LEN, "%O", r->resp.content_length_n);
678         content_length->value_length = end - p;
679 
680         r->resp.content_length = content_length;
681     }
682 
683     if (nxt_fast_path(r->proto.any != NULL)) {
684         nxt_http_proto[r->protocol].header_send(task, r, body_handler, data);
685     }
686 
687     return;
688 
689 fail:
690 
691     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
692 }
693 
694 
695 void
nxt_http_request_ws_frame_start(nxt_task_t * task,nxt_http_request_t * r,nxt_buf_t * ws_frame)696 nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r,
697     nxt_buf_t *ws_frame)
698 {
699     if (r->proto.any != NULL) {
700         nxt_http_proto[r->protocol].ws_frame_start(task, r, ws_frame);
701     }
702 }
703 
704 
705 void
nxt_http_request_send(nxt_task_t * task,nxt_http_request_t * r,nxt_buf_t * out)706 nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out)
707 {
708     if (nxt_fast_path(r->proto.any != NULL)) {
709         nxt_http_proto[r->protocol].send(task, r, out);
710     }
711 }
712 
713 
714 nxt_buf_t *
nxt_http_buf_mem(nxt_task_t * task,nxt_http_request_t * r,size_t size)715 nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, size_t size)
716 {
717     nxt_buf_t  *b;
718 
719     b = nxt_buf_mem_alloc(r->mem_pool, size, 0);
720     if (nxt_fast_path(b != NULL)) {
721         b->completion_handler = nxt_http_request_mem_buf_completion;
722         b->parent = r;
723         nxt_mp_retain(r->mem_pool);
724 
725     } else {
726         nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
727     }
728 
729     return b;
730 }
731 
732 
733 static void
nxt_http_request_mem_buf_completion(nxt_task_t * task,void * obj,void * data)734 nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, void *data)
735 {
736     nxt_buf_t           *b, *next;
737     nxt_http_request_t  *r;
738 
739     b = obj;
740     r = data;
741 
742     do {
743         next = b->next;
744 
745         nxt_mp_free(r->mem_pool, b);
746         nxt_mp_release(r->mem_pool);
747 
748         b = next;
749     } while (b != NULL);
750 }
751 
752 
753 nxt_buf_t *
nxt_http_buf_last(nxt_http_request_t * r)754 nxt_http_buf_last(nxt_http_request_t *r)
755 {
756     nxt_buf_t  *last;
757 
758     last = r->last;
759     r->last = NULL;
760 
761     return last;
762 }
763 
764 
765 static void
nxt_http_request_done(nxt_task_t * task,void * obj,void * data)766 nxt_http_request_done(nxt_task_t *task, void *obj, void *data)
767 {
768     nxt_http_request_t  *r;
769 
770     r = data;
771 
772     nxt_debug(task, "http request done");
773 
774     nxt_http_request_close_handler(task, r, r->proto.any);
775 }
776 
777 
778 void
nxt_http_request_error_handler(nxt_task_t * task,void * obj,void * data)779 nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
780 {
781     nxt_http_proto_t    proto;
782     nxt_http_request_t  *r;
783 
784     r = obj;
785     proto.any = data;
786 
787     nxt_debug(task, "http request error handler");
788 
789     r->error = 1;
790 
791     if (nxt_fast_path(proto.any != NULL)) {
792         nxt_http_proto[r->protocol].discard(task, r, nxt_http_buf_last(r));
793     }
794 }
795 
796 
797 void
nxt_http_request_close_handler(nxt_task_t * task,void * obj,void * data)798 nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
799 {
800     nxt_tstr_t               *log_format;
801     nxt_http_proto_t         proto;
802     nxt_http_request_t       *r;
803     nxt_http_protocol_t      protocol;
804     nxt_socket_conf_joint_t  *conf;
805     nxt_router_access_log_t  *access_log;
806 
807     r = obj;
808     proto.any = data;
809 
810     conf = r->conf;
811 
812     if (!r->logged) {
813         r->logged = 1;
814 
815         access_log = conf->socket_conf->router_conf->access_log;
816         log_format = conf->socket_conf->router_conf->log_format;
817 
818         if (access_log != NULL) {
819             access_log->handler(task, r, access_log, log_format);
820             return;
821         }
822     }
823 
824     nxt_debug(task, "http request close handler");
825 
826     r->proto.any = NULL;
827 
828     if (r->body != NULL && nxt_buf_is_file(r->body)
829         && r->body->file->fd != -1)
830     {
831         nxt_fd_close(r->body->file->fd);
832 
833         r->body->file->fd = -1;
834     }
835 
836     if (nxt_fast_path(proto.any != NULL)) {
837         protocol = r->protocol;
838 
839         nxt_http_proto[protocol].close(task, proto, conf);
840 
841         nxt_mp_release(r->mem_pool);
842     }
843 }
844 
845 
846 static u_char *
nxt_http_date_cache_handler(u_char * buf,nxt_realtime_t * now,struct tm * tm,size_t size,const char * format)847 nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, struct tm *tm,
848     size_t size, const char *format)
849 {
850     return nxt_http_date(buf, tm);
851 }
852 
853 
854 nxt_array_t *
nxt_http_arguments_parse(nxt_http_request_t * r)855 nxt_http_arguments_parse(nxt_http_request_t *r)
856 {
857     size_t                 name_length;
858     u_char                 *p, *dst, *dst_start, *start, *end, *name;
859     uint8_t                d0, d1;
860     uint32_t               hash;
861     nxt_array_t            *args;
862     nxt_http_name_value_t  *nv;
863 
864     if (r->arguments != NULL) {
865         return r->arguments;
866     }
867 
868     args = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
869     if (nxt_slow_path(args == NULL)) {
870         return NULL;
871     }
872 
873     hash = NXT_HTTP_FIELD_HASH_INIT;
874     name = NULL;
875     name_length = 0;
876 
877     dst_start = nxt_mp_nget(r->mem_pool, r->args->length);
878     if (nxt_slow_path(dst_start == NULL)) {
879         return NULL;
880     }
881 
882     r->args_decoded.start = dst_start;
883 
884     start = r->args->start;
885     end = start + r->args->length;
886 
887     for (p = start, dst = dst_start; p < end; p++, dst++) {
888         *dst = *p;
889 
890         switch (*p) {
891         case '=':
892             if (name == NULL) {
893                 name_length = dst - dst_start;
894                 name = dst_start;
895                 dst_start = dst + 1;
896             }
897 
898             continue;
899 
900         case '&':
901             if (name_length != 0 || dst != dst_start) {
902                 nv = nxt_http_argument(args, name, name_length, hash, dst_start,
903                                        dst);
904                 if (nxt_slow_path(nv == NULL)) {
905                     return NULL;
906                 }
907             }
908 
909             hash = NXT_HTTP_FIELD_HASH_INIT;
910             name_length = 0;
911             name = NULL;
912             dst_start = dst + 1;
913 
914             continue;
915 
916         case '+':
917             *dst = ' ';
918 
919             break;
920 
921         case '%':
922             if (nxt_slow_path(end - p <= 2)) {
923                 break;
924             }
925 
926             d0 = nxt_hex2int[p[1]];
927             d1 = nxt_hex2int[p[2]];
928 
929             if (nxt_slow_path((d0 | d1) >= 16)) {
930                 break;
931             }
932 
933             p += 2;
934             *dst = (d0 << 4) + d1;
935 
936             break;
937         }
938 
939         if (name == NULL) {
940             hash = nxt_http_field_hash_char(hash, *dst);
941         }
942     }
943 
944     r->args_decoded.length = dst - r->args_decoded.start;
945 
946     if (name_length != 0 || dst != dst_start) {
947         nv = nxt_http_argument(args, name, name_length, hash, dst_start, dst);
948         if (nxt_slow_path(nv == NULL)) {
949             return NULL;
950         }
951     }
952 
953     r->arguments = args;
954 
955     return args;
956 }
957 
958 
959 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)960 nxt_http_argument(nxt_array_t *array, u_char *name, size_t name_length,
961     uint32_t hash, u_char *start, const u_char *end)
962 {
963     size_t                 length;
964     nxt_http_name_value_t  *nv;
965 
966     nv = nxt_array_add(array);
967     if (nxt_slow_path(nv == NULL)) {
968         return NULL;
969     }
970 
971     nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
972 
973     length = end - start;
974 
975     if (name == NULL) {
976         name_length = length;
977         name = start;
978         length = 0;
979     }
980 
981     nv->name_length = name_length;
982     nv->value_length = length;
983     nv->name = name;
984     nv->value = start;
985 
986     return nv;
987 }
988 
989 
990 nxt_array_t *
nxt_http_cookies_parse(nxt_http_request_t * r)991 nxt_http_cookies_parse(nxt_http_request_t *r)
992 {
993     nxt_int_t         ret;
994     nxt_array_t       *cookies;
995     nxt_http_field_t  *f;
996 
997     if (r->cookies != NULL) {
998         return r->cookies;
999     }
1000 
1001     cookies = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
1002     if (nxt_slow_path(cookies == NULL)) {
1003         return NULL;
1004     }
1005 
1006     nxt_list_each(f, r->fields) {
1007 
1008         if (f->hash != NXT_HTTP_COOKIE_HASH
1009             || f->name_length != 6
1010             || nxt_strncasecmp(f->name, (u_char *) "Cookie", 6) != 0)
1011         {
1012             continue;
1013         }
1014 
1015         ret = nxt_http_cookie_parse(cookies, f->value,
1016                                     f->value + f->value_length);
1017         if (ret != NXT_OK) {
1018             return NULL;
1019         }
1020 
1021     } nxt_list_loop;
1022 
1023     r->cookies = cookies;
1024 
1025     return cookies;
1026 }
1027 
1028 
1029 static nxt_int_t
nxt_http_cookie_parse(nxt_array_t * cookies,u_char * start,const u_char * end)1030 nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, const u_char *end)
1031 {
1032     size_t                 name_length;
1033     u_char                 c, *p, *name;
1034     nxt_http_name_value_t  *nv;
1035 
1036     name = NULL;
1037     name_length = 0;
1038 
1039     for (p = start; p < end; p++) {
1040         c = *p;
1041 
1042         if (c == '=' && name == NULL) {
1043             while (start[0] == ' ') { start++; }
1044 
1045             name_length = p - start;
1046             name = start;
1047 
1048             start = p + 1;
1049 
1050         } else if (c == ';') {
1051             if (name != NULL) {
1052                 nv = nxt_http_cookie(cookies, name, name_length, start, p);
1053                 if (nxt_slow_path(nv == NULL)) {
1054                     return NXT_ERROR;
1055                 }
1056             }
1057 
1058             name = NULL;
1059             start = p + 1;
1060          }
1061     }
1062 
1063     if (name != NULL) {
1064         nv = nxt_http_cookie(cookies, name, name_length, start, p);
1065         if (nxt_slow_path(nv == NULL)) {
1066             return NXT_ERROR;
1067         }
1068     }
1069 
1070     return NXT_OK;
1071 }
1072 
1073 
1074 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)1075 nxt_http_cookie(nxt_array_t *array, u_char *name, size_t name_length,
1076     u_char *start, const u_char *end)
1077 {
1078     u_char                 c, *p;
1079     uint32_t               hash;
1080     nxt_http_name_value_t  *nv;
1081 
1082     nv = nxt_array_add(array);
1083     if (nxt_slow_path(nv == NULL)) {
1084         return NULL;
1085     }
1086 
1087     nv->name_length = name_length;
1088     nv->name = name;
1089 
1090     hash = NXT_HTTP_FIELD_HASH_INIT;
1091 
1092     for (p = name; p < name + name_length; p++) {
1093         c = *p;
1094         hash = nxt_http_field_hash_char(hash, c);
1095     }
1096 
1097     nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
1098 
1099     while (start < end && end[-1] == ' ') { end--; }
1100 
1101     nv->value_length = end - start;
1102     nv->value = start;
1103 
1104     return nv;
1105 }
1106 
1107 
1108 int64_t
nxt_http_field_hash(nxt_mp_t * mp,nxt_str_t * name,nxt_bool_t case_sensitive,uint8_t encoding)1109 nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive,
1110     uint8_t encoding)
1111 {
1112     u_char      c, *p, *src, *start, *end, plus;
1113     uint8_t     d0, d1;
1114     uint32_t    hash;
1115     nxt_str_t   str;
1116     nxt_uint_t  i;
1117 
1118     str.length = name->length;
1119 
1120     str.start = nxt_mp_nget(mp, str.length);
1121     if (nxt_slow_path(str.start == NULL)) {
1122         return -1;
1123     }
1124 
1125     p = str.start;
1126 
1127     hash = NXT_HTTP_FIELD_HASH_INIT;
1128 
1129     if (encoding == NXT_HTTP_URI_ENCODING_NONE) {
1130         for (i = 0; i < name->length; i++) {
1131             c = name->start[i];
1132             *p++ = c;
1133 
1134             c = case_sensitive ? c : nxt_lowcase(c);
1135             hash = nxt_http_field_hash_char(hash, c);
1136         }
1137 
1138         goto end;
1139     }
1140 
1141     plus = (encoding == NXT_HTTP_URI_ENCODING_PLUS) ? ' ' : '+';
1142 
1143     start = name->start;
1144     end = start + name->length;
1145 
1146     for (src = start; src < end; src++) {
1147         c = *src;
1148 
1149         switch (c) {
1150         case '%':
1151             if (nxt_slow_path(end - src <= 2)) {
1152                 return -1;
1153             }
1154 
1155             d0 = nxt_hex2int[src[1]];
1156             d1 = nxt_hex2int[src[2]];
1157             src += 2;
1158 
1159             if (nxt_slow_path((d0 | d1) >= 16)) {
1160                 return -1;
1161             }
1162 
1163             c = (d0 << 4) + d1;
1164             *p++ = c;
1165             break;
1166 
1167         case '+':
1168             c = plus;
1169             *p++ = c;
1170             break;
1171 
1172         default:
1173             *p++ = c;
1174             break;
1175         }
1176 
1177         c = case_sensitive ? c : nxt_lowcase(c);
1178         hash = nxt_http_field_hash_char(hash, c);
1179     }
1180 
1181     str.length = p - str.start;
1182 
1183 end:
1184 
1185     *name = str;
1186 
1187     return nxt_http_field_hash_end(hash) & 0xFFFF;
1188 }
1189 
1190 
1191 int64_t
nxt_http_argument_hash(nxt_mp_t * mp,nxt_str_t * name)1192 nxt_http_argument_hash(nxt_mp_t *mp, nxt_str_t *name)
1193 {
1194     return nxt_http_field_hash(mp, name, 1, NXT_HTTP_URI_ENCODING_PLUS);
1195 }
1196 
1197 
1198 int64_t
nxt_http_header_hash(nxt_mp_t * mp,nxt_str_t * name)1199 nxt_http_header_hash(nxt_mp_t *mp, nxt_str_t *name)
1200 {
1201     u_char     c, *p;
1202     uint32_t   i, hash;
1203     nxt_str_t  str;
1204 
1205     str.length = name->length;
1206 
1207     str.start = nxt_mp_nget(mp, str.length);
1208     if (nxt_slow_path(str.start == NULL)) {
1209         return -1;
1210     }
1211 
1212     p = str.start;
1213     hash = NXT_HTTP_FIELD_HASH_INIT;
1214 
1215     for (i = 0; i < name->length; i++) {
1216         c = name->start[i];
1217 
1218         if (c >= 'A' && c <= 'Z') {
1219             *p = c | 0x20;
1220 
1221         } else if (c == '_') {
1222             *p = '-';
1223 
1224         } else {
1225             *p = c;
1226         }
1227 
1228         hash = nxt_http_field_hash_char(hash, *p);
1229         p++;
1230     }
1231 
1232     *name = str;
1233 
1234     return nxt_http_field_hash_end(hash) & 0xFFFF;
1235 }
1236 
1237 
1238 int64_t
nxt_http_cookie_hash(nxt_mp_t * mp,nxt_str_t * name)1239 nxt_http_cookie_hash(nxt_mp_t *mp, nxt_str_t *name)
1240 {
1241     return nxt_http_field_hash(mp, name, 1, NXT_HTTP_URI_ENCODING_NONE);
1242 }
1243