diff --git a/auto/modules b/auto/modules index 503cf05b05..c9942dbaab 100644 --- a/auto/modules +++ b/auto/modules @@ -1484,3 +1484,4 @@ have=T_NGX_HTTP_IMAGE_FILTER . auto/have have=T_HTTP_HEADER . auto/have have=T_HTTP_UPSTREAM_TIMEOUT_VAR . auto/have have=T_NGX_HTTPS_ALLOW_HTTP . auto/have +have=T_NGX_REQUEST_START_TIME . auto/have diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c index 318164d6ab..b58496d6e1 100644 --- a/src/http/ngx_http_variables.c +++ b/src/http/ngx_http_variables.c @@ -77,6 +77,12 @@ static ngx_int_t ngx_http_variable_tcpinfo(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data); #endif +#if (T_NGX_REQUEST_START_TIME) +static ngx_int_t +ngx_http_variable_request_start_time(ngx_http_request_t *r, + ngx_http_variable_value_t *v, uintptr_t data); +#endif + static ngx_int_t ngx_http_variable_content_length(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data); static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r, @@ -552,6 +558,11 @@ static ngx_http_variable_t ngx_http_core_variables[] = { #endif +#if (T_NGX_REQUEST_START_TIME) + { ngx_string("request_start_time"), NULL, ngx_http_variable_request_start_time, + 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, +#endif + ngx_http_null_variable }; @@ -1381,6 +1392,43 @@ ngx_http_variable_tcpinfo(ngx_http_request_t *r, ngx_http_variable_value_t *v, #endif +#if (T_NGX_REQUEST_START_TIME) +static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + +static ngx_int_t +ngx_http_variable_request_start_time(ngx_http_request_t *r, + ngx_http_variable_value_t *v, uintptr_t data) +{ + u_char *p; + ngx_time_t *tp; + ngx_tm_t tm; + + p = ngx_pnalloc(r->pool, sizeof("28/Sep/1970:12:00:00 +0600") - 1); + if (p == NULL) { + return NGX_ERROR; + } + + tp = ngx_timeofday(); + ngx_gmtime(r->start_sec + tp->gmtoff * 60, &tm); + + /*The time that the first request pass in*/ + (void) ngx_sprintf(p, "%02d/%s/%d:%02d:%02d:%02d %c%02i%02i", + tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1], + tm.ngx_tm_year, tm.ngx_tm_hour, + tm.ngx_tm_min, tm.ngx_tm_sec, + tp->gmtoff < 0 ? '-' : '+', + ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60)); + + v->len = sizeof("28/Sep/1970:12:00:00 +0600") - 1; + v->valid = 1; + v->no_cacheable = 0; + v->not_found = 0; + v->data = p; + + return NGX_OK; +} +#endif static ngx_int_t ngx_http_variable_content_length(ngx_http_request_t *r, diff --git a/tests/test-nginx/cases/req-start-time-variable.t b/tests/test-nginx/cases/req-start-time-variable.t new file mode 100644 index 0000000000..60282edda7 --- /dev/null +++ b/tests/test-nginx/cases/req-start-time-variable.t @@ -0,0 +1,28 @@ +# -*- mode: conf -*- +# vim:set ft= ts=4 sw=4 et fdm=marker: + +use lib 'lib'; +use Test::Nginx::Socket::Lua; + +log_level('warn'); + +repeat_each(2); + +plan tests => repeat_each() * (blocks() * 3); + +no_long_string(); +run_tests(); + +__DATA__ + +=== TEST 1: start time +--- config + location = /start { + return 200 $request_start_time; + } +--- request +GET /start +--- response_body_like: ^\d\d/[A-Z][a-z]{2}/\d{4}:\d\d:\d\d:\d\d [+-]\d{4}$ +--- no_error_log +[error] +