forked from openresty/lua-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngx_http_lua_ctx.c
More file actions
216 lines (156 loc) · 4.94 KB
/
Copy pathngx_http_lua_ctx.c
File metadata and controls
216 lines (156 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_ctx.h"
typedef struct {
int ref;
lua_State *vm;
} ngx_http_lua_ngx_ctx_cleanup_data_t;
static ngx_int_t ngx_http_lua_ngx_ctx_add_cleanup(ngx_http_request_t *r,
int ref);
static void ngx_http_lua_ngx_ctx_cleanup(void *data);
int
ngx_http_lua_ngx_get_ctx(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request found");
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no request ctx found");
}
if (ctx->ctx_ref == LUA_NOREF) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua create ngx.ctx table for the current request");
lua_pushliteral(L, ngx_http_lua_ctx_tables_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_createtable(L, 0 /* narr */, 4 /* nrec */);
lua_pushvalue(L, -1);
ctx->ctx_ref = luaL_ref(L, -3);
if (ngx_http_lua_ngx_ctx_add_cleanup(r, ctx->ctx_ref) != NGX_OK) {
return luaL_error(L, "no memory");
}
return 1;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua fetching existing ngx.ctx table for the current "
"request");
lua_pushliteral(L, ngx_http_lua_ctx_tables_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, -1, ctx->ctx_ref);
return 1;
}
int
ngx_http_lua_ngx_set_ctx(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request found");
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no request ctx found");
}
return ngx_http_lua_ngx_set_ctx_helper(L, r, ctx, 3);
}
int
ngx_http_lua_ngx_set_ctx_helper(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx, int index)
{
if (index < 0) {
index = lua_gettop(L) + index + 1;
}
if (ctx->ctx_ref == LUA_NOREF) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua create ngx.ctx table for the current request");
lua_pushliteral(L, ngx_http_lua_ctx_tables_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_pushvalue(L, index);
ctx->ctx_ref = luaL_ref(L, -2);
lua_pop(L, 1);
if (ngx_http_lua_ngx_ctx_add_cleanup(r, ctx->ctx_ref) != NGX_OK) {
return luaL_error(L, "no memory");
}
return 0;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua fetching existing ngx.ctx table for the current "
"request");
lua_pushliteral(L, ngx_http_lua_ctx_tables_key);
lua_rawget(L, LUA_REGISTRYINDEX);
luaL_unref(L, -1, ctx->ctx_ref);
lua_pushvalue(L, index);
ctx->ctx_ref = luaL_ref(L, -2);
lua_pop(L, 1);
return 0;
}
#ifndef NGX_LUA_NO_FFI_API
int
ngx_http_lua_ffi_get_ctx_ref(ngx_http_request_t *r)
{
ngx_http_lua_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return NGX_HTTP_LUA_FFI_NO_REQ_CTX;
}
return ctx->ctx_ref;
}
int
ngx_http_lua_ffi_set_ctx_ref(ngx_http_request_t *r, int ref)
{
ngx_http_lua_ctx_t *ctx;
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return NGX_HTTP_LUA_FFI_NO_REQ_CTX;
}
ctx->ctx_ref = ref;
if (ngx_http_lua_ngx_ctx_add_cleanup(r, ref) != NGX_OK) {
return NGX_ERROR;
}
return NGX_OK;
}
#endif /* NGX_LUA_NO_FFI_API */
static ngx_int_t
ngx_http_lua_ngx_ctx_add_cleanup(ngx_http_request_t *r, int ref)
{
lua_State *L;
ngx_pool_cleanup_t *cln;
ngx_http_lua_ctx_t *ctx;
ngx_http_lua_ngx_ctx_cleanup_data_t *data;
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
L = ngx_http_lua_get_lua_vm(r, ctx);
cln = ngx_pool_cleanup_add(r->pool,
sizeof(ngx_http_lua_ngx_ctx_cleanup_data_t));
if (cln == NULL) {
return NGX_ERROR;
}
cln->handler = ngx_http_lua_ngx_ctx_cleanup;
data = cln->data;
data->vm = L;
data->ref = ref;
return NGX_OK;
}
static void
ngx_http_lua_ngx_ctx_cleanup(void *data)
{
lua_State *L;
ngx_http_lua_ngx_ctx_cleanup_data_t *clndata = data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua release ngx.ctx at ref %d", clndata->ref);
L = clndata->vm;
lua_pushliteral(L, ngx_http_lua_ctx_tables_key);
lua_rawget(L, LUA_REGISTRYINDEX);
luaL_unref(L, -1, clndata->ref);
lua_pop(L, 1);
}
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */