summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorZhidao HONG <z.hong@f5.com>2024-01-23 18:57:30 +0800
committerZhidao HONG <z.hong@f5.com>2024-01-29 13:48:53 +0800
commit4c91bebb50d06b28e369d68b23022caa072cf62d (patch)
treefb15434e915f77041e56b111bfca90ce8e83ebe6
parent37abe2e4633d66241bf1766a740d2b2734c132fa (diff)
downloadunit-4c91bebb50d06b28e369d68b23022caa072cf62d.tar.gz
unit-4c91bebb50d06b28e369d68b23022caa072cf62d.tar.bz2
HTTP: enhanced access log with conditional filtering.
This feature allows users to specify conditions to control if access log should be recorded. The "if" option supports a string and JavaScript code. If its value is empty, 0, false, null, or undefined, the logs will not be recorded. And the '!' as a prefix inverses the condition. Example 1: Only log requests that sent a session cookie. { "access_log": { "if": "$cookie_session", "path": "..." } } Example 2: Do not log health check requests. { "access_log": { "if": "`${uri == '/health' ? false : true}`", "path": "..." } } Example 3: Only log requests when the time is before 22:00. { "access_log": { "if": "`${new Date().getHours() < 22}`", "path": "..." } } or { "access_log": { "if": "!`${new Date().getHours() >= 22}`", "path": "..." } } Closes: https://github.com/nginx/unit/issues/594
-rw-r--r--src/nxt_conf_validation.c37
-rw-r--r--src/nxt_http_request.c54
-rw-r--r--src/nxt_router.h2
-rw-r--r--src/nxt_router_access_log.c25
4 files changed, 112 insertions, 6 deletions
diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c
index 32ed4ffd..32124ee9 100644
--- a/src/nxt_conf_validation.c
+++ b/src/nxt_conf_validation.c
@@ -77,6 +77,8 @@ static nxt_int_t nxt_conf_vldt_error(nxt_conf_validation_t *vldt,
const char *fmt, ...);
static nxt_int_t nxt_conf_vldt_var(nxt_conf_validation_t *vldt, nxt_str_t *name,
nxt_str_t *value);
+static nxt_int_t nxt_conf_vldt_if(nxt_conf_validation_t *vldt,
+ nxt_conf_value_t *value, void *data);
nxt_inline nxt_int_t nxt_conf_vldt_unsupported(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data)
NXT_MAYBE_UNUSED;
@@ -1369,6 +1371,10 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_access_log_members[] = {
}, {
.name = nxt_string("format"),
.type = NXT_CONF_VLDT_STRING,
+ }, {
+ .name = nxt_string("if"),
+ .type = NXT_CONF_VLDT_STRING,
+ .validator = nxt_conf_vldt_if,
},
NXT_CONF_VLDT_END
@@ -1538,6 +1544,37 @@ nxt_conf_vldt_var(nxt_conf_validation_t *vldt, nxt_str_t *name,
}
+static nxt_int_t
+nxt_conf_vldt_if(nxt_conf_validation_t *vldt, nxt_conf_value_t *value,
+ void *data)
+{
+ nxt_str_t str;
+
+ static nxt_str_t if_str = nxt_string("if");
+
+ if (nxt_conf_type(value) != NXT_CONF_STRING) {
+ return nxt_conf_vldt_error(vldt, "The \"if\" must be a string");
+ }
+
+ nxt_conf_get_string(value, &str);
+
+ if (str.length == 0) {
+ return NXT_OK;
+ }
+
+ if (str.start[0] == '!') {
+ str.start++;
+ str.length--;
+ }
+
+ if (nxt_is_tstr(&str)) {
+ return nxt_conf_vldt_var(vldt, &if_str, &str);
+ }
+
+ return NXT_OK;
+}
+
+
typedef struct {
nxt_mp_t *pool;
nxt_str_t *type;
diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c
index 2e39e4e8..f8d8d887 100644
--- a/src/nxt_http_request.c
+++ b/src/nxt_http_request.c
@@ -24,8 +24,8 @@ static void nxt_http_request_proto_info(nxt_task_t *task,
static void nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj,
void *data);
static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data);
-static void nxt_http_request_access_log(nxt_task_t *task, nxt_http_request_t *r,
- nxt_router_conf_t *rtcf);
+static nxt_int_t nxt_http_request_access_log(nxt_task_t *task,
+ nxt_http_request_t *r, nxt_router_conf_t *rtcf);
static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now,
struct tm *tm, size_t size, const char *format);
@@ -818,6 +818,7 @@ nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
void
nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
{
+ nxt_int_t ret;
nxt_http_proto_t proto;
nxt_router_conf_t *rtcf;
nxt_http_request_t *r;
@@ -834,8 +835,10 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
r->logged = 1;
if (rtcf->access_log != NULL) {
- nxt_http_request_access_log(task, r, rtcf);
- return;
+ ret = nxt_http_request_access_log(task, r, rtcf);
+ if (ret == NXT_OK) {
+ return;
+ }
}
}
@@ -865,15 +868,54 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
}
-static void
+static nxt_int_t
nxt_http_request_access_log(nxt_task_t *task, nxt_http_request_t *r,
nxt_router_conf_t *rtcf)
{
+ nxt_int_t ret;
+ nxt_str_t str;
+ nxt_bool_t expr;
nxt_router_access_log_t *access_log;
access_log = rtcf->access_log;
- access_log->handler(task, r, access_log, rtcf->log_format);
+ expr = 1;
+
+ if (rtcf->log_expr != NULL) {
+
+ if (nxt_tstr_is_const(rtcf->log_expr)) {
+ nxt_tstr_str(rtcf->log_expr, &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 NXT_DECLINED;
+ }
+
+ nxt_tstr_query(task, r->tstr_query, rtcf->log_expr, &str);
+
+ if (nxt_slow_path(nxt_tstr_query_failed(r->tstr_query))) {
+ return NXT_DECLINED;
+ }
+ }
+
+ if (str.length == 0
+ || nxt_str_eq(&str, "0", 1)
+ || nxt_str_eq(&str, "false", 5)
+ || nxt_str_eq(&str, "null", 4)
+ || nxt_str_eq(&str, "undefined", 9))
+ {
+ expr = 0;
+ }
+ }
+
+ if (rtcf->log_negate ^ expr) {
+ access_log->handler(task, r, access_log, rtcf->log_format);
+ return NXT_OK;
+ }
+
+ return NXT_DECLINED;
}
diff --git a/src/nxt_router.h b/src/nxt_router.h
index b14f8410..3e523001 100644
--- a/src/nxt_router.h
+++ b/src/nxt_router.h
@@ -54,6 +54,8 @@ typedef struct {
nxt_router_access_log_t *access_log;
nxt_tstr_t *log_format;
+ nxt_tstr_t *log_expr;
+ uint8_t log_negate; /* 1 bit */
} nxt_router_conf_t;
diff --git a/src/nxt_router_access_log.c b/src/nxt_router_access_log.c
index ccbddb96..7fc59972 100644
--- a/src/nxt_router_access_log.c
+++ b/src/nxt_router_access_log.c
@@ -13,6 +13,7 @@
typedef struct {
nxt_str_t path;
nxt_str_t format;
+ nxt_conf_value_t *expr;
} nxt_router_access_log_conf_t;
@@ -53,6 +54,12 @@ static nxt_conf_map_t nxt_router_access_log_conf[] = {
NXT_CONF_MAP_STR,
offsetof(nxt_router_access_log_conf_t, format),
},
+
+ {
+ nxt_string("if"),
+ NXT_CONF_MAP_PTR,
+ offsetof(nxt_router_access_log_conf_t, expr),
+ },
};
@@ -72,6 +79,8 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf,
"[$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) {
@@ -133,6 +142,22 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf,
rtcf->access_log = access_log;
rtcf->log_format = format;
+ if (alcf.expr != NULL) {
+ nxt_conf_get_string(alcf.expr, &str);
+
+ if (str.length > 0 && str.start[0] == '!') {
+ rtcf->log_negate = 1;
+
+ str.start++;
+ str.length--;
+ }
+
+ rtcf->log_expr = nxt_tstr_compile(rtcf->tstr_state, &str, 0);
+ if (nxt_slow_path(rtcf->log_expr == NULL)) {
+ return NXT_ERROR;
+ }
+ }
+
return NXT_OK;
}