Ticket #1450: log_no_escape.patch

File log_no_escape.patch, 4.4 KB (added by calind@…, 9 years ago)
  • src/http/modules/ngx_http_log_module.c

    a b  
    1313#include <zlib.h>
    1414#endif
    1515
     16#define ESCAPE_NONE    0
     17#define ESCAPE_DEFAULT 1
     18#define ESCAPE_JSON    2
    1619
    1720typedef struct ngx_http_log_op_s  ngx_http_log_op_t;
    1821
    static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,  
    126129    ngx_http_log_op_t *op);
    127130
    128131static 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);
    130133static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
    131134    uintptr_t data);
    132135static 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,  
    136139    uintptr_t data);
    137140static u_char *ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,
    138141    ngx_http_log_op_t *op);
     142static size_t ngx_http_log_no_escape_variable_getlen(ngx_http_request_t *r,
     143    uintptr_t data);
     144static u_char *ngx_http_log_no_escape_variable(ngx_http_request_t *r, u_char *buf,
     145    ngx_http_log_op_t *op);
    139146
    140147
    141148static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
    ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,  
    905912
    906913static ngx_int_t
    907914ngx_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)
    909916{
    910917    ngx_int_t  index;
    911918
    ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,  
    916923
    917924    op->len = 0;
    918925
    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;
    926938    }
    927939
    928940    op->data = index;
    ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,  
    10731085}
    10741086
    10751087
     1088static size_t
     1089ngx_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
     1105static u_char *
     1106ngx_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
    10761121static void *
    10771122ngx_http_log_create_main_conf(ngx_conf_t *cf)
    10781123{
    ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,  
    15361581    size_t               i, len;
    15371582    ngx_str_t           *value, var;
    15381583    ngx_int_t           *flush;
    1539     ngx_uint_t           bracket, json;
     1584    ngx_uint_t           bracket, escape_format;
    15401585    ngx_http_log_op_t   *op;
    15411586    ngx_http_log_var_t  *v;
    15421587
    1543     json = 0;
     1588    escape_format = ESCAPE_DEFAULT;
    15441589    value = args->elts;
    15451590
    15461591    if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
    15471592        data = value[s].data + 7;
    15481593
    15491594        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;
    15511598
    15521599        } else if (ngx_strcmp(data, "default") != 0) {
    15531600            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,  
    16361683                    }
    16371684                }
    16381685
    1639                 if (ngx_http_log_variable_compile(cf, op, &var, json)
     1686                if (ngx_http_log_variable_compile(cf, op, &var, escape_format)
    16401687                    != NGX_OK)
    16411688                {
    16421689                    return NGX_CONF_ERROR;