Ticket #1450: log_no_escape.patch
| File log_no_escape.patch, 4.4 KB (added by , 9 years ago) |
|---|
-
src/http/modules/ngx_http_log_module.c
a b 13 13 #include <zlib.h> 14 14 #endif 15 15 16 #define ESCAPE_NONE 0 17 #define ESCAPE_DEFAULT 1 18 #define ESCAPE_JSON 2 16 19 17 20 typedef struct ngx_http_log_op_s ngx_http_log_op_t; 18 21 … … static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf, 126 129 ngx_http_log_op_t *op); 127 130 128 131 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);132 ngx_http_log_op_t *op, ngx_str_t *value, ngx_uint_t escape_format); 130 133 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r, 131 134 uintptr_t data); 132 135 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, … … static size_t ngx_http_log_json_variable_getlen(ngx_http_request_t *r, 136 139 uintptr_t data); 137 140 static u_char *ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf, 138 141 ngx_http_log_op_t *op); 142 static size_t ngx_http_log_no_escape_variable_getlen(ngx_http_request_t *r, 143 uintptr_t data); 144 static u_char *ngx_http_log_no_escape_variable(ngx_http_request_t *r, u_char *buf, 145 ngx_http_log_op_t *op); 139 146 140 147 141 148 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf); … … ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf, 905 912 906 913 static ngx_int_t 907 914 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op, 908 ngx_str_t *value, ngx_uint_t json)915 ngx_str_t *value, ngx_uint_t escape_format) 909 916 { 910 917 ngx_int_t index; 911 918 … … ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op, 916 923 917 924 op->len = 0; 918 925 919 if (json) { 920 op->getlen = ngx_http_log_json_variable_getlen; 921 op->run = ngx_http_log_json_variable; 922 923 } else { 924 op->getlen = ngx_http_log_variable_getlen; 925 op->run = ngx_http_log_variable; 926 switch (escape_format) { 927 case ESCAPE_JSON: 928 op->getlen = ngx_http_log_json_variable_getlen; 929 op->run = ngx_http_log_json_variable; 930 break; 931 case ESCAPE_NONE: 932 op->getlen = ngx_http_log_no_escape_variable_getlen; 933 op->run = ngx_http_log_no_escape_variable; 934 break; 935 default: 936 op->getlen = ngx_http_log_variable_getlen; 937 op->run = ngx_http_log_variable; 926 938 } 927 939 928 940 op->data = index; … … ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf, 1073 1085 } 1074 1086 1075 1087 1088 static size_t 1089 ngx_http_log_no_escape_variable_getlen(ngx_http_request_t *r, uintptr_t data) 1090 { 1091 ngx_http_variable_value_t *value; 1092 1093 value = ngx_http_get_indexed_variable(r, data); 1094 1095 if (value == NULL || value->not_found) { 1096 return 0; 1097 } 1098 1099 value->escape = 0; 1100 1101 return value->len; 1102 } 1103 1104 1105 static u_char * 1106 ngx_http_log_no_escape_variable(ngx_http_request_t *r, u_char *buf, 1107 ngx_http_log_op_t *op) 1108 { 1109 ngx_http_variable_value_t *value; 1110 1111 value = ngx_http_get_indexed_variable(r, op->data); 1112 1113 if (value == NULL || value->not_found) { 1114 return buf; 1115 } 1116 1117 return ngx_cpymem(buf, value->data, value->len); 1118 } 1119 1120 1076 1121 static void * 1077 1122 ngx_http_log_create_main_conf(ngx_conf_t *cf) 1078 1123 { … … ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes, 1536 1581 size_t i, len; 1537 1582 ngx_str_t *value, var; 1538 1583 ngx_int_t *flush; 1539 ngx_uint_t bracket, json;1584 ngx_uint_t bracket, escape_format; 1540 1585 ngx_http_log_op_t *op; 1541 1586 ngx_http_log_var_t *v; 1542 1587 1543 json = 0;1588 escape_format = ESCAPE_DEFAULT; 1544 1589 value = args->elts; 1545 1590 1546 1591 if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) { 1547 1592 data = value[s].data + 7; 1548 1593 1549 1594 if (ngx_strcmp(data, "json") == 0) { 1550 json = 1; 1595 escape_format = ESCAPE_JSON; 1596 } else if (ngx_strcmp(data, "none") == 0) { 1597 escape_format = ESCAPE_NONE; 1551 1598 1552 1599 } else if (ngx_strcmp(data, "default") != 0) { 1553 1600 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, … … ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes, 1636 1683 } 1637 1684 } 1638 1685 1639 if (ngx_http_log_variable_compile(cf, op, &var, json)1686 if (ngx_http_log_variable_compile(cf, op, &var, escape_format) 1640 1687 != NGX_OK) 1641 1688 { 1642 1689 return NGX_CONF_ERROR;
