@@ -17,6 +17,7 @@ local get_request = base.get_request
17
17
local FFI_NO_REQ_CTX = base .FFI_NO_REQ_CTX
18
18
local FFI_OK = base .FFI_OK
19
19
local error = error
20
+ local setmetatable = setmetatable
20
21
local subsystem = ngx .config .subsystem
21
22
22
23
@@ -26,7 +27,8 @@ local ngx_lua_ffi_set_ctx_ref
26
27
27
28
if subsystem == " http" then
28
29
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 );
30
32
int ngx_http_lua_ffi_set_ctx_ref (ngx_http_request_t * r , int ref );
31
33
]]
32
34
@@ -35,7 +37,8 @@ if subsystem == "http" then
35
37
36
38
elseif subsystem == " stream" then
37
39
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 );
39
42
int ngx_stream_lua_ffi_set_ctx_ref (ngx_stream_lua_request_t * r , int ref );
40
43
]]
41
44
@@ -49,28 +52,56 @@ local _M = {
49
52
}
50
53
51
54
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]" )
54
59
55
- if not r then
56
- error (" no request found" )
57
- end
60
+ function get_ctx_table ()
61
+ local r = get_request ()
58
62
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
63
66
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
70
102
end
71
- return ctx
103
+ return ctxs [ ctx_ref ]
72
104
end
73
- return ctxs [ctx_ref ]
74
105
end
75
106
register_getter (" ctx" , get_ctx_table )
76
107
@@ -82,7 +113,7 @@ local function set_ctx_table(ctx)
82
113
error (" no request found" )
83
114
end
84
115
85
- local ctx_ref = ngx_lua_ffi_get_ctx_ref (r )
116
+ local ctx_ref = ngx_lua_ffi_get_ctx_ref (r , nil , nil )
86
117
if ctx_ref == FFI_NO_REQ_CTX then
87
118
error (" no request ctx found" )
88
119
end
0 commit comments