From 6adbf02a068c8c9c5a6279add121e9b7bc773d3e Mon Sep 17 00:00:00 2001 From: taotao <20034838@qq.com> Date: Tue, 4 Aug 2020 09:36:05 +0800 Subject: [PATCH] core: support get_scheme/host/port/http_version in core.request (#1978) --- apisix/core/request.lua | 28 ++++++++ t/core/request.t | 148 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) diff --git a/apisix/core/request.lua b/apisix/core/request.lua index 7fe8cdd9..fcf6e071 100644 --- a/apisix/core/request.lua +++ b/apisix/core/request.lua @@ -146,4 +146,32 @@ function _M.get_body(max_size) end +function _M.get_scheme(ctx) + if not ctx then + ctx = ngx.ctx.api_ctx + end + return ctx.var.scheme or '' +end + + +function _M.get_host(ctx) + if not ctx then + ctx = ngx.ctx.api_ctx + end + return ctx.var.host or '' +end + + +function _M.get_port(ctx) + if not ctx then + ctx = ngx.ctx.api_ctx + end + return tonumber(ctx.var.server_port) +end + + +function _M.get_http_version() + return ngx.req.http_version() +end + return _M diff --git a/t/core/request.t b/t/core/request.t index 2fe9cd53..5f6fd7ee 100644 --- a/t/core/request.t +++ b/t/core/request.t @@ -206,3 +206,151 @@ X-Forwarded-For: 10.0.0.1 10.0.0.1 --- no_error_log [error] + + + +=== TEST 6: get_host +--- config + location = /hello { + real_ip_header X-Real-IP; + + set_real_ip_from 0.0.0.0/0; + set_real_ip_from ::/0; + set_real_ip_from unix:; + + access_by_lua_block { + local core = require("apisix.core") + local ngx_ctx = ngx.ctx + local api_ctx = ngx_ctx.api_ctx + if api_ctx == nil then + api_ctx = core.tablepool.fetch("api_ctx", 0, 32) + ngx_ctx.api_ctx = api_ctx + end + + core.ctx.set_vars_meta(api_ctx) + } + content_by_lua_block { + local core = require("apisix.core") + local host = core.request.get_host(ngx.ctx.api_ctx) + ngx.say(host) + } + } +--- request +GET /hello +--- more_headers +X-Real-IP: 10.0.0.1 +--- response_body +localhost +--- no_error_log +[error] + + + +=== TEST 7: get_scheme +--- config + location = /hello { + real_ip_header X-Real-IP; + + set_real_ip_from 0.0.0.0/0; + set_real_ip_from ::/0; + set_real_ip_from unix:; + + access_by_lua_block { + local core = require("apisix.core") + local ngx_ctx = ngx.ctx + local api_ctx = ngx_ctx.api_ctx + if api_ctx == nil then + api_ctx = core.tablepool.fetch("api_ctx", 0, 32) + ngx_ctx.api_ctx = api_ctx + end + + core.ctx.set_vars_meta(api_ctx) + } + content_by_lua_block { + local core = require("apisix.core") + local scheme = core.request.get_scheme(ngx.ctx.api_ctx) + ngx.say(scheme) + } + } +--- request +GET /hello +--- more_headers +X-Real-IP: 10.0.0.1 +--- response_body +http +--- no_error_log +[error] + + + +=== TEST 8: get_port +--- config + location = /hello { + real_ip_header X-Real-IP; + + set_real_ip_from 0.0.0.0/0; + set_real_ip_from ::/0; + set_real_ip_from unix:; + + access_by_lua_block { + local core = require("apisix.core") + local ngx_ctx = ngx.ctx + local api_ctx = ngx_ctx.api_ctx + if api_ctx == nil then + api_ctx = core.tablepool.fetch("api_ctx", 0, 32) + ngx_ctx.api_ctx = api_ctx + end + + core.ctx.set_vars_meta(api_ctx) + } + content_by_lua_block { + local core = require("apisix.core") + local port = core.request.get_port(ngx.ctx.api_ctx) + ngx.say(port) + } + } +--- request +GET /hello +--- more_headers +X-Real-IP: 10.0.0.1 +--- response_body +1984 +--- no_error_log +[error] + + + +=== TEST 9: get_http_version +--- config + location = /hello { + real_ip_header X-Real-IP; + + set_real_ip_from 0.0.0.0/0; + set_real_ip_from ::/0; + set_real_ip_from unix:; + + access_by_lua_block { + local core = require("apisix.core") + local ngx_ctx = ngx.ctx + local api_ctx = ngx_ctx.api_ctx + if api_ctx == nil then + api_ctx = core.tablepool.fetch("api_ctx", 0, 32) + ngx_ctx.api_ctx = api_ctx + end + + core.ctx.set_vars_meta(api_ctx) + } + content_by_lua_block { + local core = require("apisix.core") + local http_version = core.request.get_http_version() + ngx.say(http_version) + } + } +--- request +GET /hello +--- more_headers +X-Real-IP: 10.0.0.1 +--- response_body +1.1 +--- no_error_log +[error]