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 return r;
282
283 fail:
284
285 nxt_mp_release(mp);
286
287 return NULL;
288 }
289
290
291 static const nxt_http_request_state_t nxt_http_request_init_state
292 nxt_aligned(64) =
293 {
294 .ready_handler = nxt_http_request_start,
295 .error_handler = nxt_http_request_close_handler,
296 };
297
298
299 static void
nxt_http_request_start(nxt_task_t * task,void * obj,void * data)300 nxt_http_request_start(nxt_task_t *task, void *obj, void *data)
301 {
302 nxt_int_t ret;
303 nxt_socket_conf_t *skcf;
304 nxt_http_request_t *r;
305
306 r = obj;
307
308 r->state = &nxt_http_request_body_state;
309
310 skcf = r->conf->socket_conf;
311
312 if (skcf->forwarded != NULL) {
313 ret = nxt_http_request_forward(task, r, skcf->forwarded);
314 if (nxt_slow_path(ret != NXT_OK)) {
315 goto fail;
316 }
317 }
318
319 if (skcf->client_ip != NULL) {
320 ret = nxt_http_request_forward(task, r, skcf->client_ip);
321 if (nxt_slow_path(ret != NXT_OK)) {
322 goto fail;
323 }
324 }
325
326 nxt_http_request_read_body(task, r);
327
328 return;
329
330 fail:
331 nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
332 }
333
334
335 static nxt_int_t
nxt_http_request_forward(nxt_task_t * task,nxt_http_request_t * r,nxt_http_forward_t * forward)336 nxt_http_request_forward(nxt_task_t *task, nxt_http_request_t *r,
337 nxt_http_forward_t *forward)
338 {
339 nxt_int_t ret;
340 nxt_array_t *client_ip_fields;
341 nxt_http_field_t *f, **fields, *protocol_field;
342 nxt_http_forward_header_t *client_ip, *protocol;
343
344 ret = nxt_http_route_addr_rule(r, forward->source, r->remote);
345 if (ret <= 0) {
346 return NXT_OK;
347 }
348
349 client_ip = &forward->client_ip;
350 protocol = &forward->protocol;
351
352 if (client_ip->header != NULL) {
353 client_ip_fields = nxt_array_create(r->mem_pool, 1,
354 sizeof(nxt_http_field_t *));
355 if (nxt_slow_path(client_ip_fields == NULL)) {
356 return NXT_ERROR;
357 }
358
359 } else {
360 client_ip_fields = NULL;
361 }
362
363 protocol_field = NULL;
364
365 nxt_list_each(f, r->fields) {
366 if (client_ip_fields != NULL
367 && f->hash == client_ip->header_hash
368 && f->value_length > 0
369 && f->name_length == client_ip->header->length
370 && nxt_memcasecmp(f->name, client_ip->header->start,
371 client_ip->header->length) == 0)
372 {
373 fields = nxt_array_add(client_ip_fields);
374 if (nxt_slow_path(fields == NULL)) {
375 return NXT_ERROR;
376 }
377
378 *fields = f;
379 }
380
381 if (protocol->header != NULL
382 && protocol_field == NULL
383 && f->hash == protocol->header_hash
384 && f->value_length > 0
385 && f->name_length == protocol->header->length
386 && nxt_memcasecmp(f->name, protocol->header->start,
387 protocol->header->length) == 0)
388 {
389 protocol_field = f;
390 }
391 } nxt_list_loop;
392
393 if (client_ip_fields != NULL) {
394 nxt_http_request_forward_client_ip(r, forward, client_ip_fields);
395 }
396
397 if (protocol_field != NULL) {
398 nxt_http_request_forward_protocol(r, protocol_field);
399 }
400
401 return NXT_OK;
402 }
403
404
405 static void
nxt_http_request_forward_client_ip(nxt_http_request_t * r,nxt_http_forward_t * forward,nxt_array_t * fields)406 nxt_http_request_forward_client_ip(nxt_http_request_t *r,
407 nxt_http_forward_t *forward, nxt_array_t *fields)
408 {
409 u_char *start, *p;
410 nxt_int_t ret, i, len;
411 nxt_sockaddr_t *sa, *prev_sa;
412 nxt_http_field_t **f;
413
414 prev_sa = r->remote;
415 f = (nxt_http_field_t **) fields->elts;
416
417 i = fields->nelts;
418
419 while (i-- > 0) {
420 start = f[i]->value;
421 len = f[i]->value_length;
422
423 do {
424 for (p = start + len - 1; p > start; p--, len--) {
425 if (*p != ' ' && *p != ',') {
426 break;
427 }
428 }
429
430 for (/* void */; p > start; p--) {
431 if (*p == ' ' || *p == ',') {
432 p++;
433 break;
434 }
435 }
436
437 sa = nxt_http_request_client_ip_sockaddr(r, p, len - (p - start));
438 if (nxt_slow_path(sa == NULL)) {
439 if (prev_sa != NULL) {
440 r->remote = prev_sa;
441 }
442
443 return;
444 }
445
446 if (!forward->recursive) {
447 r->remote = sa;
448 return;
449 }
450
451 ret = nxt_http_route_addr_rule(r, forward->source, sa);
452 if (ret <= 0 || (i == 0 && p == start)) {
453 r->remote = sa;
454 return;
455 }
456
457 prev_sa = sa;
458 len = p - 1 - start;
459
460 } while (len > 0);
461 }
462 }
463
464
465 static nxt_sockaddr_t *
nxt_http_request_client_ip_sockaddr(nxt_http_request_t * r,u_char * start,size_t len)466 nxt_http_request_client_ip_sockaddr(nxt_http_request_t *r, u_char *start,
467 size_t len)
468 {
469 nxt_str_t addr;
470 nxt_sockaddr_t *sa;
471
472 addr.start = start;
473 addr.length = len;
474
475 sa = nxt_sockaddr_parse_optport(r->mem_pool, &addr);
476 if (nxt_slow_path(sa == NULL)) {
477 return NULL;
478 }
479
480 switch (sa->u.sockaddr.sa_family) {
481 case AF_INET:
482 if (sa->u.sockaddr_in.sin_addr.s_addr == INADDR_ANY) {
483 return NULL;
484 }
485
486 break;
487
488 #if (NXT_INET6)
489 case AF_INET6:
490 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sockaddr_in6.sin6_addr)) {
491 return NULL;
492 }
493
494 break;
495 #endif /* NXT_INET6 */
496
497 default:
498 return NULL;
499 }
500
501 return sa;
502 }
503
504
505 static void
nxt_http_request_forward_protocol(nxt_http_request_t * r,nxt_http_field_t * field)506 nxt_http_request_forward_protocol(nxt_http_request_t *r,
507 nxt_http_field_t *field)
508 {
509 if (field->value_length == 4) {
510 if (nxt_memcasecmp(field->value, "http", 4) == 0) {
511 r->tls = 0;
512 }
513
514 } else if (field->value_length == 5) {
515 if (nxt_memcasecmp(field->value, "https", 5) == 0) {
516 r->tls = 1;
517 }
518
519 } else if (field->value_length == 2) {
520 if (nxt_memcasecmp(field->value, "on", 2) == 0) {
521 r->tls = 1;
522 }
523 }
524 }
525
526
527 static const nxt_http_request_state_t nxt_http_request_body_state
528 nxt_aligned(64) =
529 {
530 .ready_handler = nxt_http_request_ready,
531 .error_handler = nxt_http_request_close_handler,
532 };
533
534
535 static void
nxt_http_request_ready(nxt_task_t * task,void * obj,void * data)536 nxt_http_request_ready(nxt_task_t *task, void *obj, void *data)
537 {
538 nxt_http_action_t *action;
539 nxt_http_request_t *r;
540
541 r = obj;
542 action = r->conf->socket_conf->action;
543
544 nxt_http_request_action(task, r, action);
545 }
546
547
548 void
nxt_http_request_action(nxt_task_t * task,nxt_http_request_t * r,nxt_http_action_t * action)549 nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r,
550 nxt_http_action_t *action)
551 {
552 if (nxt_fast_path(action != NULL)) {
553
554 do {
555 action = action->handler(task, r, action);
556
557 if (action == NULL) {
558 return;
559 }
560
561 if (action == NXT_HTTP_ACTION_ERROR) {
562 break;
563 }
564
565 } while (r->pass_count++ < 255);
566 }
567
568 nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
569 }
570
571
572 nxt_http_action_t *
nxt_http_application_handler(nxt_task_t * task,nxt_http_request_t * r,nxt_http_action_t * action)573 nxt_http_application_handler(nxt_task_t *task, nxt_http_request_t *r,
574 nxt_http_action_t *action)
575 {
576 nxt_debug(task, "http application handler");
577
578 /*
579 * TODO: need an application flag to get local address
580 * required by "SERVER_ADDR" in Pyhton and PHP. Not used in Go.
581 */
582 nxt_http_request_proto_info(task, r);
583
584 if (r->host.length != 0) {
585 r->server_name = r->host;
586
587 } else {
588 nxt_str_set(&r->server_name, "localhost");
589 }
590
591 nxt_router_process_http_request(task, r, action);
592
593 return NULL;
594 }
595
596
597 static void
nxt_http_request_proto_info(nxt_task_t * task,nxt_http_request_t * r)598 nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r)
599 {
600 if (nxt_fast_path(r->proto.any != NULL)) {
601 nxt_http_proto[r->protocol].local_addr(task, r);
602 }
603 }
604
605
606 void
nxt_http_request_read_body(nxt_task_t * task,nxt_http_request_t * r)607 nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r)
608 {
609 if (nxt_fast_path(r->proto.any != NULL)) {
610 nxt_http_proto[r->protocol].body_read(task, r);
611 }
612 }
613
614
615 void
nxt_http_request_header_send(nxt_task_t * task,nxt_http_request_t * r,nxt_work_handler_t body_handler,void * data)616 nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
617 nxt_work_handler_t body_handler, void *data)
618 {
619 u_char *p, *end;
620 nxt_http_field_t *server, *date, *content_length;
621
622 /*
623 * TODO: "Server", "Date", and "Content-Length" processing should be moved
624 * to the last header filter.
625 */
626
627 server = nxt_list_zero_add(r->resp.fields);
628 if (nxt_slow_path(server == NULL)) {
629 goto fail;
630 }
631
632 nxt_http_field_set(server, "Server", NXT_SERVER);
633
634 if (r->resp.date == NULL) {
635 date = nxt_list_zero_add(r->resp.fields);
636 if (nxt_slow_path(date == NULL)) {
637 goto fail;
638 }
639
640 nxt_http_field_name_set(date, "Date");
641
642 p = nxt_mp_nget(r->mem_pool, nxt_http_date_cache.size);
643 if (nxt_slow_path(p == NULL)) {
644 goto fail;
645 }
646
647 (void) nxt_thread_time_string(task->thread, &nxt_http_date_cache, p);
648
649 date->value = p;
650 date->value_length = nxt_http_date_cache.size;
651
652 r->resp.date = date;
653 }
654
655 if (r->resp.content_length_n != -1
656 && (r->resp.content_length == NULL || r->resp.content_length->skip))
657 {
658 content_length = nxt_list_zero_add(r->resp.fields);
659 if (nxt_slow_path(content_length == NULL)) {
660 goto fail;
661 }
662
663 nxt_http_field_name_set(content_length, "Content-Length");
664
665 p = nxt_mp_nget(r->mem_pool, NXT_OFF_T_LEN);
666 if (nxt_slow_path(p == NULL)) {
667 goto fail;
668 }
669
670 content_length->value = p;
671 end = nxt_sprintf(p, p + NXT_OFF_T_LEN, "%O", r->resp.content_length_n);
672 content_length->value_length = end - p;
673
674 r->resp.content_length = content_length;
675 }
676
677 if (nxt_fast_path(r->proto.any != NULL)) {
678 nxt_http_proto[r->protocol].header_send(task, r, body_handler, data);
679 }
680
681 return;
682
683 fail:
684
685 nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
686 }
687
688
689 void
nxt_http_request_ws_frame_start(nxt_task_t * task,nxt_http_request_t * r,nxt_buf_t * ws_frame)690 nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r,
691 nxt_buf_t *ws_frame)
692 {
693 if (r->proto.any != NULL) {
694 nxt_http_proto[r->protocol].ws_frame_start(task, r, ws_frame);
695 }
696 }
697
698
699 void
nxt_http_request_send(nxt_task_t * task,nxt_http_request_t * r,nxt_buf_t * out)700 nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out)
701 {
702 if (nxt_fast_path(r->proto.any != NULL)) {
703 nxt_http_proto[r->protocol].send(task, r, out);
704 }
705 }
706
707
708 nxt_buf_t *
nxt_http_buf_mem(nxt_task_t * task,nxt_http_request_t * r,size_t size)709 nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, size_t size)
710 {
711 nxt_buf_t *b;
712
713 b = nxt_buf_mem_alloc(r->mem_pool, size, 0);
714 if (nxt_fast_path(b != NULL)) {
715 b->completion_handler = nxt_http_request_mem_buf_completion;
716 b->parent = r;
717 nxt_mp_retain(r->mem_pool);
718
719 } else {
720 nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
721 }
722
723 return b;
724 }
725
726
727 static void
nxt_http_request_mem_buf_completion(nxt_task_t * task,void * obj,void * data)728 nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, void *data)
729 {
730 nxt_buf_t *b, *next;
731 nxt_http_request_t *r;
732
733 b = obj;
734 r = data;
735
736 do {
737 next = b->next;
738
739 nxt_mp_free(r->mem_pool, b);
740 nxt_mp_release(r->mem_pool);
741
742 b = next;
743 } while (b != NULL);
744 }
745
746
747 nxt_buf_t *
nxt_http_buf_last(nxt_http_request_t * r)748 nxt_http_buf_last(nxt_http_request_t *r)
749 {
750 nxt_buf_t *last;
751
752 last = r->last;
753 r->last = NULL;
754
755 return last;
756 }
757
758
759 static void
nxt_http_request_done(nxt_task_t * task,void * obj,void * data)760 nxt_http_request_done(nxt_task_t *task, void *obj, void *data)
761 {
762 nxt_http_request_t *r;
763
764 r = data;
765
766 nxt_debug(task, "http request done");
767
768 nxt_http_request_close_handler(task, r, r->proto.any);
769 }
770
771
772 void
nxt_http_request_error_handler(nxt_task_t * task,void * obj,void * data)773 nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
774 {
775 nxt_http_proto_t proto;
776 nxt_http_request_t *r;
777
778 r = obj;
779 proto.any = data;
780
781 nxt_debug(task, "http request error handler");
782
783 r->error = 1;
784
785 if (nxt_fast_path(proto.any != NULL)) {
786 nxt_http_proto[r->protocol].discard(task, r, nxt_http_buf_last(r));
787 }
788 }
789
790
791 void
nxt_http_request_close_handler(nxt_task_t * task,void * obj,void * data)792 nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
793 {
794 nxt_http_proto_t proto;
795 nxt_http_request_t *r;
796 nxt_http_protocol_t protocol;
797 nxt_socket_conf_joint_t *conf;
798 nxt_router_access_log_t *access_log;
799
800 r = obj;
801 proto.any = data;
802
803 nxt_debug(task, "http request close handler");
804
805 conf = r->conf;
806
807 if (!r->logged) {
808 r->logged = 1;
809
810 access_log = conf->socket_conf->router_conf->access_log;
811
812 if (access_log != NULL) {
813 access_log->handler(task, r, access_log);
814 }
815 }
816
817 r->proto.any = NULL;
818
819 if (r->body != NULL && nxt_buf_is_file(r->body)
820 && r->body->file->fd != -1)
821 {
822 nxt_fd_close(r->body->file->fd);
823
824 r->body->file->fd = -1;
825 }
826
827 if (nxt_fast_path(proto.any != NULL)) {
828 protocol = r->protocol;
829
830 nxt_http_proto[protocol].close(task, proto, conf);
831
832 nxt_mp_release(r->mem_pool);
833 }
834 }
835
836
837 static u_char *
nxt_http_date_cache_handler(u_char * buf,nxt_realtime_t * now,struct tm * tm,size_t size,const char * format)838 nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, struct tm *tm,
839 size_t size, const char *format)
840 {
841 return nxt_http_date(buf, tm);
842 }
843
844
845 nxt_array_t *
nxt_http_arguments_parse(nxt_http_request_t * r)846 nxt_http_arguments_parse(nxt_http_request_t *r)
847 {
848 size_t name_length;
849 u_char *p, *dst, *dst_start, *start, *end, *name;
850 uint8_t d0, d1;
851 uint32_t hash;
852 nxt_array_t *args;
853 nxt_http_name_value_t *nv;
854
855 if (r->arguments != NULL) {
856 return r->arguments;
857 }
858
859 args = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
860 if (nxt_slow_path(args == NULL)) {
861 return NULL;
862 }
863
864 hash = NXT_HTTP_FIELD_HASH_INIT;
865 name = NULL;
866 name_length = 0;
867
868 dst_start = nxt_mp_nget(r->mem_pool, r->args->length);
869 if (nxt_slow_path(dst_start == NULL)) {
870 return NULL;
871 }
872
873 r->args_decoded.start = dst_start;
874
875 start = r->args->start;
876 end = start + r->args->length;
877
878 for (p = start, dst = dst_start; p < end; p++, dst++) {
879 *dst = *p;
880
881 switch (*p) {
882 case '=':
883 if (name == NULL) {
884 name_length = dst - dst_start;
885 name = dst_start;
886 dst_start = dst + 1;
887 }
888
889 continue;
890
891 case '&':
892 if (name_length != 0 || dst != dst_start) {
893 nv = nxt_http_argument(args, name, name_length, hash, dst_start,
894 dst);
895 if (nxt_slow_path(nv == NULL)) {
896 return NULL;
897 }
898 }
899
900 hash = NXT_HTTP_FIELD_HASH_INIT;
901 name_length = 0;
902 name = NULL;
903 dst_start = dst + 1;
904
905 continue;
906
907 case '+':
908 *dst = ' ';
909
910 break;
911
912 case '%':
913 if (nxt_slow_path(end - p <= 2)) {
914 break;
915 }
916
917 d0 = nxt_hex2int[p[1]];
918 d1 = nxt_hex2int[p[2]];
919
920 if (nxt_slow_path((d0 | d1) >= 16)) {
921 break;
922 }
923
924 p += 2;
925 *dst = (d0 << 4) + d1;
926
927 break;
928 }
929
930 if (name == NULL) {
931 hash = nxt_http_field_hash_char(hash, *dst);
932 }
933 }
934
935 r->args_decoded.length = dst - r->args_decoded.start;
936
937 if (name_length != 0 || dst != dst_start) {
938 nv = nxt_http_argument(args, name, name_length, hash, dst_start, dst);
939 if (nxt_slow_path(nv == NULL)) {
940 return NULL;
941 }
942 }
943
944 r->arguments = args;
945
946 return args;
947 }
948
949
950 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)951 nxt_http_argument(nxt_array_t *array, u_char *name, size_t name_length,
952 uint32_t hash, u_char *start, const u_char *end)
953 {
954 size_t length;
955 nxt_http_name_value_t *nv;
956
957 nv = nxt_array_add(array);
958 if (nxt_slow_path(nv == NULL)) {
959 return NULL;
960 }
961
962 nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
963
964 length = end - start;
965
966 if (name == NULL) {
967 name_length = length;
968 name = start;
969 length = 0;
970 }
971
972 nv->name_length = name_length;
973 nv->value_length = length;
974 nv->name = name;
975 nv->value = start;
976
977 return nv;
978 }
979
980
981 nxt_array_t *
nxt_http_cookies_parse(nxt_http_request_t * r)982 nxt_http_cookies_parse(nxt_http_request_t *r)
983 {
984 nxt_int_t ret;
985 nxt_array_t *cookies;
986 nxt_http_field_t *f;
987
988 if (r->cookies != NULL) {
989 return r->cookies;
990 }
991
992 cookies = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
993 if (nxt_slow_path(cookies == NULL)) {
994 return NULL;
995 }
996
997 nxt_list_each(f, r->fields) {
998
999 if (f->hash != NXT_HTTP_COOKIE_HASH
1000 || f->name_length != 6
1001 || nxt_strncasecmp(f->name, (u_char *) "Cookie", 6) != 0)
1002 {
1003 continue;
1004 }
1005
1006 ret = nxt_http_cookie_parse(cookies, f->value,
1007 f->value + f->value_length);
1008 if (ret != NXT_OK) {
1009 return NULL;
1010 }
1011
1012 } nxt_list_loop;
1013
1014 r->cookies = cookies;
1015
1016 return cookies;
1017 }
1018
1019
1020 static nxt_int_t
nxt_http_cookie_parse(nxt_array_t * cookies,u_char * start,const u_char * end)1021 nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, const u_char *end)
1022 {
1023 size_t name_length;
1024 u_char c, *p, *name;
1025 nxt_http_name_value_t *nv;
1026
1027 name = NULL;
1028 name_length = 0;
1029
1030 for (p = start; p < end; p++) {
1031 c = *p;
1032
1033 if (c == '=') {
1034 while (start[0] == ' ') { start++; }
1035
1036 name_length = p - start;
1037
1038 if (name_length != 0) {
1039 name = start;
1040 }
1041
1042 start = p + 1;
1043
1044 } else if (c == ';') {
1045 if (name != NULL) {
1046 nv = nxt_http_cookie(cookies, name, name_length, start, p);
1047 if (nxt_slow_path(nv == NULL)) {
1048 return NXT_ERROR;
1049 }
1050 }
1051
1052 name = NULL;
1053 start = p + 1;
1054 }
1055 }
1056
1057 if (name != NULL) {
1058 nv = nxt_http_cookie(cookies, name, name_length, start, p);
1059 if (nxt_slow_path(nv == NULL)) {
1060 return NXT_ERROR;
1061 }
1062 }
1063
1064 return NXT_OK;
1065 }
1066
1067
1068 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)1069 nxt_http_cookie(nxt_array_t *array, u_char *name, size_t name_length,
1070 u_char *start, const u_char *end)
1071 {
1072 u_char c, *p;
1073 uint32_t hash;
1074 nxt_http_name_value_t *nv;
1075
1076 nv = nxt_array_add(array);
1077 if (nxt_slow_path(nv == NULL)) {
1078 return NULL;
1079 }
1080
1081 nv->name_length = name_length;
1082 nv->name = name;
1083
1084 hash = NXT_HTTP_FIELD_HASH_INIT;
1085
1086 for (p = name; p < name + name_length; p++) {
1087 c = *p;
1088 hash = nxt_http_field_hash_char(hash, c);
1089 }
1090
1091 nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
1092
1093 while (start < end && end[-1] == ' ') { end--; }
1094
1095 nv->value_length = end - start;
1096 nv->value = start;
1097
1098 return nv;
1099 }
1100
1101
1102 int64_t
nxt_http_field_hash(nxt_mp_t * mp,nxt_str_t * name,nxt_bool_t case_sensitive,uint8_t encoding)1103 nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive,
1104 uint8_t encoding)
1105 {
1106 u_char c, *p, *src, *start, *end, plus;
1107 uint8_t d0, d1;
1108 uint32_t hash;
1109 nxt_str_t str;
1110 nxt_uint_t i;
1111
1112 str.length = name->length;
1113
1114 str.start = nxt_mp_nget(mp, str.length);
1115 if (nxt_slow_path(str.start == NULL)) {
1116 return -1;
1117 }
1118
1119 p = str.start;
1120
1121 hash = NXT_HTTP_FIELD_HASH_INIT;
1122
1123 if (encoding == NXT_HTTP_URI_ENCODING_NONE) {
1124 for (i = 0; i < name->length; i++) {
1125 c = name->start[i];
1126 *p++ = c;
1127
1128 c = case_sensitive ? c : nxt_lowcase(c);
1129 hash = nxt_http_field_hash_char(hash, c);
1130 }
1131
1132 goto end;
1133 }
1134
1135 plus = (encoding == NXT_HTTP_URI_ENCODING_PLUS) ? ' ' : '+';
1136
1137 start = name->start;
1138 end = start + name->length;
1139
1140 for (src = start; src < end; src++) {
1141 c = *src;
1142
1143 switch (c) {
1144 case '%':
1145 if (nxt_slow_path(end - src <= 2)) {
1146 return -1;
1147 }
1148
1149 d0 = nxt_hex2int[src[1]];
1150 d1 = nxt_hex2int[src[2]];
1151 src += 2;
1152
1153 if (nxt_slow_path((d0 | d1) >= 16)) {
1154 return -1;
1155 }
1156
1157 c = (d0 << 4) + d1;
1158 *p++ = c;
1159 break;
1160
1161 case '+':
1162 c = plus;
1163 *p++ = c;
1164 break;
1165
1166 default:
1167 *p++ = c;
1168 break;
1169 }
1170
1171 c = case_sensitive ? c : nxt_lowcase(c);
1172 hash = nxt_http_field_hash_char(hash, c);
1173 }
1174
1175 str.length = p - str.start;
1176
1177 end:
1178
1179 *name = str;
1180
1181 return nxt_http_field_hash_end(hash) & 0xFFFF;
1182 }
1183