Skip to content

Commit

Permalink
Access log: support for json escaping.
Browse files Browse the repository at this point in the history
  • Loading branch information
VBart committed Dec 15, 2016
1 parent 7040eee commit c40d8dd
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 12 deletions.
83 changes: 77 additions & 6 deletions src/http/modules/ngx_http_log_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,16 @@ static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op);

static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
ngx_http_log_op_t *op, ngx_str_t *value);
ngx_http_log_op_t *op, ngx_str_t *value, ngx_uint_t json);
static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
uintptr_t data);
static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op);
static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
static size_t ngx_http_log_json_variable_getlen(ngx_http_request_t *r,
uintptr_t data);
static u_char *ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op);


static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
Expand Down Expand Up @@ -909,7 +913,7 @@ ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,

static ngx_int_t
ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
ngx_str_t *value)
ngx_str_t *value, ngx_uint_t json)
{
ngx_int_t index;

Expand All @@ -919,8 +923,16 @@ ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
}

op->len = 0;
op->getlen = ngx_http_log_variable_getlen;
op->run = ngx_http_log_variable;

if (json) {
op->getlen = ngx_http_log_json_variable_getlen;
op->run = ngx_http_log_json_variable;

} else {
op->getlen = ngx_http_log_variable_getlen;
op->run = ngx_http_log_variable;
}

op->data = index;

return NGX_OK;
Expand Down Expand Up @@ -1028,6 +1040,47 @@ ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
}


static size_t
ngx_http_log_json_variable_getlen(ngx_http_request_t *r, uintptr_t data)
{
uintptr_t len;
ngx_http_variable_value_t *value;

value = ngx_http_get_indexed_variable(r, data);

if (value == NULL || value->not_found) {
return 0;
}

len = ngx_escape_json(NULL, value->data, value->len);

value->escape = len ? 1 : 0;

return value->len + len;
}


static u_char *
ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op)
{
ngx_http_variable_value_t *value;

value = ngx_http_get_indexed_variable(r, op->data);

if (value == NULL || value->not_found) {
return buf;
}

if (value->escape == 0) {
return ngx_cpymem(buf, value->data, value->len);

} else {
return (u_char *) ngx_escape_json(buf, value->data, value->len);
}
}


static void *
ngx_http_log_create_main_conf(ngx_conf_t *cf)
{
Expand Down Expand Up @@ -1491,12 +1544,28 @@ ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
size_t i, len;
ngx_str_t *value, var;
ngx_int_t *flush;
ngx_uint_t bracket;
ngx_uint_t bracket, json;
ngx_http_log_op_t *op;
ngx_http_log_var_t *v;

json = 0;
value = args->elts;

if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
data = value[s].data + 7;

if (ngx_strcmp(data, "json") == 0) {
json = 1;

} else if (ngx_strcmp(data, "default") != 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"unknown log format escaping \"%s\"", data);
return NGX_CONF_ERROR;
}

s++;
}

for ( /* void */ ; s < args->nelts; s++) {

i = 0;
Expand Down Expand Up @@ -1575,7 +1644,9 @@ ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
}
}

if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
if (ngx_http_log_variable_compile(cf, op, &var, json)
!= NGX_OK)
{
return NGX_CONF_ERROR;
}

Expand Down
83 changes: 77 additions & 6 deletions src/stream/ngx_stream_log_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ static void ngx_stream_log_flush(ngx_open_file_t *file, ngx_log_t *log);
static void ngx_stream_log_flush_handler(ngx_event_t *ev);

static ngx_int_t ngx_stream_log_variable_compile(ngx_conf_t *cf,
ngx_stream_log_op_t *op, ngx_str_t *value);
ngx_stream_log_op_t *op, ngx_str_t *value, ngx_uint_t json);
static size_t ngx_stream_log_variable_getlen(ngx_stream_session_t *s,
uintptr_t data);
static u_char *ngx_stream_log_variable(ngx_stream_session_t *s, u_char *buf,
ngx_stream_log_op_t *op);
static uintptr_t ngx_stream_log_escape(u_char *dst, u_char *src, size_t size);
static size_t ngx_stream_log_json_variable_getlen(ngx_stream_session_t *s,
uintptr_t data);
static u_char *ngx_stream_log_json_variable(ngx_stream_session_t *s,
u_char *buf, ngx_stream_log_op_t *op);


static void *ngx_stream_log_create_main_conf(ngx_conf_t *cf);
Expand Down Expand Up @@ -686,7 +690,7 @@ ngx_stream_log_copy_long(ngx_stream_session_t *s, u_char *buf,

static ngx_int_t
ngx_stream_log_variable_compile(ngx_conf_t *cf, ngx_stream_log_op_t *op,
ngx_str_t *value)
ngx_str_t *value, ngx_uint_t json)
{
ngx_int_t index;

Expand All @@ -696,8 +700,16 @@ ngx_stream_log_variable_compile(ngx_conf_t *cf, ngx_stream_log_op_t *op,
}

op->len = 0;
op->getlen = ngx_stream_log_variable_getlen;
op->run = ngx_stream_log_variable;

if (json) {
op->getlen = ngx_stream_log_json_variable_getlen;
op->run = ngx_stream_log_json_variable;

} else {
op->getlen = ngx_stream_log_variable_getlen;
op->run = ngx_stream_log_variable;
}

op->data = index;

return NGX_OK;
Expand Down Expand Up @@ -806,6 +818,47 @@ ngx_stream_log_escape(u_char *dst, u_char *src, size_t size)
}


static size_t
ngx_stream_log_json_variable_getlen(ngx_stream_session_t *s, uintptr_t data)
{
uintptr_t len;
ngx_stream_variable_value_t *value;

value = ngx_stream_get_indexed_variable(s, data);

if (value == NULL || value->not_found) {
return 0;
}

len = ngx_escape_json(NULL, value->data, value->len);

value->escape = len ? 1 : 0;

return value->len + len;
}


static u_char *
ngx_stream_log_json_variable(ngx_stream_session_t *s, u_char *buf,
ngx_stream_log_op_t *op)
{
ngx_stream_variable_value_t *value;

value = ngx_stream_get_indexed_variable(s, op->data);

if (value == NULL || value->not_found) {
return buf;
}

if (value->escape == 0) {
return ngx_cpymem(buf, value->data, value->len);

} else {
return (u_char *) ngx_escape_json(buf, value->data, value->len);
}
}


static void *
ngx_stream_log_create_main_conf(ngx_conf_t *cf)
{
Expand Down Expand Up @@ -1220,11 +1273,27 @@ ngx_stream_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
size_t i, len;
ngx_str_t *value, var;
ngx_int_t *flush;
ngx_uint_t bracket;
ngx_uint_t bracket, json;
ngx_stream_log_op_t *op;

json = 0;
value = args->elts;

if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
data = value[s].data + 7;

if (ngx_strcmp(data, "json") == 0) {
json = 1;

} else if (ngx_strcmp(data, "default") != 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"unknown log format escaping \"%s\"", data);
return NGX_CONF_ERROR;
}

s++;
}

for ( /* void */ ; s < args->nelts; s++) {

i = 0;
Expand Down Expand Up @@ -1289,7 +1358,9 @@ ngx_stream_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
goto invalid;
}

if (ngx_stream_log_variable_compile(cf, op, &var) != NGX_OK) {
if (ngx_stream_log_variable_compile(cf, op, &var, json)
!= NGX_OK)
{
return NGX_CONF_ERROR;
}

Expand Down

0 comments on commit c40d8dd

Please sign in to comment.