diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c index 5d7f7c52b..c398036f5 100644 --- a/src/nxt_conf_validation.c +++ b/src/nxt_conf_validation.c @@ -216,6 +216,11 @@ static nxt_int_t nxt_conf_vldt_server_weight(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); static nxt_int_t nxt_conf_vldt_access_log(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); +static nxt_int_t nxt_conf_vldt_access_log_format(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value, void *data); +static nxt_int_t nxt_conf_vldt_access_log_format_field( + nxt_conf_validation_t *vldt, const nxt_str_t *name, + nxt_conf_value_t *value); static nxt_int_t nxt_conf_vldt_isolation(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); @@ -1418,7 +1423,8 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_access_log_members[] = { .type = NXT_CONF_VLDT_STRING, }, { .name = nxt_string("format"), - .type = NXT_CONF_VLDT_STRING, + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_access_log_format, }, { .name = nxt_string("if"), .type = NXT_CONF_VLDT_STRING, @@ -3523,3 +3529,46 @@ nxt_conf_vldt_access_log(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, return NXT_OK; } + + +static nxt_int_t +nxt_conf_vldt_access_log_format(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value, void *data) +{ + static const nxt_str_t format = nxt_string("format"); + + if (nxt_conf_type(value) == NXT_CONF_OBJECT) { + return nxt_conf_vldt_object_iterator(vldt, value, + nxt_conf_vldt_access_log_format_field); + } + + /* NXT_CONF_STRING */ + + return nxt_conf_vldt_access_log_format_field(vldt, &format, value); +} + + +static nxt_int_t +nxt_conf_vldt_access_log_format_field(nxt_conf_validation_t *vldt, + const nxt_str_t *name, nxt_conf_value_t *value) +{ + nxt_str_t str; + + if (name->length == 0) { + return nxt_conf_vldt_error(vldt, "In the access log format, the name " + "must not be empty."); + } + + if (nxt_conf_type(value) != NXT_CONF_STRING) { + return nxt_conf_vldt_error(vldt, "In the access log format, the value " + "must be only string."); + } + + nxt_conf_get_string(value, &str); + + if (nxt_is_tstr(&str)) { + return nxt_conf_vldt_var(vldt, name, &str); + } + + return NXT_OK; +} diff --git a/src/nxt_router.h b/src/nxt_router.h index 06c6bb32a..e2c5b83bc 100644 --- a/src/nxt_router.h +++ b/src/nxt_router.h @@ -16,12 +16,13 @@ typedef struct nxt_http_request_s nxt_http_request_t; #include -typedef struct nxt_http_action_s nxt_http_action_t; -typedef struct nxt_http_routes_s nxt_http_routes_t; -typedef struct nxt_http_forward_s nxt_http_forward_t; -typedef struct nxt_upstream_s nxt_upstream_t; -typedef struct nxt_upstreams_s nxt_upstreams_t; -typedef struct nxt_router_access_log_s nxt_router_access_log_t; +typedef struct nxt_http_action_s nxt_http_action_t; +typedef struct nxt_http_routes_s nxt_http_routes_t; +typedef struct nxt_http_forward_s nxt_http_forward_t; +typedef struct nxt_upstream_s nxt_upstream_t; +typedef struct nxt_upstreams_s nxt_upstreams_t; +typedef struct nxt_router_access_log_s nxt_router_access_log_t; +typedef struct nxt_router_access_log_format_s nxt_router_access_log_format_t; #define NXT_HTTP_ACTION_ERROR ((nxt_http_action_t *) -1) @@ -39,22 +40,22 @@ typedef struct { typedef struct { - uint32_t count; - uint32_t threads; + uint32_t count; + uint32_t threads; - nxt_mp_t *mem_pool; - nxt_tstr_state_t *tstr_state; + nxt_mp_t *mem_pool; + nxt_tstr_state_t *tstr_state; - nxt_router_t *router; - nxt_http_routes_t *routes; - nxt_upstreams_t *upstreams; + nxt_router_t *router; + nxt_http_routes_t *routes; + nxt_upstreams_t *upstreams; - nxt_lvlhsh_t mtypes_hash; - nxt_lvlhsh_t apps_hash; + nxt_lvlhsh_t mtypes_hash; + nxt_lvlhsh_t apps_hash; - nxt_router_access_log_t *access_log; - nxt_tstr_t *log_format; - nxt_tstr_cond_t log_cond; + nxt_tstr_cond_t log_cond; + nxt_router_access_log_t *access_log; + nxt_router_access_log_format_t *log_format; } nxt_router_conf_t; @@ -235,7 +236,7 @@ typedef struct { struct nxt_router_access_log_s { void (*handler)(nxt_task_t *task, nxt_http_request_t *r, nxt_router_access_log_t *access_log, - nxt_tstr_t *format); + nxt_router_access_log_format_t *format); nxt_fd_t fd; nxt_str_t path; uint32_t count; diff --git a/src/nxt_router_access_log.c b/src/nxt_router_access_log.c index 024bff395..abb859cce 100644 --- a/src/nxt_router_access_log.c +++ b/src/nxt_router_access_log.c @@ -11,21 +11,34 @@ typedef struct { - nxt_str_t path; - nxt_str_t format; - nxt_conf_value_t *expr; + nxt_str_t path; + nxt_conf_value_t *format; + nxt_conf_value_t *expr; } nxt_router_access_log_conf_t; typedef struct { - nxt_str_t text; - nxt_router_access_log_t *access_log; + nxt_str_t text; + nxt_router_access_log_t *access_log; } nxt_router_access_log_ctx_t; +typedef struct { + nxt_str_t name; + nxt_tstr_t *tstr; +} nxt_router_access_log_member_t; + + +struct nxt_router_access_log_format_s { + nxt_tstr_t *tstr; + nxt_uint_t nmembers; + nxt_router_access_log_member_t *member; +}; + + static void nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, nxt_router_access_log_t *access_log, - nxt_tstr_t *format); + nxt_router_access_log_format_t *format); static void nxt_router_access_log_write(nxt_task_t *task, nxt_http_request_t *r, nxt_router_access_log_ctx_t *ctx); static void nxt_router_access_log_ready(nxt_task_t *task, @@ -49,7 +62,7 @@ static nxt_conf_map_t nxt_router_access_log_conf[] = { { nxt_string("format"), - NXT_CONF_MAP_STR, + NXT_CONF_MAP_PTR, offsetof(nxt_router_access_log_conf_t, format), }, @@ -61,25 +74,125 @@ static nxt_conf_map_t nxt_router_access_log_conf[] = { }; +static nxt_router_access_log_format_t * +nxt_router_access_log_format_create(nxt_task_t *task, nxt_router_conf_t *rtcf, + nxt_router_access_log_conf_t *alcf) +{ + size_t size; + uint32_t i, n, next; + nxt_str_t name, str, *dst; + nxt_bool_t has_js; + nxt_conf_value_t *value; + nxt_router_access_log_member_t *member; + nxt_router_access_log_format_t *format; + + static const nxt_str_t default_format = nxt_string("$remote_addr - - " + "[$time_local] \"$request_line\" $status $body_bytes_sent " + "\"$header_referer\" \"$header_user_agent\""); + + format = nxt_mp_zalloc(rtcf->mem_pool, + sizeof(nxt_router_access_log_format_t)); + if (nxt_slow_path(format == NULL)) { + return NULL; + } + + if (alcf->format != NULL) { + + if (nxt_conf_type(alcf->format) == NXT_CONF_OBJECT) { + next = 0; + has_js = 0; + + n = nxt_conf_object_members_count(alcf->format); + + for ( ;; ) { + value = nxt_conf_next_object_member(alcf->format, &name, &next); + if (value == NULL) { + break; + } + + nxt_conf_get_string(value, &str); + + if (nxt_tstr_is_js(&str)) { + has_js = 1; + } + } + + if (has_js) { + member = nxt_mp_alloc(rtcf->mem_pool, + n * sizeof(nxt_router_access_log_member_t)); + if (nxt_slow_path(member == NULL)) { + return NULL; + } + + next = 0; + + for (i = 0; i < n; i++) { + value = nxt_conf_next_object_member(alcf->format, &name, + &next); + if (value == NULL) { + break; + } + + dst = nxt_str_dup(rtcf->mem_pool, &member[i].name, &name); + if (nxt_slow_path(dst == NULL)) { + return NULL; + } + + nxt_conf_get_string(value, &str); + + member[i].tstr = nxt_tstr_compile(rtcf->tstr_state, &str, + NXT_TSTR_LOGGING); + if (nxt_slow_path(member[i].tstr == NULL)) { + return NULL; + } + } + + format->nmembers = n; + format->member = member; + + return format; + } + + size = nxt_conf_json_length(alcf->format, NULL); + + str.start = nxt_mp_nget(rtcf->mem_pool, size); + if (nxt_slow_path(str.start == NULL)) { + return NULL; + } + + str.length = nxt_conf_json_print(str.start, alcf->format, NULL) + - str.start; + + } else { + nxt_conf_get_string(alcf->format, &str); + } + + } else { + str = default_format; + } + + format->tstr = nxt_tstr_compile(rtcf->tstr_state, &str, + NXT_TSTR_LOGGING | NXT_TSTR_NEWLINE); + if (nxt_slow_path(format->tstr == NULL)) { + return NULL; + } + + return format; +} + + nxt_int_t nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, nxt_conf_value_t *value) { nxt_int_t ret; nxt_str_t str; - nxt_tstr_t *format; nxt_router_t *router; nxt_router_access_log_t *access_log; nxt_router_access_log_conf_t alcf; - static const nxt_str_t log_format_str = nxt_string("$remote_addr - - " - "[$time_local] \"$request_line\" $status $body_bytes_sent " - "\"$header_referer\" \"$header_user_agent\""); - nxt_memzero(&alcf, sizeof(nxt_router_access_log_conf_t)); - alcf.format = log_format_str; - if (nxt_conf_type(value) == NXT_CONF_STRING) { nxt_conf_get_string(value, &alcf.path); @@ -120,15 +233,13 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, nxt_memcpy(access_log->path.start, alcf.path.start, alcf.path.length); } - format = nxt_tstr_compile(rtcf->tstr_state, &alcf.format, - NXT_TSTR_LOGGING | NXT_TSTR_NEWLINE); - if (nxt_slow_path(format == NULL)) { + rtcf->access_log = access_log; + + rtcf->log_format = nxt_router_access_log_format_create(task, rtcf, &alcf); + if (nxt_slow_path(rtcf->log_format == NULL)) { return NXT_ERROR; } - rtcf->access_log = access_log; - rtcf->log_format = format; - if (alcf.expr != NULL) { nxt_conf_get_string(alcf.expr, &str); @@ -143,22 +254,14 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, static void -nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, - nxt_router_access_log_t *access_log, nxt_tstr_t *format) +nxt_router_access_log_text(nxt_task_t *task, nxt_http_request_t *r, + nxt_router_access_log_ctx_t *ctx, nxt_router_access_log_format_t *format) { - nxt_int_t ret; - nxt_router_conf_t *rtcf; - nxt_router_access_log_ctx_t *ctx; + nxt_int_t ret; + nxt_router_conf_t *rtcf; - ctx = nxt_mp_get(r->mem_pool, sizeof(nxt_router_access_log_ctx_t)); - if (nxt_slow_path(ctx == NULL)) { - return; - } - - ctx->access_log = access_log; - - if (nxt_tstr_is_const(format)) { - nxt_tstr_str(format, &ctx->text); + if (nxt_tstr_is_const(format->tstr)) { + nxt_tstr_str(format->tstr, &ctx->text); } else { rtcf = r->conf->socket_conf->router_conf; @@ -169,11 +272,91 @@ nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, return; } - ret = nxt_tstr_query(task, r->tstr_query, format, &ctx->text); + ret = nxt_tstr_query(task, r->tstr_query, format->tstr, &ctx->text); if (nxt_slow_path(ret != NXT_OK)) { return; } } +} + + +static void +nxt_router_access_log_json(nxt_task_t *task, nxt_http_request_t *r, + nxt_router_access_log_ctx_t *ctx, nxt_router_access_log_format_t *format) +{ + u_char *p; + size_t size; + nxt_int_t ret; + nxt_str_t str; + nxt_uint_t i; + nxt_conf_value_t *value; + nxt_router_conf_t *rtcf; + nxt_router_access_log_member_t *member; + + rtcf = r->conf->socket_conf->router_conf; + + value = nxt_conf_create_object(r->mem_pool, format->nmembers); + if (nxt_slow_path(value == NULL)) { + return; + } + + for (i = 0; i < format->nmembers; i++) { + member = &format->member[i]; + + if (nxt_tstr_is_const(member->tstr)) { + nxt_tstr_str(member->tstr, &str); + + } else { + ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state, + &r->tstr_cache, r, r->mem_pool); + if (nxt_slow_path(ret != NXT_OK)) { + return; + } + + ret = nxt_tstr_query(task, r->tstr_query, member->tstr, &str); + if (nxt_slow_path(ret != NXT_OK)) { + return; + } + } + + nxt_conf_set_member_string(value, &member->name, &str, i); + } + + size = nxt_conf_json_length(value, NULL) + 1; + + p = nxt_mp_nget(r->mem_pool, size); + if (nxt_slow_path(p == NULL)) { + return; + } + + ctx->text.start = p; + + p = nxt_conf_json_print(p, value, NULL); + *p++ = '\n'; + + ctx->text.length = p - ctx->text.start; +} + + +static void +nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, + nxt_router_access_log_t *access_log, nxt_router_access_log_format_t *format) +{ + nxt_router_access_log_ctx_t *ctx; + + ctx = nxt_mp_get(r->mem_pool, sizeof(nxt_router_access_log_ctx_t)); + if (nxt_slow_path(ctx == NULL)) { + return; + } + + ctx->access_log = access_log; + + if (format->tstr != NULL) { + nxt_router_access_log_text(task, r, ctx, format); + + } else { + nxt_router_access_log_json(task, r, ctx, format); + } nxt_router_access_log_write(task, r, ctx); } diff --git a/src/nxt_tstr.c b/src/nxt_tstr.c index 36d77e69e..d939d44e2 100644 --- a/src/nxt_tstr.c +++ b/src/nxt_tstr.c @@ -41,10 +41,6 @@ struct nxt_tstr_query_s { }; -#define nxt_tstr_is_js(str) \ - nxt_strchr_start(str, '`') - - nxt_tstr_state_t * nxt_tstr_state_new(nxt_mp_t *mp, nxt_bool_t test) { diff --git a/src/nxt_tstr.h b/src/nxt_tstr.h index 8e3cdb93e..7fb02631e 100644 --- a/src/nxt_tstr.h +++ b/src/nxt_tstr.h @@ -64,6 +64,10 @@ nxt_int_t nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, void nxt_tstr_query_release(nxt_tstr_query_t *query); +#define nxt_tstr_is_js(str) \ + nxt_strchr_start(str, '`') + + nxt_inline nxt_bool_t nxt_is_tstr(nxt_str_t *str) {