- Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathngx_http_lua_server_rewriteby.c
343 lines (257 loc) · 9.23 KB
/
ngx_http_lua_server_rewriteby.c
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndefDDEBUG
#defineDDEBUG 0
#endif
#include"ddebug.h"
#include<nginx.h>
#include"ngx_http_lua_server_rewriteby.h"
#include"ngx_http_lua_util.h"
#include"ngx_http_lua_exception.h"
#include"ngx_http_lua_cache.h"
staticngx_int_tngx_http_lua_server_rewrite_by_chunk(lua_State*L,
ngx_http_request_t*r);
ngx_int_t
ngx_http_lua_server_rewrite_handler(ngx_http_request_t*r)
{
ngx_int_trc;
lua_State*L;
ngx_http_lua_srv_conf_t*lscf;
ngx_http_lua_loc_conf_t*llcf;
ngx_http_lua_ctx_t*ctx;
/* XXX we need to take into account ngx_rewrite's location dump */
if (r->uri_changed) {
returnNGX_DECLINED;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua server rewrite handler, uri:\"%V\" c:%ud", &r->uri,
r->main->count);
lscf=ngx_http_get_module_srv_conf(r, ngx_http_lua_module);
llcf=ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
L=ngx_http_lua_get_lua_vm(r, NULL);
if (lscf->srv.server_rewrite_handler==NULL) {
dd("no rewrite handler found");
returnNGX_DECLINED;
}
ctx=ngx_http_get_module_ctx(r, ngx_http_lua_module);
dd("ctx = %p", ctx);
if (ctx==NULL) {
ctx=ngx_http_lua_create_ctx(r);
if (ctx==NULL) {
returnNGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
dd("entered? %d", (int) ctx->entered_server_rewrite_phase);
if (ctx->entered_server_rewrite_phase) {
dd("rewriteby: calling wev handler");
rc=ctx->resume_handler(r);
dd("rewriteby: wev handler returns %d", (int) rc);
if (rc==NGX_OK) {
rc=NGX_DECLINED;
}
if (rc==NGX_DECLINED) {
if (r->header_sent
|| (r->headers_out.status!=0&&ctx->out!=NULL))
{
dd("header already sent");
/* response header was already generated in rewrite_by_lua*,
* so it is no longer safe to proceed to later phases
* which may generate responses again */
if (!ctx->eof) {
dd("eof not yet sent");
rc=ngx_http_lua_send_chain_link(r, ctx, NULL
/* indicate last_buf */);
if (rc==NGX_ERROR||rc>NGX_OK) {
returnrc;
}
}
returnNGX_HTTP_OK;
}
r->write_event_handler=ngx_http_core_run_phases;
ctx->entered_server_rewrite_phase=0;
returnNGX_DECLINED;
}
returnrc;
}
if (ctx->waiting_more_body) {
returnNGX_DONE;
}
/* TODO: lscf do not have force_read_body */
if (llcf->force_read_body&& !ctx->read_body_done) {
r->request_body_in_single_buf=1;
r->request_body_in_persistent_file=1;
r->request_body_in_clean_file=1;
rc=ngx_http_read_client_request_body(r,
ngx_http_lua_generic_phase_post_read);
if (rc==NGX_ERROR||rc >= NGX_HTTP_SPECIAL_RESPONSE) {
returnrc;
}
if (rc==NGX_AGAIN) {
ctx->waiting_more_body=1;
returnNGX_DONE;
}
}
dd("calling server rewrite handler");
returnlscf->srv.server_rewrite_handler(r, lscf, L);
}
ngx_int_t
ngx_http_lua_server_rewrite_handler_inline(ngx_http_request_t*r,
ngx_http_lua_srv_conf_t*lscf, lua_State*L)
{
ngx_int_trc;
dd("server_rewrite by lua inline");
/* load Lua inline script (w/ cache) sp = 1 */
rc=ngx_http_lua_cache_loadbuffer(r->connection->log, L,
lscf->srv.server_rewrite_src.value.data,
lscf->srv.server_rewrite_src.value.len,
&lscf->srv.server_rewrite_src_ref,
lscf->srv.server_rewrite_src_key,
(constchar*)
lscf->srv.server_rewrite_chunkname);
if (rc!=NGX_OK) {
returnNGX_HTTP_INTERNAL_SERVER_ERROR;
}
returnngx_http_lua_server_rewrite_by_chunk(L, r);
}
ngx_int_t
ngx_http_lua_server_rewrite_handler_file(ngx_http_request_t*r,
ngx_http_lua_srv_conf_t*lscf, lua_State*L)
{
ngx_int_trc;
u_char*script_path;
ngx_str_teval_src;
if (ngx_http_complex_value(r, &lscf->srv.server_rewrite_src,
&eval_src) !=NGX_OK)
{
returnNGX_ERROR;
}
script_path=ngx_http_lua_rebase_path(r->pool, eval_src.data,
eval_src.len);
if (script_path==NULL) {
returnNGX_ERROR;
}
/* load Lua script file (w/ cache) sp = 1 */
rc=ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
&lscf->srv.server_rewrite_src_ref,
lscf->srv.server_rewrite_src_key);
if (rc!=NGX_OK) {
if (rc<NGX_HTTP_SPECIAL_RESPONSE) {
returnNGX_HTTP_INTERNAL_SERVER_ERROR;
}
returnrc;
}
returnngx_http_lua_server_rewrite_by_chunk(L, r);
}
staticngx_int_t
ngx_http_lua_server_rewrite_by_chunk(lua_State*L, ngx_http_request_t*r)
{
intco_ref;
lua_State*co;
ngx_int_trc;
ngx_uint_tnreqs;
ngx_event_t*rev;
ngx_connection_t*c;
ngx_http_lua_ctx_t*ctx;
ngx_pool_cleanup_t*cln;
ngx_http_lua_loc_conf_t*llcf;
/* {{{ new coroutine to handle request */
co=ngx_http_lua_new_thread(r, L, &co_ref);
if (co==NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"lua: failed to create new coroutine to handle request");
returnNGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* move code closure to new coroutine */
lua_xmove(L, co, 1);
#ifndefOPENRESTY_LUAJIT
/* set closure's env table to new coroutine's globals table */
ngx_http_lua_get_globals_table(co);
lua_setfenv(co, -2);
#endif
/* save nginx request in coroutine globals table */
ngx_http_lua_set_req(co, r);
/* {{{ initialize request context */
ctx=ngx_http_get_module_ctx(r, ngx_http_lua_module);
dd("ctx = %p", ctx);
if (ctx==NULL) {
returnNGX_ERROR;
}
ngx_http_lua_reset_ctx(r, L, ctx);
ctx->entered_server_rewrite_phase=1;
ctx->cur_co_ctx=&ctx->entry_co_ctx;
ctx->cur_co_ctx->co=co;
ctx->cur_co_ctx->co_ref=co_ref;
#ifdefNGX_LUA_USE_ASSERT
ctx->cur_co_ctx->co_top=1;
#endif
ngx_http_lua_attach_co_ctx_to_L(co, ctx->cur_co_ctx);
/* }}} */
/* {{{ register request cleanup hooks */
if (ctx->cleanup==NULL) {
cln=ngx_pool_cleanup_add(r->pool, 0);
if (cln==NULL) {
returnNGX_HTTP_INTERNAL_SERVER_ERROR;
}
cln->handler=ngx_http_lua_request_cleanup_handler;
cln->data=ctx;
ctx->cleanup=&cln->handler;
}
/* }}} */
ctx->context=NGX_HTTP_LUA_CONTEXT_SERVER_REWRITE;
llcf=ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
if (llcf->check_client_abort) {
r->read_event_handler=ngx_http_lua_rd_check_broken_connection;
#if (NGX_HTTP_V2)
if (!r->stream) {
#endif
rev=r->connection->read;
if (!rev->active) {
if (ngx_add_event(rev, NGX_READ_EVENT, 0) !=NGX_OK) {
returnNGX_ERROR;
}
}
#if (NGX_HTTP_V2)
}
#endif
} else {
r->read_event_handler=ngx_http_block_reading;
}
c=r->connection;
nreqs=c->requests;
rc=ngx_http_lua_run_thread(L, r, ctx, 0);
if (rc==NGX_ERROR||rc>NGX_OK) {
returnrc;
}
if (rc==NGX_AGAIN) {
rc=ngx_http_lua_run_posted_threads(c, L, r, ctx, nreqs);
} elseif (rc==NGX_DONE) {
ngx_http_lua_finalize_request(r, NGX_DONE);
rc=ngx_http_lua_run_posted_threads(c, L, r, ctx, nreqs);
}
if (rc==NGX_OK||rc==NGX_DECLINED) {
if (r->header_sent
|| (r->headers_out.status!=0&&ctx->out!=NULL))
{
dd("header already sent");
/* response header was already generated in rewrite_by_lua*,
* so it is no longer safe to proceed to later phases
* which may generate responses again */
if (!ctx->eof) {
dd("eof not yet sent");
rc=ngx_http_lua_send_chain_link(r, ctx, NULL
/* indicate last_buf */);
if (rc==NGX_ERROR||rc>NGX_OK) {
returnrc;
}
}
returnNGX_HTTP_OK;
}
r->write_event_handler=ngx_http_core_run_phases;
ctx->entered_server_rewrite_phase=0;
returnNGX_DECLINED;
}
returnrc;
}
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */