comparison src/http/ngx_http_variables.c @ 6531:59f8f2dd8b31

Variable $request_id. The variable contains text representation based on random data, usable as a unique request identifier.
author Vladimir Homutov <vl@nginx.com>
date Tue, 26 Apr 2016 19:31:46 +0300
parents 3ef7bb882ad4
children 28c76d9d75b7
comparison
equal deleted inserted replaced
6530:1d0e03db9f8e 6531:59f8f2dd8b31
95 static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r, 95 static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r,
96 ngx_http_variable_value_t *v, uintptr_t data); 96 ngx_http_variable_value_t *v, uintptr_t data);
97 static ngx_int_t ngx_http_variable_request_length(ngx_http_request_t *r, 97 static ngx_int_t ngx_http_variable_request_length(ngx_http_request_t *r,
98 ngx_http_variable_value_t *v, uintptr_t data); 98 ngx_http_variable_value_t *v, uintptr_t data);
99 static ngx_int_t ngx_http_variable_request_time(ngx_http_request_t *r, 99 static ngx_int_t ngx_http_variable_request_time(ngx_http_request_t *r,
100 ngx_http_variable_value_t *v, uintptr_t data);
101 static ngx_int_t ngx_http_variable_request_id(ngx_http_request_t *r,
100 ngx_http_variable_value_t *v, uintptr_t data); 102 ngx_http_variable_value_t *v, uintptr_t data);
101 static ngx_int_t ngx_http_variable_status(ngx_http_request_t *r, 103 static ngx_int_t ngx_http_variable_status(ngx_http_request_t *r,
102 ngx_http_variable_value_t *v, uintptr_t data); 104 ngx_http_variable_value_t *v, uintptr_t data);
103 105
104 static ngx_int_t ngx_http_variable_sent_content_type(ngx_http_request_t *r, 106 static ngx_int_t ngx_http_variable_sent_content_type(ngx_http_request_t *r,
272 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, 274 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
273 275
274 { ngx_string("request_time"), NULL, ngx_http_variable_request_time, 276 { ngx_string("request_time"), NULL, ngx_http_variable_request_time,
275 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, 277 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
276 278
279 { ngx_string("request_id"), NULL,
280 ngx_http_variable_request_id,
281 0, 0, 0 },
282
277 { ngx_string("status"), NULL, 283 { ngx_string("status"), NULL,
278 ngx_http_variable_status, 0, 284 ngx_http_variable_status, 0,
279 NGX_HTTP_VAR_NOCACHEABLE, 0 }, 285 NGX_HTTP_VAR_NOCACHEABLE, 0 },
280 286
281 { ngx_string("sent_http_content_type"), NULL, 287 { ngx_string("sent_http_content_type"), NULL,
2060 v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p; 2066 v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p;
2061 v->valid = 1; 2067 v->valid = 1;
2062 v->no_cacheable = 0; 2068 v->no_cacheable = 0;
2063 v->not_found = 0; 2069 v->not_found = 0;
2064 v->data = p; 2070 v->data = p;
2071
2072 return NGX_OK;
2073 }
2074
2075
2076 static ngx_int_t
2077 ngx_http_variable_request_id(ngx_http_request_t *r,
2078 ngx_http_variable_value_t *v, uintptr_t data)
2079 {
2080 u_char *id;
2081
2082 #if (NGX_OPENSSL)
2083 u_char random_bytes[16];
2084 #endif
2085
2086 id = ngx_pnalloc(r->pool, 32);
2087 if (id == NULL) {
2088 return NGX_ERROR;
2089 }
2090
2091 v->valid = 1;
2092 v->no_cacheable = 0;
2093 v->not_found = 0;
2094
2095 v->len = 32;
2096 v->data = id;
2097
2098 #if (NGX_OPENSSL)
2099
2100 if (RAND_bytes(random_bytes, 16) == 1) {
2101 ngx_hex_dump(id, random_bytes, 16);
2102 return NGX_OK;
2103 }
2104
2105 ngx_ssl_error(NGX_LOG_ERR, r->connection->log, 0, "RAND_bytes() failed");
2106
2107 #endif
2108
2109 ngx_sprintf(id, "%08xD%08xD%08xD%08xD",
2110 (uint32_t) ngx_random(), (uint32_t) ngx_random(),
2111 (uint32_t) ngx_random(), (uint32_t) ngx_random());
2065 2112
2066 return NGX_OK; 2113 return NGX_OK;
2067 } 2114 }
2068 2115
2069 2116