diff options
author | Zhidao HONG <z.hong@f5.com> | 2022-10-12 08:21:02 +0800 |
---|---|---|
committer | Zhidao HONG <z.hong@f5.com> | 2022-10-12 08:21:02 +0800 |
commit | 7aa6b0629897235684961dfb9aa16e226efa27e6 (patch) | |
tree | 7eb408ef4de063fe997dd334fe1b75e52819e6ac /src/nxt_http_variables.c | |
parent | 08dab702cb4140613e942b86f54824b3c7dea2ad (diff) | |
download | unit-7aa6b0629897235684961dfb9aa16e226efa27e6.tar.gz unit-7aa6b0629897235684961dfb9aa16e226efa27e6.tar.bz2 |
HTTP: added a $request_time variable.
Diffstat (limited to 'src/nxt_http_variables.c')
-rw-r--r-- | src/nxt_http_variables.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/nxt_http_variables.c b/src/nxt_http_variables.c index 5a632b24..e01bcdb9 100644 --- a/src/nxt_http_variables.c +++ b/src/nxt_http_variables.c @@ -9,6 +9,8 @@ static nxt_int_t nxt_http_var_dollar(nxt_task_t *task, nxt_str_t *str, void *ctx, uint16_t field); +static nxt_int_t nxt_http_var_request_time(nxt_task_t *task, nxt_str_t *str, + void *ctx, uint16_t field); static nxt_int_t nxt_http_var_method(nxt_task_t *task, nxt_str_t *str, void *ctx, uint16_t field); static nxt_int_t nxt_http_var_request_uri(nxt_task_t *task, nxt_str_t *str, @@ -46,6 +48,9 @@ static nxt_var_decl_t nxt_http_vars[] = { .name = nxt_string("dollar"), .handler = nxt_http_var_dollar, }, { + .name = nxt_string("request_time"), + .handler = nxt_http_var_request_time, + }, { .name = nxt_string("method"), .handler = nxt_http_var_method, }, { @@ -111,6 +116,34 @@ nxt_http_var_dollar(nxt_task_t *task, nxt_str_t *str, void *ctx, uint16_t field) static nxt_int_t +nxt_http_var_request_time(nxt_task_t *task, nxt_str_t *str, void *ctx, + uint16_t field) +{ + u_char *p; + nxt_msec_t ms; + nxt_nsec_t now; + nxt_http_request_t *r; + + r = ctx; + + now = nxt_thread_monotonic_time(task->thread); + ms = (now - r->start_time) / 1000000; + + str->start = nxt_mp_nget(r->mem_pool, NXT_TIME_T_LEN + 4); + if (nxt_slow_path(str->start == NULL)) { + return NXT_ERROR; + } + + p = nxt_sprintf(str->start, str->start + NXT_TIME_T_LEN, "%T.%03M", + (nxt_time_t) ms / 1000, ms % 1000); + + str->length = p - str->start; + + return NXT_OK; +} + + +static nxt_int_t nxt_http_var_method(nxt_task_t *task, nxt_str_t *str, void *ctx, uint16_t field) { nxt_http_request_t *r; |