aboutsummaryrefslogtreecommitdiff
path: root/src/http/ngx_http_variables.c
diff options
context:
space:
mode:
authorVladimir Homutov <vl@nginx.com>2016-04-26 19:31:46 +0300
committerVladimir Homutov <vl@nginx.com>2016-04-26 19:31:46 +0300
commitf315b7a924fa2c0da69c21078c930391a424aeef (patch)
tree0ccd5ca4bd425cfd46ec316e26d83cd2e7b11a66 /src/http/ngx_http_variables.c
parentbe79f5cb16eeb452e2a9e343a89f89b3b47bc5a2 (diff)
downloadnginx-f315b7a924fa2c0da69c21078c930391a424aeef.tar.gz
nginx-f315b7a924fa2c0da69c21078c930391a424aeef.zip
Variable $request_id.
The variable contains text representation based on random data, usable as a unique request identifier.
Diffstat (limited to 'src/http/ngx_http_variables.c')
-rw-r--r--src/http/ngx_http_variables.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
index f8271ab6d..24d169ca0 100644
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -98,6 +98,8 @@ static ngx_int_t ngx_http_variable_request_length(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_request_time(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_request_id(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_status(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
@@ -274,6 +276,10 @@ static ngx_http_variable_t ngx_http_core_variables[] = {
{ ngx_string("request_time"), NULL, ngx_http_variable_request_time,
0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+ { ngx_string("request_id"), NULL,
+ ngx_http_variable_request_id,
+ 0, 0, 0 },
+
{ ngx_string("status"), NULL,
ngx_http_variable_status, 0,
NGX_HTTP_VAR_NOCACHEABLE, 0 },
@@ -2068,6 +2074,47 @@ ngx_http_variable_request_time(ngx_http_request_t *r,
static ngx_int_t
+ngx_http_variable_request_id(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ u_char *id;
+
+#if (NGX_OPENSSL)
+ u_char random_bytes[16];
+#endif
+
+ id = ngx_pnalloc(r->pool, 32);
+ if (id == NULL) {
+ return NGX_ERROR;
+ }
+
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+
+ v->len = 32;
+ v->data = id;
+
+#if (NGX_OPENSSL)
+
+ if (RAND_bytes(random_bytes, 16) == 1) {
+ ngx_hex_dump(id, random_bytes, 16);
+ return NGX_OK;
+ }
+
+ ngx_ssl_error(NGX_LOG_ERR, r->connection->log, 0, "RAND_bytes() failed");
+
+#endif
+
+ ngx_sprintf(id, "%08xD%08xD%08xD%08xD",
+ (uint32_t) ngx_random(), (uint32_t) ngx_random(),
+ (uint32_t) ngx_random(), (uint32_t) ngx_random());
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
ngx_http_variable_connection(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{