From c6a9451f5ba294a3036f3a07b61937b18d3672ea Mon Sep 17 00:00:00 2001 From: jinjiu Date: Wed, 26 Jun 2019 15:40:44 +0800 Subject: [PATCH 1/2] Add variable $ssl_handshake_time and $ssl_handshake_time_msec to get ssl handshake time. $ssl_shandshakd_time deprecated and will be removed in the next release. --- src/event/ngx_event_openssl.c | 36 ++++++++++++++++++++++++++ src/event/ngx_event_openssl.h | 2 ++ src/http/modules/ngx_http_ssl_module.c | 7 +++++ src/stream/ngx_stream_ssl_module.c | 7 +++++ 4 files changed, 52 insertions(+) diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c index 22dd80f8e4..bf8a5e57df 100755 --- a/src/event/ngx_event_openssl.c +++ b/src/event/ngx_event_openssl.c @@ -5461,6 +5461,42 @@ ngx_ssl_get_handshake_time(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s) return NGX_ERROR; } + s->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p; + s->data = p; + + return NGX_OK; +} + + +ngx_int_t +ngx_ssl_get_handshake_time_msec(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s) +{ + ngx_msec_int_t ms; + u_char *p; + ngx_time_t *tp; + + if (c->ssl == NULL) { + ngx_str_null(s); + + return NGX_OK; + } + + tp = ngx_timeofday(); + + if (c->ssl->handshake_end_msec == 0) { + ms = tp->sec * 1000 + tp->sec - c->ssl->handshake_start_msec; + + } else { + ms = c->ssl->handshake_end_msec - c->ssl->handshake_start_msec; + } + + ms = ngx_max(ms, 0); + + p = ngx_pnalloc(pool, NGX_TIME_T_LEN); + if (p == NULL) { + return NGX_ERROR; + } + s->len = ngx_sprintf(p, "%i", ms) - p; s->data = p; diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h index 1dfb12d86c..06507ab22a 100755 --- a/src/event/ngx_event_openssl.h +++ b/src/event/ngx_event_openssl.h @@ -276,6 +276,8 @@ ngx_int_t ngx_ssl_get_client_v_remain(ngx_connection_t *c, ngx_pool_t *pool, #if (T_NGX_SSL_HANDSHAKE_TIME) ngx_int_t ngx_ssl_get_handshake_time(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s); +ngx_int_t ngx_ssl_get_handshake_time_msec(ngx_connection_t *c, ngx_pool_t *pool, + ngx_str_t *s); #endif diff --git a/src/http/modules/ngx_http_ssl_module.c b/src/http/modules/ngx_http_ssl_module.c index 107fac0224..26ee476974 100755 --- a/src/http/modules/ngx_http_ssl_module.c +++ b/src/http/modules/ngx_http_ssl_module.c @@ -385,8 +385,15 @@ static ngx_http_variable_t ngx_http_ssl_vars[] = { (uintptr_t) ngx_ssl_get_client_v_remain, NGX_HTTP_VAR_CHANGEABLE, 0 }, #if (T_NGX_SSL_HANDSHAKE_TIME) + /* $ssl_shandshakd_time deprecated and will be removed in the next release */ { ngx_string("ssl_handshakd_time"), NULL, ngx_http_ssl_variable, (uintptr_t) ngx_ssl_get_handshake_time, NGX_HTTP_VAR_CHANGEABLE, 0 }, + + { ngx_string("ssl_handshake_time"), NULL, ngx_http_ssl_variable, + (uintptr_t) ngx_ssl_get_handshake_time, NGX_HTTP_VAR_CHANGEABLE, 0 }, + + { ngx_string("ssl_handshake_time_msec"), NULL, ngx_http_ssl_variable, + (uintptr_t) ngx_ssl_get_handshake_time_msec, NGX_HTTP_VAR_CHANGEABLE, 0 }, #endif ngx_http_null_variable diff --git a/src/stream/ngx_stream_ssl_module.c b/src/stream/ngx_stream_ssl_module.c index 4069bf59f4..b6f328b86d 100755 --- a/src/stream/ngx_stream_ssl_module.c +++ b/src/stream/ngx_stream_ssl_module.c @@ -301,8 +301,15 @@ static ngx_stream_variable_t ngx_stream_ssl_vars[] = { (uintptr_t) ngx_ssl_get_client_v_remain, NGX_STREAM_VAR_CHANGEABLE, 0 }, #if (T_NGX_SSL_HANDSHAKE_TIME) + /* $ssl_shandshakd_time deprecated and will be removed in the next release */ { ngx_string("ssl_handshakd_time"), NULL, ngx_stream_ssl_variable, (uintptr_t) ngx_ssl_get_handshake_time, NGX_STREAM_VAR_CHANGEABLE, 0 }, + + { ngx_string("ssl_handshake_time"), NULL, ngx_http_ssl_variable, + (uintptr_t) ngx_ssl_get_handshake_time, NGX_HTTP_VAR_CHANGEABLE, 0 }, + + { ngx_string("ssl_handshake_time_msec"), NULL, ngx_stream_ssl_variable, + (uintptr_t) ngx_ssl_get_handshake_time_msec, NGX_STREAM_VAR_CHANGEABLE, 0 }, #endif ngx_stream_null_variable From 6a8fa7935a54771ec569fe66fdfb8d51a2770443 Mon Sep 17 00:00:00 2001 From: jinjiu Date: Wed, 26 Jun 2019 16:34:29 +0800 Subject: [PATCH 2/2] $ssl_handshake_time bugfix --- src/event/ngx_event_openssl.c | 4 ++-- src/stream/ngx_stream_ssl_module.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c index bf8a5e57df..51aa9ce0b3 100755 --- a/src/event/ngx_event_openssl.c +++ b/src/event/ngx_event_openssl.c @@ -5448,7 +5448,7 @@ ngx_ssl_get_handshake_time(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s) tp = ngx_timeofday(); if (c->ssl->handshake_end_msec == 0) { - ms = tp->sec * 1000 + tp->sec - c->ssl->handshake_start_msec; + ms = tp->sec * 1000 + tp->msec - c->ssl->handshake_start_msec; } else { ms = c->ssl->handshake_end_msec - c->ssl->handshake_start_msec; @@ -5484,7 +5484,7 @@ ngx_ssl_get_handshake_time_msec(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t tp = ngx_timeofday(); if (c->ssl->handshake_end_msec == 0) { - ms = tp->sec * 1000 + tp->sec - c->ssl->handshake_start_msec; + ms = tp->sec * 1000 + tp->msec - c->ssl->handshake_start_msec; } else { ms = c->ssl->handshake_end_msec - c->ssl->handshake_start_msec; diff --git a/src/stream/ngx_stream_ssl_module.c b/src/stream/ngx_stream_ssl_module.c index b6f328b86d..312fb5698f 100755 --- a/src/stream/ngx_stream_ssl_module.c +++ b/src/stream/ngx_stream_ssl_module.c @@ -305,8 +305,8 @@ static ngx_stream_variable_t ngx_stream_ssl_vars[] = { { ngx_string("ssl_handshakd_time"), NULL, ngx_stream_ssl_variable, (uintptr_t) ngx_ssl_get_handshake_time, NGX_STREAM_VAR_CHANGEABLE, 0 }, - { ngx_string("ssl_handshake_time"), NULL, ngx_http_ssl_variable, - (uintptr_t) ngx_ssl_get_handshake_time, NGX_HTTP_VAR_CHANGEABLE, 0 }, + { ngx_string("ssl_handshake_time"), NULL, ngx_stream_ssl_variable, + (uintptr_t) ngx_ssl_get_handshake_time, NGX_STREAM_VAR_CHANGEABLE, 0 }, { ngx_string("ssl_handshake_time_msec"), NULL, ngx_stream_ssl_variable, (uintptr_t) ngx_ssl_get_handshake_time_msec, NGX_STREAM_VAR_CHANGEABLE, 0 },