mirror of
https://gitee.com/iresty/apisix.git
synced 2024-12-04 21:17:36 +08:00
core: support get_scheme/host/port/http_version in core.request (#1978)
This commit is contained in:
parent
bb062785d5
commit
6adbf02a06
@ -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
|
||||
|
148
t/core/request.t
148
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]
|
||||
|
Loading…
Reference in New Issue
Block a user