|
| 1 | +/** |
| 2 | + * Copyright 2019-2023 Kong Inc. |
| 3 | +
|
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | +
|
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +#include "ngx_http_lua_kong_common.h" |
| 19 | + |
| 20 | + |
| 21 | +#define NGX_HTTP_LUA_KONG_RANDOM_COUNT 4 |
| 22 | +#define NGX_HTTP_LUA_KONG_UINT32_HEX_LEN sizeof(uint32_t) * 2 |
| 23 | + |
| 24 | + |
| 25 | +static ngx_int_t |
| 26 | +ngx_http_lua_kong_variable_request_id(ngx_http_request_t *r, |
| 27 | + ngx_http_variable_value_t *v, uintptr_t data) |
| 28 | +{ |
| 29 | + u_char *id; |
| 30 | + uint32_t i, rnd; |
| 31 | + |
| 32 | + id = ngx_pnalloc(r->pool, |
| 33 | + NGX_HTTP_LUA_KONG_RANDOM_COUNT * |
| 34 | + NGX_HTTP_LUA_KONG_UINT32_HEX_LEN); |
| 35 | + if (id == NULL) { |
| 36 | + return NGX_ERROR; |
| 37 | + } |
| 38 | + |
| 39 | + v->valid = 1; |
| 40 | + v->no_cacheable = 0; |
| 41 | + v->not_found = 0; |
| 42 | + |
| 43 | + v->len = NGX_HTTP_LUA_KONG_RANDOM_COUNT * |
| 44 | + NGX_HTTP_LUA_KONG_UINT32_HEX_LEN; |
| 45 | + v->data = id; |
| 46 | + |
| 47 | + for (i = 0; i < NGX_HTTP_LUA_KONG_RANDOM_COUNT; i++) { |
| 48 | + rnd = (uint32_t) ngx_random(); |
| 49 | + id = ngx_hex_dump(id, (u_char *) &rnd, sizeof(uint32_t)); |
| 50 | + } |
| 51 | + |
| 52 | + return NGX_OK; |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +static ngx_http_variable_t ngx_http_lua_kong_variables[] = { |
| 57 | + |
| 58 | + { ngx_string("kong_request_id"), NULL, |
| 59 | + ngx_http_lua_kong_variable_request_id, |
| 60 | + 0, 0, 0 }, |
| 61 | + |
| 62 | + ngx_http_null_variable |
| 63 | +}; |
| 64 | + |
| 65 | + |
| 66 | +ngx_int_t |
| 67 | +ngx_http_lua_kong_add_vars(ngx_conf_t *cf) |
| 68 | +{ |
| 69 | + ngx_http_variable_t *cv, *v; |
| 70 | + |
| 71 | + for (cv = ngx_http_lua_kong_variables; cv->name.len; cv++) { |
| 72 | + v = ngx_http_add_variable(cf, &cv->name, cv->flags); |
| 73 | + if (v == NULL) { |
| 74 | + return NGX_ERROR; |
| 75 | + } |
| 76 | + |
| 77 | + *v = *cv; |
| 78 | + } |
| 79 | + |
| 80 | + return NGX_OK; |
| 81 | +} |
0 commit comments