26
26
#include "ngx_http_lua_misc.h"
27
27
#include "ngx_http_lua_consts.h"
28
28
#include "ngx_http_lua_shdict.h"
29
#include "ngx_http_lua_socket.h"
31
32
static ngx_int_t ngx_http_lua_send_http10_headers(ngx_http_request_t *r,
98
99
lua_getglobal(L, "package");
100
if (! lua_istable(L, -1)) {
101
if (!lua_istable(L, -1)) {
101
102
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
102
103
"the \"package\" table does not exist");
198
199
*ref = luaL_ref(L, -2);
200
201
if (*ref == LUA_NOREF) {
201
lua_settop(L, top); /* restore main trhead stack */
202
lua_settop(L, top); /* restore main thread stack */
206
/* pop coroutine refernece on main thread's stack after anchoring it
207
/* pop coroutine reference on main thread's stack after anchoring it
215
ngx_http_lua_del_thread(ngx_http_request_t *r, lua_State *L, int ref,
216
ngx_http_lua_del_thread(ngx_http_request_t *r, lua_State *L, int ref)
218
ngx_http_lua_ctx_t *ctx;
220
218
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
221
219
"lua deleting thread");
223
221
lua_getfield(L, LUA_REGISTRYINDEX, NGX_LUA_CORT_REF);
225
lua_rawgeti(L, -1, ref);
226
lua_State *cr = lua_tothread(L, -1);
229
dd("cr: %p, force quit: %d", cr, (int) force_quit);
231
if (cr && force_quit) {
233
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
234
"lua terminate thread forcibly");
236
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
239
/* {{{ save orig code closure's env */
240
lua_getglobal(cr, GLOBALS_SYMBOL_RUNCODE);
245
/* {{{ clean code closure's env */
250
/* {{{ blocking run code till ending */
253
} while (lua_resume(cr, 0) == LUA_YIELD);
256
/* {{{ restore orig code closure's env */
258
lua_getglobal(cr, GLOBALS_SYMBOL_RUNCODE);
265
223
/* release reference to coroutine */
266
224
luaL_unref(L, -1, ref);
321
if ( ! ctx->headers_sent ) {
279
if (!ctx->headers_sent ) {
322
280
if (r->headers_out.status == 0) {
323
281
r->headers_out.status = NGX_HTTP_OK;
326
if (! ctx->headers_set && ngx_http_set_content_type(r) != NGX_OK) {
284
if (!ctx->headers_set && ngx_http_set_content_type(r) != NGX_OK) {
327
285
return NGX_HTTP_INTERNAL_SERVER_ERROR;
330
if (! ctx->headers_set) {
288
if (!ctx->headers_set) {
331
289
ngx_http_clear_content_length(r);
332
290
ngx_http_clear_accept_ranges(r);
335
if (r->http_version >= NGX_HTTP_VERSION_11
336
|| r->headers_out.content_length)
338
/* Send response headers for HTTP version <= 1.0 elsewhere */
293
if (!ctx->buffering) {
339
294
dd("sending headers");
340
295
rc = ngx_http_send_header(r);
341
296
ctx->headers_sent = 1;
366
322
r->header_only = 1;
325
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
327
if (llcf->http10_buffering
329
&& !ctx->headers_sent
330
&& r->http_version < NGX_HTTP_VERSION_11
331
&& r->headers_out.content_length_n < 0)
369
336
rc = ngx_http_lua_send_header_if_needed(r, ctx);
371
338
if (rc == NGX_ERROR || rc > NGX_OK) {
429
if (r->http_version < NGX_HTTP_VERSION_11 && !ctx->headers_sent) {
395
if (ctx->buffering) {
430
396
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
431
397
"lua buffering output bufs for the HTTP 1.0 request");
487
453
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
488
454
"lua initializing lua registry");
490
/* {{{ register table to anchor lua coroutines reliablly:
456
/* {{{ register a table to anchor lua coroutines reliably:
491
457
* {([int]ref) = [cort]} */
493
459
lua_setfield(L, LUA_REGISTRYINDEX, NGX_LUA_CORT_REF);
496
/* create registry entry for the Lua request ctx data table */
462
/* create the registry entry for the Lua request ctx data table */
498
464
lua_setfield(L, LUA_REGISTRYINDEX, NGX_LUA_REQ_CTX_REF);
500
/* create registry entry for the Lua request ctx data table */
466
/* create the registry entry for the Lua socket connection pool table */
468
lua_setfield(L, LUA_REGISTRYINDEX, NGX_LUA_SOCKET_POOL);
470
/* create the registry entry for the Lua precompiled regex object cache */
502
472
lua_setfield(L, LUA_REGISTRYINDEX, NGX_LUA_REGEX_CACHE);
528
498
ngx_http_lua_inject_ndk_api(L);
529
499
#endif /* defined(NDK) && NDK */
531
lua_createtable(L, 0 /* narr */, 80 /* nrec */); /* ngx.* */
501
lua_createtable(L, 0 /* narr */, 87 /* nrec */); /* ngx.* */
533
503
ngx_http_lua_inject_internal_utils(cf->log, L);
548
518
ngx_http_lua_inject_resp_header_api(L);
549
519
ngx_http_lua_inject_variable_api(L);
550
520
ngx_http_lua_inject_shdict_api(lmcf, L);
521
ngx_http_lua_inject_socket_api(cf->log, L);
551
523
ngx_http_lua_inject_misc_api(L);
553
525
lua_getglobal(L, "package"); /* ngx package */
601
cl = ngx_chain_get_free_buf(r->pool, &ctx->free);
573
cl = ngx_http_lua_chains_get_free_buf(r->connection->log, r->pool,
574
&ctx->free_bufs, len,
575
(ngx_buf_tag_t) &ngx_http_lua_module);
602
577
if (cl == NULL) {
603
578
return NGX_ERROR;
581
dd("chains get free buf: %d == %d", (int) (cl->buf->end - cl->buf->start),
608
b->start = ngx_palloc(r->pool, len);
609
if (b->start == NULL) {
613
b->end = b->start + len;
620
b->tag = (ngx_buf_tag_t) &ngx_http_lua_module;
624
587
if (ngx_buf_in_memory(in->buf)) {
625
588
b->last = ngx_copy(b->last, in->buf->pos,
644
606
"lua reset ctx");
646
608
if (ctx->cc_ref != LUA_NOREF) {
647
ngx_http_lua_del_thread(r, L, ctx->cc_ref, 0);
609
ngx_http_lua_del_thread(r, L, ctx->cc_ref);
648
610
ctx->cc_ref = LUA_NOREF;
739
701
if (lua_isthread(L, -1)) {
740
702
/* coroutine not finished yet, force quit */
741
ngx_http_lua_del_thread(r, L, ctx->cc_ref, 1);
703
ngx_http_lua_del_thread(r, L, ctx->cc_ref);
742
704
ctx->cc_ref = LUA_NOREF;
775
738
dd("ctx = %p", ctx);
741
cc_ref = ctx->cc_ref;
744
/* XXX: work-around to nginx regex subsystem */
745
old_pool = ngx_http_lua_pcre_malloc_init(r->pool);
777
748
NGX_LUA_EXCEPTION_TRY {
779
cc_ref = ctx->cc_ref;
782
/* XXX: work-around to nginx regex subsystem */
783
old_pool = ngx_http_lua_pcre_malloc_init(r->pool);
786
749
dd("calling lua_resume: vm %p, nret %d", cc, (int) nret);
792
755
/* XXX: work-around to nginx regex subsystem */
793
756
ngx_http_lua_pcre_malloc_done(old_pool);
757
pcre_pool_resumed = 1;
761
/* test the longjmp thing */
762
if (rand() % 2 == 0) {
763
NGX_LUA_EXCEPTION_THROW(1);
796
767
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
831
802
ngx_http_lua_dump_postponed(r);
834
ngx_http_lua_del_thread(r, L, cc_ref, 0);
805
ngx_http_lua_del_thread(r, L, cc_ref);
835
806
ctx->cc_ref = LUA_NOREF;
837
808
if (ctx->entered_content_phase) {
877
848
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
878
849
"lua handler aborted: %s: %s", err, msg);
880
ngx_http_lua_del_thread(r, L, cc_ref, 0);
851
ngx_http_lua_del_thread(r, L, cc_ref);
881
852
ctx->cc_ref = LUA_NOREF;
882
854
ngx_http_lua_request_cleanup(r);
884
856
dd("headers sent? %d", ctx->headers_sent ? 1 : 0);
886
858
return ctx->headers_sent ? NGX_ERROR : NGX_HTTP_INTERNAL_SERVER_ERROR;
888
860
} NGX_LUA_EXCEPTION_CATCH {
889
862
dd("nginx execution restored");
865
if (!pcre_pool_resumed) {
866
ngx_http_lua_pcre_malloc_done(old_pool);
892
871
return NGX_ERROR;
1017
if (ctx->busy_bufs) {
1020
dd("updating chains...");
1022
#if nginx_version >= 1001004
1023
ngx_chain_update_chains(r->pool,
1025
ngx_chain_update_chains(
1027
&ctx->free_bufs, &ctx->busy_bufs, &cl,
1028
(ngx_buf_tag_t) &ngx_http_lua_module);
1030
dd("update lua buf tag: %p, buffered: %x, busy bufs: %p",
1031
&ngx_http_lua_module, (int) c->buffered, ctx->busy_bufs);
1035
1034
if (c->buffered) {
1037
1036
if (!wev->delayed) {
1060
if (ctx->waiting_flush) {
1059
if (ctx->socket_busy && !ctx->socket_ready) {
1063
if (!ctx->socket_busy && ctx->socket_ready) {
1065
dd("resuming socket api");
1067
dd("setting socket_ready to 0");
1069
ctx->socket_ready = 0;
1072
nret = u->prepare_retvals(r, u, ctx->cc);
1073
if (nret == NGX_AGAIN) {
1079
} else if (ctx->waiting_flush) {
1062
1081
ctx->waiting_flush = 0;
1206
1225
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
1207
1226
"postponed request for %V: "
1209
#if defined(nginx_version) && nginx_version >= 8011
1213
1228
"a:%d, i:%d, r:%V, out:%V",
1216
#if defined(nginx_version) && nginx_version >= 8011
1217
1230
r->main->count,
1220
1231
r == r->connection->data, i,
1221
1232
pr->request ? &pr->request->uri : &nil_str, &out);
1251
1262
lua_rawset(L, index); /* stack: table */
1254
if (! lua_istable(L, -1)) {
1265
if (!lua_istable(L, -1)) {
1255
1266
/* just inserted one value */
1256
1267
lua_createtable(L, 4, 0);
1257
1268
/* stack: table key value value table */
1587
1597
/* ngx.req table */
1589
lua_createtable(L, 0 /* narr */, 15 /* nrec */); /* .req */
1599
lua_createtable(L, 0 /* narr */, 16 /* nrec */); /* .req */
1591
1601
ngx_http_lua_inject_req_header_api(L);
1610
1622
"lua thread initiated internal redirect to %V",
1611
1623
&ctx->exec_uri);
1613
ngx_http_lua_del_thread(r, L, cc_ref, 1 /* force quit */);
1625
ngx_http_lua_del_thread(r, L, cc_ref);
1614
1626
ctx->cc_ref = LUA_NOREF;
1615
1628
ngx_http_lua_request_cleanup(r);
1617
1630
if (ctx->exec_uri.data[0] == '@') {
1690
1703
"lua thread aborting request with status %d",
1691
1704
ctx->exit_code);
1693
ngx_http_lua_del_thread(r, L, cc_ref, 1 /* force quit */);
1706
ngx_http_lua_del_thread(r, L, cc_ref);
1694
1707
ctx->cc_ref = LUA_NOREF;
1695
1709
ngx_http_lua_request_cleanup(r);
1697
1711
if ((ctx->exit_code == NGX_OK &&
1735
1749
lua_pushnil(L);
1736
1750
while (lua_next(L, table) != 0) {
1737
1751
if (lua_type(L, -2) != LUA_TSTRING) {
1738
luaL_error(L, "attemp to use a non-string key in the "
1752
luaL_error(L, "attempt to use a non-string key in the "
1739
1753
"\"args\" option table");
1942
1956
"lua thread aborting request with URI rewrite jump: \"%V?%V\"",
1943
1957
&r->uri, &r->args);
1945
ngx_http_lua_del_thread(r, L, cc_ref, 1 /* force quit */);
1959
ngx_http_lua_del_thread(r, L, cc_ref);
1946
1960
ctx->cc_ref = LUA_NOREF;
1947
1962
ngx_http_lua_request_cleanup(r);
2157
ngx_http_lua_chains_get_free_buf(ngx_log_t *log, ngx_pool_t *p,
2158
ngx_chain_t **free, size_t len, ngx_buf_tag_t tag)
2169
if ((size_t) (b->end - b->start) >= len) {
2170
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, log, 0,
2171
"lua reuse free buf memory %O >= %uz, cl:%p, p:%p",
2172
(off_t) (b->end - b->start), len, cl, b->start);
2180
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, log, 0,
2181
"lua reuse free buf chain, but reallocate memory "
2182
"because %uz >= %O, cl:%p, p:%p", len,
2183
(off_t) (b->end - b->start), cl, b->start);
2185
if (ngx_buf_in_memory(b) && b->start) {
2186
ngx_pfree(p, b->start);
2189
b->start = ngx_palloc(p, len);
2190
if (b->start == NULL) {
2194
dd("buf start: %p", cl->buf->start);
2196
b->end = b->start + len;
2204
cl = ngx_alloc_chain_link(p);
2209
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
2210
"lua allocate new chainlink and new buf of size %uz, cl:%p",
2213
cl->buf = ngx_create_temp_buf(p, len);
2214
if (cl->buf == NULL) {
2218
dd("buf start: %p", cl->buf->start);