Klaus Demo nginx / a8c54c0
axe old % log format Igor Sysoev 15 years ago
7 changed file(s) with 48 addition(s) and 942 deletion(s). Raw diff Collapse all Expand all
269269 src/http/ngx_http_variables.h \
270270 src/http/ngx_http_script.h \
271271 src/http/ngx_http_upstream.h \
272 src/http/ngx_http_busy_lock.h \
273 src/http/modules/ngx_http_log_module.h"
272 src/http/ngx_http_busy_lock.h"
274273
275274 HTTP_SRCS="src/http/ngx_http.c \
276275 src/http/ngx_http_core_module.c \
7474 u_int size);
7575 static void ngx_http_gzip_filter_free(void *opaque, void *address);
7676 static void ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx);
77
78 static u_char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf,
79 ngx_http_log_op_t *op);
8077
8178 static ngx_int_t ngx_http_gzip_add_variables(ngx_conf_t *cf);
8279 static ngx_int_t ngx_http_gzip_ratio_variable(ngx_http_request_t *r,
227224 NULL, /* exit master */
228225 NGX_MODULE_V1_PADDING
229226 };
230
231
232 static ngx_http_log_op_name_t ngx_http_gzip_log_fmt_ops[] = {
233 { ngx_string("gzip_ratio"), NGX_INT32_LEN + 3,
234 NULL, NULL, ngx_http_gzip_log_ratio },
235 { ngx_null_string, 0, NULL, NULL, NULL }
236 };
237
238227
239228
240229 static u_char gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
906895 }
907896
908897
909 static u_char *
910 ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf,
911 ngx_http_log_op_t *op)
912 {
913 ngx_uint_t zint, zfrac;
914 ngx_http_gzip_ctx_t *ctx;
915
916 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
917
918 if (ctx == NULL || ctx->zout == 0) {
919 *buf = '-';
920 return buf + 1;
921 }
922
923 zint = (ngx_uint_t) (ctx->zin / ctx->zout);
924 zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100);
925
926 if ((ctx->zin * 1000 / ctx->zout) % 10 > 4) {
927
928 /* the rounding, e.g., 2.125 to 2.13 */
929
930 zfrac++;
931
932 if (zfrac > 99) {
933 zint++;
934 zfrac = 0;
935 }
936 }
937
938 return ngx_sprintf(buf, "%ui.%02ui", zint, zfrac);
939 }
940
941
942898 static void
943899 ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx)
944900 {
960916 static ngx_int_t
961917 ngx_http_gzip_add_variables(ngx_conf_t *cf)
962918 {
963 ngx_http_variable_t *var;
964 ngx_http_log_op_name_t *op;
919 ngx_http_variable_t *var;
965920
966921 var = ngx_http_add_variable(cf, &ngx_http_gzip_ratio, NGX_HTTP_VAR_NOHASH);
967922 if (var == NULL) {
969924 }
970925
971926 var->get_handler = ngx_http_gzip_ratio_variable;
972
973 for (op = ngx_http_gzip_log_fmt_ops; op->name.len; op++) { /* void */ }
974 op->run = NULL;
975
976 for (op = ngx_http_log_fmt_ops; op->run; op++) {
977 if (op->name.len == 0) {
978 op = (ngx_http_log_op_name_t *) op->run;
979 }
980 }
981
982 op->run = (ngx_http_log_op_run_pt) ngx_http_gzip_log_fmt_ops;
983927
984928 return NGX_OK;
985929 }
88 #include <ngx_http.h>
99 #include <nginx.h>
1010
11
12 typedef struct ngx_http_log_op_s ngx_http_log_op_t;
13
14 typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
15 ngx_http_log_op_t *op);
16
17 typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
18 uintptr_t data);
19
20
21 struct ngx_http_log_op_s {
22 size_t len;
23 ngx_http_log_op_getlen_pt getlen;
24 ngx_http_log_op_run_pt run;
25 uintptr_t data;
26 };
1127
1228
1329 typedef struct {
4258 } ngx_http_log_var_t;
4359
4460
45 static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf,
46 ngx_http_log_op_t *op);
4761 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
4862 ngx_http_log_op_t *op);
4963 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
6377 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
6478 ngx_http_log_op_t *op);
6579
66 static size_t ngx_http_log_request_getlen(ngx_http_request_t *r,
67 uintptr_t data);
68 static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf,
69 ngx_http_log_op_t *op);
70
71 static ngx_int_t ngx_http_log_header_in_compile(ngx_conf_t *cf,
72 ngx_http_log_op_t *op, ngx_str_t *value);
73 static size_t ngx_http_log_header_in_getlen(ngx_http_request_t *r,
74 uintptr_t data);
75 static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
76 ngx_http_log_op_t *op);
77 static size_t ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r,
78 uintptr_t data);
79 static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r,
80 u_char *buf, ngx_http_log_op_t *op);
81
82 static ngx_int_t ngx_http_log_header_out_compile(ngx_conf_t *cf,
83 ngx_http_log_op_t *op, ngx_str_t *value);
84 static size_t ngx_http_log_header_out_getlen(ngx_http_request_t *r,
85 uintptr_t data);
86 static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
87 ngx_http_log_op_t *op);
88 static size_t ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r,
89 uintptr_t data);
90 static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r,
91 u_char *buf, ngx_http_log_op_t *op);
92
93 static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
94 u_char *buf, ngx_http_log_op_t *op);
95 static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r,
96 u_char *buf, ngx_http_log_op_t *op);
97
98 static ngx_table_elt_t *ngx_http_log_unknown_header(ngx_list_t *headers,
99 ngx_str_t *value);
100
10180 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
10281 ngx_http_log_op_t *op, ngx_str_t *value);
10382 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
10685 ngx_http_log_op_t *op);
10786
10887
109 static ngx_int_t ngx_http_log_set_formats(ngx_conf_t *cf);
11088 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
11189 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
11290 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
142120
143121
144122 static ngx_http_module_t ngx_http_log_module_ctx = {
145 ngx_http_log_set_formats, /* preconfiguration */
123 NULL, /* preconfiguration */
146124 ngx_http_log_init, /* postconfiguration */
147125
148126 ngx_http_log_create_main_conf, /* create main configuration */
201179 };
202180
203181
204 ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {
205 { ngx_string("addr"), INET_ADDRSTRLEN - 1, NULL, NULL, ngx_http_log_addr },
206 { ngx_string("conn"), NGX_ATOMIC_T_LEN, NULL, NULL,
207 ngx_http_log_connection },
208 { ngx_string("pipe"), 1, NULL, NULL, ngx_http_log_pipe },
209 { ngx_string("time"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
210 NULL, NULL, ngx_http_log_time },
211 { ngx_string("msec"), NGX_TIME_T_LEN + 4, NULL, NULL, ngx_http_log_msec },
212 { ngx_string("request_time"), NGX_TIME_T_LEN, NULL, NULL,
213 ngx_http_log_request_time },
214 { ngx_string("status"), 3, NULL, NULL, ngx_http_log_status },
215 { ngx_string("length"), NGX_OFF_T_LEN,
216 NULL, NULL, ngx_http_log_bytes_sent },
217 { ngx_string("apache_length"), NGX_OFF_T_LEN,
218 NULL, NULL, ngx_http_log_body_bytes_sent },
219 { ngx_string("request_length"), NGX_SIZE_T_LEN,
220 NULL, NULL, ngx_http_log_request_length },
221
222 { ngx_string("request"), 0, NULL,
223 ngx_http_log_request_getlen,
224 ngx_http_log_request },
225
226 { ngx_string("i"), 0, ngx_http_log_header_in_compile, NULL,
227 ngx_http_log_header_in },
228 { ngx_string("o"), 0, ngx_http_log_header_out_compile, NULL,
229 ngx_http_log_header_out },
230 { ngx_string("v"), 0, ngx_http_log_variable_compile, NULL,
231 ngx_http_log_variable },
232
233 { ngx_null_string, 0, NULL, NULL, NULL }
234 };
235
236
237182 ngx_int_t
238183 ngx_http_log_handler(ngx_http_request_t *r)
239184 {
366311
367312
368313 static u_char *
369 ngx_http_log_addr(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
370 {
371 return ngx_cpymem(buf, r->connection->addr_text.data,
372 r->connection->addr_text.len);
373 }
374
375
376 static u_char *
377314 ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
378315 ngx_http_log_op_t *op)
379316 {
425362 }
426363
427364
428 static size_t
429 ngx_http_log_request_getlen(ngx_http_request_t *r, uintptr_t data)
430 {
431 return r->request_line.len;
432 }
433
434
435 static u_char *
436 ngx_http_log_request(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
437 {
438 return ngx_cpymem(buf, r->request_line.data, r->request_line.len);
439 }
440
441
442365 static u_char *
443366 ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
444367 {
478401 ngx_http_log_op_t *op)
479402 {
480403 return ngx_sprintf(buf, "%O", r->request_length);
481 }
482
483
484 static ngx_int_t
485 ngx_http_log_header_in_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
486 ngx_str_t *value)
487 {
488 ngx_uint_t i;
489
490 op->len = 0;
491
492 for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) {
493
494 if (ngx_http_headers_in[i].name.len != value->len) {
495 continue;
496 }
497
498 /* STUB: "Cookie" speacial handling */
499 if (ngx_http_headers_in[i].offset == 0) {
500 continue;
501 }
502
503 if (ngx_strncasecmp(ngx_http_headers_in[i].name.data, value->data,
504 value->len) == 0)
505 {
506 op->getlen = ngx_http_log_header_in_getlen;
507 op->run = ngx_http_log_header_in;
508 op->data = ngx_http_headers_in[i].offset;
509
510 return NGX_OK;
511 }
512 }
513
514 op->getlen = ngx_http_log_unknown_header_in_getlen;
515 op->run = ngx_http_log_unknown_header_in;
516 op->data = (uintptr_t) value;
517
518 return NGX_OK;
519 }
520
521
522 static size_t
523 ngx_http_log_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
524 {
525 ngx_table_elt_t *h;
526
527 h = *(ngx_table_elt_t **) ((char *) &r->headers_in + data);
528
529 if (h) {
530 return h->value.len;
531 }
532
533 return 1;
534 }
535
536
537 static u_char *
538 ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf,
539 ngx_http_log_op_t *op)
540 {
541 ngx_table_elt_t *h;
542
543 h = *(ngx_table_elt_t **) ((char *) &r->headers_in + op->data);
544
545 if (h) {
546 return ngx_cpymem(buf, h->value.data, h->value.len);
547 }
548
549 *buf = '-';
550
551 return buf + 1;
552 }
553
554
555 static size_t
556 ngx_http_log_unknown_header_in_getlen(ngx_http_request_t *r, uintptr_t data)
557 {
558 ngx_table_elt_t *h;
559
560 h = ngx_http_log_unknown_header(&r->headers_in.headers, (ngx_str_t *) data);
561
562 if (h) {
563 return h->value.len;
564 }
565
566 return 1;
567 }
568
569
570 static u_char *
571 ngx_http_log_unknown_header_in(ngx_http_request_t *r, u_char *buf,
572 ngx_http_log_op_t *op)
573 {
574 ngx_table_elt_t *h;
575
576 h = ngx_http_log_unknown_header(&r->headers_in.headers,
577 (ngx_str_t *) op->data);
578
579 if (h) {
580 return ngx_cpymem(buf, h->value.data, h->value.len);
581 }
582
583 *buf = '-';
584
585 return buf + 1;
586 }
587
588
589 static ngx_int_t
590 ngx_http_log_header_out_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
591 ngx_str_t *value)
592 {
593 ngx_uint_t i;
594
595 op->len = 0;
596
597 for (i = 0; ngx_http_headers_out[i].name.len != 0; i++) {
598
599 if (ngx_http_headers_out[i].name.len != value->len) {
600 continue;
601 }
602
603 if (ngx_strncasecmp(ngx_http_headers_out[i].name.data, value->data,
604 value->len) == 0)
605 {
606 op->getlen = ngx_http_log_header_out_getlen;
607 op->run = ngx_http_log_header_out;
608 op->data = ngx_http_headers_out[i].offset;
609
610 return NGX_OK;
611 }
612 }
613
614 if (value->len == sizeof("Connection") - 1
615 && ngx_strncasecmp(value->data, "Connection", value->len) == 0)
616 {
617 op->len = sizeof("keep-alive") - 1;
618 op->getlen = NULL;
619 op->run = ngx_http_log_connection_header_out;
620 op->data = 0;
621 return NGX_OK;
622 }
623
624 if (value->len == sizeof("Transfer-Encoding") - 1
625 && ngx_strncasecmp(value->data, "Transfer-Encoding", value->len) == 0)
626 {
627 op->len = sizeof("chunked") - 1;
628 op->getlen = NULL;
629 op->run = ngx_http_log_transfer_encoding_header_out;
630 op->data = 0;
631 return NGX_OK;
632 }
633
634 op->getlen = ngx_http_log_unknown_header_out_getlen;
635 op->run = ngx_http_log_unknown_header_out;
636 op->data = (uintptr_t) value;
637
638 return NGX_OK;
639 }
640
641
642 static size_t
643 ngx_http_log_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
644 {
645 ngx_table_elt_t *h;
646
647 h = *(ngx_table_elt_t **) ((char *) &r->headers_out + data);
648
649 if (h) {
650 return h->value.len;
651 }
652
653 /*
654 * No header pointer was found.
655 * However, some headers: "Date", "Server", "Content-Length",
656 * and "Last-Modified" have a special handling in the header filter,
657 * but we do not set up their pointers in the filter,
658 * because they are too seldom needed to be logged.
659 */
660
661 if (data == offsetof(ngx_http_headers_out_t, date)) {
662 return ngx_cached_http_time.len;
663 }
664
665 if (data == offsetof(ngx_http_headers_out_t, server)) {
666 return (sizeof(NGINX_VER) - 1);
667 }
668
669 if (data == offsetof(ngx_http_headers_out_t, content_length)) {
670 if (r->headers_out.content_length_n == -1) {
671 return 1;
672 }
673
674 return NGX_OFF_T_LEN;
675 }
676
677 if (data == offsetof(ngx_http_headers_out_t, last_modified)) {
678 if (r->headers_out.last_modified_time == -1) {
679 return 1;
680 }
681
682 return sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
683 }
684
685 return 1;
686 }
687
688
689 static u_char *
690 ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf,
691 ngx_http_log_op_t *op)
692 {
693 ngx_table_elt_t *h;
694
695 h = *(ngx_table_elt_t **) ((char *) &r->headers_out + op->data);
696
697 if (h) {
698 return ngx_cpymem(buf, h->value.data, h->value.len);
699 }
700
701 /*
702 * No header pointer was found.
703 * However, some headers: "Date", "Server", "Content-Length",
704 * and "Last-Modified" have a special handling in the header filter,
705 * but we do not set up their pointers in the filter,
706 * because they are too seldom needed to be logged.
707 */
708
709 if (op->data == offsetof(ngx_http_headers_out_t, date)) {
710 return ngx_cpymem(buf, ngx_cached_http_time.data,
711 ngx_cached_http_time.len);
712 }
713
714 if (op->data == offsetof(ngx_http_headers_out_t, server)) {
715 return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1);
716 }
717
718 if (op->data == offsetof(ngx_http_headers_out_t, content_length)) {
719 if (r->headers_out.content_length_n == -1) {
720 *buf = '-';
721
722 return buf + 1;
723 }
724
725 return ngx_sprintf(buf, "%O", r->headers_out.content_length_n);
726 }
727
728 if (op->data == offsetof(ngx_http_headers_out_t, last_modified)) {
729 if (r->headers_out.last_modified_time == -1) {
730 *buf = '-';
731
732 return buf + 1;
733 }
734
735 return ngx_http_time(buf, r->headers_out.last_modified_time);
736 }
737
738 *buf = '-';
739
740 return buf + 1;
741 }
742
743
744 static size_t
745 ngx_http_log_unknown_header_out_getlen(ngx_http_request_t *r, uintptr_t data)
746 {
747 ngx_table_elt_t *h;
748
749 h = ngx_http_log_unknown_header(&r->headers_out.headers,
750 (ngx_str_t *) data);
751
752 if (h) {
753 return h->value.len;
754 }
755
756 return 1;
757 }
758
759
760 static u_char *
761 ngx_http_log_unknown_header_out(ngx_http_request_t *r, u_char *buf,
762 ngx_http_log_op_t *op)
763 {
764 ngx_table_elt_t *h;
765
766 h = ngx_http_log_unknown_header(&r->headers_out.headers,
767 (ngx_str_t *) op->data);
768
769 if (h) {
770 return ngx_cpymem(buf, h->value.data, h->value.len);
771 }
772
773 *buf = '-';
774
775 return buf + 1;
776 }
777
778
779 static ngx_table_elt_t *
780 ngx_http_log_unknown_header(ngx_list_t *headers, ngx_str_t *value)
781 {
782 ngx_uint_t i;
783 ngx_list_part_t *part;
784 ngx_table_elt_t *h;
785
786 part = &headers->part;
787 h = part->elts;
788
789 for (i = 0; /* void */; i++) {
790
791 if (i >= part->nelts) {
792 if (part->next == NULL) {
793 break;
794 }
795
796 part = part->next;
797 h = part->elts;
798 i = 0;
799 }
800
801 if (h[i].key.len != value->len) {
802 continue;
803 }
804
805 if (ngx_strncasecmp(h[i].key.data, value->data, value->len) == 0) {
806 return &h[i];
807 }
808 }
809
810 return NULL;
811 }
812
813
814 static u_char *
815 ngx_http_log_connection_header_out(ngx_http_request_t *r, u_char *buf,
816 ngx_http_log_op_t *op)
817 {
818 if (r->keepalive) {
819 return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1);
820
821 } else {
822 return ngx_cpymem(buf, "close", sizeof("close") - 1);
823 }
824 }
825
826
827 static u_char *
828 ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r, u_char *buf,
829 ngx_http_log_op_t *op)
830 {
831 if (r->chunked) {
832 return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1);
833 }
834
835 *buf = '-';
836
837 return buf + 1;
838404 }
839405
840406
886452 }
887453
888454 return ngx_cpymem(buf, value->data, value->len);
889 }
890
891
892 static ngx_int_t
893 ngx_http_log_set_formats(ngx_conf_t *cf)
894 {
895 ngx_http_log_op_name_t *op;
896
897 for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ }
898 op->run = NULL;
899
900 return NGX_OK;
901455 }
902456
903457
1151705 ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *ops,
1152706 ngx_array_t *args, ngx_uint_t s)
1153707 {
1154 u_char *data, *p, *fname, *arg_data, ch;
1155 size_t i, len, fname_len, arg_len;
1156 ngx_str_t *value, var, *a;
1157 ngx_uint_t bracket;
1158 ngx_http_log_op_t *op;
1159 ngx_http_log_var_t *v;
1160 ngx_http_log_op_name_t *name;
1161 static ngx_uint_t warn;
708 u_char *data, *p, ch;
709 size_t i, len;
710 ngx_str_t *value, var;
711 ngx_uint_t bracket;
712 ngx_http_log_op_t *op;
713 ngx_http_log_var_t *v;
1162714
1163715 value = args->elts;
1164 arg_data = NULL;
1165716
1166717 for ( /* void */ ; s < args->nelts; s++) {
718
719 for (i = 0; i < value[s].len; i++) {
720 if (value[s].data[i] != '%') {
721 continue;
722 }
723
724 ch = value[s].data[i + 1];
725
726 if ((ch >= 'A' && ch <= 'Z')
727 || (ch >= 'a' && ch <= 'z')
728 || ch == '{')
729 {
730 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
731 "the parameters in the \"%%name\" form are not supported, "
732 "use the \"$variable\" instead");
733
734 return NGX_CONF_ERROR;
735 }
736 }
1167737
1168738 i = 0;
1169739
1176746
1177747 data = &value[s].data[i];
1178748
1179 if (value[s].data[i] == '%') {
1180 i++;
1181
1182 if (i == value[s].len) {
1183 goto invalid;
1184 }
1185
1186 if (value[s].data[i] == '{') {
1187 i++;
1188
1189 arg_data = &value[s].data[i];
1190
1191 while (i < value[s].len && value[s].data[i] != '}') {
1192 i++;
1193 }
1194
1195 arg_len = &value[s].data[i] - arg_data;
1196
1197 if (i == value[s].len || arg_len == 0) {
1198 goto invalid;
1199 }
1200
1201 i++;
1202
1203 } else {
1204 arg_len = 0;
1205 }
1206
1207 if (warn == 0) {
1208 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1209 "the parameters in the \"%%name\" form are deprecated, "
1210 "use the \"$variable\" instead");
1211 warn = 1;
1212 }
1213
1214 fname = &value[s].data[i];
1215
1216 while (i < value[s].len
1217 && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z')
1218 || value[s].data[i] == '_'))
1219 {
1220 i++;
1221 }
1222
1223 fname_len = &value[s].data[i] - fname;
1224
1225 if (fname_len == 0) {
1226 goto invalid;
1227 }
1228
1229 for (name = ngx_http_log_fmt_ops; name->run; name++) {
1230 if (name->name.len == 0) {
1231 name = (ngx_http_log_op_name_t *) name->run;
1232 }
1233
1234 if (name->name.len == fname_len
1235 && ngx_strncmp(name->name.data, fname, fname_len) == 0)
1236 {
1237 if (name->compile == NULL) {
1238 if (arg_len) {
1239 fname[fname_len] = '\0';
1240 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1241 "\"%s\" must not have argument",
1242 data);
1243 return NGX_CONF_ERROR;
1244 }
1245
1246 op->len = name->len;
1247 op->getlen = name->getlen;
1248 op->run = name->run;
1249 op->data = 0;
1250
1251 break;
1252 }
1253
1254 if (arg_len == 0) {
1255 fname[fname_len] = '\0';
1256 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1257 "\"%s\" requires argument",
1258 data);
1259 return NGX_CONF_ERROR;
1260 }
1261
1262 a = ngx_palloc(cf->pool, sizeof(ngx_str_t));
1263 if (a == NULL) {
1264 return NGX_CONF_ERROR;
1265 }
1266
1267 a->len = arg_len;
1268 a->data = arg_data;
1269
1270 if (name->compile(cf, op, a) == NGX_ERROR) {
1271 return NGX_CONF_ERROR;
1272 }
1273
1274 break;
1275 }
1276 }
1277
1278 if (name->name.len == 0) {
1279 goto invalid;
1280 }
1281
1282 continue;
1283
1284 } else if (value[s].data[i] == '$') {
749 if (value[s].data[i] == '$') {
1285750
1286751 if (++i == value[s].len) {
1287752 goto invalid;
1363828
1364829 i++;
1365830
1366 while (i < value[s].len
1367 && value[s].data[i] != '$'
1368 && value[s].data[i] != '%')
1369 {
831 while (i < value[s].len && value[s].data[i] != '$') {
1370832 i++;
1371833 }
1372834
+0
-48
src/http/modules/ngx_http_log_module.h less more
0
1 /*
2 * Copyright (C) Igor Sysoev
3 */
4
5
6 #ifndef _NGX_HTTP_LOG_MODULE_H_INCLUDED_
7 #define _NGX_HTTP_LOG_MODULE_H_INCLUDED_
8
9
10 #include <ngx_config.h>
11 #include <ngx_core.h>
12 #include <ngx_http.h>
13
14
15 typedef struct ngx_http_log_op_s ngx_http_log_op_t;
16
17 typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
18 ngx_http_log_op_t *op);
19
20 typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
21 uintptr_t data);
22
23 typedef ngx_int_t (*ngx_http_log_op_compile_pt) (ngx_conf_t *cf,
24 ngx_http_log_op_t *op, ngx_str_t *value);
25
26
27 struct ngx_http_log_op_s {
28 size_t len;
29 ngx_http_log_op_getlen_pt getlen;
30 ngx_http_log_op_run_pt run;
31 uintptr_t data;
32 };
33
34
35 typedef struct {
36 ngx_str_t name;
37 size_t len;
38 ngx_http_log_op_compile_pt compile;
39 ngx_http_log_op_getlen_pt getlen;
40 ngx_http_log_op_run_pt run;
41 } ngx_http_log_op_name_t;
42
43
44 extern ngx_http_log_op_name_t ngx_http_log_fmt_ops[];
45
46
47 #endif /* _NGX_HTTP_LOG_MODULE_H_INCLUDED_ */
4444 ngx_http_userid_ctx_t *ctx, ngx_http_userid_conf_t *conf);
4545 static ngx_int_t ngx_http_userid_set_uid(ngx_http_request_t *r,
4646 ngx_http_userid_ctx_t *ctx, ngx_http_userid_conf_t *conf);
47
48 static size_t ngx_http_userid_log_uid_got_getlen(ngx_http_request_t *r,
49 uintptr_t data);
50 static u_char *ngx_http_userid_log_uid_got(ngx_http_request_t *r, u_char *buf,
51 ngx_http_log_op_t *op);
52 static size_t ngx_http_userid_log_uid_set_getlen(ngx_http_request_t *r,
53 uintptr_t data);
54 static u_char *ngx_http_userid_log_uid_set(ngx_http_request_t *r, u_char *buf,
55 ngx_http_log_op_t *op);
5647
5748 static ngx_int_t ngx_http_userid_add_variables(ngx_conf_t *cf);
5849 static ngx_int_t ngx_http_userid_variable(ngx_http_request_t *r,
190181 };
191182
192183
193 static ngx_http_log_op_name_t ngx_http_userid_log_fmt_ops[] = {
194 { ngx_string("uid_got"), 0, NULL,
195 ngx_http_userid_log_uid_got_getlen,
196 ngx_http_userid_log_uid_got },
197 { ngx_string("uid_set"), 0, NULL,
198 ngx_http_userid_log_uid_set_getlen,
199 ngx_http_userid_log_uid_set },
200 { ngx_null_string, 0, NULL, NULL, NULL }
201 };
202
203
204184 static ngx_str_t ngx_http_userid_got = ngx_string("uid_got");
205185 static ngx_str_t ngx_http_userid_set = ngx_string("uid_set");
206186
461441 }
462442
463443
464 static size_t
465 ngx_http_userid_log_uid_got_getlen(ngx_http_request_t *r, uintptr_t data)
466 {
467 ngx_http_userid_ctx_t *ctx;
468 ngx_http_userid_conf_t *conf;
469
470 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);
471
472 if (ctx == NULL || ctx->uid_got[3] == 0) {
473 return 1;
474 }
475
476 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);
477
478 return conf->name.len + 1 + 32;
479 }
480
481
482 static u_char *
483 ngx_http_userid_log_uid_got(ngx_http_request_t *r, u_char *buf,
484 ngx_http_log_op_t *op)
485 {
486 ngx_http_userid_ctx_t *ctx;
487 ngx_http_userid_conf_t *conf;
488
489 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);
490
491 if (ctx == NULL || ctx->uid_got[3] == 0) {
492 *buf = '-';
493 return buf + 1;
494 }
495
496 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);
497
498 buf = ngx_copy(buf, conf->name.data, conf->name.len);
499
500 *buf++ = '=';
501
502 return ngx_sprintf(buf, "%08XD%08XD%08XD%08XD",
503 ctx->uid_got[0], ctx->uid_got[1],
504 ctx->uid_got[2], ctx->uid_got[3]);
505 }
506
507
508 static size_t
509 ngx_http_userid_log_uid_set_getlen(ngx_http_request_t *r, uintptr_t data)
510 {
511 ngx_http_userid_ctx_t *ctx;
512 ngx_http_userid_conf_t *conf;
513
514 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);
515
516 if (ctx == NULL || ctx->uid_set[3] == 0) {
517 return 1;
518 }
519
520 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);
521
522 return conf->name.len + 1 + 32;
523 }
524
525
526 static u_char *
527 ngx_http_userid_log_uid_set(ngx_http_request_t *r, u_char *buf,
528 ngx_http_log_op_t *op)
529 {
530 ngx_http_userid_ctx_t *ctx;
531 ngx_http_userid_conf_t *conf;
532
533 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);
534
535 if (ctx == NULL || ctx->uid_set[3] == 0) {
536 *buf = '-';
537 return buf + 1;
538 }
539
540 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);
541
542 buf = ngx_copy(buf, conf->name.data, conf->name.len);
543
544 *buf++ = '=';
545
546 return ngx_sprintf(buf, "%08XD%08XD%08XD%08XD",
547 ctx->uid_set[0], ctx->uid_set[1],
548 ctx->uid_set[2], ctx->uid_set[3]);
549 }
550
551
552444 static ngx_int_t
553445 ngx_http_userid_add_variables(ngx_conf_t *cf)
554446 {
555 ngx_http_variable_t *var;
556 ngx_http_log_op_name_t *op;
447 ngx_http_variable_t *var;
557448
558449 var = ngx_http_add_variable(cf, &ngx_http_userid_got, NGX_HTTP_VAR_NOHASH);
559450 if (var == NULL) {
570461
571462 var->get_handler = ngx_http_userid_variable;
572463 var->data = offsetof(ngx_http_userid_ctx_t, uid_set);
573
574
575 for (op = ngx_http_userid_log_fmt_ops; op->name.len; op++) { /* void */ }
576 op->run = NULL;
577
578 for (op = ngx_http_log_fmt_ops; op->run; op++) {
579 if (op->name.len == 0) {
580 op = (ngx_http_log_op_name_t *) op->run;
581 }
582 }
583
584 op->run = (ngx_http_log_op_run_pt) ngx_http_userid_log_fmt_ops;
585464
586465 return NGX_OK;
587466 }
3333 #include <ngx_http_upstream.h>
3434 #include <ngx_http_config.h>
3535 #include <ngx_http_busy_lock.h>
36 #include <ngx_http_log_module.h>
3736 #include <ngx_http_core_module.h>
3837 #include <ngx_http_script.h>
3938
6969 ngx_table_elt_t *h, ngx_uint_t offset);
7070 #endif
7171
72 static size_t ngx_http_upstream_log_status_getlen(ngx_http_request_t *r,
73 uintptr_t data);
74 static u_char *ngx_http_upstream_log_status(ngx_http_request_t *r,
75 u_char *buf, ngx_http_log_op_t *op);
76 static size_t ngx_http_upstream_log_response_time_getlen(ngx_http_request_t *r,
77 uintptr_t data);
78 static u_char *ngx_http_upstream_log_response_time(ngx_http_request_t *r,
79 u_char *buf, ngx_http_log_op_t *op);
80
8172 static ngx_int_t ngx_http_upstream_add_variables(ngx_conf_t *cf);
8273 static ngx_int_t ngx_http_upstream_status_variable(ngx_http_request_t *r,
8374 ngx_http_variable_value_t *v, uintptr_t data);
264255 };
265256
266257
267 static ngx_http_log_op_name_t ngx_http_upstream_log_fmt_ops[] = {
268 { ngx_string("upstream_status"), 0, NULL,
269 ngx_http_upstream_log_status_getlen,
270 ngx_http_upstream_log_status },
271 { ngx_string("upstream_response_time"), 0, NULL,
272 ngx_http_upstream_log_response_time_getlen,
273 ngx_http_upstream_log_response_time },
274 { ngx_null_string, 0, NULL, NULL, NULL }
275 };
276
277
278258 static ngx_http_variable_t ngx_http_upstream_vars[] = {
279259
280260 { ngx_string("upstream_status"), NULL,
24802460 #endif
24812461
24822462
2483 static size_t
2484 ngx_http_upstream_log_status_getlen(ngx_http_request_t *r, uintptr_t data)
2485 {
2486 if (r->upstream) {
2487 return r->upstream->states.nelts * (3 + 2);
2488 }
2489
2490 return 1;
2491 }
2492
2493
2494 static u_char *
2495 ngx_http_upstream_log_status(ngx_http_request_t *r, u_char *buf,
2496 ngx_http_log_op_t *op)
2497 {
2498 ngx_uint_t i;
2499 ngx_http_upstream_t *u;
2500 ngx_http_upstream_state_t *state;
2501
2502 u = r->upstream;
2503
2504 if (u == NULL || u->states.nelts == 0) {
2505 *buf = '-';
2506 return buf + 1;
2507 }
2508
2509 i = 0;
2510 state = u->states.elts;
2511
2512 for ( ;; ) {
2513 if (state[i].status == 0) {
2514 *buf++ = '-';
2515
2516 } else {
2517 buf = ngx_sprintf(buf, "%ui", state[i].status);
2518 }
2519
2520 if (++i == u->states.nelts) {
2521 return buf;
2522 }
2523
2524 *buf++ = ',';
2525 *buf++ = ' ';
2526 }
2527 }
2528
2529
2530 static size_t
2531 ngx_http_upstream_log_response_time_getlen(ngx_http_request_t *r,
2532 uintptr_t data)
2533 {
2534 if (r->upstream) {
2535 return r->upstream->states.nelts * (NGX_TIME_T_LEN + 4 + 2);
2536 }
2537
2538 return 1;
2539 }
2540
2541
2542 static u_char *
2543 ngx_http_upstream_log_response_time(ngx_http_request_t *r, u_char *buf,
2544 ngx_http_log_op_t *op)
2545 {
2546 ngx_uint_t i;
2547 ngx_http_upstream_t *u;
2548 ngx_http_upstream_state_t *state;
2549
2550 u = r->upstream;
2551
2552 if (u == NULL || u->states.nelts == 0) {
2553 *buf = '-';
2554 return buf + 1;
2555 }
2556
2557 i = 0;
2558 state = u->states.elts;
2559
2560 for ( ;; ) {
2561 if (state[i].status == 0) {
2562 *buf++ = '-';
2563
2564 } else {
2565 buf = ngx_sprintf(buf, "%d.%03d",
2566 state[i].response_time / 1000,
2567 state[i].response_time % 1000);
2568 }
2569
2570 if (++i == u->states.nelts) {
2571 return buf;
2572 }
2573
2574 *buf++ = ',';
2575 *buf++ = ' ';
2576 }
2577 }
2578
2579
25802463 static ngx_int_t
25812464 ngx_http_upstream_add_variables(ngx_conf_t *cf)
25822465 {
2583 ngx_http_variable_t *var, *v;
2584 ngx_http_log_op_name_t *op;
2466 ngx_http_variable_t *var, *v;
25852467
25862468 for (v = ngx_http_upstream_vars; v->name.len; v++) {
25872469 var = ngx_http_add_variable(cf, &v->name, v->flags);
25922474 var->get_handler = v->get_handler;
25932475 var->data = v->data;
25942476 }
2595
2596 for (op = ngx_http_upstream_log_fmt_ops; op->name.len; op++) { /* void */ }
2597 op->run = NULL;
2598
2599 for (op = ngx_http_log_fmt_ops; op->run; op++) {
2600 if (op->name.len == 0) {
2601 op = (ngx_http_log_op_name_t *) op->run;
2602 }
2603 }
2604
2605 op->run = (ngx_http_log_op_run_pt) ngx_http_upstream_log_fmt_ops;
26062477
26072478 return NGX_OK;
26082479 }