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 | |
parent | 08dab702cb4140613e942b86f54824b3c7dea2ad (diff) | |
download | unit-7aa6b0629897235684961dfb9aa16e226efa27e6.tar.gz unit-7aa6b0629897235684961dfb9aa16e226efa27e6.tar.bz2 |
HTTP: added a $request_time variable.
-rw-r--r-- | docs/changes.xml | 6 | ||||
-rw-r--r-- | src/nxt_http.h | 2 | ||||
-rw-r--r-- | src/nxt_http_request.c | 2 | ||||
-rw-r--r-- | src/nxt_http_variables.c | 33 |
4 files changed, 43 insertions, 0 deletions
diff --git a/docs/changes.xml b/docs/changes.xml index 8f871eb1..91cf8fe3 100644 --- a/docs/changes.xml +++ b/docs/changes.xml @@ -37,6 +37,12 @@ support rack v3 in ruby applications. </para> </change> +<change type="feature"> +<para> +the $request_time variable contains the request processing time. +</para> +</change> + <change type="bugfix"> <para> fix HTTP cookie parsing when the value contains an equals sign. diff --git a/src/nxt_http.h b/src/nxt_http.h index c2e85840..d5bff712 100644 --- a/src/nxt_http.h +++ b/src/nxt_http.h @@ -140,6 +140,8 @@ struct nxt_http_request_s { nxt_buf_t *out; const nxt_http_request_state_t *state; + nxt_nsec_t start_time; + nxt_str_t host; nxt_str_t server_name; nxt_str_t target; diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c index 7a790f73..5c1455bf 100644 --- a/src/nxt_http_request.c +++ b/src/nxt_http_request.c @@ -278,6 +278,8 @@ nxt_http_request_create(nxt_task_t *task) r->resp.content_length_n = -1; r->state = &nxt_http_request_init_state; + r->start_time = nxt_thread_monotonic_time(task->thread); + task->thread->engine->requests_cnt++; return r; 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; |