-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathngx_http_lua_util.c
4308 lines (3196 loc) · 112 KB
/
ngx_http_lua_util.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "nginx.h"
#include "ngx_http_lua_directive.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_exception.h"
#include "ngx_http_lua_pcrefix.h"
#include "ngx_http_lua_regex.h"
#include "ngx_http_lua_args.h"
#include "ngx_http_lua_uri.h"
#include "ngx_http_lua_req_body.h"
#include "ngx_http_lua_headers.h"
#include "ngx_http_lua_output.h"
#include "ngx_http_lua_time.h"
#include "ngx_http_lua_control.h"
#include "ngx_http_lua_ndk.h"
#include "ngx_http_lua_subrequest.h"
#include "ngx_http_lua_log.h"
#include "ngx_http_lua_variable.h"
#include "ngx_http_lua_string.h"
#include "ngx_http_lua_misc.h"
#include "ngx_http_lua_consts.h"
#include "ngx_http_lua_req_method.h"
#include "ngx_http_lua_shdict.h"
#include "ngx_http_lua_coroutine.h"
#include "ngx_http_lua_socket_tcp.h"
#include "ngx_http_lua_socket_udp.h"
#include "ngx_http_lua_sleep.h"
#include "ngx_http_lua_setby.h"
#include "ngx_http_lua_headerfilterby.h"
#include "ngx_http_lua_bodyfilterby.h"
#include "ngx_http_lua_logby.h"
#include "ngx_http_lua_phase.h"
#include "ngx_http_lua_probe.h"
#include "ngx_http_lua_uthread.h"
#include "ngx_http_lua_contentby.h"
#include "ngx_http_lua_timer.h"
#include "ngx_http_lua_config.h"
#include "ngx_http_lua_worker.h"
#include "ngx_http_lua_socket_tcp.h"
#include "ngx_http_lua_ssl_certby.h"
#include "ngx_http_lua_ssl.h"
#include "ngx_http_lua_log_ringbuf.h"
#if 1
#undef ngx_http_lua_probe_info
#define ngx_http_lua_probe_info(msg)
#endif
#ifndef NGX_HTTP_LUA_BT_DEPTH
#define NGX_HTTP_LUA_BT_DEPTH 22
#endif
#ifndef NGX_HTTP_LUA_BT_MAX_COROS
#define NGX_HTTP_LUA_BT_MAX_COROS 5
#endif
#if (NGX_HTTP_LUA_HAVE_SA_RESTART)
#define NGX_HTTP_LUA_SA_RESTART_SIGS { \
ngx_signal_value(NGX_RECONFIGURE_SIGNAL), \
ngx_signal_value(NGX_REOPEN_SIGNAL), \
ngx_signal_value(NGX_NOACCEPT_SIGNAL), \
ngx_signal_value(NGX_TERMINATE_SIGNAL), \
ngx_signal_value(NGX_SHUTDOWN_SIGNAL), \
ngx_signal_value(NGX_CHANGEBIN_SIGNAL), \
SIGALRM, \
SIGINT, \
SIGIO, \
SIGCHLD, \
SIGSYS, \
SIGPIPE, \
0 \
};
#endif
char ngx_http_lua_code_cache_key;
char ngx_http_lua_regex_cache_key;
char ngx_http_lua_socket_pool_key;
char ngx_http_lua_coroutines_key;
char ngx_http_lua_headers_metatable_key;
ngx_uint_t ngx_http_lua_location_hash = 0;
ngx_uint_t ngx_http_lua_content_length_hash = 0;
static ngx_int_t ngx_http_lua_send_http10_headers(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx);
static void ngx_http_lua_init_registry(lua_State *L, ngx_log_t *log);
static void ngx_http_lua_init_globals(lua_State *L, ngx_cycle_t *cycle,
ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log);
static void ngx_http_lua_set_path(ngx_cycle_t *cycle, lua_State *L, int tab_idx,
const char *fieldname, const char *path, const char *default_path,
ngx_log_t *log);
static ngx_int_t ngx_http_lua_handle_exec(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx);
static ngx_int_t ngx_http_lua_handle_exit(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx);
static ngx_int_t ngx_http_lua_handle_rewrite_jump(lua_State *L,
ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx);
static int ngx_http_lua_thread_traceback(lua_State *L, lua_State *co,
ngx_http_lua_co_ctx_t *coctx);
static void ngx_http_lua_inject_ngx_api(lua_State *L,
ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log);
static void ngx_http_lua_inject_arg_api(lua_State *L);
static int ngx_http_lua_param_get(lua_State *L);
static int ngx_http_lua_param_set(lua_State *L);
static ngx_int_t ngx_http_lua_output_filter(ngx_http_request_t *r,
ngx_chain_t *in);
static ngx_int_t ngx_http_lua_send_special(ngx_http_request_t *r,
ngx_uint_t flags);
static void ngx_http_lua_finalize_threads(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx, lua_State *L);
static ngx_int_t ngx_http_lua_post_zombie_thread(ngx_http_request_t *r,
ngx_http_lua_co_ctx_t *parent, ngx_http_lua_co_ctx_t *thread);
static void ngx_http_lua_cleanup_zombie_child_uthreads(ngx_http_request_t *r,
lua_State *L, ngx_http_lua_ctx_t *ctx, ngx_http_lua_co_ctx_t *coctx);
static ngx_int_t ngx_http_lua_on_abort_resume(ngx_http_request_t *r);
static void ngx_http_lua_close_fake_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_lua_flush_pending_output(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx);
static ngx_int_t
ngx_http_lua_process_flushing_coroutines(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx);
static lua_State *ngx_http_lua_new_state(lua_State *parent_vm,
ngx_cycle_t *cycle, ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log);
static int ngx_http_lua_get_raw_phase_context(lua_State *L);
#ifndef LUA_PATH_SEP
#define LUA_PATH_SEP ";"
#endif
#if !defined(LUA_DEFAULT_PATH) && (NGX_DEBUG)
#define LUA_DEFAULT_PATH "../lua-resty-core/lib/?.lua;" \
"../lua-resty-lrucache/lib/?.lua"
#endif
#define AUX_MARK "\1"
static void
ngx_http_lua_set_path(ngx_cycle_t *cycle, lua_State *L, int tab_idx,
const char *fieldname, const char *path, const char *default_path,
ngx_log_t *log)
{
const char *tmp_path;
const char *prefix;
/* XXX here we use some hack to simplify string manipulation */
tmp_path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
LUA_PATH_SEP AUX_MARK LUA_PATH_SEP);
lua_pushlstring(L, (char *) cycle->prefix.data, cycle->prefix.len);
prefix = lua_tostring(L, -1);
tmp_path = luaL_gsub(L, tmp_path, "$prefix", prefix);
tmp_path = luaL_gsub(L, tmp_path, "${prefix}", prefix);
lua_pop(L, 3);
dd("tmp_path path: %s", tmp_path);
#if (NGX_DEBUG)
tmp_path =
#else
(void)
#endif
luaL_gsub(L, tmp_path, AUX_MARK, default_path);
#if (NGX_DEBUG)
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
"lua setting lua package.%s to \"%s\"", fieldname, tmp_path);
#endif
lua_remove(L, -2);
/* fix negative index as there's new data on stack */
tab_idx = (tab_idx < 0) ? (tab_idx - 1) : tab_idx;
lua_setfield(L, tab_idx, fieldname);
}
#ifndef OPENRESTY_LUAJIT
/**
* Create new table and set _G field to itself.
*
* After:
* | new table | <- top
* | ... |
* */
void
ngx_http_lua_create_new_globals_table(lua_State *L, int narr, int nrec)
{
lua_createtable(L, narr, nrec + 1);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "_G");
}
#endif /* OPENRESTY_LUAJIT */
static lua_State *
ngx_http_lua_new_state(lua_State *parent_vm, ngx_cycle_t *cycle,
ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log)
{
lua_State *L;
const char *old_path;
const char *new_path;
size_t old_path_len;
const char *old_cpath;
const char *new_cpath;
size_t old_cpath_len;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "lua creating new vm state");
L = luaL_newstate();
if (L == NULL) {
return NULL;
}
luaL_openlibs(L);
lua_getglobal(L, "package");
if (!lua_istable(L, -1)) {
ngx_log_error(NGX_LOG_EMERG, log, 0,
"the \"package\" table does not exist");
return NULL;
}
if (parent_vm) {
lua_getglobal(parent_vm, "package");
lua_getfield(parent_vm, -1, "path");
old_path = lua_tolstring(parent_vm, -1, &old_path_len);
lua_pop(parent_vm, 1);
lua_pushlstring(L, old_path, old_path_len);
lua_setfield(L, -2, "path");
lua_getfield(parent_vm, -1, "cpath");
old_path = lua_tolstring(parent_vm, -1, &old_path_len);
lua_pop(parent_vm, 2);
lua_pushlstring(L, old_path, old_path_len);
lua_setfield(L, -2, "cpath");
} else {
#ifdef LUA_DEFAULT_PATH
# define LUA_DEFAULT_PATH_LEN (sizeof(LUA_DEFAULT_PATH) - 1)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"lua prepending default package.path with %s",
LUA_DEFAULT_PATH);
lua_pushliteral(L, LUA_DEFAULT_PATH ";"); /* package default */
lua_getfield(L, -2, "path"); /* package default old */
lua_concat(L, 2); /* package new */
lua_setfield(L, -2, "path"); /* package */
#endif
#ifdef LUA_DEFAULT_CPATH
# define LUA_DEFAULT_CPATH_LEN (sizeof(LUA_DEFAULT_CPATH) - 1)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"lua prepending default package.cpath with %s",
LUA_DEFAULT_CPATH);
lua_pushliteral(L, LUA_DEFAULT_CPATH ";"); /* package default */
lua_getfield(L, -2, "cpath"); /* package default old */
old_cpath = lua_tolstring(L, -1, &old_cpath_len);
lua_concat(L, 2); /* package new */
lua_setfield(L, -2, "cpath"); /* package */
#endif
if (lmcf->lua_path.len != 0) {
lua_getfield(L, -1, "path"); /* get original package.path */
old_path = lua_tolstring(L, -1, &old_path_len);
dd("old path: %s", old_path);
lua_pushlstring(L, (char *) lmcf->lua_path.data,
lmcf->lua_path.len);
new_path = lua_tostring(L, -1);
ngx_http_lua_set_path(cycle, L, -3, "path", new_path, old_path,
log);
lua_pop(L, 2);
}
if (lmcf->lua_cpath.len != 0) {
lua_getfield(L, -1, "cpath"); /* get original package.cpath */
old_cpath = lua_tolstring(L, -1, &old_cpath_len);
dd("old cpath: %s", old_cpath);
lua_pushlstring(L, (char *) lmcf->lua_cpath.data,
lmcf->lua_cpath.len);
new_cpath = lua_tostring(L, -1);
ngx_http_lua_set_path(cycle, L, -3, "cpath", new_cpath, old_cpath,
log);
lua_pop(L, 2);
}
}
lua_pop(L, 1); /* remove the "package" table */
ngx_http_lua_init_registry(L, log);
ngx_http_lua_init_globals(L, cycle, lmcf, log);
return L;
}
lua_State *
ngx_http_lua_new_thread(ngx_http_request_t *r, lua_State *L, int *ref)
{
int base;
lua_State *co;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua creating new thread");
base = lua_gettop(L);
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
coroutines_key));
lua_rawget(L, LUA_REGISTRYINDEX);
co = lua_newthread(L);
#ifndef OPENRESTY_LUAJIT
/* {{{ inherit coroutine's globals to main thread's globals table
* for print() function will try to find tostring() in current
* globals table.
*/
/* new globals table for coroutine */
ngx_http_lua_create_new_globals_table(co, 0, 0);
lua_createtable(co, 0, 1);
ngx_http_lua_get_globals_table(co);
lua_setfield(co, -2, "__index");
lua_setmetatable(co, -2);
ngx_http_lua_set_globals_table(co);
/* }}} */
#endif /* OPENRESTY_LUAJIT */
*ref = luaL_ref(L, -2);
if (*ref == LUA_NOREF) {
lua_settop(L, base); /* restore main thread stack */
return NULL;
}
lua_settop(L, base);
return co;
}
void
ngx_http_lua_del_thread(ngx_http_request_t *r, lua_State *L,
ngx_http_lua_ctx_t *ctx, ngx_http_lua_co_ctx_t *coctx)
{
if (coctx->co_ref == LUA_NOREF) {
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua deleting light thread");
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
coroutines_key));
lua_rawget(L, LUA_REGISTRYINDEX);
ngx_http_lua_probe_thread_delete(r, coctx->co, ctx);
luaL_unref(L, -1, coctx->co_ref);
coctx->co_ref = LUA_NOREF;
coctx->co_status = NGX_HTTP_LUA_CO_DEAD;
lua_pop(L, 1);
}
u_char *
ngx_http_lua_rebase_path(ngx_pool_t *pool, u_char *src, size_t len)
{
u_char *p;
ngx_str_t dst;
dst.data = ngx_palloc(pool, len + 1);
if (dst.data == NULL) {
return NULL;
}
dst.len = len;
p = ngx_copy(dst.data, src, len);
*p = '\0';
if (ngx_get_full_name(pool, (ngx_str_t *) &ngx_cycle->prefix, &dst)
!= NGX_OK)
{
return NULL;
}
return dst.data;
}
ngx_int_t
ngx_http_lua_send_header_if_needed(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx)
{
ngx_int_t rc;
dd("send header if needed: %d", r->header_sent || ctx->header_sent);
if (!r->header_sent && !ctx->header_sent) {
if (r->headers_out.status == 0) {
r->headers_out.status = NGX_HTTP_OK;
}
if (!ctx->mime_set
&& ngx_http_lua_set_content_type(r, ctx) != NGX_OK)
{
return NGX_ERROR;
}
if (!ctx->headers_set) {
ngx_http_clear_content_length(r);
ngx_http_clear_accept_ranges(r);
}
if (!ctx->buffering) {
dd("sending headers");
rc = ngx_http_send_header(r);
ctx->header_sent = 1;
return rc;
}
}
return NGX_OK;
}
ngx_int_t
ngx_http_lua_send_chain_link(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx,
ngx_chain_t *in)
{
ngx_int_t rc;
ngx_chain_t *cl;
ngx_chain_t **ll;
ngx_http_lua_loc_conf_t *llcf;
#if 1
if (ctx->acquired_raw_req_socket || ctx->eof) {
dd("ctx->eof already set or raw req socket already acquired");
return NGX_OK;
}
#endif
if ((r->method & NGX_HTTP_HEAD) && !r->header_only) {
r->header_only = 1;
}
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
if (llcf->http10_buffering
&& !ctx->buffering
&& !r->header_sent
&& !ctx->header_sent
&& r->http_version < NGX_HTTP_VERSION_11
&& r->headers_out.content_length_n < 0)
{
ctx->buffering = 1;
}
rc = ngx_http_lua_send_header_if_needed(r, ctx);
if (rc == NGX_ERROR || rc > NGX_OK) {
return rc;
}
if (r->header_only) {
ctx->eof = 1;
if (ctx->buffering) {
return ngx_http_lua_send_http10_headers(r, ctx);
}
return rc;
}
if (in == NULL) {
dd("last buf to be sent");
#if 1
if (!r->request_body && r == r->main) {
if (ngx_http_discard_request_body(r) != NGX_OK) {
return NGX_ERROR;
}
}
#endif
if (ctx->buffering) {
rc = ngx_http_lua_send_http10_headers(r, ctx);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
if (ctx->out) {
rc = ngx_http_lua_output_filter(r, ctx->out);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
ctx->out = NULL;
}
}
#if defined(nginx_version) && nginx_version <= 8004
/* earlier versions of nginx does not allow subrequests
to send last_buf themselves */
if (r != r->main) {
return NGX_OK;
}
#endif
ctx->eof = 1;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua sending last buf of the response body");
rc = ngx_http_lua_send_special(r, NGX_HTTP_LAST);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return NGX_OK;
}
/* in != NULL */
if (ctx->buffering) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua buffering output bufs for the HTTP 1.0 request");
for (cl = ctx->out, ll = &ctx->out; cl; cl = cl->next) {
ll = &cl->next;
}
*ll = in;
return NGX_OK;
}
return ngx_http_lua_output_filter(r, in);
}
static ngx_int_t
ngx_http_lua_send_special(ngx_http_request_t *r, ngx_uint_t flags)
{
ngx_int_t rc;
ngx_http_request_t *ar; /* active request */
ar = r->connection->data;
if (ar != r) {
/* bypass ngx_http_postpone_filter_module */
r->connection->data = r;
rc = ngx_http_send_special(r, flags);
r->connection->data = ar;
return rc;
}
return ngx_http_send_special(r, flags);
}
static ngx_int_t
ngx_http_lua_output_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
ngx_int_t rc;
ngx_http_lua_ctx_t *ctx;
ngx_http_request_t *ar; /* active request */
ar = r->connection->data;
if (ar != r) {
/* bypass ngx_http_postpone_filter_module */
r->connection->data = r;
rc = ngx_http_output_filter(r, in);
r->connection->data = ar;
return rc;
}
rc = ngx_http_output_filter(r, in);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
#if nginx_version >= 1001004
ngx_chain_update_chains(r->pool,
#else
ngx_chain_update_chains(
#endif
&ctx->free_bufs, &ctx->busy_bufs, &in,
(ngx_buf_tag_t) &ngx_http_lua_module);
return rc;
}
static ngx_int_t
ngx_http_lua_send_http10_headers(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx)
{
off_t size;
ngx_chain_t *cl;
ngx_int_t rc;
if (r->header_sent || ctx->header_sent) {
return NGX_OK;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua sending HTTP 1.0 response headers");
if (r->header_only) {
goto send;
}
if (r->headers_out.content_length == NULL) {
for (size = 0, cl = ctx->out; cl; cl = cl->next) {
size += ngx_buf_size(cl->buf);
}
r->headers_out.content_length_n = size;
if (r->headers_out.content_length) {
r->headers_out.content_length->hash = 0;
}
}
send:
rc = ngx_http_send_header(r);
ctx->header_sent = 1;
return rc;
}
static void
ngx_http_lua_init_registry(lua_State *L, ngx_log_t *log)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0,
"lua initializing lua registry");
/* {{{ register a table to anchor lua coroutines reliably:
* {([int]ref) = [cort]} */
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
coroutines_key));
lua_createtable(L, 0, 32 /* nrec */);
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
/* create the registry entry for the Lua request ctx data table */
lua_pushliteral(L, ngx_http_lua_ctx_tables_key);
lua_createtable(L, 0, 32 /* nrec */);
lua_rawset(L, LUA_REGISTRYINDEX);
/* create the registry entry for the Lua socket connection pool table */
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
socket_pool_key));
lua_createtable(L, 0, 8 /* nrec */);
lua_rawset(L, LUA_REGISTRYINDEX);
#if (NGX_PCRE)
/* create the registry entry for the Lua precompiled regex object cache */
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
regex_cache_key));
lua_createtable(L, 0, 16 /* nrec */);
lua_rawset(L, LUA_REGISTRYINDEX);
#endif
/* {{{ register table to cache user code:
* { [(string)cache_key] = <code closure> } */
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
code_cache_key));
lua_createtable(L, 0, 8 /* nrec */);
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
}
static void
ngx_http_lua_init_globals(lua_State *L, ngx_cycle_t *cycle,
ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0,
"lua initializing lua globals");
#if defined(NDK) && NDK
ngx_http_lua_inject_ndk_api(L);
#endif /* defined(NDK) && NDK */
ngx_http_lua_inject_ngx_api(L, lmcf, log);
}
static void
ngx_http_lua_inject_ngx_api(lua_State *L, ngx_http_lua_main_conf_t *lmcf,
ngx_log_t *log)
{
lua_createtable(L, 0 /* narr */, 117 /* nrec */); /* ngx.* */
lua_pushcfunction(L, ngx_http_lua_get_raw_phase_context);
lua_setfield(L, -2, "_phase_ctx");
ngx_http_lua_inject_arg_api(L);
ngx_http_lua_inject_http_consts(L);
ngx_http_lua_inject_core_consts(L);
ngx_http_lua_inject_log_api(L);
ngx_http_lua_inject_output_api(L);
ngx_http_lua_inject_time_api(L);
ngx_http_lua_inject_string_api(L);
ngx_http_lua_inject_control_api(log, L);
ngx_http_lua_inject_subrequest_api(L);
ngx_http_lua_inject_sleep_api(L);
ngx_http_lua_inject_phase_api(L);
#if (NGX_PCRE)
ngx_http_lua_inject_regex_api(L);
#endif
ngx_http_lua_inject_req_api(log, L);
ngx_http_lua_inject_resp_header_api(L);
ngx_http_lua_create_headers_metatable(log, L);
ngx_http_lua_inject_variable_api(L);
ngx_http_lua_inject_shdict_api(lmcf, L);
ngx_http_lua_inject_socket_tcp_api(log, L);
ngx_http_lua_inject_socket_udp_api(log, L);
ngx_http_lua_inject_uthread_api(log, L);
ngx_http_lua_inject_timer_api(L);
ngx_http_lua_inject_config_api(L);
ngx_http_lua_inject_worker_api(L);
ngx_http_lua_inject_misc_api(L);
lua_getglobal(L, "package"); /* ngx package */
lua_getfield(L, -1, "loaded"); /* ngx package loaded */
lua_pushvalue(L, -3); /* ngx package loaded ngx */
lua_setfield(L, -2, "ngx"); /* ngx package loaded */
lua_pop(L, 2);
lua_setglobal(L, "ngx");
ngx_http_lua_inject_coroutine_api(log, L);
#ifdef OPENRESTY_LUAJIT
{
int rc;
const char buf[] =
"local ngx_log = ngx.log\n"
"local ngx_WARN = ngx.WARN\n"
"local tostring = tostring\n"
"local ngx_get_phase = ngx.get_phase\n"
"local traceback = require 'debug'.traceback\n"
"local function newindex(table, key, value)\n"
"rawset(table, key, value)\n"
"local phase = ngx_get_phase()\n"
"if phase == 'init_worker' or phase == 'init' then\n"
"return\n"
"end\n"
"ngx_log(ngx_WARN, 'writing a global lua variable "
"(\\'', tostring(key), '\\') which may lead to "
"race conditions between concurrent requests, so "
"prefer the use of \\'local\\' variables', "
"traceback('', 2))\n"
"end\n"
"setmetatable(_G, { __newindex = newindex })\n"
;
rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "=_G write guard");
if (rc != 0) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"failed to load Lua code (%i): %s",
rc, lua_tostring(L, -1));
lua_pop(L, 1);
return;
}
rc = lua_pcall(L, 0, 0, 0);
if (rc != 0) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"failed to run Lua code (%i): %s",
rc, lua_tostring(L, -1));
lua_pop(L, 1);
}
}
#endif
}
void
ngx_http_lua_discard_bufs(ngx_pool_t *pool, ngx_chain_t *in)
{
ngx_chain_t *cl;
for (cl = in; cl; cl = cl->next) {
cl->buf->pos = cl->buf->last;
cl->buf->file_pos = cl->buf->file_last;
}
}
ngx_int_t
ngx_http_lua_add_copy_chain(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx,
ngx_chain_t ***plast, ngx_chain_t *in, ngx_int_t *eof)
{
ngx_chain_t *cl;
size_t len;
ngx_buf_t *b;
len = 0;
*eof = 0;
for (cl = in; cl; cl = cl->next) {
if (ngx_buf_in_memory(cl->buf)) {
len += cl->buf->last - cl->buf->pos;
}
if (cl->buf->last_in_chain || cl->buf->last_buf) {
*eof = 1;
}
}
if (len == 0) {
return NGX_OK;
}
cl = ngx_http_lua_chain_get_free_buf(r->connection->log, r->pool,
&ctx->free_bufs, len);
if (cl == NULL) {
return NGX_ERROR;
}
dd("chains get free buf: %d == %d", (int) (cl->buf->end - cl->buf->start),
(int) len);
b = cl->buf;
while (in) {
if (ngx_buf_in_memory(in->buf)) {
b->last = ngx_copy(b->last, in->buf->pos,
in->buf->last - in->buf->pos);
}
in = in->next;
}
**plast = cl;
*plast = &cl->next;
return NGX_OK;
}
void
ngx_http_lua_reset_ctx(ngx_http_request_t *r, lua_State *L,
ngx_http_lua_ctx_t *ctx)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua reset ctx");
ngx_http_lua_finalize_threads(r, ctx, L);
#if 0
if (ctx->user_co_ctx) {
/* no way to destroy a list but clean up the whole pool */
ctx->user_co_ctx = NULL;
}
#endif
ngx_memzero(&ctx->entry_co_ctx, sizeof(ngx_http_lua_co_ctx_t));
ctx->entry_co_ctx.co_ref = LUA_NOREF;
ctx->entered_rewrite_phase = 0;
ctx->entered_access_phase = 0;
ctx->entered_content_phase = 0;
ctx->exit_code = 0;
ctx->exited = 0;
ctx->resume_handler = ngx_http_lua_wev_handler;
ngx_str_null(&ctx->exec_uri);
ngx_str_null(&ctx->exec_args);
ctx->co_op = 0;
}
/* post read callback for rewrite and access phases */
void
ngx_http_lua_generic_phase_post_read(ngx_http_request_t *r)
{
ngx_http_lua_ctx_t *ctx;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua post read for rewrite/access phases");
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
ctx->read_body_done = 1;
#if defined(nginx_version) && nginx_version >= 8011
r->main->count--;
#endif
if (ctx->waiting_more_body) {
ctx->waiting_more_body = 0;
ngx_http_core_run_phases(r);
}
}
void
ngx_http_lua_request_cleanup_handler(void *data)
{
ngx_http_lua_ctx_t *ctx = data;
ngx_http_lua_request_cleanup(ctx, 0 /* forcible */);
}
void
ngx_http_lua_request_cleanup(ngx_http_lua_ctx_t *ctx, int forcible)
{
lua_State *L;
ngx_http_request_t *r;
ngx_http_lua_main_conf_t *lmcf;
/* force coroutine handling the request quit */
if (ctx == NULL) {
dd("ctx is NULL");
return;
}
r = ctx->request;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua request cleanup: forcible=%d", forcible);
if (ctx->cleanup) {
*ctx->cleanup = NULL;
ctx->cleanup = NULL;
}
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
#if 1
if (r->connection->fd == (ngx_socket_t) -1) {