This is an automated email from the ASF dual-hosted git repository.

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 6adbf02  core: support get_scheme/host/port/http_version in 
core.request  (#1978)
6adbf02 is described below

commit 6adbf02a068c8c9c5a6279add121e9b7bc773d3e
Author: taotao <20034...@qq.com>
AuthorDate: Tue Aug 4 09:36:05 2020 +0800

    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 7fe8cdd..fcf6e07 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 2fe9cd5..5f6fd7e 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]

Reply via email to