Klaus Demo nginx / c40d8dd
Access log: support for json escaping. Valentin Bartenev 3 years ago
2 changed file(s) with 154 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
125125 ngx_http_log_op_t *op);
126126
127127 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
128 ngx_http_log_op_t *op, ngx_str_t *value);
128 ngx_http_log_op_t *op, ngx_str_t *value, ngx_uint_t json);
129129 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
130130 uintptr_t data);
131131 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
132132 ngx_http_log_op_t *op);
133133 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
134 static size_t ngx_http_log_json_variable_getlen(ngx_http_request_t *r,
135 uintptr_t data);
136 static u_char *ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,
137 ngx_http_log_op_t *op);
134138
135139
136140 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
908912
909913 static ngx_int_t
910914 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
911 ngx_str_t *value)
915 ngx_str_t *value, ngx_uint_t json)
912916 {
913917 ngx_int_t index;
914918
918922 }
919923
920924 op->len = 0;
921 op->getlen = ngx_http_log_variable_getlen;
922 op->run = ngx_http_log_variable;
925
926 if (json) {
927 op->getlen = ngx_http_log_json_variable_getlen;
928 op->run = ngx_http_log_json_variable;
929
930 } else {
931 op->getlen = ngx_http_log_variable_getlen;
932 op->run = ngx_http_log_variable;
933 }
934
923935 op->data = index;
924936
925937 return NGX_OK;
10241036 }
10251037
10261038 return (uintptr_t) dst;
1039 }
1040
1041
1042 static size_t
1043 ngx_http_log_json_variable_getlen(ngx_http_request_t *r, uintptr_t data)
1044 {
1045 uintptr_t len;
1046 ngx_http_variable_value_t *value;
1047
1048 value = ngx_http_get_indexed_variable(r, data);
1049
1050 if (value == NULL || value->not_found) {
1051 return 0;
1052 }
1053
1054 len = ngx_escape_json(NULL, value->data, value->len);
1055
1056 value->escape = len ? 1 : 0;
1057
1058 return value->len + len;
1059 }
1060
1061
1062 static u_char *
1063 ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,
1064 ngx_http_log_op_t *op)
1065 {
1066 ngx_http_variable_value_t *value;
1067
1068 value = ngx_http_get_indexed_variable(r, op->data);
1069
1070 if (value == NULL || value->not_found) {
1071 return buf;
1072 }
1073
1074 if (value->escape == 0) {
1075 return ngx_cpymem(buf, value->data, value->len);
1076
1077 } else {
1078 return (u_char *) ngx_escape_json(buf, value->data, value->len);
1079 }
10271080 }
10281081
10291082
14901543 size_t i, len;
14911544 ngx_str_t *value, var;
14921545 ngx_int_t *flush;
1493 ngx_uint_t bracket;
1546 ngx_uint_t bracket, json;
14941547 ngx_http_log_op_t *op;
14951548 ngx_http_log_var_t *v;
14961549
1550 json = 0;
14971551 value = args->elts;
1552
1553 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
1554 data = value[s].data + 7;
1555
1556 if (ngx_strcmp(data, "json") == 0) {
1557 json = 1;
1558
1559 } else if (ngx_strcmp(data, "default") != 0) {
1560 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1561 "unknown log format escaping \"%s\"", data);
1562 return NGX_CONF_ERROR;
1563 }
1564
1565 s++;
1566 }
14981567
14991568 for ( /* void */ ; s < args->nelts; s++) {
15001569
15741643 }
15751644 }
15761645
1577 if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
1646 if (ngx_http_log_variable_compile(cf, op, &var, json)
1647 != NGX_OK)
1648 {
15781649 return NGX_CONF_ERROR;
15791650 }
15801651
105105 static void ngx_stream_log_flush_handler(ngx_event_t *ev);
106106
107107 static ngx_int_t ngx_stream_log_variable_compile(ngx_conf_t *cf,
108 ngx_stream_log_op_t *op, ngx_str_t *value);
108 ngx_stream_log_op_t *op, ngx_str_t *value, ngx_uint_t json);
109109 static size_t ngx_stream_log_variable_getlen(ngx_stream_session_t *s,
110110 uintptr_t data);
111111 static u_char *ngx_stream_log_variable(ngx_stream_session_t *s, u_char *buf,
112112 ngx_stream_log_op_t *op);
113113 static uintptr_t ngx_stream_log_escape(u_char *dst, u_char *src, size_t size);
114 static size_t ngx_stream_log_json_variable_getlen(ngx_stream_session_t *s,
115 uintptr_t data);
116 static u_char *ngx_stream_log_json_variable(ngx_stream_session_t *s,
117 u_char *buf, ngx_stream_log_op_t *op);
114118
115119
116120 static void *ngx_stream_log_create_main_conf(ngx_conf_t *cf);
685689
686690 static ngx_int_t
687691 ngx_stream_log_variable_compile(ngx_conf_t *cf, ngx_stream_log_op_t *op,
688 ngx_str_t *value)
692 ngx_str_t *value, ngx_uint_t json)
689693 {
690694 ngx_int_t index;
691695
695699 }
696700
697701 op->len = 0;
698 op->getlen = ngx_stream_log_variable_getlen;
699 op->run = ngx_stream_log_variable;
702
703 if (json) {
704 op->getlen = ngx_stream_log_json_variable_getlen;
705 op->run = ngx_stream_log_json_variable;
706
707 } else {
708 op->getlen = ngx_stream_log_variable_getlen;
709 op->run = ngx_stream_log_variable;
710 }
711
700712 op->data = index;
701713
702714 return NGX_OK;
802814 }
803815
804816 return (uintptr_t) dst;
817 }
818
819
820 static size_t
821 ngx_stream_log_json_variable_getlen(ngx_stream_session_t *s, uintptr_t data)
822 {
823 uintptr_t len;
824 ngx_stream_variable_value_t *value;
825
826 value = ngx_stream_get_indexed_variable(s, data);
827
828 if (value == NULL || value->not_found) {
829 return 0;
830 }
831
832 len = ngx_escape_json(NULL, value->data, value->len);
833
834 value->escape = len ? 1 : 0;
835
836 return value->len + len;
837 }
838
839
840 static u_char *
841 ngx_stream_log_json_variable(ngx_stream_session_t *s, u_char *buf,
842 ngx_stream_log_op_t *op)
843 {
844 ngx_stream_variable_value_t *value;
845
846 value = ngx_stream_get_indexed_variable(s, op->data);
847
848 if (value == NULL || value->not_found) {
849 return buf;
850 }
851
852 if (value->escape == 0) {
853 return ngx_cpymem(buf, value->data, value->len);
854
855 } else {
856 return (u_char *) ngx_escape_json(buf, value->data, value->len);
857 }
805858 }
806859
807860
12191272 size_t i, len;
12201273 ngx_str_t *value, var;
12211274 ngx_int_t *flush;
1222 ngx_uint_t bracket;
1275 ngx_uint_t bracket, json;
12231276 ngx_stream_log_op_t *op;
12241277
1278 json = 0;
12251279 value = args->elts;
1280
1281 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
1282 data = value[s].data + 7;
1283
1284 if (ngx_strcmp(data, "json") == 0) {
1285 json = 1;
1286
1287 } else if (ngx_strcmp(data, "default") != 0) {
1288 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1289 "unknown log format escaping \"%s\"", data);
1290 return NGX_CONF_ERROR;
1291 }
1292
1293 s++;
1294 }
12261295
12271296 for ( /* void */ ; s < args->nelts; s++) {
12281297
12881357 goto invalid;
12891358 }
12901359
1291 if (ngx_stream_log_variable_compile(cf, op, &var) != NGX_OK) {
1360 if (ngx_stream_log_variable_compile(cf, op, &var, json)
1361 != NGX_OK)
1362 {
12921363 return NGX_CONF_ERROR;
12931364 }
12941365