From 99ac3d28eb35c382cec4010664a8bbcfebd3263d Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 10 Oct 2023 17:00:46 +0800 Subject: [PATCH] ngx_http_lua_kong_request_id.c --- config | 1 + src/ngx_http_lua_kong_request_id.c | 50 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/ngx_http_lua_kong_request_id.c diff --git a/config b/config index 8423d392..c4bd5154 100644 --- a/config +++ b/config @@ -8,6 +8,7 @@ ngx_module_srcs=" \ $ngx_addon_dir/src/ngx_http_lua_kong_module.c \ $ngx_addon_dir/src/ngx_http_lua_kong_log.c \ $ngx_addon_dir/src/ngx_http_lua_kong_log_handler.c \ + $ngx_addon_dir/src/ngx_http_lua_kong_request_id.c \ $ngx_addon_dir/src/ssl/ngx_lua_kong_ssl.c \ " diff --git a/src/ngx_http_lua_kong_request_id.c b/src/ngx_http_lua_kong_request_id.c new file mode 100644 index 00000000..94245ab0 --- /dev/null +++ b/src/ngx_http_lua_kong_request_id.c @@ -0,0 +1,50 @@ +/** + * Copyright 2019-2023 Kong Inc. + + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + + * http://www.apache.org/licenses/LICENSE-2.0 + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "ngx_http_lua_kong_common.h" + + +#define KONG_REQUEST_ID_MAX_LEN 32 +#define KONG_REQUEST_ID_FORMAT "%08xD%08xD%08xD%08xD" + + +static ngx_int_t +ngx_http_lua_kong_variable_request_id(ngx_http_request_t *r, + ngx_http_variable_value_t *v, uintptr_t data) +{ + u_char *id; + + id = ngx_pnalloc(r->pool, KONG_REQUEST_ID_MAX_LEN); + if (id == NULL) { + return NGX_ERROR; + } + + v->valid = 1; + v->no_cacheable = 0; + v->not_found = 0; + + v->len = KONG_REQUEST_ID_MAX_LEN; + v->data = id; + + ngx_sprintf(id, KONG_REQUEST_ID_FORMAT, + (uint32_t) ngx_random(), (uint32_t) ngx_random(), + (uint32_t) ngx_random(), (uint32_t) ngx_random()); + + return NGX_OK; +} + +