Skip to content

Commit f94afe2

Browse files
authored
feature: shared ngx.ctx among SSL_* phases and the following phases. (#208)
1 parent 6ffcfdb commit f94afe2

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

src/ngx_stream_lua_ctx.c

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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

3132
static 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);
3334
static void ngx_stream_lua_ngx_ctx_cleanup(void *data);
3435

3536

3637
int
3738
ngx_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

7680
int
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

90111
int
91112
ngx_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

110148
static 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;

src/ngx_stream_lua_ssl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ typedef struct {
3535
int exit_code; /* exit code for openssl's
3636
set_cert_cb callback */
3737

38+
int ctx_ref; /* reference to anchor
39+
request ctx data in lua
40+
registry */
41+
3842
unsigned done:1;
3943
unsigned aborted:1;
4044

src/ngx_stream_lua_ssl_certby.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ ngx_stream_lua_ssl_cert_handler(ngx_ssl_conn_t *ssl_conn, void *data)
279279
if (cctx == NULL) {
280280
goto failed; /* error */
281281
}
282+
283+
cctx->ctx_ref = LUA_NOREF;
282284
}
283285

284286
cctx->exit_code = 1; /* successful by default */

src/ngx_stream_lua_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424

2525
#include "ngx_stream_lua_common.h"
26+
#include "ngx_stream_lua_ssl.h"
2627
#include "ngx_stream_lua_api.h"
2728

2829

0 commit comments

Comments
 (0)