Klaus Demo nginx / 7647372
Access log: support for disabling escaping (ticket #1450). Based on patches by Johannes Baiter <johannes.baiter@bsb-muenchen.de> and Calin Don. Vladimir Homutov 2 years ago
2 changed file(s) with 122 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
8787 size_t len;
8888 ngx_http_log_op_run_pt run;
8989 } ngx_http_log_var_t;
90
91
92 #define NGX_HTTP_LOG_ESCAPE_DEFAULT 0
93 #define NGX_HTTP_LOG_ESCAPE_JSON 1
94 #define NGX_HTTP_LOG_ESCAPE_NONE 2
9095
9196
9297 static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
125130 ngx_http_log_op_t *op);
126131
127132 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
128 ngx_http_log_op_t *op, ngx_str_t *value, ngx_uint_t json);
133 ngx_http_log_op_t *op, ngx_str_t *value, ngx_uint_t escape);
129134 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
130135 uintptr_t data);
131136 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
135140 uintptr_t data);
136141 static u_char *ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,
137142 ngx_http_log_op_t *op);
143 static size_t ngx_http_log_unescaped_variable_getlen(ngx_http_request_t *r,
144 uintptr_t data);
145 static u_char *ngx_http_log_unescaped_variable(ngx_http_request_t *r,
146 u_char *buf, ngx_http_log_op_t *op);
138147
139148
140149 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
904913
905914 static ngx_int_t
906915 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
907 ngx_str_t *value, ngx_uint_t json)
916 ngx_str_t *value, ngx_uint_t escape)
908917 {
909918 ngx_int_t index;
910919
915924
916925 op->len = 0;
917926
918 if (json) {
927 switch (escape) {
928 case NGX_HTTP_LOG_ESCAPE_JSON:
919929 op->getlen = ngx_http_log_json_variable_getlen;
920930 op->run = ngx_http_log_json_variable;
921
922 } else {
931 break;
932
933 case NGX_HTTP_LOG_ESCAPE_NONE:
934 op->getlen = ngx_http_log_unescaped_variable_getlen;
935 op->run = ngx_http_log_unescaped_variable;
936 break;
937
938 default: /* NGX_HTTP_LOG_ESCAPE_DEFAULT */
923939 op->getlen = ngx_http_log_variable_getlen;
924940 op->run = ngx_http_log_variable;
925941 }
10691085 } else {
10701086 return (u_char *) ngx_escape_json(buf, value->data, value->len);
10711087 }
1088 }
1089
1090
1091 static size_t
1092 ngx_http_log_unescaped_variable_getlen(ngx_http_request_t *r, uintptr_t data)
1093 {
1094 ngx_http_variable_value_t *value;
1095
1096 value = ngx_http_get_indexed_variable(r, data);
1097
1098 if (value == NULL || value->not_found) {
1099 return 0;
1100 }
1101
1102 value->escape = 0;
1103
1104 return value->len;
1105 }
1106
1107
1108 static u_char *
1109 ngx_http_log_unescaped_variable(ngx_http_request_t *r, u_char *buf,
1110 ngx_http_log_op_t *op)
1111 {
1112 ngx_http_variable_value_t *value;
1113
1114 value = ngx_http_get_indexed_variable(r, op->data);
1115
1116 if (value == NULL || value->not_found) {
1117 return buf;
1118 }
1119
1120 return ngx_cpymem(buf, value->data, value->len);
10721121 }
10731122
10741123
15351584 size_t i, len;
15361585 ngx_str_t *value, var;
15371586 ngx_int_t *flush;
1538 ngx_uint_t bracket, json;
1587 ngx_uint_t bracket, escape;
15391588 ngx_http_log_op_t *op;
15401589 ngx_http_log_var_t *v;
15411590
1542 json = 0;
1591 escape = NGX_HTTP_LOG_ESCAPE_DEFAULT;
15431592 value = args->elts;
15441593
15451594 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
15461595 data = value[s].data + 7;
15471596
15481597 if (ngx_strcmp(data, "json") == 0) {
1549 json = 1;
1598 escape = NGX_HTTP_LOG_ESCAPE_JSON;
1599
1600 } else if (ngx_strcmp(data, "none") == 0) {
1601 escape = NGX_HTTP_LOG_ESCAPE_NONE;
15501602
15511603 } else if (ngx_strcmp(data, "default") != 0) {
15521604 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
16351687 }
16361688 }
16371689
1638 if (ngx_http_log_variable_compile(cf, op, &var, json)
1690 if (ngx_http_log_variable_compile(cf, op, &var, escape)
16391691 != NGX_OK)
16401692 {
16411693 return NGX_CONF_ERROR;
8888 } ngx_stream_log_var_t;
8989
9090
91 #define NGX_STREAM_LOG_ESCAPE_DEFAULT 0
92 #define NGX_STREAM_LOG_ESCAPE_JSON 1
93 #define NGX_STREAM_LOG_ESCAPE_NONE 2
94
95
9196 static void ngx_stream_log_write(ngx_stream_session_t *s, ngx_stream_log_t *log,
9297 u_char *buf, size_t len);
9398 static ssize_t ngx_stream_log_script_write(ngx_stream_session_t *s,
105110 static void ngx_stream_log_flush_handler(ngx_event_t *ev);
106111
107112 static ngx_int_t ngx_stream_log_variable_compile(ngx_conf_t *cf,
108 ngx_stream_log_op_t *op, ngx_str_t *value, ngx_uint_t json);
113 ngx_stream_log_op_t *op, ngx_str_t *value, ngx_uint_t escape);
109114 static size_t ngx_stream_log_variable_getlen(ngx_stream_session_t *s,
110115 uintptr_t data);
111116 static u_char *ngx_stream_log_variable(ngx_stream_session_t *s, u_char *buf,
114119 static size_t ngx_stream_log_json_variable_getlen(ngx_stream_session_t *s,
115120 uintptr_t data);
116121 static u_char *ngx_stream_log_json_variable(ngx_stream_session_t *s,
122 u_char *buf, ngx_stream_log_op_t *op);
123 static size_t ngx_stream_log_unescaped_variable_getlen(ngx_stream_session_t *s,
124 uintptr_t data);
125 static u_char *ngx_stream_log_unescaped_variable(ngx_stream_session_t *s,
117126 u_char *buf, ngx_stream_log_op_t *op);
118127
119128
681690
682691 static ngx_int_t
683692 ngx_stream_log_variable_compile(ngx_conf_t *cf, ngx_stream_log_op_t *op,
684 ngx_str_t *value, ngx_uint_t json)
693 ngx_str_t *value, ngx_uint_t escape)
685694 {
686695 ngx_int_t index;
687696
692701
693702 op->len = 0;
694703
695 if (json) {
704 switch (escape) {
705 case NGX_STREAM_LOG_ESCAPE_JSON:
696706 op->getlen = ngx_stream_log_json_variable_getlen;
697707 op->run = ngx_stream_log_json_variable;
698
699 } else {
708 break;
709
710 case NGX_STREAM_LOG_ESCAPE_NONE:
711 op->getlen = ngx_stream_log_unescaped_variable_getlen;
712 op->run = ngx_stream_log_unescaped_variable;
713 break;
714
715 default: /* NGX_STREAM_LOG_ESCAPE_DEFAULT */
700716 op->getlen = ngx_stream_log_variable_getlen;
701717 op->run = ngx_stream_log_variable;
702718 }
847863 } else {
848864 return (u_char *) ngx_escape_json(buf, value->data, value->len);
849865 }
866 }
867
868
869 static size_t
870 ngx_stream_log_unescaped_variable_getlen(ngx_stream_session_t *s, uintptr_t data)
871 {
872 ngx_stream_variable_value_t *value;
873
874 value = ngx_stream_get_indexed_variable(s, data);
875
876 if (value == NULL || value->not_found) {
877 return 0;
878 }
879
880 value->escape = 0;
881
882 return value->len;
883 }
884
885
886 static u_char *
887 ngx_stream_log_unescaped_variable(ngx_stream_session_t *s, u_char *buf,
888 ngx_stream_log_op_t *op)
889 {
890 ngx_stream_variable_value_t *value;
891
892 value = ngx_stream_get_indexed_variable(s, op->data);
893
894 if (value == NULL || value->not_found) {
895 return buf;
896 }
897
898 return ngx_cpymem(buf, value->data, value->len);
850899 }
851900
852901
12641313 size_t i, len;
12651314 ngx_str_t *value, var;
12661315 ngx_int_t *flush;
1267 ngx_uint_t bracket, json;
1316 ngx_uint_t bracket, escape;
12681317 ngx_stream_log_op_t *op;
12691318
1270 json = 0;
1319 escape = NGX_STREAM_LOG_ESCAPE_DEFAULT;
12711320 value = args->elts;
12721321
12731322 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
12741323 data = value[s].data + 7;
12751324
12761325 if (ngx_strcmp(data, "json") == 0) {
1277 json = 1;
1326 escape = NGX_STREAM_LOG_ESCAPE_JSON;
1327
1328 } else if (ngx_strcmp(data, "none") == 0) {
1329 escape = NGX_STREAM_LOG_ESCAPE_NONE;
12781330
12791331 } else if (ngx_strcmp(data, "default") != 0) {
12801332 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
13491401 goto invalid;
13501402 }
13511403
1352 if (ngx_stream_log_variable_compile(cf, op, &var, json)
1404 if (ngx_stream_log_variable_compile(cf, op, &var, escape)
13531405 != NGX_OK)
13541406 {
13551407 return NGX_CONF_ERROR;