comparison src/stream/ngx_stream_log_module.c @ 6840:0cf4e82e7c48

Access log: support for json escaping.
author Valentin Bartenev <vbart@nginx.com>
date Thu, 15 Dec 2016 16:25:42 +0300
parents 3908156a51fa
children c36d160cd4e0
comparison
equal deleted inserted replaced
6839:32714e608629 6840:0cf4e82e7c48
104 104
105 static void ngx_stream_log_flush(ngx_open_file_t *file, ngx_log_t *log); 105 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); 106 static void ngx_stream_log_flush_handler(ngx_event_t *ev);
107 107
108 static ngx_int_t ngx_stream_log_variable_compile(ngx_conf_t *cf, 108 static ngx_int_t ngx_stream_log_variable_compile(ngx_conf_t *cf,
109 ngx_stream_log_op_t *op, ngx_str_t *value); 109 ngx_stream_log_op_t *op, ngx_str_t *value, ngx_uint_t json);
110 static size_t ngx_stream_log_variable_getlen(ngx_stream_session_t *s, 110 static size_t ngx_stream_log_variable_getlen(ngx_stream_session_t *s,
111 uintptr_t data); 111 uintptr_t data);
112 static u_char *ngx_stream_log_variable(ngx_stream_session_t *s, u_char *buf, 112 static u_char *ngx_stream_log_variable(ngx_stream_session_t *s, u_char *buf,
113 ngx_stream_log_op_t *op); 113 ngx_stream_log_op_t *op);
114 static uintptr_t ngx_stream_log_escape(u_char *dst, u_char *src, size_t size); 114 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,
116 uintptr_t data);
117 static u_char *ngx_stream_log_json_variable(ngx_stream_session_t *s,
118 u_char *buf, ngx_stream_log_op_t *op);
115 119
116 120
117 static void *ngx_stream_log_create_main_conf(ngx_conf_t *cf); 121 static void *ngx_stream_log_create_main_conf(ngx_conf_t *cf);
118 static void *ngx_stream_log_create_srv_conf(ngx_conf_t *cf); 122 static void *ngx_stream_log_create_srv_conf(ngx_conf_t *cf);
119 static char *ngx_stream_log_merge_srv_conf(ngx_conf_t *cf, void *parent, 123 static char *ngx_stream_log_merge_srv_conf(ngx_conf_t *cf, void *parent,
684 } 688 }
685 689
686 690
687 static ngx_int_t 691 static ngx_int_t
688 ngx_stream_log_variable_compile(ngx_conf_t *cf, ngx_stream_log_op_t *op, 692 ngx_stream_log_variable_compile(ngx_conf_t *cf, ngx_stream_log_op_t *op,
689 ngx_str_t *value) 693 ngx_str_t *value, ngx_uint_t json)
690 { 694 {
691 ngx_int_t index; 695 ngx_int_t index;
692 696
693 index = ngx_stream_get_variable_index(cf, value); 697 index = ngx_stream_get_variable_index(cf, value);
694 if (index == NGX_ERROR) { 698 if (index == NGX_ERROR) {
695 return NGX_ERROR; 699 return NGX_ERROR;
696 } 700 }
697 701
698 op->len = 0; 702 op->len = 0;
699 op->getlen = ngx_stream_log_variable_getlen; 703
700 op->run = ngx_stream_log_variable; 704 if (json) {
705 op->getlen = ngx_stream_log_json_variable_getlen;
706 op->run = ngx_stream_log_json_variable;
707
708 } else {
709 op->getlen = ngx_stream_log_variable_getlen;
710 op->run = ngx_stream_log_variable;
711 }
712
701 op->data = index; 713 op->data = index;
702 714
703 return NGX_OK; 715 return NGX_OK;
704 } 716 }
705 717
801 } 813 }
802 size--; 814 size--;
803 } 815 }
804 816
805 return (uintptr_t) dst; 817 return (uintptr_t) dst;
818 }
819
820
821 static size_t
822 ngx_stream_log_json_variable_getlen(ngx_stream_session_t *s, uintptr_t data)
823 {
824 uintptr_t len;
825 ngx_stream_variable_value_t *value;
826
827 value = ngx_stream_get_indexed_variable(s, data);
828
829 if (value == NULL || value->not_found) {
830 return 0;
831 }
832
833 len = ngx_escape_json(NULL, value->data, value->len);
834
835 value->escape = len ? 1 : 0;
836
837 return value->len + len;
838 }
839
840
841 static u_char *
842 ngx_stream_log_json_variable(ngx_stream_session_t *s, u_char *buf,
843 ngx_stream_log_op_t *op)
844 {
845 ngx_stream_variable_value_t *value;
846
847 value = ngx_stream_get_indexed_variable(s, op->data);
848
849 if (value == NULL || value->not_found) {
850 return buf;
851 }
852
853 if (value->escape == 0) {
854 return ngx_cpymem(buf, value->data, value->len);
855
856 } else {
857 return (u_char *) ngx_escape_json(buf, value->data, value->len);
858 }
806 } 859 }
807 860
808 861
809 static void * 862 static void *
810 ngx_stream_log_create_main_conf(ngx_conf_t *cf) 863 ngx_stream_log_create_main_conf(ngx_conf_t *cf)
1218 { 1271 {
1219 u_char *data, *p, ch; 1272 u_char *data, *p, ch;
1220 size_t i, len; 1273 size_t i, len;
1221 ngx_str_t *value, var; 1274 ngx_str_t *value, var;
1222 ngx_int_t *flush; 1275 ngx_int_t *flush;
1223 ngx_uint_t bracket; 1276 ngx_uint_t bracket, json;
1224 ngx_stream_log_op_t *op; 1277 ngx_stream_log_op_t *op;
1225 1278
1279 json = 0;
1226 value = args->elts; 1280 value = args->elts;
1281
1282 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
1283 data = value[s].data + 7;
1284
1285 if (ngx_strcmp(data, "json") == 0) {
1286 json = 1;
1287
1288 } else if (ngx_strcmp(data, "default") != 0) {
1289 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1290 "unknown log format escaping \"%s\"", data);
1291 return NGX_CONF_ERROR;
1292 }
1293
1294 s++;
1295 }
1227 1296
1228 for ( /* void */ ; s < args->nelts; s++) { 1297 for ( /* void */ ; s < args->nelts; s++) {
1229 1298
1230 i = 0; 1299 i = 0;
1231 1300
1287 1356
1288 if (var.len == 0) { 1357 if (var.len == 0) {
1289 goto invalid; 1358 goto invalid;
1290 } 1359 }
1291 1360
1292 if (ngx_stream_log_variable_compile(cf, op, &var) != NGX_OK) { 1361 if (ngx_stream_log_variable_compile(cf, op, &var, json)
1362 != NGX_OK)
1363 {
1293 return NGX_CONF_ERROR; 1364 return NGX_CONF_ERROR;
1294 } 1365 }
1295 1366
1296 if (flushes) { 1367 if (flushes) {
1297 1368