Skip to content

Commit

Permalink
feature: supported to fetch cookie value from ctx.var which is a easy…
Browse files Browse the repository at this point in the history
… way. (#449)

* feature: supported to fetch cookie value from ctx.var which is a easy way.
  • Loading branch information
membphis authored Aug 29, 2019
1 parent c32da7c commit 124a49f
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 5 deletions.
32 changes: 27 additions & 5 deletions lua/apisix/core/ctx.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
local tablepool = require("tablepool")
local get_var = require("resty.ngxvar").fetch
local get_request = require("resty.ngxvar").request
local log = require("apisix.core.log")
local tablepool = require("tablepool")
local get_var = require("resty.ngxvar").fetch
local get_request = require("resty.ngxvar").request
local ck = require "resty.cookie"
local setmetatable = setmetatable
local ffi = require("ffi")
local C = ffi.C
local sub_str = string.sub


local _M = {version = 0.1}
ffi.cdef[[
int memcmp(const void *s1, const void *s2, size_t n);
]]


local _M = {version = 0.2}


do
local var_methods = {
["method"] = ngx.req.get_method
["method"] = ngx.req.get_method,
["cookie"] = function () return ck:new() end
}

local mt = {
Expand All @@ -19,6 +30,17 @@ do
if method then
val = method()

elseif C.memcmp(name, "cookie_", 7) == 0 then
local cookie = t["cookie"]
if cookie then
local err
val, err = cookie:get(sub_str(name, 8))
if not val then
log.warn("failed to fetch cookie value by name: ",
name, " error: ", err)
end
end

else
val = get_var(name, t._request)
end
Expand Down
99 changes: 99 additions & 0 deletions t/core/ctx.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use t::APISix 'no_plan';

repeat_each(2);
no_long_string();
no_root_location();

run_tests;

__DATA__

=== TEST 1: sanity
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local ctx = {}
core.ctx.set_vars_meta(ctx)

ngx.say("remote_addr: ", ctx.var["remote_addr"])
ngx.say("server_port: ", ctx.var["server_port"])
}
}
--- request
GET /t
--- response_body
remote_addr: 127.0.0.1
server_port: 1984
--- no_error_log
[error]



=== TEST 2: http header + arg
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local ctx = {}
core.ctx.set_vars_meta(ctx)

ngx.say("http_host: ", ctx.var["http_host"])
ngx.say("arg_a: ", ctx.var["arg_a"])
}
}
--- request
GET /t?a=aaa
--- response_body
http_host: localhost
arg_a: aaa
--- no_error_log
[error]



=== TEST 3: cookie + no cookie
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local ctx = {}
core.ctx.set_vars_meta(ctx)

ngx.say("cookie_host: ", ctx.var["cookie_host"])
}
}
--- request
GET /t?a=aaa
--- response_body
cookie_host: nil
--- error_log
failed to fetch cookie value by name: cookie_host error: no cookie found in the current request



=== TEST 4: cookie
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local ctx = {}
core.ctx.set_vars_meta(ctx)

ngx.say("cookie_a: ", ctx.var["cookie_a"])
ngx.say("cookie_b: ", ctx.var["cookie_b"])
ngx.say("cookie_c: ", ctx.var["cookie_c"])
ngx.say("cookie_d: ", ctx.var["cookie_d"])
}
}
--- more_headers
Cookie: a=a; b=bb; c=ccc
--- request
GET /t?a=aaa
--- response_body
cookie_a: a
cookie_b: bb
cookie_c: ccc
cookie_d: nil
--- no_error_log
[error]

0 comments on commit 124a49f

Please sign in to comment.