52
52
ngx_http_lua_body_filter_by_lua_env(lua_State *L, ngx_http_request_t *r,
55
55
/* set nginx request pointer to current lua thread's globals table */
56
lua_pushlightuserdata(L, &ngx_http_lua_request_key);
57
lua_pushlightuserdata(L, r);
58
lua_rawset(L, LUA_GLOBALSINDEX);
56
ngx_http_lua_set_req(L, r);
60
58
lua_pushlightuserdata(L, &ngx_http_lua_body_filter_chain_key);
61
59
lua_pushlightuserdata(L, in);
75
73
ngx_http_lua_create_new_global_table(L, 0 /* narr */, 1 /* nrec */);
77
75
/* {{{ make new env inheriting main thread's globals table */
78
lua_newtable(L); /* the metatable for the new env */
76
lua_createtable(L, 0, 1 /* nrec */); /* the metatable for the new
79
78
lua_pushvalue(L, LUA_GLOBALSINDEX);
80
79
lua_setfield(L, -2, "__index");
81
80
lua_setmetatable(L, -2); /* setmetatable({}, {__index = _G}) */
159
ngx_http_lua_main_conf_t *lmcf;
160
158
ngx_http_lua_loc_conf_t *llcf;
163
160
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
164
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
162
L = ngx_http_lua_get_lua_vm(r, NULL);
168
164
/* load Lua inline script (w/ cache) sp = 1 */
169
165
rc = ngx_http_lua_cache_loadbuffer(L, llcf->body_filter_src.value.data,
170
166
llcf->body_filter_src.value.len,
171
167
llcf->body_filter_src_key,
172
"body_filter_by_lua", &err,
173
llcf->enable_code_cache ? 1 : 0);
168
"body_filter_by_lua");
175
169
if (rc != NGX_OK) {
177
err = "unknown error";
180
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
181
"Failed to load Lua inlined code: %s", err);
183
return NGX_HTTP_INTERNAL_SERVER_ERROR;
186
173
rc = ngx_http_lua_body_filter_by_chunk(L, r, in);
222
207
return NGX_ERROR;
225
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
210
L = ngx_http_lua_get_lua_vm(r, NULL);
228
212
/* load Lua script file (w/ cache) sp = 1 */
229
213
rc = ngx_http_lua_cache_loadfile(L, script_path,
230
llcf->body_filter_src_key, &err,
231
llcf->enable_code_cache ? 1 : 0);
214
llcf->body_filter_src_key);
233
215
if (rc != NGX_OK) {
235
err = "unknown error";
238
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
239
"failed to load Lua inlined code: %s", err);
241
return NGX_HTTP_INTERNAL_SERVER_ERROR;
244
219
/* make sure we have a valid code chunk */
327
301
return NGX_ERROR;
330
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
304
L = ngx_http_lua_get_lua_vm(r, ctx);
334
306
lua_pushlightuserdata(L, &ngx_http_lua_body_filter_chain_key);
335
307
lua_rawget(L, LUA_GLOBALSINDEX);
492
454
ctx->seen_last_in_filter = 1;
495
for (cl = in; cl; cl = cl->next) {
496
if (cl->next == NULL) {
498
cl->buf->last_buf = 1;
501
cl->buf->last_in_chain = 1;
456
for (cl = in; cl; cl = cl->next) {
457
if (cl->next == NULL) {
459
cl->buf->last_buf = 1;
462
cl->buf->last_in_chain = 1;
509
tag = (ngx_buf_tag_t) &ngx_http_lua_module;
511
cl = ngx_http_lua_chains_get_free_buf(r->connection->log,
512
r->pool, &ctx->free_bufs,
516
return luaL_error(L, "out of memory");
520
cl->buf->last_buf = 1;
523
cl->buf->last_in_chain = 1;
526
lua_pushlightuserdata(L, &ngx_http_lua_body_filter_chain_key);
527
lua_pushlightuserdata(L, cl);
528
lua_rawset(L, LUA_GLOBALSINDEX);
535
for (size = 0, cl = in; cl; cl = cl->next) {
536
if (cl->buf->last_buf) {
537
cl->buf->last_buf = 0;
540
if (cl->buf->last_in_chain) {
541
cl->buf->last_in_chain = 0;
544
size += cl->buf->last - cl->buf->pos;
548
lua_pushlightuserdata(L,
549
&ngx_http_lua_body_filter_chain_key);
550
lua_pushlightuserdata(L, NULL);
551
lua_rawset(L, LUA_GLOBALSINDEX);
472
for (cl = in; cl; cl = cl->next) {
473
if (cl->buf->last_buf) {
474
cl->buf->last_buf = 0;
477
if (cl->buf->last_in_chain) {
478
cl->buf->last_in_chain = 0;
481
if (cl->buf->last - cl->buf->pos == 0) {
482
cl->buf->temporary = 0;
485
cl->buf->in_file = 0;
578
513
dd("mark the buf as consumed: %d", (int) ngx_buf_size(cl->buf));
579
514
cl->buf->pos = cl->buf->last;
580
515
cl->buf->file_pos = cl->buf->file_last;
517
cl->buf->temporary = 0;
520
cl->buf->in_file = 0;
583
lua_pushlightuserdata(L, NULL); /* key val */
584
lua_rawset(L, LUA_GLOBALSINDEX);
617
in->buf->last_buf = 1;
620
in->buf->last_in_chain = 1;
555
in->buf->last_buf = 1;
625
tag = (ngx_buf_tag_t) &ngx_http_lua_module;
627
cl = ngx_http_lua_chains_get_free_buf(r->connection->log,
628
r->pool, &ctx->free_bufs,
632
return luaL_error(L, "out of memory");
636
cl->buf->last_buf = 1;
639
in->buf->last_in_chain = 1;
642
lua_pushlightuserdata(L, &ngx_http_lua_body_filter_chain_key);
643
lua_pushlightuserdata(L, cl);
644
lua_rawset(L, LUA_GLOBALSINDEX);
558
in->buf->last_in_chain = 1;
562
for (cl = in; cl; cl = cl->next) {
564
cl->buf->temporary = 0;
567
cl->buf->in_file = 0;