Skip to content

Commit 362a8fc

Browse files
authored
Merge pull request #1299 from jinjiu/master
Add new variables: $ssl_handshake_time and $ssl_handshake_time_msec and fix handshake time bug.
2 parents cae4b1c + 6a8fa79 commit 362a8fc

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/event/ngx_event_openssl.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5448,7 +5448,7 @@ ngx_ssl_get_handshake_time(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
54485448
tp = ngx_timeofday();
54495449

54505450
if (c->ssl->handshake_end_msec == 0) {
5451-
ms = tp->sec * 1000 + tp->sec - c->ssl->handshake_start_msec;
5451+
ms = tp->sec * 1000 + tp->msec - c->ssl->handshake_start_msec;
54525452

54535453
} else {
54545454
ms = c->ssl->handshake_end_msec - c->ssl->handshake_start_msec;
@@ -5461,6 +5461,42 @@ ngx_ssl_get_handshake_time(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
54615461
return NGX_ERROR;
54625462
}
54635463

5464+
s->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p;
5465+
s->data = p;
5466+
5467+
return NGX_OK;
5468+
}
5469+
5470+
5471+
ngx_int_t
5472+
ngx_ssl_get_handshake_time_msec(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
5473+
{
5474+
ngx_msec_int_t ms;
5475+
u_char *p;
5476+
ngx_time_t *tp;
5477+
5478+
if (c->ssl == NULL) {
5479+
ngx_str_null(s);
5480+
5481+
return NGX_OK;
5482+
}
5483+
5484+
tp = ngx_timeofday();
5485+
5486+
if (c->ssl->handshake_end_msec == 0) {
5487+
ms = tp->sec * 1000 + tp->msec - c->ssl->handshake_start_msec;
5488+
5489+
} else {
5490+
ms = c->ssl->handshake_end_msec - c->ssl->handshake_start_msec;
5491+
}
5492+
5493+
ms = ngx_max(ms, 0);
5494+
5495+
p = ngx_pnalloc(pool, NGX_TIME_T_LEN);
5496+
if (p == NULL) {
5497+
return NGX_ERROR;
5498+
}
5499+
54645500
s->len = ngx_sprintf(p, "%i", ms) - p;
54655501
s->data = p;
54665502

src/event/ngx_event_openssl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ ngx_int_t ngx_ssl_get_client_v_remain(ngx_connection_t *c, ngx_pool_t *pool,
276276
#if (T_NGX_SSL_HANDSHAKE_TIME)
277277
ngx_int_t ngx_ssl_get_handshake_time(ngx_connection_t *c, ngx_pool_t *pool,
278278
ngx_str_t *s);
279+
ngx_int_t ngx_ssl_get_handshake_time_msec(ngx_connection_t *c, ngx_pool_t *pool,
280+
ngx_str_t *s);
279281
#endif
280282

281283

src/http/modules/ngx_http_ssl_module.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,15 @@ static ngx_http_variable_t ngx_http_ssl_vars[] = {
385385
(uintptr_t) ngx_ssl_get_client_v_remain, NGX_HTTP_VAR_CHANGEABLE, 0 },
386386

387387
#if (T_NGX_SSL_HANDSHAKE_TIME)
388+
/* $ssl_shandshakd_time deprecated and will be removed in the next release */
388389
{ ngx_string("ssl_handshakd_time"), NULL, ngx_http_ssl_variable,
389390
(uintptr_t) ngx_ssl_get_handshake_time, NGX_HTTP_VAR_CHANGEABLE, 0 },
391+
392+
{ ngx_string("ssl_handshake_time"), NULL, ngx_http_ssl_variable,
393+
(uintptr_t) ngx_ssl_get_handshake_time, NGX_HTTP_VAR_CHANGEABLE, 0 },
394+
395+
{ ngx_string("ssl_handshake_time_msec"), NULL, ngx_http_ssl_variable,
396+
(uintptr_t) ngx_ssl_get_handshake_time_msec, NGX_HTTP_VAR_CHANGEABLE, 0 },
390397
#endif
391398

392399
ngx_http_null_variable

src/stream/ngx_stream_ssl_module.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,15 @@ static ngx_stream_variable_t ngx_stream_ssl_vars[] = {
301301
(uintptr_t) ngx_ssl_get_client_v_remain, NGX_STREAM_VAR_CHANGEABLE, 0 },
302302

303303
#if (T_NGX_SSL_HANDSHAKE_TIME)
304+
/* $ssl_shandshakd_time deprecated and will be removed in the next release */
304305
{ ngx_string("ssl_handshakd_time"), NULL, ngx_stream_ssl_variable,
305306
(uintptr_t) ngx_ssl_get_handshake_time, NGX_STREAM_VAR_CHANGEABLE, 0 },
307+
308+
{ ngx_string("ssl_handshake_time"), NULL, ngx_stream_ssl_variable,
309+
(uintptr_t) ngx_ssl_get_handshake_time, NGX_STREAM_VAR_CHANGEABLE, 0 },
310+
311+
{ ngx_string("ssl_handshake_time_msec"), NULL, ngx_stream_ssl_variable,
312+
(uintptr_t) ngx_ssl_get_handshake_time_msec, NGX_STREAM_VAR_CHANGEABLE, 0 },
306313
#endif
307314

308315
ngx_stream_null_variable

0 commit comments

Comments
 (0)