Skip to content

Commit d130d96

Browse files
authored
feature: shared ngx.ctx among SSL_* phases and the following phases. (#243)
1 parent 464121e commit d130d96

File tree

3 files changed

+1075
-22
lines changed

3 files changed

+1075
-22
lines changed

lib/resty/core/ctx.lua

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ local get_request = base.get_request
1717
local FFI_NO_REQ_CTX = base.FFI_NO_REQ_CTX
1818
local FFI_OK = base.FFI_OK
1919
local error = error
20+
local setmetatable = setmetatable
2021
local subsystem = ngx.config.subsystem
2122

2223

@@ -26,7 +27,8 @@ local ngx_lua_ffi_set_ctx_ref
2627

2728
if subsystem == "http" then
2829
ffi.cdef[[
29-
int ngx_http_lua_ffi_get_ctx_ref(ngx_http_request_t *r);
30+
int ngx_http_lua_ffi_get_ctx_ref(ngx_http_request_t *r, int *in_ssl_phase,
31+
int *ssl_ctx_ref);
3032
int ngx_http_lua_ffi_set_ctx_ref(ngx_http_request_t *r, int ref);
3133
]]
3234

@@ -35,7 +37,8 @@ if subsystem == "http" then
3537

3638
elseif subsystem == "stream" then
3739
ffi.cdef[[
38-
int ngx_stream_lua_ffi_get_ctx_ref(ngx_stream_lua_request_t *r);
40+
int ngx_stream_lua_ffi_get_ctx_ref(ngx_stream_lua_request_t *r,
41+
int *in_ssl_phase, int *ssl_ctx_ref);
3942
int ngx_stream_lua_ffi_set_ctx_ref(ngx_stream_lua_request_t *r, int ref);
4043
]]
4144

@@ -49,28 +52,56 @@ local _M = {
4952
}
5053

5154

52-
local function get_ctx_table()
53-
local r = get_request()
55+
local get_ctx_table
56+
do
57+
local in_ssl_phase = ffi.new("int[1]")
58+
local ssl_ctx_ref = ffi.new("int[1]")
5459

55-
if not r then
56-
error("no request found")
57-
end
60+
function get_ctx_table()
61+
local r = get_request()
5862

59-
local ctx_ref = ngx_lua_ffi_get_ctx_ref(r)
60-
if ctx_ref == FFI_NO_REQ_CTX then
61-
error("no request ctx found")
62-
end
63+
if not r then
64+
error("no request found")
65+
end
6366

64-
local ctxs = registry.ngx_lua_ctx_tables
65-
if ctx_ref < 0 then
66-
local ctx = new_tab(0, 4)
67-
ctx_ref = ref_in_table(ctxs, ctx)
68-
if ngx_lua_ffi_set_ctx_ref(r, ctx_ref) ~= FFI_OK then
69-
return nil
67+
local ctx_ref = ngx_lua_ffi_get_ctx_ref(r, in_ssl_phase, ssl_ctx_ref)
68+
if ctx_ref == FFI_NO_REQ_CTX then
69+
error("no request ctx found")
70+
end
71+
72+
local ctxs = registry.ngx_lua_ctx_tables
73+
if ctx_ref < 0 then
74+
local ctx
75+
76+
ctx_ref = ssl_ctx_ref[0]
77+
if ctx_ref > 0 and ctxs[ctx_ref] then
78+
if in_ssl_phase[0] ~= 0 then
79+
return ctxs[ctx_ref]
80+
end
81+
82+
ctx = new_tab(0, 4)
83+
ctx = setmetatable(ctx, ctxs[ctx_ref])
84+
85+
else
86+
if in_ssl_phase[0] ~= 0 then
87+
ctx = new_tab(1, 4)
88+
-- to avoid creating another table, we assume the users
89+
-- won't overwrite the `__index` key
90+
ctx.__index = ctx
91+
92+
else
93+
ctx = new_tab(0, 4)
94+
end
95+
end
96+
97+
ctx_ref = ref_in_table(ctxs, ctx)
98+
if ngx_lua_ffi_set_ctx_ref(r, ctx_ref) ~= FFI_OK then
99+
return nil
100+
end
101+
return ctx
70102
end
71-
return ctx
103+
return ctxs[ctx_ref]
72104
end
73-
return ctxs[ctx_ref]
74105
end
75106
register_getter("ctx", get_ctx_table)
76107

@@ -82,7 +113,7 @@ local function set_ctx_table(ctx)
82113
error("no request found")
83114
end
84115

85-
local ctx_ref = ngx_lua_ffi_get_ctx_ref(r)
116+
local ctx_ref = ngx_lua_ffi_get_ctx_ref(r, nil, nil)
86117
if ctx_ref == FFI_NO_REQ_CTX then
87118
error("no request ctx found")
88119
end

0 commit comments

Comments
 (0)