1919
2020
2121#include "ngx_stream_lua_util.h"
22+ #include "ngx_stream_lua_ssl.h"
2223#include "ngx_stream_lua_ctx.h"
2324
2425
@@ -29,14 +30,16 @@ typedef struct {
2930
3031
3132static ngx_int_t ngx_stream_lua_ngx_ctx_add_cleanup (ngx_stream_lua_request_t * r ,
32- int ref );
33+ ngx_pool_t * pool , int ref );
3334static void ngx_stream_lua_ngx_ctx_cleanup (void * data );
3435
3536
3637int
3738ngx_stream_lua_ngx_set_ctx_helper (lua_State * L , ngx_stream_lua_request_t * r ,
3839 ngx_stream_lua_ctx_t * ctx , int index )
3940{
41+ ngx_pool_t * pool ;
42+
4043 if (index < 0 ) {
4144 index = lua_gettop (L ) + index + 1 ;
4245 }
@@ -51,7 +54,8 @@ ngx_stream_lua_ngx_set_ctx_helper(lua_State *L, ngx_stream_lua_request_t *r,
5154 ctx -> ctx_ref = luaL_ref (L , -2 );
5255 lua_pop (L , 1 );
5356
54- if (ngx_stream_lua_ngx_ctx_add_cleanup (r , ctx -> ctx_ref ) != NGX_OK ) {
57+ pool = r -> pool ;
58+ if (ngx_stream_lua_ngx_ctx_add_cleanup (r , pool , ctx -> ctx_ref ) != NGX_OK ) {
5559 return luaL_error (L , "no memory" );
5660 }
5761
@@ -74,32 +78,66 @@ ngx_stream_lua_ngx_set_ctx_helper(lua_State *L, ngx_stream_lua_request_t *r,
7478
7579
7680int
77- ngx_stream_lua_ffi_get_ctx_ref (ngx_stream_lua_request_t * r )
81+ ngx_stream_lua_ffi_get_ctx_ref (ngx_stream_lua_request_t * r , int * in_ssl_phase ,
82+ int * ssl_ctx_ref )
7883{
79- ngx_stream_lua_ctx_t * ctx ;
84+ ngx_stream_lua_ctx_t * ctx ;
85+ ngx_stream_lua_ssl_ctx_t * ssl_ctx ;
8086
8187 ctx = ngx_stream_lua_get_module_ctx (r , ngx_stream_lua_module );
8288 if (ctx == NULL ) {
8389 return NGX_STREAM_LUA_FFI_NO_REQ_CTX ;
8490 }
8591
86- return ctx -> ctx_ref ;
92+ if (ctx -> ctx_ref >= 0 || in_ssl_phase == NULL ) {
93+ return ctx -> ctx_ref ;
94+ }
95+
96+ * in_ssl_phase = ctx -> context & NGX_STREAM_LUA_CONTEXT_SSL_CERT ;
97+ * ssl_ctx_ref = LUA_NOREF ;
98+
99+ if (r -> connection -> ssl != NULL ) {
100+ ssl_ctx = ngx_stream_lua_ssl_get_ctx (r -> connection -> ssl -> connection );
101+
102+ if (ssl_ctx != NULL ) {
103+ * ssl_ctx_ref = ssl_ctx -> ctx_ref ;
104+ }
105+ }
106+
107+ return LUA_NOREF ;
87108}
88109
89110
90111int
91112ngx_stream_lua_ffi_set_ctx_ref (ngx_stream_lua_request_t * r , int ref )
92113{
93- ngx_stream_lua_ctx_t * ctx ;
114+ ngx_pool_t * pool ;
115+ ngx_connection_t * c ;
116+ ngx_stream_lua_ctx_t * ctx ;
117+ ngx_stream_lua_ssl_ctx_t * ssl_ctx ;
94118
95119 ctx = ngx_stream_lua_get_module_ctx (r , ngx_stream_lua_module );
96120 if (ctx == NULL ) {
97121 return NGX_STREAM_LUA_FFI_NO_REQ_CTX ;
98122 }
99123
124+ if (ctx -> context & NGX_STREAM_LUA_CONTEXT_SSL_CERT ) {
125+ ssl_ctx = ngx_stream_lua_ssl_get_ctx (r -> connection -> ssl -> connection );
126+ if (ssl_ctx == NULL ) {
127+ return NGX_ERROR ;
128+ }
129+
130+ ssl_ctx -> ctx_ref = ref ;
131+ c = ngx_ssl_get_connection (r -> connection -> ssl -> connection );
132+ pool = c -> pool ;
133+
134+ } else {
135+ pool = r -> pool ;
136+ }
137+
100138 ctx -> ctx_ref = ref ;
101139
102- if (ngx_stream_lua_ngx_ctx_add_cleanup (r , ref ) != NGX_OK ) {
140+ if (ngx_stream_lua_ngx_ctx_add_cleanup (r , pool , ref ) != NGX_OK ) {
103141 return NGX_ERROR ;
104142 }
105143
@@ -108,7 +146,8 @@ ngx_stream_lua_ffi_set_ctx_ref(ngx_stream_lua_request_t *r, int ref)
108146
109147
110148static ngx_int_t
111- ngx_stream_lua_ngx_ctx_add_cleanup (ngx_stream_lua_request_t * r , int ref )
149+ ngx_stream_lua_ngx_ctx_add_cleanup (ngx_stream_lua_request_t * r , ngx_pool_t * pool ,
150+ int ref )
112151{
113152 lua_State * L ;
114153 ngx_pool_cleanup_t * cln ;
@@ -119,7 +158,7 @@ ngx_stream_lua_ngx_ctx_add_cleanup(ngx_stream_lua_request_t *r, int ref)
119158 ctx = ngx_stream_lua_get_module_ctx (r , ngx_stream_lua_module );
120159 L = ngx_stream_lua_get_lua_vm (r , ctx );
121160
122- cln = ngx_pool_cleanup_add (r -> pool ,
161+ cln = ngx_pool_cleanup_add (pool ,
123162 sizeof (ngx_stream_lua_ngx_ctx_cleanup_data_t ));
124163 if (cln == NULL ) {
125164 return NGX_ERROR ;
0 commit comments