forked from flygoast/libvmod-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmod_lua.c
2135 lines (1525 loc) · 44.5 KB
/
vmod_lua.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include <sys/time.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "vrt.h"
#include "vsa.h"
#include "vrt_obj.h"
#include "cache/cache.h"
#include "vcc_if.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#define DEBUG 1
#define LIBVMOD_LUA_VERSION "0.6"
#define LIBVMOD_LUA_AUTHOR "Gu Feng <[email protected]>"
#define LOG_E(...) fprintf(stderr, __VA_ARGS__);
#ifdef DEBUG
# define LOG_T(...) fprintf(stderr, __VA_ARGS__);
#else
# define LOG_T(...) do {} while(0);
#endif
#define VARNISH_CONTEXT_KEY "__varnish_context_key"
#define VARNISH_XID_KEY "__varnish_xid_key"
jmp_buf jmpbuffer;
static pthread_once_t thread_once = PTHREAD_ONCE_INIT;
static pthread_key_t thread_vm_key;
static pthread_key_t request_vm_key;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef int (*handler_t)(lua_State *L, const struct vrt_ctx *ctx);
typedef struct {
char *name;
int len;
handler_t handler;
} var_handler_t;
typedef struct {
lua_State *L;
} lua_config_t;
static int traceback(lua_State *L);
static int atpanic(lua_State *L);
static void make_thread_key();
static lua_State *new_lua_state(const char *path, const char *cpath);
static lua_State *new_lua_thread(lua_State *BL);
static void free_lua_state(void *L);
static void free_config(lua_config_t *cfg);
static struct vrt_ctx *get_ctx(lua_State *L);
static void set_ctx(lua_State *L, const struct vrt_ctx *ctx);
static unsigned int get_xid(lua_State *L);
static void set_xid(lua_State *L, unsigned int xid);
static int vcl_var_get(lua_State *L, var_handler_t *vh);
static int vcl_forbidden_set(lua_State *L);
static void inject_bereq(lua_State *L);
static void inject_bereq_http(lua_State *L);
static int vcl_bereq_get(lua_State *L);
static void inject_beresp(lua_State *L);
static void inject_beresp_http(lua_State *L);
static void inject_beresp_backend(lua_State *L);
static int vcl_beresp_get(lua_State *L);
static int vcl_beresp_backend_get(lua_State *L);
static void inject_client(lua_State *L);
static int vcl_client_get(lua_State *L);
static void inject_local(lua_State *L);
static int vcl_local_get(lua_State *L);
static void inject_obj(lua_State *L);
static void inject_obj_http(lua_State *L);
static int vcl_obj_get(lua_State *L);
static void inject_remote(lua_State *L);
static int vcl_remote_get(lua_State *L);
static void inject_req(lua_State *L);
static void inject_req_http(lua_State *L);
static int vcl_req_get(lua_State *L);
static void inject_resp(lua_State *L);
static void inject_resp_http(lua_State *L);
static int vcl_resp_get(lua_State *L);
static void inject_server(lua_State *L);
static int vcl_server_get(lua_State *L);
static void inject_varnish(lua_State *L);
static int vcl_varnish_get(lua_State *L);
static int vcl_bereq_between_bytes_timeout(lua_State *L,
const struct vrt_ctx *ctx);
static int vcl_bereq_connect_timeout(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_bereq_first_byte_timeout(lua_State *L,
const struct vrt_ctx *ctx);
static int vcl_bereq_method(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_bereq_proto(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_bereq_retries(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_bereq_uncacheable(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_bereq_url(http://github.com/lua_State%20*L,%20const%20struct%20vrt_ctx%20*ctx);
static int vcl_bereq_xid(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_bereq_http_get(lua_State *L);
static int vcl_beresp_age(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_do_esi(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_do_gunzip(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_do_gzip(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_do_stream(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_grace(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_keep(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_proto(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_reason(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_status(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_storage_hint(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_ttl(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_uncacheable(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_was_304(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_backend_name(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_backend_ip(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_backend_port(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_beresp_http_get(lua_State *L);
static int vcl_client_ip(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_client_identity(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_local_ip(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_now(lua_State *, const struct vrt_ctx *ctx);
static int vcl_obj_age(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_obj_grace(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_obj_hits(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_obj_keep(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_obj_proto(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_obj_reason(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_obj_status(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_obj_ttl(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_obj_uncacheable(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_obj_http_get(lua_State *L);
static int vcl_remote_ip(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_can_gzip(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_esi(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_esi_level(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_hash_always_miss(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_hash_ignore_busy(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_method(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_proto(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_restarts(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_ttl(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_url(http://github.com/lua_State%20*L,%20const%20struct%20vrt_ctx%20*ctx);
static int vcl_req_xid(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_req_http_get(lua_State *L);
static int vcl_resp_is_streaming(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_resp_proto(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_resp_reason(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_resp_status(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_resp_http_get(lua_State *L);
static int vcl_server_hostname(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_server_identity(lua_State *L, const struct vrt_ctx *ctx);
static int vcl_server_ip(lua_State *L, const struct vrt_ctx *ctx);
static var_handler_t bereq_handlers[] = {
{ "between_bytes_timeout", sizeof("first_byte_timeout"),
vcl_bereq_between_bytes_timeout },
{ "connect_timeout", sizeof("connect_timeout") - 1,
vcl_bereq_connect_timeout },
{ "first_byte_timeout", sizeof("first_byte_timeout"),
vcl_bereq_first_byte_timeout },
{ "method", sizeof("method") - 1, vcl_bereq_method },
{ "proto", sizeof("proto") - 1, vcl_bereq_proto },
{ "retries", sizeof("retries") - 1, vcl_bereq_retries },
{ "uncacheable", sizeof("uncacheable") - 1, vcl_bereq_uncacheable },
{ "url", sizeof("url") - 1, vcl_bereq_url },
{ "xid", sizeof("xid") - 1, vcl_bereq_xid },
{ NULL, 0, NULL }
};
static var_handler_t beresp_handlers[] = {
{ "age", sizeof("age") - 1, vcl_beresp_age },
{ "do_esi", sizeof("do_esi") - 1, vcl_beresp_do_esi },
{ "do_gunzip", sizeof("do_gunzip") - 1, vcl_beresp_do_gunzip },
{ "do_gzip", sizeof("do_gzip") - 1, vcl_beresp_do_gzip },
{ "do_stream", sizeof("do_stream") - 1, vcl_beresp_do_stream },
{ "grace", sizeof("grace") - 1, vcl_beresp_grace },
{ "keep", sizeof("keep") - 1, vcl_beresp_keep },
{ "proto", sizeof("proto") - 1, vcl_beresp_proto },
{ "reason", sizeof("reason") - 1, vcl_beresp_reason },
{ "status", sizeof("status") - 1, vcl_beresp_status },
{ "storage_hint", sizeof("storage_hint") - 1, vcl_beresp_storage_hint },
{ "ttl", sizeof("ttl") - 1, vcl_beresp_ttl },
{ "uncacheable", sizeof("uncacheable") - 1, vcl_beresp_uncacheable },
{ "was_304", sizeof("was_304") - 1, vcl_beresp_was_304 },
{ NULL, 0, NULL }
};
static var_handler_t beresp_backend_handlers[] = {
{ "name", sizeof("name") - 1, vcl_beresp_backend_name },
{ "ip", sizeof("ip") - 1, vcl_beresp_backend_ip },
{ "port", sizeof("port") - 1, vcl_beresp_backend_port },
{ NULL, 0, NULL }
};
static var_handler_t client_handlers[] = {
{ "ip", sizeof("ip") - 1, vcl_client_ip },
{ "identity", sizeof("identity") - 1, vcl_client_identity },
{ NULL, 0, NULL}
};
static var_handler_t local_handlers[] = {
{ "ip", sizeof("ip") - 1, vcl_local_ip },
{ NULL, 0, NULL }
};
static var_handler_t varnish_handlers[] = {
{ "now", sizeof("now") - 1, vcl_now },
{ NULL, 0, NULL}
};
static var_handler_t obj_handlers[] = {
{ "age", sizeof("") - 1, vcl_obj_age },
{ "grace", sizeof("grace") - 1, vcl_obj_grace },
{ "hits", sizeof("hits") - 1, vcl_obj_hits },
{ "keep", sizeof("keep") - 1, vcl_obj_keep },
{ "proto", sizeof("proto") - 1, vcl_obj_proto },
{ "reason", sizeof("reason") - 1, vcl_obj_reason },
{ "status", sizeof("status") - 1, vcl_obj_status },
{ "ttl", sizeof("ttl") - 1, vcl_obj_ttl },
{ "uncacheable", sizeof("uncacheable") - 1, vcl_obj_uncacheable },
{ NULL, 0, NULL }
};
static var_handler_t remote_handlers[] = {
{ "ip", sizeof("ip") - 1, vcl_remote_ip },
{ NULL, 0, NULL }
};
static var_handler_t req_handlers[] = {
{ "can_gzip", sizeof("can_gzip") - 1, vcl_req_can_gzip },
{ "esi", sizeof("esi") - 1, vcl_req_esi },
{ "esi_level", sizeof("esi_level") - 1, vcl_req_esi_level },
{ "hash_always_miss", sizeof("hash_always_miss") - 1,
vcl_req_hash_always_miss },
{ "hash_ignore_busy", sizeof("hash_ignore_busy") - 1,
vcl_req_hash_ignore_busy },
{ "method", sizeof("method") -1, vcl_req_method },
{ "proto", sizeof("proto") - 1, vcl_req_proto },
{ "restarts", sizeof("restarts") - 1, vcl_req_restarts },
{ "ttl", sizeof("ttl") - 1, vcl_req_ttl },
{ "url", sizeof("url") - 1, vcl_req_url },
{ "xid", sizeof("xid") - 1, vcl_req_xid },
{ NULL, 0, NULL }
};
static var_handler_t resp_handlers[] = {
{ "is_streaming", sizeof("is_streaming") - 1, vcl_resp_is_streaming },
{ "proto", sizeof("proto") - 1, vcl_resp_proto },
{ "reason", sizeof("reason") - 1, vcl_resp_reason },
{ "status", sizeof("status") - 1, vcl_resp_status },
};
static var_handler_t server_handlers[] = {
{ "hostname", sizeof("hostname") - 1, vcl_server_hostname },
{ "identity", sizeof("identity") - 1, vcl_server_identity },
{ "ip", sizeof("ip") - 1, vcl_server_ip },
{ NULL, 0, NULL }
};
/************************* Exported functions ***************************/
/* "import lua" */
int
init_function(struct vmod_priv *priv, const struct VCL_conf *conf)
{
LOG_T("VMOD[lua] init_function called\n");
pthread_once(&thread_once, make_thread_key);
priv->free = (vmod_priv_free_f *) free_config;
return 0;
}
/* "lua.init('/path/to/?.lua;', '/cpath/to/?.so;', '/path/to/foo.lua')" */
void
vmod_init(const struct vrt_ctx *ctx, struct vmod_priv *priv,
const char *path, const char *cpath, const char *luafile)
{
lua_config_t *cfg;
lua_State *L;
int base;
if (priv->priv != NULL) {
LOG_E("VMOD[lua] init should only be invoked once\n");
return;
}
LOG_T("VMOD[lua] init called\n");
L = new_lua_state(path, cpath);
if (L == NULL) {
LOG_E("VMOD[lua] init failed: create Lua state failed\n");
return;
}
if (luaL_loadfile(L, luafile) != 0) {
LOG_E("VMOD[lua] luaL_loadfile(\"%s\") failed, errstr=\"%s\"\n",
luafile, luaL_checkstring(L, -1));
lua_close(L);
return;
}
base = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_insert(L, base);
if (lua_pcall(L, 0, 0, base) != 0) {
LOG_E("VMOD[lua] lua_pcall(\"%s\") failed, errstr=\"%s\"\n",
luafile, luaL_checkstring(L, -1));
lua_close(L);
return;
}
lua_remove(L, base);
if (lua_gettop(L) != 0) {
LOG_E("VMOD[lua] lua VM stack not empty, top: %d\n", lua_gettop(L));
lua_close(L);
return;
}
cfg = malloc(sizeof(lua_config_t));
if (cfg == NULL) {
LOG_E("VMOD[lua] init failed: no memory\n");
return;
}
cfg->L = L;
priv->priv = cfg;
}
/* "lua.call(function)" */
const char *
vmod_call(const struct vrt_ctx *ctx, struct vmod_priv *priv,
const char *function, const char *value)
{
lua_State *L, *BL;
const char *ret = NULL;
unsigned int oxid;
int base, type;
size_t len;
LOG_T("VMOD[lua] lua.call(\"%s\") %u\n", function, (unsigned int) pthread_self());
if (ctx == NULL) {
LOG_E("VMOD[lua] \"call\" can not be called in this VCL\n");
return NULL;
}
if (priv->priv == NULL) {
LOG_E("VMOD[lua] \"init\" should be called first in vcl_init\n");
return NULL;
}
/* create a Lua state owned only by this thread */
BL = pthread_getspecific(thread_vm_key);
if (BL == NULL) {
pthread_mutex_lock(&mutex);
BL = new_lua_thread(((lua_config_t *) priv->priv)->L);
pthread_mutex_unlock(&mutex);
pthread_setspecific(thread_vm_key, BL);
}
L = pthread_getspecific(request_vm_key);
if (L == NULL) {
LOG_T("VMOD[lua] spawn a new Lua thread\n");
/* stack top of BL is new thread */
L = new_lua_thread(BL);
set_ctx(L, ctx);
set_xid(L, VXID(ctx->req->vsl->wid));
pthread_setspecific(request_vm_key, L);
} else {
oxid = get_xid(L);
if (oxid != VXID(ctx->req->vsl->wid)) {
LOG_T("VMOD[lua] spawn a new Lua thread to process new request\n");
if (!lua_isthread(BL, -1)) {
LOG_E("VMOD[lua] Lua VM crashed, top is not Lua thread\n");
lua_settop(BL, 0);
return NULL;
}
/* pop the old thread */
lua_pop(BL, 1);
/* another request */
L = new_lua_thread(BL);
set_ctx(L, ctx);
set_xid(L, VXID(ctx->req->vsl->wid));
pthread_setspecific(request_vm_key, L);
#ifdef DEBUG
} else {
LOG_T("VMOD[lua] use old Lua thread\n");
#endif
}
}
lua_getglobal(L, function);
if (!lua_isfunction(L, -1)) {
LOG_E("VMOD[lua] global function \"%s\" not fould in lua module\n",
function);
lua_pop(L, 1);
return NULL;
}
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_setfenv(L, -2);
lua_atpanic(L, atpanic);
if (setjmp(jmpbuffer) == 0) {
base = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_insert(L, base);
lua_pushstring(L, value);
if (lua_pcall(L, 1, 1, base) != 0) {
LOG_E("VMOD[lua] call function \"%s\" failed, errstr=\"%s\"\n",
function, luaL_checkstring(L, -1));
/* clear Lua stack to execute other functions */
lua_settop(L, 0);
return NULL;
}
lua_remove(L, base);
type = lua_type(L, 1);
switch (type) {
case LUA_TNIL:
LOG_T("VMOD[lua] function \"%s\" returned nil\n",
function);
lua_pop(L, 1);
return NULL;
case LUA_TSTRING:
case LUA_TNUMBER:
break;
default:
LOG_E("VMOD[lua] function \"%s\" returned type \"%s\""
", should return string or nil\n",
function, lua_typename(L, type));
lua_pop(L, 1);
return NULL;
}
ret = lua_tolstring(L, 1, &len);
ret = WS_Copy(ctx->ws, ret, len);
if (ret == NULL) {
LOG_E("VMOD[lua] WS_Dup failed\n");
lua_pop(L, 1);
return NULL;
}
lua_pop(L, 1);
} else {
LOG_T("VMOD[lua] varnish execution restored\n");
}
/* clear Lua stack */
lua_settop(L, 0);
return ret;
}
/* "lua.cleanup()" */
void
vmod_cleanup(const struct vrt_ctx *ctx, struct vmod_priv *priv)
{
LOG_T("VMOD[lua] cleanup called\n");
if (ctx != NULL) {
LOG_E("VMOD[lua] cleanup should called in vcl_fini\n");
return;
}
if (priv->priv) {
free_config(priv->priv);
}
}
const char *
vmod_author(const struct vrt_ctx *ctx)
{
(void) ctx;
return LIBVMOD_LUA_AUTHOR;
}
const char *
vmod_version(const struct vrt_ctx *ctx)
{
(void) ctx;
return LIBVMOD_LUA_VERSION;
}
/********************* utility functions ********************/
static int
traceback(lua_State *L)
{
if (!lua_isstring(L, 1)) {
return 1;
}
lua_getglobal(L, "debug");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return 1;
}
lua_getfield(L, -1, "traceback");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2);
return 1;
}
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
return 1;
}
static int
atpanic(lua_State *L)
{
const char *s;
size_t len;
s = NULL;
if (lua_type(L, -1) == LUA_TSTRING) {
s = lua_tolstring(L, -1, &len);
}
if (s == NULL) {
s = "unknown reason";
len = sizeof("unknown reason") - 1;
}
LOG_E("VMOD[lua] lua atpanic: Lua VM crashed, reason: %*s\n", (int) len, s);
longjmp(jmpbuffer, 1);
/* never get here */
return 1;
}
static void
make_thread_key()
{
pthread_key_create(&thread_vm_key, free_lua_state);
pthread_key_create(&request_vm_key, free_lua_state);
}
static lua_State*
new_lua_state(const char *path, const char *cpath)
{
lua_State *L;
const char *old_path;
const char *new_path;
const char *old_cpath;
const char *new_cpath;
L = luaL_newstate();
if (L == NULL) {
LOG_E("VMOD[lua] create new VM failed\n");
goto failed;
}
luaL_openlibs(L);
lua_getglobal(L, "package");
if (!lua_istable(L, -1)) {
LOG_E("VMOD[lua] the \"package\" table does not exist\n");
goto failed;
}
/* set path */
lua_pushlstring(L, path, strlen(path));
lua_pushstring(L, ";");
lua_getfield(L, -3, "path");
old_path = lua_tostring(L, -1);
LOG_T("VMOD[lua] old path: %s\n", old_path);
lua_concat(L, 3);
new_path = lua_tostring(L, -1);
LOG_T("VMOD[lua] new path: %s\n", new_path);
lua_setfield(L, -2, "path");
/* set cpath */
lua_pushlstring(L, cpath, strlen(cpath));
lua_pushstring(L, ";");
lua_getfield(L, -3, "cpath");
old_cpath = lua_tostring(L, -1);
LOG_T("VMOD[lua] old cpath: %s\n", old_cpath);
lua_concat(L, 3);
new_cpath = lua_tostring(L, -1);
LOG_T("VMOD[lua] new cpath: %s\n", new_cpath);
lua_setfield(L, -2, "cpath");
lua_pop(L, 1); /* rmeove the "package" table */
inject_varnish(L);
return L;
failed:
if (L) {
lua_close(L);
}
return NULL;
}
static lua_State *
new_lua_thread(lua_State *BL)
{
lua_State *L;
L = lua_newthread(BL);
lua_createtable(L, 0, 0);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "_G");
lua_createtable(L, 0, 1);
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_replace(L, LUA_GLOBALSINDEX);
return L;
}
static void
free_lua_state(void *L)
{
if (L) {
lua_close((lua_State *)L);
}
}
static void
free_config(lua_config_t *cfg)
{
if (cfg) {
lua_close(cfg->L);
free(cfg);
}
}
static struct vrt_ctx*
get_ctx(lua_State *L)
{
struct vrt_ctx *ctx;
lua_getglobal(L, VARNISH_CONTEXT_KEY);
ctx = lua_touserdata(L, -1);
lua_pop(L, 1);
return ctx;
}
static void
set_ctx(lua_State *L, const struct vrt_ctx *ctx)
{
lua_pushlightuserdata(L, (void *)ctx);
lua_setglobal(L, VARNISH_CONTEXT_KEY);
}
static unsigned int
get_xid(lua_State *L)
{
unsigned int xid;
lua_getglobal(L, VARNISH_XID_KEY);
xid = (unsigned int) lua_tointeger(L, -1);
lua_pop(L, 1);
return xid;
}
static void
set_xid(lua_State *L, unsigned int xid)
{
lua_pushinteger(L, (lua_Integer) xid);
lua_setglobal(L, VARNISH_XID_KEY);
}
static int
vcl_var_get(lua_State *L, var_handler_t *vh)
{
struct vrt_ctx *ctx;
const char *p;
size_t len;
ctx = get_ctx(L);
if (ctx == NULL) {
return luaL_error(L, "no session object found");
}
if (lua_type(L, -1) != LUA_TSTRING) {
return luaL_error(L, "bad variable name");
}
p = lua_tolstring(L, -1, &len);
for (; vh->name; vh++) {
if (len == vh->len && !strcmp(p, vh->name)) {
return vh->handler(L, ctx);
}
}
lua_pushnil(L);
return 1;
}
static int
vcl_forbidden_set(lua_State *L)
{
return luaL_error(L, "table is read only");
}
static void
inject_bereq(lua_State *L)
{
lua_newtable(L); /* table varnish.bereq.* */
/* table varnish.bereq.http.* */
inject_bereq_http(L);
lua_createtable(L, 0, 2); /* metatable */
lua_pushcfunction(L, vcl_bereq_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, vcl_forbidden_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "bereq");
}
static void
inject_bereq_http(lua_State *L)
{
lua_newtable(L); /* varnish.bereq.http table */
lua_createtable(L, 0, 2); /* metatable */
lua_pushcfunction(L, vcl_bereq_http_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, vcl_forbidden_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2); /* tie the metatable to http table */
lua_setfield(L, -2, "http");
}
static int
vcl_bereq_get(lua_State *L)
{
return vcl_var_get(L, bereq_handlers);
}
static void
inject_beresp(lua_State *L)
{
lua_newtable(L); /* table varnish.beresp.* */
/* table varnish.beresp.http.* */
inject_beresp_http(L);
/* table varnish.beresp.backend.* */
inject_beresp_backend(L);
lua_createtable(L, 0, 2); /* metatable */
lua_pushcfunction(L, vcl_beresp_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, vcl_forbidden_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "beresp");
}
static void
inject_beresp_http(lua_State *L)
{
lua_newtable(L); /* varnish.beresp.http table */
lua_createtable(L, 0, 2); /* metatable */
lua_pushcfunction(L, vcl_beresp_http_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, vcl_forbidden_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2); /* tie the metatable to http table */
lua_setfield(L, -2, "http");
}
static void
inject_beresp_backend(lua_State *L)
{
lua_newtable(L); /* table varnish.resp.backend.* */
lua_createtable(L, 0, 2); /* metatable */
lua_pushcfunction(L, vcl_beresp_backend_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, vcl_forbidden_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2); /* tie the metatable to http table */
lua_setfield(L, -2, "backend");
}
static int
vcl_beresp_backend_get(lua_State *L)
{
return vcl_var_get(L, beresp_backend_handlers);
}
static int
vcl_beresp_get(lua_State *L)
{
return vcl_var_get(L, beresp_handlers);
}
static void
inject_client(lua_State *L)
{
lua_newtable(L); /* table varnish.client.* */
lua_createtable(L, 0, 2); /* metatable */
lua_pushcfunction(L, vcl_client_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, vcl_forbidden_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "client");
}
static int
vcl_client_get(lua_State *L)
{
return vcl_var_get(L, client_handlers);
}
static void
inject_local(lua_State *L)
{
lua_newtable(L); /* table varnish.local.* */
lua_createtable(L, 0, 2); /* metatable */
lua_pushcfunction(L, vcl_local_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, vcl_forbidden_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "local");
}
static int
vcl_local_get(lua_State *L)
{
return vcl_var_get(L, local_handlers);
}
static void
inject_obj(lua_State *L)
{
lua_newtable(L); /* table varnish.obj.* */
/* table varnish.obj.http.* */
inject_obj_http(L);
lua_createtable(L, 0, 2); /* metatable */
lua_pushcfunction(L, vcl_obj_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, vcl_forbidden_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "obj");
}