-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathngx_http_lua_subrequest.c
1736 lines (1259 loc) · 47.4 KB
/
ngx_http_lua_subrequest.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 "ngx_http_lua_subrequest.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_ctx.h"
#include "ngx_http_lua_contentby.h"
#include "ngx_http_lua_headers_in.h"
#if defined(NGX_DTRACE) && NGX_DTRACE
#include "ngx_http_probe.h"
#endif
#define NGX_HTTP_LUA_SHARE_ALL_VARS 0x01
#define NGX_HTTP_LUA_COPY_ALL_VARS 0x02
#define ngx_http_lua_method_name(m) { sizeof(m) - 1, (u_char *) m " " }
ngx_str_t ngx_http_lua_get_method = ngx_http_lua_method_name("GET");
ngx_str_t ngx_http_lua_put_method = ngx_http_lua_method_name("PUT");
ngx_str_t ngx_http_lua_post_method = ngx_http_lua_method_name("POST");
ngx_str_t ngx_http_lua_head_method = ngx_http_lua_method_name("HEAD");
ngx_str_t ngx_http_lua_delete_method =
ngx_http_lua_method_name("DELETE");
ngx_str_t ngx_http_lua_options_method =
ngx_http_lua_method_name("OPTIONS");
ngx_str_t ngx_http_lua_copy_method = ngx_http_lua_method_name("COPY");
ngx_str_t ngx_http_lua_move_method = ngx_http_lua_method_name("MOVE");
ngx_str_t ngx_http_lua_lock_method = ngx_http_lua_method_name("LOCK");
ngx_str_t ngx_http_lua_mkcol_method =
ngx_http_lua_method_name("MKCOL");
ngx_str_t ngx_http_lua_propfind_method =
ngx_http_lua_method_name("PROPFIND");
ngx_str_t ngx_http_lua_proppatch_method =
ngx_http_lua_method_name("PROPPATCH");
ngx_str_t ngx_http_lua_unlock_method =
ngx_http_lua_method_name("UNLOCK");
ngx_str_t ngx_http_lua_patch_method =
ngx_http_lua_method_name("PATCH");
ngx_str_t ngx_http_lua_trace_method =
ngx_http_lua_method_name("TRACE");
static ngx_str_t ngx_http_lua_content_length_header_key =
ngx_string("Content-Length");
static ngx_int_t ngx_http_lua_adjust_subrequest(ngx_http_request_t *sr,
ngx_uint_t method, int forward_body,
ngx_http_request_body_t *body, unsigned vars_action,
ngx_array_t *extra_vars);
static int ngx_http_lua_ngx_location_capture(lua_State *L);
static int ngx_http_lua_ngx_location_capture_multi(lua_State *L);
static void ngx_http_lua_process_vars_option(ngx_http_request_t *r,
lua_State *L, int table, ngx_array_t **varsp);
static ngx_int_t ngx_http_lua_subrequest_add_extra_vars(ngx_http_request_t *r,
ngx_array_t *extra_vars);
static ngx_int_t ngx_http_lua_subrequest(ngx_http_request_t *r,
ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,
ngx_http_post_subrequest_t *ps, ngx_uint_t flags);
static ngx_int_t ngx_http_lua_subrequest_resume(ngx_http_request_t *r);
static void ngx_http_lua_handle_subreq_responses(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx);
static void ngx_http_lua_cancel_subreq(ngx_http_request_t *r);
static ngx_int_t ngx_http_post_request_to_head(ngx_http_request_t *r);
static ngx_int_t ngx_http_lua_copy_in_file_request_body(ngx_http_request_t *r);
static ngx_int_t ngx_http_lua_copy_request_headers(ngx_http_request_t *sr,
ngx_http_request_t *pr, int pr_not_chunked);
enum {
NGX_HTTP_LUA_SUBREQ_TRUNCATED = 1,
};
/* ngx.location.capture is just a thin wrapper around
* ngx.location.capture_multi */
static int
ngx_http_lua_ngx_location_capture(lua_State *L)
{
int n;
n = lua_gettop(L);
if (n != 1 && n != 2) {
return luaL_error(L, "expecting one or two arguments");
}
lua_createtable(L, n, 0); /* uri opts? table */
lua_insert(L, 1); /* table uri opts? */
if (n == 1) { /* table uri */
lua_rawseti(L, 1, 1); /* table */
} else { /* table uri opts */
lua_rawseti(L, 1, 2); /* table uri */
lua_rawseti(L, 1, 1); /* table */
}
lua_createtable(L, 1, 0); /* table table' */
lua_insert(L, 1); /* table' table */
lua_rawseti(L, 1, 1); /* table' */
return ngx_http_lua_ngx_location_capture_multi(L);
}
static int
ngx_http_lua_ngx_location_capture_multi(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_request_t *sr = NULL; /* subrequest object */
ngx_http_post_subrequest_t *psr;
ngx_http_lua_ctx_t *sr_ctx;
ngx_http_lua_ctx_t *ctx;
ngx_array_t *extra_vars;
ngx_str_t uri;
ngx_str_t args;
ngx_str_t extra_args;
ngx_uint_t flags;
u_char *p;
u_char *q;
size_t len;
size_t nargs;
int rc;
int n;
int always_forward_body = 0;
ngx_uint_t method;
ngx_http_request_body_t *body;
int type;
ngx_buf_t *b;
unsigned vars_action;
ngx_uint_t nsubreqs;
ngx_uint_t index;
size_t sr_statuses_len;
size_t sr_headers_len;
size_t sr_bodies_len;
size_t sr_flags_len;
size_t ofs1, ofs2;
unsigned custom_ctx;
ngx_http_lua_co_ctx_t *coctx;
ngx_http_lua_post_subrequest_data_t *psr_data;
n = lua_gettop(L);
if (n != 1) {
return luaL_error(L, "only one argument is expected, but got %d", n);
}
luaL_checktype(L, 1, LUA_TTABLE);
nsubreqs = lua_objlen(L, 1);
if (nsubreqs == 0) {
return luaL_error(L, "at least one subrequest should be specified");
}
r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
}
#if (NGX_HTTP_V2)
if (r->main->stream) {
return luaL_error(L, "http2 requests not supported yet");
}
#endif
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
| NGX_HTTP_LUA_CONTEXT_ACCESS
| NGX_HTTP_LUA_CONTEXT_CONTENT);
coctx = ctx->cur_co_ctx;
if (coctx == NULL) {
return luaL_error(L, "no co ctx found");
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua location capture, uri:\"%V\" c:%ud", &r->uri,
r->main->count);
sr_statuses_len = nsubreqs * sizeof(ngx_int_t);
sr_headers_len = nsubreqs * sizeof(ngx_http_headers_out_t *);
sr_bodies_len = nsubreqs * sizeof(ngx_str_t);
sr_flags_len = nsubreqs * sizeof(uint8_t);
p = ngx_pcalloc(r->pool, sr_statuses_len + sr_headers_len +
sr_bodies_len + sr_flags_len);
if (p == NULL) {
return luaL_error(L, "no memory");
}
coctx->sr_statuses = (void *) p;
p += sr_statuses_len;
coctx->sr_headers = (void *) p;
p += sr_headers_len;
coctx->sr_bodies = (void *) p;
p += sr_bodies_len;
coctx->sr_flags = (void *) p;
coctx->nsubreqs = nsubreqs;
coctx->pending_subreqs = 0;
extra_vars = NULL;
for (index = 0; index < nsubreqs; index++) {
coctx->pending_subreqs++;
lua_rawgeti(L, 1, index + 1);
if (lua_isnil(L, -1)) {
return luaL_error(L, "only array-like tables are allowed");
}
dd("queries query: top %d", lua_gettop(L));
if (lua_type(L, -1) != LUA_TTABLE) {
return luaL_error(L, "the query argument %d is not a table, "
"but a %s",
index, lua_typename(L, lua_type(L, -1)));
}
nargs = lua_objlen(L, -1);
if (nargs != 1 && nargs != 2) {
return luaL_error(L, "query argument %d expecting one or "
"two arguments", index);
}
lua_rawgeti(L, 2, 1); /* queries query uri */
dd("queries query uri: %d", lua_gettop(L));
dd("first arg in first query: %s", lua_typename(L, lua_type(L, -1)));
body = NULL;
ngx_str_null(&extra_args);
if (extra_vars != NULL) {
/* flush out existing elements in the array */
extra_vars->nelts = 0;
}
vars_action = 0;
custom_ctx = 0;
if (nargs == 2) {
/* check out the options table */
lua_rawgeti(L, 2, 2); /* queries query uri opts */
dd("queries query uri opts: %d", lua_gettop(L));
if (lua_type(L, 4) != LUA_TTABLE) {
return luaL_error(L, "expecting table as the 2nd argument for "
"subrequest %d, but got %s", index,
luaL_typename(L, 4));
}
dd("queries query uri opts: %d", lua_gettop(L));
/* check the args option */
lua_getfield(L, 4, "args");
type = lua_type(L, -1);
switch (type) {
case LUA_TTABLE:
ngx_http_lua_process_args_option(r, L, -1, &extra_args);
break;
case LUA_TNIL:
/* do nothing */
break;
case LUA_TNUMBER:
case LUA_TSTRING:
extra_args.data = (u_char *) lua_tolstring(L, -1, &len);
extra_args.len = len;
break;
default:
return luaL_error(L, "Bad args option value");
}
lua_pop(L, 1);
dd("queries query uri opts: %d", lua_gettop(L));
/* check the vars option */
lua_getfield(L, 4, "vars");
switch (lua_type(L, -1)) {
case LUA_TTABLE:
ngx_http_lua_process_vars_option(r, L, -1, &extra_vars);
dd("post process vars top: %d", lua_gettop(L));
break;
case LUA_TNIL:
/* do nothing */
break;
default:
return luaL_error(L, "Bad vars option value");
}
lua_pop(L, 1);
dd("queries query uri opts: %d", lua_gettop(L));
/* check the share_all_vars option */
lua_getfield(L, 4, "share_all_vars");
switch (lua_type(L, -1)) {
case LUA_TNIL:
/* do nothing */
break;
case LUA_TBOOLEAN:
if (lua_toboolean(L, -1)) {
vars_action |= NGX_HTTP_LUA_SHARE_ALL_VARS;
}
break;
default:
return luaL_error(L, "Bad share_all_vars option value");
}
lua_pop(L, 1);
dd("queries query uri opts: %d", lua_gettop(L));
/* check the copy_all_vars option */
lua_getfield(L, 4, "copy_all_vars");
switch (lua_type(L, -1)) {
case LUA_TNIL:
/* do nothing */
break;
case LUA_TBOOLEAN:
if (lua_toboolean(L, -1)) {
vars_action |= NGX_HTTP_LUA_COPY_ALL_VARS;
}
break;
default:
return luaL_error(L, "Bad copy_all_vars option value");
}
lua_pop(L, 1);
dd("queries query uri opts: %d", lua_gettop(L));
/* check the "forward_body" option */
lua_getfield(L, 4, "always_forward_body");
always_forward_body = lua_toboolean(L, -1);
lua_pop(L, 1);
dd("always forward body: %d", always_forward_body);
/* check the "method" option */
lua_getfield(L, 4, "method");
type = lua_type(L, -1);
if (type == LUA_TNIL) {
method = NGX_HTTP_GET;
} else {
if (type != LUA_TNUMBER) {
return luaL_error(L, "Bad http request method");
}
method = (ngx_uint_t) lua_tonumber(L, -1);
}
lua_pop(L, 1);
dd("queries query uri opts: %d", lua_gettop(L));
/* check the "ctx" option */
lua_getfield(L, 4, "ctx");
type = lua_type(L, -1);
if (type != LUA_TNIL) {
if (type != LUA_TTABLE) {
return luaL_error(L, "Bad ctx option value type %s, "
"expected a Lua table",
lua_typename(L, type));
}
custom_ctx = 1;
} else {
lua_pop(L, 1);
}
dd("queries query uri opts ctx?: %d", lua_gettop(L));
/* check the "body" option */
lua_getfield(L, 4, "body");
type = lua_type(L, -1);
if (type != LUA_TNIL) {
if (type != LUA_TSTRING && type != LUA_TNUMBER) {
return luaL_error(L, "Bad http request body");
}
body = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
if (body == NULL) {
return luaL_error(L, "no memory");
}
q = (u_char *) lua_tolstring(L, -1, &len);
dd("request body: [%.*s]", (int) len, q);
if (len) {
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return luaL_error(L, "no memory");
}
b->last = ngx_copy(b->last, q, len);
body->bufs = ngx_alloc_chain_link(r->pool);
if (body->bufs == NULL) {
return luaL_error(L, "no memory");
}
body->bufs->buf = b;
body->bufs->next = NULL;
body->buf = b;
}
}
lua_pop(L, 1); /* pop the body */
/* stack: queries query uri opts ctx? */
lua_remove(L, 4);
/* stack: queries query uri ctx? */
dd("queries query uri ctx?: %d", lua_gettop(L));
} else {
method = NGX_HTTP_GET;
}
/* stack: queries query uri ctx? */
p = (u_char *) luaL_checklstring(L, 3, &len);
uri.data = ngx_palloc(r->pool, len);
if (uri.data == NULL) {
return luaL_error(L, "memory allocation error");
}
ngx_memcpy(uri.data, p, len);
uri.len = len;
ngx_str_null(&args);
flags = 0;
rc = ngx_http_parse_unsafe_uri(r, &uri, &args, &flags);
if (rc != NGX_OK) {
dd("rc = %d", (int) rc);
return luaL_error(L, "unsafe uri in argument #1: %s", p);
}
if (args.len == 0) {
if (extra_args.len) {
p = ngx_palloc(r->pool, extra_args.len);
if (p == NULL) {
return luaL_error(L, "no memory");
}
ngx_memcpy(p, extra_args.data, extra_args.len);
args.data = p;
args.len = extra_args.len;
}
} else if (extra_args.len) {
/* concatenate the two parts of args together */
len = args.len + (sizeof("&") - 1) + extra_args.len;
p = ngx_palloc(r->pool, len);
if (p == NULL) {
return luaL_error(L, "no memory");
}
q = ngx_copy(p, args.data, args.len);
*q++ = '&';
ngx_memcpy(q, extra_args.data, extra_args.len);
args.data = p;
args.len = len;
}
ofs1 = ngx_align(sizeof(ngx_http_post_subrequest_t), sizeof(void *));
ofs2 = ngx_align(sizeof(ngx_http_lua_ctx_t), sizeof(void *));
p = ngx_palloc(r->pool, ofs1 + ofs2
+ sizeof(ngx_http_lua_post_subrequest_data_t));
if (p == NULL) {
return luaL_error(L, "no memory");
}
psr = (ngx_http_post_subrequest_t *) p;
p += ofs1;
sr_ctx = (ngx_http_lua_ctx_t *) p;
ngx_http_lua_assert((void *) sr_ctx == ngx_align_ptr(sr_ctx,
sizeof(void *)));
p += ofs2;
psr_data = (ngx_http_lua_post_subrequest_data_t *) p;
ngx_http_lua_assert((void *) psr_data == ngx_align_ptr(psr_data,
sizeof(void *)));
ngx_memzero(sr_ctx, sizeof(ngx_http_lua_ctx_t));
/* set by ngx_memzero:
* sr_ctx->run_post_subrequest = 0
* sr_ctx->free = NULL
* sr_ctx->body = NULL
*/
psr_data->ctx = sr_ctx;
psr_data->pr_co_ctx = coctx;
psr->handler = ngx_http_lua_post_subrequest;
psr->data = psr_data;
rc = ngx_http_lua_subrequest(r, &uri, &args, &sr, psr, 0);
if (rc != NGX_OK) {
return luaL_error(L, "failed to issue subrequest: %d", (int) rc);
}
ngx_http_lua_init_ctx(sr, sr_ctx);
sr_ctx->capture = 1;
sr_ctx->index = index;
sr_ctx->last_body = &sr_ctx->body;
sr_ctx->vm_state = ctx->vm_state;
ngx_http_set_ctx(sr, sr_ctx, ngx_http_lua_module);
rc = ngx_http_lua_adjust_subrequest(sr, method, always_forward_body,
body, vars_action, extra_vars);
if (rc != NGX_OK) {
ngx_http_lua_cancel_subreq(sr);
return luaL_error(L, "failed to adjust the subrequest: %d",
(int) rc);
}
dd("queries query uri opts ctx? %d", lua_gettop(L));
/* stack: queries query uri ctx? */
if (custom_ctx) {
ngx_http_lua_ngx_set_ctx_helper(L, sr, sr_ctx, -1);
lua_pop(L, 3);
} else {
lua_pop(L, 2);
}
/* stack: queries */
}
if (extra_vars) {
ngx_array_destroy(extra_vars);
}
ctx->no_abort = 1;
return lua_yield(L, 0);
}
static ngx_int_t
ngx_http_lua_adjust_subrequest(ngx_http_request_t *sr, ngx_uint_t method,
int always_forward_body, ngx_http_request_body_t *body,
unsigned vars_action, ngx_array_t *extra_vars)
{
ngx_http_request_t *r;
ngx_http_core_main_conf_t *cmcf;
int pr_not_chunked = 0;
size_t size;
r = sr->parent;
sr->header_in = r->header_in;
if (body) {
sr->request_body = body;
} else if (!always_forward_body
&& method != NGX_HTTP_PUT
&& method != NGX_HTTP_POST
&& r->headers_in.content_length_n > 0)
{
sr->request_body = NULL;
} else {
if (!r->headers_in.chunked) {
pr_not_chunked = 1;
}
if (sr->request_body && sr->request_body->temp_file) {
/* deep-copy the request body */
if (ngx_http_lua_copy_in_file_request_body(sr) != NGX_OK) {
return NGX_ERROR;
}
}
}
if (ngx_http_lua_copy_request_headers(sr, r, pr_not_chunked) != NGX_OK) {
return NGX_ERROR;
}
sr->method = method;
switch (method) {
case NGX_HTTP_GET:
sr->method_name = ngx_http_lua_get_method;
break;
case NGX_HTTP_POST:
sr->method_name = ngx_http_lua_post_method;
break;
case NGX_HTTP_PUT:
sr->method_name = ngx_http_lua_put_method;
break;
case NGX_HTTP_HEAD:
sr->method_name = ngx_http_lua_head_method;
break;
case NGX_HTTP_DELETE:
sr->method_name = ngx_http_lua_delete_method;
break;
case NGX_HTTP_OPTIONS:
sr->method_name = ngx_http_lua_options_method;
break;
case NGX_HTTP_MKCOL:
sr->method_name = ngx_http_lua_mkcol_method;
break;
case NGX_HTTP_COPY:
sr->method_name = ngx_http_lua_copy_method;
break;
case NGX_HTTP_MOVE:
sr->method_name = ngx_http_lua_move_method;
break;
case NGX_HTTP_PROPFIND:
sr->method_name = ngx_http_lua_propfind_method;
break;
case NGX_HTTP_PROPPATCH:
sr->method_name = ngx_http_lua_proppatch_method;
break;
case NGX_HTTP_LOCK:
sr->method_name = ngx_http_lua_lock_method;
break;
case NGX_HTTP_UNLOCK:
sr->method_name = ngx_http_lua_unlock_method;
break;
case NGX_HTTP_PATCH:
sr->method_name = ngx_http_lua_patch_method;
break;
case NGX_HTTP_TRACE:
sr->method_name = ngx_http_lua_trace_method;
break;
default:
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"unsupported HTTP method: %ui", method);
return NGX_ERROR;
}
if (!(vars_action & NGX_HTTP_LUA_SHARE_ALL_VARS)) {
/* we do not inherit the parent request's variables */
cmcf = ngx_http_get_module_main_conf(sr, ngx_http_core_module);
size = cmcf->variables.nelts * sizeof(ngx_http_variable_value_t);
if (vars_action & NGX_HTTP_LUA_COPY_ALL_VARS) {
sr->variables = ngx_palloc(sr->pool, size);
if (sr->variables == NULL) {
return NGX_ERROR;
}
ngx_memcpy(sr->variables, r->variables, size);
} else {
/* we do not inherit the parent request's variables */
sr->variables = ngx_pcalloc(sr->pool, size);
if (sr->variables == NULL) {
return NGX_ERROR;
}
}
}
return ngx_http_lua_subrequest_add_extra_vars(sr, extra_vars);
}
static ngx_int_t
ngx_http_lua_subrequest_add_extra_vars(ngx_http_request_t *sr,
ngx_array_t *extra_vars)
{
ngx_http_core_main_conf_t *cmcf;
ngx_http_variable_t *v;
ngx_http_variable_value_t *vv;
u_char *val;
u_char *p;
ngx_uint_t i, hash;
ngx_str_t name;
size_t len;
ngx_hash_t *variables_hash;
ngx_keyval_t *var;
/* set any extra variables that were passed to the subrequest */
if (extra_vars == NULL || extra_vars->nelts == 0) {
return NGX_OK;
}
cmcf = ngx_http_get_module_main_conf(sr, ngx_http_core_module);
variables_hash = &cmcf->variables_hash;
var = extra_vars->elts;
for (i = 0; i < extra_vars->nelts; i++, var++) {
/* copy the variable's name and value because they are allocated
* by the lua VM */
len = var->key.len + var->value.len;
p = ngx_pnalloc(sr->pool, len);
if (p == NULL) {
return NGX_ERROR;
}
name.data = p;
name.len = var->key.len;
p = ngx_copy(p, var->key.data, var->key.len);
hash = ngx_hash_strlow(name.data, name.data, name.len);
val = p;
len = var->value.len;
ngx_memcpy(p, var->value.data, len);
v = ngx_hash_find(variables_hash, hash, name.data, name.len);
if (v) {
if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {
ngx_log_error(NGX_LOG_ERR, sr->connection->log, 0,
"variable \"%V\" not changeable", &name);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (v->set_handler) {
vv = ngx_palloc(sr->pool, sizeof(ngx_http_variable_value_t));
if (vv == NULL) {
return NGX_ERROR;
}
vv->valid = 1;
vv->not_found = 0;
vv->no_cacheable = 0;
vv->data = val;
vv->len = len;
v->set_handler(sr, vv, v->data);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, sr->connection->log, 0,
"variable \"%V\" set to value \"%v\"", &name,
vv);
continue;
}
if (v->flags & NGX_HTTP_VAR_INDEXED) {
vv = &sr->variables[v->index];
vv->valid = 1;
vv->not_found = 0;
vv->no_cacheable = 0;
vv->data = val;
vv->len = len;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, sr->connection->log, 0,
"variable \"%V\" set to value \"%v\"",
&name, vv);
continue;
}
}
ngx_log_error(NGX_LOG_ERR, sr->connection->log, 0,
"variable \"%V\" cannot be assigned a value (maybe you "
"forgot to define it first?) ", &name);
return NGX_ERROR;
}
return NGX_OK;
}
static void
ngx_http_lua_process_vars_option(ngx_http_request_t *r, lua_State *L,
int table, ngx_array_t **varsp)
{
ngx_array_t *vars;
ngx_keyval_t *var;
if (table < 0) {
table = lua_gettop(L) + table + 1;
}
vars = *varsp;
if (vars == NULL) {
vars = ngx_array_create(r->pool, 4, sizeof(ngx_keyval_t));
if (vars == NULL) {
dd("here");
luaL_error(L, "no memory");
return;
}
*varsp = vars;
}
lua_pushnil(L);
while (lua_next(L, table) != 0) {
if (lua_type(L, -2) != LUA_TSTRING) {
luaL_error(L, "attempt to use a non-string key in the "
"\"vars\" option table");
return;
}
if (!lua_isstring(L, -1)) {
luaL_error(L, "attempt to use bad variable value type %s",
luaL_typename(L, -1));
return;
}
var = ngx_array_push(vars);
if (var == NULL) {
dd("here");
luaL_error(L, "no memory");
return;
}
var->key.data = (u_char *) lua_tolstring(L, -2, &var->key.len);
var->value.data = (u_char *) lua_tolstring(L, -1, &var->value.len);
lua_pop(L, 1);
}
}
ngx_int_t
ngx_http_lua_post_subrequest(ngx_http_request_t *r, void *data, ngx_int_t rc)
{
ngx_http_request_t *pr;
ngx_http_lua_ctx_t *pr_ctx;
ngx_http_lua_ctx_t *ctx; /* subrequest ctx */
ngx_http_lua_co_ctx_t *pr_coctx;
size_t len;
ngx_str_t *body_str;
u_char *p;
ngx_chain_t *cl;
ngx_http_lua_post_subrequest_data_t *psr_data = data;
ctx = psr_data->ctx;
if (ctx->run_post_subrequest) {
if (r != r->connection->data) {
r->connection->data = r;
}
return NGX_OK;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua run post subrequest handler, rc:%i c:%ud", rc,
r->main->count);
ctx->run_post_subrequest = 1;
pr = r->parent;
pr_ctx = ngx_http_get_module_ctx(pr, ngx_http_lua_module);
if (pr_ctx == NULL) {
return NGX_ERROR;
}
pr_coctx = psr_data->pr_co_ctx;
pr_coctx->pending_subreqs--;
if (pr_coctx->pending_subreqs == 0) {
dd("all subrequests are done");
pr_ctx->no_abort = 0;
pr_ctx->resume_handler = ngx_http_lua_subrequest_resume;
pr_ctx->cur_co_ctx = pr_coctx;
}
if (pr_ctx->entered_content_phase) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua restoring write event handler");
pr->write_event_handler = ngx_http_lua_content_wev_handler;
} else {
pr->write_event_handler = ngx_http_core_run_phases;
}
dd("status rc = %d", (int) rc);
dd("status headers_out.status = %d", (int) r->headers_out.status);
dd("uri: %.*s", (int) r->uri.len, r->uri.data);
/* capture subrequest response status */