diff options
author | Zhidao HONG <z.hong@f5.com> | 2024-10-28 16:30:03 +0800 |
---|---|---|
committer | Zhidao HONG <z.hong@f5.com> | 2024-11-14 09:43:48 +0800 |
commit | a760e24a2c4e5f93703bb365b12807d8b1035cf5 (patch) | |
tree | 5c27dad02b27eb70523f060a98916c9c5ef57282 | |
parent | a92d8149e47334d23e8cd31c48e3f02030049e30 (diff) | |
download | unit-a760e24a2c4e5f93703bb365b12807d8b1035cf5.tar.gz unit-a760e24a2c4e5f93703bb365b12807d8b1035cf5.tar.bz2 |
http: Introduce nxt_router_access_log_format_t structure
This is a preparatory refactoring for upcoming JSON format support
in access log. We will extend format option to access object for
JSON support.
No functional changes.
-rw-r--r-- | src/nxt_router.h | 39 | ||||
-rw-r--r-- | src/nxt_router_access_log.c | 66 |
2 files changed, 68 insertions, 37 deletions
diff --git a/src/nxt_router.h b/src/nxt_router.h index 06c6bb32..e2c5b83b 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 <nxt_application.h> -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 cc34fd94..30b38aa1 100644 --- a/src/nxt_router_access_log.c +++ b/src/nxt_router_access_log.c @@ -23,9 +23,16 @@ typedef struct { } nxt_router_access_log_ctx_t; +struct nxt_router_access_log_format_s { + nxt_tstr_t *tstr; +}; + + +static nxt_router_access_log_format_t *nxt_router_access_log_format_create( + nxt_task_t *task, nxt_router_conf_t *rtcf, nxt_conf_value_t *value); 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, @@ -71,10 +78,6 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, nxt_router_access_log_t *access_log; nxt_router_access_log_conf_t alcf; - static const nxt_str_t default_format = 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)); if (nxt_conf_type(value) == NXT_CONF_STRING) { @@ -119,15 +122,8 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, rtcf->access_log = access_log; - if (alcf.format != NULL) { - nxt_conf_get_string(alcf.format, &str); - - } else { - str = default_format; - } - - rtcf->log_format = nxt_tstr_compile(rtcf->tstr_state, &str, - NXT_TSTR_LOGGING | NXT_TSTR_NEWLINE); + rtcf->log_format = nxt_router_access_log_format_create(task, rtcf, + alcf.format); if (nxt_slow_path(rtcf->log_format == NULL)) { return NXT_ERROR; } @@ -145,9 +141,43 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, } +static nxt_router_access_log_format_t * +nxt_router_access_log_format_create(nxt_task_t *task, nxt_router_conf_t *rtcf, + nxt_conf_value_t *value) +{ + nxt_str_t str; + 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 (value != NULL) { + nxt_conf_get_string(value, &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; +} + + 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_t *access_log, nxt_router_access_log_format_t *format) { nxt_int_t ret; nxt_router_conf_t *rtcf; @@ -160,8 +190,8 @@ nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, 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; @@ -172,7 +202,7 @@ 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; } |