-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathngx_http_lua_balancer_ssl_session_fetchby.c
More file actions
272 lines (198 loc) · 7.02 KB
/
ngx_http_lua_balancer_ssl_session_fetchby.c
File metadata and controls
272 lines (198 loc) · 7.02 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (C) Chenglong Zhang (kone)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_balancer_ssl_session_fetchby.h"
#include "ngx_http_lua_cache.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_directive.h"
#include "ngx_http_lua_balancer.h"
#if (NGX_HTTP_SSL)
static ngx_int_t ngx_http_lua_balancer_ssl_sess_fetch_by_chunk(lua_State *L,
ngx_http_request_t *r);
ngx_int_t
ngx_http_lua_balancer_ssl_sess_fetch_handler_file(ngx_http_request_t *r,
ngx_http_lua_srv_conf_t *lscf, lua_State *L)
{
ngx_int_t rc;
rc = ngx_http_lua_cache_loadfile(r->connection->log, L,
lscf->balancer.ssl_sess_fetch_src.data,
lscf->balancer.ssl_sess_fetch_src_key);
if (rc != NGX_OK) {
return rc;
}
/* make sure we have a valid code chunk */
ngx_http_lua_assert(lua_isfunction(L, -1));
return ngx_http_lua_balancer_ssl_sess_fetch_by_chunk(L, r);
}
ngx_int_t
ngx_http_lua_balancer_ssl_sess_fetch_handler_inline(ngx_http_request_t *r,
ngx_http_lua_srv_conf_t *lscf, lua_State *L)
{
ngx_int_t rc;
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
lscf->balancer.ssl_sess_fetch_src.data,
lscf->balancer.ssl_sess_fetch_src.len,
lscf->balancer.ssl_sess_fetch_src_key,
"=balancer_ssl_session_fetch_by_lua");
if (rc != NGX_OK) {
return rc;
}
/* make sure we have a valid code chunk */
ngx_http_lua_assert(lua_isfunction(L, -1));
return ngx_http_lua_balancer_ssl_sess_fetch_by_chunk(L, r);
}
ngx_int_t
ngx_http_lua_balancer_ssl_sess_fetch(ngx_peer_connection_t *pc, void *data)
{
lua_State *L;
ngx_int_t rc;
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
ngx_http_lua_srv_conf_t *lscf;
ngx_http_lua_balancer_peer_data_t *bp = data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"balancer ssl session fetch");
lscf = bp->conf;
r = bp->request;
ngx_http_lua_assert(lscf->balancer.ssl_sess_fetch_handler && r);
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
ctx = ngx_http_lua_create_ctx(r);
if (ctx == NULL) {
return NGX_ERROR;
}
L = ngx_http_lua_get_lua_vm(r, ctx);
} else {
L = ngx_http_lua_get_lua_vm(r, ctx);
dd("reset ctx");
ngx_http_lua_reset_ctx(r, L, ctx);
}
ctx->context = NGX_HTTP_LUA_CONTEXT_BALANCER_SSL_SESS_FETCH;
rc = lscf->balancer.ssl_sess_fetch_handler(r, lscf, L);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
if (ctx->exited && ctx->exit_code != NGX_OK) {
rc = ctx->exit_code;
if (rc == NGX_ERROR
|| rc == NGX_BUSY
|| rc == NGX_DECLINED
#ifdef HAVE_BALANCER_STATUS_CODE_PATCH
|| rc >= NGX_HTTP_SPECIAL_RESPONSE
#endif
) {
return rc;
}
if (rc > NGX_OK) {
return NGX_ERROR;
}
}
return NGX_OK;
}
char *
ngx_http_lua_balancer_ssl_sess_fetch_by_lua_block(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf)
{
char *rv;
ngx_conf_t save;
save = *cf;
cf->handler = ngx_http_lua_balancer_ssl_sess_fetch_by_lua;
cf->handler_conf = conf;
rv = ngx_http_lua_conf_lua_block_parse(cf, cmd);
*cf = save;
return rv;
}
char *
ngx_http_lua_balancer_ssl_sess_fetch_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
u_char *p;
u_char *name;
ngx_str_t *value;
ngx_http_lua_srv_conf_t *lscf = conf;
dd("enter");
/* must specify a content handler */
if (cmd->post == NULL) {
return NGX_CONF_ERROR;
}
if(lscf->balancer.ssl_sess_fetch_handler) {
return "is duplicate";
}
value = cf->args->elts;
lscf->balancer.ssl_sess_fetch_handler =
(ngx_http_lua_srv_conf_handler_pt) cmd->post;
if (cmd->post == ngx_http_lua_balancer_ssl_sess_fetch_handler_file) {
/* Lua code in an external file */
name = ngx_http_lua_rebase_path(cf->pool, value[1].data,
value[1].len);
if (name == NULL) {
return NGX_CONF_ERROR;
}
lscf->balancer.ssl_sess_fetch_src.data = name;
lscf->balancer.ssl_sess_fetch_src.len = ngx_strlen(name);
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
if (p == NULL) {
return NGX_CONF_ERROR;
}
lscf->balancer.ssl_sess_fetch_src_key = p;
p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
*p = '\0';
} else {
/* inlined Lua code */
lscf->balancer.ssl_sess_fetch_src = value[1];
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
if (p == NULL) {
return NGX_CONF_ERROR;
}
lscf->balancer.ssl_sess_fetch_src_key = p;
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
*p = '\0';
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_lua_balancer_ssl_sess_fetch_by_chunk(lua_State *L,
ngx_http_request_t *r)
{
u_char *err_msg;
size_t len;
ngx_int_t rc;
/* init nginx context in Lua VM */
ngx_http_lua_set_req(L, r);
ngx_http_lua_create_new_globals_table(L, 0 /* narr */, 1 /* nrec */);
/* {{{ make new env inheriting main thread's globals table */
lua_createtable(L, 0, 1 /* nrec */); /* the metatable for the new env */
ngx_http_lua_get_globals_table(L);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2); /* setmetatable({}, {__index = _G}) */
/* }}} */
lua_setfenv(L, -2); /* set new running env for the code closure */
lua_pushcfunction(L, ngx_http_lua_traceback);
lua_insert(L, 1); /* put it under chunk and args */
/* protected call user code */
rc = lua_pcall(L, 0, 1, 1);
lua_remove(L, 1); /* remove traceback function */
dd("rc == %d", (int) rc);
if (rc != 0) {
/* error occurred when running loaded code */
err_msg = (u_char *) lua_tolstring(L, -1, &len);
if (err_msg == NULL) {
err_msg = (u_char *) "unknown reason";
len = sizeof("unknown reason") - 1;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"failed to run balancer_ssl_session_fetch_by_lua*: %*s",
len, err_msg);
lua_settop(L, 0); /* clear remaining elems on stack */
return NGX_ERROR;
}
lua_settop(L, 0); /* clear remaining elems on stack */
return rc;
}
#endif /* NGX_HTTP_SSL */