Skip to content

Commit

Permalink
add request_start_time variable (alibaba#1867)
Browse files Browse the repository at this point in the history
* add request_start_time variable

* request_start_time: format alignment

* request_start_time: add testcase
  • Loading branch information
drawing authored and lhanjian committed Aug 27, 2024
1 parent 43048ec commit 8cb7ceb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions auto/modules
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 48 additions & 0 deletions src/http/ngx_http_variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
};

Expand Down Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions tests/test-nginx/cases/req-start-time-variable.t
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 8cb7ceb

Please sign in to comment.