comparison src/stream/ngx_stream_log_module.c @ 7223:265c29b0b8b8

Access log: support for disabling escaping (ticket #1450). Based on patches by Johannes Baiter <johannes.baiter@bsb-muenchen.de> and Calin Don.
author Vladimir Homutov <vl@nginx.com>
date Thu, 01 Mar 2018 11:42:55 +0300
parents 3fb9b5eb75c0
children 1ab290cf5267
comparison
equal deleted inserted replaced
7222:81fae70d6cb8 7223:265c29b0b8b8
87 size_t len; 87 size_t len;
88 ngx_stream_log_op_run_pt run; 88 ngx_stream_log_op_run_pt run;
89 } ngx_stream_log_var_t; 89 } ngx_stream_log_var_t;
90 90
91 91
92 #define NGX_STREAM_LOG_ESCAPE_DEFAULT 0
93 #define NGX_STREAM_LOG_ESCAPE_JSON 1
94 #define NGX_STREAM_LOG_ESCAPE_NONE 2
95
96
92 static void ngx_stream_log_write(ngx_stream_session_t *s, ngx_stream_log_t *log, 97 static void ngx_stream_log_write(ngx_stream_session_t *s, ngx_stream_log_t *log,
93 u_char *buf, size_t len); 98 u_char *buf, size_t len);
94 static ssize_t ngx_stream_log_script_write(ngx_stream_session_t *s, 99 static ssize_t ngx_stream_log_script_write(ngx_stream_session_t *s,
95 ngx_stream_log_script_t *script, u_char **name, u_char *buf, size_t len); 100 ngx_stream_log_script_t *script, u_char **name, u_char *buf, size_t len);
96 101
104 109
105 static void ngx_stream_log_flush(ngx_open_file_t *file, ngx_log_t *log); 110 static void ngx_stream_log_flush(ngx_open_file_t *file, ngx_log_t *log);
106 static void ngx_stream_log_flush_handler(ngx_event_t *ev); 111 static void ngx_stream_log_flush_handler(ngx_event_t *ev);
107 112
108 static ngx_int_t ngx_stream_log_variable_compile(ngx_conf_t *cf, 113 static ngx_int_t ngx_stream_log_variable_compile(ngx_conf_t *cf,
109 ngx_stream_log_op_t *op, ngx_str_t *value, ngx_uint_t json); 114 ngx_stream_log_op_t *op, ngx_str_t *value, ngx_uint_t escape);
110 static size_t ngx_stream_log_variable_getlen(ngx_stream_session_t *s, 115 static size_t ngx_stream_log_variable_getlen(ngx_stream_session_t *s,
111 uintptr_t data); 116 uintptr_t data);
112 static u_char *ngx_stream_log_variable(ngx_stream_session_t *s, u_char *buf, 117 static u_char *ngx_stream_log_variable(ngx_stream_session_t *s, u_char *buf,
113 ngx_stream_log_op_t *op); 118 ngx_stream_log_op_t *op);
114 static uintptr_t ngx_stream_log_escape(u_char *dst, u_char *src, size_t size); 119 static uintptr_t ngx_stream_log_escape(u_char *dst, u_char *src, size_t size);
115 static size_t ngx_stream_log_json_variable_getlen(ngx_stream_session_t *s, 120 static size_t ngx_stream_log_json_variable_getlen(ngx_stream_session_t *s,
116 uintptr_t data); 121 uintptr_t data);
117 static u_char *ngx_stream_log_json_variable(ngx_stream_session_t *s, 122 static u_char *ngx_stream_log_json_variable(ngx_stream_session_t *s,
123 u_char *buf, ngx_stream_log_op_t *op);
124 static size_t ngx_stream_log_unescaped_variable_getlen(ngx_stream_session_t *s,
125 uintptr_t data);
126 static u_char *ngx_stream_log_unescaped_variable(ngx_stream_session_t *s,
118 u_char *buf, ngx_stream_log_op_t *op); 127 u_char *buf, ngx_stream_log_op_t *op);
119 128
120 129
121 static void *ngx_stream_log_create_main_conf(ngx_conf_t *cf); 130 static void *ngx_stream_log_create_main_conf(ngx_conf_t *cf);
122 static void *ngx_stream_log_create_srv_conf(ngx_conf_t *cf); 131 static void *ngx_stream_log_create_srv_conf(ngx_conf_t *cf);
680 } 689 }
681 690
682 691
683 static ngx_int_t 692 static ngx_int_t
684 ngx_stream_log_variable_compile(ngx_conf_t *cf, ngx_stream_log_op_t *op, 693 ngx_stream_log_variable_compile(ngx_conf_t *cf, ngx_stream_log_op_t *op,
685 ngx_str_t *value, ngx_uint_t json) 694 ngx_str_t *value, ngx_uint_t escape)
686 { 695 {
687 ngx_int_t index; 696 ngx_int_t index;
688 697
689 index = ngx_stream_get_variable_index(cf, value); 698 index = ngx_stream_get_variable_index(cf, value);
690 if (index == NGX_ERROR) { 699 if (index == NGX_ERROR) {
691 return NGX_ERROR; 700 return NGX_ERROR;
692 } 701 }
693 702
694 op->len = 0; 703 op->len = 0;
695 704
696 if (json) { 705 switch (escape) {
706 case NGX_STREAM_LOG_ESCAPE_JSON:
697 op->getlen = ngx_stream_log_json_variable_getlen; 707 op->getlen = ngx_stream_log_json_variable_getlen;
698 op->run = ngx_stream_log_json_variable; 708 op->run = ngx_stream_log_json_variable;
699 709 break;
700 } else { 710
711 case NGX_STREAM_LOG_ESCAPE_NONE:
712 op->getlen = ngx_stream_log_unescaped_variable_getlen;
713 op->run = ngx_stream_log_unescaped_variable;
714 break;
715
716 default: /* NGX_STREAM_LOG_ESCAPE_DEFAULT */
701 op->getlen = ngx_stream_log_variable_getlen; 717 op->getlen = ngx_stream_log_variable_getlen;
702 op->run = ngx_stream_log_variable; 718 op->run = ngx_stream_log_variable;
703 } 719 }
704 720
705 op->data = index; 721 op->data = index;
846 return ngx_cpymem(buf, value->data, value->len); 862 return ngx_cpymem(buf, value->data, value->len);
847 863
848 } else { 864 } else {
849 return (u_char *) ngx_escape_json(buf, value->data, value->len); 865 return (u_char *) ngx_escape_json(buf, value->data, value->len);
850 } 866 }
867 }
868
869
870 static size_t
871 ngx_stream_log_unescaped_variable_getlen(ngx_stream_session_t *s, uintptr_t data)
872 {
873 ngx_stream_variable_value_t *value;
874
875 value = ngx_stream_get_indexed_variable(s, data);
876
877 if (value == NULL || value->not_found) {
878 return 0;
879 }
880
881 value->escape = 0;
882
883 return value->len;
884 }
885
886
887 static u_char *
888 ngx_stream_log_unescaped_variable(ngx_stream_session_t *s, u_char *buf,
889 ngx_stream_log_op_t *op)
890 {
891 ngx_stream_variable_value_t *value;
892
893 value = ngx_stream_get_indexed_variable(s, op->data);
894
895 if (value == NULL || value->not_found) {
896 return buf;
897 }
898
899 return ngx_cpymem(buf, value->data, value->len);
851 } 900 }
852 901
853 902
854 static void * 903 static void *
855 ngx_stream_log_create_main_conf(ngx_conf_t *cf) 904 ngx_stream_log_create_main_conf(ngx_conf_t *cf)
1263 { 1312 {
1264 u_char *data, *p, ch; 1313 u_char *data, *p, ch;
1265 size_t i, len; 1314 size_t i, len;
1266 ngx_str_t *value, var; 1315 ngx_str_t *value, var;
1267 ngx_int_t *flush; 1316 ngx_int_t *flush;
1268 ngx_uint_t bracket, json; 1317 ngx_uint_t bracket, escape;
1269 ngx_stream_log_op_t *op; 1318 ngx_stream_log_op_t *op;
1270 1319
1271 json = 0; 1320 escape = NGX_STREAM_LOG_ESCAPE_DEFAULT;
1272 value = args->elts; 1321 value = args->elts;
1273 1322
1274 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) { 1323 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
1275 data = value[s].data + 7; 1324 data = value[s].data + 7;
1276 1325
1277 if (ngx_strcmp(data, "json") == 0) { 1326 if (ngx_strcmp(data, "json") == 0) {
1278 json = 1; 1327 escape = NGX_STREAM_LOG_ESCAPE_JSON;
1328
1329 } else if (ngx_strcmp(data, "none") == 0) {
1330 escape = NGX_STREAM_LOG_ESCAPE_NONE;
1279 1331
1280 } else if (ngx_strcmp(data, "default") != 0) { 1332 } else if (ngx_strcmp(data, "default") != 0) {
1281 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 1333 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1282 "unknown log format escaping \"%s\"", data); 1334 "unknown log format escaping \"%s\"", data);
1283 return NGX_CONF_ERROR; 1335 return NGX_CONF_ERROR;
1348 1400
1349 if (var.len == 0) { 1401 if (var.len == 0) {
1350 goto invalid; 1402 goto invalid;
1351 } 1403 }
1352 1404
1353 if (ngx_stream_log_variable_compile(cf, op, &var, json) 1405 if (ngx_stream_log_variable_compile(cf, op, &var, escape)
1354 != NGX_OK) 1406 != NGX_OK)
1355 { 1407 {
1356 return NGX_CONF_ERROR; 1408 return NGX_CONF_ERROR;
1357 } 1409 }
1358 1410