comparison src/http/modules/ngx_http_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 c7c8354f99fa
comparison
equal deleted inserted replaced
7222:81fae70d6cb8 7223:265c29b0b8b8
86 typedef struct { 86 typedef struct {
87 ngx_str_t name; 87 ngx_str_t name;
88 size_t len; 88 size_t len;
89 ngx_http_log_op_run_pt run; 89 ngx_http_log_op_run_pt run;
90 } ngx_http_log_var_t; 90 } ngx_http_log_var_t;
91
92
93 #define NGX_HTTP_LOG_ESCAPE_DEFAULT 0
94 #define NGX_HTTP_LOG_ESCAPE_JSON 1
95 #define NGX_HTTP_LOG_ESCAPE_NONE 2
91 96
92 97
93 static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log, 98 static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
94 u_char *buf, size_t len); 99 u_char *buf, size_t len);
95 static ssize_t ngx_http_log_script_write(ngx_http_request_t *r, 100 static ssize_t ngx_http_log_script_write(ngx_http_request_t *r,
124 u_char *buf, ngx_http_log_op_t *op); 129 u_char *buf, ngx_http_log_op_t *op);
125 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf, 130 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
126 ngx_http_log_op_t *op); 131 ngx_http_log_op_t *op);
127 132
128 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf, 133 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
129 ngx_http_log_op_t *op, ngx_str_t *value, ngx_uint_t json); 134 ngx_http_log_op_t *op, ngx_str_t *value, ngx_uint_t escape);
130 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r, 135 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
131 uintptr_t data); 136 uintptr_t data);
132 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, 137 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
133 ngx_http_log_op_t *op); 138 ngx_http_log_op_t *op);
134 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size); 139 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
135 static size_t ngx_http_log_json_variable_getlen(ngx_http_request_t *r, 140 static size_t ngx_http_log_json_variable_getlen(ngx_http_request_t *r,
136 uintptr_t data); 141 uintptr_t data);
137 static u_char *ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf, 142 static u_char *ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,
138 ngx_http_log_op_t *op); 143 ngx_http_log_op_t *op);
144 static size_t ngx_http_log_unescaped_variable_getlen(ngx_http_request_t *r,
145 uintptr_t data);
146 static u_char *ngx_http_log_unescaped_variable(ngx_http_request_t *r,
147 u_char *buf, ngx_http_log_op_t *op);
139 148
140 149
141 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf); 150 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
142 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf); 151 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
143 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, 152 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
903 } 912 }
904 913
905 914
906 static ngx_int_t 915 static ngx_int_t
907 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op, 916 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
908 ngx_str_t *value, ngx_uint_t json) 917 ngx_str_t *value, ngx_uint_t escape)
909 { 918 {
910 ngx_int_t index; 919 ngx_int_t index;
911 920
912 index = ngx_http_get_variable_index(cf, value); 921 index = ngx_http_get_variable_index(cf, value);
913 if (index == NGX_ERROR) { 922 if (index == NGX_ERROR) {
914 return NGX_ERROR; 923 return NGX_ERROR;
915 } 924 }
916 925
917 op->len = 0; 926 op->len = 0;
918 927
919 if (json) { 928 switch (escape) {
929 case NGX_HTTP_LOG_ESCAPE_JSON:
920 op->getlen = ngx_http_log_json_variable_getlen; 930 op->getlen = ngx_http_log_json_variable_getlen;
921 op->run = ngx_http_log_json_variable; 931 op->run = ngx_http_log_json_variable;
922 932 break;
923 } else { 933
934 case NGX_HTTP_LOG_ESCAPE_NONE:
935 op->getlen = ngx_http_log_unescaped_variable_getlen;
936 op->run = ngx_http_log_unescaped_variable;
937 break;
938
939 default: /* NGX_HTTP_LOG_ESCAPE_DEFAULT */
924 op->getlen = ngx_http_log_variable_getlen; 940 op->getlen = ngx_http_log_variable_getlen;
925 op->run = ngx_http_log_variable; 941 op->run = ngx_http_log_variable;
926 } 942 }
927 943
928 op->data = index; 944 op->data = index;
1068 return ngx_cpymem(buf, value->data, value->len); 1084 return ngx_cpymem(buf, value->data, value->len);
1069 1085
1070 } else { 1086 } else {
1071 return (u_char *) ngx_escape_json(buf, value->data, value->len); 1087 return (u_char *) ngx_escape_json(buf, value->data, value->len);
1072 } 1088 }
1089 }
1090
1091
1092 static size_t
1093 ngx_http_log_unescaped_variable_getlen(ngx_http_request_t *r, uintptr_t data)
1094 {
1095 ngx_http_variable_value_t *value;
1096
1097 value = ngx_http_get_indexed_variable(r, data);
1098
1099 if (value == NULL || value->not_found) {
1100 return 0;
1101 }
1102
1103 value->escape = 0;
1104
1105 return value->len;
1106 }
1107
1108
1109 static u_char *
1110 ngx_http_log_unescaped_variable(ngx_http_request_t *r, u_char *buf,
1111 ngx_http_log_op_t *op)
1112 {
1113 ngx_http_variable_value_t *value;
1114
1115 value = ngx_http_get_indexed_variable(r, op->data);
1116
1117 if (value == NULL || value->not_found) {
1118 return buf;
1119 }
1120
1121 return ngx_cpymem(buf, value->data, value->len);
1073 } 1122 }
1074 1123
1075 1124
1076 static void * 1125 static void *
1077 ngx_http_log_create_main_conf(ngx_conf_t *cf) 1126 ngx_http_log_create_main_conf(ngx_conf_t *cf)
1534 { 1583 {
1535 u_char *data, *p, ch; 1584 u_char *data, *p, ch;
1536 size_t i, len; 1585 size_t i, len;
1537 ngx_str_t *value, var; 1586 ngx_str_t *value, var;
1538 ngx_int_t *flush; 1587 ngx_int_t *flush;
1539 ngx_uint_t bracket, json; 1588 ngx_uint_t bracket, escape;
1540 ngx_http_log_op_t *op; 1589 ngx_http_log_op_t *op;
1541 ngx_http_log_var_t *v; 1590 ngx_http_log_var_t *v;
1542 1591
1543 json = 0; 1592 escape = NGX_HTTP_LOG_ESCAPE_DEFAULT;
1544 value = args->elts; 1593 value = args->elts;
1545 1594
1546 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) { 1595 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
1547 data = value[s].data + 7; 1596 data = value[s].data + 7;
1548 1597
1549 if (ngx_strcmp(data, "json") == 0) { 1598 if (ngx_strcmp(data, "json") == 0) {
1550 json = 1; 1599 escape = NGX_HTTP_LOG_ESCAPE_JSON;
1600
1601 } else if (ngx_strcmp(data, "none") == 0) {
1602 escape = NGX_HTTP_LOG_ESCAPE_NONE;
1551 1603
1552 } else if (ngx_strcmp(data, "default") != 0) { 1604 } else if (ngx_strcmp(data, "default") != 0) {
1553 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 1605 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1554 "unknown log format escaping \"%s\"", data); 1606 "unknown log format escaping \"%s\"", data);
1555 return NGX_CONF_ERROR; 1607 return NGX_CONF_ERROR;
1634 1686
1635 goto found; 1687 goto found;
1636 } 1688 }
1637 } 1689 }
1638 1690
1639 if (ngx_http_log_variable_compile(cf, op, &var, json) 1691 if (ngx_http_log_variable_compile(cf, op, &var, escape)
1640 != NGX_OK) 1692 != NGX_OK)
1641 { 1693 {
1642 return NGX_CONF_ERROR; 1694 return NGX_CONF_ERROR;
1643 } 1695 }
1644 1696